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