blob: 201796b52a842fa1bda98e4d0aee425a4f59311c [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 },
Richard Low6711f442007-05-05 19:00:59 +0000159 // from npedrosa
160 { "Philips PSA610", 0x0471, 0x0181, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000161
162 /*
163 * SanDisk
Linus Walleijf41abe52007-03-20 19:25:18 +0000164 * several devices (c150 for sure) are definately dual-mode and must
165 * have the USB mass storage driver that hooks them unloaded first.
Linus Walleij3d2c7842006-08-18 09:35:08 +0000166 */
167 // Reported by Brian Robison
Linus Walleijf41abe52007-03-20 19:25:18 +0000168 { "SanDisk Sansa m230/m240", 0x0781, 0x7400, DEVICE_FLAG_UNLOAD_DRIVER },
169 // Reported by tangent_@users.sourceforge.net
Linus Walleijb6583182007-03-07 08:14:18 +0000170 { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000171 // From libgphoto2 source
Linus Walleij3d2c7842006-08-18 09:35:08 +0000172 // Reported by gonkflea@users.sourceforge.net
Linus Walleijcbe1b642007-05-02 10:53:28 +0000173 { "SanDisk Sansa e200/e250/e260", 0x0781, 0x7420, DEVICE_FLAG_UNLOAD_DRIVER },
Richard Lowbf2675c2007-03-18 18:20:26 +0000174 // Reported by XNJB user
Linus Walleijf41abe52007-03-20 19:25:18 +0000175 { "SanDisk Sansa e280", 0x0781, 0x7421, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleijef8c1172007-04-30 21:35:50 +0000176 // Reported by anonymous user at sourceforge.net
177 { "SanDisk Sansa c250", 0x0781, 0x7450, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleija927f432007-04-18 20:24:34 +0000178 // Reported by XNJB user, and Miguel de Icaza <miguel@gnome.org>
Linus Walleija20a2432007-04-19 08:41:51 +0000179 // This has no dual-mode so no need to unload any driver.
180 { "SanDisk Sansa Connect", 0x0781, 0x7480, DEVICE_FLAG_NONE },
Richard Low856f3452007-04-13 11:21:26 +0000181
Linus Walleij3d2c7842006-08-18 09:35:08 +0000182
183 /*
184 * iRiver
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000185 * we assume that PTP_OC_MTP_GetObjPropList is essentially
Linus Walleijfabffc02006-12-17 00:18:57 +0000186 * broken on all iRiver devices, meaning it simply won't return
187 * all properties for a file when asking for metadata 0xffffffff.
188 * Please test on your device if you believe it isn't broken!
189 * Some devices from http://www.mtp-ums.net/viewdeviceinfo.php
Linus Walleij3d2c7842006-08-18 09:35:08 +0000190 */
Linus Walleij2be40c72007-03-16 15:19:44 +0000191 { "iRiver Portable Media Center", 0x1006, 0x4002,
192 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
193 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
194 { "iRiver Portable Media Center", 0x1006, 0x4003,
195 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
196 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000197 // From libgphoto2 source
Linus Walleij2be40c72007-03-16 15:19:44 +0000198 { "iRiver T10", 0x4102, 0x1113,
199 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
200 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
201 { "iRiver T20 FM", 0x4102, 0x1114,
202 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
203 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleijfabffc02006-12-17 00:18:57 +0000204 // This appears at the MTP-UMS site
Linus Walleij2be40c72007-03-16 15:19:44 +0000205 { "iRiver T20", 0x4102, 0x1115,
206 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
207 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
208 { "iRiver U10", 0x4102, 0x1116,
209 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
210 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
211 { "iRiver T10", 0x4102, 0x1117,
212 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
213 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
214 { "iRiver T20", 0x4102, 0x1118,
215 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
216 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
217 { "iRiver T30", 0x4102, 0x1119,
218 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
219 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij77e047c2006-09-11 19:09:06 +0000220 // Reported by David Wolpoff
Linus Walleij2be40c72007-03-16 15:19:44 +0000221 { "iRiver T10 2GB", 0x4102, 0x1120,
222 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
223 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij87099812006-12-17 00:29:58 +0000224 // Rough guess this is the MTP device ID...
Linus Walleij2be40c72007-03-16 15:19:44 +0000225 { "iRiver N12", 0x4102, 0x1122,
226 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
227 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij04377da2006-08-30 13:03:35 +0000228 // Reported by Adam Torgerson
Linus Walleij2be40c72007-03-16 15:19:44 +0000229 { "iRiver Clix", 0x4102, 0x112a,
230 DEVICE_FLAG_NO_ZERO_READS | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000231 // Reported by Scott Call
Linus Walleij2be40c72007-03-16 15:19:44 +0000232 { "iRiver H10 20GB", 0x4102, 0x2101,
233 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
234 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
235 { "iRiver H10", 0x4102, 0x2102,
236 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
237 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000238
239 /*
240 * Dell
241 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000242 { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000243
244 /*
245 * Toshiba
246 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000247 { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE },
248 { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE },
Linus Walleij1e3d1762007-02-26 08:27:35 +0000249 // Reported by Nicholas Tripp
250 { "Toshiba Gigabeat P20", 0x0930, 0x000f, DEVICE_FLAG_NONE },
Linus Walleij405e2392006-10-23 07:13:49 +0000251 // From libgphoto2
252 { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE },
Linus Walleij402e4bd2006-09-12 07:50:56 +0000253 // Reported by Rob Brown
254 { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE },
Linus Walleijb01d18b2006-12-13 12:49:15 +0000255
Linus Walleij6f680562006-08-19 22:49:33 +0000256 /*
257 * Archos
258 */
259 // Reported by gudul1@users.sourceforge.net
Linus Walleije5430902006-10-08 14:46:03 +0000260 { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE },
Linus Walleijbbdf44e2007-02-14 09:36:00 +0000261 // Added by Jan Binder
262 { "Archos XS202 (MTP mode)", 0x0e79, 0x1208, DEVICE_FLAG_NONE },
Linus Walleij8282c972007-04-10 19:59:16 +0000263 // Reported by Etienne Chauchot <chauchot.etienne@free.fr>
Linus Walleij933c7802007-04-20 19:18:30 +0000264 // This seems to be dual-mode and thus need to have the unload flag.
265 { "Archos 504 (MTP mode)", 0x0e79, 0x1307, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleije5430902006-10-08 14:46:03 +0000266
267 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000268 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000269 * This unit is falsely detected as USB mass storage in Linux
Linus Walleijcc4dd482006-11-25 21:58:30 +0000270 * prior to kernel 2.6.19 (fixed by patch from Alan Stern)
271 * so on older kernels special care is needed to remove the
272 * USB mass storage driver that erroneously binds to the device
273 * interface.
Linus Walleije5430902006-10-08 14:46:03 +0000274 */
Richard Lowc7d48d22006-11-29 18:24:55 +0000275 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER},
276
277 /*
278 * Microsoft
279 */
280 // Reported by Farooq Zaman
281 { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE },
282
283 /*
284 * Sirius
285 */
Linus Walleijb01d18b2006-12-13 12:49:15 +0000286 { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE },
287
288 /*
289 * Canon
290 * This is actually a camera, but it has a Microsoft device descriptor
291 * and reports itself as supporting the MTP extension.
292 */
Linus Walleijc1b49402007-02-07 08:00:38 +0000293 {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE },
294
295 /*
296 * Nokia
297 */
Linus Walleijde7ad542007-02-14 09:32:26 +0000298 {"Nokia Mobile Phones (MTP mode)", 0x0421, 0x04e1, DEVICE_FLAG_NONE },
299
300 /*
301 * LOGIK
302 * Sold in the UK, seem to be manufactured by CCTech in China.
303 */
Linus Walleijb26e0f52007-04-24 07:40:04 +0000304 {"Logik LOG DAX MP3 and DAB Player", 0x13d1, 0x7002, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleijd2ff9e32007-03-16 15:27:45 +0000305
306 /*
Linus Walleijedb45332007-04-26 18:54:47 +0000307 * RCA / Thomson
308 */
309 {"Thomson Opal / RCA Lyra MC4002", 0x069b, 0x0777, DEVICE_FLAG_NONE },
310
311 /*
Linus Walleijd2ff9e32007-03-16 15:27:45 +0000312 * Other strange stuff.
313 */
Richard Low1869d272007-04-28 13:55:17 +0000314 {"Isabella's prototype", 0x0b20, 0xddee, DEVICE_FLAG_NONE },
315
316 /*
317 * NTT DoCoMo
318 */
319 {"FOMA F903iX HIGH-SPEED", 0x04c5, 0x1140, DEVICE_FLAG_NONE }
Richard Low6711f442007-05-05 19:00:59 +0000320
Linus Walleija5483642006-03-09 09:20:38 +0000321};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000322static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000323
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000324int ptpcam_usb_timeout = USB_TIMEOUT;
325
Linus Walleijd6a49972006-05-02 08:24:54 +0000326// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000327static struct usb_bus* init_usb();
Linus Walleijd6a49972006-05-02 08:24:54 +0000328static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);
Linus Walleijd6a49972006-05-02 08:24:54 +0000329static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);
330static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000331static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000332static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000333static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000334static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
335static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000336
Linus Walleij552ba322007-01-22 11:49:59 +0000337/**
338 * Get a list of the supported USB devices.
339 *
340 * The developers depend on users of this library to constantly
341 * add in to the list of supported devices. What we need is the
342 * device name, USB Vendor ID (VID) and USB Product ID (PID).
343 * put this into a bug ticket at the project homepage, please.
344 * The VID/PID is used to let e.g. udev lift the device to
345 * console userspace access when it's plugged in.
346 *
347 * @param devices a pointer to a pointer that will hold a device
348 * list after the call to this function, if it was
349 * successful.
350 * @param numdevs a pointer to an integer that will hold the number
351 * of devices in the device list if the call was successful.
352 * @return 0 if the list was successfull retrieved, any other
353 * value means failure.
354 */
355int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000356{
357 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
358 *numdevs = mtp_device_table_size;
359 return 0;
360}
361
Linus Walleij552ba322007-01-22 11:49:59 +0000362
Linus Walleij1fd2d272006-05-08 09:22:01 +0000363static struct usb_bus* init_usb()
364{
365 usb_init();
366 usb_find_busses();
367 usb_find_devices();
368 return (usb_get_busses());
369}
370
371/**
tedbullock0f033cb2007-02-14 20:56:54 +0000372 * Small recursive function to append a new usb_device to the linked list of
373 * USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000374 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000375 * properties.
376 * @param next New USB MTP device to be added to list
377 * @return nothing
378 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000379static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
380 struct usb_device *newdevice)
tedbullock0f033cb2007-02-14 20:56:54 +0000381{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000382 mtpdevice_list_t *new_list_entry;
tedbullock0f033cb2007-02-14 20:56:54 +0000383
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000384 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
385 if (new_list_entry == NULL) {
386 return NULL;
387 }
388 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
389 new_list_entry->libusb_device = newdevice;
390 new_list_entry->next = NULL;
391
392 if (devlist == NULL) {
393 return new_list_entry;
394 } else {
395 mtpdevice_list_t *tmp = devlist;
396 while (tmp->next != NULL) {
397 tmp = tmp->next;
398 }
399 tmp->next = new_list_entry;
400 }
401 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000402}
403
404/**
405 * Small recursive function to free dynamic memory allocated to the linked list
406 * of USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000407 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000408 * properties.
409 * @return nothing
410 */
Linus Walleij45a86372007-03-07 09:36:19 +0000411void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000412{
Linus Walleijac061862007-03-07 08:28:33 +0000413 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000414
415 if (devlist == NULL)
416 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000417 while (tmplist != NULL) {
418 mtpdevice_list_t *tmp = tmplist;
419 tmplist = tmplist->next;
Linus Walleij45a86372007-03-07 09:36:19 +0000420 // Do not free() the fields (ptp_usb, parms)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000421 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000422 }
tedbullock0f033cb2007-02-14 20:56:54 +0000423 return;
424}
425
426/**
Linus Walleija4942fc2007-03-12 19:23:21 +0000427 * This checks if a device has an MTP descriptor.
428 * @param dev a device struct from libusb.
429 * @return 1 if the device is MTP compliant, 0 if not.
430 */
431static int device_has_descriptor(struct usb_device *dev)
432{
433 usb_dev_handle *devh;
434 unsigned char buf[1024], cmd;
435 int ret;
436
437 /* Don't examine hubs (no point in that) */
438 if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) {
439 return 0;
440 }
441
442 /* Attempt to open Device on this port */
443 devh = usb_open(dev);
444 if (devh == NULL) {
445 /* Could not open this device */
446 return 0;
447 }
448
449 /* Read the special descriptor */
450 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
451
452 /* Check if descriptor length is at least 10 bytes */
453 if (ret < 10) {
454 usb_close(devh);
455 return 0;
456 }
457
458 /* Check if this device has a Microsoft Descriptor */
459 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
460 (buf[6] == 'F') && (buf[8] == 'T'))) {
461 usb_close(devh);
462 return 0;
463 }
464
465 /* Check if device responds to control message 1 or if there is an error */
466 cmd = buf[16];
467 ret = usb_control_msg (devh,
468 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
469 cmd,
470 0,
471 4,
472 (char *) buf,
473 sizeof(buf),
474 1000);
475
476 /* If this is true, the device either isn't MTP or there was an error */
477 if (ret <= 0x15) {
478 /* TODO: If there was an error, flag it and let the user know somehow */
479 /* if(ret == -1) {} */
480 usb_close(devh);
481 return 0;
482 }
483
484 /* Check if device is MTP or if it is something like a USB Mass Storage
485 device with Janus DRM support */
486 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
487 usb_close(devh);
488 return 0;
489 }
490
491 /* After this point we are probably dealing with an MTP device */
492
493 /* Check if device responds to control message 2 or if there is an error*/
494 ret = usb_control_msg (devh,
495 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
496 cmd,
497 0,
498 5,
499 (char *) buf,
500 sizeof(buf),
501 1000);
502
503 /* If this is true, the device errored against control message 2 */
504 if (ret == -1) {
505 /* TODO: Implement callback function to let managing program know there
506 was a problem, along with description of the problem */
507 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
508 "ProductID:%04x encountered an error responding to "
509 "control message 2.\n"
510 "Problems may arrise but continuing\n",
511 dev->descriptor.idVendor, dev->descriptor.idProduct);
512 } else if (ret <= 0x15) {
513 /* TODO: Implement callback function to let managing program know there
514 was a problem, along with description of the problem */
515 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
516 "ProductID:%04x responded to control message 2 with a "
517 "response that was too short. Problems may arrise but "
518 "continuing\n",
519 dev->descriptor.idVendor, dev->descriptor.idProduct);
520 } else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
521 /* TODO: Implement callback function to let managing program know there
522 was a problem, along with description of the problem */
523 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
524 "ProductID:%04x encountered an error responding to "
525 "control message 2\n"
526 "Problems may arrise but continuing\n",
527 dev->descriptor.idVendor, dev->descriptor.idProduct);
528 }
529
530 /* Close the USB device handle */
531 usb_close(devh);
532 return 1;
533}
534
535/**
536 * This function scans through the connected usb devices on a machine and
537 * if they match known Vendor and Product identifiers appends them to the
538 * dynamic array mtp_device_list. Be sure to call
539 * <code>free(mtp_device_list)</code> when you are done with it, assuming it
540 * is not NULL.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000541 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000542 * properties (if this list is not empty, new entries will be appended
543 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000544 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
545 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no devices have
546 * been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000547 */
Linus Walleij45a86372007-03-07 09:36:19 +0000548static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000549{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000550 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000551
552 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000553 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000554 for (; dev != NULL; dev = dev->next) {
Linus Walleija4942fc2007-03-12 19:23:21 +0000555 if (device_has_descriptor(dev)) {
556 /* Append this usb device to the MTP USB Device List */
557 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
558 } else {
559 /* Check if it's in the known devices list then */
560 int i;
561
562 for(i = 0; i < mtp_device_table_size; i++) {
563 if(dev->descriptor.bDeviceClass != USB_CLASS_HUB &&
564 dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
565 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
566 /* Append this usb device to the MTP device list */
567 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
568 break;
569 }
570 }
Linus Walleij35c0ef12007-03-06 20:49:09 +0000571 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000572 }
573 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000574
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000575 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000576 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000577 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000578 }
579 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000580}
581
582/**
Linus Walleij1fd2d272006-05-08 09:22:01 +0000583 * Detect the MTP device descriptor and return the VID and PID
584 * of the first device found. This is a very low-level function
585 * which is intended for use with <b>udev</b> or other hotplug
586 * mechanisms. The idea is that a script may want to know if the
587 * just plugged-in device was an MTP device or not.
tedbullock2b45f2e2007-02-23 20:16:37 +0000588 *
Linus Walleij1fd2d272006-05-08 09:22:01 +0000589 * @param vid the Vendor ID (VID) of the first device found.
590 * @param pid the Product ID (PID) of the first device found.
591 * @return the number of detected devices or -1 if the call
592 * was unsuccessful.
593 */
594int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
595{
Linus Walleijb6b69132007-03-07 09:45:05 +0000596 mtpdevice_list_t *devlist;
597 LIBMTP_error_number_t ret;
598
599 ret = get_mtp_usb_device_list(&devlist);
600 if (ret != LIBMTP_ERROR_NONE) {
601 *vid = *pid = 0;
602 return -1;
603 }
604 *vid = devlist->libusb_device->descriptor.idVendor;
605 *pid = devlist->libusb_device->descriptor.idProduct;
606 free_mtpdevice_list(devlist);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000607 return 1;
608}
609
Linus Walleijc6210fb2006-05-08 11:11:41 +0000610/**
611 * This routine just dumps out low-level
612 * USB information about the current device.
613 * @param ptp_usb the USB device to get information from.
614 */
615void dump_usbinfo(PTP_USB *ptp_usb)
616{
617 int res;
618 struct usb_device *dev;
619
620#ifdef LIBUSB_HAS_GET_DRIVER_NP
621 char devname[0x10];
622
623 devname[0] = '\0';
624 res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));
625 if (devname[0] != '\0') {
626 printf(" Using kernel interface \"%s\"\n", devname);
627 }
628#endif
629 dev = usb_device(ptp_usb->handle);
630 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
631 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
632 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
633 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000634 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
635 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000636 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
637 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000638 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000639 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
640}
641
Linus Walleija0323272007-01-07 12:21:30 +0000642static void
643ptp_debug (PTPParams *params, const char *format, ...)
644{
645 va_list args;
646
647 va_start (args, format);
648 if (params->debug_func!=NULL)
649 params->debug_func (params->data, format, args);
650 else
651 {
652 vfprintf (stderr, format, args);
653 fprintf (stderr,"\n");
654 fflush (stderr);
655 }
656 va_end (args);
657}
658
659static void
660ptp_error (PTPParams *params, const char *format, ...)
661{
662 va_list args;
663
664 va_start (args, format);
665 if (params->error_func!=NULL)
666 params->error_func (params->data, format, args);
667 else
668 {
669 vfprintf (stderr, format, args);
670 fprintf (stderr,"\n");
671 fflush (stderr);
672 }
673 va_end (args);
674}
675
Linus Walleijc6210fb2006-05-08 11:11:41 +0000676
Linus Walleij99ed83c2007-01-02 18:46:02 +0000677/*
678 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000679 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000680 * Much reading packet logs and having fun with trials and errors
681 * reveals that WMP / Windows is probably using an algorithm like this
682 * for large transfers:
683 *
684 * 1. Send the command (0x0c bytes) if headers are split, else, send
685 * command plus sizeof(endpoint) - 0x0c bytes.
686 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
687 * split headers. Else goto 3.
688 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
689 * We call 0x10000 CONTEXT_BLOCK_SIZE.
690 * 4. Send remaining bytes MOD sizeof(endpoint)
691 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
692 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000693 *
694 * Further there is some special quirks to handle zero reads from the
695 * device, since some devices can't do them at all due to shortcomings
696 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000697 */
Richard Low4fd59d62007-03-03 16:34:37 +0000698#define CONTEXT_BLOCK_SIZE_1 0x3e00
699#define CONTEXT_BLOCK_SIZE_2 0x200
700#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000701static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000702ptp_read_func (
703 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000704 unsigned long *readbytes,
705 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000706) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000707 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000708 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000709 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000710 unsigned long curread = 0;
711 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000712 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000713 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000714
715 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000716 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000717 while (curread < size) {
Richard Low4fd59d62007-03-03 16:34:37 +0000718
719#ifdef ENABLE_USB_BULK_DEBUG
720 printf("Remaining size to read: 0x%04x bytes\n", size - curread);
721#endif
722 // check equal to condition here
723 if (size - curread < CONTEXT_BLOCK_SIZE)
724 {
725 // this is the last packet
726 toread = size - curread;
727 // this is equivalent to zero read for these devices
Linus Walleij049f50c2007-03-03 20:30:43 +0000728 if (readzero && ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000729 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000730 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000731 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000732 }
Richard Low4fd59d62007-03-03 16:34:37 +0000733 else if (curread == 0)
734 // we are first packet, but not last packet
735 toread = CONTEXT_BLOCK_SIZE_1;
736 else if (toread == CONTEXT_BLOCK_SIZE_1)
737 toread = CONTEXT_BLOCK_SIZE_2;
738 else if (toread == CONTEXT_BLOCK_SIZE_2)
739 toread = CONTEXT_BLOCK_SIZE_1;
740 else
Linus Walleij4cec9872007-03-03 21:00:53 +0000741 printf("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
742 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000743
Richard Lowd284f072007-02-17 08:52:41 +0000744#ifdef ENABLE_USB_BULK_DEBUG
745 printf("Reading in 0x%04x bytes\n", toread);
746#endif
Linus Walleij784ac3f2006-12-29 10:36:51 +0000747 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout);
Richard Lowd284f072007-02-17 08:52:41 +0000748#ifdef ENABLE_USB_BULK_DEBUG
749 printf("Result of read: 0x%04x\n", result);
750#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000751
Linus Walleijc49c6372007-01-09 00:18:51 +0000752 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000753 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000754 }
Linus Walleije78de6d2006-10-31 14:46:36 +0000755#ifdef ENABLE_USB_BULK_DEBUG
756 printf("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000757 if (result == 0)
758 printf("Zero Read\n");
759 else
760 data_dump_ascii (stdout,bytes,result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000761#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000762
763 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000764 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000765 {
766#ifdef ENABLE_USB_BULK_DEBUG
767 printf("<==USB IN\nDiscarding extra byte\n");
768#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000769 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000770 }
Richard Low4fd59d62007-03-03 16:34:37 +0000771
Linus Walleijc49c6372007-01-09 00:18:51 +0000772 handler->putfunc(NULL, handler->private, result, bytes, &written);
Richard Lowbf2675c2007-03-18 18:20:26 +0000773
Linus Walleijc49c6372007-01-09 00:18:51 +0000774 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000775 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000776
777 // Increase counters, call callback
778 if (ptp_usb->callback_active) {
779 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
780 // send last update and disable callback.
781 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
782 ptp_usb->callback_active = 0;
783 }
784 if (ptp_usb->current_transfer_callback != NULL) {
785 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
786 ptp_usb->current_transfer_total,
787 ptp_usb->current_transfer_callback_data);
788 }
789 }
790
Linus Walleijd6a49972006-05-02 08:24:54 +0000791 if (result < toread) /* short reads are common */
792 break;
793 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000794 if (readbytes) *readbytes = curread;
795 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000796
Richard Low4df32f82007-01-11 20:04:39 +0000797 // there might be a zero packet waiting for us...
Linus Walleij049f50c2007-03-03 20:30:43 +0000798 if (readzero &&
799 !(ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS) &&
800 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000801 char temp;
802 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000803
Richard Low4df32f82007-01-11 20:04:39 +0000804#ifdef ENABLE_USB_BULK_DEBUG
805 printf("<==USB IN\n");
806 printf("Zero Read\n");
807#endif
808 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptpcam_usb_timeout);
809 if (zeroresult != 0)
810 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
811 }
812
Linus Walleijd6a49972006-05-02 08:24:54 +0000813 if (result > 0) {
Linus Walleijd6a49972006-05-02 08:24:54 +0000814 return (PTP_RC_OK);
815 } else {
816 return PTP_ERROR_IO;
817 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000818}
819
Linus Walleijb02a0662006-04-25 08:05:09 +0000820static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000821ptp_write_func (
822 unsigned long size,
823 PTPDataHandler *handler,
824 void *data,
825 unsigned long *written
826) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000827 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000828 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000829 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000830 unsigned long curwrite = 0;
831 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000832
833 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000834 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000835 if (!bytes) {
836 return PTP_ERROR_IO;
837 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000838 while (curwrite < size) {
839 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000840 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000841 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000842 } else {
843 // This magic makes packets the same size that WMP send them.
844 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000845 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000846 }
847 }
848 handler->getfunc(NULL, handler->private,towrite,bytes,&towrite);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000849 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000850#ifdef ENABLE_USB_BULK_DEBUG
851 printf("USB OUT==>\n");
Richard Lowedd61832006-12-30 12:38:18 +0000852 data_dump_ascii (stdout,bytes,towrite,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000853#endif
Linus Walleijc49c6372007-01-09 00:18:51 +0000854 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000855 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000856 }
857 // Increase counters
858 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000859 curwrite += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000860
861 // call callback
862 if (ptp_usb->callback_active) {
863 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
864 // send last update and disable callback.
865 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
866 ptp_usb->callback_active = 0;
867 }
868 if (ptp_usb->current_transfer_callback != NULL) {
869 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
870 ptp_usb->current_transfer_total,
871 ptp_usb->current_transfer_callback_data);
872 }
873 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000874 if (result < towrite) /* short writes happen */
875 break;
876 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000877 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000878 if (written) {
879 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000880 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000881
Linus Walleijc49c6372007-01-09 00:18:51 +0000882
Richard Lowef05b892007-01-03 21:18:56 +0000883 // If this is the last transfer send a zero write if required
884 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000885 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
Richard Low021421e2007-01-01 18:08:57 +0000886#ifdef ENABLE_USB_BULK_DEBUG
887 printf("USB OUT==>\n");
888 printf("Zero Write\n");
889#endif
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000890 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000891 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000892 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000893
Linus Walleijd6a49972006-05-02 08:24:54 +0000894 if (result < 0)
895 return PTP_ERROR_IO;
896 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000897}
898
Linus Walleija0323272007-01-07 12:21:30 +0000899/* memory data get/put handler */
900typedef struct {
901 unsigned char *data;
902 unsigned long size, curoff;
903} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000904
Linus Walleija0323272007-01-07 12:21:30 +0000905static uint16_t
906memory_getfunc(PTPParams* params, void* private,
907 unsigned long wantlen, unsigned char *data,
908 unsigned long *gotlen
909) {
910 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
911 unsigned long tocopy = wantlen;
912
913 if (priv->curoff + tocopy > priv->size)
914 tocopy = priv->size - priv->curoff;
915 memcpy (data, priv->data + priv->curoff, tocopy);
916 priv->curoff += tocopy;
917 *gotlen = tocopy;
918 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000919}
920
Linus Walleija0323272007-01-07 12:21:30 +0000921static uint16_t
922memory_putfunc(PTPParams* params, void* private,
923 unsigned long sendlen, unsigned char *data,
924 unsigned long *putlen
925) {
926 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
927
928 if (priv->curoff + sendlen > priv->size) {
929 priv->data = realloc (priv->data, priv->curoff+sendlen);
930 priv->size = priv->curoff + sendlen;
931 }
932 memcpy (priv->data + priv->curoff, data, sendlen);
933 priv->curoff += sendlen;
934 *putlen = sendlen;
935 return PTP_RC_OK;
936}
937
938/* init private struct for receiving data. */
939static uint16_t
940ptp_init_recv_memory_handler(PTPDataHandler *handler) {
941 PTPMemHandlerPrivate* priv;
942 priv = malloc (sizeof(PTPMemHandlerPrivate));
943 handler->private = priv;
944 handler->getfunc = memory_getfunc;
945 handler->putfunc = memory_putfunc;
946 priv->data = NULL;
947 priv->size = 0;
948 priv->curoff = 0;
949 return PTP_RC_OK;
950}
951
952/* init private struct and put data in for sending data.
953 * data is still owned by caller.
954 */
955static uint16_t
956ptp_init_send_memory_handler(PTPDataHandler *handler,
957 unsigned char *data, unsigned long len
958) {
959 PTPMemHandlerPrivate* priv;
960 priv = malloc (sizeof(PTPMemHandlerPrivate));
961 if (!priv)
962 return PTP_RC_GeneralError;
963 handler->private = priv;
964 handler->getfunc = memory_getfunc;
965 handler->putfunc = memory_putfunc;
966 priv->data = data;
967 priv->size = len;
968 priv->curoff = 0;
969 return PTP_RC_OK;
970}
971
972/* free private struct + data */
973static uint16_t
974ptp_exit_send_memory_handler (PTPDataHandler *handler) {
975 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
976 /* data is owned by caller */
977 free (priv);
978 return PTP_RC_OK;
979}
980
981/* hand over our internal data to caller */
982static uint16_t
983ptp_exit_recv_memory_handler (PTPDataHandler *handler,
984 unsigned char **data, unsigned long *size
985) {
986 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
987 *data = priv->data;
988 *size = priv->size;
989 free (priv);
990 return PTP_RC_OK;
991}
992
993/* send / receive functions */
994
995uint16_t
996ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
997{
998 uint16_t ret;
999 PTPUSBBulkContainer usbreq;
1000 PTPDataHandler memhandler;
1001 unsigned long written, towrite;
1002
1003 /* build appropriate USB container */
1004 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
1005 (sizeof(uint32_t)*(5-req->Nparam)));
1006 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
1007 usbreq.code=htod16(req->Code);
1008 usbreq.trans_id=htod32(req->Transaction_ID);
1009 usbreq.payload.params.param1=htod32(req->Param1);
1010 usbreq.payload.params.param2=htod32(req->Param2);
1011 usbreq.payload.params.param3=htod32(req->Param3);
1012 usbreq.payload.params.param4=htod32(req->Param4);
1013 usbreq.payload.params.param5=htod32(req->Param5);
1014 /* send it to responder */
1015 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
1016 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
1017 ret=ptp_write_func(
1018 towrite,
1019 &memhandler,
1020 params->data,
1021 &written
1022 );
1023 ptp_exit_send_memory_handler (&memhandler);
1024 if (ret!=PTP_RC_OK) {
1025 ret = PTP_ERROR_IO;
1026/* ptp_error (params,
1027 "PTP: request code 0x%04x sending req error 0x%04x",
1028 req->Code,ret); */
1029 }
1030 if (written != towrite) {
1031 ptp_error (params,
1032 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001033 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001034 );
1035 ret = PTP_ERROR_IO;
1036 }
1037 return ret;
1038}
1039
1040uint16_t
1041ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1042 unsigned long size, PTPDataHandler *handler
1043) {
1044 uint16_t ret;
1045 int wlen, datawlen;
1046 unsigned long written;
1047 PTPUSBBulkContainer usbdata;
1048 uint32_t bytes_left_to_transfer;
1049 PTPDataHandler memhandler;
1050
1051 /* build appropriate USB container */
1052 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1053 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1054 usbdata.code = htod16(ptp->Code);
1055 usbdata.trans_id= htod32(ptp->Transaction_ID);
1056
1057 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001058 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001059
1060 if (params->split_header_data) {
1061 datawlen = 0;
1062 wlen = PTP_USB_BULK_HDR_LEN;
1063 } else {
1064 unsigned long gotlen;
1065 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001066 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001067 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
1068 ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen);
1069 if (ret != PTP_RC_OK)
1070 return ret;
1071 if (gotlen != datawlen)
1072 return PTP_RC_GeneralError;
1073 }
1074 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1075 /* send first part of data */
1076 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1077 ptp_exit_send_memory_handler (&memhandler);
1078 if (ret!=PTP_RC_OK) {
1079 ret = PTP_ERROR_IO;
1080/* ptp_error (params,
1081 "PTP: request code 0x%04x sending data error 0x%04x",
1082 ptp->Code,ret);*/
1083 return ret;
1084 }
1085 if (size <= datawlen) return ret;
1086 /* if everything OK send the rest */
1087 bytes_left_to_transfer = size-datawlen;
1088 ret = PTP_RC_OK;
1089 while(bytes_left_to_transfer > 0) {
1090 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1091 if (ret != PTP_RC_OK)
1092 break;
1093 if (written == 0) {
1094 ret = PTP_ERROR_IO;
1095 break;
1096 }
1097 bytes_left_to_transfer -= written;
1098 }
1099 if (ret!=PTP_RC_OK)
1100 ret = PTP_ERROR_IO;
1101 return ret;
1102}
1103
1104static uint16_t ptp_usb_getpacket(PTPParams *params,
1105 PTPUSBBulkContainer *packet, unsigned long *rlen)
1106{
1107 PTPDataHandler memhandler;
1108 uint16_t ret;
1109 unsigned char *x = NULL;
1110
1111 /* read the header and potentially the first data */
1112 if (params->response_packet_size > 0) {
1113 /* If there is a buffered packet, just use it. */
1114 memcpy(packet, params->response_packet, params->response_packet_size);
1115 *rlen = params->response_packet_size;
1116 free(params->response_packet);
1117 params->response_packet = NULL;
1118 params->response_packet_size = 0;
1119 /* Here this signifies a "virtual read" */
1120 return PTP_RC_OK;
1121 }
1122 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001123 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001124 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1125 if (x) {
1126 memcpy (packet, x, *rlen);
1127 free (x);
1128 }
1129 return ret;
1130}
1131
1132uint16_t
1133ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1134{
1135 uint16_t ret;
1136 PTPUSBBulkContainer usbdata;
Linus Walleija0323272007-01-07 12:21:30 +00001137 unsigned long written;
1138
1139 memset(&usbdata,0,sizeof(usbdata));
1140 do {
1141 unsigned long len, rlen;
1142
1143 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1144 if (ret!=PTP_RC_OK) {
1145 ret = PTP_ERROR_IO;
1146 break;
1147 } else
1148 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1149 ret = PTP_ERROR_DATA_EXPECTED;
1150 break;
1151 } else
1152 if (dtoh16(usbdata.code)!=ptp->Code) {
1153 ret = dtoh16(usbdata.code);
1154 break;
1155 }
1156 if (usbdata.length == 0xffffffffU) {
1157 /* stuff data directly to passed data handler */
1158 while (1) {
1159 unsigned long readdata;
1160 int xret;
1161
1162 xret = ptp_read_func(
Richard Low02724f62007-02-17 09:47:26 +00001163 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
Linus Walleija0323272007-01-07 12:21:30 +00001164 handler,
1165 params->data,
Richard Low4df32f82007-01-11 20:04:39 +00001166 &readdata,
Linus Walleijc0a11432007-03-02 21:21:18 +00001167 0
Linus Walleija0323272007-01-07 12:21:30 +00001168 );
1169 if (xret == -1)
1170 return PTP_ERROR_IO;
Richard Low02724f62007-02-17 09:47:26 +00001171 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
Linus Walleija0323272007-01-07 12:21:30 +00001172 break;
1173 }
1174 return PTP_RC_OK;
1175 }
1176 if (rlen > dtoh32(usbdata.length)) {
1177 /*
1178 * Buffer the surplus response packet if it is >=
1179 * PTP_USB_BULK_HDR_LEN
1180 * (i.e. it is probably an entire package)
1181 * else discard it as erroneous surplus data.
1182 * This will even work if more than 2 packets appear
1183 * in the same transaction, they will just be handled
1184 * iteratively.
1185 *
1186 * Marcus observed stray bytes on iRiver devices;
1187 * these are still discarded.
1188 */
1189 unsigned int packlen = dtoh32(usbdata.length);
1190 unsigned int surplen = rlen - packlen;
1191
1192 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1193 params->response_packet = malloc(surplen);
1194 memcpy(params->response_packet,
1195 (uint8_t *) &usbdata + packlen, surplen);
1196 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001197 /* Ignore reading one extra byte if device flags have been set */
1198 } else if( !(((PTP_USB *)params->data)->device_flags &
1199 DEVICE_FLAG_NO_ZERO_READS &&
1200 rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001201 ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
1202 "too much, expect problems!",
1203 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001204 }
1205 rlen = packlen;
1206 }
1207
1208 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1209 * here. For MTP devices splitting header and data it might
1210 * be 12.
1211 */
1212 /* Evaluate full data length. */
1213 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1214
1215 /* autodetect split header/data MTP devices */
1216 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1217 params->split_header_data = 1;
1218
Linus Walleija0323272007-01-07 12:21:30 +00001219 /* Copy first part of data to 'data' */
1220 handler->putfunc(
1221 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1222 &written
1223 );
Richard Low4fd59d62007-03-03 16:34:37 +00001224
Linus Walleij049f50c2007-03-03 20:30:43 +00001225 if (((PTP_USB *)params->data)->device_flags & DEVICE_FLAG_NO_ZERO_READS &&
1226 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
Richard Low057ea772007-03-24 13:37:38 +00001227#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleij049f50c2007-03-03 20:30:43 +00001228 printf("Reading in extra terminating byte\n");
Richard Low4fd59d62007-03-03 16:34:37 +00001229#endif
Linus Walleij049f50c2007-03-03 20:30:43 +00001230 // need to read in extra byte and discard it
1231 int result = 0;
1232 char byte = 0;
1233 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1234 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptpcam_usb_timeout);
1235
1236 if (result != 1)
1237 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 +00001238 } else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ && params->split_header_data == 0) {
1239#ifdef ENABLE_USB_BULK_DEBUG
1240 printf("Reading in zero packet after header\n");
1241#endif
1242 int zeroresult = 0;
1243 char zerobyte = 0;
1244 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1245 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptpcam_usb_timeout);
1246
1247 if (zeroresult != 0)
1248 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
1249 }
1250
Linus Walleija0323272007-01-07 12:21:30 +00001251 /* Is that all of data? */
1252 if (len+PTP_USB_BULK_HDR_LEN<=rlen) break;
Linus Walleij049f50c2007-03-03 20:30:43 +00001253
1254 ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1255 handler,
1256 params->data, &rlen, 1);
1257
Linus Walleija0323272007-01-07 12:21:30 +00001258 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001259 ret = PTP_ERROR_IO;
1260 break;
Linus Walleija0323272007-01-07 12:21:30 +00001261 }
1262 } while (0);
Linus Walleij049f50c2007-03-03 20:30:43 +00001263 /*
Linus Walleija0323272007-01-07 12:21:30 +00001264 if (ret!=PTP_RC_OK) {
1265 ptp_error (params,
1266 "PTP: request code 0x%04x getting data error 0x%04x",
1267 ptp->Code, ret);
1268 }*/
1269 return ret;
1270}
1271
1272uint16_t
1273ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1274{
1275 uint16_t ret;
1276 unsigned long rlen;
1277 PTPUSBBulkContainer usbresp;
1278
1279 memset(&usbresp,0,sizeof(usbresp));
1280 /* read response, it should never be longer than sizeof(usbresp) */
1281 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1282
1283 if (ret!=PTP_RC_OK) {
1284 ret = PTP_ERROR_IO;
1285 } else
1286 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1287 ret = PTP_ERROR_RESP_EXPECTED;
1288 } else
1289 if (dtoh16(usbresp.code)!=resp->Code) {
1290 ret = dtoh16(usbresp.code);
1291 }
1292 if (ret!=PTP_RC_OK) {
1293/* ptp_error (params,
1294 "PTP: request code 0x%04x getting resp error 0x%04x",
1295 resp->Code, ret);*/
1296 return ret;
1297 }
1298 /* build an appropriate PTPContainer */
1299 resp->Code=dtoh16(usbresp.code);
1300 resp->SessionID=params->session_id;
1301 resp->Transaction_ID=dtoh32(usbresp.trans_id);
1302 resp->Param1=dtoh32(usbresp.payload.params.param1);
1303 resp->Param2=dtoh32(usbresp.payload.params.param2);
1304 resp->Param3=dtoh32(usbresp.payload.params.param3);
1305 resp->Param4=dtoh32(usbresp.payload.params.param4);
1306 resp->Param5=dtoh32(usbresp.payload.params.param5);
1307 return ret;
1308}
1309
1310/* Event handling functions */
1311
1312/* PTP Events wait for or check mode */
1313#define PTP_EVENT_CHECK 0x0000 /* waits for */
1314#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1315
1316static inline uint16_t
1317ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1318{
1319 uint16_t ret;
1320 int result;
1321 unsigned long rlen;
1322 PTPUSBEventContainer usbevent;
1323 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1324
1325 memset(&usbevent,0,sizeof(usbevent));
1326
1327 if ((params==NULL) || (event==NULL))
1328 return PTP_ERROR_BADPARAM;
1329 ret = PTP_RC_OK;
1330 switch(wait) {
1331 case PTP_EVENT_CHECK:
1332 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1333 if (result==0)
1334 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1335 if (result < 0) ret = PTP_ERROR_IO;
1336 break;
1337 case PTP_EVENT_CHECK_FAST:
1338 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1339 if (result==0)
1340 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1341 if (result < 0) ret = PTP_ERROR_IO;
1342 break;
1343 default:
1344 ret=PTP_ERROR_BADPARAM;
1345 break;
1346 }
1347 if (ret!=PTP_RC_OK) {
1348 ptp_error (params,
1349 "PTP: reading event an error 0x%04x occurred", ret);
1350 return PTP_ERROR_IO;
1351 }
1352 rlen = result;
1353 if (rlen < 8) {
1354 ptp_error (params,
1355 "PTP: reading event an short read of %ld bytes occurred", rlen);
1356 return PTP_ERROR_IO;
1357 }
1358 /* if we read anything over interrupt endpoint it must be an event */
1359 /* build an appropriate PTPContainer */
1360 event->Code=dtoh16(usbevent.code);
1361 event->SessionID=params->session_id;
1362 event->Transaction_ID=dtoh32(usbevent.trans_id);
1363 event->Param1=dtoh32(usbevent.param1);
1364 event->Param2=dtoh32(usbevent.param2);
1365 event->Param3=dtoh32(usbevent.param3);
1366 return ret;
1367}
1368
1369uint16_t
1370ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1371
1372 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1373}
1374
1375uint16_t
1376ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1377
1378 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1379}
1380
1381
Linus Walleij9eb3d312006-08-04 19:25:59 +00001382static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001383{
Linus Walleijd6a49972006-05-02 08:24:54 +00001384 usb_dev_handle *device_handle;
1385
Linus Walleijd6a49972006-05-02 08:24:54 +00001386 params->error_func=NULL;
1387 params->debug_func=NULL;
1388 params->sendreq_func=ptp_usb_sendreq;
1389 params->senddata_func=ptp_usb_senddata;
1390 params->getresp_func=ptp_usb_getresp;
1391 params->getdata_func=ptp_usb_getdata;
1392 params->data=ptp_usb;
1393 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001394 /*
1395 * This is hardcoded here since we have no devices whatsoever that are BE.
1396 * Change this the day we run into our first BE device (if ever).
1397 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001398 params->byteorder = PTP_DL_LE;
1399
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001400 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001401 if (!device_handle) {
1402 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001403 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001404 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001405 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001406#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1407 /*
1408 * If this device is known to be wrongfully claimed by other kernel
1409 * drivers (such as mass storage), then try to unload it to make it
1410 * accessible from user space.
1411 */
1412 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
1413 if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij7beba572006-11-29 08:56:12 +00001414 // Totally ignore this error!
1415 // perror("usb_detach_kernel_driver_np()");
Linus Walleij501ba4d2006-10-16 06:17:04 +00001416 }
1417 }
1418#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001419#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001420 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001421 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1422 perror("usb_set_configuration()");
1423 return -1;
1424 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001425#endif
Linus Walleijef5cca92006-05-05 07:19:08 +00001426 if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001427 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001428 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001429 }
Linus Walleijc6210fb2006-05-08 11:11:41 +00001430 ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber;
Linus Walleijd6a49972006-05-02 08:24:54 +00001431 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001432 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001433}
1434
Linus Walleijd6a49972006-05-02 08:24:54 +00001435static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001436{
Linus Walleij58b62792007-01-07 12:38:59 +00001437 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001438 int ret;
1439
1440 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001441 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001442 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001443 if (ret<0) {
1444 perror ("inep: usb_get_endpoint_status()");
1445 } else if (status) {
1446 printf("Clearing stall on IN endpoint\n");
1447 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1448 if (ret<0) {
1449 perror ("usb_clear_stall_feature()");
1450 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001451 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001452
1453 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001454 status=0;
1455 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1456 if (ret<0) {
1457 perror("outep: usb_get_endpoint_status()");
1458 } else if (status) {
1459 printf("Clearing stall on OUT endpoint\n");
1460 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1461 if (ret<0) {
1462 perror("usb_clear_stall_feature()");
1463 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001464 }
Linus Walleij58b62792007-01-07 12:38:59 +00001465
1466 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1467}
1468
1469static void clear_halt(PTP_USB* ptp_usb)
1470{
1471 int ret;
1472
1473 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1474 if (ret<0) {
1475 perror("usb_clear_halt() on IN endpoint");
1476 }
1477 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1478 if (ret<0) {
1479 perror("usb_clear_halt() on OUT endpoint");
1480 }
1481 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1482 if (ret<0) {
1483 perror("usb_clear_halt() on INTERRUPT endpoint");
1484 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001485}
1486
Linus Walleijd6a49972006-05-02 08:24:54 +00001487static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001488{
Linus Walleij58b62792007-01-07 12:38:59 +00001489 // Clear any stalled endpoints
Linus Walleijd6a49972006-05-02 08:24:54 +00001490 clear_stall(ptp_usb);
Linus Walleij58b62792007-01-07 12:38:59 +00001491 // Clear halts on any endpoints
1492 clear_halt(ptp_usb);
Linus Walleijd6a49972006-05-02 08:24:54 +00001493 // Added to clear some stuff on the OUT endpoint
1494 // TODO: is this good on the Mac too?
1495 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1496 usb_release_interface(ptp_usb->handle, interfaceNumber);
1497 // Brutally reset device
1498 // TODO: is this good on the Mac too?
1499 usb_reset(ptp_usb->handle);
1500 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001501}
1502
Linus Walleij45a86372007-03-07 09:36:19 +00001503static LIBMTP_error_number_t prime_device_memory(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001504{
Linus Walleij45a86372007-03-07 09:36:19 +00001505 mtpdevice_list_t *tmplist = devlist;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001506
Linus Walleij45a86372007-03-07 09:36:19 +00001507 while (tmplist != NULL) {
tedbullock0f033cb2007-02-14 20:56:54 +00001508 /* Allocate a parameter box */
Linus Walleij45a86372007-03-07 09:36:19 +00001509 tmplist->params = (PTPParams *) malloc(sizeof(PTPParams));
1510 tmplist->ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleijbfcb7922007-03-06 20:14:04 +00001511
tedbullock0f033cb2007-02-14 20:56:54 +00001512 /* Check for allocation Error */
Linus Walleij45a86372007-03-07 09:36:19 +00001513 if(tmplist->params == NULL || tmplist->ptp_usb == NULL) {
Linus Walleijbfcb7922007-03-06 20:14:04 +00001514 /* Error and deallocation of memory will be handled by caller. */
tedbullock0f033cb2007-02-14 20:56:54 +00001515 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1516 }
1517
tedbullock69a445b2007-02-15 07:41:04 +00001518 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleij45a86372007-03-07 09:36:19 +00001519 memset(tmplist->params, 0, sizeof(PTPParams));
1520 memset(tmplist->ptp_usb, 0, sizeof(PTP_USB));
1521 tmplist = tmplist->next;
tedbullock0f033cb2007-02-14 20:56:54 +00001522 }
tedbullock0f033cb2007-02-14 20:56:54 +00001523 return LIBMTP_ERROR_NONE;
1524}
1525
Linus Walleij45a86372007-03-07 09:36:19 +00001526static void assign_known_device_flags(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001527{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001528 int i;
Linus Walleij27c50c52007-03-06 20:34:48 +00001529 mtpdevice_list_t *tmplist = devlist;
1530 uint8_t current_device = 0;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001531
tedbullock0f033cb2007-02-14 20:56:54 +00001532 /* Search through known device list and set correct device flags */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001533 while (tmplist != NULL) {
1534 int device_known = 0;
1535
1536 for(i = 0; i < mtp_device_table_size; i++) {
1537 if(tmplist->libusb_device->descriptor.idVendor == mtp_device_table[i].vendor_id &&
1538 tmplist->libusb_device->descriptor.idProduct == mtp_device_table[i].product_id) {
1539 /* This device is known, assign the correct device flags */
1540 /* Note that ptp_usb[current_device] could potentially be NULL */
Linus Walleij45a86372007-03-07 09:36:19 +00001541 if(tmplist->ptp_usb != NULL) {
1542 tmplist->ptp_usb->device_flags = mtp_device_table[i].device_flags;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001543
1544 /*
1545 * TODO:
1546 * Preferable to not do this with #ifdef ENABLE_USB_BULK_DEBUG but there is
1547 * currently no other compile time debug option
1548 */
1549
1550 device_known = 1;
tedbullock65a05772007-03-03 22:56:56 +00001551#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001552 /* This device is known to the developers */
1553 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is a %s.\n",
1554 current_device + 1,
1555 tmplist->libusb_device->descriptor.idVendor,
1556 tmplist->libusb_device->descriptor.idProduct,
1557 mtp_device_table[i].name);
tedbullock65a05772007-03-03 22:56:56 +00001558#endif
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001559 }
1560 break;
1561 }
tedbullock0f033cb2007-02-14 20:56:54 +00001562 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001563 if (!device_known) {
1564 /* This device is unknown to the developers */
1565 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
1566 current_device + 1,
1567 tmplist->libusb_device->descriptor.idVendor,
1568 tmplist->libusb_device->descriptor.idProduct);
1569 fprintf(stderr, "Please report this VID/PID and the device model to the "
1570 "libmtp development team\n");
1571 }
1572 tmplist = tmplist->next;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001573 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001574 }
tedbullock0f033cb2007-02-14 20:56:54 +00001575}
1576
tedbullock0f033cb2007-02-14 20:56:54 +00001577
Linus Walleij45a86372007-03-07 09:36:19 +00001578static LIBMTP_error_number_t configure_usb_devices(mtpdevice_list_t *devicelist)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001579{
1580 mtpdevice_list_t *tmplist = devicelist;
1581 uint16_t ret = 0;
1582 uint8_t current_device = 0;
1583
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001584 while (tmplist != NULL) {
Linus Walleij45a86372007-03-07 09:36:19 +00001585 /* This is erroneous, there must be a PTP_USB instance that we can initialize. */
1586 if(tmplist->ptp_usb == NULL) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001587 return LIBMTP_ERROR_MEMORY_ALLOCATION;
tedbullock0f033cb2007-02-14 20:56:54 +00001588 }
1589
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001590 /* TODO: Will this always be little endian? */
Linus Walleij45a86372007-03-07 09:36:19 +00001591 tmplist->params->byteorder = PTP_DL_LE;
1592 tmplist->params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8");
1593 tmplist->params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE");
tedbullock0f033cb2007-02-14 20:56:54 +00001594
Linus Walleij45a86372007-03-07 09:36:19 +00001595 if(tmplist->params->cd_locale_to_ucs2 == (iconv_t) -1 ||
1596 tmplist->params->cd_ucs2_to_locale == (iconv_t) -1) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001597 fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
1598 "Too old stdlibc, glibc and libiconv?\n");
Linus Walleij35c0ef12007-03-06 20:49:09 +00001599 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001600 }
1601
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001602 // ep = device->config->interface->altsetting->endpoint;
1603 // no_of_ep = device->config->interface->altsetting->bNumEndpoints;
tedbullock0f033cb2007-02-14 20:56:54 +00001604
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001605 /* Assign endpoints to usbinfo... */
1606 find_endpoints(tmplist->libusb_device,
Linus Walleij45a86372007-03-07 09:36:19 +00001607 &tmplist->ptp_usb->inep,
1608 &tmplist->ptp_usb->inep_maxpacket,
1609 &tmplist->ptp_usb->outep,
1610 &tmplist->ptp_usb->outep_maxpacket,
1611 &tmplist->ptp_usb->intep);
tedbullock0f033cb2007-02-14 20:56:54 +00001612
Linus Walleij45a86372007-03-07 09:36:19 +00001613 /* Attempt to initialize this device */
1614 if (init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) < 0) {
1615 fprintf(stderr, "LIBMTP PANIC: Unable to initialize device %d\n", current_device+1);
1616 // FIXME: perhaps use "continue" to keep trying the other devices.
Linus Walleij35c0ef12007-03-06 20:49:09 +00001617 return LIBMTP_ERROR_CONNECTING;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001618 }
1619
1620 /*
1621 * This works in situations where previous bad applications
1622 * have not used LIBMTP_Release_Device on exit
1623 */
Linus Walleij45a86372007-03-07 09:36:19 +00001624 if ((ret = ptp_opensession(tmplist->params, 1)) == PTP_ERROR_IO) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001625 fprintf(stderr, "PTP_ERROR_IO: Trying again after resetting USB\n");
Linus Walleij45a86372007-03-07 09:36:19 +00001626 close_usb(tmplist->ptp_usb, tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001627
Linus Walleij45a86372007-03-07 09:36:19 +00001628 if(init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) <0) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001629 fprintf(stderr, "LIBMTP PANIC: Could not open session on device %d\n", current_device+1);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001630 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001631 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001632
1633 /* Device has been reset, try again */
Linus Walleij45a86372007-03-07 09:36:19 +00001634 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001635 }
1636
1637 /* Was the transaction id invalid? Try again */
1638 if (ret == PTP_RC_InvalidTransactionID) {
Linus Walleij45a86372007-03-07 09:36:19 +00001639 fprintf(stderr, "LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
1640 tmplist->params->transaction_id += 10;
1641 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001642 }
1643
1644 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
Linus Walleij45a86372007-03-07 09:36:19 +00001645 fprintf(stderr, "LIBMTP PANIC: Could not open session! "
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001646 "(Return code %d)\n Try to reset the device.\n",
1647 ret);
Linus Walleij45a86372007-03-07 09:36:19 +00001648 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001649 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001650 return LIBMTP_ERROR_CONNECTING;
1651 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001652
1653 /* It is permissible to call this before opening the session */
Linus Walleij45a86372007-03-07 09:36:19 +00001654 if (ptp_getdeviceinfo(tmplist->params,
1655 &tmplist->params->deviceinfo) != PTP_RC_OK) {
1656 fprintf(stderr, "LIBMTP PANIC: Could not get device info!\n");
1657 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001658 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001659 return LIBMTP_ERROR_CONNECTING;
1660 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001661
1662 tmplist = tmplist->next;
1663 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001664 }
1665
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001666 /* Exit with the nice list */
1667 return LIBMTP_ERROR_NONE;
tedbullock0f033cb2007-02-14 20:56:54 +00001668}
1669
Linus Walleij550d6d52007-01-24 14:32:51 +00001670/**
1671 * This function scans through the results of the get_mtp_usb_device_list
1672 * function and attempts to connect to those devices listed using the
1673 * mtp_device_table at the top of the file. Returns a LIBMTP_error_number_t.
1674 *
Linus Walleij45a86372007-03-07 09:36:19 +00001675 * @param devlist a list of devices with primed PTP_USB and params structs.
Linus Walleij550d6d52007-01-24 14:32:51 +00001676 * @return Error Codes as per the type definition
1677 */
Linus Walleij45a86372007-03-07 09:36:19 +00001678LIBMTP_error_number_t find_usb_devices(mtpdevice_list_t **devlist)
Linus Walleij550d6d52007-01-24 14:32:51 +00001679{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001680 mtpdevice_list_t *mtp_device_list = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001681 LIBMTP_error_number_t ret;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001682
Linus Walleija4942fc2007-03-12 19:23:21 +00001683 /*
1684 * Recover list of attached USB devices that match MTP criteria, i.e.
1685 * it either has an MTP device descriptor or it is in the known
1686 * devices list.
1687 */
Linus Walleij45a86372007-03-07 09:36:19 +00001688 ret = get_mtp_usb_device_list (&mtp_device_list);
Linus Walleijac061862007-03-07 08:28:33 +00001689 if (ret != LIBMTP_ERROR_NONE) {
1690 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001691 }
1692
Linus Walleij45a86372007-03-07 09:36:19 +00001693 // Then prime them
1694 ret = prime_device_memory(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001695 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001696 fprintf(stderr, "LIBMTP PANIC: prime_device_memory() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001697 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001698 }
tedbullock0f033cb2007-02-14 20:56:54 +00001699
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001700 /* Assign specific device flags and detect unknown devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001701 assign_known_device_flags(mtp_device_list);
Linus Walleij550d6d52007-01-24 14:32:51 +00001702
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001703 /* Configure the devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001704 ret = configure_usb_devices(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001705 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001706 fprintf(stderr, "LIBMTP PANIC: configure_usb_devices() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001707 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001708 }
Linus Walleij550d6d52007-01-24 14:32:51 +00001709
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001710 /* Configure interface numbers */
Linus Walleij550d6d52007-01-24 14:32:51 +00001711 {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001712 mtpdevice_list_t *tmplist = mtp_device_list;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001713
Linus Walleij45a86372007-03-07 09:36:19 +00001714 while (tmplist != NULL) {
1715 tmplist->interface_number = tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001716 tmplist = tmplist->next;
Linus Walleij550d6d52007-01-24 14:32:51 +00001717 }
1718 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001719
Linus Walleij45a86372007-03-07 09:36:19 +00001720 /* we're connected to all devices, return the list and OK */
1721 *devlist = mtp_device_list;
1722 return LIBMTP_ERROR_NONE;
1723
1724 find_usb_devices_error_exit:
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001725 if(mtp_device_list != NULL) {
1726 free_mtpdevice_list(mtp_device_list);
1727 mtp_device_list = NULL;
Linus Walleij550d6d52007-01-24 14:32:51 +00001728 }
Linus Walleij45a86372007-03-07 09:36:19 +00001729 *devlist = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001730 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001731}
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001732
Linus Walleijd6a49972006-05-02 08:24:54 +00001733static 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 +00001734{
Linus Walleijd6a49972006-05-02 08:24:54 +00001735 int i,n;
1736 struct usb_endpoint_descriptor *ep;
1737
1738 ep = dev->config->interface->altsetting->endpoint;
1739 n=dev->config->interface->altsetting->bNumEndpoints;
1740
1741 for (i=0;i<n;i++) {
1742 if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1743 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1744 USB_ENDPOINT_DIR_MASK)
1745 {
1746 *inep=ep[i].bEndpointAddress;
1747 *inep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001748 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001749 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1750 {
1751 *outep=ep[i].bEndpointAddress;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001752 *outep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijd6a49972006-05-02 08:24:54 +00001753 }
1754 } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1755 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1756 USB_ENDPOINT_DIR_MASK)
1757 {
1758 *intep=ep[i].bEndpointAddress;
1759 }
1760 }
1761 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001762}
1763
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001764void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber)
1765{
Linus Walleijd6a49972006-05-02 08:24:54 +00001766 if (ptp_closesession(params)!=PTP_RC_OK)
1767 fprintf(stderr,"ERROR: Could not close session!\n");
1768 close_usb(ptp_usb, interfaceNumber);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001769}
1770
Linus Walleijd6a49972006-05-02 08:24:54 +00001771static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001772{
Linus Walleijd6a49972006-05-02 08:24:54 +00001773
1774 return (usb_control_msg(ptp_usb->handle,
1775 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
1776 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001777}
1778
Linus Walleijd6a49972006-05-02 08:24:54 +00001779static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001780{
Linus Walleijd6a49972006-05-02 08:24:54 +00001781 return (usb_control_msg(ptp_usb->handle,
1782 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
1783 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001784}