blob: c2e55665c23795c75af7d7d9ac5cabbf08ce48bb [file] [log] [blame]
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001/*
Linus Walleij2f45d222007-02-02 22:47:39 +00002 * \file libusb-glue.c
3 * Low-level USB interface glue towards libusb.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00004 *
Linus Walleij2f45d222007-02-02 22:47:39 +00005 * 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 Walleijeb8c6fe2006-02-03 09:46:22 +00009 *
Linus Walleij2f45d222007-02-02 22:47:39 +000010 * 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 Walleij2e1c8a32007-01-22 06:46:08 +000014 *
Linus Walleij2f45d222007-02-02 22:47:39 +000015 * 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 Walleij2e1c8a32007-01-22 06:46:08 +000019 *
Linus Walleij2f45d222007-02-02 22:47:39 +000020 * 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 Walleijeb8c6fe2006-02-03 09:46:22 +000029 *
30 */
Linus Walleij7beba572006-11-29 08:56:12 +000031#include "libmtp.h"
32#include "libusb-glue.h"
33#include "util.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000034#include "ptp.h"
Linus Walleij7beba572006-11-29 08:56:12 +000035
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000036#include <errno.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000040#include <usb.h>
41
Linus Walleija0323272007-01-07 12:21:30 +000042#include "ptp-pack.c"
43
Linus Walleije78de6d2006-10-31 14:46:36 +000044/* To enable debug prints, switch on this */
45//#define ENABLE_USB_BULK_DEBUG
46
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000047/* 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 Walleija5483642006-03-09 09:20:38 +000070/*
Linus Walleije8c54642006-03-28 09:45:00 +000071 * MTP device list, trying real bad to get all devices into
72 * this list by stealing from everyone I know.
Linus Walleija5483642006-03-09 09:20:38 +000073 */
Linus Walleij6fd2f082006-03-28 07:19:22 +000074static const LIBMTP_device_entry_t mtp_device_table[] = {
Linus Walleij3d2c7842006-08-18 09:35:08 +000075
76 /*
77 * Creative Technology
Linus Walleijcc4dd482006-11-25 21:58:30 +000078 * Initially the Creative devices was all we supported so these are
79 * the most thoroughly tested devices.
Linus Walleij3d2c7842006-08-18 09:35:08 +000080 */
Linus Walleij0558ac52006-09-07 06:55:03 +000081 { "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 Walleijbdd006b2006-09-15 04:54:13 +000092 // Reported by marazm@o2.pl
93 { "Creative Zen V", 0x041e, 0x4150, DEVICE_FLAG_NONE },
Linus Walleij92a84962006-11-11 20:04:37 +000094 // Reported by danielw@iinet.net.au
95 { "Creative Zen Vision:M (DVP-HD0004)", 0x041e, 0x4151, DEVICE_FLAG_NONE },
Linus Walleij550bc672006-10-10 11:11:29 +000096 // Reported by Darel on the XNJB forums
Richard Lowe1f06922006-11-12 16:38:39 +000097 { "Creative Zen V Plus", 0x041e, 0x4152, DEVICE_FLAG_NONE },
98 { "Creative Zen Vision W", 0x041e, 0x4153, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000099
100 /*
101 * Samsung
Linus Walleij0558ac52006-09-07 06:55:03 +0000102 * We suspect that more of these are dual mode.
Linus Walleij3d2c7842006-08-18 09:35:08 +0000103 */
104 // From libgphoto2
Richard Low5224b1c2007-03-24 12:56:34 +0000105 { "Samsung YH-820", 0x04e8, 0x502e, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000106 // Contributed by polux2001@users.sourceforge.net
Richard Low5224b1c2007-03-24 12:56:34 +0000107 { "Samsung YH-925(-GS)", 0x04e8, 0x502f, DEVICE_FLAG_NONE },
Richard Low7059ed42007-01-21 09:33:34 +0000108 // Contributed by aronvanammers on SourceForge
Richard Low5224b1c2007-03-24 12:56:34 +0000109 { "Samsung YH-925GS", 0x04e8, 0x5024, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000110 // Contributed by anonymous person on SourceForge
Richard Low5224b1c2007-03-24 12:56:34 +0000111 { "Samsung YP-T7J", 0x04e8, 0x5047, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000112 // Reported by cstrickler@gmail.com
Richard Low5224b1c2007-03-24 12:56:34 +0000113 { "Samsung YP-U2J (YP-U2JXB/XAA)", 0x04e8, 0x5054, DEVICE_FLAG_NONE },
Linus Walleij82ed6612006-08-31 08:06:19 +0000114 // Reported by Andrew Benson
Richard Low5224b1c2007-03-24 12:56:34 +0000115 { "Samsung YP-F2J", 0x04e8, 0x5057, DEVICE_FLAG_DUALMODE },
Linus Walleij19be4212006-11-16 09:00:48 +0000116 // Reported by Patrick <skibler@gmail.com>
Richard Low6c8fea42007-03-18 19:11:52 +0000117 { "Samsung YP-K5", 0x04e8, 0x505a, DEVICE_FLAG_NO_ZERO_READS },
Linus Walleija5073c72006-11-24 07:55:12 +0000118 // Reported by Matthew Wilcox <matthew@wil.cx>
Richard Low5224b1c2007-03-24 12:56:34 +0000119 { "Samsung YP-T9", 0x04e8, 0x507f, DEVICE_FLAG_NONE },
Linus Walleij12413c02007-02-22 18:33:07 +0000120 // From Paul Clinch
Richard Low5224b1c2007-03-24 12:56:34 +0000121 { "Samsung YP-K3", 0x04e8, 0x5081, DEVICE_FLAG_NONE },
Linus Walleijf06189e2007-04-16 13:52:42 +0000122 // 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 Walleij8556f522007-02-14 09:47:50 +0000125 // From Lionel Bouton
Richard Low5224b1c2007-03-24 12:56:34 +0000126 { "Samsung X830 Mobile Phone", 0x04e8, 0x6702, DEVICE_FLAG_NONE },
Richard Lowbf2675c2007-03-18 18:20:26 +0000127 // From XNJB user
Richard Low5224b1c2007-03-24 12:56:34 +0000128 { "Samsung YP-Z5", 0x04e8, 0x503c, DEVICE_FLAG_NONE },
Richard Low3637e1a2007-04-17 21:25:10 +0000129 // From XNJB user
130 { "Samsung YP-Z5 2GB", 0x04e8, 0x5041, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000131
132 /*
133 * Intel
134 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000135 { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000136
137 /*
138 * JVC
139 */
140 // From Mark Veinot
Linus Walleij0558ac52006-09-07 06:55:03 +0000141 { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000142
143 /*
Linus Walleij093ca0a2006-11-02 21:15:00 +0000144 * Philips
Linus Walleij3d2c7842006-08-18 09:35:08 +0000145 */
146 // From libgphoto2 source
Linus Walleij093ca0a2006-11-02 21:15:00 +0000147 { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE },
Richard Low7059ed42007-01-21 09:33:34 +0000148 { "Philips HDD6320/00 & HDD6330/17", 0x0471, 0x014b, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000149 // Anonymous SourceForge user
Linus Walleij093ca0a2006-11-02 21:15:00 +0000150 { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE },
151 // From Gerhard Mekenkamp
152 { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE },
Richard Lowa6c924c2006-12-29 17:44:28 +0000153 // from XNJB forum
154 { "Philips GoGear SA9200", 0x0471, 0x014f, DEVICE_FLAG_NONE },
Richard Lowa98d0542007-01-20 12:31:07 +0000155 // from XNJB user
156 { "Philips PSA235", 0x0471, 0x7e01, DEVICE_FLAG_NONE },
Richard Lowbf2675c2007-03-18 18:20:26 +0000157 // from discussion forum
Richard Low856f3452007-04-13 11:21:26 +0000158 { "Philips HDD085/00 & HDD082/17", 0x0471, 0x014d, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000159
160 /*
161 * SanDisk
Linus Walleijf41abe52007-03-20 19:25:18 +0000162 * several devices (c150 for sure) are definately dual-mode and must
163 * have the USB mass storage driver that hooks them unloaded first.
Linus Walleij3d2c7842006-08-18 09:35:08 +0000164 */
165 // Reported by Brian Robison
Linus Walleijf41abe52007-03-20 19:25:18 +0000166 { "SanDisk Sansa m230/m240", 0x0781, 0x7400, DEVICE_FLAG_UNLOAD_DRIVER },
167 // Reported by tangent_@users.sourceforge.net
Linus Walleijb6583182007-03-07 08:14:18 +0000168 { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000169 // From libgphoto2 source
Linus Walleij3d2c7842006-08-18 09:35:08 +0000170 // Reported by gonkflea@users.sourceforge.net
Linus Walleijef8c1172007-04-30 21:35:50 +0000171 { "SanDisk Sansa e200/e260", 0x0781, 0x7420, DEVICE_FLAG_UNLOAD_DRIVER },
Richard Lowbf2675c2007-03-18 18:20:26 +0000172 // Reported by XNJB user
Linus Walleijf41abe52007-03-20 19:25:18 +0000173 { "SanDisk Sansa e280", 0x0781, 0x7421, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleijef8c1172007-04-30 21:35:50 +0000174 // Reported by anonymous user at sourceforge.net
175 { "SanDisk Sansa c250", 0x0781, 0x7450, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleija927f432007-04-18 20:24:34 +0000176 // Reported by XNJB user, and Miguel de Icaza <miguel@gnome.org>
Linus Walleija20a2432007-04-19 08:41:51 +0000177 // This has no dual-mode so no need to unload any driver.
178 { "SanDisk Sansa Connect", 0x0781, 0x7480, DEVICE_FLAG_NONE },
Richard Low856f3452007-04-13 11:21:26 +0000179
Linus Walleij3d2c7842006-08-18 09:35:08 +0000180
181 /*
182 * iRiver
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000183 * we assume that PTP_OC_MTP_GetObjPropList is essentially
Linus Walleijfabffc02006-12-17 00:18:57 +0000184 * broken on all iRiver devices, meaning it simply won't return
185 * all properties for a file when asking for metadata 0xffffffff.
186 * Please test on your device if you believe it isn't broken!
187 * Some devices from http://www.mtp-ums.net/viewdeviceinfo.php
Linus Walleij3d2c7842006-08-18 09:35:08 +0000188 */
Linus Walleij2be40c72007-03-16 15:19:44 +0000189 { "iRiver Portable Media Center", 0x1006, 0x4002,
190 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
191 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
192 { "iRiver Portable Media Center", 0x1006, 0x4003,
193 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
194 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000195 // From libgphoto2 source
Linus Walleij2be40c72007-03-16 15:19:44 +0000196 { "iRiver T10", 0x4102, 0x1113,
197 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
198 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
199 { "iRiver T20 FM", 0x4102, 0x1114,
200 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
201 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleijfabffc02006-12-17 00:18:57 +0000202 // This appears at the MTP-UMS site
Linus Walleij2be40c72007-03-16 15:19:44 +0000203 { "iRiver T20", 0x4102, 0x1115,
204 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
205 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
206 { "iRiver U10", 0x4102, 0x1116,
207 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
208 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
209 { "iRiver T10", 0x4102, 0x1117,
210 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
211 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
212 { "iRiver T20", 0x4102, 0x1118,
213 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
214 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
215 { "iRiver T30", 0x4102, 0x1119,
216 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
217 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij77e047c2006-09-11 19:09:06 +0000218 // Reported by David Wolpoff
Linus Walleij2be40c72007-03-16 15:19:44 +0000219 { "iRiver T10 2GB", 0x4102, 0x1120,
220 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
221 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij87099812006-12-17 00:29:58 +0000222 // Rough guess this is the MTP device ID...
Linus Walleij2be40c72007-03-16 15:19:44 +0000223 { "iRiver N12", 0x4102, 0x1122,
224 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
225 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij04377da2006-08-30 13:03:35 +0000226 // Reported by Adam Torgerson
Linus Walleij2be40c72007-03-16 15:19:44 +0000227 { "iRiver Clix", 0x4102, 0x112a,
228 DEVICE_FLAG_NO_ZERO_READS | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000229 // Reported by Scott Call
Linus Walleij2be40c72007-03-16 15:19:44 +0000230 { "iRiver H10 20GB", 0x4102, 0x2101,
231 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
232 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
233 { "iRiver H10", 0x4102, 0x2102,
234 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
235 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000236
237 /*
238 * Dell
239 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000240 { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000241
242 /*
243 * Toshiba
244 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000245 { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE },
246 { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE },
Linus Walleij1e3d1762007-02-26 08:27:35 +0000247 // Reported by Nicholas Tripp
248 { "Toshiba Gigabeat P20", 0x0930, 0x000f, DEVICE_FLAG_NONE },
Linus Walleij405e2392006-10-23 07:13:49 +0000249 // From libgphoto2
250 { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE },
Linus Walleij402e4bd2006-09-12 07:50:56 +0000251 // Reported by Rob Brown
252 { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE },
Linus Walleijb01d18b2006-12-13 12:49:15 +0000253
Linus Walleij6f680562006-08-19 22:49:33 +0000254 /*
255 * Archos
256 */
257 // Reported by gudul1@users.sourceforge.net
Linus Walleije5430902006-10-08 14:46:03 +0000258 { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE },
Linus Walleijbbdf44e2007-02-14 09:36:00 +0000259 // Added by Jan Binder
260 { "Archos XS202 (MTP mode)", 0x0e79, 0x1208, DEVICE_FLAG_NONE },
Linus Walleij8282c972007-04-10 19:59:16 +0000261 // Reported by Etienne Chauchot <chauchot.etienne@free.fr>
Linus Walleij933c7802007-04-20 19:18:30 +0000262 // This seems to be dual-mode and thus need to have the unload flag.
263 { "Archos 504 (MTP mode)", 0x0e79, 0x1307, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleije5430902006-10-08 14:46:03 +0000264
265 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000266 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000267 * This unit is falsely detected as USB mass storage in Linux
Linus Walleijcc4dd482006-11-25 21:58:30 +0000268 * prior to kernel 2.6.19 (fixed by patch from Alan Stern)
269 * so on older kernels special care is needed to remove the
270 * USB mass storage driver that erroneously binds to the device
271 * interface.
Linus Walleije5430902006-10-08 14:46:03 +0000272 */
Richard Lowc7d48d22006-11-29 18:24:55 +0000273 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER},
274
275 /*
276 * Microsoft
277 */
278 // Reported by Farooq Zaman
279 { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE },
280
281 /*
282 * Sirius
283 */
Linus Walleijb01d18b2006-12-13 12:49:15 +0000284 { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE },
285
286 /*
287 * Canon
288 * This is actually a camera, but it has a Microsoft device descriptor
289 * and reports itself as supporting the MTP extension.
290 */
Linus Walleijc1b49402007-02-07 08:00:38 +0000291 {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE },
292
293 /*
294 * Nokia
295 */
Linus Walleijde7ad542007-02-14 09:32:26 +0000296 {"Nokia Mobile Phones (MTP mode)", 0x0421, 0x04e1, DEVICE_FLAG_NONE },
297
298 /*
299 * LOGIK
300 * Sold in the UK, seem to be manufactured by CCTech in China.
301 */
Linus Walleijb26e0f52007-04-24 07:40:04 +0000302 {"Logik LOG DAX MP3 and DAB Player", 0x13d1, 0x7002, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleijd2ff9e32007-03-16 15:27:45 +0000303
304 /*
Linus Walleijedb45332007-04-26 18:54:47 +0000305 * RCA / Thomson
306 */
307 {"Thomson Opal / RCA Lyra MC4002", 0x069b, 0x0777, DEVICE_FLAG_NONE },
308
309 /*
Linus Walleijd2ff9e32007-03-16 15:27:45 +0000310 * Other strange stuff.
311 */
Richard Low1869d272007-04-28 13:55:17 +0000312 {"Isabella's prototype", 0x0b20, 0xddee, DEVICE_FLAG_NONE },
313
314 /*
315 * NTT DoCoMo
316 */
317 {"FOMA F903iX HIGH-SPEED", 0x04c5, 0x1140, DEVICE_FLAG_NONE }
Linus Walleija5483642006-03-09 09:20:38 +0000318};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000319static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000320
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000321int ptpcam_usb_timeout = USB_TIMEOUT;
322
Linus Walleijd6a49972006-05-02 08:24:54 +0000323// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000324static struct usb_bus* init_usb();
Linus Walleijd6a49972006-05-02 08:24:54 +0000325static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);
Linus Walleijd6a49972006-05-02 08:24:54 +0000326static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);
327static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000328static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000329static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000330static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000331static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
332static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000333
Linus Walleij552ba322007-01-22 11:49:59 +0000334/**
335 * Get a list of the supported USB devices.
336 *
337 * The developers depend on users of this library to constantly
338 * add in to the list of supported devices. What we need is the
339 * device name, USB Vendor ID (VID) and USB Product ID (PID).
340 * put this into a bug ticket at the project homepage, please.
341 * The VID/PID is used to let e.g. udev lift the device to
342 * console userspace access when it's plugged in.
343 *
344 * @param devices a pointer to a pointer that will hold a device
345 * list after the call to this function, if it was
346 * successful.
347 * @param numdevs a pointer to an integer that will hold the number
348 * of devices in the device list if the call was successful.
349 * @return 0 if the list was successfull retrieved, any other
350 * value means failure.
351 */
352int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000353{
354 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
355 *numdevs = mtp_device_table_size;
356 return 0;
357}
358
Linus Walleij552ba322007-01-22 11:49:59 +0000359
Linus Walleij1fd2d272006-05-08 09:22:01 +0000360static struct usb_bus* init_usb()
361{
362 usb_init();
363 usb_find_busses();
364 usb_find_devices();
365 return (usb_get_busses());
366}
367
368/**
tedbullock0f033cb2007-02-14 20:56:54 +0000369 * Small recursive function to append a new usb_device to the linked list of
370 * USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000371 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000372 * properties.
373 * @param next New USB MTP device to be added to list
374 * @return nothing
375 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000376static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
377 struct usb_device *newdevice)
tedbullock0f033cb2007-02-14 20:56:54 +0000378{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000379 mtpdevice_list_t *new_list_entry;
tedbullock0f033cb2007-02-14 20:56:54 +0000380
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000381 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
382 if (new_list_entry == NULL) {
383 return NULL;
384 }
385 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
386 new_list_entry->libusb_device = newdevice;
387 new_list_entry->next = NULL;
388
389 if (devlist == NULL) {
390 return new_list_entry;
391 } else {
392 mtpdevice_list_t *tmp = devlist;
393 while (tmp->next != NULL) {
394 tmp = tmp->next;
395 }
396 tmp->next = new_list_entry;
397 }
398 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000399}
400
401/**
402 * Small recursive function to free dynamic memory allocated to the linked list
403 * of USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000404 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000405 * properties.
406 * @return nothing
407 */
Linus Walleij45a86372007-03-07 09:36:19 +0000408void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000409{
Linus Walleijac061862007-03-07 08:28:33 +0000410 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000411
412 if (devlist == NULL)
413 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000414 while (tmplist != NULL) {
415 mtpdevice_list_t *tmp = tmplist;
416 tmplist = tmplist->next;
Linus Walleij45a86372007-03-07 09:36:19 +0000417 // Do not free() the fields (ptp_usb, parms)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000418 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000419 }
tedbullock0f033cb2007-02-14 20:56:54 +0000420 return;
421}
422
423/**
Linus Walleija4942fc2007-03-12 19:23:21 +0000424 * This checks if a device has an MTP descriptor.
425 * @param dev a device struct from libusb.
426 * @return 1 if the device is MTP compliant, 0 if not.
427 */
428static int device_has_descriptor(struct usb_device *dev)
429{
430 usb_dev_handle *devh;
431 unsigned char buf[1024], cmd;
432 int ret;
433
434 /* Don't examine hubs (no point in that) */
435 if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) {
436 return 0;
437 }
438
439 /* Attempt to open Device on this port */
440 devh = usb_open(dev);
441 if (devh == NULL) {
442 /* Could not open this device */
443 return 0;
444 }
445
446 /* Read the special descriptor */
447 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
448
449 /* Check if descriptor length is at least 10 bytes */
450 if (ret < 10) {
451 usb_close(devh);
452 return 0;
453 }
454
455 /* Check if this device has a Microsoft Descriptor */
456 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
457 (buf[6] == 'F') && (buf[8] == 'T'))) {
458 usb_close(devh);
459 return 0;
460 }
461
462 /* Check if device responds to control message 1 or if there is an error */
463 cmd = buf[16];
464 ret = usb_control_msg (devh,
465 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
466 cmd,
467 0,
468 4,
469 (char *) buf,
470 sizeof(buf),
471 1000);
472
473 /* If this is true, the device either isn't MTP or there was an error */
474 if (ret <= 0x15) {
475 /* TODO: If there was an error, flag it and let the user know somehow */
476 /* if(ret == -1) {} */
477 usb_close(devh);
478 return 0;
479 }
480
481 /* Check if device is MTP or if it is something like a USB Mass Storage
482 device with Janus DRM support */
483 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
484 usb_close(devh);
485 return 0;
486 }
487
488 /* After this point we are probably dealing with an MTP device */
489
490 /* Check if device responds to control message 2 or if there is an error*/
491 ret = usb_control_msg (devh,
492 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
493 cmd,
494 0,
495 5,
496 (char *) buf,
497 sizeof(buf),
498 1000);
499
500 /* If this is true, the device errored against control message 2 */
501 if (ret == -1) {
502 /* TODO: Implement callback function to let managing program know there
503 was a problem, along with description of the problem */
504 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
505 "ProductID:%04x encountered an error responding to "
506 "control message 2.\n"
507 "Problems may arrise but continuing\n",
508 dev->descriptor.idVendor, dev->descriptor.idProduct);
509 } else if (ret <= 0x15) {
510 /* TODO: Implement callback function to let managing program know there
511 was a problem, along with description of the problem */
512 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
513 "ProductID:%04x responded to control message 2 with a "
514 "response that was too short. Problems may arrise but "
515 "continuing\n",
516 dev->descriptor.idVendor, dev->descriptor.idProduct);
517 } else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
518 /* TODO: Implement callback function to let managing program know there
519 was a problem, along with description of the problem */
520 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
521 "ProductID:%04x encountered an error responding to "
522 "control message 2\n"
523 "Problems may arrise but continuing\n",
524 dev->descriptor.idVendor, dev->descriptor.idProduct);
525 }
526
527 /* Close the USB device handle */
528 usb_close(devh);
529 return 1;
530}
531
532/**
533 * This function scans through the connected usb devices on a machine and
534 * if they match known Vendor and Product identifiers appends them to the
535 * dynamic array mtp_device_list. Be sure to call
536 * <code>free(mtp_device_list)</code> when you are done with it, assuming it
537 * is not NULL.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000538 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000539 * properties (if this list is not empty, new entries will be appended
540 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000541 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
542 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no devices have
543 * been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000544 */
Linus Walleij45a86372007-03-07 09:36:19 +0000545static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000546{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000547 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000548
549 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000550 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000551 for (; dev != NULL; dev = dev->next) {
Linus Walleija4942fc2007-03-12 19:23:21 +0000552 if (device_has_descriptor(dev)) {
553 /* Append this usb device to the MTP USB Device List */
554 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
555 } else {
556 /* Check if it's in the known devices list then */
557 int i;
558
559 for(i = 0; i < mtp_device_table_size; i++) {
560 if(dev->descriptor.bDeviceClass != USB_CLASS_HUB &&
561 dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
562 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
563 /* Append this usb device to the MTP device list */
564 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
565 break;
566 }
567 }
Linus Walleij35c0ef12007-03-06 20:49:09 +0000568 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000569 }
570 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000571
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000572 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000573 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000574 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000575 }
576 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000577}
578
579/**
Linus Walleij1fd2d272006-05-08 09:22:01 +0000580 * Detect the MTP device descriptor and return the VID and PID
581 * of the first device found. This is a very low-level function
582 * which is intended for use with <b>udev</b> or other hotplug
583 * mechanisms. The idea is that a script may want to know if the
584 * just plugged-in device was an MTP device or not.
tedbullock2b45f2e2007-02-23 20:16:37 +0000585 *
Linus Walleij1fd2d272006-05-08 09:22:01 +0000586 * @param vid the Vendor ID (VID) of the first device found.
587 * @param pid the Product ID (PID) of the first device found.
588 * @return the number of detected devices or -1 if the call
589 * was unsuccessful.
590 */
591int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
592{
Linus Walleijb6b69132007-03-07 09:45:05 +0000593 mtpdevice_list_t *devlist;
594 LIBMTP_error_number_t ret;
595
596 ret = get_mtp_usb_device_list(&devlist);
597 if (ret != LIBMTP_ERROR_NONE) {
598 *vid = *pid = 0;
599 return -1;
600 }
601 *vid = devlist->libusb_device->descriptor.idVendor;
602 *pid = devlist->libusb_device->descriptor.idProduct;
603 free_mtpdevice_list(devlist);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000604 return 1;
605}
606
Linus Walleijc6210fb2006-05-08 11:11:41 +0000607/**
608 * This routine just dumps out low-level
609 * USB information about the current device.
610 * @param ptp_usb the USB device to get information from.
611 */
612void dump_usbinfo(PTP_USB *ptp_usb)
613{
614 int res;
615 struct usb_device *dev;
616
617#ifdef LIBUSB_HAS_GET_DRIVER_NP
618 char devname[0x10];
619
620 devname[0] = '\0';
621 res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));
622 if (devname[0] != '\0') {
623 printf(" Using kernel interface \"%s\"\n", devname);
624 }
625#endif
626 dev = usb_device(ptp_usb->handle);
627 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
628 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
629 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
630 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000631 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
632 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000633 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
634 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000635 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000636 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
637}
638
Linus Walleija0323272007-01-07 12:21:30 +0000639static void
640ptp_debug (PTPParams *params, const char *format, ...)
641{
642 va_list args;
643
644 va_start (args, format);
645 if (params->debug_func!=NULL)
646 params->debug_func (params->data, format, args);
647 else
648 {
649 vfprintf (stderr, format, args);
650 fprintf (stderr,"\n");
651 fflush (stderr);
652 }
653 va_end (args);
654}
655
656static void
657ptp_error (PTPParams *params, const char *format, ...)
658{
659 va_list args;
660
661 va_start (args, format);
662 if (params->error_func!=NULL)
663 params->error_func (params->data, format, args);
664 else
665 {
666 vfprintf (stderr, format, args);
667 fprintf (stderr,"\n");
668 fflush (stderr);
669 }
670 va_end (args);
671}
672
Linus Walleijc6210fb2006-05-08 11:11:41 +0000673
Linus Walleij99ed83c2007-01-02 18:46:02 +0000674/*
675 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000676 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000677 * Much reading packet logs and having fun with trials and errors
678 * reveals that WMP / Windows is probably using an algorithm like this
679 * for large transfers:
680 *
681 * 1. Send the command (0x0c bytes) if headers are split, else, send
682 * command plus sizeof(endpoint) - 0x0c bytes.
683 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
684 * split headers. Else goto 3.
685 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
686 * We call 0x10000 CONTEXT_BLOCK_SIZE.
687 * 4. Send remaining bytes MOD sizeof(endpoint)
688 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
689 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000690 *
691 * Further there is some special quirks to handle zero reads from the
692 * device, since some devices can't do them at all due to shortcomings
693 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000694 */
Richard Low4fd59d62007-03-03 16:34:37 +0000695#define CONTEXT_BLOCK_SIZE_1 0x3e00
696#define CONTEXT_BLOCK_SIZE_2 0x200
697#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000698static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000699ptp_read_func (
700 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000701 unsigned long *readbytes,
702 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000703) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000704 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000705 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000706 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000707 unsigned long curread = 0;
708 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000709 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000710 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000711
712 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000713 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000714 while (curread < size) {
Richard Low4fd59d62007-03-03 16:34:37 +0000715
716#ifdef ENABLE_USB_BULK_DEBUG
717 printf("Remaining size to read: 0x%04x bytes\n", size - curread);
718#endif
719 // check equal to condition here
720 if (size - curread < CONTEXT_BLOCK_SIZE)
721 {
722 // this is the last packet
723 toread = size - curread;
724 // this is equivalent to zero read for these devices
Linus Walleij049f50c2007-03-03 20:30:43 +0000725 if (readzero && ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000726 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000727 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000728 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000729 }
Richard Low4fd59d62007-03-03 16:34:37 +0000730 else if (curread == 0)
731 // we are first packet, but not last packet
732 toread = CONTEXT_BLOCK_SIZE_1;
733 else if (toread == CONTEXT_BLOCK_SIZE_1)
734 toread = CONTEXT_BLOCK_SIZE_2;
735 else if (toread == CONTEXT_BLOCK_SIZE_2)
736 toread = CONTEXT_BLOCK_SIZE_1;
737 else
Linus Walleij4cec9872007-03-03 21:00:53 +0000738 printf("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
739 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000740
Richard Lowd284f072007-02-17 08:52:41 +0000741#ifdef ENABLE_USB_BULK_DEBUG
742 printf("Reading in 0x%04x bytes\n", toread);
743#endif
Linus Walleij784ac3f2006-12-29 10:36:51 +0000744 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout);
Richard Lowd284f072007-02-17 08:52:41 +0000745#ifdef ENABLE_USB_BULK_DEBUG
746 printf("Result of read: 0x%04x\n", result);
747#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000748
Linus Walleijc49c6372007-01-09 00:18:51 +0000749 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000750 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000751 }
Linus Walleije78de6d2006-10-31 14:46:36 +0000752#ifdef ENABLE_USB_BULK_DEBUG
753 printf("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000754 if (result == 0)
755 printf("Zero Read\n");
756 else
757 data_dump_ascii (stdout,bytes,result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000758#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000759
760 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000761 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000762 {
763#ifdef ENABLE_USB_BULK_DEBUG
764 printf("<==USB IN\nDiscarding extra byte\n");
765#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000766 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000767 }
Richard Low4fd59d62007-03-03 16:34:37 +0000768
Linus Walleijc49c6372007-01-09 00:18:51 +0000769 handler->putfunc(NULL, handler->private, result, bytes, &written);
Richard Lowbf2675c2007-03-18 18:20:26 +0000770
Linus Walleijc49c6372007-01-09 00:18:51 +0000771 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000772 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000773
774 // Increase counters, call callback
775 if (ptp_usb->callback_active) {
776 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
777 // send last update and disable callback.
778 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
779 ptp_usb->callback_active = 0;
780 }
781 if (ptp_usb->current_transfer_callback != NULL) {
782 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
783 ptp_usb->current_transfer_total,
784 ptp_usb->current_transfer_callback_data);
785 }
786 }
787
Linus Walleijd6a49972006-05-02 08:24:54 +0000788 if (result < toread) /* short reads are common */
789 break;
790 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000791 if (readbytes) *readbytes = curread;
792 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000793
Richard Low4df32f82007-01-11 20:04:39 +0000794 // there might be a zero packet waiting for us...
Linus Walleij049f50c2007-03-03 20:30:43 +0000795 if (readzero &&
796 !(ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS) &&
797 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000798 char temp;
799 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000800
Richard Low4df32f82007-01-11 20:04:39 +0000801#ifdef ENABLE_USB_BULK_DEBUG
802 printf("<==USB IN\n");
803 printf("Zero Read\n");
804#endif
805 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptpcam_usb_timeout);
806 if (zeroresult != 0)
807 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
808 }
809
Linus Walleijd6a49972006-05-02 08:24:54 +0000810 if (result > 0) {
Linus Walleijd6a49972006-05-02 08:24:54 +0000811 return (PTP_RC_OK);
812 } else {
813 return PTP_ERROR_IO;
814 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000815}
816
Linus Walleijb02a0662006-04-25 08:05:09 +0000817static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000818ptp_write_func (
819 unsigned long size,
820 PTPDataHandler *handler,
821 void *data,
822 unsigned long *written
823) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000824 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000825 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000826 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000827 unsigned long curwrite = 0;
828 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000829
830 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000831 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000832 if (!bytes) {
833 return PTP_ERROR_IO;
834 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000835 while (curwrite < size) {
836 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000837 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000838 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000839 } else {
840 // This magic makes packets the same size that WMP send them.
841 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000842 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000843 }
844 }
845 handler->getfunc(NULL, handler->private,towrite,bytes,&towrite);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000846 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000847#ifdef ENABLE_USB_BULK_DEBUG
848 printf("USB OUT==>\n");
Richard Lowedd61832006-12-30 12:38:18 +0000849 data_dump_ascii (stdout,bytes,towrite,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000850#endif
Linus Walleijc49c6372007-01-09 00:18:51 +0000851 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000852 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000853 }
854 // Increase counters
855 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000856 curwrite += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000857
858 // call callback
859 if (ptp_usb->callback_active) {
860 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
861 // send last update and disable callback.
862 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
863 ptp_usb->callback_active = 0;
864 }
865 if (ptp_usb->current_transfer_callback != NULL) {
866 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
867 ptp_usb->current_transfer_total,
868 ptp_usb->current_transfer_callback_data);
869 }
870 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000871 if (result < towrite) /* short writes happen */
872 break;
873 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000874 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000875 if (written) {
876 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000877 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000878
Linus Walleijc49c6372007-01-09 00:18:51 +0000879
Richard Lowef05b892007-01-03 21:18:56 +0000880 // If this is the last transfer send a zero write if required
881 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000882 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
Richard Low021421e2007-01-01 18:08:57 +0000883#ifdef ENABLE_USB_BULK_DEBUG
884 printf("USB OUT==>\n");
885 printf("Zero Write\n");
886#endif
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000887 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000888 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000889 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000890
Linus Walleijd6a49972006-05-02 08:24:54 +0000891 if (result < 0)
892 return PTP_ERROR_IO;
893 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000894}
895
Linus Walleija0323272007-01-07 12:21:30 +0000896/* memory data get/put handler */
897typedef struct {
898 unsigned char *data;
899 unsigned long size, curoff;
900} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000901
Linus Walleija0323272007-01-07 12:21:30 +0000902static uint16_t
903memory_getfunc(PTPParams* params, void* private,
904 unsigned long wantlen, unsigned char *data,
905 unsigned long *gotlen
906) {
907 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
908 unsigned long tocopy = wantlen;
909
910 if (priv->curoff + tocopy > priv->size)
911 tocopy = priv->size - priv->curoff;
912 memcpy (data, priv->data + priv->curoff, tocopy);
913 priv->curoff += tocopy;
914 *gotlen = tocopy;
915 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000916}
917
Linus Walleija0323272007-01-07 12:21:30 +0000918static uint16_t
919memory_putfunc(PTPParams* params, void* private,
920 unsigned long sendlen, unsigned char *data,
921 unsigned long *putlen
922) {
923 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
924
925 if (priv->curoff + sendlen > priv->size) {
926 priv->data = realloc (priv->data, priv->curoff+sendlen);
927 priv->size = priv->curoff + sendlen;
928 }
929 memcpy (priv->data + priv->curoff, data, sendlen);
930 priv->curoff += sendlen;
931 *putlen = sendlen;
932 return PTP_RC_OK;
933}
934
935/* init private struct for receiving data. */
936static uint16_t
937ptp_init_recv_memory_handler(PTPDataHandler *handler) {
938 PTPMemHandlerPrivate* priv;
939 priv = malloc (sizeof(PTPMemHandlerPrivate));
940 handler->private = priv;
941 handler->getfunc = memory_getfunc;
942 handler->putfunc = memory_putfunc;
943 priv->data = NULL;
944 priv->size = 0;
945 priv->curoff = 0;
946 return PTP_RC_OK;
947}
948
949/* init private struct and put data in for sending data.
950 * data is still owned by caller.
951 */
952static uint16_t
953ptp_init_send_memory_handler(PTPDataHandler *handler,
954 unsigned char *data, unsigned long len
955) {
956 PTPMemHandlerPrivate* priv;
957 priv = malloc (sizeof(PTPMemHandlerPrivate));
958 if (!priv)
959 return PTP_RC_GeneralError;
960 handler->private = priv;
961 handler->getfunc = memory_getfunc;
962 handler->putfunc = memory_putfunc;
963 priv->data = data;
964 priv->size = len;
965 priv->curoff = 0;
966 return PTP_RC_OK;
967}
968
969/* free private struct + data */
970static uint16_t
971ptp_exit_send_memory_handler (PTPDataHandler *handler) {
972 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
973 /* data is owned by caller */
974 free (priv);
975 return PTP_RC_OK;
976}
977
978/* hand over our internal data to caller */
979static uint16_t
980ptp_exit_recv_memory_handler (PTPDataHandler *handler,
981 unsigned char **data, unsigned long *size
982) {
983 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
984 *data = priv->data;
985 *size = priv->size;
986 free (priv);
987 return PTP_RC_OK;
988}
989
990/* send / receive functions */
991
992uint16_t
993ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
994{
995 uint16_t ret;
996 PTPUSBBulkContainer usbreq;
997 PTPDataHandler memhandler;
998 unsigned long written, towrite;
999
1000 /* build appropriate USB container */
1001 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
1002 (sizeof(uint32_t)*(5-req->Nparam)));
1003 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
1004 usbreq.code=htod16(req->Code);
1005 usbreq.trans_id=htod32(req->Transaction_ID);
1006 usbreq.payload.params.param1=htod32(req->Param1);
1007 usbreq.payload.params.param2=htod32(req->Param2);
1008 usbreq.payload.params.param3=htod32(req->Param3);
1009 usbreq.payload.params.param4=htod32(req->Param4);
1010 usbreq.payload.params.param5=htod32(req->Param5);
1011 /* send it to responder */
1012 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
1013 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
1014 ret=ptp_write_func(
1015 towrite,
1016 &memhandler,
1017 params->data,
1018 &written
1019 );
1020 ptp_exit_send_memory_handler (&memhandler);
1021 if (ret!=PTP_RC_OK) {
1022 ret = PTP_ERROR_IO;
1023/* ptp_error (params,
1024 "PTP: request code 0x%04x sending req error 0x%04x",
1025 req->Code,ret); */
1026 }
1027 if (written != towrite) {
1028 ptp_error (params,
1029 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001030 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001031 );
1032 ret = PTP_ERROR_IO;
1033 }
1034 return ret;
1035}
1036
1037uint16_t
1038ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1039 unsigned long size, PTPDataHandler *handler
1040) {
1041 uint16_t ret;
1042 int wlen, datawlen;
1043 unsigned long written;
1044 PTPUSBBulkContainer usbdata;
1045 uint32_t bytes_left_to_transfer;
1046 PTPDataHandler memhandler;
1047
1048 /* build appropriate USB container */
1049 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1050 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1051 usbdata.code = htod16(ptp->Code);
1052 usbdata.trans_id= htod32(ptp->Transaction_ID);
1053
1054 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001055 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001056
1057 if (params->split_header_data) {
1058 datawlen = 0;
1059 wlen = PTP_USB_BULK_HDR_LEN;
1060 } else {
1061 unsigned long gotlen;
1062 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001063 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001064 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
1065 ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen);
1066 if (ret != PTP_RC_OK)
1067 return ret;
1068 if (gotlen != datawlen)
1069 return PTP_RC_GeneralError;
1070 }
1071 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1072 /* send first part of data */
1073 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1074 ptp_exit_send_memory_handler (&memhandler);
1075 if (ret!=PTP_RC_OK) {
1076 ret = PTP_ERROR_IO;
1077/* ptp_error (params,
1078 "PTP: request code 0x%04x sending data error 0x%04x",
1079 ptp->Code,ret);*/
1080 return ret;
1081 }
1082 if (size <= datawlen) return ret;
1083 /* if everything OK send the rest */
1084 bytes_left_to_transfer = size-datawlen;
1085 ret = PTP_RC_OK;
1086 while(bytes_left_to_transfer > 0) {
1087 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1088 if (ret != PTP_RC_OK)
1089 break;
1090 if (written == 0) {
1091 ret = PTP_ERROR_IO;
1092 break;
1093 }
1094 bytes_left_to_transfer -= written;
1095 }
1096 if (ret!=PTP_RC_OK)
1097 ret = PTP_ERROR_IO;
1098 return ret;
1099}
1100
1101static uint16_t ptp_usb_getpacket(PTPParams *params,
1102 PTPUSBBulkContainer *packet, unsigned long *rlen)
1103{
1104 PTPDataHandler memhandler;
1105 uint16_t ret;
1106 unsigned char *x = NULL;
1107
1108 /* read the header and potentially the first data */
1109 if (params->response_packet_size > 0) {
1110 /* If there is a buffered packet, just use it. */
1111 memcpy(packet, params->response_packet, params->response_packet_size);
1112 *rlen = params->response_packet_size;
1113 free(params->response_packet);
1114 params->response_packet = NULL;
1115 params->response_packet_size = 0;
1116 /* Here this signifies a "virtual read" */
1117 return PTP_RC_OK;
1118 }
1119 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001120 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001121 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1122 if (x) {
1123 memcpy (packet, x, *rlen);
1124 free (x);
1125 }
1126 return ret;
1127}
1128
1129uint16_t
1130ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1131{
1132 uint16_t ret;
1133 PTPUSBBulkContainer usbdata;
Linus Walleija0323272007-01-07 12:21:30 +00001134 unsigned long written;
1135
1136 memset(&usbdata,0,sizeof(usbdata));
1137 do {
1138 unsigned long len, rlen;
1139
1140 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1141 if (ret!=PTP_RC_OK) {
1142 ret = PTP_ERROR_IO;
1143 break;
1144 } else
1145 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1146 ret = PTP_ERROR_DATA_EXPECTED;
1147 break;
1148 } else
1149 if (dtoh16(usbdata.code)!=ptp->Code) {
1150 ret = dtoh16(usbdata.code);
1151 break;
1152 }
1153 if (usbdata.length == 0xffffffffU) {
1154 /* stuff data directly to passed data handler */
1155 while (1) {
1156 unsigned long readdata;
1157 int xret;
1158
1159 xret = ptp_read_func(
Richard Low02724f62007-02-17 09:47:26 +00001160 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
Linus Walleija0323272007-01-07 12:21:30 +00001161 handler,
1162 params->data,
Richard Low4df32f82007-01-11 20:04:39 +00001163 &readdata,
Linus Walleijc0a11432007-03-02 21:21:18 +00001164 0
Linus Walleija0323272007-01-07 12:21:30 +00001165 );
1166 if (xret == -1)
1167 return PTP_ERROR_IO;
Richard Low02724f62007-02-17 09:47:26 +00001168 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
Linus Walleija0323272007-01-07 12:21:30 +00001169 break;
1170 }
1171 return PTP_RC_OK;
1172 }
1173 if (rlen > dtoh32(usbdata.length)) {
1174 /*
1175 * Buffer the surplus response packet if it is >=
1176 * PTP_USB_BULK_HDR_LEN
1177 * (i.e. it is probably an entire package)
1178 * else discard it as erroneous surplus data.
1179 * This will even work if more than 2 packets appear
1180 * in the same transaction, they will just be handled
1181 * iteratively.
1182 *
1183 * Marcus observed stray bytes on iRiver devices;
1184 * these are still discarded.
1185 */
1186 unsigned int packlen = dtoh32(usbdata.length);
1187 unsigned int surplen = rlen - packlen;
1188
1189 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1190 params->response_packet = malloc(surplen);
1191 memcpy(params->response_packet,
1192 (uint8_t *) &usbdata + packlen, surplen);
1193 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001194 /* Ignore reading one extra byte if device flags have been set */
1195 } else if( !(((PTP_USB *)params->data)->device_flags &
1196 DEVICE_FLAG_NO_ZERO_READS &&
1197 rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001198 ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
1199 "too much, expect problems!",
1200 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001201 }
1202 rlen = packlen;
1203 }
1204
1205 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1206 * here. For MTP devices splitting header and data it might
1207 * be 12.
1208 */
1209 /* Evaluate full data length. */
1210 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1211
1212 /* autodetect split header/data MTP devices */
1213 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1214 params->split_header_data = 1;
1215
Linus Walleija0323272007-01-07 12:21:30 +00001216 /* Copy first part of data to 'data' */
1217 handler->putfunc(
1218 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1219 &written
1220 );
Richard Low4fd59d62007-03-03 16:34:37 +00001221
Linus Walleij049f50c2007-03-03 20:30:43 +00001222 if (((PTP_USB *)params->data)->device_flags & DEVICE_FLAG_NO_ZERO_READS &&
1223 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
Richard Low057ea772007-03-24 13:37:38 +00001224#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleij049f50c2007-03-03 20:30:43 +00001225 printf("Reading in extra terminating byte\n");
Richard Low4fd59d62007-03-03 16:34:37 +00001226#endif
Linus Walleij049f50c2007-03-03 20:30:43 +00001227 // need to read in extra byte and discard it
1228 int result = 0;
1229 char byte = 0;
1230 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1231 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptpcam_usb_timeout);
1232
1233 if (result != 1)
1234 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 Low057ea772007-03-24 13:37:38 +00001235 } else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ && params->split_header_data == 0) {
1236#ifdef ENABLE_USB_BULK_DEBUG
1237 printf("Reading in zero packet after header\n");
1238#endif
1239 int zeroresult = 0;
1240 char zerobyte = 0;
1241 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1242 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptpcam_usb_timeout);
1243
1244 if (zeroresult != 0)
1245 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
1246 }
1247
Linus Walleija0323272007-01-07 12:21:30 +00001248 /* Is that all of data? */
1249 if (len+PTP_USB_BULK_HDR_LEN<=rlen) break;
Linus Walleij049f50c2007-03-03 20:30:43 +00001250
1251 ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1252 handler,
1253 params->data, &rlen, 1);
1254
Linus Walleija0323272007-01-07 12:21:30 +00001255 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001256 ret = PTP_ERROR_IO;
1257 break;
Linus Walleija0323272007-01-07 12:21:30 +00001258 }
1259 } while (0);
Linus Walleij049f50c2007-03-03 20:30:43 +00001260 /*
Linus Walleija0323272007-01-07 12:21:30 +00001261 if (ret!=PTP_RC_OK) {
1262 ptp_error (params,
1263 "PTP: request code 0x%04x getting data error 0x%04x",
1264 ptp->Code, ret);
1265 }*/
1266 return ret;
1267}
1268
1269uint16_t
1270ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1271{
1272 uint16_t ret;
1273 unsigned long rlen;
1274 PTPUSBBulkContainer usbresp;
1275
1276 memset(&usbresp,0,sizeof(usbresp));
1277 /* read response, it should never be longer than sizeof(usbresp) */
1278 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1279
1280 if (ret!=PTP_RC_OK) {
1281 ret = PTP_ERROR_IO;
1282 } else
1283 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1284 ret = PTP_ERROR_RESP_EXPECTED;
1285 } else
1286 if (dtoh16(usbresp.code)!=resp->Code) {
1287 ret = dtoh16(usbresp.code);
1288 }
1289 if (ret!=PTP_RC_OK) {
1290/* ptp_error (params,
1291 "PTP: request code 0x%04x getting resp error 0x%04x",
1292 resp->Code, ret);*/
1293 return ret;
1294 }
1295 /* build an appropriate PTPContainer */
1296 resp->Code=dtoh16(usbresp.code);
1297 resp->SessionID=params->session_id;
1298 resp->Transaction_ID=dtoh32(usbresp.trans_id);
1299 resp->Param1=dtoh32(usbresp.payload.params.param1);
1300 resp->Param2=dtoh32(usbresp.payload.params.param2);
1301 resp->Param3=dtoh32(usbresp.payload.params.param3);
1302 resp->Param4=dtoh32(usbresp.payload.params.param4);
1303 resp->Param5=dtoh32(usbresp.payload.params.param5);
1304 return ret;
1305}
1306
1307/* Event handling functions */
1308
1309/* PTP Events wait for or check mode */
1310#define PTP_EVENT_CHECK 0x0000 /* waits for */
1311#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1312
1313static inline uint16_t
1314ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1315{
1316 uint16_t ret;
1317 int result;
1318 unsigned long rlen;
1319 PTPUSBEventContainer usbevent;
1320 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1321
1322 memset(&usbevent,0,sizeof(usbevent));
1323
1324 if ((params==NULL) || (event==NULL))
1325 return PTP_ERROR_BADPARAM;
1326 ret = PTP_RC_OK;
1327 switch(wait) {
1328 case PTP_EVENT_CHECK:
1329 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1330 if (result==0)
1331 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1332 if (result < 0) ret = PTP_ERROR_IO;
1333 break;
1334 case PTP_EVENT_CHECK_FAST:
1335 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1336 if (result==0)
1337 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1338 if (result < 0) ret = PTP_ERROR_IO;
1339 break;
1340 default:
1341 ret=PTP_ERROR_BADPARAM;
1342 break;
1343 }
1344 if (ret!=PTP_RC_OK) {
1345 ptp_error (params,
1346 "PTP: reading event an error 0x%04x occurred", ret);
1347 return PTP_ERROR_IO;
1348 }
1349 rlen = result;
1350 if (rlen < 8) {
1351 ptp_error (params,
1352 "PTP: reading event an short read of %ld bytes occurred", rlen);
1353 return PTP_ERROR_IO;
1354 }
1355 /* if we read anything over interrupt endpoint it must be an event */
1356 /* build an appropriate PTPContainer */
1357 event->Code=dtoh16(usbevent.code);
1358 event->SessionID=params->session_id;
1359 event->Transaction_ID=dtoh32(usbevent.trans_id);
1360 event->Param1=dtoh32(usbevent.param1);
1361 event->Param2=dtoh32(usbevent.param2);
1362 event->Param3=dtoh32(usbevent.param3);
1363 return ret;
1364}
1365
1366uint16_t
1367ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1368
1369 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1370}
1371
1372uint16_t
1373ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1374
1375 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1376}
1377
1378
Linus Walleij9eb3d312006-08-04 19:25:59 +00001379static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001380{
Linus Walleijd6a49972006-05-02 08:24:54 +00001381 usb_dev_handle *device_handle;
1382
Linus Walleijd6a49972006-05-02 08:24:54 +00001383 params->error_func=NULL;
1384 params->debug_func=NULL;
1385 params->sendreq_func=ptp_usb_sendreq;
1386 params->senddata_func=ptp_usb_senddata;
1387 params->getresp_func=ptp_usb_getresp;
1388 params->getdata_func=ptp_usb_getdata;
1389 params->data=ptp_usb;
1390 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001391 /*
1392 * This is hardcoded here since we have no devices whatsoever that are BE.
1393 * Change this the day we run into our first BE device (if ever).
1394 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001395 params->byteorder = PTP_DL_LE;
1396
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001397 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001398 if (!device_handle) {
1399 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001400 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001401 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001402 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001403#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1404 /*
1405 * If this device is known to be wrongfully claimed by other kernel
1406 * drivers (such as mass storage), then try to unload it to make it
1407 * accessible from user space.
1408 */
1409 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
1410 if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij7beba572006-11-29 08:56:12 +00001411 // Totally ignore this error!
1412 // perror("usb_detach_kernel_driver_np()");
Linus Walleij501ba4d2006-10-16 06:17:04 +00001413 }
1414 }
1415#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001416#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001417 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001418 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1419 perror("usb_set_configuration()");
1420 return -1;
1421 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001422#endif
Linus Walleijef5cca92006-05-05 07:19:08 +00001423 if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001424 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001425 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001426 }
Linus Walleijc6210fb2006-05-08 11:11:41 +00001427 ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber;
Linus Walleijd6a49972006-05-02 08:24:54 +00001428 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001429 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001430}
1431
Linus Walleijd6a49972006-05-02 08:24:54 +00001432static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001433{
Linus Walleij58b62792007-01-07 12:38:59 +00001434 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001435 int ret;
1436
1437 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001438 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001439 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001440 if (ret<0) {
1441 perror ("inep: usb_get_endpoint_status()");
1442 } else if (status) {
1443 printf("Clearing stall on IN endpoint\n");
1444 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1445 if (ret<0) {
1446 perror ("usb_clear_stall_feature()");
1447 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001448 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001449
1450 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001451 status=0;
1452 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1453 if (ret<0) {
1454 perror("outep: usb_get_endpoint_status()");
1455 } else if (status) {
1456 printf("Clearing stall on OUT endpoint\n");
1457 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1458 if (ret<0) {
1459 perror("usb_clear_stall_feature()");
1460 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001461 }
Linus Walleij58b62792007-01-07 12:38:59 +00001462
1463 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1464}
1465
1466static void clear_halt(PTP_USB* ptp_usb)
1467{
1468 int ret;
1469
1470 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1471 if (ret<0) {
1472 perror("usb_clear_halt() on IN endpoint");
1473 }
1474 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1475 if (ret<0) {
1476 perror("usb_clear_halt() on OUT endpoint");
1477 }
1478 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1479 if (ret<0) {
1480 perror("usb_clear_halt() on INTERRUPT endpoint");
1481 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001482}
1483
Linus Walleijd6a49972006-05-02 08:24:54 +00001484static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001485{
Linus Walleij58b62792007-01-07 12:38:59 +00001486 // Clear any stalled endpoints
Linus Walleijd6a49972006-05-02 08:24:54 +00001487 clear_stall(ptp_usb);
Linus Walleij58b62792007-01-07 12:38:59 +00001488 // Clear halts on any endpoints
1489 clear_halt(ptp_usb);
Linus Walleijd6a49972006-05-02 08:24:54 +00001490 // Added to clear some stuff on the OUT endpoint
1491 // TODO: is this good on the Mac too?
1492 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1493 usb_release_interface(ptp_usb->handle, interfaceNumber);
1494 // Brutally reset device
1495 // TODO: is this good on the Mac too?
1496 usb_reset(ptp_usb->handle);
1497 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001498}
1499
Linus Walleij45a86372007-03-07 09:36:19 +00001500static LIBMTP_error_number_t prime_device_memory(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001501{
Linus Walleij45a86372007-03-07 09:36:19 +00001502 mtpdevice_list_t *tmplist = devlist;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001503
Linus Walleij45a86372007-03-07 09:36:19 +00001504 while (tmplist != NULL) {
tedbullock0f033cb2007-02-14 20:56:54 +00001505 /* Allocate a parameter box */
Linus Walleij45a86372007-03-07 09:36:19 +00001506 tmplist->params = (PTPParams *) malloc(sizeof(PTPParams));
1507 tmplist->ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleijbfcb7922007-03-06 20:14:04 +00001508
tedbullock0f033cb2007-02-14 20:56:54 +00001509 /* Check for allocation Error */
Linus Walleij45a86372007-03-07 09:36:19 +00001510 if(tmplist->params == NULL || tmplist->ptp_usb == NULL) {
Linus Walleijbfcb7922007-03-06 20:14:04 +00001511 /* Error and deallocation of memory will be handled by caller. */
tedbullock0f033cb2007-02-14 20:56:54 +00001512 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1513 }
1514
tedbullock69a445b2007-02-15 07:41:04 +00001515 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleij45a86372007-03-07 09:36:19 +00001516 memset(tmplist->params, 0, sizeof(PTPParams));
1517 memset(tmplist->ptp_usb, 0, sizeof(PTP_USB));
1518 tmplist = tmplist->next;
tedbullock0f033cb2007-02-14 20:56:54 +00001519 }
tedbullock0f033cb2007-02-14 20:56:54 +00001520 return LIBMTP_ERROR_NONE;
1521}
1522
Linus Walleij45a86372007-03-07 09:36:19 +00001523static void assign_known_device_flags(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001524{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001525 int i;
Linus Walleij27c50c52007-03-06 20:34:48 +00001526 mtpdevice_list_t *tmplist = devlist;
1527 uint8_t current_device = 0;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001528
tedbullock0f033cb2007-02-14 20:56:54 +00001529 /* Search through known device list and set correct device flags */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001530 while (tmplist != NULL) {
1531 int device_known = 0;
1532
1533 for(i = 0; i < mtp_device_table_size; i++) {
1534 if(tmplist->libusb_device->descriptor.idVendor == mtp_device_table[i].vendor_id &&
1535 tmplist->libusb_device->descriptor.idProduct == mtp_device_table[i].product_id) {
1536 /* This device is known, assign the correct device flags */
1537 /* Note that ptp_usb[current_device] could potentially be NULL */
Linus Walleij45a86372007-03-07 09:36:19 +00001538 if(tmplist->ptp_usb != NULL) {
1539 tmplist->ptp_usb->device_flags = mtp_device_table[i].device_flags;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001540
1541 /*
1542 * TODO:
1543 * Preferable to not do this with #ifdef ENABLE_USB_BULK_DEBUG but there is
1544 * currently no other compile time debug option
1545 */
1546
1547 device_known = 1;
tedbullock65a05772007-03-03 22:56:56 +00001548#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001549 /* This device is known to the developers */
1550 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is a %s.\n",
1551 current_device + 1,
1552 tmplist->libusb_device->descriptor.idVendor,
1553 tmplist->libusb_device->descriptor.idProduct,
1554 mtp_device_table[i].name);
tedbullock65a05772007-03-03 22:56:56 +00001555#endif
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001556 }
1557 break;
1558 }
tedbullock0f033cb2007-02-14 20:56:54 +00001559 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001560 if (!device_known) {
1561 /* This device is unknown to the developers */
1562 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
1563 current_device + 1,
1564 tmplist->libusb_device->descriptor.idVendor,
1565 tmplist->libusb_device->descriptor.idProduct);
1566 fprintf(stderr, "Please report this VID/PID and the device model to the "
1567 "libmtp development team\n");
1568 }
1569 tmplist = tmplist->next;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001570 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001571 }
tedbullock0f033cb2007-02-14 20:56:54 +00001572}
1573
tedbullock0f033cb2007-02-14 20:56:54 +00001574
Linus Walleij45a86372007-03-07 09:36:19 +00001575static LIBMTP_error_number_t configure_usb_devices(mtpdevice_list_t *devicelist)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001576{
1577 mtpdevice_list_t *tmplist = devicelist;
1578 uint16_t ret = 0;
1579 uint8_t current_device = 0;
1580
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001581 while (tmplist != NULL) {
Linus Walleij45a86372007-03-07 09:36:19 +00001582 /* This is erroneous, there must be a PTP_USB instance that we can initialize. */
1583 if(tmplist->ptp_usb == NULL) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001584 return LIBMTP_ERROR_MEMORY_ALLOCATION;
tedbullock0f033cb2007-02-14 20:56:54 +00001585 }
1586
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001587 /* TODO: Will this always be little endian? */
Linus Walleij45a86372007-03-07 09:36:19 +00001588 tmplist->params->byteorder = PTP_DL_LE;
1589 tmplist->params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8");
1590 tmplist->params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE");
tedbullock0f033cb2007-02-14 20:56:54 +00001591
Linus Walleij45a86372007-03-07 09:36:19 +00001592 if(tmplist->params->cd_locale_to_ucs2 == (iconv_t) -1 ||
1593 tmplist->params->cd_ucs2_to_locale == (iconv_t) -1) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001594 fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
1595 "Too old stdlibc, glibc and libiconv?\n");
Linus Walleij35c0ef12007-03-06 20:49:09 +00001596 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001597 }
1598
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001599 // ep = device->config->interface->altsetting->endpoint;
1600 // no_of_ep = device->config->interface->altsetting->bNumEndpoints;
tedbullock0f033cb2007-02-14 20:56:54 +00001601
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001602 /* Assign endpoints to usbinfo... */
1603 find_endpoints(tmplist->libusb_device,
Linus Walleij45a86372007-03-07 09:36:19 +00001604 &tmplist->ptp_usb->inep,
1605 &tmplist->ptp_usb->inep_maxpacket,
1606 &tmplist->ptp_usb->outep,
1607 &tmplist->ptp_usb->outep_maxpacket,
1608 &tmplist->ptp_usb->intep);
tedbullock0f033cb2007-02-14 20:56:54 +00001609
Linus Walleij45a86372007-03-07 09:36:19 +00001610 /* Attempt to initialize this device */
1611 if (init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) < 0) {
1612 fprintf(stderr, "LIBMTP PANIC: Unable to initialize device %d\n", current_device+1);
1613 // FIXME: perhaps use "continue" to keep trying the other devices.
Linus Walleij35c0ef12007-03-06 20:49:09 +00001614 return LIBMTP_ERROR_CONNECTING;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001615 }
1616
1617 /*
1618 * This works in situations where previous bad applications
1619 * have not used LIBMTP_Release_Device on exit
1620 */
Linus Walleij45a86372007-03-07 09:36:19 +00001621 if ((ret = ptp_opensession(tmplist->params, 1)) == PTP_ERROR_IO) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001622 fprintf(stderr, "PTP_ERROR_IO: Trying again after resetting USB\n");
Linus Walleij45a86372007-03-07 09:36:19 +00001623 close_usb(tmplist->ptp_usb, tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001624
Linus Walleij45a86372007-03-07 09:36:19 +00001625 if(init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) <0) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001626 fprintf(stderr, "LIBMTP PANIC: Could not open session on device %d\n", current_device+1);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001627 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001628 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001629
1630 /* Device has been reset, try again */
Linus Walleij45a86372007-03-07 09:36:19 +00001631 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001632 }
1633
1634 /* Was the transaction id invalid? Try again */
1635 if (ret == PTP_RC_InvalidTransactionID) {
Linus Walleij45a86372007-03-07 09:36:19 +00001636 fprintf(stderr, "LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
1637 tmplist->params->transaction_id += 10;
1638 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001639 }
1640
1641 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
Linus Walleij45a86372007-03-07 09:36:19 +00001642 fprintf(stderr, "LIBMTP PANIC: Could not open session! "
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001643 "(Return code %d)\n Try to reset the device.\n",
1644 ret);
Linus Walleij45a86372007-03-07 09:36:19 +00001645 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001646 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001647 return LIBMTP_ERROR_CONNECTING;
1648 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001649
1650 /* It is permissible to call this before opening the session */
Linus Walleij45a86372007-03-07 09:36:19 +00001651 if (ptp_getdeviceinfo(tmplist->params,
1652 &tmplist->params->deviceinfo) != PTP_RC_OK) {
1653 fprintf(stderr, "LIBMTP PANIC: Could not get device info!\n");
1654 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001655 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001656 return LIBMTP_ERROR_CONNECTING;
1657 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001658
1659 tmplist = tmplist->next;
1660 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001661 }
1662
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001663 /* Exit with the nice list */
1664 return LIBMTP_ERROR_NONE;
tedbullock0f033cb2007-02-14 20:56:54 +00001665}
1666
Linus Walleij550d6d52007-01-24 14:32:51 +00001667/**
1668 * This function scans through the results of the get_mtp_usb_device_list
1669 * function and attempts to connect to those devices listed using the
1670 * mtp_device_table at the top of the file. Returns a LIBMTP_error_number_t.
1671 *
Linus Walleij45a86372007-03-07 09:36:19 +00001672 * @param devlist a list of devices with primed PTP_USB and params structs.
Linus Walleij550d6d52007-01-24 14:32:51 +00001673 * @return Error Codes as per the type definition
1674 */
Linus Walleij45a86372007-03-07 09:36:19 +00001675LIBMTP_error_number_t find_usb_devices(mtpdevice_list_t **devlist)
Linus Walleij550d6d52007-01-24 14:32:51 +00001676{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001677 mtpdevice_list_t *mtp_device_list = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001678 LIBMTP_error_number_t ret;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001679
Linus Walleija4942fc2007-03-12 19:23:21 +00001680 /*
1681 * Recover list of attached USB devices that match MTP criteria, i.e.
1682 * it either has an MTP device descriptor or it is in the known
1683 * devices list.
1684 */
Linus Walleij45a86372007-03-07 09:36:19 +00001685 ret = get_mtp_usb_device_list (&mtp_device_list);
Linus Walleijac061862007-03-07 08:28:33 +00001686 if (ret != LIBMTP_ERROR_NONE) {
1687 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001688 }
1689
Linus Walleij45a86372007-03-07 09:36:19 +00001690 // Then prime them
1691 ret = prime_device_memory(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001692 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001693 fprintf(stderr, "LIBMTP PANIC: prime_device_memory() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001694 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001695 }
tedbullock0f033cb2007-02-14 20:56:54 +00001696
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001697 /* Assign specific device flags and detect unknown devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001698 assign_known_device_flags(mtp_device_list);
Linus Walleij550d6d52007-01-24 14:32:51 +00001699
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001700 /* Configure the devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001701 ret = configure_usb_devices(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001702 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001703 fprintf(stderr, "LIBMTP PANIC: configure_usb_devices() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001704 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001705 }
Linus Walleij550d6d52007-01-24 14:32:51 +00001706
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001707 /* Configure interface numbers */
Linus Walleij550d6d52007-01-24 14:32:51 +00001708 {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001709 mtpdevice_list_t *tmplist = mtp_device_list;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001710
Linus Walleij45a86372007-03-07 09:36:19 +00001711 while (tmplist != NULL) {
1712 tmplist->interface_number = tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001713 tmplist = tmplist->next;
Linus Walleij550d6d52007-01-24 14:32:51 +00001714 }
1715 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001716
Linus Walleij45a86372007-03-07 09:36:19 +00001717 /* we're connected to all devices, return the list and OK */
1718 *devlist = mtp_device_list;
1719 return LIBMTP_ERROR_NONE;
1720
1721 find_usb_devices_error_exit:
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001722 if(mtp_device_list != NULL) {
1723 free_mtpdevice_list(mtp_device_list);
1724 mtp_device_list = NULL;
Linus Walleij550d6d52007-01-24 14:32:51 +00001725 }
Linus Walleij45a86372007-03-07 09:36:19 +00001726 *devlist = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001727 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001728}
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001729
Linus Walleijd6a49972006-05-02 08:24:54 +00001730static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int *outep_maxpacket, int* intep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001731{
Linus Walleijd6a49972006-05-02 08:24:54 +00001732 int i,n;
1733 struct usb_endpoint_descriptor *ep;
1734
1735 ep = dev->config->interface->altsetting->endpoint;
1736 n=dev->config->interface->altsetting->bNumEndpoints;
1737
1738 for (i=0;i<n;i++) {
1739 if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1740 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1741 USB_ENDPOINT_DIR_MASK)
1742 {
1743 *inep=ep[i].bEndpointAddress;
1744 *inep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001745 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001746 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1747 {
1748 *outep=ep[i].bEndpointAddress;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001749 *outep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijd6a49972006-05-02 08:24:54 +00001750 }
1751 } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1752 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1753 USB_ENDPOINT_DIR_MASK)
1754 {
1755 *intep=ep[i].bEndpointAddress;
1756 }
1757 }
1758 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001759}
1760
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001761void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber)
1762{
Linus Walleijd6a49972006-05-02 08:24:54 +00001763 if (ptp_closesession(params)!=PTP_RC_OK)
1764 fprintf(stderr,"ERROR: Could not close session!\n");
1765 close_usb(ptp_usb, interfaceNumber);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001766}
1767
Linus Walleijd6a49972006-05-02 08:24:54 +00001768static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001769{
Linus Walleijd6a49972006-05-02 08:24:54 +00001770
1771 return (usb_control_msg(ptp_usb->handle,
1772 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
1773 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001774}
1775
Linus Walleijd6a49972006-05-02 08:24:54 +00001776static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001777{
Linus Walleijd6a49972006-05-02 08:24:54 +00001778 return (usb_control_msg(ptp_usb->handle,
1779 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
1780 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001781}