blob: aea1d2122946ef94b944d0f87f68093ca4e6923d [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 Walleijf41abe52007-03-20 19:25:18 +0000170 { "SanDisk Sansa e200", 0x0781, 0x7420, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000171 // Reported by gonkflea@users.sourceforge.net
Linus Walleijf41abe52007-03-20 19:25:18 +0000172 { "SanDisk Sansa e260", 0x0781, 0x7420, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij1df47cd2007-01-24 10:19:52 +0000173 // Reported by anonymous user at sourceforge.net
Linus Walleijf41abe52007-03-20 19:25:18 +0000174 { "SanDisk Sansa c250", 0x0781, 0x7450, DEVICE_FLAG_UNLOAD_DRIVER },
Richard Lowbf2675c2007-03-18 18:20:26 +0000175 // Reported by XNJB user
Linus Walleijf41abe52007-03-20 19:25:18 +0000176 { "SanDisk Sansa e280", 0x0781, 0x7421, DEVICE_FLAG_UNLOAD_DRIVER },
Richard Low856f3452007-04-13 11:21:26 +0000177 // Reported by XNJB user
178 { "SanDisk Sansa Connect", 0x0781, 0x7480, DEVICE_FLAG_UNLOAD_DRIVER },
179
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>
262 { "Archos 504 (MTP mode)", 0x0e79, 0x1307, DEVICE_FLAG_NONE },
Linus Walleije5430902006-10-08 14:46:03 +0000263
264 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000265 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000266 * This unit is falsely detected as USB mass storage in Linux
Linus Walleijcc4dd482006-11-25 21:58:30 +0000267 * prior to kernel 2.6.19 (fixed by patch from Alan Stern)
268 * so on older kernels special care is needed to remove the
269 * USB mass storage driver that erroneously binds to the device
270 * interface.
Linus Walleije5430902006-10-08 14:46:03 +0000271 */
Richard Lowc7d48d22006-11-29 18:24:55 +0000272 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER},
273
274 /*
275 * Microsoft
276 */
277 // Reported by Farooq Zaman
278 { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE },
279
280 /*
281 * Sirius
282 */
Linus Walleijb01d18b2006-12-13 12:49:15 +0000283 { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE },
284
285 /*
286 * Canon
287 * This is actually a camera, but it has a Microsoft device descriptor
288 * and reports itself as supporting the MTP extension.
289 */
Linus Walleijc1b49402007-02-07 08:00:38 +0000290 {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE },
291
292 /*
293 * Nokia
294 */
Linus Walleijde7ad542007-02-14 09:32:26 +0000295 {"Nokia Mobile Phones (MTP mode)", 0x0421, 0x04e1, DEVICE_FLAG_NONE },
296
297 /*
298 * LOGIK
299 * Sold in the UK, seem to be manufactured by CCTech in China.
300 */
Linus Walleijd2ff9e32007-03-16 15:27:45 +0000301 {"Logik LOG DAX MP3 and DAB Player", 0x13d1, 0x7002, DEVICE_FLAG_NONE },
302
303 /*
304 * Other strange stuff.
305 */
306 {"Isabella's prototype", 0x0b20, 0xddee, DEVICE_FLAG_NONE }
Linus Walleija5483642006-03-09 09:20:38 +0000307};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000308static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000309
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000310int ptpcam_usb_timeout = USB_TIMEOUT;
311
Linus Walleijd6a49972006-05-02 08:24:54 +0000312// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000313static struct usb_bus* init_usb();
Linus Walleijd6a49972006-05-02 08:24:54 +0000314static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);
Linus Walleijd6a49972006-05-02 08:24:54 +0000315static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);
316static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000317static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000318static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000319static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000320static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
321static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000322
Linus Walleij552ba322007-01-22 11:49:59 +0000323/**
324 * Get a list of the supported USB devices.
325 *
326 * The developers depend on users of this library to constantly
327 * add in to the list of supported devices. What we need is the
328 * device name, USB Vendor ID (VID) and USB Product ID (PID).
329 * put this into a bug ticket at the project homepage, please.
330 * The VID/PID is used to let e.g. udev lift the device to
331 * console userspace access when it's plugged in.
332 *
333 * @param devices a pointer to a pointer that will hold a device
334 * list after the call to this function, if it was
335 * successful.
336 * @param numdevs a pointer to an integer that will hold the number
337 * of devices in the device list if the call was successful.
338 * @return 0 if the list was successfull retrieved, any other
339 * value means failure.
340 */
341int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000342{
343 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
344 *numdevs = mtp_device_table_size;
345 return 0;
346}
347
Linus Walleij552ba322007-01-22 11:49:59 +0000348
Linus Walleij1fd2d272006-05-08 09:22:01 +0000349static struct usb_bus* init_usb()
350{
351 usb_init();
352 usb_find_busses();
353 usb_find_devices();
354 return (usb_get_busses());
355}
356
357/**
tedbullock0f033cb2007-02-14 20:56:54 +0000358 * Small recursive function to append a new usb_device to the linked list of
359 * USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000360 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000361 * properties.
362 * @param next New USB MTP device to be added to list
363 * @return nothing
364 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000365static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
366 struct usb_device *newdevice)
tedbullock0f033cb2007-02-14 20:56:54 +0000367{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000368 mtpdevice_list_t *new_list_entry;
tedbullock0f033cb2007-02-14 20:56:54 +0000369
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000370 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
371 if (new_list_entry == NULL) {
372 return NULL;
373 }
374 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
375 new_list_entry->libusb_device = newdevice;
376 new_list_entry->next = NULL;
377
378 if (devlist == NULL) {
379 return new_list_entry;
380 } else {
381 mtpdevice_list_t *tmp = devlist;
382 while (tmp->next != NULL) {
383 tmp = tmp->next;
384 }
385 tmp->next = new_list_entry;
386 }
387 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000388}
389
390/**
391 * Small recursive function to free dynamic memory allocated to the linked list
392 * of USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000393 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000394 * properties.
395 * @return nothing
396 */
Linus Walleij45a86372007-03-07 09:36:19 +0000397void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000398{
Linus Walleijac061862007-03-07 08:28:33 +0000399 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000400
401 if (devlist == NULL)
402 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000403 while (tmplist != NULL) {
404 mtpdevice_list_t *tmp = tmplist;
405 tmplist = tmplist->next;
Linus Walleij45a86372007-03-07 09:36:19 +0000406 // Do not free() the fields (ptp_usb, parms)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000407 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000408 }
tedbullock0f033cb2007-02-14 20:56:54 +0000409 return;
410}
411
412/**
Linus Walleija4942fc2007-03-12 19:23:21 +0000413 * This checks if a device has an MTP descriptor.
414 * @param dev a device struct from libusb.
415 * @return 1 if the device is MTP compliant, 0 if not.
416 */
417static int device_has_descriptor(struct usb_device *dev)
418{
419 usb_dev_handle *devh;
420 unsigned char buf[1024], cmd;
421 int ret;
422
423 /* Don't examine hubs (no point in that) */
424 if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) {
425 return 0;
426 }
427
428 /* Attempt to open Device on this port */
429 devh = usb_open(dev);
430 if (devh == NULL) {
431 /* Could not open this device */
432 return 0;
433 }
434
435 /* Read the special descriptor */
436 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
437
438 /* Check if descriptor length is at least 10 bytes */
439 if (ret < 10) {
440 usb_close(devh);
441 return 0;
442 }
443
444 /* Check if this device has a Microsoft Descriptor */
445 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
446 (buf[6] == 'F') && (buf[8] == 'T'))) {
447 usb_close(devh);
448 return 0;
449 }
450
451 /* Check if device responds to control message 1 or if there is an error */
452 cmd = buf[16];
453 ret = usb_control_msg (devh,
454 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
455 cmd,
456 0,
457 4,
458 (char *) buf,
459 sizeof(buf),
460 1000);
461
462 /* If this is true, the device either isn't MTP or there was an error */
463 if (ret <= 0x15) {
464 /* TODO: If there was an error, flag it and let the user know somehow */
465 /* if(ret == -1) {} */
466 usb_close(devh);
467 return 0;
468 }
469
470 /* Check if device is MTP or if it is something like a USB Mass Storage
471 device with Janus DRM support */
472 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
473 usb_close(devh);
474 return 0;
475 }
476
477 /* After this point we are probably dealing with an MTP device */
478
479 /* Check if device responds to control message 2 or if there is an error*/
480 ret = usb_control_msg (devh,
481 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
482 cmd,
483 0,
484 5,
485 (char *) buf,
486 sizeof(buf),
487 1000);
488
489 /* If this is true, the device errored against control message 2 */
490 if (ret == -1) {
491 /* TODO: Implement callback function to let managing program know there
492 was a problem, along with description of the problem */
493 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
494 "ProductID:%04x encountered an error responding to "
495 "control message 2.\n"
496 "Problems may arrise but continuing\n",
497 dev->descriptor.idVendor, dev->descriptor.idProduct);
498 } else if (ret <= 0x15) {
499 /* TODO: Implement callback function to let managing program know there
500 was a problem, along with description of the problem */
501 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
502 "ProductID:%04x responded to control message 2 with a "
503 "response that was too short. Problems may arrise but "
504 "continuing\n",
505 dev->descriptor.idVendor, dev->descriptor.idProduct);
506 } else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
507 /* TODO: Implement callback function to let managing program know there
508 was a problem, along with description of the problem */
509 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
510 "ProductID:%04x encountered an error responding to "
511 "control message 2\n"
512 "Problems may arrise but continuing\n",
513 dev->descriptor.idVendor, dev->descriptor.idProduct);
514 }
515
516 /* Close the USB device handle */
517 usb_close(devh);
518 return 1;
519}
520
521/**
522 * This function scans through the connected usb devices on a machine and
523 * if they match known Vendor and Product identifiers appends them to the
524 * dynamic array mtp_device_list. Be sure to call
525 * <code>free(mtp_device_list)</code> when you are done with it, assuming it
526 * is not NULL.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000527 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000528 * properties (if this list is not empty, new entries will be appended
529 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000530 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
531 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no devices have
532 * been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000533 */
Linus Walleij45a86372007-03-07 09:36:19 +0000534static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000535{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000536 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000537
538 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000539 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000540 for (; dev != NULL; dev = dev->next) {
Linus Walleija4942fc2007-03-12 19:23:21 +0000541 if (device_has_descriptor(dev)) {
542 /* Append this usb device to the MTP USB Device List */
543 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
544 } else {
545 /* Check if it's in the known devices list then */
546 int i;
547
548 for(i = 0; i < mtp_device_table_size; i++) {
549 if(dev->descriptor.bDeviceClass != USB_CLASS_HUB &&
550 dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
551 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
552 /* Append this usb device to the MTP device list */
553 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
554 break;
555 }
556 }
Linus Walleij35c0ef12007-03-06 20:49:09 +0000557 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000558 }
559 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000560
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000561 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000562 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000563 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000564 }
565 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000566}
567
568/**
Linus Walleij1fd2d272006-05-08 09:22:01 +0000569 * Detect the MTP device descriptor and return the VID and PID
570 * of the first device found. This is a very low-level function
571 * which is intended for use with <b>udev</b> or other hotplug
572 * mechanisms. The idea is that a script may want to know if the
573 * just plugged-in device was an MTP device or not.
tedbullock2b45f2e2007-02-23 20:16:37 +0000574 *
Linus Walleij1fd2d272006-05-08 09:22:01 +0000575 * @param vid the Vendor ID (VID) of the first device found.
576 * @param pid the Product ID (PID) of the first device found.
577 * @return the number of detected devices or -1 if the call
578 * was unsuccessful.
579 */
580int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
581{
Linus Walleijb6b69132007-03-07 09:45:05 +0000582 mtpdevice_list_t *devlist;
583 LIBMTP_error_number_t ret;
584
585 ret = get_mtp_usb_device_list(&devlist);
586 if (ret != LIBMTP_ERROR_NONE) {
587 *vid = *pid = 0;
588 return -1;
589 }
590 *vid = devlist->libusb_device->descriptor.idVendor;
591 *pid = devlist->libusb_device->descriptor.idProduct;
592 free_mtpdevice_list(devlist);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000593 return 1;
594}
595
Linus Walleijc6210fb2006-05-08 11:11:41 +0000596/**
597 * This routine just dumps out low-level
598 * USB information about the current device.
599 * @param ptp_usb the USB device to get information from.
600 */
601void dump_usbinfo(PTP_USB *ptp_usb)
602{
603 int res;
604 struct usb_device *dev;
605
606#ifdef LIBUSB_HAS_GET_DRIVER_NP
607 char devname[0x10];
608
609 devname[0] = '\0';
610 res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));
611 if (devname[0] != '\0') {
612 printf(" Using kernel interface \"%s\"\n", devname);
613 }
614#endif
615 dev = usb_device(ptp_usb->handle);
616 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
617 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
618 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
619 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000620 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
621 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000622 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
623 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000624 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000625 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
626}
627
Linus Walleija0323272007-01-07 12:21:30 +0000628static void
629ptp_debug (PTPParams *params, const char *format, ...)
630{
631 va_list args;
632
633 va_start (args, format);
634 if (params->debug_func!=NULL)
635 params->debug_func (params->data, format, args);
636 else
637 {
638 vfprintf (stderr, format, args);
639 fprintf (stderr,"\n");
640 fflush (stderr);
641 }
642 va_end (args);
643}
644
645static void
646ptp_error (PTPParams *params, const char *format, ...)
647{
648 va_list args;
649
650 va_start (args, format);
651 if (params->error_func!=NULL)
652 params->error_func (params->data, format, args);
653 else
654 {
655 vfprintf (stderr, format, args);
656 fprintf (stderr,"\n");
657 fflush (stderr);
658 }
659 va_end (args);
660}
661
Linus Walleijc6210fb2006-05-08 11:11:41 +0000662
Linus Walleij99ed83c2007-01-02 18:46:02 +0000663/*
664 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000665 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000666 * Much reading packet logs and having fun with trials and errors
667 * reveals that WMP / Windows is probably using an algorithm like this
668 * for large transfers:
669 *
670 * 1. Send the command (0x0c bytes) if headers are split, else, send
671 * command plus sizeof(endpoint) - 0x0c bytes.
672 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
673 * split headers. Else goto 3.
674 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
675 * We call 0x10000 CONTEXT_BLOCK_SIZE.
676 * 4. Send remaining bytes MOD sizeof(endpoint)
677 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
678 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000679 *
680 * Further there is some special quirks to handle zero reads from the
681 * device, since some devices can't do them at all due to shortcomings
682 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000683 */
Richard Low4fd59d62007-03-03 16:34:37 +0000684#define CONTEXT_BLOCK_SIZE_1 0x3e00
685#define CONTEXT_BLOCK_SIZE_2 0x200
686#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000687static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000688ptp_read_func (
689 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000690 unsigned long *readbytes,
691 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000692) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000693 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000694 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000695 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000696 unsigned long curread = 0;
697 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000698 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000699 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000700
701 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000702 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000703 while (curread < size) {
Richard Low4fd59d62007-03-03 16:34:37 +0000704
705#ifdef ENABLE_USB_BULK_DEBUG
706 printf("Remaining size to read: 0x%04x bytes\n", size - curread);
707#endif
708 // check equal to condition here
709 if (size - curread < CONTEXT_BLOCK_SIZE)
710 {
711 // this is the last packet
712 toread = size - curread;
713 // this is equivalent to zero read for these devices
Linus Walleij049f50c2007-03-03 20:30:43 +0000714 if (readzero && ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000715 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000716 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000717 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000718 }
Richard Low4fd59d62007-03-03 16:34:37 +0000719 else if (curread == 0)
720 // we are first packet, but not last packet
721 toread = CONTEXT_BLOCK_SIZE_1;
722 else if (toread == CONTEXT_BLOCK_SIZE_1)
723 toread = CONTEXT_BLOCK_SIZE_2;
724 else if (toread == CONTEXT_BLOCK_SIZE_2)
725 toread = CONTEXT_BLOCK_SIZE_1;
726 else
Linus Walleij4cec9872007-03-03 21:00:53 +0000727 printf("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
728 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000729
Richard Lowd284f072007-02-17 08:52:41 +0000730#ifdef ENABLE_USB_BULK_DEBUG
731 printf("Reading in 0x%04x bytes\n", toread);
732#endif
Linus Walleij784ac3f2006-12-29 10:36:51 +0000733 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout);
Richard Lowd284f072007-02-17 08:52:41 +0000734#ifdef ENABLE_USB_BULK_DEBUG
735 printf("Result of read: 0x%04x\n", result);
736#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000737
Linus Walleijc49c6372007-01-09 00:18:51 +0000738 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000739 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000740 }
Linus Walleije78de6d2006-10-31 14:46:36 +0000741#ifdef ENABLE_USB_BULK_DEBUG
742 printf("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000743 if (result == 0)
744 printf("Zero Read\n");
745 else
746 data_dump_ascii (stdout,bytes,result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000747#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000748
749 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000750 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000751 {
752#ifdef ENABLE_USB_BULK_DEBUG
753 printf("<==USB IN\nDiscarding extra byte\n");
754#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000755 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000756 }
Richard Low4fd59d62007-03-03 16:34:37 +0000757
Linus Walleijc49c6372007-01-09 00:18:51 +0000758 handler->putfunc(NULL, handler->private, result, bytes, &written);
Richard Lowbf2675c2007-03-18 18:20:26 +0000759
Linus Walleijc49c6372007-01-09 00:18:51 +0000760 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000761 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000762
763 // Increase counters, call callback
764 if (ptp_usb->callback_active) {
765 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
766 // send last update and disable callback.
767 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
768 ptp_usb->callback_active = 0;
769 }
770 if (ptp_usb->current_transfer_callback != NULL) {
771 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
772 ptp_usb->current_transfer_total,
773 ptp_usb->current_transfer_callback_data);
774 }
775 }
776
Linus Walleijd6a49972006-05-02 08:24:54 +0000777 if (result < toread) /* short reads are common */
778 break;
779 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000780 if (readbytes) *readbytes = curread;
781 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000782
Richard Low4df32f82007-01-11 20:04:39 +0000783 // there might be a zero packet waiting for us...
Linus Walleij049f50c2007-03-03 20:30:43 +0000784 if (readzero &&
785 !(ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS) &&
786 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000787 char temp;
788 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000789
Richard Low4df32f82007-01-11 20:04:39 +0000790#ifdef ENABLE_USB_BULK_DEBUG
791 printf("<==USB IN\n");
792 printf("Zero Read\n");
793#endif
794 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptpcam_usb_timeout);
795 if (zeroresult != 0)
796 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
797 }
798
Linus Walleijd6a49972006-05-02 08:24:54 +0000799 if (result > 0) {
Linus Walleijd6a49972006-05-02 08:24:54 +0000800 return (PTP_RC_OK);
801 } else {
802 return PTP_ERROR_IO;
803 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000804}
805
Linus Walleijb02a0662006-04-25 08:05:09 +0000806static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000807ptp_write_func (
808 unsigned long size,
809 PTPDataHandler *handler,
810 void *data,
811 unsigned long *written
812) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000813 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000814 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000815 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000816 unsigned long curwrite = 0;
817 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000818
819 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000820 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000821 if (!bytes) {
822 return PTP_ERROR_IO;
823 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000824 while (curwrite < size) {
825 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000826 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000827 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000828 } else {
829 // This magic makes packets the same size that WMP send them.
830 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000831 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000832 }
833 }
834 handler->getfunc(NULL, handler->private,towrite,bytes,&towrite);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000835 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000836#ifdef ENABLE_USB_BULK_DEBUG
837 printf("USB OUT==>\n");
Richard Lowedd61832006-12-30 12:38:18 +0000838 data_dump_ascii (stdout,bytes,towrite,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000839#endif
Linus Walleijc49c6372007-01-09 00:18:51 +0000840 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000841 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000842 }
843 // Increase counters
844 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000845 curwrite += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000846
847 // call callback
848 if (ptp_usb->callback_active) {
849 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
850 // send last update and disable callback.
851 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
852 ptp_usb->callback_active = 0;
853 }
854 if (ptp_usb->current_transfer_callback != NULL) {
855 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
856 ptp_usb->current_transfer_total,
857 ptp_usb->current_transfer_callback_data);
858 }
859 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000860 if (result < towrite) /* short writes happen */
861 break;
862 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000863 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000864 if (written) {
865 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000866 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000867
Linus Walleijc49c6372007-01-09 00:18:51 +0000868
Richard Lowef05b892007-01-03 21:18:56 +0000869 // If this is the last transfer send a zero write if required
870 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000871 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
Richard Low021421e2007-01-01 18:08:57 +0000872#ifdef ENABLE_USB_BULK_DEBUG
873 printf("USB OUT==>\n");
874 printf("Zero Write\n");
875#endif
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000876 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000877 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000878 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000879
Linus Walleijd6a49972006-05-02 08:24:54 +0000880 if (result < 0)
881 return PTP_ERROR_IO;
882 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000883}
884
Linus Walleija0323272007-01-07 12:21:30 +0000885/* memory data get/put handler */
886typedef struct {
887 unsigned char *data;
888 unsigned long size, curoff;
889} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000890
Linus Walleija0323272007-01-07 12:21:30 +0000891static uint16_t
892memory_getfunc(PTPParams* params, void* private,
893 unsigned long wantlen, unsigned char *data,
894 unsigned long *gotlen
895) {
896 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
897 unsigned long tocopy = wantlen;
898
899 if (priv->curoff + tocopy > priv->size)
900 tocopy = priv->size - priv->curoff;
901 memcpy (data, priv->data + priv->curoff, tocopy);
902 priv->curoff += tocopy;
903 *gotlen = tocopy;
904 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000905}
906
Linus Walleija0323272007-01-07 12:21:30 +0000907static uint16_t
908memory_putfunc(PTPParams* params, void* private,
909 unsigned long sendlen, unsigned char *data,
910 unsigned long *putlen
911) {
912 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
913
914 if (priv->curoff + sendlen > priv->size) {
915 priv->data = realloc (priv->data, priv->curoff+sendlen);
916 priv->size = priv->curoff + sendlen;
917 }
918 memcpy (priv->data + priv->curoff, data, sendlen);
919 priv->curoff += sendlen;
920 *putlen = sendlen;
921 return PTP_RC_OK;
922}
923
924/* init private struct for receiving data. */
925static uint16_t
926ptp_init_recv_memory_handler(PTPDataHandler *handler) {
927 PTPMemHandlerPrivate* priv;
928 priv = malloc (sizeof(PTPMemHandlerPrivate));
929 handler->private = priv;
930 handler->getfunc = memory_getfunc;
931 handler->putfunc = memory_putfunc;
932 priv->data = NULL;
933 priv->size = 0;
934 priv->curoff = 0;
935 return PTP_RC_OK;
936}
937
938/* init private struct and put data in for sending data.
939 * data is still owned by caller.
940 */
941static uint16_t
942ptp_init_send_memory_handler(PTPDataHandler *handler,
943 unsigned char *data, unsigned long len
944) {
945 PTPMemHandlerPrivate* priv;
946 priv = malloc (sizeof(PTPMemHandlerPrivate));
947 if (!priv)
948 return PTP_RC_GeneralError;
949 handler->private = priv;
950 handler->getfunc = memory_getfunc;
951 handler->putfunc = memory_putfunc;
952 priv->data = data;
953 priv->size = len;
954 priv->curoff = 0;
955 return PTP_RC_OK;
956}
957
958/* free private struct + data */
959static uint16_t
960ptp_exit_send_memory_handler (PTPDataHandler *handler) {
961 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
962 /* data is owned by caller */
963 free (priv);
964 return PTP_RC_OK;
965}
966
967/* hand over our internal data to caller */
968static uint16_t
969ptp_exit_recv_memory_handler (PTPDataHandler *handler,
970 unsigned char **data, unsigned long *size
971) {
972 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
973 *data = priv->data;
974 *size = priv->size;
975 free (priv);
976 return PTP_RC_OK;
977}
978
979/* send / receive functions */
980
981uint16_t
982ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
983{
984 uint16_t ret;
985 PTPUSBBulkContainer usbreq;
986 PTPDataHandler memhandler;
987 unsigned long written, towrite;
988
989 /* build appropriate USB container */
990 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
991 (sizeof(uint32_t)*(5-req->Nparam)));
992 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
993 usbreq.code=htod16(req->Code);
994 usbreq.trans_id=htod32(req->Transaction_ID);
995 usbreq.payload.params.param1=htod32(req->Param1);
996 usbreq.payload.params.param2=htod32(req->Param2);
997 usbreq.payload.params.param3=htod32(req->Param3);
998 usbreq.payload.params.param4=htod32(req->Param4);
999 usbreq.payload.params.param5=htod32(req->Param5);
1000 /* send it to responder */
1001 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
1002 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
1003 ret=ptp_write_func(
1004 towrite,
1005 &memhandler,
1006 params->data,
1007 &written
1008 );
1009 ptp_exit_send_memory_handler (&memhandler);
1010 if (ret!=PTP_RC_OK) {
1011 ret = PTP_ERROR_IO;
1012/* ptp_error (params,
1013 "PTP: request code 0x%04x sending req error 0x%04x",
1014 req->Code,ret); */
1015 }
1016 if (written != towrite) {
1017 ptp_error (params,
1018 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001019 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001020 );
1021 ret = PTP_ERROR_IO;
1022 }
1023 return ret;
1024}
1025
1026uint16_t
1027ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1028 unsigned long size, PTPDataHandler *handler
1029) {
1030 uint16_t ret;
1031 int wlen, datawlen;
1032 unsigned long written;
1033 PTPUSBBulkContainer usbdata;
1034 uint32_t bytes_left_to_transfer;
1035 PTPDataHandler memhandler;
1036
1037 /* build appropriate USB container */
1038 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1039 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1040 usbdata.code = htod16(ptp->Code);
1041 usbdata.trans_id= htod32(ptp->Transaction_ID);
1042
1043 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001044 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001045
1046 if (params->split_header_data) {
1047 datawlen = 0;
1048 wlen = PTP_USB_BULK_HDR_LEN;
1049 } else {
1050 unsigned long gotlen;
1051 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001052 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001053 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
1054 ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen);
1055 if (ret != PTP_RC_OK)
1056 return ret;
1057 if (gotlen != datawlen)
1058 return PTP_RC_GeneralError;
1059 }
1060 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1061 /* send first part of data */
1062 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1063 ptp_exit_send_memory_handler (&memhandler);
1064 if (ret!=PTP_RC_OK) {
1065 ret = PTP_ERROR_IO;
1066/* ptp_error (params,
1067 "PTP: request code 0x%04x sending data error 0x%04x",
1068 ptp->Code,ret);*/
1069 return ret;
1070 }
1071 if (size <= datawlen) return ret;
1072 /* if everything OK send the rest */
1073 bytes_left_to_transfer = size-datawlen;
1074 ret = PTP_RC_OK;
1075 while(bytes_left_to_transfer > 0) {
1076 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1077 if (ret != PTP_RC_OK)
1078 break;
1079 if (written == 0) {
1080 ret = PTP_ERROR_IO;
1081 break;
1082 }
1083 bytes_left_to_transfer -= written;
1084 }
1085 if (ret!=PTP_RC_OK)
1086 ret = PTP_ERROR_IO;
1087 return ret;
1088}
1089
1090static uint16_t ptp_usb_getpacket(PTPParams *params,
1091 PTPUSBBulkContainer *packet, unsigned long *rlen)
1092{
1093 PTPDataHandler memhandler;
1094 uint16_t ret;
1095 unsigned char *x = NULL;
1096
1097 /* read the header and potentially the first data */
1098 if (params->response_packet_size > 0) {
1099 /* If there is a buffered packet, just use it. */
1100 memcpy(packet, params->response_packet, params->response_packet_size);
1101 *rlen = params->response_packet_size;
1102 free(params->response_packet);
1103 params->response_packet = NULL;
1104 params->response_packet_size = 0;
1105 /* Here this signifies a "virtual read" */
1106 return PTP_RC_OK;
1107 }
1108 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001109 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001110 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1111 if (x) {
1112 memcpy (packet, x, *rlen);
1113 free (x);
1114 }
1115 return ret;
1116}
1117
1118uint16_t
1119ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1120{
1121 uint16_t ret;
1122 PTPUSBBulkContainer usbdata;
1123 unsigned char *data;
1124 unsigned long written;
1125
1126 memset(&usbdata,0,sizeof(usbdata));
1127 do {
1128 unsigned long len, rlen;
1129
1130 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1131 if (ret!=PTP_RC_OK) {
1132 ret = PTP_ERROR_IO;
1133 break;
1134 } else
1135 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1136 ret = PTP_ERROR_DATA_EXPECTED;
1137 break;
1138 } else
1139 if (dtoh16(usbdata.code)!=ptp->Code) {
1140 ret = dtoh16(usbdata.code);
1141 break;
1142 }
1143 if (usbdata.length == 0xffffffffU) {
1144 /* stuff data directly to passed data handler */
1145 while (1) {
1146 unsigned long readdata;
1147 int xret;
1148
1149 xret = ptp_read_func(
Richard Low02724f62007-02-17 09:47:26 +00001150 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
Linus Walleija0323272007-01-07 12:21:30 +00001151 handler,
1152 params->data,
Richard Low4df32f82007-01-11 20:04:39 +00001153 &readdata,
Linus Walleijc0a11432007-03-02 21:21:18 +00001154 0
Linus Walleija0323272007-01-07 12:21:30 +00001155 );
1156 if (xret == -1)
1157 return PTP_ERROR_IO;
Richard Low02724f62007-02-17 09:47:26 +00001158 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
Linus Walleija0323272007-01-07 12:21:30 +00001159 break;
1160 }
1161 return PTP_RC_OK;
1162 }
1163 if (rlen > dtoh32(usbdata.length)) {
1164 /*
1165 * Buffer the surplus response packet if it is >=
1166 * PTP_USB_BULK_HDR_LEN
1167 * (i.e. it is probably an entire package)
1168 * else discard it as erroneous surplus data.
1169 * This will even work if more than 2 packets appear
1170 * in the same transaction, they will just be handled
1171 * iteratively.
1172 *
1173 * Marcus observed stray bytes on iRiver devices;
1174 * these are still discarded.
1175 */
1176 unsigned int packlen = dtoh32(usbdata.length);
1177 unsigned int surplen = rlen - packlen;
1178
1179 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1180 params->response_packet = malloc(surplen);
1181 memcpy(params->response_packet,
1182 (uint8_t *) &usbdata + packlen, surplen);
1183 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001184 /* Ignore reading one extra byte if device flags have been set */
1185 } else if( !(((PTP_USB *)params->data)->device_flags &
1186 DEVICE_FLAG_NO_ZERO_READS &&
1187 rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001188 ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
1189 "too much, expect problems!",
1190 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001191 }
1192 rlen = packlen;
1193 }
1194
1195 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1196 * here. For MTP devices splitting header and data it might
1197 * be 12.
1198 */
1199 /* Evaluate full data length. */
1200 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1201
1202 /* autodetect split header/data MTP devices */
1203 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1204 params->split_header_data = 1;
1205
Richard Low02724f62007-02-17 09:47:26 +00001206 data = malloc(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ);
Linus Walleija0323272007-01-07 12:21:30 +00001207 /* Copy first part of data to 'data' */
1208 handler->putfunc(
1209 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1210 &written
1211 );
Richard Low4fd59d62007-03-03 16:34:37 +00001212
Linus Walleij049f50c2007-03-03 20:30:43 +00001213 if (((PTP_USB *)params->data)->device_flags & DEVICE_FLAG_NO_ZERO_READS &&
1214 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
Richard Low057ea772007-03-24 13:37:38 +00001215#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleij049f50c2007-03-03 20:30:43 +00001216 printf("Reading in extra terminating byte\n");
Richard Low4fd59d62007-03-03 16:34:37 +00001217#endif
Linus Walleij049f50c2007-03-03 20:30:43 +00001218 // need to read in extra byte and discard it
1219 int result = 0;
1220 char byte = 0;
1221 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1222 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptpcam_usb_timeout);
1223
1224 if (result != 1)
1225 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 +00001226 } else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ && params->split_header_data == 0) {
1227#ifdef ENABLE_USB_BULK_DEBUG
1228 printf("Reading in zero packet after header\n");
1229#endif
1230 int zeroresult = 0;
1231 char zerobyte = 0;
1232 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1233 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptpcam_usb_timeout);
1234
1235 if (zeroresult != 0)
1236 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
1237 }
1238
Linus Walleija0323272007-01-07 12:21:30 +00001239 /* Is that all of data? */
1240 if (len+PTP_USB_BULK_HDR_LEN<=rlen) break;
Linus Walleij049f50c2007-03-03 20:30:43 +00001241
1242 ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1243 handler,
1244 params->data, &rlen, 1);
1245
Linus Walleija0323272007-01-07 12:21:30 +00001246 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001247 ret = PTP_ERROR_IO;
1248 break;
Linus Walleija0323272007-01-07 12:21:30 +00001249 }
1250 } while (0);
Linus Walleij049f50c2007-03-03 20:30:43 +00001251 /*
Linus Walleija0323272007-01-07 12:21:30 +00001252 if (ret!=PTP_RC_OK) {
1253 ptp_error (params,
1254 "PTP: request code 0x%04x getting data error 0x%04x",
1255 ptp->Code, ret);
1256 }*/
1257 return ret;
1258}
1259
1260uint16_t
1261ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1262{
1263 uint16_t ret;
1264 unsigned long rlen;
1265 PTPUSBBulkContainer usbresp;
1266
1267 memset(&usbresp,0,sizeof(usbresp));
1268 /* read response, it should never be longer than sizeof(usbresp) */
1269 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1270
1271 if (ret!=PTP_RC_OK) {
1272 ret = PTP_ERROR_IO;
1273 } else
1274 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1275 ret = PTP_ERROR_RESP_EXPECTED;
1276 } else
1277 if (dtoh16(usbresp.code)!=resp->Code) {
1278 ret = dtoh16(usbresp.code);
1279 }
1280 if (ret!=PTP_RC_OK) {
1281/* ptp_error (params,
1282 "PTP: request code 0x%04x getting resp error 0x%04x",
1283 resp->Code, ret);*/
1284 return ret;
1285 }
1286 /* build an appropriate PTPContainer */
1287 resp->Code=dtoh16(usbresp.code);
1288 resp->SessionID=params->session_id;
1289 resp->Transaction_ID=dtoh32(usbresp.trans_id);
1290 resp->Param1=dtoh32(usbresp.payload.params.param1);
1291 resp->Param2=dtoh32(usbresp.payload.params.param2);
1292 resp->Param3=dtoh32(usbresp.payload.params.param3);
1293 resp->Param4=dtoh32(usbresp.payload.params.param4);
1294 resp->Param5=dtoh32(usbresp.payload.params.param5);
1295 return ret;
1296}
1297
1298/* Event handling functions */
1299
1300/* PTP Events wait for or check mode */
1301#define PTP_EVENT_CHECK 0x0000 /* waits for */
1302#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1303
1304static inline uint16_t
1305ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1306{
1307 uint16_t ret;
1308 int result;
1309 unsigned long rlen;
1310 PTPUSBEventContainer usbevent;
1311 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1312
1313 memset(&usbevent,0,sizeof(usbevent));
1314
1315 if ((params==NULL) || (event==NULL))
1316 return PTP_ERROR_BADPARAM;
1317 ret = PTP_RC_OK;
1318 switch(wait) {
1319 case PTP_EVENT_CHECK:
1320 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1321 if (result==0)
1322 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1323 if (result < 0) ret = PTP_ERROR_IO;
1324 break;
1325 case PTP_EVENT_CHECK_FAST:
1326 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1327 if (result==0)
1328 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1329 if (result < 0) ret = PTP_ERROR_IO;
1330 break;
1331 default:
1332 ret=PTP_ERROR_BADPARAM;
1333 break;
1334 }
1335 if (ret!=PTP_RC_OK) {
1336 ptp_error (params,
1337 "PTP: reading event an error 0x%04x occurred", ret);
1338 return PTP_ERROR_IO;
1339 }
1340 rlen = result;
1341 if (rlen < 8) {
1342 ptp_error (params,
1343 "PTP: reading event an short read of %ld bytes occurred", rlen);
1344 return PTP_ERROR_IO;
1345 }
1346 /* if we read anything over interrupt endpoint it must be an event */
1347 /* build an appropriate PTPContainer */
1348 event->Code=dtoh16(usbevent.code);
1349 event->SessionID=params->session_id;
1350 event->Transaction_ID=dtoh32(usbevent.trans_id);
1351 event->Param1=dtoh32(usbevent.param1);
1352 event->Param2=dtoh32(usbevent.param2);
1353 event->Param3=dtoh32(usbevent.param3);
1354 return ret;
1355}
1356
1357uint16_t
1358ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1359
1360 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1361}
1362
1363uint16_t
1364ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1365
1366 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1367}
1368
1369
Linus Walleij9eb3d312006-08-04 19:25:59 +00001370static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001371{
Linus Walleijd6a49972006-05-02 08:24:54 +00001372 usb_dev_handle *device_handle;
1373
Linus Walleijd6a49972006-05-02 08:24:54 +00001374 params->error_func=NULL;
1375 params->debug_func=NULL;
1376 params->sendreq_func=ptp_usb_sendreq;
1377 params->senddata_func=ptp_usb_senddata;
1378 params->getresp_func=ptp_usb_getresp;
1379 params->getdata_func=ptp_usb_getdata;
1380 params->data=ptp_usb;
1381 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001382 /*
1383 * This is hardcoded here since we have no devices whatsoever that are BE.
1384 * Change this the day we run into our first BE device (if ever).
1385 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001386 params->byteorder = PTP_DL_LE;
1387
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001388 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001389 if (!device_handle) {
1390 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001391 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001392 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001393 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001394#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1395 /*
1396 * If this device is known to be wrongfully claimed by other kernel
1397 * drivers (such as mass storage), then try to unload it to make it
1398 * accessible from user space.
1399 */
1400 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
1401 if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij7beba572006-11-29 08:56:12 +00001402 // Totally ignore this error!
1403 // perror("usb_detach_kernel_driver_np()");
Linus Walleij501ba4d2006-10-16 06:17:04 +00001404 }
1405 }
1406#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001407#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001408 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001409 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1410 perror("usb_set_configuration()");
1411 return -1;
1412 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001413#endif
Linus Walleijef5cca92006-05-05 07:19:08 +00001414 if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001415 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001416 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001417 }
Linus Walleijc6210fb2006-05-08 11:11:41 +00001418 ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber;
Linus Walleijd6a49972006-05-02 08:24:54 +00001419 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001420 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001421}
1422
Linus Walleijd6a49972006-05-02 08:24:54 +00001423static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001424{
Linus Walleij58b62792007-01-07 12:38:59 +00001425 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001426 int ret;
1427
1428 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001429 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001430 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001431 if (ret<0) {
1432 perror ("inep: usb_get_endpoint_status()");
1433 } else if (status) {
1434 printf("Clearing stall on IN endpoint\n");
1435 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1436 if (ret<0) {
1437 perror ("usb_clear_stall_feature()");
1438 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001439 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001440
1441 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001442 status=0;
1443 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1444 if (ret<0) {
1445 perror("outep: usb_get_endpoint_status()");
1446 } else if (status) {
1447 printf("Clearing stall on OUT endpoint\n");
1448 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1449 if (ret<0) {
1450 perror("usb_clear_stall_feature()");
1451 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001452 }
Linus Walleij58b62792007-01-07 12:38:59 +00001453
1454 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1455}
1456
1457static void clear_halt(PTP_USB* ptp_usb)
1458{
1459 int ret;
1460
1461 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1462 if (ret<0) {
1463 perror("usb_clear_halt() on IN endpoint");
1464 }
1465 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1466 if (ret<0) {
1467 perror("usb_clear_halt() on OUT endpoint");
1468 }
1469 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1470 if (ret<0) {
1471 perror("usb_clear_halt() on INTERRUPT endpoint");
1472 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001473}
1474
Linus Walleijd6a49972006-05-02 08:24:54 +00001475static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001476{
Linus Walleij58b62792007-01-07 12:38:59 +00001477 // Clear any stalled endpoints
Linus Walleijd6a49972006-05-02 08:24:54 +00001478 clear_stall(ptp_usb);
Linus Walleij58b62792007-01-07 12:38:59 +00001479 // Clear halts on any endpoints
1480 clear_halt(ptp_usb);
Linus Walleijd6a49972006-05-02 08:24:54 +00001481 // Added to clear some stuff on the OUT endpoint
1482 // TODO: is this good on the Mac too?
1483 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1484 usb_release_interface(ptp_usb->handle, interfaceNumber);
1485 // Brutally reset device
1486 // TODO: is this good on the Mac too?
1487 usb_reset(ptp_usb->handle);
1488 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001489}
1490
Linus Walleij45a86372007-03-07 09:36:19 +00001491static LIBMTP_error_number_t prime_device_memory(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001492{
Linus Walleij45a86372007-03-07 09:36:19 +00001493 mtpdevice_list_t *tmplist = devlist;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001494
Linus Walleij45a86372007-03-07 09:36:19 +00001495 while (tmplist != NULL) {
tedbullock0f033cb2007-02-14 20:56:54 +00001496 /* Allocate a parameter box */
Linus Walleij45a86372007-03-07 09:36:19 +00001497 tmplist->params = (PTPParams *) malloc(sizeof(PTPParams));
1498 tmplist->ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleijbfcb7922007-03-06 20:14:04 +00001499
tedbullock0f033cb2007-02-14 20:56:54 +00001500 /* Check for allocation Error */
Linus Walleij45a86372007-03-07 09:36:19 +00001501 if(tmplist->params == NULL || tmplist->ptp_usb == NULL) {
Linus Walleijbfcb7922007-03-06 20:14:04 +00001502 /* Error and deallocation of memory will be handled by caller. */
tedbullock0f033cb2007-02-14 20:56:54 +00001503 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1504 }
1505
tedbullock69a445b2007-02-15 07:41:04 +00001506 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleij45a86372007-03-07 09:36:19 +00001507 memset(tmplist->params, 0, sizeof(PTPParams));
1508 memset(tmplist->ptp_usb, 0, sizeof(PTP_USB));
1509 tmplist = tmplist->next;
tedbullock0f033cb2007-02-14 20:56:54 +00001510 }
tedbullock0f033cb2007-02-14 20:56:54 +00001511 return LIBMTP_ERROR_NONE;
1512}
1513
Linus Walleij45a86372007-03-07 09:36:19 +00001514static void assign_known_device_flags(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001515{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001516 int i;
Linus Walleij27c50c52007-03-06 20:34:48 +00001517 mtpdevice_list_t *tmplist = devlist;
1518 uint8_t current_device = 0;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001519
tedbullock0f033cb2007-02-14 20:56:54 +00001520 /* Search through known device list and set correct device flags */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001521 while (tmplist != NULL) {
1522 int device_known = 0;
1523
1524 for(i = 0; i < mtp_device_table_size; i++) {
1525 if(tmplist->libusb_device->descriptor.idVendor == mtp_device_table[i].vendor_id &&
1526 tmplist->libusb_device->descriptor.idProduct == mtp_device_table[i].product_id) {
1527 /* This device is known, assign the correct device flags */
1528 /* Note that ptp_usb[current_device] could potentially be NULL */
Linus Walleij45a86372007-03-07 09:36:19 +00001529 if(tmplist->ptp_usb != NULL) {
1530 tmplist->ptp_usb->device_flags = mtp_device_table[i].device_flags;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001531
1532 /*
1533 * TODO:
1534 * Preferable to not do this with #ifdef ENABLE_USB_BULK_DEBUG but there is
1535 * currently no other compile time debug option
1536 */
1537
1538 device_known = 1;
tedbullock65a05772007-03-03 22:56:56 +00001539#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001540 /* This device is known to the developers */
1541 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is a %s.\n",
1542 current_device + 1,
1543 tmplist->libusb_device->descriptor.idVendor,
1544 tmplist->libusb_device->descriptor.idProduct,
1545 mtp_device_table[i].name);
tedbullock65a05772007-03-03 22:56:56 +00001546#endif
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001547 }
1548 break;
1549 }
tedbullock0f033cb2007-02-14 20:56:54 +00001550 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001551 if (!device_known) {
1552 /* This device is unknown to the developers */
1553 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
1554 current_device + 1,
1555 tmplist->libusb_device->descriptor.idVendor,
1556 tmplist->libusb_device->descriptor.idProduct);
1557 fprintf(stderr, "Please report this VID/PID and the device model to the "
1558 "libmtp development team\n");
1559 }
1560 tmplist = tmplist->next;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001561 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001562 }
tedbullock0f033cb2007-02-14 20:56:54 +00001563}
1564
tedbullock0f033cb2007-02-14 20:56:54 +00001565
Linus Walleij45a86372007-03-07 09:36:19 +00001566static LIBMTP_error_number_t configure_usb_devices(mtpdevice_list_t *devicelist)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001567{
1568 mtpdevice_list_t *tmplist = devicelist;
1569 uint16_t ret = 0;
1570 uint8_t current_device = 0;
1571
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001572 while (tmplist != NULL) {
Linus Walleij45a86372007-03-07 09:36:19 +00001573 /* This is erroneous, there must be a PTP_USB instance that we can initialize. */
1574 if(tmplist->ptp_usb == NULL) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001575 return LIBMTP_ERROR_MEMORY_ALLOCATION;
tedbullock0f033cb2007-02-14 20:56:54 +00001576 }
1577
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001578 /* TODO: Will this always be little endian? */
Linus Walleij45a86372007-03-07 09:36:19 +00001579 tmplist->params->byteorder = PTP_DL_LE;
1580 tmplist->params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8");
1581 tmplist->params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE");
tedbullock0f033cb2007-02-14 20:56:54 +00001582
Linus Walleij45a86372007-03-07 09:36:19 +00001583 if(tmplist->params->cd_locale_to_ucs2 == (iconv_t) -1 ||
1584 tmplist->params->cd_ucs2_to_locale == (iconv_t) -1) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001585 fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
1586 "Too old stdlibc, glibc and libiconv?\n");
Linus Walleij35c0ef12007-03-06 20:49:09 +00001587 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001588 }
1589
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001590 // ep = device->config->interface->altsetting->endpoint;
1591 // no_of_ep = device->config->interface->altsetting->bNumEndpoints;
tedbullock0f033cb2007-02-14 20:56:54 +00001592
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001593 /* Assign endpoints to usbinfo... */
1594 find_endpoints(tmplist->libusb_device,
Linus Walleij45a86372007-03-07 09:36:19 +00001595 &tmplist->ptp_usb->inep,
1596 &tmplist->ptp_usb->inep_maxpacket,
1597 &tmplist->ptp_usb->outep,
1598 &tmplist->ptp_usb->outep_maxpacket,
1599 &tmplist->ptp_usb->intep);
tedbullock0f033cb2007-02-14 20:56:54 +00001600
Linus Walleij45a86372007-03-07 09:36:19 +00001601 /* Attempt to initialize this device */
1602 if (init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) < 0) {
1603 fprintf(stderr, "LIBMTP PANIC: Unable to initialize device %d\n", current_device+1);
1604 // FIXME: perhaps use "continue" to keep trying the other devices.
Linus Walleij35c0ef12007-03-06 20:49:09 +00001605 return LIBMTP_ERROR_CONNECTING;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001606 }
1607
1608 /*
1609 * This works in situations where previous bad applications
1610 * have not used LIBMTP_Release_Device on exit
1611 */
Linus Walleij45a86372007-03-07 09:36:19 +00001612 if ((ret = ptp_opensession(tmplist->params, 1)) == PTP_ERROR_IO) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001613 fprintf(stderr, "PTP_ERROR_IO: Trying again after resetting USB\n");
Linus Walleij45a86372007-03-07 09:36:19 +00001614 close_usb(tmplist->ptp_usb, tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001615
Linus Walleij45a86372007-03-07 09:36:19 +00001616 if(init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) <0) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001617 fprintf(stderr, "LIBMTP PANIC: Could not open session on device %d\n", current_device+1);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001618 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001619 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001620
1621 /* Device has been reset, try again */
Linus Walleij45a86372007-03-07 09:36:19 +00001622 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001623 }
1624
1625 /* Was the transaction id invalid? Try again */
1626 if (ret == PTP_RC_InvalidTransactionID) {
Linus Walleij45a86372007-03-07 09:36:19 +00001627 fprintf(stderr, "LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
1628 tmplist->params->transaction_id += 10;
1629 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001630 }
1631
1632 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
Linus Walleij45a86372007-03-07 09:36:19 +00001633 fprintf(stderr, "LIBMTP PANIC: Could not open session! "
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001634 "(Return code %d)\n Try to reset the device.\n",
1635 ret);
Linus Walleij45a86372007-03-07 09:36:19 +00001636 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001637 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001638 return LIBMTP_ERROR_CONNECTING;
1639 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001640
1641 /* It is permissible to call this before opening the session */
Linus Walleij45a86372007-03-07 09:36:19 +00001642 if (ptp_getdeviceinfo(tmplist->params,
1643 &tmplist->params->deviceinfo) != PTP_RC_OK) {
1644 fprintf(stderr, "LIBMTP PANIC: Could not get device info!\n");
1645 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001646 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001647 return LIBMTP_ERROR_CONNECTING;
1648 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001649
1650 tmplist = tmplist->next;
1651 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001652 }
1653
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001654 /* Exit with the nice list */
1655 return LIBMTP_ERROR_NONE;
tedbullock0f033cb2007-02-14 20:56:54 +00001656}
1657
Linus Walleij550d6d52007-01-24 14:32:51 +00001658/**
1659 * This function scans through the results of the get_mtp_usb_device_list
1660 * function and attempts to connect to those devices listed using the
1661 * mtp_device_table at the top of the file. Returns a LIBMTP_error_number_t.
1662 *
Linus Walleij45a86372007-03-07 09:36:19 +00001663 * @param devlist a list of devices with primed PTP_USB and params structs.
Linus Walleij550d6d52007-01-24 14:32:51 +00001664 * @return Error Codes as per the type definition
1665 */
Linus Walleij45a86372007-03-07 09:36:19 +00001666LIBMTP_error_number_t find_usb_devices(mtpdevice_list_t **devlist)
Linus Walleij550d6d52007-01-24 14:32:51 +00001667{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001668 mtpdevice_list_t *mtp_device_list = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001669 LIBMTP_error_number_t ret;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001670
Linus Walleija4942fc2007-03-12 19:23:21 +00001671 /*
1672 * Recover list of attached USB devices that match MTP criteria, i.e.
1673 * it either has an MTP device descriptor or it is in the known
1674 * devices list.
1675 */
Linus Walleij45a86372007-03-07 09:36:19 +00001676 ret = get_mtp_usb_device_list (&mtp_device_list);
Linus Walleijac061862007-03-07 08:28:33 +00001677 if (ret != LIBMTP_ERROR_NONE) {
1678 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001679 }
1680
Linus Walleij45a86372007-03-07 09:36:19 +00001681 // Then prime them
1682 ret = prime_device_memory(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001683 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001684 fprintf(stderr, "LIBMTP PANIC: prime_device_memory() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001685 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001686 }
tedbullock0f033cb2007-02-14 20:56:54 +00001687
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001688 /* Assign specific device flags and detect unknown devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001689 assign_known_device_flags(mtp_device_list);
Linus Walleij550d6d52007-01-24 14:32:51 +00001690
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001691 /* Configure the devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001692 ret = configure_usb_devices(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001693 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001694 fprintf(stderr, "LIBMTP PANIC: configure_usb_devices() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001695 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001696 }
Linus Walleij550d6d52007-01-24 14:32:51 +00001697
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001698 /* Configure interface numbers */
Linus Walleij550d6d52007-01-24 14:32:51 +00001699 {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001700 mtpdevice_list_t *tmplist = mtp_device_list;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001701
Linus Walleij45a86372007-03-07 09:36:19 +00001702 while (tmplist != NULL) {
1703 tmplist->interface_number = tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001704 tmplist = tmplist->next;
Linus Walleij550d6d52007-01-24 14:32:51 +00001705 }
1706 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001707
Linus Walleij45a86372007-03-07 09:36:19 +00001708 /* we're connected to all devices, return the list and OK */
1709 *devlist = mtp_device_list;
1710 return LIBMTP_ERROR_NONE;
1711
1712 find_usb_devices_error_exit:
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001713 if(mtp_device_list != NULL) {
1714 free_mtpdevice_list(mtp_device_list);
1715 mtp_device_list = NULL;
Linus Walleij550d6d52007-01-24 14:32:51 +00001716 }
Linus Walleij45a86372007-03-07 09:36:19 +00001717 *devlist = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001718 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001719}
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001720
Linus Walleijd6a49972006-05-02 08:24:54 +00001721static 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 +00001722{
Linus Walleijd6a49972006-05-02 08:24:54 +00001723 int i,n;
1724 struct usb_endpoint_descriptor *ep;
1725
1726 ep = dev->config->interface->altsetting->endpoint;
1727 n=dev->config->interface->altsetting->bNumEndpoints;
1728
1729 for (i=0;i<n;i++) {
1730 if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1731 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1732 USB_ENDPOINT_DIR_MASK)
1733 {
1734 *inep=ep[i].bEndpointAddress;
1735 *inep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001736 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001737 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1738 {
1739 *outep=ep[i].bEndpointAddress;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001740 *outep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijd6a49972006-05-02 08:24:54 +00001741 }
1742 } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1743 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1744 USB_ENDPOINT_DIR_MASK)
1745 {
1746 *intep=ep[i].bEndpointAddress;
1747 }
1748 }
1749 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001750}
1751
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001752void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber)
1753{
Linus Walleijd6a49972006-05-02 08:24:54 +00001754 if (ptp_closesession(params)!=PTP_RC_OK)
1755 fprintf(stderr,"ERROR: Could not close session!\n");
1756 close_usb(ptp_usb, interfaceNumber);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001757}
1758
Linus Walleijd6a49972006-05-02 08:24:54 +00001759static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001760{
Linus Walleijd6a49972006-05-02 08:24:54 +00001761
1762 return (usb_control_msg(ptp_usb->handle,
1763 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
1764 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001765}
1766
Linus Walleijd6a49972006-05-02 08:24:54 +00001767static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001768{
Linus Walleijd6a49972006-05-02 08:24:54 +00001769 return (usb_control_msg(ptp_usb->handle,
1770 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
1771 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001772}