Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1 | /* |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 2 | * \file libusb-glue.c |
| 3 | * Low-level USB interface glue towards libusb. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 4 | * |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com> |
| 6 | * Copyright (C) 2005-2007 Linus Walleij <triad@df.lth.se> |
| 7 | * Copyright (C) 2006-2007 Marcus Meissner |
| 8 | * Copyright (C) 2007 Ted Bullock |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 9 | * |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Lesser General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 14 | * |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Lesser General Public License for more details. |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 19 | * |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with this library; if not, write to the |
| 22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | * Boston, MA 02111-1307, USA. |
| 24 | * |
| 25 | * Created by Richard Low on 24/12/2005. (as mtp-utils.c) |
| 26 | * Modified by Linus Walleij 2006-03-06 |
| 27 | * (Notice that Anglo-Saxons use little-endian dates and Swedes |
| 28 | * use big-endian dates.) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 29 | * |
| 30 | */ |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 31 | #include "libmtp.h" |
| 32 | #include "libusb-glue.h" |
| 33 | #include "util.h" |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 34 | #include "ptp.h" |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 35 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 36 | #include <errno.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 40 | #include <usb.h> |
| 41 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 42 | #include "ptp-pack.c" |
| 43 | |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 44 | /* To enable debug prints, switch on this */ |
| 45 | //#define ENABLE_USB_BULK_DEBUG |
| 46 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 47 | /* OUR APPLICATION USB URB (2MB) ;) */ |
| 48 | #define PTPCAM_USB_URB 2097152 |
| 49 | |
| 50 | /* this must not be too short - the original 4000 was not long |
| 51 | enough for big file transfers. I imagine the player spends a |
| 52 | bit of time gearing up to receiving lots of data. This also makes |
| 53 | connecting/disconnecting more reliable */ |
| 54 | #define USB_TIMEOUT 10000 |
| 55 | #define USB_CAPTURE_TIMEOUT 20000 |
| 56 | |
| 57 | /* USB control message data phase direction */ |
| 58 | #ifndef USB_DP_HTD |
| 59 | #define USB_DP_HTD (0x00 << 7) /* host to device */ |
| 60 | #endif |
| 61 | #ifndef USB_DP_DTH |
| 62 | #define USB_DP_DTH (0x01 << 7) /* device to host */ |
| 63 | #endif |
| 64 | |
| 65 | /* USB Feature selector HALT */ |
| 66 | #ifndef USB_FEATURE_HALT |
| 67 | #define USB_FEATURE_HALT 0x00 |
| 68 | #endif |
| 69 | |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 70 | /* |
Linus Walleij | e8c5464 | 2006-03-28 09:45:00 +0000 | [diff] [blame] | 71 | * MTP device list, trying real bad to get all devices into |
| 72 | * this list by stealing from everyone I know. |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 73 | */ |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 74 | static const LIBMTP_device_entry_t mtp_device_table[] = { |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * Creative Technology |
Linus Walleij | cc4dd48 | 2006-11-25 21:58:30 +0000 | [diff] [blame] | 78 | * Initially the Creative devices was all we supported so these are |
| 79 | * the most thoroughly tested devices. |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 80 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 81 | { "Creative Zen Vision", 0x041e, 0x411f, DEVICE_FLAG_NONE }, |
| 82 | { "Creative Portable Media Center", 0x041e, 0x4123, DEVICE_FLAG_NONE }, |
| 83 | { "Creative Zen Xtra (MTP mode)", 0x041e, 0x4128, DEVICE_FLAG_NONE }, |
| 84 | { "Second generation Dell DJ", 0x041e, 0x412f, DEVICE_FLAG_NONE }, |
| 85 | { "Creative Zen Micro (MTP mode)", 0x041e, 0x4130, DEVICE_FLAG_NONE }, |
| 86 | { "Creative Zen Touch (MTP mode)", 0x041e, 0x4131, DEVICE_FLAG_NONE }, |
| 87 | { "Dell Pocket DJ (MTP mode)", 0x041e, 0x4132, DEVICE_FLAG_NONE }, |
| 88 | { "Creative Zen Sleek (MTP mode)", 0x041e, 0x4137, DEVICE_FLAG_NONE }, |
| 89 | { "Creative Zen MicroPhoto", 0x041e, 0x413c, DEVICE_FLAG_NONE }, |
| 90 | { "Creative Zen Sleek Photo", 0x041e, 0x413d, DEVICE_FLAG_NONE }, |
| 91 | { "Creative Zen Vision:M", 0x041e, 0x413e, DEVICE_FLAG_NONE }, |
Linus Walleij | bdd006b | 2006-09-15 04:54:13 +0000 | [diff] [blame] | 92 | // Reported by marazm@o2.pl |
| 93 | { "Creative Zen V", 0x041e, 0x4150, DEVICE_FLAG_NONE }, |
Linus Walleij | 92a8496 | 2006-11-11 20:04:37 +0000 | [diff] [blame] | 94 | // Reported by danielw@iinet.net.au |
| 95 | { "Creative Zen Vision:M (DVP-HD0004)", 0x041e, 0x4151, DEVICE_FLAG_NONE }, |
Linus Walleij | 550bc67 | 2006-10-10 11:11:29 +0000 | [diff] [blame] | 96 | // Reported by Darel on the XNJB forums |
Richard Low | e1f0692 | 2006-11-12 16:38:39 +0000 | [diff] [blame] | 97 | { "Creative Zen V Plus", 0x041e, 0x4152, DEVICE_FLAG_NONE }, |
| 98 | { "Creative Zen Vision W", 0x041e, 0x4153, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * Samsung |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 102 | * We suspect that more of these are dual mode. |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 103 | */ |
Linus Walleij | 5beb1cf | 2007-06-17 19:10:42 +0000 | [diff] [blame] | 104 | // From Soren O'Neill |
| 105 | { "Samsung YH-920", 0x04e8, 0x5022, DEVICE_FLAG_UNLOAD_DRIVER }, |
| 106 | // Contributed by aronvanammers on SourceForge |
| 107 | { "Samsung YH-925GS", 0x04e8, 0x5024, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 108 | // From libgphoto2 |
Richard Low | 5224b1c | 2007-03-24 12:56:34 +0000 | [diff] [blame] | 109 | { "Samsung YH-820", 0x04e8, 0x502e, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 110 | // Contributed by polux2001@users.sourceforge.net |
Richard Low | 5224b1c | 2007-03-24 12:56:34 +0000 | [diff] [blame] | 111 | { "Samsung YH-925(-GS)", 0x04e8, 0x502f, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 112 | // Contributed by anonymous person on SourceForge |
Linus Walleij | fe03656 | 2007-06-17 19:04:59 +0000 | [diff] [blame] | 113 | { "Samsung YH-J70J", 0x04e8, 0x5033, DEVICE_FLAG_UNLOAD_DRIVER }, |
Linus Walleij | 5beb1cf | 2007-06-17 19:10:42 +0000 | [diff] [blame] | 114 | // From XNJB user |
| 115 | { "Samsung YP-Z5", 0x04e8, 0x503c, DEVICE_FLAG_NONE }, |
| 116 | // From XNJB user |
| 117 | { "Samsung YP-Z5 2GB", 0x04e8, 0x5041, DEVICE_FLAG_NONE }, |
Linus Walleij | fe03656 | 2007-06-17 19:04:59 +0000 | [diff] [blame] | 118 | // Contributed by anonymous person on SourceForge |
Richard Low | 5224b1c | 2007-03-24 12:56:34 +0000 | [diff] [blame] | 119 | { "Samsung YP-T7J", 0x04e8, 0x5047, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 120 | // Reported by cstrickler@gmail.com |
Linus Walleij | bd7624c | 2007-05-28 10:48:54 +0000 | [diff] [blame] | 121 | { "Samsung YP-U2J (YP-U2JXB/XAA)", 0x04e8, 0x5054, DEVICE_FLAG_UNLOAD_DRIVER }, |
Linus Walleij | 82ed661 | 2006-08-31 08:06:19 +0000 | [diff] [blame] | 122 | // Reported by Andrew Benson |
Linus Walleij | 4fa9d5e | 2007-05-05 20:35:56 +0000 | [diff] [blame] | 123 | { "Samsung YP-F2J", 0x04e8, 0x5057, DEVICE_FLAG_UNLOAD_DRIVER }, |
Linus Walleij | 19be421 | 2006-11-16 09:00:48 +0000 | [diff] [blame] | 124 | // Reported by Patrick <skibler@gmail.com> |
Richard Low | 6c8fea4 | 2007-03-18 19:11:52 +0000 | [diff] [blame] | 125 | { "Samsung YP-K5", 0x04e8, 0x505a, DEVICE_FLAG_NO_ZERO_READS }, |
Linus Walleij | a5073c7 | 2006-11-24 07:55:12 +0000 | [diff] [blame] | 126 | // Reported by Matthew Wilcox <matthew@wil.cx> |
Richard Low | 5224b1c | 2007-03-24 12:56:34 +0000 | [diff] [blame] | 127 | { "Samsung YP-T9", 0x04e8, 0x507f, DEVICE_FLAG_NONE }, |
Linus Walleij | 12413c0 | 2007-02-22 18:33:07 +0000 | [diff] [blame] | 128 | // From Paul Clinch |
Richard Low | 5224b1c | 2007-03-24 12:56:34 +0000 | [diff] [blame] | 129 | { "Samsung YP-K3", 0x04e8, 0x5081, DEVICE_FLAG_NONE }, |
Linus Walleij | f06189e | 2007-04-16 13:52:42 +0000 | [diff] [blame] | 130 | // From a rouge .INF file, |
| 131 | // this device ID seems to have been recycled for the Samsung SGH-A707 Cingular cellphone |
| 132 | { "Samsung YH-999 Portable Media Center / Samsung SGH-A707", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE }, |
Linus Walleij | 8556f52 | 2007-02-14 09:47:50 +0000 | [diff] [blame] | 133 | // From Lionel Bouton |
Richard Low | 5224b1c | 2007-03-24 12:56:34 +0000 | [diff] [blame] | 134 | { "Samsung X830 Mobile Phone", 0x04e8, 0x6702, DEVICE_FLAG_NONE }, |
Linus Walleij | 5beb1cf | 2007-06-17 19:10:42 +0000 | [diff] [blame] | 135 | // From James <jamestech@gmail.com> |
| 136 | { "Samsung U600 Mobile Phone", 0x04e8, 0x6709, DEVICE_FLAG_UNLOAD_DRIVER }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * Intel |
| 140 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 141 | { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * JVC |
| 145 | */ |
| 146 | // From Mark Veinot |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 147 | { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 148 | |
| 149 | /* |
Linus Walleij | 093ca0a | 2006-11-02 21:15:00 +0000 | [diff] [blame] | 150 | * Philips |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 151 | */ |
| 152 | // From libgphoto2 source |
Linus Walleij | 093ca0a | 2006-11-02 21:15:00 +0000 | [diff] [blame] | 153 | { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE }, |
jefferai | f4e6871 | 2007-05-25 15:47:15 +0000 | [diff] [blame] | 154 | { "Philips HDD6320/00 and HDD6330/17", 0x0471, 0x014b, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 155 | // Anonymous SourceForge user |
Linus Walleij | 093ca0a | 2006-11-02 21:15:00 +0000 | [diff] [blame] | 156 | { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE }, |
| 157 | // From Gerhard Mekenkamp |
| 158 | { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE }, |
Richard Low | a6c924c | 2006-12-29 17:44:28 +0000 | [diff] [blame] | 159 | // from XNJB forum |
| 160 | { "Philips GoGear SA9200", 0x0471, 0x014f, DEVICE_FLAG_NONE }, |
Richard Low | a98d054 | 2007-01-20 12:31:07 +0000 | [diff] [blame] | 161 | // from XNJB user |
| 162 | { "Philips PSA235", 0x0471, 0x7e01, DEVICE_FLAG_NONE }, |
Richard Low | bf2675c | 2007-03-18 18:20:26 +0000 | [diff] [blame] | 163 | // from discussion forum |
jefferai | f4e6871 | 2007-05-25 15:47:15 +0000 | [diff] [blame] | 164 | { "Philips HDD085/00 and HDD082/17", 0x0471, 0x014d, DEVICE_FLAG_NONE }, |
Richard Low | 6711f44 | 2007-05-05 19:00:59 +0000 | [diff] [blame] | 165 | // from npedrosa |
| 166 | { "Philips PSA610", 0x0471, 0x0181, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * SanDisk |
Linus Walleij | f41abe5 | 2007-03-20 19:25:18 +0000 | [diff] [blame] | 170 | * several devices (c150 for sure) are definately dual-mode and must |
| 171 | * have the USB mass storage driver that hooks them unloaded first. |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 172 | */ |
| 173 | // Reported by Brian Robison |
Linus Walleij | f41abe5 | 2007-03-20 19:25:18 +0000 | [diff] [blame] | 174 | { "SanDisk Sansa m230/m240", 0x0781, 0x7400, DEVICE_FLAG_UNLOAD_DRIVER }, |
| 175 | // Reported by tangent_@users.sourceforge.net |
Linus Walleij | b658318 | 2007-03-07 08:14:18 +0000 | [diff] [blame] | 176 | { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_UNLOAD_DRIVER }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 177 | // From libgphoto2 source |
Linus Walleij | d6b6702 | 2007-05-10 07:34:11 +0000 | [diff] [blame] | 178 | // Reported by <gonkflea@users.sourceforge.net> |
| 179 | // Reported by Mike Owen <mikeowen@computerbaseusa.com> |
Linus Walleij | 6ae2c3c | 2007-06-08 21:59:59 +0000 | [diff] [blame] | 180 | { "SanDisk Sansa e200/e250/e260/e270/e280", 0x0781, 0x7420, DEVICE_FLAG_UNLOAD_DRIVER }, |
Richard Low | bf2675c | 2007-03-18 18:20:26 +0000 | [diff] [blame] | 181 | // Reported by XNJB user |
Linus Walleij | f41abe5 | 2007-03-20 19:25:18 +0000 | [diff] [blame] | 182 | { "SanDisk Sansa e280", 0x0781, 0x7421, DEVICE_FLAG_UNLOAD_DRIVER }, |
Linus Walleij | ef8c117 | 2007-04-30 21:35:50 +0000 | [diff] [blame] | 183 | // Reported by anonymous user at sourceforge.net |
| 184 | { "SanDisk Sansa c250", 0x0781, 0x7450, DEVICE_FLAG_UNLOAD_DRIVER }, |
Linus Walleij | a927f43 | 2007-04-18 20:24:34 +0000 | [diff] [blame] | 185 | // Reported by XNJB user, and Miguel de Icaza <miguel@gnome.org> |
Linus Walleij | a20a243 | 2007-04-19 08:41:51 +0000 | [diff] [blame] | 186 | // This has no dual-mode so no need to unload any driver. |
Linus Walleij | 1ecab69 | 2007-05-09 22:07:45 +0000 | [diff] [blame] | 187 | // This is a Linux based device! |
Linus Walleij | a20a243 | 2007-04-19 08:41:51 +0000 | [diff] [blame] | 188 | { "SanDisk Sansa Connect", 0x0781, 0x7480, DEVICE_FLAG_NONE }, |
Richard Low | 856f345 | 2007-04-13 11:21:26 +0000 | [diff] [blame] | 189 | |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | * iRiver |
Linus Walleij | 1a1b2d1 | 2006-11-23 13:32:17 +0000 | [diff] [blame] | 193 | * we assume that PTP_OC_MTP_GetObjPropList is essentially |
Linus Walleij | fabffc0 | 2006-12-17 00:18:57 +0000 | [diff] [blame] | 194 | * broken on all iRiver devices, meaning it simply won't return |
| 195 | * all properties for a file when asking for metadata 0xffffffff. |
| 196 | * Please test on your device if you believe it isn't broken! |
| 197 | * Some devices from http://www.mtp-ums.net/viewdeviceinfo.php |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 198 | */ |
Linus Walleij | 2be40c7 | 2007-03-16 15:19:44 +0000 | [diff] [blame] | 199 | { "iRiver Portable Media Center", 0x1006, 0x4002, |
| 200 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 201 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
| 202 | { "iRiver Portable Media Center", 0x1006, 0x4003, |
| 203 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 204 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
Linus Walleij | fe03656 | 2007-06-17 19:04:59 +0000 | [diff] [blame] | 205 | // From an anonymous person at SourceForge |
| 206 | { "iRiver iFP-880", 0x4102, 0x1008, |
| 207 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 208 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 209 | // From libgphoto2 source |
Linus Walleij | 2be40c7 | 2007-03-16 15:19:44 +0000 | [diff] [blame] | 210 | { "iRiver T10", 0x4102, 0x1113, |
| 211 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 212 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
| 213 | { "iRiver T20 FM", 0x4102, 0x1114, |
| 214 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 215 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
Linus Walleij | fabffc0 | 2006-12-17 00:18:57 +0000 | [diff] [blame] | 216 | // This appears at the MTP-UMS site |
Linus Walleij | 2be40c7 | 2007-03-16 15:19:44 +0000 | [diff] [blame] | 217 | { "iRiver T20", 0x4102, 0x1115, |
| 218 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 219 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
| 220 | { "iRiver U10", 0x4102, 0x1116, |
| 221 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 222 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
| 223 | { "iRiver T10", 0x4102, 0x1117, |
| 224 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 225 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
| 226 | { "iRiver T20", 0x4102, 0x1118, |
| 227 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 228 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
| 229 | { "iRiver T30", 0x4102, 0x1119, |
| 230 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 231 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
Linus Walleij | 77e047c | 2006-09-11 19:09:06 +0000 | [diff] [blame] | 232 | // Reported by David Wolpoff |
Linus Walleij | 2be40c7 | 2007-03-16 15:19:44 +0000 | [diff] [blame] | 233 | { "iRiver T10 2GB", 0x4102, 0x1120, |
| 234 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 235 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
Linus Walleij | 8709981 | 2006-12-17 00:29:58 +0000 | [diff] [blame] | 236 | // Rough guess this is the MTP device ID... |
Linus Walleij | 2be40c7 | 2007-03-16 15:19:44 +0000 | [diff] [blame] | 237 | { "iRiver N12", 0x4102, 0x1122, |
| 238 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 239 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
Linus Walleij | 04377da | 2006-08-30 13:03:35 +0000 | [diff] [blame] | 240 | // Reported by Adam Torgerson |
Linus Walleij | 2be40c7 | 2007-03-16 15:19:44 +0000 | [diff] [blame] | 241 | { "iRiver Clix", 0x4102, 0x112a, |
| 242 | DEVICE_FLAG_NO_ZERO_READS | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
Linus Walleij | 1ecab69 | 2007-05-09 22:07:45 +0000 | [diff] [blame] | 243 | // Reported by Douglas Roth <dougaus@gmail.com> |
| 244 | { "iRiver X20", 0x4102, 0x1132, |
| 245 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 246 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 247 | // Reported by Scott Call |
Linus Walleij | 2be40c7 | 2007-03-16 15:19:44 +0000 | [diff] [blame] | 248 | { "iRiver H10 20GB", 0x4102, 0x2101, |
| 249 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 250 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
| 251 | { "iRiver H10", 0x4102, 0x2102, |
| 252 | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | |
| 253 | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 254 | |
Linus Walleij | 1ecab69 | 2007-05-09 22:07:45 +0000 | [diff] [blame] | 255 | |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 256 | /* |
| 257 | * Dell |
| 258 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 259 | { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 260 | |
| 261 | /* |
| 262 | * Toshiba |
| 263 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 264 | { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE }, |
| 265 | { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE }, |
Linus Walleij | 1e3d176 | 2007-02-26 08:27:35 +0000 | [diff] [blame] | 266 | // Reported by Nicholas Tripp |
| 267 | { "Toshiba Gigabeat P20", 0x0930, 0x000f, DEVICE_FLAG_NONE }, |
Linus Walleij | 405e239 | 2006-10-23 07:13:49 +0000 | [diff] [blame] | 268 | // From libgphoto2 |
| 269 | { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE }, |
Linus Walleij | 402e4bd | 2006-09-12 07:50:56 +0000 | [diff] [blame] | 270 | // Reported by Rob Brown |
| 271 | { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE }, |
Linus Walleij | b01d18b | 2006-12-13 12:49:15 +0000 | [diff] [blame] | 272 | |
Linus Walleij | 6f68056 | 2006-08-19 22:49:33 +0000 | [diff] [blame] | 273 | /* |
| 274 | * Archos |
| 275 | */ |
| 276 | // Reported by gudul1@users.sourceforge.net |
Linus Walleij | e543090 | 2006-10-08 14:46:03 +0000 | [diff] [blame] | 277 | { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE }, |
Linus Walleij | bbdf44e | 2007-02-14 09:36:00 +0000 | [diff] [blame] | 278 | // Added by Jan Binder |
| 279 | { "Archos XS202 (MTP mode)", 0x0e79, 0x1208, DEVICE_FLAG_NONE }, |
Linus Walleij | 8282c97 | 2007-04-10 19:59:16 +0000 | [diff] [blame] | 280 | // Reported by Etienne Chauchot <chauchot.etienne@free.fr> |
Linus Walleij | 933c780 | 2007-04-20 19:18:30 +0000 | [diff] [blame] | 281 | // This seems to be dual-mode and thus need to have the unload flag. |
| 282 | { "Archos 504 (MTP mode)", 0x0e79, 0x1307, DEVICE_FLAG_UNLOAD_DRIVER }, |
Linus Walleij | 1327a85 | 2007-05-06 19:47:38 +0000 | [diff] [blame] | 283 | // Reported by Alexander Haertig <AlexanderHaertig@gmx.de> |
Linus Walleij | 9455a9a | 2007-05-09 21:29:49 +0000 | [diff] [blame] | 284 | { "Archos Gmini XS100", 0x0e79, 0x1207, DEVICE_FLAG_UNLOAD_DRIVER }, |
Linus Walleij | e543090 | 2006-10-08 14:46:03 +0000 | [diff] [blame] | 285 | |
| 286 | /* |
Linus Walleij | 2a9d2f0 | 2006-10-15 20:44:27 +0000 | [diff] [blame] | 287 | * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad |
Linus Walleij | 550bc67 | 2006-10-10 11:11:29 +0000 | [diff] [blame] | 288 | * This unit is falsely detected as USB mass storage in Linux |
Linus Walleij | cc4dd48 | 2006-11-25 21:58:30 +0000 | [diff] [blame] | 289 | * prior to kernel 2.6.19 (fixed by patch from Alan Stern) |
| 290 | * so on older kernels special care is needed to remove the |
| 291 | * USB mass storage driver that erroneously binds to the device |
| 292 | * interface. |
Linus Walleij | e543090 | 2006-10-08 14:46:03 +0000 | [diff] [blame] | 293 | */ |
Richard Low | c7d48d2 | 2006-11-29 18:24:55 +0000 | [diff] [blame] | 294 | { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER}, |
| 295 | |
| 296 | /* |
| 297 | * Microsoft |
| 298 | */ |
| 299 | // Reported by Farooq Zaman |
| 300 | { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE }, |
| 301 | |
| 302 | /* |
| 303 | * Sirius |
| 304 | */ |
Linus Walleij | b01d18b | 2006-12-13 12:49:15 +0000 | [diff] [blame] | 305 | { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE }, |
| 306 | |
| 307 | /* |
| 308 | * Canon |
| 309 | * This is actually a camera, but it has a Microsoft device descriptor |
| 310 | * and reports itself as supporting the MTP extension. |
| 311 | */ |
Linus Walleij | c1b4940 | 2007-02-07 08:00:38 +0000 | [diff] [blame] | 312 | {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE }, |
| 313 | |
| 314 | /* |
| 315 | * Nokia |
| 316 | */ |
Linus Walleij | 42d6356 | 2007-06-24 18:25:07 +0000 | [diff] [blame^] | 317 | // From: Mitchell Hicks <mitchix@yahoo.com> |
| 318 | {"Nokia 5300 Mobile Phone", 0x0421, 0x04ba, DEVICE_FLAG_NONE }, |
Linus Walleij | 0f8a6f5 | 2007-06-14 08:37:58 +0000 | [diff] [blame] | 319 | {"Some Nokia Mobile Phone (MTP mode)", 0x0421, 0x04e1, DEVICE_FLAG_NONE }, |
| 320 | // From: Pat Nicholls <pat@patandannie.co.uk> |
| 321 | {"Nokia N80 Internet Edition (Media Player)", 0x0421, 0x04f1, DEVICE_FLAG_UNLOAD_DRIVER }, |
Linus Walleij | de7ad54 | 2007-02-14 09:32:26 +0000 | [diff] [blame] | 322 | |
| 323 | /* |
| 324 | * LOGIK |
| 325 | * Sold in the UK, seem to be manufactured by CCTech in China. |
| 326 | */ |
Linus Walleij | b26e0f5 | 2007-04-24 07:40:04 +0000 | [diff] [blame] | 327 | {"Logik LOG DAX MP3 and DAB Player", 0x13d1, 0x7002, DEVICE_FLAG_UNLOAD_DRIVER }, |
Linus Walleij | d2ff9e3 | 2007-03-16 15:27:45 +0000 | [diff] [blame] | 328 | |
| 329 | /* |
Linus Walleij | edb4533 | 2007-04-26 18:54:47 +0000 | [diff] [blame] | 330 | * RCA / Thomson |
| 331 | */ |
| 332 | {"Thomson Opal / RCA Lyra MC4002", 0x069b, 0x0777, DEVICE_FLAG_NONE }, |
Richard Low | 1869d27 | 2007-04-28 13:55:17 +0000 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * NTT DoCoMo |
| 336 | */ |
Linus Walleij | 41b2bb8 | 2007-05-08 18:23:31 +0000 | [diff] [blame] | 337 | {"FOMA F903iX HIGH-SPEED", 0x04c5, 0x1140, DEVICE_FLAG_NONE }, |
Richard Low | 6711f44 | 2007-05-05 19:00:59 +0000 | [diff] [blame] | 338 | |
Linus Walleij | 41b2bb8 | 2007-05-08 18:23:31 +0000 | [diff] [blame] | 339 | /* |
Linus Walleij | b2c1b48 | 2007-05-09 21:43:54 +0000 | [diff] [blame] | 340 | * Palm device userland program named Pocket Tunes |
Linus Walleij | 41b2bb8 | 2007-05-08 18:23:31 +0000 | [diff] [blame] | 341 | * Reported by Peter Gyongyosi <gyp@impulzus.com> |
| 342 | */ |
Linus Walleij | b2c1b48 | 2007-05-09 21:43:54 +0000 | [diff] [blame] | 343 | {"Palm / Handspring Pocket Tunes", 0x1703, 0x0001, DEVICE_FLAG_NONE }, |
Linus Walleij | 41b2bb8 | 2007-05-08 18:23:31 +0000 | [diff] [blame] | 344 | |
| 345 | /* |
Linus Walleij | 1ca24c5 | 2007-05-10 19:53:17 +0000 | [diff] [blame] | 346 | * TrekStor devices |
| 347 | * Their datasheet claims their devices are dualmode so probably needs to |
| 348 | * unload the attached drivers here. |
| 349 | */ |
| 350 | // Reported by Stefan Voss <svoss@web.de> |
| 351 | {"TrekStor Vibez 8/12GB", 0x066f, 0x842a, DEVICE_FLAG_UNLOAD_DRIVER }, |
| 352 | |
| 353 | /* |
Linus Walleij | 41b2bb8 | 2007-05-08 18:23:31 +0000 | [diff] [blame] | 354 | * Other strange stuff. |
| 355 | */ |
| 356 | {"Isabella's prototype", 0x0b20, 0xddee, DEVICE_FLAG_NONE } |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 357 | }; |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 358 | 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] | 359 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 360 | int ptpcam_usb_timeout = USB_TIMEOUT; |
| 361 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 362 | // Local functions |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 363 | static struct usb_bus* init_usb(); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 364 | static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 365 | static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep); |
| 366 | static void clear_stall(PTP_USB* ptp_usb); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 367 | 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] | 368 | static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 369 | 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] | 370 | static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep); |
| 371 | 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] | 372 | |
Linus Walleij | 552ba32 | 2007-01-22 11:49:59 +0000 | [diff] [blame] | 373 | /** |
| 374 | * Get a list of the supported USB devices. |
| 375 | * |
| 376 | * The developers depend on users of this library to constantly |
| 377 | * add in to the list of supported devices. What we need is the |
| 378 | * device name, USB Vendor ID (VID) and USB Product ID (PID). |
| 379 | * put this into a bug ticket at the project homepage, please. |
| 380 | * The VID/PID is used to let e.g. udev lift the device to |
| 381 | * console userspace access when it's plugged in. |
| 382 | * |
| 383 | * @param devices a pointer to a pointer that will hold a device |
| 384 | * list after the call to this function, if it was |
| 385 | * successful. |
| 386 | * @param numdevs a pointer to an integer that will hold the number |
| 387 | * of devices in the device list if the call was successful. |
| 388 | * @return 0 if the list was successfull retrieved, any other |
| 389 | * value means failure. |
| 390 | */ |
| 391 | 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] | 392 | { |
| 393 | *devices = (LIBMTP_device_entry_t *) &mtp_device_table; |
| 394 | *numdevs = mtp_device_table_size; |
| 395 | return 0; |
| 396 | } |
| 397 | |
Linus Walleij | 552ba32 | 2007-01-22 11:49:59 +0000 | [diff] [blame] | 398 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 399 | static struct usb_bus* init_usb() |
| 400 | { |
| 401 | usb_init(); |
| 402 | usb_find_busses(); |
| 403 | usb_find_devices(); |
| 404 | return (usb_get_busses()); |
| 405 | } |
| 406 | |
| 407 | /** |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 408 | * Small recursive function to append a new usb_device to the linked list of |
| 409 | * USB MTP devices |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 410 | * @param devlist dynamic linked list of pointers to usb devices with MTP |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 411 | * properties. |
| 412 | * @param next New USB MTP device to be added to list |
| 413 | * @return nothing |
| 414 | */ |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 415 | static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist, |
| 416 | struct usb_device *newdevice) |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 417 | { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 418 | mtpdevice_list_t *new_list_entry; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 419 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 420 | new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t)); |
| 421 | if (new_list_entry == NULL) { |
| 422 | return NULL; |
| 423 | } |
| 424 | // Fill in USB device, if we *HAVE* to make a copy of the device do it here. |
| 425 | new_list_entry->libusb_device = newdevice; |
| 426 | new_list_entry->next = NULL; |
| 427 | |
| 428 | if (devlist == NULL) { |
| 429 | return new_list_entry; |
| 430 | } else { |
| 431 | mtpdevice_list_t *tmp = devlist; |
| 432 | while (tmp->next != NULL) { |
| 433 | tmp = tmp->next; |
| 434 | } |
| 435 | tmp->next = new_list_entry; |
| 436 | } |
| 437 | return devlist; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Small recursive function to free dynamic memory allocated to the linked list |
| 442 | * of USB MTP devices |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 443 | * @param devlist dynamic linked list of pointers to usb devices with MTP |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 444 | * properties. |
| 445 | * @return nothing |
| 446 | */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 447 | void free_mtpdevice_list(mtpdevice_list_t *devlist) |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 448 | { |
Linus Walleij | ac06186 | 2007-03-07 08:28:33 +0000 | [diff] [blame] | 449 | mtpdevice_list_t *tmplist = devlist; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 450 | |
| 451 | if (devlist == NULL) |
| 452 | return; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 453 | while (tmplist != NULL) { |
| 454 | mtpdevice_list_t *tmp = tmplist; |
| 455 | tmplist = tmplist->next; |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 456 | // Do not free() the fields (ptp_usb, parms)! These are used elsewhere. |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 457 | free(tmp); |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 458 | } |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
| 462 | /** |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 463 | * This checks if a device has an MTP descriptor. |
| 464 | * @param dev a device struct from libusb. |
| 465 | * @return 1 if the device is MTP compliant, 0 if not. |
| 466 | */ |
| 467 | static int device_has_descriptor(struct usb_device *dev) |
| 468 | { |
| 469 | usb_dev_handle *devh; |
| 470 | unsigned char buf[1024], cmd; |
| 471 | int ret; |
| 472 | |
| 473 | /* Don't examine hubs (no point in that) */ |
| 474 | if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) { |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | /* Attempt to open Device on this port */ |
| 479 | devh = usb_open(dev); |
| 480 | if (devh == NULL) { |
| 481 | /* Could not open this device */ |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | /* Read the special descriptor */ |
| 486 | ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf)); |
| 487 | |
| 488 | /* Check if descriptor length is at least 10 bytes */ |
| 489 | if (ret < 10) { |
| 490 | usb_close(devh); |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | /* Check if this device has a Microsoft Descriptor */ |
| 495 | if (!((buf[2] == 'M') && (buf[4] == 'S') && |
| 496 | (buf[6] == 'F') && (buf[8] == 'T'))) { |
| 497 | usb_close(devh); |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | /* Check if device responds to control message 1 or if there is an error */ |
| 502 | cmd = buf[16]; |
| 503 | ret = usb_control_msg (devh, |
| 504 | USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, |
| 505 | cmd, |
| 506 | 0, |
| 507 | 4, |
| 508 | (char *) buf, |
| 509 | sizeof(buf), |
| 510 | 1000); |
| 511 | |
| 512 | /* If this is true, the device either isn't MTP or there was an error */ |
| 513 | if (ret <= 0x15) { |
| 514 | /* TODO: If there was an error, flag it and let the user know somehow */ |
| 515 | /* if(ret == -1) {} */ |
| 516 | usb_close(devh); |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | /* Check if device is MTP or if it is something like a USB Mass Storage |
| 521 | device with Janus DRM support */ |
| 522 | if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) { |
| 523 | usb_close(devh); |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* After this point we are probably dealing with an MTP device */ |
| 528 | |
| 529 | /* Check if device responds to control message 2 or if there is an error*/ |
| 530 | ret = usb_control_msg (devh, |
| 531 | USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, |
| 532 | cmd, |
| 533 | 0, |
| 534 | 5, |
| 535 | (char *) buf, |
| 536 | sizeof(buf), |
| 537 | 1000); |
| 538 | |
| 539 | /* If this is true, the device errored against control message 2 */ |
| 540 | if (ret == -1) { |
| 541 | /* TODO: Implement callback function to let managing program know there |
| 542 | was a problem, along with description of the problem */ |
| 543 | fprintf(stderr, "Potential MTP Device with VendorID:%04x and " |
| 544 | "ProductID:%04x encountered an error responding to " |
| 545 | "control message 2.\n" |
| 546 | "Problems may arrise but continuing\n", |
| 547 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
| 548 | } else if (ret <= 0x15) { |
| 549 | /* TODO: Implement callback function to let managing program know there |
| 550 | was a problem, along with description of the problem */ |
| 551 | fprintf(stderr, "Potential MTP Device with VendorID:%04x and " |
| 552 | "ProductID:%04x responded to control message 2 with a " |
| 553 | "response that was too short. Problems may arrise but " |
| 554 | "continuing\n", |
| 555 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
| 556 | } else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) { |
| 557 | /* TODO: Implement callback function to let managing program know there |
| 558 | was a problem, along with description of the problem */ |
| 559 | fprintf(stderr, "Potential MTP Device with VendorID:%04x and " |
| 560 | "ProductID:%04x encountered an error responding to " |
| 561 | "control message 2\n" |
| 562 | "Problems may arrise but continuing\n", |
| 563 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
| 564 | } |
| 565 | |
| 566 | /* Close the USB device handle */ |
| 567 | usb_close(devh); |
| 568 | return 1; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * This function scans through the connected usb devices on a machine and |
| 573 | * if they match known Vendor and Product identifiers appends them to the |
| 574 | * dynamic array mtp_device_list. Be sure to call |
| 575 | * <code>free(mtp_device_list)</code> when you are done with it, assuming it |
| 576 | * is not NULL. |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 577 | * @param mtp_device_list dynamic array of pointers to usb devices with MTP |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 578 | * properties (if this list is not empty, new entries will be appended |
| 579 | * to the list). |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 580 | * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list |
| 581 | * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no devices have |
| 582 | * been found. |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 583 | */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 584 | static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list) |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 585 | { |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 586 | struct usb_bus *bus = init_usb(); |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 587 | |
| 588 | for (; bus != NULL; bus = bus->next) { |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 589 | struct usb_device *dev = bus->devices; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 590 | for (; dev != NULL; dev = dev->next) { |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 591 | if (device_has_descriptor(dev)) { |
| 592 | /* Append this usb device to the MTP USB Device List */ |
| 593 | *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev); |
| 594 | } else { |
| 595 | /* Check if it's in the known devices list then */ |
| 596 | int i; |
| 597 | |
| 598 | for(i = 0; i < mtp_device_table_size; i++) { |
| 599 | if(dev->descriptor.bDeviceClass != USB_CLASS_HUB && |
| 600 | dev->descriptor.idVendor == mtp_device_table[i].vendor_id && |
| 601 | dev->descriptor.idProduct == mtp_device_table[i].product_id) { |
| 602 | /* Append this usb device to the MTP device list */ |
| 603 | *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev); |
| 604 | break; |
| 605 | } |
| 606 | } |
Linus Walleij | 35c0ef1 | 2007-03-06 20:49:09 +0000 | [diff] [blame] | 607 | } |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 608 | } |
| 609 | } |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 610 | |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 611 | /* If nothing was found we end up here. */ |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 612 | if(*mtp_device_list == NULL) { |
tedbullock | 433d217 | 2007-02-23 22:39:12 +0000 | [diff] [blame] | 613 | return LIBMTP_ERROR_NO_DEVICE_ATTACHED; |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 614 | } |
| 615 | return LIBMTP_ERROR_NONE; |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /** |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 619 | * Detect the MTP device descriptor and return the VID and PID |
| 620 | * of the first device found. This is a very low-level function |
| 621 | * which is intended for use with <b>udev</b> or other hotplug |
| 622 | * mechanisms. The idea is that a script may want to know if the |
| 623 | * just plugged-in device was an MTP device or not. |
tedbullock | 2b45f2e | 2007-02-23 20:16:37 +0000 | [diff] [blame] | 624 | * |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 625 | * @param vid the Vendor ID (VID) of the first device found. |
| 626 | * @param pid the Product ID (PID) of the first device found. |
| 627 | * @return the number of detected devices or -1 if the call |
| 628 | * was unsuccessful. |
| 629 | */ |
| 630 | int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid) |
| 631 | { |
Linus Walleij | b6b6913 | 2007-03-07 09:45:05 +0000 | [diff] [blame] | 632 | mtpdevice_list_t *devlist; |
| 633 | LIBMTP_error_number_t ret; |
| 634 | |
| 635 | ret = get_mtp_usb_device_list(&devlist); |
| 636 | if (ret != LIBMTP_ERROR_NONE) { |
| 637 | *vid = *pid = 0; |
| 638 | return -1; |
| 639 | } |
| 640 | *vid = devlist->libusb_device->descriptor.idVendor; |
| 641 | *pid = devlist->libusb_device->descriptor.idProduct; |
| 642 | free_mtpdevice_list(devlist); |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 643 | return 1; |
| 644 | } |
| 645 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 646 | /** |
| 647 | * This routine just dumps out low-level |
| 648 | * USB information about the current device. |
| 649 | * @param ptp_usb the USB device to get information from. |
| 650 | */ |
| 651 | void dump_usbinfo(PTP_USB *ptp_usb) |
| 652 | { |
| 653 | int res; |
| 654 | struct usb_device *dev; |
| 655 | |
| 656 | #ifdef LIBUSB_HAS_GET_DRIVER_NP |
| 657 | char devname[0x10]; |
| 658 | |
| 659 | devname[0] = '\0'; |
| 660 | res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname)); |
| 661 | if (devname[0] != '\0') { |
| 662 | printf(" Using kernel interface \"%s\"\n", devname); |
| 663 | } |
| 664 | #endif |
| 665 | dev = usb_device(ptp_usb->handle); |
| 666 | printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB); |
| 667 | printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass); |
| 668 | printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass); |
| 669 | printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol); |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 670 | printf(" idVendor: %04x\n", dev->descriptor.idVendor); |
| 671 | printf(" idProduct: %04x\n", dev->descriptor.idProduct); |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 672 | printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket); |
| 673 | printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket); |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 674 | printf(" Device flags: 0x%08x\n", ptp_usb->device_flags); |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 675 | // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber... |
| 676 | } |
| 677 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 678 | static void |
| 679 | ptp_debug (PTPParams *params, const char *format, ...) |
| 680 | { |
| 681 | va_list args; |
| 682 | |
| 683 | va_start (args, format); |
| 684 | if (params->debug_func!=NULL) |
| 685 | params->debug_func (params->data, format, args); |
| 686 | else |
| 687 | { |
| 688 | vfprintf (stderr, format, args); |
| 689 | fprintf (stderr,"\n"); |
| 690 | fflush (stderr); |
| 691 | } |
| 692 | va_end (args); |
| 693 | } |
| 694 | |
| 695 | static void |
| 696 | ptp_error (PTPParams *params, const char *format, ...) |
| 697 | { |
| 698 | va_list args; |
| 699 | |
| 700 | va_start (args, format); |
| 701 | if (params->error_func!=NULL) |
| 702 | params->error_func (params->data, format, args); |
| 703 | else |
| 704 | { |
| 705 | vfprintf (stderr, format, args); |
| 706 | fprintf (stderr,"\n"); |
| 707 | fflush (stderr); |
| 708 | } |
| 709 | va_end (args); |
| 710 | } |
| 711 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 712 | |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 713 | /* |
| 714 | * ptp_read_func() and ptp_write_func() are |
Linus Walleij | c0a1143 | 2007-03-02 21:21:18 +0000 | [diff] [blame] | 715 | * based on same functions usb.c in libgphoto2. |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 716 | * Much reading packet logs and having fun with trials and errors |
| 717 | * reveals that WMP / Windows is probably using an algorithm like this |
| 718 | * for large transfers: |
| 719 | * |
| 720 | * 1. Send the command (0x0c bytes) if headers are split, else, send |
| 721 | * command plus sizeof(endpoint) - 0x0c bytes. |
| 722 | * 2. Send first packet, max size to be sizeof(endpoint) but only when using |
| 723 | * split headers. Else goto 3. |
| 724 | * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000 |
| 725 | * We call 0x10000 CONTEXT_BLOCK_SIZE. |
| 726 | * 4. Send remaining bytes MOD sizeof(endpoint) |
| 727 | * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint) |
| 728 | * then also send a zero-length package. |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 729 | * |
| 730 | * Further there is some special quirks to handle zero reads from the |
| 731 | * device, since some devices can't do them at all due to shortcomings |
| 732 | * of the USB slave controller in the device. |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 733 | */ |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 734 | #define CONTEXT_BLOCK_SIZE_1 0x3e00 |
| 735 | #define CONTEXT_BLOCK_SIZE_2 0x200 |
| 736 | #define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2 |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 737 | static short |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 738 | ptp_read_func ( |
| 739 | unsigned long size, PTPDataHandler *handler,void *data, |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 740 | unsigned long *readbytes, |
| 741 | int readzero |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 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 toread = 0; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 745 | int result = 0; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 746 | unsigned long curread = 0; |
| 747 | unsigned long written; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 748 | unsigned char *bytes; |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 749 | int expect_terminator_byte = 0; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 750 | |
| 751 | // This is the largest block we'll need to read in. |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 752 | bytes = malloc(CONTEXT_BLOCK_SIZE); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 753 | while (curread < size) { |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 754 | |
| 755 | #ifdef ENABLE_USB_BULK_DEBUG |
| 756 | printf("Remaining size to read: 0x%04x bytes\n", size - curread); |
| 757 | #endif |
| 758 | // check equal to condition here |
| 759 | if (size - curread < CONTEXT_BLOCK_SIZE) |
| 760 | { |
| 761 | // this is the last packet |
| 762 | toread = size - curread; |
| 763 | // this is equivalent to zero read for these devices |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 764 | if (readzero && ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS && toread % 64 == 0) { |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 765 | toread += 1; |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 766 | expect_terminator_byte = 1; |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 767 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 768 | } |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 769 | else if (curread == 0) |
| 770 | // we are first packet, but not last packet |
| 771 | toread = CONTEXT_BLOCK_SIZE_1; |
| 772 | else if (toread == CONTEXT_BLOCK_SIZE_1) |
| 773 | toread = CONTEXT_BLOCK_SIZE_2; |
| 774 | else if (toread == CONTEXT_BLOCK_SIZE_2) |
| 775 | toread = CONTEXT_BLOCK_SIZE_1; |
| 776 | else |
Linus Walleij | 4cec987 | 2007-03-03 21:00:53 +0000 | [diff] [blame] | 777 | printf("unexpected toread size 0x%04x, 0x%04x remaining bytes\n", |
| 778 | (unsigned int) toread, (unsigned int) (size-curread)); |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 779 | |
Richard Low | d284f07 | 2007-02-17 08:52:41 +0000 | [diff] [blame] | 780 | #ifdef ENABLE_USB_BULK_DEBUG |
| 781 | printf("Reading in 0x%04x bytes\n", toread); |
| 782 | #endif |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 783 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout); |
Richard Low | d284f07 | 2007-02-17 08:52:41 +0000 | [diff] [blame] | 784 | #ifdef ENABLE_USB_BULK_DEBUG |
| 785 | printf("Result of read: 0x%04x\n", result); |
| 786 | #endif |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 787 | |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 788 | if (result < 0) { |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 789 | return PTP_ERROR_IO; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 790 | } |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 791 | #ifdef ENABLE_USB_BULK_DEBUG |
| 792 | printf("<==USB IN\n"); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 793 | if (result == 0) |
| 794 | printf("Zero Read\n"); |
| 795 | else |
| 796 | data_dump_ascii (stdout,bytes,result,16); |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 797 | #endif |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 798 | |
| 799 | // want to discard extra byte |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 800 | if (expect_terminator_byte && result == toread) |
Richard Low | bf2675c | 2007-03-18 18:20:26 +0000 | [diff] [blame] | 801 | { |
| 802 | #ifdef ENABLE_USB_BULK_DEBUG |
| 803 | printf("<==USB IN\nDiscarding extra byte\n"); |
| 804 | #endif |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 805 | result--; |
Richard Low | bf2675c | 2007-03-18 18:20:26 +0000 | [diff] [blame] | 806 | } |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 807 | |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 808 | handler->putfunc(NULL, handler->private, result, bytes, &written); |
Richard Low | bf2675c | 2007-03-18 18:20:26 +0000 | [diff] [blame] | 809 | |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 810 | ptp_usb->current_transfer_complete += result; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 811 | curread += result; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 812 | |
| 813 | // Increase counters, call callback |
| 814 | if (ptp_usb->callback_active) { |
| 815 | if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) { |
| 816 | // send last update and disable callback. |
| 817 | ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total; |
| 818 | ptp_usb->callback_active = 0; |
| 819 | } |
| 820 | if (ptp_usb->current_transfer_callback != NULL) { |
| 821 | (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete, |
| 822 | ptp_usb->current_transfer_total, |
| 823 | ptp_usb->current_transfer_callback_data); |
| 824 | } |
| 825 | } |
| 826 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 827 | if (result < toread) /* short reads are common */ |
| 828 | break; |
| 829 | } |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 830 | if (readbytes) *readbytes = curread; |
| 831 | free (bytes); |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 832 | |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 833 | // there might be a zero packet waiting for us... |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 834 | if (readzero && |
| 835 | !(ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS) && |
| 836 | curread % ptp_usb->outep_maxpacket == 0) { |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 837 | char temp; |
| 838 | int zeroresult = 0; |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 839 | |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 840 | #ifdef ENABLE_USB_BULK_DEBUG |
| 841 | printf("<==USB IN\n"); |
| 842 | printf("Zero Read\n"); |
| 843 | #endif |
| 844 | zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptpcam_usb_timeout); |
| 845 | if (zeroresult != 0) |
| 846 | printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult); |
| 847 | } |
| 848 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 849 | if (result > 0) { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 850 | return (PTP_RC_OK); |
| 851 | } else { |
| 852 | return PTP_ERROR_IO; |
| 853 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 856 | static short |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 857 | ptp_write_func ( |
| 858 | unsigned long size, |
| 859 | PTPDataHandler *handler, |
| 860 | void *data, |
| 861 | unsigned long *written |
| 862 | ) { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 863 | PTP_USB *ptp_usb = (PTP_USB *)data; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 864 | unsigned long towrite = 0; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 865 | int result = 0; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 866 | unsigned long curwrite = 0; |
| 867 | unsigned char *bytes; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 868 | |
| 869 | // This is the largest block we'll need to read in. |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 870 | bytes = malloc(CONTEXT_BLOCK_SIZE); |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 871 | if (!bytes) { |
| 872 | return PTP_ERROR_IO; |
| 873 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 874 | while (curwrite < size) { |
| 875 | towrite = size-curwrite; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 876 | if (towrite > CONTEXT_BLOCK_SIZE) { |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 877 | towrite = CONTEXT_BLOCK_SIZE; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 878 | } else { |
| 879 | // This magic makes packets the same size that WMP send them. |
| 880 | if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) { |
Richard Low | 021421e | 2007-01-01 18:08:57 +0000 | [diff] [blame] | 881 | towrite -= towrite % ptp_usb->outep_maxpacket; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | handler->getfunc(NULL, handler->private,towrite,bytes,&towrite); |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 885 | 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] | 886 | #ifdef ENABLE_USB_BULK_DEBUG |
| 887 | printf("USB OUT==>\n"); |
Richard Low | edd6183 | 2006-12-30 12:38:18 +0000 | [diff] [blame] | 888 | data_dump_ascii (stdout,bytes,towrite,16); |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 889 | #endif |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 890 | if (result < 0) { |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 891 | return PTP_ERROR_IO; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 892 | } |
| 893 | // Increase counters |
| 894 | ptp_usb->current_transfer_complete += result; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 895 | curwrite += result; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 896 | |
| 897 | // call callback |
| 898 | if (ptp_usb->callback_active) { |
| 899 | if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) { |
| 900 | // send last update and disable callback. |
| 901 | ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total; |
| 902 | ptp_usb->callback_active = 0; |
| 903 | } |
| 904 | if (ptp_usb->current_transfer_callback != NULL) { |
| 905 | (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete, |
| 906 | ptp_usb->current_transfer_total, |
| 907 | ptp_usb->current_transfer_callback_data); |
| 908 | } |
| 909 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 910 | if (result < towrite) /* short writes happen */ |
| 911 | break; |
| 912 | } |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 913 | free (bytes); |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 914 | if (written) { |
| 915 | *written = curwrite; |
Linus Walleij | 80d134a | 2006-08-22 21:41:37 +0000 | [diff] [blame] | 916 | } |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 917 | |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 918 | |
Richard Low | ef05b89 | 2007-01-03 21:18:56 +0000 | [diff] [blame] | 919 | // If this is the last transfer send a zero write if required |
| 920 | if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) { |
Richard Low | 68f4588 | 2007-01-03 10:08:31 +0000 | [diff] [blame] | 921 | if ((towrite % ptp_usb->outep_maxpacket) == 0) { |
Richard Low | 021421e | 2007-01-01 18:08:57 +0000 | [diff] [blame] | 922 | #ifdef ENABLE_USB_BULK_DEBUG |
| 923 | printf("USB OUT==>\n"); |
| 924 | printf("Zero Write\n"); |
| 925 | #endif |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 926 | 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] | 927 | } |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 928 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 929 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 930 | if (result < 0) |
| 931 | return PTP_ERROR_IO; |
| 932 | return PTP_RC_OK; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 935 | /* memory data get/put handler */ |
| 936 | typedef struct { |
| 937 | unsigned char *data; |
| 938 | unsigned long size, curoff; |
| 939 | } PTPMemHandlerPrivate; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 940 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 941 | static uint16_t |
| 942 | memory_getfunc(PTPParams* params, void* private, |
| 943 | unsigned long wantlen, unsigned char *data, |
| 944 | unsigned long *gotlen |
| 945 | ) { |
| 946 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private; |
| 947 | unsigned long tocopy = wantlen; |
| 948 | |
| 949 | if (priv->curoff + tocopy > priv->size) |
| 950 | tocopy = priv->size - priv->curoff; |
| 951 | memcpy (data, priv->data + priv->curoff, tocopy); |
| 952 | priv->curoff += tocopy; |
| 953 | *gotlen = tocopy; |
| 954 | return PTP_RC_OK; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 957 | static uint16_t |
| 958 | memory_putfunc(PTPParams* params, void* private, |
| 959 | unsigned long sendlen, unsigned char *data, |
| 960 | unsigned long *putlen |
| 961 | ) { |
| 962 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private; |
| 963 | |
| 964 | if (priv->curoff + sendlen > priv->size) { |
| 965 | priv->data = realloc (priv->data, priv->curoff+sendlen); |
| 966 | priv->size = priv->curoff + sendlen; |
| 967 | } |
| 968 | memcpy (priv->data + priv->curoff, data, sendlen); |
| 969 | priv->curoff += sendlen; |
| 970 | *putlen = sendlen; |
| 971 | return PTP_RC_OK; |
| 972 | } |
| 973 | |
| 974 | /* init private struct for receiving data. */ |
| 975 | static uint16_t |
| 976 | ptp_init_recv_memory_handler(PTPDataHandler *handler) { |
| 977 | PTPMemHandlerPrivate* priv; |
| 978 | priv = malloc (sizeof(PTPMemHandlerPrivate)); |
| 979 | handler->private = priv; |
| 980 | handler->getfunc = memory_getfunc; |
| 981 | handler->putfunc = memory_putfunc; |
| 982 | priv->data = NULL; |
| 983 | priv->size = 0; |
| 984 | priv->curoff = 0; |
| 985 | return PTP_RC_OK; |
| 986 | } |
| 987 | |
| 988 | /* init private struct and put data in for sending data. |
| 989 | * data is still owned by caller. |
| 990 | */ |
| 991 | static uint16_t |
| 992 | ptp_init_send_memory_handler(PTPDataHandler *handler, |
| 993 | unsigned char *data, unsigned long len |
| 994 | ) { |
| 995 | PTPMemHandlerPrivate* priv; |
| 996 | priv = malloc (sizeof(PTPMemHandlerPrivate)); |
| 997 | if (!priv) |
| 998 | return PTP_RC_GeneralError; |
| 999 | handler->private = priv; |
| 1000 | handler->getfunc = memory_getfunc; |
| 1001 | handler->putfunc = memory_putfunc; |
| 1002 | priv->data = data; |
| 1003 | priv->size = len; |
| 1004 | priv->curoff = 0; |
| 1005 | return PTP_RC_OK; |
| 1006 | } |
| 1007 | |
| 1008 | /* free private struct + data */ |
| 1009 | static uint16_t |
| 1010 | ptp_exit_send_memory_handler (PTPDataHandler *handler) { |
| 1011 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private; |
| 1012 | /* data is owned by caller */ |
| 1013 | free (priv); |
| 1014 | return PTP_RC_OK; |
| 1015 | } |
| 1016 | |
| 1017 | /* hand over our internal data to caller */ |
| 1018 | static uint16_t |
| 1019 | ptp_exit_recv_memory_handler (PTPDataHandler *handler, |
| 1020 | unsigned char **data, unsigned long *size |
| 1021 | ) { |
| 1022 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private; |
| 1023 | *data = priv->data; |
| 1024 | *size = priv->size; |
| 1025 | free (priv); |
| 1026 | return PTP_RC_OK; |
| 1027 | } |
| 1028 | |
| 1029 | /* send / receive functions */ |
| 1030 | |
| 1031 | uint16_t |
| 1032 | ptp_usb_sendreq (PTPParams* params, PTPContainer* req) |
| 1033 | { |
| 1034 | uint16_t ret; |
| 1035 | PTPUSBBulkContainer usbreq; |
| 1036 | PTPDataHandler memhandler; |
| 1037 | unsigned long written, towrite; |
| 1038 | |
| 1039 | /* build appropriate USB container */ |
| 1040 | usbreq.length=htod32(PTP_USB_BULK_REQ_LEN- |
| 1041 | (sizeof(uint32_t)*(5-req->Nparam))); |
| 1042 | usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND); |
| 1043 | usbreq.code=htod16(req->Code); |
| 1044 | usbreq.trans_id=htod32(req->Transaction_ID); |
| 1045 | usbreq.payload.params.param1=htod32(req->Param1); |
| 1046 | usbreq.payload.params.param2=htod32(req->Param2); |
| 1047 | usbreq.payload.params.param3=htod32(req->Param3); |
| 1048 | usbreq.payload.params.param4=htod32(req->Param4); |
| 1049 | usbreq.payload.params.param5=htod32(req->Param5); |
| 1050 | /* send it to responder */ |
| 1051 | towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam)); |
| 1052 | ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite); |
| 1053 | ret=ptp_write_func( |
| 1054 | towrite, |
| 1055 | &memhandler, |
| 1056 | params->data, |
| 1057 | &written |
| 1058 | ); |
| 1059 | ptp_exit_send_memory_handler (&memhandler); |
| 1060 | if (ret!=PTP_RC_OK) { |
| 1061 | ret = PTP_ERROR_IO; |
| 1062 | /* ptp_error (params, |
| 1063 | "PTP: request code 0x%04x sending req error 0x%04x", |
| 1064 | req->Code,ret); */ |
| 1065 | } |
| 1066 | if (written != towrite) { |
| 1067 | ptp_error (params, |
| 1068 | "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d", |
Richard Low | d284f07 | 2007-02-17 08:52:41 +0000 | [diff] [blame] | 1069 | req->Code, written, towrite |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1070 | ); |
| 1071 | ret = PTP_ERROR_IO; |
| 1072 | } |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
| 1076 | uint16_t |
| 1077 | ptp_usb_senddata (PTPParams* params, PTPContainer* ptp, |
| 1078 | unsigned long size, PTPDataHandler *handler |
| 1079 | ) { |
| 1080 | uint16_t ret; |
| 1081 | int wlen, datawlen; |
| 1082 | unsigned long written; |
| 1083 | PTPUSBBulkContainer usbdata; |
| 1084 | uint32_t bytes_left_to_transfer; |
| 1085 | PTPDataHandler memhandler; |
| 1086 | |
| 1087 | /* build appropriate USB container */ |
| 1088 | usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size); |
| 1089 | usbdata.type = htod16(PTP_USB_CONTAINER_DATA); |
| 1090 | usbdata.code = htod16(ptp->Code); |
| 1091 | usbdata.trans_id= htod32(ptp->Transaction_ID); |
| 1092 | |
| 1093 | ((PTP_USB*)params->data)->current_transfer_complete = 0; |
Richard Low | 4eb0112 | 2007-02-24 10:11:32 +0000 | [diff] [blame] | 1094 | ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1095 | |
| 1096 | if (params->split_header_data) { |
| 1097 | datawlen = 0; |
| 1098 | wlen = PTP_USB_BULK_HDR_LEN; |
| 1099 | } else { |
| 1100 | unsigned long gotlen; |
| 1101 | /* For all camera devices. */ |
Richard Low | 02724f6 | 2007-02-17 09:47:26 +0000 | [diff] [blame] | 1102 | datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1103 | wlen = PTP_USB_BULK_HDR_LEN + datawlen; |
| 1104 | ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen); |
| 1105 | if (ret != PTP_RC_OK) |
| 1106 | return ret; |
| 1107 | if (gotlen != datawlen) |
| 1108 | return PTP_RC_GeneralError; |
| 1109 | } |
| 1110 | ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen); |
| 1111 | /* send first part of data */ |
| 1112 | ret = ptp_write_func(wlen, &memhandler, params->data, &written); |
| 1113 | ptp_exit_send_memory_handler (&memhandler); |
| 1114 | if (ret!=PTP_RC_OK) { |
| 1115 | ret = PTP_ERROR_IO; |
| 1116 | /* ptp_error (params, |
| 1117 | "PTP: request code 0x%04x sending data error 0x%04x", |
| 1118 | ptp->Code,ret);*/ |
| 1119 | return ret; |
| 1120 | } |
| 1121 | if (size <= datawlen) return ret; |
| 1122 | /* if everything OK send the rest */ |
| 1123 | bytes_left_to_transfer = size-datawlen; |
| 1124 | ret = PTP_RC_OK; |
| 1125 | while(bytes_left_to_transfer > 0) { |
| 1126 | ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written); |
| 1127 | if (ret != PTP_RC_OK) |
| 1128 | break; |
| 1129 | if (written == 0) { |
| 1130 | ret = PTP_ERROR_IO; |
| 1131 | break; |
| 1132 | } |
| 1133 | bytes_left_to_transfer -= written; |
| 1134 | } |
| 1135 | if (ret!=PTP_RC_OK) |
| 1136 | ret = PTP_ERROR_IO; |
| 1137 | return ret; |
| 1138 | } |
| 1139 | |
| 1140 | static uint16_t ptp_usb_getpacket(PTPParams *params, |
| 1141 | PTPUSBBulkContainer *packet, unsigned long *rlen) |
| 1142 | { |
| 1143 | PTPDataHandler memhandler; |
| 1144 | uint16_t ret; |
| 1145 | unsigned char *x = NULL; |
| 1146 | |
| 1147 | /* read the header and potentially the first data */ |
| 1148 | if (params->response_packet_size > 0) { |
| 1149 | /* If there is a buffered packet, just use it. */ |
| 1150 | memcpy(packet, params->response_packet, params->response_packet_size); |
| 1151 | *rlen = params->response_packet_size; |
| 1152 | free(params->response_packet); |
| 1153 | params->response_packet = NULL; |
| 1154 | params->response_packet_size = 0; |
| 1155 | /* Here this signifies a "virtual read" */ |
| 1156 | return PTP_RC_OK; |
| 1157 | } |
| 1158 | ptp_init_recv_memory_handler (&memhandler); |
Richard Low | 02724f6 | 2007-02-17 09:47:26 +0000 | [diff] [blame] | 1159 | ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1160 | ptp_exit_recv_memory_handler (&memhandler, &x, rlen); |
| 1161 | if (x) { |
| 1162 | memcpy (packet, x, *rlen); |
| 1163 | free (x); |
| 1164 | } |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
| 1168 | uint16_t |
| 1169 | ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler) |
| 1170 | { |
| 1171 | uint16_t ret; |
| 1172 | PTPUSBBulkContainer usbdata; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1173 | unsigned long written; |
| 1174 | |
| 1175 | memset(&usbdata,0,sizeof(usbdata)); |
| 1176 | do { |
| 1177 | unsigned long len, rlen; |
| 1178 | |
| 1179 | ret = ptp_usb_getpacket(params, &usbdata, &rlen); |
| 1180 | if (ret!=PTP_RC_OK) { |
| 1181 | ret = PTP_ERROR_IO; |
| 1182 | break; |
| 1183 | } else |
| 1184 | if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) { |
| 1185 | ret = PTP_ERROR_DATA_EXPECTED; |
| 1186 | break; |
| 1187 | } else |
| 1188 | if (dtoh16(usbdata.code)!=ptp->Code) { |
| 1189 | ret = dtoh16(usbdata.code); |
| 1190 | break; |
| 1191 | } |
| 1192 | if (usbdata.length == 0xffffffffU) { |
| 1193 | /* stuff data directly to passed data handler */ |
| 1194 | while (1) { |
| 1195 | unsigned long readdata; |
| 1196 | int xret; |
| 1197 | |
| 1198 | xret = ptp_read_func( |
Richard Low | 02724f6 | 2007-02-17 09:47:26 +0000 | [diff] [blame] | 1199 | PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1200 | handler, |
| 1201 | params->data, |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 1202 | &readdata, |
Linus Walleij | c0a1143 | 2007-03-02 21:21:18 +0000 | [diff] [blame] | 1203 | 0 |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1204 | ); |
| 1205 | if (xret == -1) |
| 1206 | return PTP_ERROR_IO; |
Richard Low | 02724f6 | 2007-02-17 09:47:26 +0000 | [diff] [blame] | 1207 | if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1208 | break; |
| 1209 | } |
| 1210 | return PTP_RC_OK; |
| 1211 | } |
| 1212 | if (rlen > dtoh32(usbdata.length)) { |
| 1213 | /* |
| 1214 | * Buffer the surplus response packet if it is >= |
| 1215 | * PTP_USB_BULK_HDR_LEN |
| 1216 | * (i.e. it is probably an entire package) |
| 1217 | * else discard it as erroneous surplus data. |
| 1218 | * This will even work if more than 2 packets appear |
| 1219 | * in the same transaction, they will just be handled |
| 1220 | * iteratively. |
| 1221 | * |
| 1222 | * Marcus observed stray bytes on iRiver devices; |
| 1223 | * these are still discarded. |
| 1224 | */ |
| 1225 | unsigned int packlen = dtoh32(usbdata.length); |
| 1226 | unsigned int surplen = rlen - packlen; |
| 1227 | |
| 1228 | if (surplen >= PTP_USB_BULK_HDR_LEN) { |
| 1229 | params->response_packet = malloc(surplen); |
| 1230 | memcpy(params->response_packet, |
| 1231 | (uint8_t *) &usbdata + packlen, surplen); |
| 1232 | params->response_packet_size = surplen; |
tedbullock | 36f2afb | 2007-03-03 22:16:44 +0000 | [diff] [blame] | 1233 | /* Ignore reading one extra byte if device flags have been set */ |
| 1234 | } else if( !(((PTP_USB *)params->data)->device_flags & |
| 1235 | DEVICE_FLAG_NO_ZERO_READS && |
| 1236 | rlen - dtoh32(usbdata.length) == 1)) { |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1237 | ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes " |
| 1238 | "too much, expect problems!", |
| 1239 | rlen - dtoh32(usbdata.length)); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1240 | } |
| 1241 | rlen = packlen; |
| 1242 | } |
| 1243 | |
| 1244 | /* For most PTP devices rlen is 512 == sizeof(usbdata) |
| 1245 | * here. For MTP devices splitting header and data it might |
| 1246 | * be 12. |
| 1247 | */ |
| 1248 | /* Evaluate full data length. */ |
| 1249 | len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN; |
| 1250 | |
| 1251 | /* autodetect split header/data MTP devices */ |
| 1252 | if (dtoh32(usbdata.length) > 12 && (rlen==12)) |
| 1253 | params->split_header_data = 1; |
| 1254 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1255 | /* Copy first part of data to 'data' */ |
| 1256 | handler->putfunc( |
| 1257 | params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data, |
| 1258 | &written |
| 1259 | ); |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 1260 | |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1261 | if (((PTP_USB *)params->data)->device_flags & DEVICE_FLAG_NO_ZERO_READS && |
| 1262 | len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) { |
Richard Low | 057ea77 | 2007-03-24 13:37:38 +0000 | [diff] [blame] | 1263 | #ifdef ENABLE_USB_BULK_DEBUG |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1264 | printf("Reading in extra terminating byte\n"); |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 1265 | #endif |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1266 | // need to read in extra byte and discard it |
| 1267 | int result = 0; |
| 1268 | char byte = 0; |
| 1269 | PTP_USB *ptp_usb = (PTP_USB *)params->data; |
| 1270 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptpcam_usb_timeout); |
| 1271 | |
| 1272 | if (result != 1) |
| 1273 | printf("Could not read in extra byte for PTP_USB_BULK_HS_MAX_PACKET_LEN_READ long file, return value 0x%04x\n", result); |
Richard Low | 057ea77 | 2007-03-24 13:37:38 +0000 | [diff] [blame] | 1274 | } else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ && params->split_header_data == 0) { |
| 1275 | #ifdef ENABLE_USB_BULK_DEBUG |
| 1276 | printf("Reading in zero packet after header\n"); |
| 1277 | #endif |
| 1278 | int zeroresult = 0; |
| 1279 | char zerobyte = 0; |
| 1280 | PTP_USB *ptp_usb = (PTP_USB *)params->data; |
| 1281 | zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptpcam_usb_timeout); |
| 1282 | |
| 1283 | if (zeroresult != 0) |
| 1284 | printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult); |
| 1285 | } |
| 1286 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1287 | /* Is that all of data? */ |
| 1288 | if (len+PTP_USB_BULK_HDR_LEN<=rlen) break; |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1289 | |
| 1290 | ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN), |
| 1291 | handler, |
| 1292 | params->data, &rlen, 1); |
| 1293 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1294 | if (ret!=PTP_RC_OK) { |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1295 | ret = PTP_ERROR_IO; |
| 1296 | break; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1297 | } |
| 1298 | } while (0); |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1299 | /* |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1300 | if (ret!=PTP_RC_OK) { |
| 1301 | ptp_error (params, |
| 1302 | "PTP: request code 0x%04x getting data error 0x%04x", |
| 1303 | ptp->Code, ret); |
| 1304 | }*/ |
| 1305 | return ret; |
| 1306 | } |
| 1307 | |
| 1308 | uint16_t |
| 1309 | ptp_usb_getresp (PTPParams* params, PTPContainer* resp) |
| 1310 | { |
| 1311 | uint16_t ret; |
| 1312 | unsigned long rlen; |
| 1313 | PTPUSBBulkContainer usbresp; |
| 1314 | |
| 1315 | memset(&usbresp,0,sizeof(usbresp)); |
| 1316 | /* read response, it should never be longer than sizeof(usbresp) */ |
| 1317 | ret = ptp_usb_getpacket(params, &usbresp, &rlen); |
| 1318 | |
| 1319 | if (ret!=PTP_RC_OK) { |
| 1320 | ret = PTP_ERROR_IO; |
| 1321 | } else |
| 1322 | if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) { |
| 1323 | ret = PTP_ERROR_RESP_EXPECTED; |
| 1324 | } else |
| 1325 | if (dtoh16(usbresp.code)!=resp->Code) { |
| 1326 | ret = dtoh16(usbresp.code); |
| 1327 | } |
| 1328 | if (ret!=PTP_RC_OK) { |
| 1329 | /* ptp_error (params, |
| 1330 | "PTP: request code 0x%04x getting resp error 0x%04x", |
| 1331 | resp->Code, ret);*/ |
| 1332 | return ret; |
| 1333 | } |
| 1334 | /* build an appropriate PTPContainer */ |
| 1335 | resp->Code=dtoh16(usbresp.code); |
| 1336 | resp->SessionID=params->session_id; |
| 1337 | resp->Transaction_ID=dtoh32(usbresp.trans_id); |
| 1338 | resp->Param1=dtoh32(usbresp.payload.params.param1); |
| 1339 | resp->Param2=dtoh32(usbresp.payload.params.param2); |
| 1340 | resp->Param3=dtoh32(usbresp.payload.params.param3); |
| 1341 | resp->Param4=dtoh32(usbresp.payload.params.param4); |
| 1342 | resp->Param5=dtoh32(usbresp.payload.params.param5); |
| 1343 | return ret; |
| 1344 | } |
| 1345 | |
| 1346 | /* Event handling functions */ |
| 1347 | |
| 1348 | /* PTP Events wait for or check mode */ |
| 1349 | #define PTP_EVENT_CHECK 0x0000 /* waits for */ |
| 1350 | #define PTP_EVENT_CHECK_FAST 0x0001 /* checks */ |
| 1351 | |
| 1352 | static inline uint16_t |
| 1353 | ptp_usb_event (PTPParams* params, PTPContainer* event, int wait) |
| 1354 | { |
| 1355 | uint16_t ret; |
| 1356 | int result; |
| 1357 | unsigned long rlen; |
| 1358 | PTPUSBEventContainer usbevent; |
| 1359 | PTP_USB *ptp_usb = (PTP_USB *)(params->data); |
| 1360 | |
| 1361 | memset(&usbevent,0,sizeof(usbevent)); |
| 1362 | |
| 1363 | if ((params==NULL) || (event==NULL)) |
| 1364 | return PTP_ERROR_BADPARAM; |
| 1365 | ret = PTP_RC_OK; |
| 1366 | switch(wait) { |
| 1367 | case PTP_EVENT_CHECK: |
| 1368 | result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout); |
| 1369 | if (result==0) |
| 1370 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout); |
| 1371 | if (result < 0) ret = PTP_ERROR_IO; |
| 1372 | break; |
| 1373 | case PTP_EVENT_CHECK_FAST: |
| 1374 | result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout); |
| 1375 | if (result==0) |
| 1376 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout); |
| 1377 | if (result < 0) ret = PTP_ERROR_IO; |
| 1378 | break; |
| 1379 | default: |
| 1380 | ret=PTP_ERROR_BADPARAM; |
| 1381 | break; |
| 1382 | } |
| 1383 | if (ret!=PTP_RC_OK) { |
| 1384 | ptp_error (params, |
| 1385 | "PTP: reading event an error 0x%04x occurred", ret); |
| 1386 | return PTP_ERROR_IO; |
| 1387 | } |
| 1388 | rlen = result; |
| 1389 | if (rlen < 8) { |
| 1390 | ptp_error (params, |
| 1391 | "PTP: reading event an short read of %ld bytes occurred", rlen); |
| 1392 | return PTP_ERROR_IO; |
| 1393 | } |
| 1394 | /* if we read anything over interrupt endpoint it must be an event */ |
| 1395 | /* build an appropriate PTPContainer */ |
| 1396 | event->Code=dtoh16(usbevent.code); |
| 1397 | event->SessionID=params->session_id; |
| 1398 | event->Transaction_ID=dtoh32(usbevent.trans_id); |
| 1399 | event->Param1=dtoh32(usbevent.param1); |
| 1400 | event->Param2=dtoh32(usbevent.param2); |
| 1401 | event->Param3=dtoh32(usbevent.param3); |
| 1402 | return ret; |
| 1403 | } |
| 1404 | |
| 1405 | uint16_t |
| 1406 | ptp_usb_event_check (PTPParams* params, PTPContainer* event) { |
| 1407 | |
| 1408 | return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST); |
| 1409 | } |
| 1410 | |
| 1411 | uint16_t |
| 1412 | ptp_usb_event_wait (PTPParams* params, PTPContainer* event) { |
| 1413 | |
| 1414 | return ptp_usb_event (params, event, PTP_EVENT_CHECK); |
| 1415 | } |
| 1416 | |
| 1417 | |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1418 | 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] | 1419 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1420 | usb_dev_handle *device_handle; |
| 1421 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1422 | params->error_func=NULL; |
| 1423 | params->debug_func=NULL; |
| 1424 | params->sendreq_func=ptp_usb_sendreq; |
| 1425 | params->senddata_func=ptp_usb_senddata; |
| 1426 | params->getresp_func=ptp_usb_getresp; |
| 1427 | params->getdata_func=ptp_usb_getdata; |
| 1428 | params->data=ptp_usb; |
| 1429 | params->transaction_id=0; |
Linus Walleij | 462a5e7 | 2007-02-13 06:54:56 +0000 | [diff] [blame] | 1430 | /* |
| 1431 | * This is hardcoded here since we have no devices whatsoever that are BE. |
| 1432 | * Change this the day we run into our first BE device (if ever). |
| 1433 | */ |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1434 | params->byteorder = PTP_DL_LE; |
| 1435 | |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 1436 | if ((device_handle = usb_open(dev))){ |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1437 | if (!device_handle) { |
| 1438 | perror("usb_open()"); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1439 | return -1; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1440 | } |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 1441 | ptp_usb->handle = device_handle; |
Linus Walleij | 501ba4d | 2006-10-16 06:17:04 +0000 | [diff] [blame] | 1442 | #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP |
| 1443 | /* |
| 1444 | * If this device is known to be wrongfully claimed by other kernel |
| 1445 | * drivers (such as mass storage), then try to unload it to make it |
| 1446 | * accessible from user space. |
| 1447 | */ |
| 1448 | if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) { |
| 1449 | 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] | 1450 | // Totally ignore this error! |
| 1451 | // perror("usb_detach_kernel_driver_np()"); |
Linus Walleij | 501ba4d | 2006-10-16 06:17:04 +0000 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | #endif |
Linus Walleij | eac1e00 | 2006-11-30 22:06:02 +0000 | [diff] [blame] | 1455 | #ifdef __WIN32__ |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 1456 | // Only needed on Windows, and cause problems on other platforms. |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 1457 | if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) { |
| 1458 | perror("usb_set_configuration()"); |
| 1459 | return -1; |
| 1460 | } |
Linus Walleij | eac1e00 | 2006-11-30 22:06:02 +0000 | [diff] [blame] | 1461 | #endif |
Linus Walleij | ef5cca9 | 2006-05-05 07:19:08 +0000 | [diff] [blame] | 1462 | if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) { |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 1463 | perror("usb_claim_interface()"); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1464 | return -1; |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 1465 | } |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 1466 | ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1467 | } |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1468 | return 0; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1471 | static void clear_stall(PTP_USB* ptp_usb) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1472 | { |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1473 | uint16_t status; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1474 | int ret; |
| 1475 | |
| 1476 | /* check the inep status */ |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1477 | status = 0; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1478 | ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status); |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1479 | if (ret<0) { |
| 1480 | perror ("inep: usb_get_endpoint_status()"); |
| 1481 | } else if (status) { |
| 1482 | printf("Clearing stall on IN endpoint\n"); |
| 1483 | ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep); |
| 1484 | if (ret<0) { |
| 1485 | perror ("usb_clear_stall_feature()"); |
| 1486 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1487 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1488 | |
| 1489 | /* check the outep status */ |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1490 | status=0; |
| 1491 | ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status); |
| 1492 | if (ret<0) { |
| 1493 | perror("outep: usb_get_endpoint_status()"); |
| 1494 | } else if (status) { |
| 1495 | printf("Clearing stall on OUT endpoint\n"); |
| 1496 | ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep); |
| 1497 | if (ret<0) { |
| 1498 | perror("usb_clear_stall_feature()"); |
| 1499 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1500 | } |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1501 | |
| 1502 | /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */ |
| 1503 | } |
| 1504 | |
| 1505 | static void clear_halt(PTP_USB* ptp_usb) |
| 1506 | { |
| 1507 | int ret; |
| 1508 | |
| 1509 | ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep); |
| 1510 | if (ret<0) { |
| 1511 | perror("usb_clear_halt() on IN endpoint"); |
| 1512 | } |
| 1513 | ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep); |
| 1514 | if (ret<0) { |
| 1515 | perror("usb_clear_halt() on OUT endpoint"); |
| 1516 | } |
| 1517 | ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep); |
| 1518 | if (ret<0) { |
| 1519 | perror("usb_clear_halt() on INTERRUPT endpoint"); |
| 1520 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1523 | static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1524 | { |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1525 | // Clear any stalled endpoints |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1526 | clear_stall(ptp_usb); |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1527 | // Clear halts on any endpoints |
| 1528 | clear_halt(ptp_usb); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1529 | // Added to clear some stuff on the OUT endpoint |
| 1530 | // TODO: is this good on the Mac too? |
| 1531 | usb_resetep(ptp_usb->handle, ptp_usb->outep); |
| 1532 | usb_release_interface(ptp_usb->handle, interfaceNumber); |
| 1533 | // Brutally reset device |
| 1534 | // TODO: is this good on the Mac too? |
| 1535 | usb_reset(ptp_usb->handle); |
| 1536 | usb_close(ptp_usb->handle); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1539 | static LIBMTP_error_number_t prime_device_memory(mtpdevice_list_t *devlist) |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1540 | { |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1541 | mtpdevice_list_t *tmplist = devlist; |
Linus Walleij | bfcb792 | 2007-03-06 20:14:04 +0000 | [diff] [blame] | 1542 | |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1543 | while (tmplist != NULL) { |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1544 | /* Allocate a parameter box */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1545 | tmplist->params = (PTPParams *) malloc(sizeof(PTPParams)); |
| 1546 | tmplist->ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB)); |
Linus Walleij | bfcb792 | 2007-03-06 20:14:04 +0000 | [diff] [blame] | 1547 | |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1548 | /* Check for allocation Error */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1549 | if(tmplist->params == NULL || tmplist->ptp_usb == NULL) { |
Linus Walleij | bfcb792 | 2007-03-06 20:14:04 +0000 | [diff] [blame] | 1550 | /* Error and deallocation of memory will be handled by caller. */ |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1551 | return LIBMTP_ERROR_MEMORY_ALLOCATION; |
| 1552 | } |
| 1553 | |
tedbullock | 69a445b | 2007-02-15 07:41:04 +0000 | [diff] [blame] | 1554 | /* Start with a blank slate (includes setting device_flags to 0) */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1555 | memset(tmplist->params, 0, sizeof(PTPParams)); |
| 1556 | memset(tmplist->ptp_usb, 0, sizeof(PTP_USB)); |
| 1557 | tmplist = tmplist->next; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1558 | } |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1559 | return LIBMTP_ERROR_NONE; |
| 1560 | } |
| 1561 | |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1562 | static void assign_known_device_flags(mtpdevice_list_t *devlist) |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1563 | { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1564 | int i; |
Linus Walleij | 27c50c5 | 2007-03-06 20:34:48 +0000 | [diff] [blame] | 1565 | mtpdevice_list_t *tmplist = devlist; |
| 1566 | uint8_t current_device = 0; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1567 | |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1568 | /* Search through known device list and set correct device flags */ |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1569 | while (tmplist != NULL) { |
| 1570 | int device_known = 0; |
| 1571 | |
| 1572 | for(i = 0; i < mtp_device_table_size; i++) { |
| 1573 | if(tmplist->libusb_device->descriptor.idVendor == mtp_device_table[i].vendor_id && |
| 1574 | tmplist->libusb_device->descriptor.idProduct == mtp_device_table[i].product_id) { |
| 1575 | /* This device is known, assign the correct device flags */ |
| 1576 | /* Note that ptp_usb[current_device] could potentially be NULL */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1577 | if(tmplist->ptp_usb != NULL) { |
| 1578 | tmplist->ptp_usb->device_flags = mtp_device_table[i].device_flags; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1579 | |
| 1580 | /* |
| 1581 | * TODO: |
| 1582 | * Preferable to not do this with #ifdef ENABLE_USB_BULK_DEBUG but there is |
| 1583 | * currently no other compile time debug option |
| 1584 | */ |
| 1585 | |
| 1586 | device_known = 1; |
tedbullock | 65a0577 | 2007-03-03 22:56:56 +0000 | [diff] [blame] | 1587 | #ifdef ENABLE_USB_BULK_DEBUG |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1588 | /* This device is known to the developers */ |
| 1589 | fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is a %s.\n", |
| 1590 | current_device + 1, |
| 1591 | tmplist->libusb_device->descriptor.idVendor, |
| 1592 | tmplist->libusb_device->descriptor.idProduct, |
| 1593 | mtp_device_table[i].name); |
tedbullock | 65a0577 | 2007-03-03 22:56:56 +0000 | [diff] [blame] | 1594 | #endif |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1595 | } |
| 1596 | break; |
| 1597 | } |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1598 | } |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1599 | if (!device_known) { |
| 1600 | /* This device is unknown to the developers */ |
| 1601 | fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n", |
| 1602 | current_device + 1, |
| 1603 | tmplist->libusb_device->descriptor.idVendor, |
| 1604 | tmplist->libusb_device->descriptor.idProduct); |
| 1605 | fprintf(stderr, "Please report this VID/PID and the device model to the " |
| 1606 | "libmtp development team\n"); |
| 1607 | } |
| 1608 | tmplist = tmplist->next; |
Linus Walleij | bfcb792 | 2007-03-06 20:14:04 +0000 | [diff] [blame] | 1609 | current_device++; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1610 | } |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1613 | |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1614 | static LIBMTP_error_number_t configure_usb_devices(mtpdevice_list_t *devicelist) |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1615 | { |
| 1616 | mtpdevice_list_t *tmplist = devicelist; |
| 1617 | uint16_t ret = 0; |
| 1618 | uint8_t current_device = 0; |
| 1619 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1620 | while (tmplist != NULL) { |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1621 | /* This is erroneous, there must be a PTP_USB instance that we can initialize. */ |
| 1622 | if(tmplist->ptp_usb == NULL) { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1623 | return LIBMTP_ERROR_MEMORY_ALLOCATION; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1626 | /* TODO: Will this always be little endian? */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1627 | tmplist->params->byteorder = PTP_DL_LE; |
| 1628 | tmplist->params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8"); |
| 1629 | tmplist->params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE"); |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1630 | |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1631 | if(tmplist->params->cd_locale_to_ucs2 == (iconv_t) -1 || |
| 1632 | tmplist->params->cd_ucs2_to_locale == (iconv_t) -1) { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1633 | fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n" |
| 1634 | "Too old stdlibc, glibc and libiconv?\n"); |
Linus Walleij | 35c0ef1 | 2007-03-06 20:49:09 +0000 | [diff] [blame] | 1635 | return LIBMTP_ERROR_CONNECTING; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1638 | // ep = device->config->interface->altsetting->endpoint; |
| 1639 | // no_of_ep = device->config->interface->altsetting->bNumEndpoints; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1640 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1641 | /* Assign endpoints to usbinfo... */ |
| 1642 | find_endpoints(tmplist->libusb_device, |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1643 | &tmplist->ptp_usb->inep, |
| 1644 | &tmplist->ptp_usb->inep_maxpacket, |
| 1645 | &tmplist->ptp_usb->outep, |
| 1646 | &tmplist->ptp_usb->outep_maxpacket, |
| 1647 | &tmplist->ptp_usb->intep); |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1648 | |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1649 | /* Attempt to initialize this device */ |
| 1650 | if (init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) < 0) { |
| 1651 | fprintf(stderr, "LIBMTP PANIC: Unable to initialize device %d\n", current_device+1); |
| 1652 | // FIXME: perhaps use "continue" to keep trying the other devices. |
Linus Walleij | 35c0ef1 | 2007-03-06 20:49:09 +0000 | [diff] [blame] | 1653 | return LIBMTP_ERROR_CONNECTING; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | /* |
| 1657 | * This works in situations where previous bad applications |
| 1658 | * have not used LIBMTP_Release_Device on exit |
| 1659 | */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1660 | if ((ret = ptp_opensession(tmplist->params, 1)) == PTP_ERROR_IO) { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1661 | fprintf(stderr, "PTP_ERROR_IO: Trying again after resetting USB\n"); |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1662 | close_usb(tmplist->ptp_usb, tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber); |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1663 | |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1664 | if(init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) <0) { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1665 | fprintf(stderr, "LIBMTP PANIC: Could not open session on device %d\n", current_device+1); |
Linus Walleij | 35c0ef1 | 2007-03-06 20:49:09 +0000 | [diff] [blame] | 1666 | return LIBMTP_ERROR_CONNECTING; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1667 | } |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1668 | |
| 1669 | /* Device has been reset, try again */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1670 | ret = ptp_opensession(tmplist->params, 1); |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | /* Was the transaction id invalid? Try again */ |
| 1674 | if (ret == PTP_RC_InvalidTransactionID) { |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1675 | fprintf(stderr, "LIBMTP WARNING: Transaction ID was invalid, increment and try again\n"); |
| 1676 | tmplist->params->transaction_id += 10; |
| 1677 | ret = ptp_opensession(tmplist->params, 1); |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) { |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1681 | fprintf(stderr, "LIBMTP PANIC: Could not open session! " |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1682 | "(Return code %d)\n Try to reset the device.\n", |
| 1683 | ret); |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1684 | usb_release_interface(tmplist->ptp_usb->handle, |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1685 | tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber); |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1686 | return LIBMTP_ERROR_CONNECTING; |
| 1687 | } |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1688 | |
| 1689 | /* It is permissible to call this before opening the session */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1690 | if (ptp_getdeviceinfo(tmplist->params, |
| 1691 | &tmplist->params->deviceinfo) != PTP_RC_OK) { |
| 1692 | fprintf(stderr, "LIBMTP PANIC: Could not get device info!\n"); |
| 1693 | usb_release_interface(tmplist->ptp_usb->handle, |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1694 | tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber); |
Linus Walleij | 35c0ef1 | 2007-03-06 20:49:09 +0000 | [diff] [blame] | 1695 | return LIBMTP_ERROR_CONNECTING; |
| 1696 | } |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1697 | |
| 1698 | tmplist = tmplist->next; |
| 1699 | current_device++; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1702 | /* Exit with the nice list */ |
| 1703 | return LIBMTP_ERROR_NONE; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1706 | /** |
| 1707 | * This function scans through the results of the get_mtp_usb_device_list |
| 1708 | * function and attempts to connect to those devices listed using the |
| 1709 | * mtp_device_table at the top of the file. Returns a LIBMTP_error_number_t. |
| 1710 | * |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1711 | * @param devlist a list of devices with primed PTP_USB and params structs. |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1712 | * @return Error Codes as per the type definition |
| 1713 | */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1714 | LIBMTP_error_number_t find_usb_devices(mtpdevice_list_t **devlist) |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1715 | { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1716 | mtpdevice_list_t *mtp_device_list = NULL; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1717 | LIBMTP_error_number_t ret; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1718 | |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 1719 | /* |
| 1720 | * Recover list of attached USB devices that match MTP criteria, i.e. |
| 1721 | * it either has an MTP device descriptor or it is in the known |
| 1722 | * devices list. |
| 1723 | */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1724 | ret = get_mtp_usb_device_list (&mtp_device_list); |
Linus Walleij | ac06186 | 2007-03-07 08:28:33 +0000 | [diff] [blame] | 1725 | if (ret != LIBMTP_ERROR_NONE) { |
| 1726 | return ret; |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1729 | // Then prime them |
| 1730 | ret = prime_device_memory(mtp_device_list); |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1731 | if(ret) { |
Linus Walleij | ac06186 | 2007-03-07 08:28:33 +0000 | [diff] [blame] | 1732 | fprintf(stderr, "LIBMTP PANIC: prime_device_memory() error code: %d on line %d\n", ret, __LINE__); |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1733 | goto find_usb_devices_error_exit; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1734 | } |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1735 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1736 | /* Assign specific device flags and detect unknown devices */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1737 | assign_known_device_flags(mtp_device_list); |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1738 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1739 | /* Configure the devices */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1740 | ret = configure_usb_devices(mtp_device_list); |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1741 | if(ret) { |
Linus Walleij | ac06186 | 2007-03-07 08:28:33 +0000 | [diff] [blame] | 1742 | fprintf(stderr, "LIBMTP PANIC: configure_usb_devices() error code: %d on line %d\n", ret, __LINE__); |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1743 | goto find_usb_devices_error_exit; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1744 | } |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1745 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1746 | /* Configure interface numbers */ |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1747 | { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1748 | mtpdevice_list_t *tmplist = mtp_device_list; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1749 | |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1750 | while (tmplist != NULL) { |
| 1751 | tmplist->interface_number = tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1752 | tmplist = tmplist->next; |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1753 | } |
| 1754 | } |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1755 | |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1756 | /* we're connected to all devices, return the list and OK */ |
| 1757 | *devlist = mtp_device_list; |
| 1758 | return LIBMTP_ERROR_NONE; |
| 1759 | |
| 1760 | find_usb_devices_error_exit: |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 1761 | if(mtp_device_list != NULL) { |
| 1762 | free_mtpdevice_list(mtp_device_list); |
| 1763 | mtp_device_list = NULL; |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1764 | } |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 1765 | *devlist = NULL; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 1766 | return ret; |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1767 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1768 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1769 | 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] | 1770 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1771 | int i,n; |
| 1772 | struct usb_endpoint_descriptor *ep; |
| 1773 | |
| 1774 | ep = dev->config->interface->altsetting->endpoint; |
| 1775 | n=dev->config->interface->altsetting->bNumEndpoints; |
| 1776 | |
| 1777 | for (i=0;i<n;i++) { |
| 1778 | if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) { |
| 1779 | if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)== |
| 1780 | USB_ENDPOINT_DIR_MASK) |
| 1781 | { |
| 1782 | *inep=ep[i].bEndpointAddress; |
| 1783 | *inep_maxpacket=ep[i].wMaxPacketSize; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1784 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1785 | if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0) |
| 1786 | { |
| 1787 | *outep=ep[i].bEndpointAddress; |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 1788 | *outep_maxpacket=ep[i].wMaxPacketSize; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1789 | } |
| 1790 | } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){ |
| 1791 | if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)== |
| 1792 | USB_ENDPOINT_DIR_MASK) |
| 1793 | { |
| 1794 | *intep=ep[i].bEndpointAddress; |
| 1795 | } |
| 1796 | } |
| 1797 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1800 | void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber) |
| 1801 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1802 | if (ptp_closesession(params)!=PTP_RC_OK) |
| 1803 | fprintf(stderr,"ERROR: Could not close session!\n"); |
| 1804 | close_usb(ptp_usb, interfaceNumber); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1807 | static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1808 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1809 | |
| 1810 | return (usb_control_msg(ptp_usb->handle, |
| 1811 | USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT, |
| 1812 | ep, NULL, 0, 3000)); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1815 | 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] | 1816 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1817 | return (usb_control_msg(ptp_usb->handle, |
| 1818 | USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS, |
| 1819 | USB_FEATURE_HALT, ep, (char *)status, 2, 3000)); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1820 | } |