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