blob: 0227e43fbfa8bcb3262a27b3ce54330239bb6fd1 [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
Linus Walleij0558ac52006-09-07 06:55:03 +0000105 { "Samsung YH-820", 0x04e8, 0x502e, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000106 // Contributed by polux2001@users.sourceforge.net
Linus Walleija29ed2a2007-02-25 22:01:37 +0000107 { "Samsung YH-925(-GS)", 0x04e8, 0x502f, DEVICE_FLAG_NONE },
Richard Low7059ed42007-01-21 09:33:34 +0000108 // Contributed by aronvanammers on SourceForge
109 { "Samsung YH-925GS", 0x04e8, 0x5024, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000110 // Contributed by anonymous person on SourceForge
Linus Walleij0558ac52006-09-07 06:55:03 +0000111 { "Samsung YP-T7J", 0x04e8, 0x5047, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000112 // Reported by cstrickler@gmail.com
Linus Walleij0558ac52006-09-07 06:55:03 +0000113 { "Samsung YP-U2J (YP-U2JXB/XAA)", 0x04e8, 0x5054, DEVICE_FLAG_NONE },
Linus Walleij82ed6612006-08-31 08:06:19 +0000114 // Reported by Andrew Benson
Linus Walleij0558ac52006-09-07 06:55:03 +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>
Linus Walleij12413c02007-02-22 18:33:07 +0000119 { "Samsung YP-T9", 0x04e8, 0x507f, DEVICE_FLAG_NONE },
120 // From Paul Clinch
121 { "Samsung YP-K3", 0x04e8, 0x5081, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000122 // From a rouge .INF file
Linus Walleij0558ac52006-09-07 06:55:03 +0000123 { "Samsung YH-999 Portable Media Center", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE },
Linus Walleij8556f522007-02-14 09:47:50 +0000124 // From Lionel Bouton
125 { "Samsung X830 Mobile Phone", 0x04e8, 0x6702, DEVICE_FLAG_NONE },
Richard Lowbf2675c2007-03-18 18:20:26 +0000126 // From XNJB user
127 { "Samsung YP-Z5", 0x04e8, 0x503c, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000128
129 /*
130 * Intel
131 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000132 { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000133
134 /*
135 * JVC
136 */
137 // From Mark Veinot
Linus Walleij0558ac52006-09-07 06:55:03 +0000138 { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000139
140 /*
Linus Walleij093ca0a2006-11-02 21:15:00 +0000141 * Philips
Linus Walleij3d2c7842006-08-18 09:35:08 +0000142 */
143 // From libgphoto2 source
Linus Walleij093ca0a2006-11-02 21:15:00 +0000144 { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE },
Richard Low7059ed42007-01-21 09:33:34 +0000145 { "Philips HDD6320/00 & HDD6330/17", 0x0471, 0x014b, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000146 // Anonymous SourceForge user
Linus Walleij093ca0a2006-11-02 21:15:00 +0000147 { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE },
148 // From Gerhard Mekenkamp
149 { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE },
Richard Lowa6c924c2006-12-29 17:44:28 +0000150 // from XNJB forum
151 { "Philips GoGear SA9200", 0x0471, 0x014f, DEVICE_FLAG_NONE },
Richard Lowa98d0542007-01-20 12:31:07 +0000152 // from XNJB user
153 { "Philips PSA235", 0x0471, 0x7e01, DEVICE_FLAG_NONE },
Richard Lowbf2675c2007-03-18 18:20:26 +0000154 // from discussion forum
155 { "Philips GoGear HDD085/00", 0x0471, 0x014d, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000156
157 /*
158 * SanDisk
Linus Walleijf41abe52007-03-20 19:25:18 +0000159 * several devices (c150 for sure) are definately dual-mode and must
160 * have the USB mass storage driver that hooks them unloaded first.
Linus Walleij3d2c7842006-08-18 09:35:08 +0000161 */
162 // Reported by Brian Robison
Linus Walleijf41abe52007-03-20 19:25:18 +0000163 { "SanDisk Sansa m230/m240", 0x0781, 0x7400, DEVICE_FLAG_UNLOAD_DRIVER },
164 // Reported by tangent_@users.sourceforge.net
Linus Walleijb6583182007-03-07 08:14:18 +0000165 { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000166 // From libgphoto2 source
Linus Walleijf41abe52007-03-20 19:25:18 +0000167 { "SanDisk Sansa e200", 0x0781, 0x7420, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000168 // Reported by gonkflea@users.sourceforge.net
Linus Walleijf41abe52007-03-20 19:25:18 +0000169 { "SanDisk Sansa e260", 0x0781, 0x7420, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij1df47cd2007-01-24 10:19:52 +0000170 // Reported by anonymous user at sourceforge.net
Linus Walleijf41abe52007-03-20 19:25:18 +0000171 { "SanDisk Sansa c250", 0x0781, 0x7450, 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 Walleij3d2c7842006-08-18 09:35:08 +0000174
175 /*
176 * iRiver
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000177 * we assume that PTP_OC_MTP_GetObjPropList is essentially
Linus Walleijfabffc02006-12-17 00:18:57 +0000178 * broken on all iRiver devices, meaning it simply won't return
179 * all properties for a file when asking for metadata 0xffffffff.
180 * Please test on your device if you believe it isn't broken!
181 * Some devices from http://www.mtp-ums.net/viewdeviceinfo.php
Linus Walleij3d2c7842006-08-18 09:35:08 +0000182 */
Linus Walleij2be40c72007-03-16 15:19:44 +0000183 { "iRiver Portable Media Center", 0x1006, 0x4002,
184 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
185 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
186 { "iRiver Portable Media Center", 0x1006, 0x4003,
187 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
188 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000189 // From libgphoto2 source
Linus Walleij2be40c72007-03-16 15:19:44 +0000190 { "iRiver T10", 0x4102, 0x1113,
191 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
192 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
193 { "iRiver T20 FM", 0x4102, 0x1114,
194 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
195 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleijfabffc02006-12-17 00:18:57 +0000196 // This appears at the MTP-UMS site
Linus Walleij2be40c72007-03-16 15:19:44 +0000197 { "iRiver T20", 0x4102, 0x1115,
198 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
199 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
200 { "iRiver U10", 0x4102, 0x1116,
201 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
202 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
203 { "iRiver T10", 0x4102, 0x1117,
204 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
205 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
206 { "iRiver T20", 0x4102, 0x1118,
207 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
208 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
209 { "iRiver T30", 0x4102, 0x1119,
210 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
211 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij77e047c2006-09-11 19:09:06 +0000212 // Reported by David Wolpoff
Linus Walleij2be40c72007-03-16 15:19:44 +0000213 { "iRiver T10 2GB", 0x4102, 0x1120,
214 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
215 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij87099812006-12-17 00:29:58 +0000216 // Rough guess this is the MTP device ID...
Linus Walleij2be40c72007-03-16 15:19:44 +0000217 { "iRiver N12", 0x4102, 0x1122,
218 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
219 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij04377da2006-08-30 13:03:35 +0000220 // Reported by Adam Torgerson
Linus Walleij2be40c72007-03-16 15:19:44 +0000221 { "iRiver Clix", 0x4102, 0x112a,
222 DEVICE_FLAG_NO_ZERO_READS | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000223 // Reported by Scott Call
Linus Walleij2be40c72007-03-16 15:19:44 +0000224 { "iRiver H10 20GB", 0x4102, 0x2101,
225 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
226 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
227 { "iRiver H10", 0x4102, 0x2102,
228 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
229 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000230
231 /*
232 * Dell
233 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000234 { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000235
236 /*
237 * Toshiba
238 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000239 { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE },
240 { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE },
Linus Walleij1e3d1762007-02-26 08:27:35 +0000241 // Reported by Nicholas Tripp
242 { "Toshiba Gigabeat P20", 0x0930, 0x000f, DEVICE_FLAG_NONE },
Linus Walleij405e2392006-10-23 07:13:49 +0000243 // From libgphoto2
244 { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE },
Linus Walleij402e4bd2006-09-12 07:50:56 +0000245 // Reported by Rob Brown
246 { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE },
Linus Walleijb01d18b2006-12-13 12:49:15 +0000247
Linus Walleij6f680562006-08-19 22:49:33 +0000248 /*
249 * Archos
250 */
251 // Reported by gudul1@users.sourceforge.net
Linus Walleije5430902006-10-08 14:46:03 +0000252 { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE },
Linus Walleijbbdf44e2007-02-14 09:36:00 +0000253 // Added by Jan Binder
254 { "Archos XS202 (MTP mode)", 0x0e79, 0x1208, DEVICE_FLAG_NONE },
Linus Walleije5430902006-10-08 14:46:03 +0000255
256 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000257 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000258 * This unit is falsely detected as USB mass storage in Linux
Linus Walleijcc4dd482006-11-25 21:58:30 +0000259 * prior to kernel 2.6.19 (fixed by patch from Alan Stern)
260 * so on older kernels special care is needed to remove the
261 * USB mass storage driver that erroneously binds to the device
262 * interface.
Linus Walleije5430902006-10-08 14:46:03 +0000263 */
Richard Lowc7d48d22006-11-29 18:24:55 +0000264 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER},
265
266 /*
267 * Microsoft
268 */
269 // Reported by Farooq Zaman
270 { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE },
271
272 /*
273 * Sirius
274 */
Linus Walleijb01d18b2006-12-13 12:49:15 +0000275 { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE },
276
277 /*
278 * Canon
279 * This is actually a camera, but it has a Microsoft device descriptor
280 * and reports itself as supporting the MTP extension.
281 */
Linus Walleijc1b49402007-02-07 08:00:38 +0000282 {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE },
283
284 /*
285 * Nokia
286 */
Linus Walleijde7ad542007-02-14 09:32:26 +0000287 {"Nokia Mobile Phones (MTP mode)", 0x0421, 0x04e1, DEVICE_FLAG_NONE },
288
289 /*
290 * LOGIK
291 * Sold in the UK, seem to be manufactured by CCTech in China.
292 */
Linus Walleijd2ff9e32007-03-16 15:27:45 +0000293 {"Logik LOG DAX MP3 and DAB Player", 0x13d1, 0x7002, DEVICE_FLAG_NONE },
294
295 /*
296 * Other strange stuff.
297 */
298 {"Isabella's prototype", 0x0b20, 0xddee, DEVICE_FLAG_NONE }
Linus Walleija5483642006-03-09 09:20:38 +0000299};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000300static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000301
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000302int ptpcam_usb_timeout = USB_TIMEOUT;
303
Linus Walleijd6a49972006-05-02 08:24:54 +0000304// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000305static struct usb_bus* init_usb();
Linus Walleijd6a49972006-05-02 08:24:54 +0000306static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);
Linus Walleijd6a49972006-05-02 08:24:54 +0000307static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);
308static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000309static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000310static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000311static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000312static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
313static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000314
Linus Walleij552ba322007-01-22 11:49:59 +0000315/**
316 * Get a list of the supported USB devices.
317 *
318 * The developers depend on users of this library to constantly
319 * add in to the list of supported devices. What we need is the
320 * device name, USB Vendor ID (VID) and USB Product ID (PID).
321 * put this into a bug ticket at the project homepage, please.
322 * The VID/PID is used to let e.g. udev lift the device to
323 * console userspace access when it's plugged in.
324 *
325 * @param devices a pointer to a pointer that will hold a device
326 * list after the call to this function, if it was
327 * successful.
328 * @param numdevs a pointer to an integer that will hold the number
329 * of devices in the device list if the call was successful.
330 * @return 0 if the list was successfull retrieved, any other
331 * value means failure.
332 */
333int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000334{
335 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
336 *numdevs = mtp_device_table_size;
337 return 0;
338}
339
Linus Walleij552ba322007-01-22 11:49:59 +0000340
Linus Walleij1fd2d272006-05-08 09:22:01 +0000341static struct usb_bus* init_usb()
342{
343 usb_init();
344 usb_find_busses();
345 usb_find_devices();
346 return (usb_get_busses());
347}
348
349/**
tedbullock0f033cb2007-02-14 20:56:54 +0000350 * Small recursive function to append a new usb_device to the linked list of
351 * USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000352 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000353 * properties.
354 * @param next New USB MTP device to be added to list
355 * @return nothing
356 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000357static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
358 struct usb_device *newdevice)
tedbullock0f033cb2007-02-14 20:56:54 +0000359{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000360 mtpdevice_list_t *new_list_entry;
tedbullock0f033cb2007-02-14 20:56:54 +0000361
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000362 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
363 if (new_list_entry == NULL) {
364 return NULL;
365 }
366 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
367 new_list_entry->libusb_device = newdevice;
368 new_list_entry->next = NULL;
369
370 if (devlist == NULL) {
371 return new_list_entry;
372 } else {
373 mtpdevice_list_t *tmp = devlist;
374 while (tmp->next != NULL) {
375 tmp = tmp->next;
376 }
377 tmp->next = new_list_entry;
378 }
379 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000380}
381
382/**
383 * Small recursive function to free dynamic memory allocated to the linked list
384 * of USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000385 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000386 * properties.
387 * @return nothing
388 */
Linus Walleij45a86372007-03-07 09:36:19 +0000389void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000390{
Linus Walleijac061862007-03-07 08:28:33 +0000391 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000392
393 if (devlist == NULL)
394 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000395 while (tmplist != NULL) {
396 mtpdevice_list_t *tmp = tmplist;
397 tmplist = tmplist->next;
Linus Walleij45a86372007-03-07 09:36:19 +0000398 // Do not free() the fields (ptp_usb, parms)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000399 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000400 }
tedbullock0f033cb2007-02-14 20:56:54 +0000401 return;
402}
403
404/**
Linus Walleija4942fc2007-03-12 19:23:21 +0000405 * This checks if a device has an MTP descriptor.
406 * @param dev a device struct from libusb.
407 * @return 1 if the device is MTP compliant, 0 if not.
408 */
409static int device_has_descriptor(struct usb_device *dev)
410{
411 usb_dev_handle *devh;
412 unsigned char buf[1024], cmd;
413 int ret;
414
415 /* Don't examine hubs (no point in that) */
416 if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) {
417 return 0;
418 }
419
420 /* Attempt to open Device on this port */
421 devh = usb_open(dev);
422 if (devh == NULL) {
423 /* Could not open this device */
424 return 0;
425 }
426
427 /* Read the special descriptor */
428 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
429
430 /* Check if descriptor length is at least 10 bytes */
431 if (ret < 10) {
432 usb_close(devh);
433 return 0;
434 }
435
436 /* Check if this device has a Microsoft Descriptor */
437 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
438 (buf[6] == 'F') && (buf[8] == 'T'))) {
439 usb_close(devh);
440 return 0;
441 }
442
443 /* Check if device responds to control message 1 or if there is an error */
444 cmd = buf[16];
445 ret = usb_control_msg (devh,
446 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
447 cmd,
448 0,
449 4,
450 (char *) buf,
451 sizeof(buf),
452 1000);
453
454 /* If this is true, the device either isn't MTP or there was an error */
455 if (ret <= 0x15) {
456 /* TODO: If there was an error, flag it and let the user know somehow */
457 /* if(ret == -1) {} */
458 usb_close(devh);
459 return 0;
460 }
461
462 /* Check if device is MTP or if it is something like a USB Mass Storage
463 device with Janus DRM support */
464 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
465 usb_close(devh);
466 return 0;
467 }
468
469 /* After this point we are probably dealing with an MTP device */
470
471 /* Check if device responds to control message 2 or if there is an error*/
472 ret = usb_control_msg (devh,
473 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
474 cmd,
475 0,
476 5,
477 (char *) buf,
478 sizeof(buf),
479 1000);
480
481 /* If this is true, the device errored against control message 2 */
482 if (ret == -1) {
483 /* TODO: Implement callback function to let managing program know there
484 was a problem, along with description of the problem */
485 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
486 "ProductID:%04x encountered an error responding to "
487 "control message 2.\n"
488 "Problems may arrise but continuing\n",
489 dev->descriptor.idVendor, dev->descriptor.idProduct);
490 } else if (ret <= 0x15) {
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 responded to control message 2 with a "
495 "response that was too short. Problems may arrise but "
496 "continuing\n",
497 dev->descriptor.idVendor, dev->descriptor.idProduct);
498 } else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
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 encountered an error responding to "
503 "control message 2\n"
504 "Problems may arrise but continuing\n",
505 dev->descriptor.idVendor, dev->descriptor.idProduct);
506 }
507
508 /* Close the USB device handle */
509 usb_close(devh);
510 return 1;
511}
512
513/**
514 * This function scans through the connected usb devices on a machine and
515 * if they match known Vendor and Product identifiers appends them to the
516 * dynamic array mtp_device_list. Be sure to call
517 * <code>free(mtp_device_list)</code> when you are done with it, assuming it
518 * is not NULL.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000519 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000520 * properties (if this list is not empty, new entries will be appended
521 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000522 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
523 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no devices have
524 * been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000525 */
Linus Walleij45a86372007-03-07 09:36:19 +0000526static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000527{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000528 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000529
530 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000531 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000532 for (; dev != NULL; dev = dev->next) {
Linus Walleija4942fc2007-03-12 19:23:21 +0000533 if (device_has_descriptor(dev)) {
534 /* Append this usb device to the MTP USB Device List */
535 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
536 } else {
537 /* Check if it's in the known devices list then */
538 int i;
539
540 for(i = 0; i < mtp_device_table_size; i++) {
541 if(dev->descriptor.bDeviceClass != USB_CLASS_HUB &&
542 dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
543 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
544 /* Append this usb device to the MTP device list */
545 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
546 break;
547 }
548 }
Linus Walleij35c0ef12007-03-06 20:49:09 +0000549 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000550 }
551 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000552
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000553 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000554 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000555 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000556 }
557 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000558}
559
560/**
Linus Walleij1fd2d272006-05-08 09:22:01 +0000561 * Detect the MTP device descriptor and return the VID and PID
562 * of the first device found. This is a very low-level function
563 * which is intended for use with <b>udev</b> or other hotplug
564 * mechanisms. The idea is that a script may want to know if the
565 * just plugged-in device was an MTP device or not.
tedbullock2b45f2e2007-02-23 20:16:37 +0000566 *
Linus Walleij1fd2d272006-05-08 09:22:01 +0000567 * @param vid the Vendor ID (VID) of the first device found.
568 * @param pid the Product ID (PID) of the first device found.
569 * @return the number of detected devices or -1 if the call
570 * was unsuccessful.
571 */
572int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
573{
Linus Walleijb6b69132007-03-07 09:45:05 +0000574 mtpdevice_list_t *devlist;
575 LIBMTP_error_number_t ret;
576
577 ret = get_mtp_usb_device_list(&devlist);
578 if (ret != LIBMTP_ERROR_NONE) {
579 *vid = *pid = 0;
580 return -1;
581 }
582 *vid = devlist->libusb_device->descriptor.idVendor;
583 *pid = devlist->libusb_device->descriptor.idProduct;
584 free_mtpdevice_list(devlist);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000585 return 1;
586}
587
Linus Walleijc6210fb2006-05-08 11:11:41 +0000588/**
589 * This routine just dumps out low-level
590 * USB information about the current device.
591 * @param ptp_usb the USB device to get information from.
592 */
593void dump_usbinfo(PTP_USB *ptp_usb)
594{
595 int res;
596 struct usb_device *dev;
597
598#ifdef LIBUSB_HAS_GET_DRIVER_NP
599 char devname[0x10];
600
601 devname[0] = '\0';
602 res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));
603 if (devname[0] != '\0') {
604 printf(" Using kernel interface \"%s\"\n", devname);
605 }
606#endif
607 dev = usb_device(ptp_usb->handle);
608 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
609 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
610 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
611 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000612 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
613 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000614 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
615 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000616 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000617 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
618}
619
Linus Walleija0323272007-01-07 12:21:30 +0000620static void
621ptp_debug (PTPParams *params, const char *format, ...)
622{
623 va_list args;
624
625 va_start (args, format);
626 if (params->debug_func!=NULL)
627 params->debug_func (params->data, format, args);
628 else
629 {
630 vfprintf (stderr, format, args);
631 fprintf (stderr,"\n");
632 fflush (stderr);
633 }
634 va_end (args);
635}
636
637static void
638ptp_error (PTPParams *params, const char *format, ...)
639{
640 va_list args;
641
642 va_start (args, format);
643 if (params->error_func!=NULL)
644 params->error_func (params->data, format, args);
645 else
646 {
647 vfprintf (stderr, format, args);
648 fprintf (stderr,"\n");
649 fflush (stderr);
650 }
651 va_end (args);
652}
653
Linus Walleijc6210fb2006-05-08 11:11:41 +0000654
Linus Walleij99ed83c2007-01-02 18:46:02 +0000655/*
656 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000657 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000658 * Much reading packet logs and having fun with trials and errors
659 * reveals that WMP / Windows is probably using an algorithm like this
660 * for large transfers:
661 *
662 * 1. Send the command (0x0c bytes) if headers are split, else, send
663 * command plus sizeof(endpoint) - 0x0c bytes.
664 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
665 * split headers. Else goto 3.
666 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
667 * We call 0x10000 CONTEXT_BLOCK_SIZE.
668 * 4. Send remaining bytes MOD sizeof(endpoint)
669 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
670 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000671 *
672 * Further there is some special quirks to handle zero reads from the
673 * device, since some devices can't do them at all due to shortcomings
674 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000675 */
Richard Low4fd59d62007-03-03 16:34:37 +0000676#define CONTEXT_BLOCK_SIZE_1 0x3e00
677#define CONTEXT_BLOCK_SIZE_2 0x200
678#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000679static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000680ptp_read_func (
681 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000682 unsigned long *readbytes,
683 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000684) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000685 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000686 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000687 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000688 unsigned long curread = 0;
689 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000690 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000691 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000692
693 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000694 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000695 while (curread < size) {
Richard Low4fd59d62007-03-03 16:34:37 +0000696
697#ifdef ENABLE_USB_BULK_DEBUG
698 printf("Remaining size to read: 0x%04x bytes\n", size - curread);
699#endif
700 // check equal to condition here
701 if (size - curread < CONTEXT_BLOCK_SIZE)
702 {
703 // this is the last packet
704 toread = size - curread;
705 // this is equivalent to zero read for these devices
Linus Walleij049f50c2007-03-03 20:30:43 +0000706 if (readzero && ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000707 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000708 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000709 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000710 }
Richard Low4fd59d62007-03-03 16:34:37 +0000711 else if (curread == 0)
712 // we are first packet, but not last packet
713 toread = CONTEXT_BLOCK_SIZE_1;
714 else if (toread == CONTEXT_BLOCK_SIZE_1)
715 toread = CONTEXT_BLOCK_SIZE_2;
716 else if (toread == CONTEXT_BLOCK_SIZE_2)
717 toread = CONTEXT_BLOCK_SIZE_1;
718 else
Linus Walleij4cec9872007-03-03 21:00:53 +0000719 printf("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
720 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000721
Richard Lowd284f072007-02-17 08:52:41 +0000722#ifdef ENABLE_USB_BULK_DEBUG
723 printf("Reading in 0x%04x bytes\n", toread);
724#endif
Linus Walleij784ac3f2006-12-29 10:36:51 +0000725 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout);
Richard Lowd284f072007-02-17 08:52:41 +0000726#ifdef ENABLE_USB_BULK_DEBUG
727 printf("Result of read: 0x%04x\n", result);
728#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000729
Linus Walleijc49c6372007-01-09 00:18:51 +0000730 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000731 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000732 }
Linus Walleije78de6d2006-10-31 14:46:36 +0000733#ifdef ENABLE_USB_BULK_DEBUG
734 printf("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000735 if (result == 0)
736 printf("Zero Read\n");
737 else
738 data_dump_ascii (stdout,bytes,result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000739#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000740
741 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000742 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000743 {
744#ifdef ENABLE_USB_BULK_DEBUG
745 printf("<==USB IN\nDiscarding extra byte\n");
746#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000747 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000748 }
Richard Low4fd59d62007-03-03 16:34:37 +0000749
Linus Walleijc49c6372007-01-09 00:18:51 +0000750 handler->putfunc(NULL, handler->private, result, bytes, &written);
Richard Lowbf2675c2007-03-18 18:20:26 +0000751
Linus Walleijc49c6372007-01-09 00:18:51 +0000752 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000753 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000754
755 // Increase counters, call callback
756 if (ptp_usb->callback_active) {
757 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
758 // send last update and disable callback.
759 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
760 ptp_usb->callback_active = 0;
761 }
762 if (ptp_usb->current_transfer_callback != NULL) {
763 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
764 ptp_usb->current_transfer_total,
765 ptp_usb->current_transfer_callback_data);
766 }
767 }
768
Linus Walleijd6a49972006-05-02 08:24:54 +0000769 if (result < toread) /* short reads are common */
770 break;
771 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000772 if (readbytes) *readbytes = curread;
773 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000774
Richard Low4df32f82007-01-11 20:04:39 +0000775 // there might be a zero packet waiting for us...
Linus Walleij049f50c2007-03-03 20:30:43 +0000776 if (readzero &&
777 !(ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS) &&
778 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000779 char temp;
780 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000781
Richard Low4df32f82007-01-11 20:04:39 +0000782#ifdef ENABLE_USB_BULK_DEBUG
783 printf("<==USB IN\n");
784 printf("Zero Read\n");
785#endif
786 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptpcam_usb_timeout);
787 if (zeroresult != 0)
788 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
789 }
790
Linus Walleijd6a49972006-05-02 08:24:54 +0000791 if (result > 0) {
Linus Walleijd6a49972006-05-02 08:24:54 +0000792 return (PTP_RC_OK);
793 } else {
794 return PTP_ERROR_IO;
795 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000796}
797
Linus Walleijb02a0662006-04-25 08:05:09 +0000798static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000799ptp_write_func (
800 unsigned long size,
801 PTPDataHandler *handler,
802 void *data,
803 unsigned long *written
804) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000805 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000806 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000807 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000808 unsigned long curwrite = 0;
809 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000810
811 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000812 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000813 if (!bytes) {
814 return PTP_ERROR_IO;
815 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000816 while (curwrite < size) {
817 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000818 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000819 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000820 } else {
821 // This magic makes packets the same size that WMP send them.
822 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000823 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000824 }
825 }
826 handler->getfunc(NULL, handler->private,towrite,bytes,&towrite);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000827 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000828#ifdef ENABLE_USB_BULK_DEBUG
829 printf("USB OUT==>\n");
Richard Lowedd61832006-12-30 12:38:18 +0000830 data_dump_ascii (stdout,bytes,towrite,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000831#endif
Linus Walleijc49c6372007-01-09 00:18:51 +0000832 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000833 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000834 }
835 // Increase counters
836 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000837 curwrite += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000838
839 // call callback
840 if (ptp_usb->callback_active) {
841 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
842 // send last update and disable callback.
843 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
844 ptp_usb->callback_active = 0;
845 }
846 if (ptp_usb->current_transfer_callback != NULL) {
847 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
848 ptp_usb->current_transfer_total,
849 ptp_usb->current_transfer_callback_data);
850 }
851 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000852 if (result < towrite) /* short writes happen */
853 break;
854 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000855 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000856 if (written) {
857 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000858 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000859
Linus Walleijc49c6372007-01-09 00:18:51 +0000860
Richard Lowef05b892007-01-03 21:18:56 +0000861 // If this is the last transfer send a zero write if required
862 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000863 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
Richard Low021421e2007-01-01 18:08:57 +0000864#ifdef ENABLE_USB_BULK_DEBUG
865 printf("USB OUT==>\n");
866 printf("Zero Write\n");
867#endif
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000868 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000869 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000870 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000871
Linus Walleijd6a49972006-05-02 08:24:54 +0000872 if (result < 0)
873 return PTP_ERROR_IO;
874 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000875}
876
Linus Walleija0323272007-01-07 12:21:30 +0000877/* memory data get/put handler */
878typedef struct {
879 unsigned char *data;
880 unsigned long size, curoff;
881} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000882
Linus Walleija0323272007-01-07 12:21:30 +0000883static uint16_t
884memory_getfunc(PTPParams* params, void* private,
885 unsigned long wantlen, unsigned char *data,
886 unsigned long *gotlen
887) {
888 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
889 unsigned long tocopy = wantlen;
890
891 if (priv->curoff + tocopy > priv->size)
892 tocopy = priv->size - priv->curoff;
893 memcpy (data, priv->data + priv->curoff, tocopy);
894 priv->curoff += tocopy;
895 *gotlen = tocopy;
896 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000897}
898
Linus Walleija0323272007-01-07 12:21:30 +0000899static uint16_t
900memory_putfunc(PTPParams* params, void* private,
901 unsigned long sendlen, unsigned char *data,
902 unsigned long *putlen
903) {
904 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
905
906 if (priv->curoff + sendlen > priv->size) {
907 priv->data = realloc (priv->data, priv->curoff+sendlen);
908 priv->size = priv->curoff + sendlen;
909 }
910 memcpy (priv->data + priv->curoff, data, sendlen);
911 priv->curoff += sendlen;
912 *putlen = sendlen;
913 return PTP_RC_OK;
914}
915
916/* init private struct for receiving data. */
917static uint16_t
918ptp_init_recv_memory_handler(PTPDataHandler *handler) {
919 PTPMemHandlerPrivate* priv;
920 priv = malloc (sizeof(PTPMemHandlerPrivate));
921 handler->private = priv;
922 handler->getfunc = memory_getfunc;
923 handler->putfunc = memory_putfunc;
924 priv->data = NULL;
925 priv->size = 0;
926 priv->curoff = 0;
927 return PTP_RC_OK;
928}
929
930/* init private struct and put data in for sending data.
931 * data is still owned by caller.
932 */
933static uint16_t
934ptp_init_send_memory_handler(PTPDataHandler *handler,
935 unsigned char *data, unsigned long len
936) {
937 PTPMemHandlerPrivate* priv;
938 priv = malloc (sizeof(PTPMemHandlerPrivate));
939 if (!priv)
940 return PTP_RC_GeneralError;
941 handler->private = priv;
942 handler->getfunc = memory_getfunc;
943 handler->putfunc = memory_putfunc;
944 priv->data = data;
945 priv->size = len;
946 priv->curoff = 0;
947 return PTP_RC_OK;
948}
949
950/* free private struct + data */
951static uint16_t
952ptp_exit_send_memory_handler (PTPDataHandler *handler) {
953 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
954 /* data is owned by caller */
955 free (priv);
956 return PTP_RC_OK;
957}
958
959/* hand over our internal data to caller */
960static uint16_t
961ptp_exit_recv_memory_handler (PTPDataHandler *handler,
962 unsigned char **data, unsigned long *size
963) {
964 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
965 *data = priv->data;
966 *size = priv->size;
967 free (priv);
968 return PTP_RC_OK;
969}
970
971/* send / receive functions */
972
973uint16_t
974ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
975{
976 uint16_t ret;
977 PTPUSBBulkContainer usbreq;
978 PTPDataHandler memhandler;
979 unsigned long written, towrite;
980
981 /* build appropriate USB container */
982 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
983 (sizeof(uint32_t)*(5-req->Nparam)));
984 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
985 usbreq.code=htod16(req->Code);
986 usbreq.trans_id=htod32(req->Transaction_ID);
987 usbreq.payload.params.param1=htod32(req->Param1);
988 usbreq.payload.params.param2=htod32(req->Param2);
989 usbreq.payload.params.param3=htod32(req->Param3);
990 usbreq.payload.params.param4=htod32(req->Param4);
991 usbreq.payload.params.param5=htod32(req->Param5);
992 /* send it to responder */
993 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
994 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
995 ret=ptp_write_func(
996 towrite,
997 &memhandler,
998 params->data,
999 &written
1000 );
1001 ptp_exit_send_memory_handler (&memhandler);
1002 if (ret!=PTP_RC_OK) {
1003 ret = PTP_ERROR_IO;
1004/* ptp_error (params,
1005 "PTP: request code 0x%04x sending req error 0x%04x",
1006 req->Code,ret); */
1007 }
1008 if (written != towrite) {
1009 ptp_error (params,
1010 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001011 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001012 );
1013 ret = PTP_ERROR_IO;
1014 }
1015 return ret;
1016}
1017
1018uint16_t
1019ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1020 unsigned long size, PTPDataHandler *handler
1021) {
1022 uint16_t ret;
1023 int wlen, datawlen;
1024 unsigned long written;
1025 PTPUSBBulkContainer usbdata;
1026 uint32_t bytes_left_to_transfer;
1027 PTPDataHandler memhandler;
1028
1029 /* build appropriate USB container */
1030 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1031 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1032 usbdata.code = htod16(ptp->Code);
1033 usbdata.trans_id= htod32(ptp->Transaction_ID);
1034
1035 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001036 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001037
1038 if (params->split_header_data) {
1039 datawlen = 0;
1040 wlen = PTP_USB_BULK_HDR_LEN;
1041 } else {
1042 unsigned long gotlen;
1043 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001044 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001045 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
1046 ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen);
1047 if (ret != PTP_RC_OK)
1048 return ret;
1049 if (gotlen != datawlen)
1050 return PTP_RC_GeneralError;
1051 }
1052 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1053 /* send first part of data */
1054 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1055 ptp_exit_send_memory_handler (&memhandler);
1056 if (ret!=PTP_RC_OK) {
1057 ret = PTP_ERROR_IO;
1058/* ptp_error (params,
1059 "PTP: request code 0x%04x sending data error 0x%04x",
1060 ptp->Code,ret);*/
1061 return ret;
1062 }
1063 if (size <= datawlen) return ret;
1064 /* if everything OK send the rest */
1065 bytes_left_to_transfer = size-datawlen;
1066 ret = PTP_RC_OK;
1067 while(bytes_left_to_transfer > 0) {
1068 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1069 if (ret != PTP_RC_OK)
1070 break;
1071 if (written == 0) {
1072 ret = PTP_ERROR_IO;
1073 break;
1074 }
1075 bytes_left_to_transfer -= written;
1076 }
1077 if (ret!=PTP_RC_OK)
1078 ret = PTP_ERROR_IO;
1079 return ret;
1080}
1081
1082static uint16_t ptp_usb_getpacket(PTPParams *params,
1083 PTPUSBBulkContainer *packet, unsigned long *rlen)
1084{
1085 PTPDataHandler memhandler;
1086 uint16_t ret;
1087 unsigned char *x = NULL;
1088
1089 /* read the header and potentially the first data */
1090 if (params->response_packet_size > 0) {
1091 /* If there is a buffered packet, just use it. */
1092 memcpy(packet, params->response_packet, params->response_packet_size);
1093 *rlen = params->response_packet_size;
1094 free(params->response_packet);
1095 params->response_packet = NULL;
1096 params->response_packet_size = 0;
1097 /* Here this signifies a "virtual read" */
1098 return PTP_RC_OK;
1099 }
1100 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001101 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001102 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1103 if (x) {
1104 memcpy (packet, x, *rlen);
1105 free (x);
1106 }
1107 return ret;
1108}
1109
1110uint16_t
1111ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1112{
1113 uint16_t ret;
1114 PTPUSBBulkContainer usbdata;
1115 unsigned char *data;
1116 unsigned long written;
1117
1118 memset(&usbdata,0,sizeof(usbdata));
1119 do {
1120 unsigned long len, rlen;
1121
1122 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1123 if (ret!=PTP_RC_OK) {
1124 ret = PTP_ERROR_IO;
1125 break;
1126 } else
1127 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1128 ret = PTP_ERROR_DATA_EXPECTED;
1129 break;
1130 } else
1131 if (dtoh16(usbdata.code)!=ptp->Code) {
1132 ret = dtoh16(usbdata.code);
1133 break;
1134 }
1135 if (usbdata.length == 0xffffffffU) {
1136 /* stuff data directly to passed data handler */
1137 while (1) {
1138 unsigned long readdata;
1139 int xret;
1140
1141 xret = ptp_read_func(
Richard Low02724f62007-02-17 09:47:26 +00001142 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
Linus Walleija0323272007-01-07 12:21:30 +00001143 handler,
1144 params->data,
Richard Low4df32f82007-01-11 20:04:39 +00001145 &readdata,
Linus Walleijc0a11432007-03-02 21:21:18 +00001146 0
Linus Walleija0323272007-01-07 12:21:30 +00001147 );
1148 if (xret == -1)
1149 return PTP_ERROR_IO;
Richard Low02724f62007-02-17 09:47:26 +00001150 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
Linus Walleija0323272007-01-07 12:21:30 +00001151 break;
1152 }
1153 return PTP_RC_OK;
1154 }
1155 if (rlen > dtoh32(usbdata.length)) {
1156 /*
1157 * Buffer the surplus response packet if it is >=
1158 * PTP_USB_BULK_HDR_LEN
1159 * (i.e. it is probably an entire package)
1160 * else discard it as erroneous surplus data.
1161 * This will even work if more than 2 packets appear
1162 * in the same transaction, they will just be handled
1163 * iteratively.
1164 *
1165 * Marcus observed stray bytes on iRiver devices;
1166 * these are still discarded.
1167 */
1168 unsigned int packlen = dtoh32(usbdata.length);
1169 unsigned int surplen = rlen - packlen;
1170
1171 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1172 params->response_packet = malloc(surplen);
1173 memcpy(params->response_packet,
1174 (uint8_t *) &usbdata + packlen, surplen);
1175 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001176 /* Ignore reading one extra byte if device flags have been set */
1177 } else if( !(((PTP_USB *)params->data)->device_flags &
1178 DEVICE_FLAG_NO_ZERO_READS &&
1179 rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001180 ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
1181 "too much, expect problems!",
1182 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001183 }
1184 rlen = packlen;
1185 }
1186
1187 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1188 * here. For MTP devices splitting header and data it might
1189 * be 12.
1190 */
1191 /* Evaluate full data length. */
1192 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1193
1194 /* autodetect split header/data MTP devices */
1195 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1196 params->split_header_data = 1;
1197
Richard Low02724f62007-02-17 09:47:26 +00001198 data = malloc(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ);
Linus Walleija0323272007-01-07 12:21:30 +00001199 /* Copy first part of data to 'data' */
1200 handler->putfunc(
1201 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1202 &written
1203 );
Richard Low4fd59d62007-03-03 16:34:37 +00001204
Linus Walleij049f50c2007-03-03 20:30:43 +00001205 if (((PTP_USB *)params->data)->device_flags & DEVICE_FLAG_NO_ZERO_READS &&
1206 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
Richard Low4fd59d62007-03-03 16:34:37 +00001207#ifdef ENABLE_USB_DEBUG
Linus Walleij049f50c2007-03-03 20:30:43 +00001208 printf("Reading in extra terminating byte\n");
Richard Low4fd59d62007-03-03 16:34:37 +00001209#endif
Linus Walleij049f50c2007-03-03 20:30:43 +00001210 // need to read in extra byte and discard it
1211 int result = 0;
1212 char byte = 0;
1213 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1214 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptpcam_usb_timeout);
1215
1216 if (result != 1)
1217 printf("Could not read in extra byte for PTP_USB_BULK_HS_MAX_PACKET_LEN_READ long file, return value 0x%04x\n", result);
1218 }
1219
Linus Walleija0323272007-01-07 12:21:30 +00001220 /* Is that all of data? */
1221 if (len+PTP_USB_BULK_HDR_LEN<=rlen) break;
Linus Walleij049f50c2007-03-03 20:30:43 +00001222
1223 ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1224 handler,
1225 params->data, &rlen, 1);
1226
Linus Walleija0323272007-01-07 12:21:30 +00001227 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001228 ret = PTP_ERROR_IO;
1229 break;
Linus Walleija0323272007-01-07 12:21:30 +00001230 }
1231 } while (0);
Linus Walleij049f50c2007-03-03 20:30:43 +00001232 /*
Linus Walleija0323272007-01-07 12:21:30 +00001233 if (ret!=PTP_RC_OK) {
1234 ptp_error (params,
1235 "PTP: request code 0x%04x getting data error 0x%04x",
1236 ptp->Code, ret);
1237 }*/
1238 return ret;
1239}
1240
1241uint16_t
1242ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1243{
1244 uint16_t ret;
1245 unsigned long rlen;
1246 PTPUSBBulkContainer usbresp;
1247
1248 memset(&usbresp,0,sizeof(usbresp));
1249 /* read response, it should never be longer than sizeof(usbresp) */
1250 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1251
1252 if (ret!=PTP_RC_OK) {
1253 ret = PTP_ERROR_IO;
1254 } else
1255 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1256 ret = PTP_ERROR_RESP_EXPECTED;
1257 } else
1258 if (dtoh16(usbresp.code)!=resp->Code) {
1259 ret = dtoh16(usbresp.code);
1260 }
1261 if (ret!=PTP_RC_OK) {
1262/* ptp_error (params,
1263 "PTP: request code 0x%04x getting resp error 0x%04x",
1264 resp->Code, ret);*/
1265 return ret;
1266 }
1267 /* build an appropriate PTPContainer */
1268 resp->Code=dtoh16(usbresp.code);
1269 resp->SessionID=params->session_id;
1270 resp->Transaction_ID=dtoh32(usbresp.trans_id);
1271 resp->Param1=dtoh32(usbresp.payload.params.param1);
1272 resp->Param2=dtoh32(usbresp.payload.params.param2);
1273 resp->Param3=dtoh32(usbresp.payload.params.param3);
1274 resp->Param4=dtoh32(usbresp.payload.params.param4);
1275 resp->Param5=dtoh32(usbresp.payload.params.param5);
1276 return ret;
1277}
1278
1279/* Event handling functions */
1280
1281/* PTP Events wait for or check mode */
1282#define PTP_EVENT_CHECK 0x0000 /* waits for */
1283#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1284
1285static inline uint16_t
1286ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1287{
1288 uint16_t ret;
1289 int result;
1290 unsigned long rlen;
1291 PTPUSBEventContainer usbevent;
1292 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1293
1294 memset(&usbevent,0,sizeof(usbevent));
1295
1296 if ((params==NULL) || (event==NULL))
1297 return PTP_ERROR_BADPARAM;
1298 ret = PTP_RC_OK;
1299 switch(wait) {
1300 case PTP_EVENT_CHECK:
1301 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1302 if (result==0)
1303 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1304 if (result < 0) ret = PTP_ERROR_IO;
1305 break;
1306 case PTP_EVENT_CHECK_FAST:
1307 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1308 if (result==0)
1309 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1310 if (result < 0) ret = PTP_ERROR_IO;
1311 break;
1312 default:
1313 ret=PTP_ERROR_BADPARAM;
1314 break;
1315 }
1316 if (ret!=PTP_RC_OK) {
1317 ptp_error (params,
1318 "PTP: reading event an error 0x%04x occurred", ret);
1319 return PTP_ERROR_IO;
1320 }
1321 rlen = result;
1322 if (rlen < 8) {
1323 ptp_error (params,
1324 "PTP: reading event an short read of %ld bytes occurred", rlen);
1325 return PTP_ERROR_IO;
1326 }
1327 /* if we read anything over interrupt endpoint it must be an event */
1328 /* build an appropriate PTPContainer */
1329 event->Code=dtoh16(usbevent.code);
1330 event->SessionID=params->session_id;
1331 event->Transaction_ID=dtoh32(usbevent.trans_id);
1332 event->Param1=dtoh32(usbevent.param1);
1333 event->Param2=dtoh32(usbevent.param2);
1334 event->Param3=dtoh32(usbevent.param3);
1335 return ret;
1336}
1337
1338uint16_t
1339ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1340
1341 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1342}
1343
1344uint16_t
1345ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1346
1347 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1348}
1349
1350
Linus Walleij9eb3d312006-08-04 19:25:59 +00001351static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001352{
Linus Walleijd6a49972006-05-02 08:24:54 +00001353 usb_dev_handle *device_handle;
1354
Linus Walleijd6a49972006-05-02 08:24:54 +00001355 params->error_func=NULL;
1356 params->debug_func=NULL;
1357 params->sendreq_func=ptp_usb_sendreq;
1358 params->senddata_func=ptp_usb_senddata;
1359 params->getresp_func=ptp_usb_getresp;
1360 params->getdata_func=ptp_usb_getdata;
1361 params->data=ptp_usb;
1362 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001363 /*
1364 * This is hardcoded here since we have no devices whatsoever that are BE.
1365 * Change this the day we run into our first BE device (if ever).
1366 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001367 params->byteorder = PTP_DL_LE;
1368
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001369 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001370 if (!device_handle) {
1371 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001372 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001373 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001374 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001375#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1376 /*
1377 * If this device is known to be wrongfully claimed by other kernel
1378 * drivers (such as mass storage), then try to unload it to make it
1379 * accessible from user space.
1380 */
1381 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
1382 if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij7beba572006-11-29 08:56:12 +00001383 // Totally ignore this error!
1384 // perror("usb_detach_kernel_driver_np()");
Linus Walleij501ba4d2006-10-16 06:17:04 +00001385 }
1386 }
1387#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001388#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001389 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001390 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1391 perror("usb_set_configuration()");
1392 return -1;
1393 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001394#endif
Linus Walleijef5cca92006-05-05 07:19:08 +00001395 if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001396 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001397 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001398 }
Linus Walleijc6210fb2006-05-08 11:11:41 +00001399 ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber;
Linus Walleijd6a49972006-05-02 08:24:54 +00001400 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001401 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001402}
1403
Linus Walleijd6a49972006-05-02 08:24:54 +00001404static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001405{
Linus Walleij58b62792007-01-07 12:38:59 +00001406 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001407 int ret;
1408
1409 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001410 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001411 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001412 if (ret<0) {
1413 perror ("inep: usb_get_endpoint_status()");
1414 } else if (status) {
1415 printf("Clearing stall on IN endpoint\n");
1416 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1417 if (ret<0) {
1418 perror ("usb_clear_stall_feature()");
1419 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001420 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001421
1422 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001423 status=0;
1424 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1425 if (ret<0) {
1426 perror("outep: usb_get_endpoint_status()");
1427 } else if (status) {
1428 printf("Clearing stall on OUT endpoint\n");
1429 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1430 if (ret<0) {
1431 perror("usb_clear_stall_feature()");
1432 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001433 }
Linus Walleij58b62792007-01-07 12:38:59 +00001434
1435 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1436}
1437
1438static void clear_halt(PTP_USB* ptp_usb)
1439{
1440 int ret;
1441
1442 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1443 if (ret<0) {
1444 perror("usb_clear_halt() on IN endpoint");
1445 }
1446 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1447 if (ret<0) {
1448 perror("usb_clear_halt() on OUT endpoint");
1449 }
1450 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1451 if (ret<0) {
1452 perror("usb_clear_halt() on INTERRUPT endpoint");
1453 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001454}
1455
Linus Walleijd6a49972006-05-02 08:24:54 +00001456static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001457{
Linus Walleij58b62792007-01-07 12:38:59 +00001458 // Clear any stalled endpoints
Linus Walleijd6a49972006-05-02 08:24:54 +00001459 clear_stall(ptp_usb);
Linus Walleij58b62792007-01-07 12:38:59 +00001460 // Clear halts on any endpoints
1461 clear_halt(ptp_usb);
Linus Walleijd6a49972006-05-02 08:24:54 +00001462 // Added to clear some stuff on the OUT endpoint
1463 // TODO: is this good on the Mac too?
1464 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1465 usb_release_interface(ptp_usb->handle, interfaceNumber);
1466 // Brutally reset device
1467 // TODO: is this good on the Mac too?
1468 usb_reset(ptp_usb->handle);
1469 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001470}
1471
Linus Walleij45a86372007-03-07 09:36:19 +00001472static LIBMTP_error_number_t prime_device_memory(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001473{
Linus Walleij45a86372007-03-07 09:36:19 +00001474 mtpdevice_list_t *tmplist = devlist;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001475
Linus Walleij45a86372007-03-07 09:36:19 +00001476 while (tmplist != NULL) {
tedbullock0f033cb2007-02-14 20:56:54 +00001477 /* Allocate a parameter box */
Linus Walleij45a86372007-03-07 09:36:19 +00001478 tmplist->params = (PTPParams *) malloc(sizeof(PTPParams));
1479 tmplist->ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleijbfcb7922007-03-06 20:14:04 +00001480
tedbullock0f033cb2007-02-14 20:56:54 +00001481 /* Check for allocation Error */
Linus Walleij45a86372007-03-07 09:36:19 +00001482 if(tmplist->params == NULL || tmplist->ptp_usb == NULL) {
Linus Walleijbfcb7922007-03-06 20:14:04 +00001483 /* Error and deallocation of memory will be handled by caller. */
tedbullock0f033cb2007-02-14 20:56:54 +00001484 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1485 }
1486
tedbullock69a445b2007-02-15 07:41:04 +00001487 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleij45a86372007-03-07 09:36:19 +00001488 memset(tmplist->params, 0, sizeof(PTPParams));
1489 memset(tmplist->ptp_usb, 0, sizeof(PTP_USB));
1490 tmplist = tmplist->next;
tedbullock0f033cb2007-02-14 20:56:54 +00001491 }
tedbullock0f033cb2007-02-14 20:56:54 +00001492 return LIBMTP_ERROR_NONE;
1493}
1494
Linus Walleij45a86372007-03-07 09:36:19 +00001495static void assign_known_device_flags(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001496{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001497 int i;
Linus Walleij27c50c52007-03-06 20:34:48 +00001498 mtpdevice_list_t *tmplist = devlist;
1499 uint8_t current_device = 0;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001500
tedbullock0f033cb2007-02-14 20:56:54 +00001501 /* Search through known device list and set correct device flags */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001502 while (tmplist != NULL) {
1503 int device_known = 0;
1504
1505 for(i = 0; i < mtp_device_table_size; i++) {
1506 if(tmplist->libusb_device->descriptor.idVendor == mtp_device_table[i].vendor_id &&
1507 tmplist->libusb_device->descriptor.idProduct == mtp_device_table[i].product_id) {
1508 /* This device is known, assign the correct device flags */
1509 /* Note that ptp_usb[current_device] could potentially be NULL */
Linus Walleij45a86372007-03-07 09:36:19 +00001510 if(tmplist->ptp_usb != NULL) {
1511 tmplist->ptp_usb->device_flags = mtp_device_table[i].device_flags;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001512
1513 /*
1514 * TODO:
1515 * Preferable to not do this with #ifdef ENABLE_USB_BULK_DEBUG but there is
1516 * currently no other compile time debug option
1517 */
1518
1519 device_known = 1;
tedbullock65a05772007-03-03 22:56:56 +00001520#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001521 /* This device is known to the developers */
1522 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is a %s.\n",
1523 current_device + 1,
1524 tmplist->libusb_device->descriptor.idVendor,
1525 tmplist->libusb_device->descriptor.idProduct,
1526 mtp_device_table[i].name);
tedbullock65a05772007-03-03 22:56:56 +00001527#endif
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001528 }
1529 break;
1530 }
tedbullock0f033cb2007-02-14 20:56:54 +00001531 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001532 if (!device_known) {
1533 /* This device is unknown to the developers */
1534 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
1535 current_device + 1,
1536 tmplist->libusb_device->descriptor.idVendor,
1537 tmplist->libusb_device->descriptor.idProduct);
1538 fprintf(stderr, "Please report this VID/PID and the device model to the "
1539 "libmtp development team\n");
1540 }
1541 tmplist = tmplist->next;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001542 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001543 }
tedbullock0f033cb2007-02-14 20:56:54 +00001544}
1545
tedbullock0f033cb2007-02-14 20:56:54 +00001546
Linus Walleij45a86372007-03-07 09:36:19 +00001547static LIBMTP_error_number_t configure_usb_devices(mtpdevice_list_t *devicelist)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001548{
1549 mtpdevice_list_t *tmplist = devicelist;
1550 uint16_t ret = 0;
1551 uint8_t current_device = 0;
1552
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001553 while (tmplist != NULL) {
Linus Walleij45a86372007-03-07 09:36:19 +00001554 /* This is erroneous, there must be a PTP_USB instance that we can initialize. */
1555 if(tmplist->ptp_usb == NULL) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001556 return LIBMTP_ERROR_MEMORY_ALLOCATION;
tedbullock0f033cb2007-02-14 20:56:54 +00001557 }
1558
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001559 /* TODO: Will this always be little endian? */
Linus Walleij45a86372007-03-07 09:36:19 +00001560 tmplist->params->byteorder = PTP_DL_LE;
1561 tmplist->params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8");
1562 tmplist->params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE");
tedbullock0f033cb2007-02-14 20:56:54 +00001563
Linus Walleij45a86372007-03-07 09:36:19 +00001564 if(tmplist->params->cd_locale_to_ucs2 == (iconv_t) -1 ||
1565 tmplist->params->cd_ucs2_to_locale == (iconv_t) -1) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001566 fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
1567 "Too old stdlibc, glibc and libiconv?\n");
Linus Walleij35c0ef12007-03-06 20:49:09 +00001568 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001569 }
1570
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001571 // ep = device->config->interface->altsetting->endpoint;
1572 // no_of_ep = device->config->interface->altsetting->bNumEndpoints;
tedbullock0f033cb2007-02-14 20:56:54 +00001573
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001574 /* Assign endpoints to usbinfo... */
1575 find_endpoints(tmplist->libusb_device,
Linus Walleij45a86372007-03-07 09:36:19 +00001576 &tmplist->ptp_usb->inep,
1577 &tmplist->ptp_usb->inep_maxpacket,
1578 &tmplist->ptp_usb->outep,
1579 &tmplist->ptp_usb->outep_maxpacket,
1580 &tmplist->ptp_usb->intep);
tedbullock0f033cb2007-02-14 20:56:54 +00001581
Linus Walleij45a86372007-03-07 09:36:19 +00001582 /* Attempt to initialize this device */
1583 if (init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) < 0) {
1584 fprintf(stderr, "LIBMTP PANIC: Unable to initialize device %d\n", current_device+1);
1585 // FIXME: perhaps use "continue" to keep trying the other devices.
Linus Walleij35c0ef12007-03-06 20:49:09 +00001586 return LIBMTP_ERROR_CONNECTING;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001587 }
1588
1589 /*
1590 * This works in situations where previous bad applications
1591 * have not used LIBMTP_Release_Device on exit
1592 */
Linus Walleij45a86372007-03-07 09:36:19 +00001593 if ((ret = ptp_opensession(tmplist->params, 1)) == PTP_ERROR_IO) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001594 fprintf(stderr, "PTP_ERROR_IO: Trying again after resetting USB\n");
Linus Walleij45a86372007-03-07 09:36:19 +00001595 close_usb(tmplist->ptp_usb, tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001596
Linus Walleij45a86372007-03-07 09:36:19 +00001597 if(init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) <0) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001598 fprintf(stderr, "LIBMTP PANIC: Could not open session on device %d\n", current_device+1);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001599 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001600 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001601
1602 /* Device has been reset, try again */
Linus Walleij45a86372007-03-07 09:36:19 +00001603 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001604 }
1605
1606 /* Was the transaction id invalid? Try again */
1607 if (ret == PTP_RC_InvalidTransactionID) {
Linus Walleij45a86372007-03-07 09:36:19 +00001608 fprintf(stderr, "LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
1609 tmplist->params->transaction_id += 10;
1610 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001611 }
1612
1613 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
Linus Walleij45a86372007-03-07 09:36:19 +00001614 fprintf(stderr, "LIBMTP PANIC: Could not open session! "
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001615 "(Return code %d)\n Try to reset the device.\n",
1616 ret);
Linus Walleij45a86372007-03-07 09:36:19 +00001617 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001618 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001619 return LIBMTP_ERROR_CONNECTING;
1620 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001621
1622 /* It is permissible to call this before opening the session */
Linus Walleij45a86372007-03-07 09:36:19 +00001623 if (ptp_getdeviceinfo(tmplist->params,
1624 &tmplist->params->deviceinfo) != PTP_RC_OK) {
1625 fprintf(stderr, "LIBMTP PANIC: Could not get device info!\n");
1626 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001627 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001628 return LIBMTP_ERROR_CONNECTING;
1629 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001630
1631 tmplist = tmplist->next;
1632 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001633 }
1634
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001635 /* Exit with the nice list */
1636 return LIBMTP_ERROR_NONE;
tedbullock0f033cb2007-02-14 20:56:54 +00001637}
1638
Linus Walleij550d6d52007-01-24 14:32:51 +00001639/**
1640 * This function scans through the results of the get_mtp_usb_device_list
1641 * function and attempts to connect to those devices listed using the
1642 * mtp_device_table at the top of the file. Returns a LIBMTP_error_number_t.
1643 *
Linus Walleij45a86372007-03-07 09:36:19 +00001644 * @param devlist a list of devices with primed PTP_USB and params structs.
Linus Walleij550d6d52007-01-24 14:32:51 +00001645 * @return Error Codes as per the type definition
1646 */
Linus Walleij45a86372007-03-07 09:36:19 +00001647LIBMTP_error_number_t find_usb_devices(mtpdevice_list_t **devlist)
Linus Walleij550d6d52007-01-24 14:32:51 +00001648{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001649 mtpdevice_list_t *mtp_device_list = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001650 LIBMTP_error_number_t ret;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001651
Linus Walleija4942fc2007-03-12 19:23:21 +00001652 /*
1653 * Recover list of attached USB devices that match MTP criteria, i.e.
1654 * it either has an MTP device descriptor or it is in the known
1655 * devices list.
1656 */
Linus Walleij45a86372007-03-07 09:36:19 +00001657 ret = get_mtp_usb_device_list (&mtp_device_list);
Linus Walleijac061862007-03-07 08:28:33 +00001658 if (ret != LIBMTP_ERROR_NONE) {
1659 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001660 }
1661
Linus Walleij45a86372007-03-07 09:36:19 +00001662 // Then prime them
1663 ret = prime_device_memory(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001664 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001665 fprintf(stderr, "LIBMTP PANIC: prime_device_memory() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001666 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001667 }
tedbullock0f033cb2007-02-14 20:56:54 +00001668
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001669 /* Assign specific device flags and detect unknown devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001670 assign_known_device_flags(mtp_device_list);
Linus Walleij550d6d52007-01-24 14:32:51 +00001671
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001672 /* Configure the devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001673 ret = configure_usb_devices(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001674 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001675 fprintf(stderr, "LIBMTP PANIC: configure_usb_devices() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001676 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001677 }
Linus Walleij550d6d52007-01-24 14:32:51 +00001678
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001679 /* Configure interface numbers */
Linus Walleij550d6d52007-01-24 14:32:51 +00001680 {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001681 mtpdevice_list_t *tmplist = mtp_device_list;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001682
Linus Walleij45a86372007-03-07 09:36:19 +00001683 while (tmplist != NULL) {
1684 tmplist->interface_number = tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001685 tmplist = tmplist->next;
Linus Walleij550d6d52007-01-24 14:32:51 +00001686 }
1687 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001688
Linus Walleij45a86372007-03-07 09:36:19 +00001689 /* we're connected to all devices, return the list and OK */
1690 *devlist = mtp_device_list;
1691 return LIBMTP_ERROR_NONE;
1692
1693 find_usb_devices_error_exit:
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001694 if(mtp_device_list != NULL) {
1695 free_mtpdevice_list(mtp_device_list);
1696 mtp_device_list = NULL;
Linus Walleij550d6d52007-01-24 14:32:51 +00001697 }
Linus Walleij45a86372007-03-07 09:36:19 +00001698 *devlist = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001699 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001700}
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001701
Linus Walleijd6a49972006-05-02 08:24:54 +00001702static 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 +00001703{
Linus Walleijd6a49972006-05-02 08:24:54 +00001704 int i,n;
1705 struct usb_endpoint_descriptor *ep;
1706
1707 ep = dev->config->interface->altsetting->endpoint;
1708 n=dev->config->interface->altsetting->bNumEndpoints;
1709
1710 for (i=0;i<n;i++) {
1711 if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1712 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1713 USB_ENDPOINT_DIR_MASK)
1714 {
1715 *inep=ep[i].bEndpointAddress;
1716 *inep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001717 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001718 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1719 {
1720 *outep=ep[i].bEndpointAddress;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001721 *outep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijd6a49972006-05-02 08:24:54 +00001722 }
1723 } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1724 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1725 USB_ENDPOINT_DIR_MASK)
1726 {
1727 *intep=ep[i].bEndpointAddress;
1728 }
1729 }
1730 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001731}
1732
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001733void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber)
1734{
Linus Walleijd6a49972006-05-02 08:24:54 +00001735 if (ptp_closesession(params)!=PTP_RC_OK)
1736 fprintf(stderr,"ERROR: Could not close session!\n");
1737 close_usb(ptp_usb, interfaceNumber);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001738}
1739
Linus Walleijd6a49972006-05-02 08:24:54 +00001740static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001741{
Linus Walleijd6a49972006-05-02 08:24:54 +00001742
1743 return (usb_control_msg(ptp_usb->handle,
1744 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
1745 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001746}
1747
Linus Walleijd6a49972006-05-02 08:24:54 +00001748static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001749{
Linus Walleijd6a49972006-05-02 08:24:54 +00001750 return (usb_control_msg(ptp_usb->handle,
1751 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
1752 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001753}