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 | */ |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 12 | #include "libmtp.h" |
| 13 | #include "libusb-glue.h" |
| 14 | #include "util.h" |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 15 | #include "ptp.h" |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 16 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 21 | #include <usb.h> |
| 22 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 23 | #include "ptp-pack.c" |
| 24 | |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 25 | /* To enable debug prints, switch on this */ |
| 26 | //#define ENABLE_USB_BULK_DEBUG |
| 27 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 28 | /* OUR APPLICATION USB URB (2MB) ;) */ |
| 29 | #define PTPCAM_USB_URB 2097152 |
| 30 | |
| 31 | /* this must not be too short - the original 4000 was not long |
| 32 | enough for big file transfers. I imagine the player spends a |
| 33 | bit of time gearing up to receiving lots of data. This also makes |
| 34 | connecting/disconnecting more reliable */ |
| 35 | #define USB_TIMEOUT 10000 |
| 36 | #define USB_CAPTURE_TIMEOUT 20000 |
| 37 | |
| 38 | /* USB control message data phase direction */ |
| 39 | #ifndef USB_DP_HTD |
| 40 | #define USB_DP_HTD (0x00 << 7) /* host to device */ |
| 41 | #endif |
| 42 | #ifndef USB_DP_DTH |
| 43 | #define USB_DP_DTH (0x01 << 7) /* device to host */ |
| 44 | #endif |
| 45 | |
| 46 | /* USB Feature selector HALT */ |
| 47 | #ifndef USB_FEATURE_HALT |
| 48 | #define USB_FEATURE_HALT 0x00 |
| 49 | #endif |
| 50 | |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 51 | /* |
Linus Walleij | e8c5464 | 2006-03-28 09:45:00 +0000 | [diff] [blame] | 52 | * MTP device list, trying real bad to get all devices into |
| 53 | * this list by stealing from everyone I know. |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 54 | */ |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 55 | static const LIBMTP_device_entry_t mtp_device_table[] = { |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Creative Technology |
Linus Walleij | cc4dd48 | 2006-11-25 21:58:30 +0000 | [diff] [blame] | 59 | * Initially the Creative devices was all we supported so these are |
| 60 | * the most thoroughly tested devices. |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 61 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 62 | { "Creative Zen Vision", 0x041e, 0x411f, DEVICE_FLAG_NONE }, |
| 63 | { "Creative Portable Media Center", 0x041e, 0x4123, DEVICE_FLAG_NONE }, |
| 64 | { "Creative Zen Xtra (MTP mode)", 0x041e, 0x4128, DEVICE_FLAG_NONE }, |
| 65 | { "Second generation Dell DJ", 0x041e, 0x412f, DEVICE_FLAG_NONE }, |
| 66 | { "Creative Zen Micro (MTP mode)", 0x041e, 0x4130, DEVICE_FLAG_NONE }, |
| 67 | { "Creative Zen Touch (MTP mode)", 0x041e, 0x4131, DEVICE_FLAG_NONE }, |
| 68 | { "Dell Pocket DJ (MTP mode)", 0x041e, 0x4132, DEVICE_FLAG_NONE }, |
| 69 | { "Creative Zen Sleek (MTP mode)", 0x041e, 0x4137, DEVICE_FLAG_NONE }, |
| 70 | { "Creative Zen MicroPhoto", 0x041e, 0x413c, DEVICE_FLAG_NONE }, |
| 71 | { "Creative Zen Sleek Photo", 0x041e, 0x413d, DEVICE_FLAG_NONE }, |
| 72 | { "Creative Zen Vision:M", 0x041e, 0x413e, DEVICE_FLAG_NONE }, |
Linus Walleij | bdd006b | 2006-09-15 04:54:13 +0000 | [diff] [blame] | 73 | // Reported by marazm@o2.pl |
| 74 | { "Creative Zen V", 0x041e, 0x4150, DEVICE_FLAG_NONE }, |
Linus Walleij | 92a8496 | 2006-11-11 20:04:37 +0000 | [diff] [blame] | 75 | // Reported by danielw@iinet.net.au |
| 76 | { "Creative Zen Vision:M (DVP-HD0004)", 0x041e, 0x4151, DEVICE_FLAG_NONE }, |
Linus Walleij | 550bc67 | 2006-10-10 11:11:29 +0000 | [diff] [blame] | 77 | // Reported by Darel on the XNJB forums |
Richard Low | e1f0692 | 2006-11-12 16:38:39 +0000 | [diff] [blame] | 78 | { "Creative Zen V Plus", 0x041e, 0x4152, DEVICE_FLAG_NONE }, |
| 79 | { "Creative Zen Vision W", 0x041e, 0x4153, 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 | 19be421 | 2006-11-16 09:00:48 +0000 | [diff] [blame] | 95 | // Reported by Patrick <skibler@gmail.com> |
Linus Walleij | abdf607 | 2006-11-24 07:34:02 +0000 | [diff] [blame] | 96 | { "Samsung YP-K5", 0x04e8, 0x505a, DEVICE_FLAG_NONE }, |
Linus Walleij | a5073c7 | 2006-11-24 07:55:12 +0000 | [diff] [blame] | 97 | // Reported by Matthew Wilcox <matthew@wil.cx> |
Richard Low | c046fdf | 2006-11-24 09:19:33 +0000 | [diff] [blame] | 98 | { "Samsung Yepp T9", 0x04e8, 0x507f, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 99 | // From a rouge .INF file |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 100 | { "Samsung YH-999 Portable Media Center", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 101 | |
| 102 | /* |
| 103 | * Intel |
| 104 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 105 | { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * JVC |
| 109 | */ |
| 110 | // From Mark Veinot |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 111 | { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 112 | |
| 113 | /* |
Linus Walleij | 093ca0a | 2006-11-02 21:15:00 +0000 | [diff] [blame] | 114 | * Philips |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 115 | */ |
| 116 | // From libgphoto2 source |
Linus Walleij | 093ca0a | 2006-11-02 21:15:00 +0000 | [diff] [blame] | 117 | { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE }, |
Linus Walleij | bf7fc27 | 2006-11-10 21:35:06 +0000 | [diff] [blame] | 118 | { "Philips HDD6320/00", 0x0471, 0x014b, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 119 | // Anonymous SourceForge user |
Linus Walleij | 093ca0a | 2006-11-02 21:15:00 +0000 | [diff] [blame] | 120 | { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE }, |
| 121 | // From Gerhard Mekenkamp |
| 122 | { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE }, |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 123 | // from XNJB forum |
| 124 | { "Philips GoGear SA9200", 0x0471, 0x014f, DEVICE_FLAG_NONE }, |
Richard Low | a98d054 | 2007-01-20 12:31:07 +0000 | [diff] [blame^] | 125 | // from XNJB user |
| 126 | { "Philips PSA235", 0x0471, 0x7e01, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * SanDisk |
| 130 | */ |
| 131 | // Reported by Brian Robison |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 132 | { "SanDisk Sansa m240", 0x0781, 0x7400, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 133 | // Reported by tangent_@users.sourceforge.net |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 134 | { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 135 | // From libgphoto2 source |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 136 | { "SanDisk Sansa e200", 0x0781, 0x7420, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 137 | // Reported by gonkflea@users.sourceforge.net |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 138 | { "SanDisk Sansa e260", 0x0781, 0x7420, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * iRiver |
Linus Walleij | 1a1b2d1 | 2006-11-23 13:32:17 +0000 | [diff] [blame] | 142 | * we assume that PTP_OC_MTP_GetObjPropList is essentially |
Linus Walleij | fabffc0 | 2006-12-17 00:18:57 +0000 | [diff] [blame] | 143 | * broken on all iRiver devices, meaning it simply won't return |
| 144 | * all properties for a file when asking for metadata 0xffffffff. |
| 145 | * Please test on your device if you believe it isn't broken! |
| 146 | * Some devices from http://www.mtp-ums.net/viewdeviceinfo.php |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 147 | */ |
Linus Walleij | 1a1b2d1 | 2006-11-23 13:32:17 +0000 | [diff] [blame] | 148 | { "iRiver Portable Media Center", 0x1006, 0x4002, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
| 149 | { "iRiver Portable Media Center", 0x1006, 0x4003, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 150 | // From libgphoto2 source |
Linus Walleij | 1a1b2d1 | 2006-11-23 13:32:17 +0000 | [diff] [blame] | 151 | { "iRiver T10", 0x4102, 0x1113, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
| 152 | { "iRiver T20 FM", 0x4102, 0x1114, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
Linus Walleij | fabffc0 | 2006-12-17 00:18:57 +0000 | [diff] [blame] | 153 | // This appears at the MTP-UMS site |
| 154 | { "iRiver T20", 0x4102, 0x1115, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
Linus Walleij | 1a1b2d1 | 2006-11-23 13:32:17 +0000 | [diff] [blame] | 155 | { "iRiver U10", 0x4102, 0x1116, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
| 156 | { "iRiver T10", 0x4102, 0x1117, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
| 157 | { "iRiver T20", 0x4102, 0x1118, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
| 158 | { "iRiver T30", 0x4102, 0x1119, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
Linus Walleij | 77e047c | 2006-09-11 19:09:06 +0000 | [diff] [blame] | 159 | // Reported by David Wolpoff |
Linus Walleij | 1a1b2d1 | 2006-11-23 13:32:17 +0000 | [diff] [blame] | 160 | { "iRiver T10 2GB", 0x4102, 0x1120, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
Linus Walleij | 8709981 | 2006-12-17 00:29:58 +0000 | [diff] [blame] | 161 | // Rough guess this is the MTP device ID... |
| 162 | { "iRiver N12", 0x4102, 0x1122, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
Linus Walleij | 04377da | 2006-08-30 13:03:35 +0000 | [diff] [blame] | 163 | // Reported by Adam Torgerson |
Linus Walleij | 1a1b2d1 | 2006-11-23 13:32:17 +0000 | [diff] [blame] | 164 | { "iRiver Clix", 0x4102, 0x112a, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 165 | // Reported by Scott Call |
Linus Walleij | 1a1b2d1 | 2006-11-23 13:32:17 +0000 | [diff] [blame] | 166 | { "iRiver H10 20GB", 0x4102, 0x2101, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
| 167 | { "iRiver H10", 0x4102, 0x2102, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * Dell |
| 171 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 172 | { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * Toshiba |
| 176 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 177 | { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE }, |
| 178 | { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE }, |
Linus Walleij | 405e239 | 2006-10-23 07:13:49 +0000 | [diff] [blame] | 179 | // From libgphoto2 |
| 180 | { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE }, |
Linus Walleij | 402e4bd | 2006-09-12 07:50:56 +0000 | [diff] [blame] | 181 | // Reported by Rob Brown |
| 182 | { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE }, |
Linus Walleij | b01d18b | 2006-12-13 12:49:15 +0000 | [diff] [blame] | 183 | |
Linus Walleij | 6f68056 | 2006-08-19 22:49:33 +0000 | [diff] [blame] | 184 | /* |
| 185 | * Archos |
| 186 | */ |
| 187 | // Reported by gudul1@users.sourceforge.net |
Linus Walleij | e543090 | 2006-10-08 14:46:03 +0000 | [diff] [blame] | 188 | { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE }, |
| 189 | |
| 190 | /* |
Linus Walleij | 2a9d2f0 | 2006-10-15 20:44:27 +0000 | [diff] [blame] | 191 | * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad |
Linus Walleij | 550bc67 | 2006-10-10 11:11:29 +0000 | [diff] [blame] | 192 | * This unit is falsely detected as USB mass storage in Linux |
Linus Walleij | cc4dd48 | 2006-11-25 21:58:30 +0000 | [diff] [blame] | 193 | * prior to kernel 2.6.19 (fixed by patch from Alan Stern) |
| 194 | * so on older kernels special care is needed to remove the |
| 195 | * USB mass storage driver that erroneously binds to the device |
| 196 | * interface. |
Linus Walleij | e543090 | 2006-10-08 14:46:03 +0000 | [diff] [blame] | 197 | */ |
Richard Low | c7d48d2 | 2006-11-29 18:24:55 +0000 | [diff] [blame] | 198 | { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER}, |
| 199 | |
| 200 | /* |
| 201 | * Microsoft |
| 202 | */ |
| 203 | // Reported by Farooq Zaman |
| 204 | { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE }, |
| 205 | |
| 206 | /* |
| 207 | * Sirius |
| 208 | */ |
Linus Walleij | b01d18b | 2006-12-13 12:49:15 +0000 | [diff] [blame] | 209 | { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE }, |
| 210 | |
| 211 | /* |
| 212 | * Canon |
| 213 | * This is actually a camera, but it has a Microsoft device descriptor |
| 214 | * and reports itself as supporting the MTP extension. |
| 215 | */ |
| 216 | {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE } |
Linus Walleij | e543090 | 2006-10-08 14:46:03 +0000 | [diff] [blame] | 217 | |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 218 | }; |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 219 | 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] | 220 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 221 | int ptpcam_usb_timeout = USB_TIMEOUT; |
| 222 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 223 | // Local functions |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 224 | static struct usb_bus* init_usb(); |
| 225 | static struct usb_device *probe_usb_bus_for_mtp_devices(void); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 226 | static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber); |
| 227 | static struct usb_device* find_device (int busn, int devicen, short force); |
| 228 | static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep); |
| 229 | static void clear_stall(PTP_USB* ptp_usb); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 230 | 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] | 231 | static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 232 | 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] | 233 | static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep); |
| 234 | 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] | 235 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 236 | |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 237 | int get_device_list(LIBMTP_device_entry_t ** const devices, int * const numdevs) |
| 238 | { |
| 239 | *devices = (LIBMTP_device_entry_t *) &mtp_device_table; |
| 240 | *numdevs = mtp_device_table_size; |
| 241 | return 0; |
| 242 | } |
| 243 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 244 | static struct usb_bus* init_usb() |
| 245 | { |
| 246 | usb_init(); |
| 247 | usb_find_busses(); |
| 248 | usb_find_devices(); |
| 249 | return (usb_get_busses()); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Check for the Microsoft OS device descriptor and returns device struct |
| 254 | * if the device is MTP-compliant. The function will only recognize |
| 255 | * a single device connected to the USB bus. |
| 256 | * |
| 257 | * @return an MTP-compliant USB device if one was found, else NULL. |
| 258 | */ |
| 259 | static struct usb_device *probe_usb_bus_for_mtp_devices(void) |
| 260 | { |
| 261 | struct usb_bus *bus; |
| 262 | |
| 263 | bus = init_usb(); |
| 264 | for (; bus; bus = bus->next) { |
| 265 | struct usb_device *dev; |
| 266 | |
| 267 | for (dev = bus->devices; dev; dev = dev->next) { |
| 268 | usb_dev_handle *devh; |
| 269 | unsigned char buf[1024], cmd; |
| 270 | int ret; |
| 271 | |
| 272 | devh = usb_open(dev); |
| 273 | if (devh == NULL) { |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | // Read the special descripor, if possible... |
| 278 | ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf)); |
| 279 | |
| 280 | if (ret < 10) { |
| 281 | // printf("Device: VID %04x/PID %04x: no extended device property...\n", |
| 282 | // dev->descriptor.idVendor, |
| 283 | // dev->descriptor.idProduct); |
| 284 | usb_close(devh); |
| 285 | continue; |
| 286 | } |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 287 | |
| 288 | // It is atleast 10 bytes... |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 289 | 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] | 290 | printf("This is not a Microsoft MTP descriptor...\n"); |
| 291 | printf("Device response to read device property 0xee:\n"); |
| 292 | data_dump_ascii (stdout, buf, ret, 0); |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 293 | usb_close(devh); |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | cmd = buf[16]; |
| 298 | ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, |
| 299 | cmd, 0, 4, (char *) buf, sizeof(buf), 1000); |
| 300 | if (ret == -1) { |
| 301 | //printf("Decice could not respond to control message 1.\n"); |
| 302 | usb_close(devh); |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 303 | continue; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 306 | if (ret > 0x15) { |
| 307 | if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) { |
| 308 | printf("The device has a Microsoft device descriptor, but it's not MTP.\n"); |
| 309 | printf("This is not an MTP device. Presumable it is USB mass storage\n"); |
| 310 | printf("with some additional Janus (DRM) support.\n"); |
| 311 | printf("Device response to control message 1:\n"); |
| 312 | data_dump_ascii (stdout, buf, ret, 0); |
| 313 | continue; |
| 314 | } |
| 315 | } else { |
| 316 | // Not MTP or broken |
| 317 | continue; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, |
| 321 | cmd, 0, 5, (char *) buf, sizeof(buf), 1000); |
| 322 | if (ret == -1) { |
| 323 | //printf("Device could not respond to control message 2.\n"); |
| 324 | usb_close(devh); |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 325 | // Return the device anyway, it said previously it was MTP, right? |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 326 | return dev; |
| 327 | } |
| 328 | |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 329 | if (ret > 0x15) { |
| 330 | if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) { |
| 331 | printf("This device does not respond with MTP characteristics on\n"); |
| 332 | printf("the second device property read (0x05), but will be regarded\n"); |
| 333 | printf("as MTP anyway.\n"); |
| 334 | printf("Device response to control message 2:\n"); |
| 335 | data_dump_ascii (stdout, buf, ret, 0); |
| 336 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 337 | } |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 338 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 339 | usb_close(devh); |
| 340 | // We can return the device here, it will be the first. |
| 341 | // If it was not MTP, the loop continues before it reaches this point. |
| 342 | return dev; |
| 343 | } |
| 344 | } |
| 345 | // If nothing was found we end up here. |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Detect the MTP device descriptor and return the VID and PID |
| 351 | * of the first device found. This is a very low-level function |
| 352 | * which is intended for use with <b>udev</b> or other hotplug |
| 353 | * mechanisms. The idea is that a script may want to know if the |
| 354 | * just plugged-in device was an MTP device or not. |
| 355 | * @param vid the Vendor ID (VID) of the first device found. |
| 356 | * @param pid the Product ID (PID) of the first device found. |
| 357 | * @return the number of detected devices or -1 if the call |
| 358 | * was unsuccessful. |
| 359 | */ |
| 360 | int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid) |
| 361 | { |
| 362 | struct usb_device *dev = probe_usb_bus_for_mtp_devices(); |
| 363 | if (dev == NULL) { |
| 364 | return 0; |
| 365 | } |
| 366 | *vid = dev->descriptor.idVendor; |
| 367 | *pid = dev->descriptor.idProduct; |
| 368 | return 1; |
| 369 | } |
| 370 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 371 | /** |
| 372 | * This routine just dumps out low-level |
| 373 | * USB information about the current device. |
| 374 | * @param ptp_usb the USB device to get information from. |
| 375 | */ |
| 376 | void dump_usbinfo(PTP_USB *ptp_usb) |
| 377 | { |
| 378 | int res; |
| 379 | struct usb_device *dev; |
| 380 | |
| 381 | #ifdef LIBUSB_HAS_GET_DRIVER_NP |
| 382 | char devname[0x10]; |
| 383 | |
| 384 | devname[0] = '\0'; |
| 385 | res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname)); |
| 386 | if (devname[0] != '\0') { |
| 387 | printf(" Using kernel interface \"%s\"\n", devname); |
| 388 | } |
| 389 | #endif |
| 390 | dev = usb_device(ptp_usb->handle); |
| 391 | printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB); |
| 392 | printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass); |
| 393 | printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass); |
| 394 | printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol); |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 395 | printf(" idVendor: %04x\n", dev->descriptor.idVendor); |
| 396 | printf(" idProduct: %04x\n", dev->descriptor.idProduct); |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 397 | printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket); |
| 398 | printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket); |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 399 | printf(" Device flags: 0x%08x\n", ptp_usb->device_flags); |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 400 | // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber... |
| 401 | } |
| 402 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 403 | static void |
| 404 | ptp_debug (PTPParams *params, const char *format, ...) |
| 405 | { |
| 406 | va_list args; |
| 407 | |
| 408 | va_start (args, format); |
| 409 | if (params->debug_func!=NULL) |
| 410 | params->debug_func (params->data, format, args); |
| 411 | else |
| 412 | { |
| 413 | vfprintf (stderr, format, args); |
| 414 | fprintf (stderr,"\n"); |
| 415 | fflush (stderr); |
| 416 | } |
| 417 | va_end (args); |
| 418 | } |
| 419 | |
| 420 | static void |
| 421 | ptp_error (PTPParams *params, const char *format, ...) |
| 422 | { |
| 423 | va_list args; |
| 424 | |
| 425 | va_start (args, format); |
| 426 | if (params->error_func!=NULL) |
| 427 | params->error_func (params->data, format, args); |
| 428 | else |
| 429 | { |
| 430 | vfprintf (stderr, format, args); |
| 431 | fprintf (stderr,"\n"); |
| 432 | fflush (stderr); |
| 433 | } |
| 434 | va_end (args); |
| 435 | } |
| 436 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 437 | |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 438 | /* |
| 439 | * ptp_read_func() and ptp_write_func() are |
| 440 | * based on same functions in library.c in libgphoto2. |
| 441 | * Much reading packet logs and having fun with trials and errors |
| 442 | * reveals that WMP / Windows is probably using an algorithm like this |
| 443 | * for large transfers: |
| 444 | * |
| 445 | * 1. Send the command (0x0c bytes) if headers are split, else, send |
| 446 | * command plus sizeof(endpoint) - 0x0c bytes. |
| 447 | * 2. Send first packet, max size to be sizeof(endpoint) but only when using |
| 448 | * split headers. Else goto 3. |
| 449 | * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000 |
| 450 | * We call 0x10000 CONTEXT_BLOCK_SIZE. |
| 451 | * 4. Send remaining bytes MOD sizeof(endpoint) |
| 452 | * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint) |
| 453 | * then also send a zero-length package. |
| 454 | */ |
Richard Low | 021421e | 2007-01-01 18:08:57 +0000 | [diff] [blame] | 455 | #define CONTEXT_BLOCK_SIZE 0x10000 |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 456 | static short |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 457 | ptp_read_func ( |
| 458 | unsigned long size, PTPDataHandler *handler,void *data, |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 459 | unsigned long *readbytes, |
| 460 | int readzero |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 461 | ) { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 462 | PTP_USB *ptp_usb = (PTP_USB *)data; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 463 | unsigned long toread = 0; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 464 | int result = 0; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 465 | unsigned long curread = 0; |
| 466 | unsigned long written; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 467 | unsigned char *bytes; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 468 | |
| 469 | // This is the largest block we'll need to read in. |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 470 | bytes = malloc(CONTEXT_BLOCK_SIZE); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 471 | while (curread < size) { |
| 472 | toread = size - curread; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 473 | if (toread > CONTEXT_BLOCK_SIZE) { |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 474 | toread = CONTEXT_BLOCK_SIZE; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 475 | } else if (toread > ptp_usb->outep_maxpacket) { |
Richard Low | b89ea94 | 2007-01-02 11:47:19 +0000 | [diff] [blame] | 476 | toread -= toread % ptp_usb->outep_maxpacket; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 477 | } |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 478 | |
| 479 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 480 | |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 481 | if (result < 0) { |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 482 | return PTP_ERROR_IO; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 483 | } |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 484 | #ifdef ENABLE_USB_BULK_DEBUG |
| 485 | printf("<==USB IN\n"); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 486 | if (result == 0) |
| 487 | printf("Zero Read\n"); |
| 488 | else |
| 489 | data_dump_ascii (stdout,bytes,result,16); |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 490 | #endif |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 491 | handler->putfunc(NULL, handler->private, result, bytes, &written); |
| 492 | ptp_usb->current_transfer_complete += result; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 493 | curread += result; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 494 | |
| 495 | // Increase counters, call callback |
| 496 | if (ptp_usb->callback_active) { |
| 497 | if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) { |
| 498 | // send last update and disable callback. |
| 499 | ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total; |
| 500 | ptp_usb->callback_active = 0; |
| 501 | } |
| 502 | if (ptp_usb->current_transfer_callback != NULL) { |
| 503 | (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete, |
| 504 | ptp_usb->current_transfer_total, |
| 505 | ptp_usb->current_transfer_callback_data); |
| 506 | } |
| 507 | } |
| 508 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 509 | if (result < toread) /* short reads are common */ |
| 510 | break; |
| 511 | } |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 512 | if (readbytes) *readbytes = curread; |
| 513 | free (bytes); |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 514 | |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 515 | // there might be a zero packet waiting for us... |
| 516 | if (readzero && curread % ptp_usb->outep_maxpacket == 0) { |
| 517 | char temp; |
| 518 | int zeroresult = 0; |
| 519 | #ifdef ENABLE_USB_BULK_DEBUG |
| 520 | printf("<==USB IN\n"); |
| 521 | printf("Zero Read\n"); |
| 522 | #endif |
| 523 | zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptpcam_usb_timeout); |
| 524 | if (zeroresult != 0) |
| 525 | printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult); |
| 526 | } |
| 527 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 528 | if (result > 0) { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 529 | return (PTP_RC_OK); |
| 530 | } else { |
| 531 | return PTP_ERROR_IO; |
| 532 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 535 | static short |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 536 | ptp_write_func ( |
| 537 | unsigned long size, |
| 538 | PTPDataHandler *handler, |
| 539 | void *data, |
| 540 | unsigned long *written |
| 541 | ) { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 542 | PTP_USB *ptp_usb = (PTP_USB *)data; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 543 | unsigned long towrite = 0; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 544 | int result = 0; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 545 | unsigned long curwrite = 0; |
| 546 | unsigned char *bytes; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 547 | |
| 548 | // This is the largest block we'll need to read in. |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 549 | bytes = malloc(CONTEXT_BLOCK_SIZE); |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 550 | if (!bytes) { |
| 551 | return PTP_ERROR_IO; |
| 552 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 553 | while (curwrite < size) { |
| 554 | towrite = size-curwrite; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 555 | if (towrite > CONTEXT_BLOCK_SIZE) { |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 556 | towrite = CONTEXT_BLOCK_SIZE; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 557 | } else { |
| 558 | // This magic makes packets the same size that WMP send them. |
| 559 | if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) { |
Richard Low | 021421e | 2007-01-01 18:08:57 +0000 | [diff] [blame] | 560 | towrite -= towrite % ptp_usb->outep_maxpacket; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | handler->getfunc(NULL, handler->private,towrite,bytes,&towrite); |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 564 | result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout); |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 565 | #ifdef ENABLE_USB_BULK_DEBUG |
| 566 | printf("USB OUT==>\n"); |
Richard Low | edd6183 | 2006-12-30 12:38:18 +0000 | [diff] [blame] | 567 | data_dump_ascii (stdout,bytes,towrite,16); |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 568 | #endif |
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 | } |
| 572 | // Increase counters |
| 573 | ptp_usb->current_transfer_complete += result; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 574 | curwrite += result; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 575 | |
| 576 | // call callback |
| 577 | if (ptp_usb->callback_active) { |
| 578 | if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) { |
| 579 | // send last update and disable callback. |
| 580 | ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total; |
| 581 | ptp_usb->callback_active = 0; |
| 582 | } |
| 583 | if (ptp_usb->current_transfer_callback != NULL) { |
| 584 | (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete, |
| 585 | ptp_usb->current_transfer_total, |
| 586 | ptp_usb->current_transfer_callback_data); |
| 587 | } |
| 588 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 589 | if (result < towrite) /* short writes happen */ |
| 590 | break; |
| 591 | } |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 592 | free (bytes); |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 593 | if (written) { |
| 594 | *written = curwrite; |
Linus Walleij | 80d134a | 2006-08-22 21:41:37 +0000 | [diff] [blame] | 595 | } |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 596 | |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 597 | |
Richard Low | ef05b89 | 2007-01-03 21:18:56 +0000 | [diff] [blame] | 598 | // If this is the last transfer send a zero write if required |
| 599 | if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) { |
Richard Low | 68f4588 | 2007-01-03 10:08:31 +0000 | [diff] [blame] | 600 | if ((towrite % ptp_usb->outep_maxpacket) == 0) { |
Richard Low | 021421e | 2007-01-01 18:08:57 +0000 | [diff] [blame] | 601 | #ifdef ENABLE_USB_BULK_DEBUG |
| 602 | printf("USB OUT==>\n"); |
| 603 | printf("Zero Write\n"); |
| 604 | #endif |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 605 | 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] | 606 | } |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 607 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 608 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 609 | if (result < 0) |
| 610 | return PTP_ERROR_IO; |
| 611 | return PTP_RC_OK; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 614 | /* memory data get/put handler */ |
| 615 | typedef struct { |
| 616 | unsigned char *data; |
| 617 | unsigned long size, curoff; |
| 618 | } PTPMemHandlerPrivate; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 619 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 620 | static uint16_t |
| 621 | memory_getfunc(PTPParams* params, void* private, |
| 622 | unsigned long wantlen, unsigned char *data, |
| 623 | unsigned long *gotlen |
| 624 | ) { |
| 625 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private; |
| 626 | unsigned long tocopy = wantlen; |
| 627 | |
| 628 | if (priv->curoff + tocopy > priv->size) |
| 629 | tocopy = priv->size - priv->curoff; |
| 630 | memcpy (data, priv->data + priv->curoff, tocopy); |
| 631 | priv->curoff += tocopy; |
| 632 | *gotlen = tocopy; |
| 633 | return PTP_RC_OK; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 636 | static uint16_t |
| 637 | memory_putfunc(PTPParams* params, void* private, |
| 638 | unsigned long sendlen, unsigned char *data, |
| 639 | unsigned long *putlen |
| 640 | ) { |
| 641 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private; |
| 642 | |
| 643 | if (priv->curoff + sendlen > priv->size) { |
| 644 | priv->data = realloc (priv->data, priv->curoff+sendlen); |
| 645 | priv->size = priv->curoff + sendlen; |
| 646 | } |
| 647 | memcpy (priv->data + priv->curoff, data, sendlen); |
| 648 | priv->curoff += sendlen; |
| 649 | *putlen = sendlen; |
| 650 | return PTP_RC_OK; |
| 651 | } |
| 652 | |
| 653 | /* init private struct for receiving data. */ |
| 654 | static uint16_t |
| 655 | ptp_init_recv_memory_handler(PTPDataHandler *handler) { |
| 656 | PTPMemHandlerPrivate* priv; |
| 657 | priv = malloc (sizeof(PTPMemHandlerPrivate)); |
| 658 | handler->private = priv; |
| 659 | handler->getfunc = memory_getfunc; |
| 660 | handler->putfunc = memory_putfunc; |
| 661 | priv->data = NULL; |
| 662 | priv->size = 0; |
| 663 | priv->curoff = 0; |
| 664 | return PTP_RC_OK; |
| 665 | } |
| 666 | |
| 667 | /* init private struct and put data in for sending data. |
| 668 | * data is still owned by caller. |
| 669 | */ |
| 670 | static uint16_t |
| 671 | ptp_init_send_memory_handler(PTPDataHandler *handler, |
| 672 | unsigned char *data, unsigned long len |
| 673 | ) { |
| 674 | PTPMemHandlerPrivate* priv; |
| 675 | priv = malloc (sizeof(PTPMemHandlerPrivate)); |
| 676 | if (!priv) |
| 677 | return PTP_RC_GeneralError; |
| 678 | handler->private = priv; |
| 679 | handler->getfunc = memory_getfunc; |
| 680 | handler->putfunc = memory_putfunc; |
| 681 | priv->data = data; |
| 682 | priv->size = len; |
| 683 | priv->curoff = 0; |
| 684 | return PTP_RC_OK; |
| 685 | } |
| 686 | |
| 687 | /* free private struct + data */ |
| 688 | static uint16_t |
| 689 | ptp_exit_send_memory_handler (PTPDataHandler *handler) { |
| 690 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private; |
| 691 | /* data is owned by caller */ |
| 692 | free (priv); |
| 693 | return PTP_RC_OK; |
| 694 | } |
| 695 | |
| 696 | /* hand over our internal data to caller */ |
| 697 | static uint16_t |
| 698 | ptp_exit_recv_memory_handler (PTPDataHandler *handler, |
| 699 | unsigned char **data, unsigned long *size |
| 700 | ) { |
| 701 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private; |
| 702 | *data = priv->data; |
| 703 | *size = priv->size; |
| 704 | free (priv); |
| 705 | return PTP_RC_OK; |
| 706 | } |
| 707 | |
| 708 | /* send / receive functions */ |
| 709 | |
| 710 | uint16_t |
| 711 | ptp_usb_sendreq (PTPParams* params, PTPContainer* req) |
| 712 | { |
| 713 | uint16_t ret; |
| 714 | PTPUSBBulkContainer usbreq; |
| 715 | PTPDataHandler memhandler; |
| 716 | unsigned long written, towrite; |
| 717 | |
| 718 | /* build appropriate USB container */ |
| 719 | usbreq.length=htod32(PTP_USB_BULK_REQ_LEN- |
| 720 | (sizeof(uint32_t)*(5-req->Nparam))); |
| 721 | usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND); |
| 722 | usbreq.code=htod16(req->Code); |
| 723 | usbreq.trans_id=htod32(req->Transaction_ID); |
| 724 | usbreq.payload.params.param1=htod32(req->Param1); |
| 725 | usbreq.payload.params.param2=htod32(req->Param2); |
| 726 | usbreq.payload.params.param3=htod32(req->Param3); |
| 727 | usbreq.payload.params.param4=htod32(req->Param4); |
| 728 | usbreq.payload.params.param5=htod32(req->Param5); |
| 729 | /* send it to responder */ |
| 730 | towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam)); |
| 731 | ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite); |
| 732 | ret=ptp_write_func( |
| 733 | towrite, |
| 734 | &memhandler, |
| 735 | params->data, |
| 736 | &written |
| 737 | ); |
| 738 | ptp_exit_send_memory_handler (&memhandler); |
| 739 | if (ret!=PTP_RC_OK) { |
| 740 | ret = PTP_ERROR_IO; |
| 741 | /* ptp_error (params, |
| 742 | "PTP: request code 0x%04x sending req error 0x%04x", |
| 743 | req->Code,ret); */ |
| 744 | } |
| 745 | if (written != towrite) { |
| 746 | ptp_error (params, |
| 747 | "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d", |
| 748 | written, towrite |
| 749 | ); |
| 750 | ret = PTP_ERROR_IO; |
| 751 | } |
| 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | uint16_t |
| 756 | ptp_usb_senddata (PTPParams* params, PTPContainer* ptp, |
| 757 | unsigned long size, PTPDataHandler *handler |
| 758 | ) { |
| 759 | uint16_t ret; |
| 760 | int wlen, datawlen; |
| 761 | unsigned long written; |
| 762 | PTPUSBBulkContainer usbdata; |
| 763 | uint32_t bytes_left_to_transfer; |
| 764 | PTPDataHandler memhandler; |
| 765 | |
| 766 | /* build appropriate USB container */ |
| 767 | usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size); |
| 768 | usbdata.type = htod16(PTP_USB_CONTAINER_DATA); |
| 769 | usbdata.code = htod16(ptp->Code); |
| 770 | usbdata.trans_id= htod32(ptp->Transaction_ID); |
| 771 | |
| 772 | ((PTP_USB*)params->data)->current_transfer_complete = 0; |
| 773 | ((PTP_USB*)params->data)->current_transfer_total = size; |
| 774 | |
| 775 | if (params->split_header_data) { |
| 776 | datawlen = 0; |
| 777 | wlen = PTP_USB_BULK_HDR_LEN; |
| 778 | } else { |
| 779 | unsigned long gotlen; |
| 780 | /* For all camera devices. */ |
| 781 | datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN)?size:PTP_USB_BULK_PAYLOAD_LEN; |
| 782 | wlen = PTP_USB_BULK_HDR_LEN + datawlen; |
| 783 | ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen); |
| 784 | if (ret != PTP_RC_OK) |
| 785 | return ret; |
| 786 | if (gotlen != datawlen) |
| 787 | return PTP_RC_GeneralError; |
| 788 | } |
| 789 | ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen); |
| 790 | /* send first part of data */ |
| 791 | ret = ptp_write_func(wlen, &memhandler, params->data, &written); |
| 792 | ptp_exit_send_memory_handler (&memhandler); |
| 793 | if (ret!=PTP_RC_OK) { |
| 794 | ret = PTP_ERROR_IO; |
| 795 | /* ptp_error (params, |
| 796 | "PTP: request code 0x%04x sending data error 0x%04x", |
| 797 | ptp->Code,ret);*/ |
| 798 | return ret; |
| 799 | } |
| 800 | if (size <= datawlen) return ret; |
| 801 | /* if everything OK send the rest */ |
| 802 | bytes_left_to_transfer = size-datawlen; |
| 803 | ret = PTP_RC_OK; |
| 804 | while(bytes_left_to_transfer > 0) { |
| 805 | ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written); |
| 806 | if (ret != PTP_RC_OK) |
| 807 | break; |
| 808 | if (written == 0) { |
| 809 | ret = PTP_ERROR_IO; |
| 810 | break; |
| 811 | } |
| 812 | bytes_left_to_transfer -= written; |
| 813 | } |
| 814 | if (ret!=PTP_RC_OK) |
| 815 | ret = PTP_ERROR_IO; |
| 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | static uint16_t ptp_usb_getpacket(PTPParams *params, |
| 820 | PTPUSBBulkContainer *packet, unsigned long *rlen) |
| 821 | { |
| 822 | PTPDataHandler memhandler; |
| 823 | uint16_t ret; |
| 824 | unsigned char *x = NULL; |
| 825 | |
| 826 | /* read the header and potentially the first data */ |
| 827 | if (params->response_packet_size > 0) { |
| 828 | /* If there is a buffered packet, just use it. */ |
| 829 | memcpy(packet, params->response_packet, params->response_packet_size); |
| 830 | *rlen = params->response_packet_size; |
| 831 | free(params->response_packet); |
| 832 | params->response_packet = NULL; |
| 833 | params->response_packet_size = 0; |
| 834 | /* Here this signifies a "virtual read" */ |
| 835 | return PTP_RC_OK; |
| 836 | } |
| 837 | ptp_init_recv_memory_handler (&memhandler); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 838 | ret = ptp_read_func( sizeof(*packet), &memhandler, params->data, rlen, 0); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 839 | ptp_exit_recv_memory_handler (&memhandler, &x, rlen); |
| 840 | if (x) { |
| 841 | memcpy (packet, x, *rlen); |
| 842 | free (x); |
| 843 | } |
| 844 | return ret; |
| 845 | } |
| 846 | |
| 847 | uint16_t |
| 848 | ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler) |
| 849 | { |
| 850 | uint16_t ret; |
| 851 | PTPUSBBulkContainer usbdata; |
| 852 | unsigned char *data; |
| 853 | unsigned long written; |
| 854 | |
| 855 | memset(&usbdata,0,sizeof(usbdata)); |
| 856 | do { |
| 857 | unsigned long len, rlen; |
| 858 | |
| 859 | ret = ptp_usb_getpacket(params, &usbdata, &rlen); |
| 860 | if (ret!=PTP_RC_OK) { |
| 861 | ret = PTP_ERROR_IO; |
| 862 | break; |
| 863 | } else |
| 864 | if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) { |
| 865 | ret = PTP_ERROR_DATA_EXPECTED; |
| 866 | break; |
| 867 | } else |
| 868 | if (dtoh16(usbdata.code)!=ptp->Code) { |
| 869 | ret = dtoh16(usbdata.code); |
| 870 | break; |
| 871 | } |
| 872 | if (usbdata.length == 0xffffffffU) { |
| 873 | /* stuff data directly to passed data handler */ |
| 874 | while (1) { |
| 875 | unsigned long readdata; |
| 876 | int xret; |
| 877 | |
| 878 | xret = ptp_read_func( |
| 879 | PTP_USB_BULK_HS_MAX_PACKET_LEN, |
| 880 | handler, |
| 881 | params->data, |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 882 | &readdata, |
| 883 | 0 |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 884 | ); |
| 885 | if (xret == -1) |
| 886 | return PTP_ERROR_IO; |
| 887 | if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN) |
| 888 | break; |
| 889 | } |
| 890 | return PTP_RC_OK; |
| 891 | } |
| 892 | if (rlen > dtoh32(usbdata.length)) { |
| 893 | /* |
| 894 | * Buffer the surplus response packet if it is >= |
| 895 | * PTP_USB_BULK_HDR_LEN |
| 896 | * (i.e. it is probably an entire package) |
| 897 | * else discard it as erroneous surplus data. |
| 898 | * This will even work if more than 2 packets appear |
| 899 | * in the same transaction, they will just be handled |
| 900 | * iteratively. |
| 901 | * |
| 902 | * Marcus observed stray bytes on iRiver devices; |
| 903 | * these are still discarded. |
| 904 | */ |
| 905 | unsigned int packlen = dtoh32(usbdata.length); |
| 906 | unsigned int surplen = rlen - packlen; |
| 907 | |
| 908 | if (surplen >= PTP_USB_BULK_HDR_LEN) { |
| 909 | params->response_packet = malloc(surplen); |
| 910 | memcpy(params->response_packet, |
| 911 | (uint8_t *) &usbdata + packlen, surplen); |
| 912 | params->response_packet_size = surplen; |
| 913 | } else { |
| 914 | ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes too much, expect problems!", rlen - dtoh32(usbdata.length)); |
| 915 | } |
| 916 | rlen = packlen; |
| 917 | } |
| 918 | |
| 919 | /* For most PTP devices rlen is 512 == sizeof(usbdata) |
| 920 | * here. For MTP devices splitting header and data it might |
| 921 | * be 12. |
| 922 | */ |
| 923 | /* Evaluate full data length. */ |
| 924 | len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN; |
| 925 | |
| 926 | /* autodetect split header/data MTP devices */ |
| 927 | if (dtoh32(usbdata.length) > 12 && (rlen==12)) |
| 928 | params->split_header_data = 1; |
| 929 | |
| 930 | data = malloc(PTP_USB_BULK_HS_MAX_PACKET_LEN); |
| 931 | /* Copy first part of data to 'data' */ |
| 932 | handler->putfunc( |
| 933 | params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data, |
| 934 | &written |
| 935 | ); |
| 936 | |
| 937 | /* Is that all of data? */ |
| 938 | if (len+PTP_USB_BULK_HDR_LEN<=rlen) break; |
| 939 | |
| 940 | /* If not read the rest of it. */ |
| 941 | ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN), |
| 942 | handler, |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 943 | params->data, &rlen, 1); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 944 | if (ret!=PTP_RC_OK) { |
| 945 | ret = PTP_ERROR_IO; |
| 946 | break; |
| 947 | } |
| 948 | } while (0); |
| 949 | /* |
| 950 | if (ret!=PTP_RC_OK) { |
| 951 | ptp_error (params, |
| 952 | "PTP: request code 0x%04x getting data error 0x%04x", |
| 953 | ptp->Code, ret); |
| 954 | }*/ |
| 955 | return ret; |
| 956 | } |
| 957 | |
| 958 | uint16_t |
| 959 | ptp_usb_getresp (PTPParams* params, PTPContainer* resp) |
| 960 | { |
| 961 | uint16_t ret; |
| 962 | unsigned long rlen; |
| 963 | PTPUSBBulkContainer usbresp; |
| 964 | |
| 965 | memset(&usbresp,0,sizeof(usbresp)); |
| 966 | /* read response, it should never be longer than sizeof(usbresp) */ |
| 967 | ret = ptp_usb_getpacket(params, &usbresp, &rlen); |
| 968 | |
| 969 | if (ret!=PTP_RC_OK) { |
| 970 | ret = PTP_ERROR_IO; |
| 971 | } else |
| 972 | if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) { |
| 973 | ret = PTP_ERROR_RESP_EXPECTED; |
| 974 | } else |
| 975 | if (dtoh16(usbresp.code)!=resp->Code) { |
| 976 | ret = dtoh16(usbresp.code); |
| 977 | } |
| 978 | if (ret!=PTP_RC_OK) { |
| 979 | /* ptp_error (params, |
| 980 | "PTP: request code 0x%04x getting resp error 0x%04x", |
| 981 | resp->Code, ret);*/ |
| 982 | return ret; |
| 983 | } |
| 984 | /* build an appropriate PTPContainer */ |
| 985 | resp->Code=dtoh16(usbresp.code); |
| 986 | resp->SessionID=params->session_id; |
| 987 | resp->Transaction_ID=dtoh32(usbresp.trans_id); |
| 988 | resp->Param1=dtoh32(usbresp.payload.params.param1); |
| 989 | resp->Param2=dtoh32(usbresp.payload.params.param2); |
| 990 | resp->Param3=dtoh32(usbresp.payload.params.param3); |
| 991 | resp->Param4=dtoh32(usbresp.payload.params.param4); |
| 992 | resp->Param5=dtoh32(usbresp.payload.params.param5); |
| 993 | return ret; |
| 994 | } |
| 995 | |
| 996 | /* Event handling functions */ |
| 997 | |
| 998 | /* PTP Events wait for or check mode */ |
| 999 | #define PTP_EVENT_CHECK 0x0000 /* waits for */ |
| 1000 | #define PTP_EVENT_CHECK_FAST 0x0001 /* checks */ |
| 1001 | |
| 1002 | static inline uint16_t |
| 1003 | ptp_usb_event (PTPParams* params, PTPContainer* event, int wait) |
| 1004 | { |
| 1005 | uint16_t ret; |
| 1006 | int result; |
| 1007 | unsigned long rlen; |
| 1008 | PTPUSBEventContainer usbevent; |
| 1009 | PTP_USB *ptp_usb = (PTP_USB *)(params->data); |
| 1010 | |
| 1011 | memset(&usbevent,0,sizeof(usbevent)); |
| 1012 | |
| 1013 | if ((params==NULL) || (event==NULL)) |
| 1014 | return PTP_ERROR_BADPARAM; |
| 1015 | ret = PTP_RC_OK; |
| 1016 | switch(wait) { |
| 1017 | case PTP_EVENT_CHECK: |
| 1018 | result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout); |
| 1019 | if (result==0) |
| 1020 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout); |
| 1021 | if (result < 0) ret = PTP_ERROR_IO; |
| 1022 | break; |
| 1023 | case PTP_EVENT_CHECK_FAST: |
| 1024 | result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout); |
| 1025 | if (result==0) |
| 1026 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout); |
| 1027 | if (result < 0) ret = PTP_ERROR_IO; |
| 1028 | break; |
| 1029 | default: |
| 1030 | ret=PTP_ERROR_BADPARAM; |
| 1031 | break; |
| 1032 | } |
| 1033 | if (ret!=PTP_RC_OK) { |
| 1034 | ptp_error (params, |
| 1035 | "PTP: reading event an error 0x%04x occurred", ret); |
| 1036 | return PTP_ERROR_IO; |
| 1037 | } |
| 1038 | rlen = result; |
| 1039 | if (rlen < 8) { |
| 1040 | ptp_error (params, |
| 1041 | "PTP: reading event an short read of %ld bytes occurred", rlen); |
| 1042 | return PTP_ERROR_IO; |
| 1043 | } |
| 1044 | /* if we read anything over interrupt endpoint it must be an event */ |
| 1045 | /* build an appropriate PTPContainer */ |
| 1046 | event->Code=dtoh16(usbevent.code); |
| 1047 | event->SessionID=params->session_id; |
| 1048 | event->Transaction_ID=dtoh32(usbevent.trans_id); |
| 1049 | event->Param1=dtoh32(usbevent.param1); |
| 1050 | event->Param2=dtoh32(usbevent.param2); |
| 1051 | event->Param3=dtoh32(usbevent.param3); |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
| 1055 | uint16_t |
| 1056 | ptp_usb_event_check (PTPParams* params, PTPContainer* event) { |
| 1057 | |
| 1058 | return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST); |
| 1059 | } |
| 1060 | |
| 1061 | uint16_t |
| 1062 | ptp_usb_event_wait (PTPParams* params, PTPContainer* event) { |
| 1063 | |
| 1064 | return ptp_usb_event (params, event, PTP_EVENT_CHECK); |
| 1065 | } |
| 1066 | |
| 1067 | |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1068 | 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] | 1069 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1070 | usb_dev_handle *device_handle; |
| 1071 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1072 | params->error_func=NULL; |
| 1073 | params->debug_func=NULL; |
| 1074 | params->sendreq_func=ptp_usb_sendreq; |
| 1075 | params->senddata_func=ptp_usb_senddata; |
| 1076 | params->getresp_func=ptp_usb_getresp; |
| 1077 | params->getdata_func=ptp_usb_getdata; |
| 1078 | params->data=ptp_usb; |
| 1079 | params->transaction_id=0; |
| 1080 | params->byteorder = PTP_DL_LE; |
| 1081 | |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 1082 | if ((device_handle = usb_open(dev))){ |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1083 | if (!device_handle) { |
| 1084 | perror("usb_open()"); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1085 | return -1; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1086 | } |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 1087 | ptp_usb->handle = device_handle; |
Linus Walleij | 501ba4d | 2006-10-16 06:17:04 +0000 | [diff] [blame] | 1088 | #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP |
| 1089 | /* |
| 1090 | * If this device is known to be wrongfully claimed by other kernel |
| 1091 | * drivers (such as mass storage), then try to unload it to make it |
| 1092 | * accessible from user space. |
| 1093 | */ |
| 1094 | if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) { |
| 1095 | if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) { |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 1096 | // Totally ignore this error! |
| 1097 | // perror("usb_detach_kernel_driver_np()"); |
Linus Walleij | 501ba4d | 2006-10-16 06:17:04 +0000 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | #endif |
Linus Walleij | eac1e00 | 2006-11-30 22:06:02 +0000 | [diff] [blame] | 1101 | #ifdef __WIN32__ |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 1102 | // Only needed on Windows, and cause problems on other platforms. |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 1103 | if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) { |
| 1104 | perror("usb_set_configuration()"); |
| 1105 | return -1; |
| 1106 | } |
Linus Walleij | eac1e00 | 2006-11-30 22:06:02 +0000 | [diff] [blame] | 1107 | #endif |
Linus Walleij | ef5cca9 | 2006-05-05 07:19:08 +0000 | [diff] [blame] | 1108 | if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) { |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 1109 | perror("usb_claim_interface()"); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1110 | return -1; |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 1111 | } |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 1112 | ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1113 | } |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1114 | return 0; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1117 | static void clear_stall(PTP_USB* ptp_usb) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1118 | { |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1119 | uint16_t status; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1120 | int ret; |
| 1121 | |
| 1122 | /* check the inep status */ |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1123 | status = 0; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1124 | ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status); |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1125 | if (ret<0) { |
| 1126 | perror ("inep: usb_get_endpoint_status()"); |
| 1127 | } else if (status) { |
| 1128 | printf("Clearing stall on IN endpoint\n"); |
| 1129 | ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep); |
| 1130 | if (ret<0) { |
| 1131 | perror ("usb_clear_stall_feature()"); |
| 1132 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1133 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1134 | |
| 1135 | /* check the outep status */ |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1136 | status=0; |
| 1137 | ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status); |
| 1138 | if (ret<0) { |
| 1139 | perror("outep: usb_get_endpoint_status()"); |
| 1140 | } else if (status) { |
| 1141 | printf("Clearing stall on OUT endpoint\n"); |
| 1142 | ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep); |
| 1143 | if (ret<0) { |
| 1144 | perror("usb_clear_stall_feature()"); |
| 1145 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1146 | } |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1147 | |
| 1148 | /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */ |
| 1149 | } |
| 1150 | |
| 1151 | static void clear_halt(PTP_USB* ptp_usb) |
| 1152 | { |
| 1153 | int ret; |
| 1154 | |
| 1155 | ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep); |
| 1156 | if (ret<0) { |
| 1157 | perror("usb_clear_halt() on IN endpoint"); |
| 1158 | } |
| 1159 | ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep); |
| 1160 | if (ret<0) { |
| 1161 | perror("usb_clear_halt() on OUT endpoint"); |
| 1162 | } |
| 1163 | ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep); |
| 1164 | if (ret<0) { |
| 1165 | perror("usb_clear_halt() on INTERRUPT endpoint"); |
| 1166 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1169 | static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1170 | { |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1171 | // Clear any stalled endpoints |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1172 | clear_stall(ptp_usb); |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1173 | // Clear halts on any endpoints |
| 1174 | clear_halt(ptp_usb); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1175 | // Added to clear some stuff on the OUT endpoint |
| 1176 | // TODO: is this good on the Mac too? |
| 1177 | usb_resetep(ptp_usb->handle, ptp_usb->outep); |
| 1178 | usb_release_interface(ptp_usb->handle, interfaceNumber); |
| 1179 | // Brutally reset device |
| 1180 | // TODO: is this good on the Mac too? |
| 1181 | usb_reset(ptp_usb->handle); |
| 1182 | usb_close(ptp_usb->handle); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1186 | /* |
| 1187 | find_device() returns the pointer to a usb_device structure matching |
| 1188 | given busn, devicen numbers. If any or both of arguments are 0 then the |
| 1189 | first matching PTP device structure is returned. |
| 1190 | */ |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1191 | static struct usb_device* find_device (int busn, int devn, short force) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1192 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1193 | struct usb_bus *bus; |
| 1194 | struct usb_device *dev; |
| 1195 | |
| 1196 | bus=init_usb(); |
| 1197 | for (; bus; bus = bus->next) |
| 1198 | for (dev = bus->devices; dev; dev = dev->next) |
| 1199 | /* somtimes dev->config is null, not sure why... */ |
| 1200 | if (dev->config != NULL) |
| 1201 | if (dev->descriptor.bDeviceClass!=USB_CLASS_HUB) |
| 1202 | { |
| 1203 | int curbusn, curdevn; |
| 1204 | |
| 1205 | curbusn=strtol(bus->dirname,NULL,10); |
| 1206 | curdevn=strtol(dev->filename,NULL,10); |
| 1207 | |
| 1208 | if (devn==0) { |
| 1209 | if (busn==0) return dev; |
| 1210 | if (curbusn==busn) return dev; |
| 1211 | } else { |
| 1212 | if ((busn==0)&&(curdevn==devn)) return dev; |
| 1213 | if ((curbusn==busn)&&(curdevn==devn)) return dev; |
| 1214 | } |
| 1215 | } |
| 1216 | return NULL; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 1219 | /** |
| 1220 | * This function scans through the device list to see if there are |
| 1221 | * some devices to connect to. The table at the top of this file is |
| 1222 | * used to identify potential devices. |
| 1223 | */ |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1224 | uint16_t connect_first_device(PTPParams *params, PTP_USB *ptp_usb, uint8_t *interfaceNumber) |
| 1225 | { |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 1226 | struct usb_bus *bus; |
| 1227 | struct usb_device *dev; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1228 | struct usb_endpoint_descriptor *ep; |
| 1229 | PTPDeviceInfo deviceinfo; |
| 1230 | uint16_t ret=0; |
| 1231 | int n; |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 1232 | |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 1233 | // Reset device flags |
| 1234 | ptp_usb->device_flags = DEVICE_FLAG_NONE; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1235 | // First try to locate the device using the extended |
| 1236 | // device descriptor. |
| 1237 | dev = probe_usb_bus_for_mtp_devices(); |
| 1238 | |
| 1239 | if (dev != NULL) { |
| 1240 | int i; |
| 1241 | |
| 1242 | // See if we can find the name of this beast |
| 1243 | for (i = 0; i < mtp_device_table_size; i++) { |
| 1244 | LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i]; |
| 1245 | if (dev->descriptor.idVendor == mtp_device->vendor_id && |
| 1246 | dev->descriptor.idProduct == mtp_device->product_id ) { |
| 1247 | printf("Autodetected device \"%s\" (VID=%04x,PID=%04x) is known.\n", |
| 1248 | mtp_device->name, dev->descriptor.idVendor, dev->descriptor.idProduct); |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 1249 | ptp_usb->device_flags = mtp_device->device_flags; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1250 | break; |
| 1251 | } |
| 1252 | } |
| 1253 | if (i == mtp_device_table_size) { |
| 1254 | printf("Autodetected device with VID=%04x and PID=%04x is UNKNOWN.\n", |
| 1255 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
| 1256 | printf("Please report this VID/PID and the device model name etc to the\n"); |
| 1257 | printf("libmtp development team!\n"); |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | // If autodetection fails, scan the bus for well known devices. |
| 1262 | if (dev == NULL) { |
| 1263 | bus = init_usb(); |
| 1264 | for (; bus; bus = bus->next) { |
| 1265 | for (dev = bus->devices; dev; dev = dev->next) { |
| 1266 | int i; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1267 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1268 | // Loop over the list of supported devices |
| 1269 | for (i = 0; i < mtp_device_table_size; i++) { |
| 1270 | LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i]; |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 1271 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1272 | if (dev->descriptor.bDeviceClass != USB_CLASS_HUB && |
| 1273 | dev->descriptor.idVendor == mtp_device->vendor_id && |
| 1274 | dev->descriptor.idProduct == mtp_device->product_id ) { |
| 1275 | |
| 1276 | 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] | 1277 | ptp_usb->device_flags = mtp_device->device_flags; |
Linus Walleij | d3fdd97 | 2006-05-30 15:51:37 +0000 | [diff] [blame] | 1278 | goto next_step; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1279 | |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 1280 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1281 | } |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1285 | |
Linus Walleij | d3fdd97 | 2006-05-30 15:51:37 +0000 | [diff] [blame] | 1286 | next_step: |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1287 | // Still not found any? |
| 1288 | if (dev == NULL) { |
| 1289 | return PTP_CD_RC_NO_DEVICES; |
| 1290 | } |
| 1291 | |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 1292 | // Found a device, then assign endpoints to ptp_usb... |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1293 | ep = dev->config->interface->altsetting->endpoint; |
| 1294 | n = dev->config->interface->altsetting->bNumEndpoints; |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 1295 | find_endpoints(dev, &ptp_usb->inep, &ptp_usb->inep_maxpacket, |
| 1296 | &ptp_usb->outep, &ptp_usb->outep_maxpacket, &ptp_usb->intep); |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1297 | |
| 1298 | // printf("Init PTP USB...\n"); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1299 | if (init_ptp_usb(params, ptp_usb, dev) < 0) { |
| 1300 | return PTP_CD_RC_ERROR_CONNECTING; |
| 1301 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1302 | |
| 1303 | ret = ptp_opensession(params,1); |
Linus Walleij | 48a2d46 | 2006-11-19 14:07:59 +0000 | [diff] [blame] | 1304 | |
| 1305 | // This works in situations where previous bad applications have not used LIBMTP_Release_Device on exit |
| 1306 | if (ret == PTP_ERROR_IO) { |
| 1307 | printf("%s\n","PTP ERROR IO: Trying again after resetting USB"); |
| 1308 | // printf("%s\n","Closing USB interface..."); |
| 1309 | close_usb(ptp_usb,dev->config->interface->altsetting->bInterfaceNumber); |
| 1310 | // printf("%s\n","Init PTP USB..."); |
| 1311 | if (init_ptp_usb(params, ptp_usb, dev) < 0) { |
| 1312 | return PTP_CD_RC_ERROR_CONNECTING; |
| 1313 | } |
| 1314 | |
| 1315 | ret = ptp_opensession(params,1); |
| 1316 | } |
| 1317 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1318 | // printf("Session open (%d)...\n", ret); |
| 1319 | if (ret == PTP_RC_InvalidTransactionID) { |
| 1320 | params->transaction_id += 10; |
| 1321 | ret = ptp_opensession(params,1); |
| 1322 | } |
| 1323 | if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) { |
| 1324 | printf("Could not open session! (Return code %d)\n Try to reset the device.\n", ret); |
| 1325 | usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber); |
| 1326 | } |
| 1327 | |
| 1328 | // It is actually permissible to call this before opening the session |
| 1329 | ret = ptp_getdeviceinfo(params, &deviceinfo); |
| 1330 | if (ret != PTP_RC_OK) { |
| 1331 | printf("Could not get device info!\n"); |
| 1332 | usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber); |
| 1333 | return PTP_CD_RC_ERROR_CONNECTING; |
| 1334 | } |
| 1335 | |
| 1336 | // we're connected, return OK |
| 1337 | *interfaceNumber = dev->config->interface->altsetting->bInterfaceNumber; |
| 1338 | return PTP_CD_RC_CONNECTED; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1341 | 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] | 1342 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1343 | int i,n; |
| 1344 | struct usb_endpoint_descriptor *ep; |
| 1345 | |
| 1346 | ep = dev->config->interface->altsetting->endpoint; |
| 1347 | n=dev->config->interface->altsetting->bNumEndpoints; |
| 1348 | |
| 1349 | for (i=0;i<n;i++) { |
| 1350 | if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) { |
| 1351 | if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)== |
| 1352 | USB_ENDPOINT_DIR_MASK) |
| 1353 | { |
| 1354 | *inep=ep[i].bEndpointAddress; |
| 1355 | *inep_maxpacket=ep[i].wMaxPacketSize; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1356 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1357 | if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0) |
| 1358 | { |
| 1359 | *outep=ep[i].bEndpointAddress; |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 1360 | *outep_maxpacket=ep[i].wMaxPacketSize; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1361 | } |
| 1362 | } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){ |
| 1363 | if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)== |
| 1364 | USB_ENDPOINT_DIR_MASK) |
| 1365 | { |
| 1366 | *intep=ep[i].bEndpointAddress; |
| 1367 | } |
| 1368 | } |
| 1369 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1372 | int open_device (int busn, int devn, short force, PTP_USB *ptp_usb, PTPParams *params, struct usb_device **dev) |
| 1373 | { |
| 1374 | #ifdef DEBUG |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1375 | printf("dev %i\tbus %i\n",devn,busn); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1376 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1377 | |
| 1378 | *dev=find_device(busn,devn,force); |
| 1379 | if (*dev==NULL) { |
| 1380 | fprintf(stderr,"could not find any device matching given " |
| 1381 | "bus/dev numbers\n"); |
| 1382 | exit(-1); |
| 1383 | } |
| 1384 | find_endpoints(*dev,&ptp_usb->inep,&ptp_usb->inep_maxpacket,&ptp_usb->outep,&ptp_usb->outep_maxpacket,&ptp_usb->intep); |
| 1385 | |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1386 | if (init_ptp_usb(params, ptp_usb, *dev) < 0) { |
| 1387 | return -1; |
| 1388 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1389 | if (ptp_opensession(params,1)!=PTP_RC_OK) { |
| 1390 | fprintf(stderr,"ERROR: Could not open session!\n"); |
| 1391 | close_usb(ptp_usb, (*dev)->config->interface->altsetting->bInterfaceNumber); |
| 1392 | return -1; |
| 1393 | } |
| 1394 | return 0; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber) |
| 1398 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1399 | if (ptp_closesession(params)!=PTP_RC_OK) |
| 1400 | fprintf(stderr,"ERROR: Could not close session!\n"); |
| 1401 | close_usb(ptp_usb, interfaceNumber); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1404 | static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1405 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1406 | |
| 1407 | return (usb_control_msg(ptp_usb->handle, |
| 1408 | USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT, |
| 1409 | ep, NULL, 0, 3000)); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1412 | 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] | 1413 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1414 | return (usb_control_msg(ptp_usb->handle, |
| 1415 | USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS, |
| 1416 | USB_FEATURE_HALT, ep, (char *)status, 2, 3000)); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1417 | } |