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