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