blob: 440bd77ca22c7d00a9af02016a36f9d260a6e770 [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 Walleij3d2c7842006-08-18 09:35:08 +0000122 // From a rouge .INF file
Richard Low5224b1c2007-03-24 12:56:34 +0000123 { "Samsung YH-999 Portable Media Center", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE },
Linus Walleij8556f522007-02-14 09:47:50 +0000124 // From Lionel Bouton
Richard Low5224b1c2007-03-24 12:56:34 +0000125 { "Samsung X830 Mobile Phone", 0x04e8, 0x6702, DEVICE_FLAG_NONE },
Richard Lowbf2675c2007-03-18 18:20:26 +0000126 // From XNJB user
Richard Low5224b1c2007-03-24 12:56:34 +0000127 { "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
Richard Low1f95cf92007-03-24 12:50:59 +0000155 { "Philips 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 Walleij8282c972007-04-10 19:59:16 +0000255 // Reported by Etienne Chauchot <chauchot.etienne@free.fr>
256 { "Archos 504 (MTP mode)", 0x0e79, 0x1307, DEVICE_FLAG_NONE },
Linus Walleije5430902006-10-08 14:46:03 +0000257
258 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000259 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000260 * This unit is falsely detected as USB mass storage in Linux
Linus Walleijcc4dd482006-11-25 21:58:30 +0000261 * prior to kernel 2.6.19 (fixed by patch from Alan Stern)
262 * so on older kernels special care is needed to remove the
263 * USB mass storage driver that erroneously binds to the device
264 * interface.
Linus Walleije5430902006-10-08 14:46:03 +0000265 */
Richard Lowc7d48d22006-11-29 18:24:55 +0000266 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER},
267
268 /*
269 * Microsoft
270 */
271 // Reported by Farooq Zaman
272 { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE },
273
274 /*
275 * Sirius
276 */
Linus Walleijb01d18b2006-12-13 12:49:15 +0000277 { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE },
278
279 /*
280 * Canon
281 * This is actually a camera, but it has a Microsoft device descriptor
282 * and reports itself as supporting the MTP extension.
283 */
Linus Walleijc1b49402007-02-07 08:00:38 +0000284 {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE },
285
286 /*
287 * Nokia
288 */
Linus Walleijde7ad542007-02-14 09:32:26 +0000289 {"Nokia Mobile Phones (MTP mode)", 0x0421, 0x04e1, DEVICE_FLAG_NONE },
290
291 /*
292 * LOGIK
293 * Sold in the UK, seem to be manufactured by CCTech in China.
294 */
Linus Walleijd2ff9e32007-03-16 15:27:45 +0000295 {"Logik LOG DAX MP3 and DAB Player", 0x13d1, 0x7002, DEVICE_FLAG_NONE },
296
297 /*
298 * Other strange stuff.
299 */
300 {"Isabella's prototype", 0x0b20, 0xddee, DEVICE_FLAG_NONE }
Linus Walleija5483642006-03-09 09:20:38 +0000301};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000302static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000303
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000304int ptpcam_usb_timeout = USB_TIMEOUT;
305
Linus Walleijd6a49972006-05-02 08:24:54 +0000306// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000307static struct usb_bus* init_usb();
Linus Walleijd6a49972006-05-02 08:24:54 +0000308static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);
Linus Walleijd6a49972006-05-02 08:24:54 +0000309static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);
310static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000311static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000312static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000313static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000314static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
315static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000316
Linus Walleij552ba322007-01-22 11:49:59 +0000317/**
318 * Get a list of the supported USB devices.
319 *
320 * The developers depend on users of this library to constantly
321 * add in to the list of supported devices. What we need is the
322 * device name, USB Vendor ID (VID) and USB Product ID (PID).
323 * put this into a bug ticket at the project homepage, please.
324 * The VID/PID is used to let e.g. udev lift the device to
325 * console userspace access when it's plugged in.
326 *
327 * @param devices a pointer to a pointer that will hold a device
328 * list after the call to this function, if it was
329 * successful.
330 * @param numdevs a pointer to an integer that will hold the number
331 * of devices in the device list if the call was successful.
332 * @return 0 if the list was successfull retrieved, any other
333 * value means failure.
334 */
335int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000336{
337 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
338 *numdevs = mtp_device_table_size;
339 return 0;
340}
341
Linus Walleij552ba322007-01-22 11:49:59 +0000342
Linus Walleij1fd2d272006-05-08 09:22:01 +0000343static struct usb_bus* init_usb()
344{
345 usb_init();
346 usb_find_busses();
347 usb_find_devices();
348 return (usb_get_busses());
349}
350
351/**
tedbullock0f033cb2007-02-14 20:56:54 +0000352 * Small recursive function to append a new usb_device to the linked list of
353 * USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000354 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000355 * properties.
356 * @param next New USB MTP device to be added to list
357 * @return nothing
358 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000359static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
360 struct usb_device *newdevice)
tedbullock0f033cb2007-02-14 20:56:54 +0000361{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000362 mtpdevice_list_t *new_list_entry;
tedbullock0f033cb2007-02-14 20:56:54 +0000363
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000364 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
365 if (new_list_entry == NULL) {
366 return NULL;
367 }
368 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
369 new_list_entry->libusb_device = newdevice;
370 new_list_entry->next = NULL;
371
372 if (devlist == NULL) {
373 return new_list_entry;
374 } else {
375 mtpdevice_list_t *tmp = devlist;
376 while (tmp->next != NULL) {
377 tmp = tmp->next;
378 }
379 tmp->next = new_list_entry;
380 }
381 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000382}
383
384/**
385 * Small recursive function to free dynamic memory allocated to the linked list
386 * of USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000387 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000388 * properties.
389 * @return nothing
390 */
Linus Walleij45a86372007-03-07 09:36:19 +0000391void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000392{
Linus Walleijac061862007-03-07 08:28:33 +0000393 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000394
395 if (devlist == NULL)
396 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000397 while (tmplist != NULL) {
398 mtpdevice_list_t *tmp = tmplist;
399 tmplist = tmplist->next;
Linus Walleij45a86372007-03-07 09:36:19 +0000400 // Do not free() the fields (ptp_usb, parms)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000401 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000402 }
tedbullock0f033cb2007-02-14 20:56:54 +0000403 return;
404}
405
406/**
Linus Walleija4942fc2007-03-12 19:23:21 +0000407 * This checks if a device has an MTP descriptor.
408 * @param dev a device struct from libusb.
409 * @return 1 if the device is MTP compliant, 0 if not.
410 */
411static int device_has_descriptor(struct usb_device *dev)
412{
413 usb_dev_handle *devh;
414 unsigned char buf[1024], cmd;
415 int ret;
416
417 /* Don't examine hubs (no point in that) */
418 if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) {
419 return 0;
420 }
421
422 /* Attempt to open Device on this port */
423 devh = usb_open(dev);
424 if (devh == NULL) {
425 /* Could not open this device */
426 return 0;
427 }
428
429 /* Read the special descriptor */
430 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
431
432 /* Check if descriptor length is at least 10 bytes */
433 if (ret < 10) {
434 usb_close(devh);
435 return 0;
436 }
437
438 /* Check if this device has a Microsoft Descriptor */
439 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
440 (buf[6] == 'F') && (buf[8] == 'T'))) {
441 usb_close(devh);
442 return 0;
443 }
444
445 /* Check if device responds to control message 1 or if there is an error */
446 cmd = buf[16];
447 ret = usb_control_msg (devh,
448 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
449 cmd,
450 0,
451 4,
452 (char *) buf,
453 sizeof(buf),
454 1000);
455
456 /* If this is true, the device either isn't MTP or there was an error */
457 if (ret <= 0x15) {
458 /* TODO: If there was an error, flag it and let the user know somehow */
459 /* if(ret == -1) {} */
460 usb_close(devh);
461 return 0;
462 }
463
464 /* Check if device is MTP or if it is something like a USB Mass Storage
465 device with Janus DRM support */
466 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
467 usb_close(devh);
468 return 0;
469 }
470
471 /* After this point we are probably dealing with an MTP device */
472
473 /* Check if device responds to control message 2 or if there is an error*/
474 ret = usb_control_msg (devh,
475 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
476 cmd,
477 0,
478 5,
479 (char *) buf,
480 sizeof(buf),
481 1000);
482
483 /* If this is true, the device errored against control message 2 */
484 if (ret == -1) {
485 /* TODO: Implement callback function to let managing program know there
486 was a problem, along with description of the problem */
487 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
488 "ProductID:%04x encountered an error responding to "
489 "control message 2.\n"
490 "Problems may arrise but continuing\n",
491 dev->descriptor.idVendor, dev->descriptor.idProduct);
492 } else if (ret <= 0x15) {
493 /* TODO: Implement callback function to let managing program know there
494 was a problem, along with description of the problem */
495 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
496 "ProductID:%04x responded to control message 2 with a "
497 "response that was too short. Problems may arrise but "
498 "continuing\n",
499 dev->descriptor.idVendor, dev->descriptor.idProduct);
500 } else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
501 /* TODO: Implement callback function to let managing program know there
502 was a problem, along with description of the problem */
503 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
504 "ProductID:%04x encountered an error responding to "
505 "control message 2\n"
506 "Problems may arrise but continuing\n",
507 dev->descriptor.idVendor, dev->descriptor.idProduct);
508 }
509
510 /* Close the USB device handle */
511 usb_close(devh);
512 return 1;
513}
514
515/**
516 * This function scans through the connected usb devices on a machine and
517 * if they match known Vendor and Product identifiers appends them to the
518 * dynamic array mtp_device_list. Be sure to call
519 * <code>free(mtp_device_list)</code> when you are done with it, assuming it
520 * is not NULL.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000521 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000522 * properties (if this list is not empty, new entries will be appended
523 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000524 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
525 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no devices have
526 * been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000527 */
Linus Walleij45a86372007-03-07 09:36:19 +0000528static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000529{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000530 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000531
532 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000533 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000534 for (; dev != NULL; dev = dev->next) {
Linus Walleija4942fc2007-03-12 19:23:21 +0000535 if (device_has_descriptor(dev)) {
536 /* Append this usb device to the MTP USB Device List */
537 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
538 } else {
539 /* Check if it's in the known devices list then */
540 int i;
541
542 for(i = 0; i < mtp_device_table_size; i++) {
543 if(dev->descriptor.bDeviceClass != USB_CLASS_HUB &&
544 dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
545 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
546 /* Append this usb device to the MTP device list */
547 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
548 break;
549 }
550 }
Linus Walleij35c0ef12007-03-06 20:49:09 +0000551 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000552 }
553 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000554
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000555 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000556 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000557 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000558 }
559 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000560}
561
562/**
Linus Walleij1fd2d272006-05-08 09:22:01 +0000563 * Detect the MTP device descriptor and return the VID and PID
564 * of the first device found. This is a very low-level function
565 * which is intended for use with <b>udev</b> or other hotplug
566 * mechanisms. The idea is that a script may want to know if the
567 * just plugged-in device was an MTP device or not.
tedbullock2b45f2e2007-02-23 20:16:37 +0000568 *
Linus Walleij1fd2d272006-05-08 09:22:01 +0000569 * @param vid the Vendor ID (VID) of the first device found.
570 * @param pid the Product ID (PID) of the first device found.
571 * @return the number of detected devices or -1 if the call
572 * was unsuccessful.
573 */
574int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
575{
Linus Walleijb6b69132007-03-07 09:45:05 +0000576 mtpdevice_list_t *devlist;
577 LIBMTP_error_number_t ret;
578
579 ret = get_mtp_usb_device_list(&devlist);
580 if (ret != LIBMTP_ERROR_NONE) {
581 *vid = *pid = 0;
582 return -1;
583 }
584 *vid = devlist->libusb_device->descriptor.idVendor;
585 *pid = devlist->libusb_device->descriptor.idProduct;
586 free_mtpdevice_list(devlist);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000587 return 1;
588}
589
Linus Walleijc6210fb2006-05-08 11:11:41 +0000590/**
591 * This routine just dumps out low-level
592 * USB information about the current device.
593 * @param ptp_usb the USB device to get information from.
594 */
595void dump_usbinfo(PTP_USB *ptp_usb)
596{
597 int res;
598 struct usb_device *dev;
599
600#ifdef LIBUSB_HAS_GET_DRIVER_NP
601 char devname[0x10];
602
603 devname[0] = '\0';
604 res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));
605 if (devname[0] != '\0') {
606 printf(" Using kernel interface \"%s\"\n", devname);
607 }
608#endif
609 dev = usb_device(ptp_usb->handle);
610 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
611 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
612 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
613 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000614 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
615 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000616 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
617 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000618 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000619 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
620}
621
Linus Walleija0323272007-01-07 12:21:30 +0000622static void
623ptp_debug (PTPParams *params, const char *format, ...)
624{
625 va_list args;
626
627 va_start (args, format);
628 if (params->debug_func!=NULL)
629 params->debug_func (params->data, format, args);
630 else
631 {
632 vfprintf (stderr, format, args);
633 fprintf (stderr,"\n");
634 fflush (stderr);
635 }
636 va_end (args);
637}
638
639static void
640ptp_error (PTPParams *params, const char *format, ...)
641{
642 va_list args;
643
644 va_start (args, format);
645 if (params->error_func!=NULL)
646 params->error_func (params->data, format, args);
647 else
648 {
649 vfprintf (stderr, format, args);
650 fprintf (stderr,"\n");
651 fflush (stderr);
652 }
653 va_end (args);
654}
655
Linus Walleijc6210fb2006-05-08 11:11:41 +0000656
Linus Walleij99ed83c2007-01-02 18:46:02 +0000657/*
658 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000659 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000660 * Much reading packet logs and having fun with trials and errors
661 * reveals that WMP / Windows is probably using an algorithm like this
662 * for large transfers:
663 *
664 * 1. Send the command (0x0c bytes) if headers are split, else, send
665 * command plus sizeof(endpoint) - 0x0c bytes.
666 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
667 * split headers. Else goto 3.
668 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
669 * We call 0x10000 CONTEXT_BLOCK_SIZE.
670 * 4. Send remaining bytes MOD sizeof(endpoint)
671 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
672 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000673 *
674 * Further there is some special quirks to handle zero reads from the
675 * device, since some devices can't do them at all due to shortcomings
676 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000677 */
Richard Low4fd59d62007-03-03 16:34:37 +0000678#define CONTEXT_BLOCK_SIZE_1 0x3e00
679#define CONTEXT_BLOCK_SIZE_2 0x200
680#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000681static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000682ptp_read_func (
683 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000684 unsigned long *readbytes,
685 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000686) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000687 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000688 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000689 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000690 unsigned long curread = 0;
691 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000692 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000693 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000694
695 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000696 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000697 while (curread < size) {
Richard Low4fd59d62007-03-03 16:34:37 +0000698
699#ifdef ENABLE_USB_BULK_DEBUG
700 printf("Remaining size to read: 0x%04x bytes\n", size - curread);
701#endif
702 // check equal to condition here
703 if (size - curread < CONTEXT_BLOCK_SIZE)
704 {
705 // this is the last packet
706 toread = size - curread;
707 // this is equivalent to zero read for these devices
Linus Walleij049f50c2007-03-03 20:30:43 +0000708 if (readzero && ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000709 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000710 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000711 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000712 }
Richard Low4fd59d62007-03-03 16:34:37 +0000713 else if (curread == 0)
714 // we are first packet, but not last packet
715 toread = CONTEXT_BLOCK_SIZE_1;
716 else if (toread == CONTEXT_BLOCK_SIZE_1)
717 toread = CONTEXT_BLOCK_SIZE_2;
718 else if (toread == CONTEXT_BLOCK_SIZE_2)
719 toread = CONTEXT_BLOCK_SIZE_1;
720 else
Linus Walleij4cec9872007-03-03 21:00:53 +0000721 printf("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
722 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000723
Richard Lowd284f072007-02-17 08:52:41 +0000724#ifdef ENABLE_USB_BULK_DEBUG
725 printf("Reading in 0x%04x bytes\n", toread);
726#endif
Linus Walleij784ac3f2006-12-29 10:36:51 +0000727 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout);
Richard Lowd284f072007-02-17 08:52:41 +0000728#ifdef ENABLE_USB_BULK_DEBUG
729 printf("Result of read: 0x%04x\n", result);
730#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000731
Linus Walleijc49c6372007-01-09 00:18:51 +0000732 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000733 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000734 }
Linus Walleije78de6d2006-10-31 14:46:36 +0000735#ifdef ENABLE_USB_BULK_DEBUG
736 printf("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000737 if (result == 0)
738 printf("Zero Read\n");
739 else
740 data_dump_ascii (stdout,bytes,result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000741#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000742
743 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000744 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000745 {
746#ifdef ENABLE_USB_BULK_DEBUG
747 printf("<==USB IN\nDiscarding extra byte\n");
748#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000749 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000750 }
Richard Low4fd59d62007-03-03 16:34:37 +0000751
Linus Walleijc49c6372007-01-09 00:18:51 +0000752 handler->putfunc(NULL, handler->private, result, bytes, &written);
Richard Lowbf2675c2007-03-18 18:20:26 +0000753
Linus Walleijc49c6372007-01-09 00:18:51 +0000754 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000755 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000756
757 // Increase counters, call callback
758 if (ptp_usb->callback_active) {
759 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
760 // send last update and disable callback.
761 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
762 ptp_usb->callback_active = 0;
763 }
764 if (ptp_usb->current_transfer_callback != NULL) {
765 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
766 ptp_usb->current_transfer_total,
767 ptp_usb->current_transfer_callback_data);
768 }
769 }
770
Linus Walleijd6a49972006-05-02 08:24:54 +0000771 if (result < toread) /* short reads are common */
772 break;
773 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000774 if (readbytes) *readbytes = curread;
775 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000776
Richard Low4df32f82007-01-11 20:04:39 +0000777 // there might be a zero packet waiting for us...
Linus Walleij049f50c2007-03-03 20:30:43 +0000778 if (readzero &&
779 !(ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS) &&
780 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000781 char temp;
782 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000783
Richard Low4df32f82007-01-11 20:04:39 +0000784#ifdef ENABLE_USB_BULK_DEBUG
785 printf("<==USB IN\n");
786 printf("Zero Read\n");
787#endif
788 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptpcam_usb_timeout);
789 if (zeroresult != 0)
790 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
791 }
792
Linus Walleijd6a49972006-05-02 08:24:54 +0000793 if (result > 0) {
Linus Walleijd6a49972006-05-02 08:24:54 +0000794 return (PTP_RC_OK);
795 } else {
796 return PTP_ERROR_IO;
797 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000798}
799
Linus Walleijb02a0662006-04-25 08:05:09 +0000800static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000801ptp_write_func (
802 unsigned long size,
803 PTPDataHandler *handler,
804 void *data,
805 unsigned long *written
806) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000807 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000808 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000809 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000810 unsigned long curwrite = 0;
811 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000812
813 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000814 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000815 if (!bytes) {
816 return PTP_ERROR_IO;
817 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000818 while (curwrite < size) {
819 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000820 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000821 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000822 } else {
823 // This magic makes packets the same size that WMP send them.
824 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000825 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000826 }
827 }
828 handler->getfunc(NULL, handler->private,towrite,bytes,&towrite);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000829 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000830#ifdef ENABLE_USB_BULK_DEBUG
831 printf("USB OUT==>\n");
Richard Lowedd61832006-12-30 12:38:18 +0000832 data_dump_ascii (stdout,bytes,towrite,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000833#endif
Linus Walleijc49c6372007-01-09 00:18:51 +0000834 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000835 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000836 }
837 // Increase counters
838 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000839 curwrite += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000840
841 // call callback
842 if (ptp_usb->callback_active) {
843 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
844 // send last update and disable callback.
845 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
846 ptp_usb->callback_active = 0;
847 }
848 if (ptp_usb->current_transfer_callback != NULL) {
849 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
850 ptp_usb->current_transfer_total,
851 ptp_usb->current_transfer_callback_data);
852 }
853 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000854 if (result < towrite) /* short writes happen */
855 break;
856 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000857 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000858 if (written) {
859 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000860 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000861
Linus Walleijc49c6372007-01-09 00:18:51 +0000862
Richard Lowef05b892007-01-03 21:18:56 +0000863 // If this is the last transfer send a zero write if required
864 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000865 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
Richard Low021421e2007-01-01 18:08:57 +0000866#ifdef ENABLE_USB_BULK_DEBUG
867 printf("USB OUT==>\n");
868 printf("Zero Write\n");
869#endif
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000870 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000871 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000872 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000873
Linus Walleijd6a49972006-05-02 08:24:54 +0000874 if (result < 0)
875 return PTP_ERROR_IO;
876 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000877}
878
Linus Walleija0323272007-01-07 12:21:30 +0000879/* memory data get/put handler */
880typedef struct {
881 unsigned char *data;
882 unsigned long size, curoff;
883} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000884
Linus Walleija0323272007-01-07 12:21:30 +0000885static uint16_t
886memory_getfunc(PTPParams* params, void* private,
887 unsigned long wantlen, unsigned char *data,
888 unsigned long *gotlen
889) {
890 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
891 unsigned long tocopy = wantlen;
892
893 if (priv->curoff + tocopy > priv->size)
894 tocopy = priv->size - priv->curoff;
895 memcpy (data, priv->data + priv->curoff, tocopy);
896 priv->curoff += tocopy;
897 *gotlen = tocopy;
898 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000899}
900
Linus Walleija0323272007-01-07 12:21:30 +0000901static uint16_t
902memory_putfunc(PTPParams* params, void* private,
903 unsigned long sendlen, unsigned char *data,
904 unsigned long *putlen
905) {
906 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
907
908 if (priv->curoff + sendlen > priv->size) {
909 priv->data = realloc (priv->data, priv->curoff+sendlen);
910 priv->size = priv->curoff + sendlen;
911 }
912 memcpy (priv->data + priv->curoff, data, sendlen);
913 priv->curoff += sendlen;
914 *putlen = sendlen;
915 return PTP_RC_OK;
916}
917
918/* init private struct for receiving data. */
919static uint16_t
920ptp_init_recv_memory_handler(PTPDataHandler *handler) {
921 PTPMemHandlerPrivate* priv;
922 priv = malloc (sizeof(PTPMemHandlerPrivate));
923 handler->private = priv;
924 handler->getfunc = memory_getfunc;
925 handler->putfunc = memory_putfunc;
926 priv->data = NULL;
927 priv->size = 0;
928 priv->curoff = 0;
929 return PTP_RC_OK;
930}
931
932/* init private struct and put data in for sending data.
933 * data is still owned by caller.
934 */
935static uint16_t
936ptp_init_send_memory_handler(PTPDataHandler *handler,
937 unsigned char *data, unsigned long len
938) {
939 PTPMemHandlerPrivate* priv;
940 priv = malloc (sizeof(PTPMemHandlerPrivate));
941 if (!priv)
942 return PTP_RC_GeneralError;
943 handler->private = priv;
944 handler->getfunc = memory_getfunc;
945 handler->putfunc = memory_putfunc;
946 priv->data = data;
947 priv->size = len;
948 priv->curoff = 0;
949 return PTP_RC_OK;
950}
951
952/* free private struct + data */
953static uint16_t
954ptp_exit_send_memory_handler (PTPDataHandler *handler) {
955 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
956 /* data is owned by caller */
957 free (priv);
958 return PTP_RC_OK;
959}
960
961/* hand over our internal data to caller */
962static uint16_t
963ptp_exit_recv_memory_handler (PTPDataHandler *handler,
964 unsigned char **data, unsigned long *size
965) {
966 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
967 *data = priv->data;
968 *size = priv->size;
969 free (priv);
970 return PTP_RC_OK;
971}
972
973/* send / receive functions */
974
975uint16_t
976ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
977{
978 uint16_t ret;
979 PTPUSBBulkContainer usbreq;
980 PTPDataHandler memhandler;
981 unsigned long written, towrite;
982
983 /* build appropriate USB container */
984 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
985 (sizeof(uint32_t)*(5-req->Nparam)));
986 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
987 usbreq.code=htod16(req->Code);
988 usbreq.trans_id=htod32(req->Transaction_ID);
989 usbreq.payload.params.param1=htod32(req->Param1);
990 usbreq.payload.params.param2=htod32(req->Param2);
991 usbreq.payload.params.param3=htod32(req->Param3);
992 usbreq.payload.params.param4=htod32(req->Param4);
993 usbreq.payload.params.param5=htod32(req->Param5);
994 /* send it to responder */
995 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
996 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
997 ret=ptp_write_func(
998 towrite,
999 &memhandler,
1000 params->data,
1001 &written
1002 );
1003 ptp_exit_send_memory_handler (&memhandler);
1004 if (ret!=PTP_RC_OK) {
1005 ret = PTP_ERROR_IO;
1006/* ptp_error (params,
1007 "PTP: request code 0x%04x sending req error 0x%04x",
1008 req->Code,ret); */
1009 }
1010 if (written != towrite) {
1011 ptp_error (params,
1012 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001013 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001014 );
1015 ret = PTP_ERROR_IO;
1016 }
1017 return ret;
1018}
1019
1020uint16_t
1021ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1022 unsigned long size, PTPDataHandler *handler
1023) {
1024 uint16_t ret;
1025 int wlen, datawlen;
1026 unsigned long written;
1027 PTPUSBBulkContainer usbdata;
1028 uint32_t bytes_left_to_transfer;
1029 PTPDataHandler memhandler;
1030
1031 /* build appropriate USB container */
1032 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1033 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1034 usbdata.code = htod16(ptp->Code);
1035 usbdata.trans_id= htod32(ptp->Transaction_ID);
1036
1037 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001038 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001039
1040 if (params->split_header_data) {
1041 datawlen = 0;
1042 wlen = PTP_USB_BULK_HDR_LEN;
1043 } else {
1044 unsigned long gotlen;
1045 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001046 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001047 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
1048 ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen);
1049 if (ret != PTP_RC_OK)
1050 return ret;
1051 if (gotlen != datawlen)
1052 return PTP_RC_GeneralError;
1053 }
1054 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1055 /* send first part of data */
1056 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1057 ptp_exit_send_memory_handler (&memhandler);
1058 if (ret!=PTP_RC_OK) {
1059 ret = PTP_ERROR_IO;
1060/* ptp_error (params,
1061 "PTP: request code 0x%04x sending data error 0x%04x",
1062 ptp->Code,ret);*/
1063 return ret;
1064 }
1065 if (size <= datawlen) return ret;
1066 /* if everything OK send the rest */
1067 bytes_left_to_transfer = size-datawlen;
1068 ret = PTP_RC_OK;
1069 while(bytes_left_to_transfer > 0) {
1070 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1071 if (ret != PTP_RC_OK)
1072 break;
1073 if (written == 0) {
1074 ret = PTP_ERROR_IO;
1075 break;
1076 }
1077 bytes_left_to_transfer -= written;
1078 }
1079 if (ret!=PTP_RC_OK)
1080 ret = PTP_ERROR_IO;
1081 return ret;
1082}
1083
1084static uint16_t ptp_usb_getpacket(PTPParams *params,
1085 PTPUSBBulkContainer *packet, unsigned long *rlen)
1086{
1087 PTPDataHandler memhandler;
1088 uint16_t ret;
1089 unsigned char *x = NULL;
1090
1091 /* read the header and potentially the first data */
1092 if (params->response_packet_size > 0) {
1093 /* If there is a buffered packet, just use it. */
1094 memcpy(packet, params->response_packet, params->response_packet_size);
1095 *rlen = params->response_packet_size;
1096 free(params->response_packet);
1097 params->response_packet = NULL;
1098 params->response_packet_size = 0;
1099 /* Here this signifies a "virtual read" */
1100 return PTP_RC_OK;
1101 }
1102 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001103 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001104 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1105 if (x) {
1106 memcpy (packet, x, *rlen);
1107 free (x);
1108 }
1109 return ret;
1110}
1111
1112uint16_t
1113ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1114{
1115 uint16_t ret;
1116 PTPUSBBulkContainer usbdata;
1117 unsigned char *data;
1118 unsigned long written;
1119
1120 memset(&usbdata,0,sizeof(usbdata));
1121 do {
1122 unsigned long len, rlen;
1123
1124 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1125 if (ret!=PTP_RC_OK) {
1126 ret = PTP_ERROR_IO;
1127 break;
1128 } else
1129 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1130 ret = PTP_ERROR_DATA_EXPECTED;
1131 break;
1132 } else
1133 if (dtoh16(usbdata.code)!=ptp->Code) {
1134 ret = dtoh16(usbdata.code);
1135 break;
1136 }
1137 if (usbdata.length == 0xffffffffU) {
1138 /* stuff data directly to passed data handler */
1139 while (1) {
1140 unsigned long readdata;
1141 int xret;
1142
1143 xret = ptp_read_func(
Richard Low02724f62007-02-17 09:47:26 +00001144 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
Linus Walleija0323272007-01-07 12:21:30 +00001145 handler,
1146 params->data,
Richard Low4df32f82007-01-11 20:04:39 +00001147 &readdata,
Linus Walleijc0a11432007-03-02 21:21:18 +00001148 0
Linus Walleija0323272007-01-07 12:21:30 +00001149 );
1150 if (xret == -1)
1151 return PTP_ERROR_IO;
Richard Low02724f62007-02-17 09:47:26 +00001152 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
Linus Walleija0323272007-01-07 12:21:30 +00001153 break;
1154 }
1155 return PTP_RC_OK;
1156 }
1157 if (rlen > dtoh32(usbdata.length)) {
1158 /*
1159 * Buffer the surplus response packet if it is >=
1160 * PTP_USB_BULK_HDR_LEN
1161 * (i.e. it is probably an entire package)
1162 * else discard it as erroneous surplus data.
1163 * This will even work if more than 2 packets appear
1164 * in the same transaction, they will just be handled
1165 * iteratively.
1166 *
1167 * Marcus observed stray bytes on iRiver devices;
1168 * these are still discarded.
1169 */
1170 unsigned int packlen = dtoh32(usbdata.length);
1171 unsigned int surplen = rlen - packlen;
1172
1173 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1174 params->response_packet = malloc(surplen);
1175 memcpy(params->response_packet,
1176 (uint8_t *) &usbdata + packlen, surplen);
1177 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001178 /* Ignore reading one extra byte if device flags have been set */
1179 } else if( !(((PTP_USB *)params->data)->device_flags &
1180 DEVICE_FLAG_NO_ZERO_READS &&
1181 rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001182 ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
1183 "too much, expect problems!",
1184 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001185 }
1186 rlen = packlen;
1187 }
1188
1189 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1190 * here. For MTP devices splitting header and data it might
1191 * be 12.
1192 */
1193 /* Evaluate full data length. */
1194 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1195
1196 /* autodetect split header/data MTP devices */
1197 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1198 params->split_header_data = 1;
1199
Richard Low02724f62007-02-17 09:47:26 +00001200 data = malloc(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ);
Linus Walleija0323272007-01-07 12:21:30 +00001201 /* Copy first part of data to 'data' */
1202 handler->putfunc(
1203 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1204 &written
1205 );
Richard Low4fd59d62007-03-03 16:34:37 +00001206
Linus Walleij049f50c2007-03-03 20:30:43 +00001207 if (((PTP_USB *)params->data)->device_flags & DEVICE_FLAG_NO_ZERO_READS &&
1208 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
Richard Low057ea772007-03-24 13:37:38 +00001209#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleij049f50c2007-03-03 20:30:43 +00001210 printf("Reading in extra terminating byte\n");
Richard Low4fd59d62007-03-03 16:34:37 +00001211#endif
Linus Walleij049f50c2007-03-03 20:30:43 +00001212 // need to read in extra byte and discard it
1213 int result = 0;
1214 char byte = 0;
1215 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1216 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptpcam_usb_timeout);
1217
1218 if (result != 1)
1219 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 +00001220 } else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ && params->split_header_data == 0) {
1221#ifdef ENABLE_USB_BULK_DEBUG
1222 printf("Reading in zero packet after header\n");
1223#endif
1224 int zeroresult = 0;
1225 char zerobyte = 0;
1226 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1227 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptpcam_usb_timeout);
1228
1229 if (zeroresult != 0)
1230 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
1231 }
1232
Linus Walleija0323272007-01-07 12:21:30 +00001233 /* Is that all of data? */
1234 if (len+PTP_USB_BULK_HDR_LEN<=rlen) break;
Linus Walleij049f50c2007-03-03 20:30:43 +00001235
1236 ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1237 handler,
1238 params->data, &rlen, 1);
1239
Linus Walleija0323272007-01-07 12:21:30 +00001240 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001241 ret = PTP_ERROR_IO;
1242 break;
Linus Walleija0323272007-01-07 12:21:30 +00001243 }
1244 } while (0);
Linus Walleij049f50c2007-03-03 20:30:43 +00001245 /*
Linus Walleija0323272007-01-07 12:21:30 +00001246 if (ret!=PTP_RC_OK) {
1247 ptp_error (params,
1248 "PTP: request code 0x%04x getting data error 0x%04x",
1249 ptp->Code, ret);
1250 }*/
1251 return ret;
1252}
1253
1254uint16_t
1255ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1256{
1257 uint16_t ret;
1258 unsigned long rlen;
1259 PTPUSBBulkContainer usbresp;
1260
1261 memset(&usbresp,0,sizeof(usbresp));
1262 /* read response, it should never be longer than sizeof(usbresp) */
1263 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1264
1265 if (ret!=PTP_RC_OK) {
1266 ret = PTP_ERROR_IO;
1267 } else
1268 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1269 ret = PTP_ERROR_RESP_EXPECTED;
1270 } else
1271 if (dtoh16(usbresp.code)!=resp->Code) {
1272 ret = dtoh16(usbresp.code);
1273 }
1274 if (ret!=PTP_RC_OK) {
1275/* ptp_error (params,
1276 "PTP: request code 0x%04x getting resp error 0x%04x",
1277 resp->Code, ret);*/
1278 return ret;
1279 }
1280 /* build an appropriate PTPContainer */
1281 resp->Code=dtoh16(usbresp.code);
1282 resp->SessionID=params->session_id;
1283 resp->Transaction_ID=dtoh32(usbresp.trans_id);
1284 resp->Param1=dtoh32(usbresp.payload.params.param1);
1285 resp->Param2=dtoh32(usbresp.payload.params.param2);
1286 resp->Param3=dtoh32(usbresp.payload.params.param3);
1287 resp->Param4=dtoh32(usbresp.payload.params.param4);
1288 resp->Param5=dtoh32(usbresp.payload.params.param5);
1289 return ret;
1290}
1291
1292/* Event handling functions */
1293
1294/* PTP Events wait for or check mode */
1295#define PTP_EVENT_CHECK 0x0000 /* waits for */
1296#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1297
1298static inline uint16_t
1299ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1300{
1301 uint16_t ret;
1302 int result;
1303 unsigned long rlen;
1304 PTPUSBEventContainer usbevent;
1305 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1306
1307 memset(&usbevent,0,sizeof(usbevent));
1308
1309 if ((params==NULL) || (event==NULL))
1310 return PTP_ERROR_BADPARAM;
1311 ret = PTP_RC_OK;
1312 switch(wait) {
1313 case PTP_EVENT_CHECK:
1314 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1315 if (result==0)
1316 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1317 if (result < 0) ret = PTP_ERROR_IO;
1318 break;
1319 case PTP_EVENT_CHECK_FAST:
1320 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1321 if (result==0)
1322 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1323 if (result < 0) ret = PTP_ERROR_IO;
1324 break;
1325 default:
1326 ret=PTP_ERROR_BADPARAM;
1327 break;
1328 }
1329 if (ret!=PTP_RC_OK) {
1330 ptp_error (params,
1331 "PTP: reading event an error 0x%04x occurred", ret);
1332 return PTP_ERROR_IO;
1333 }
1334 rlen = result;
1335 if (rlen < 8) {
1336 ptp_error (params,
1337 "PTP: reading event an short read of %ld bytes occurred", rlen);
1338 return PTP_ERROR_IO;
1339 }
1340 /* if we read anything over interrupt endpoint it must be an event */
1341 /* build an appropriate PTPContainer */
1342 event->Code=dtoh16(usbevent.code);
1343 event->SessionID=params->session_id;
1344 event->Transaction_ID=dtoh32(usbevent.trans_id);
1345 event->Param1=dtoh32(usbevent.param1);
1346 event->Param2=dtoh32(usbevent.param2);
1347 event->Param3=dtoh32(usbevent.param3);
1348 return ret;
1349}
1350
1351uint16_t
1352ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1353
1354 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1355}
1356
1357uint16_t
1358ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1359
1360 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1361}
1362
1363
Linus Walleij9eb3d312006-08-04 19:25:59 +00001364static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001365{
Linus Walleijd6a49972006-05-02 08:24:54 +00001366 usb_dev_handle *device_handle;
1367
Linus Walleijd6a49972006-05-02 08:24:54 +00001368 params->error_func=NULL;
1369 params->debug_func=NULL;
1370 params->sendreq_func=ptp_usb_sendreq;
1371 params->senddata_func=ptp_usb_senddata;
1372 params->getresp_func=ptp_usb_getresp;
1373 params->getdata_func=ptp_usb_getdata;
1374 params->data=ptp_usb;
1375 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001376 /*
1377 * This is hardcoded here since we have no devices whatsoever that are BE.
1378 * Change this the day we run into our first BE device (if ever).
1379 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001380 params->byteorder = PTP_DL_LE;
1381
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001382 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001383 if (!device_handle) {
1384 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001385 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001386 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001387 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001388#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1389 /*
1390 * If this device is known to be wrongfully claimed by other kernel
1391 * drivers (such as mass storage), then try to unload it to make it
1392 * accessible from user space.
1393 */
1394 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
1395 if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij7beba572006-11-29 08:56:12 +00001396 // Totally ignore this error!
1397 // perror("usb_detach_kernel_driver_np()");
Linus Walleij501ba4d2006-10-16 06:17:04 +00001398 }
1399 }
1400#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001401#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001402 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001403 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1404 perror("usb_set_configuration()");
1405 return -1;
1406 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001407#endif
Linus Walleijef5cca92006-05-05 07:19:08 +00001408 if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001409 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001410 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001411 }
Linus Walleijc6210fb2006-05-08 11:11:41 +00001412 ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber;
Linus Walleijd6a49972006-05-02 08:24:54 +00001413 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001414 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001415}
1416
Linus Walleijd6a49972006-05-02 08:24:54 +00001417static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001418{
Linus Walleij58b62792007-01-07 12:38:59 +00001419 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001420 int ret;
1421
1422 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001423 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001424 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001425 if (ret<0) {
1426 perror ("inep: usb_get_endpoint_status()");
1427 } else if (status) {
1428 printf("Clearing stall on IN endpoint\n");
1429 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1430 if (ret<0) {
1431 perror ("usb_clear_stall_feature()");
1432 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001433 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001434
1435 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001436 status=0;
1437 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1438 if (ret<0) {
1439 perror("outep: usb_get_endpoint_status()");
1440 } else if (status) {
1441 printf("Clearing stall on OUT endpoint\n");
1442 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1443 if (ret<0) {
1444 perror("usb_clear_stall_feature()");
1445 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001446 }
Linus Walleij58b62792007-01-07 12:38:59 +00001447
1448 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1449}
1450
1451static void clear_halt(PTP_USB* ptp_usb)
1452{
1453 int ret;
1454
1455 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1456 if (ret<0) {
1457 perror("usb_clear_halt() on IN endpoint");
1458 }
1459 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1460 if (ret<0) {
1461 perror("usb_clear_halt() on OUT endpoint");
1462 }
1463 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1464 if (ret<0) {
1465 perror("usb_clear_halt() on INTERRUPT endpoint");
1466 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001467}
1468
Linus Walleijd6a49972006-05-02 08:24:54 +00001469static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001470{
Linus Walleij58b62792007-01-07 12:38:59 +00001471 // Clear any stalled endpoints
Linus Walleijd6a49972006-05-02 08:24:54 +00001472 clear_stall(ptp_usb);
Linus Walleij58b62792007-01-07 12:38:59 +00001473 // Clear halts on any endpoints
1474 clear_halt(ptp_usb);
Linus Walleijd6a49972006-05-02 08:24:54 +00001475 // Added to clear some stuff on the OUT endpoint
1476 // TODO: is this good on the Mac too?
1477 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1478 usb_release_interface(ptp_usb->handle, interfaceNumber);
1479 // Brutally reset device
1480 // TODO: is this good on the Mac too?
1481 usb_reset(ptp_usb->handle);
1482 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001483}
1484
Linus Walleij45a86372007-03-07 09:36:19 +00001485static LIBMTP_error_number_t prime_device_memory(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001486{
Linus Walleij45a86372007-03-07 09:36:19 +00001487 mtpdevice_list_t *tmplist = devlist;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001488
Linus Walleij45a86372007-03-07 09:36:19 +00001489 while (tmplist != NULL) {
tedbullock0f033cb2007-02-14 20:56:54 +00001490 /* Allocate a parameter box */
Linus Walleij45a86372007-03-07 09:36:19 +00001491 tmplist->params = (PTPParams *) malloc(sizeof(PTPParams));
1492 tmplist->ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleijbfcb7922007-03-06 20:14:04 +00001493
tedbullock0f033cb2007-02-14 20:56:54 +00001494 /* Check for allocation Error */
Linus Walleij45a86372007-03-07 09:36:19 +00001495 if(tmplist->params == NULL || tmplist->ptp_usb == NULL) {
Linus Walleijbfcb7922007-03-06 20:14:04 +00001496 /* Error and deallocation of memory will be handled by caller. */
tedbullock0f033cb2007-02-14 20:56:54 +00001497 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1498 }
1499
tedbullock69a445b2007-02-15 07:41:04 +00001500 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleij45a86372007-03-07 09:36:19 +00001501 memset(tmplist->params, 0, sizeof(PTPParams));
1502 memset(tmplist->ptp_usb, 0, sizeof(PTP_USB));
1503 tmplist = tmplist->next;
tedbullock0f033cb2007-02-14 20:56:54 +00001504 }
tedbullock0f033cb2007-02-14 20:56:54 +00001505 return LIBMTP_ERROR_NONE;
1506}
1507
Linus Walleij45a86372007-03-07 09:36:19 +00001508static void assign_known_device_flags(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001509{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001510 int i;
Linus Walleij27c50c52007-03-06 20:34:48 +00001511 mtpdevice_list_t *tmplist = devlist;
1512 uint8_t current_device = 0;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001513
tedbullock0f033cb2007-02-14 20:56:54 +00001514 /* Search through known device list and set correct device flags */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001515 while (tmplist != NULL) {
1516 int device_known = 0;
1517
1518 for(i = 0; i < mtp_device_table_size; i++) {
1519 if(tmplist->libusb_device->descriptor.idVendor == mtp_device_table[i].vendor_id &&
1520 tmplist->libusb_device->descriptor.idProduct == mtp_device_table[i].product_id) {
1521 /* This device is known, assign the correct device flags */
1522 /* Note that ptp_usb[current_device] could potentially be NULL */
Linus Walleij45a86372007-03-07 09:36:19 +00001523 if(tmplist->ptp_usb != NULL) {
1524 tmplist->ptp_usb->device_flags = mtp_device_table[i].device_flags;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001525
1526 /*
1527 * TODO:
1528 * Preferable to not do this with #ifdef ENABLE_USB_BULK_DEBUG but there is
1529 * currently no other compile time debug option
1530 */
1531
1532 device_known = 1;
tedbullock65a05772007-03-03 22:56:56 +00001533#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001534 /* This device is known to the developers */
1535 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is a %s.\n",
1536 current_device + 1,
1537 tmplist->libusb_device->descriptor.idVendor,
1538 tmplist->libusb_device->descriptor.idProduct,
1539 mtp_device_table[i].name);
tedbullock65a05772007-03-03 22:56:56 +00001540#endif
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001541 }
1542 break;
1543 }
tedbullock0f033cb2007-02-14 20:56:54 +00001544 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001545 if (!device_known) {
1546 /* This device is unknown to the developers */
1547 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
1548 current_device + 1,
1549 tmplist->libusb_device->descriptor.idVendor,
1550 tmplist->libusb_device->descriptor.idProduct);
1551 fprintf(stderr, "Please report this VID/PID and the device model to the "
1552 "libmtp development team\n");
1553 }
1554 tmplist = tmplist->next;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001555 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001556 }
tedbullock0f033cb2007-02-14 20:56:54 +00001557}
1558
tedbullock0f033cb2007-02-14 20:56:54 +00001559
Linus Walleij45a86372007-03-07 09:36:19 +00001560static LIBMTP_error_number_t configure_usb_devices(mtpdevice_list_t *devicelist)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001561{
1562 mtpdevice_list_t *tmplist = devicelist;
1563 uint16_t ret = 0;
1564 uint8_t current_device = 0;
1565
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001566 while (tmplist != NULL) {
Linus Walleij45a86372007-03-07 09:36:19 +00001567 /* This is erroneous, there must be a PTP_USB instance that we can initialize. */
1568 if(tmplist->ptp_usb == NULL) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001569 return LIBMTP_ERROR_MEMORY_ALLOCATION;
tedbullock0f033cb2007-02-14 20:56:54 +00001570 }
1571
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001572 /* TODO: Will this always be little endian? */
Linus Walleij45a86372007-03-07 09:36:19 +00001573 tmplist->params->byteorder = PTP_DL_LE;
1574 tmplist->params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8");
1575 tmplist->params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE");
tedbullock0f033cb2007-02-14 20:56:54 +00001576
Linus Walleij45a86372007-03-07 09:36:19 +00001577 if(tmplist->params->cd_locale_to_ucs2 == (iconv_t) -1 ||
1578 tmplist->params->cd_ucs2_to_locale == (iconv_t) -1) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001579 fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
1580 "Too old stdlibc, glibc and libiconv?\n");
Linus Walleij35c0ef12007-03-06 20:49:09 +00001581 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001582 }
1583
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001584 // ep = device->config->interface->altsetting->endpoint;
1585 // no_of_ep = device->config->interface->altsetting->bNumEndpoints;
tedbullock0f033cb2007-02-14 20:56:54 +00001586
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001587 /* Assign endpoints to usbinfo... */
1588 find_endpoints(tmplist->libusb_device,
Linus Walleij45a86372007-03-07 09:36:19 +00001589 &tmplist->ptp_usb->inep,
1590 &tmplist->ptp_usb->inep_maxpacket,
1591 &tmplist->ptp_usb->outep,
1592 &tmplist->ptp_usb->outep_maxpacket,
1593 &tmplist->ptp_usb->intep);
tedbullock0f033cb2007-02-14 20:56:54 +00001594
Linus Walleij45a86372007-03-07 09:36:19 +00001595 /* Attempt to initialize this device */
1596 if (init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) < 0) {
1597 fprintf(stderr, "LIBMTP PANIC: Unable to initialize device %d\n", current_device+1);
1598 // FIXME: perhaps use "continue" to keep trying the other devices.
Linus Walleij35c0ef12007-03-06 20:49:09 +00001599 return LIBMTP_ERROR_CONNECTING;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001600 }
1601
1602 /*
1603 * This works in situations where previous bad applications
1604 * have not used LIBMTP_Release_Device on exit
1605 */
Linus Walleij45a86372007-03-07 09:36:19 +00001606 if ((ret = ptp_opensession(tmplist->params, 1)) == PTP_ERROR_IO) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001607 fprintf(stderr, "PTP_ERROR_IO: Trying again after resetting USB\n");
Linus Walleij45a86372007-03-07 09:36:19 +00001608 close_usb(tmplist->ptp_usb, tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001609
Linus Walleij45a86372007-03-07 09:36:19 +00001610 if(init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) <0) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001611 fprintf(stderr, "LIBMTP PANIC: Could not open session on device %d\n", current_device+1);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001612 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001613 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001614
1615 /* Device has been reset, try again */
Linus Walleij45a86372007-03-07 09:36:19 +00001616 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001617 }
1618
1619 /* Was the transaction id invalid? Try again */
1620 if (ret == PTP_RC_InvalidTransactionID) {
Linus Walleij45a86372007-03-07 09:36:19 +00001621 fprintf(stderr, "LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
1622 tmplist->params->transaction_id += 10;
1623 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001624 }
1625
1626 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
Linus Walleij45a86372007-03-07 09:36:19 +00001627 fprintf(stderr, "LIBMTP PANIC: Could not open session! "
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001628 "(Return code %d)\n Try to reset the device.\n",
1629 ret);
Linus Walleij45a86372007-03-07 09:36:19 +00001630 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001631 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001632 return LIBMTP_ERROR_CONNECTING;
1633 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001634
1635 /* It is permissible to call this before opening the session */
Linus Walleij45a86372007-03-07 09:36:19 +00001636 if (ptp_getdeviceinfo(tmplist->params,
1637 &tmplist->params->deviceinfo) != PTP_RC_OK) {
1638 fprintf(stderr, "LIBMTP PANIC: Could not get device info!\n");
1639 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001640 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001641 return LIBMTP_ERROR_CONNECTING;
1642 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001643
1644 tmplist = tmplist->next;
1645 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001646 }
1647
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001648 /* Exit with the nice list */
1649 return LIBMTP_ERROR_NONE;
tedbullock0f033cb2007-02-14 20:56:54 +00001650}
1651
Linus Walleij550d6d52007-01-24 14:32:51 +00001652/**
1653 * This function scans through the results of the get_mtp_usb_device_list
1654 * function and attempts to connect to those devices listed using the
1655 * mtp_device_table at the top of the file. Returns a LIBMTP_error_number_t.
1656 *
Linus Walleij45a86372007-03-07 09:36:19 +00001657 * @param devlist a list of devices with primed PTP_USB and params structs.
Linus Walleij550d6d52007-01-24 14:32:51 +00001658 * @return Error Codes as per the type definition
1659 */
Linus Walleij45a86372007-03-07 09:36:19 +00001660LIBMTP_error_number_t find_usb_devices(mtpdevice_list_t **devlist)
Linus Walleij550d6d52007-01-24 14:32:51 +00001661{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001662 mtpdevice_list_t *mtp_device_list = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001663 LIBMTP_error_number_t ret;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001664
Linus Walleija4942fc2007-03-12 19:23:21 +00001665 /*
1666 * Recover list of attached USB devices that match MTP criteria, i.e.
1667 * it either has an MTP device descriptor or it is in the known
1668 * devices list.
1669 */
Linus Walleij45a86372007-03-07 09:36:19 +00001670 ret = get_mtp_usb_device_list (&mtp_device_list);
Linus Walleijac061862007-03-07 08:28:33 +00001671 if (ret != LIBMTP_ERROR_NONE) {
1672 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001673 }
1674
Linus Walleij45a86372007-03-07 09:36:19 +00001675 // Then prime them
1676 ret = prime_device_memory(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001677 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001678 fprintf(stderr, "LIBMTP PANIC: prime_device_memory() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001679 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001680 }
tedbullock0f033cb2007-02-14 20:56:54 +00001681
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001682 /* Assign specific device flags and detect unknown devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001683 assign_known_device_flags(mtp_device_list);
Linus Walleij550d6d52007-01-24 14:32:51 +00001684
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001685 /* Configure the devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001686 ret = configure_usb_devices(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001687 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001688 fprintf(stderr, "LIBMTP PANIC: configure_usb_devices() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001689 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001690 }
Linus Walleij550d6d52007-01-24 14:32:51 +00001691
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001692 /* Configure interface numbers */
Linus Walleij550d6d52007-01-24 14:32:51 +00001693 {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001694 mtpdevice_list_t *tmplist = mtp_device_list;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001695
Linus Walleij45a86372007-03-07 09:36:19 +00001696 while (tmplist != NULL) {
1697 tmplist->interface_number = tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001698 tmplist = tmplist->next;
Linus Walleij550d6d52007-01-24 14:32:51 +00001699 }
1700 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001701
Linus Walleij45a86372007-03-07 09:36:19 +00001702 /* we're connected to all devices, return the list and OK */
1703 *devlist = mtp_device_list;
1704 return LIBMTP_ERROR_NONE;
1705
1706 find_usb_devices_error_exit:
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001707 if(mtp_device_list != NULL) {
1708 free_mtpdevice_list(mtp_device_list);
1709 mtp_device_list = NULL;
Linus Walleij550d6d52007-01-24 14:32:51 +00001710 }
Linus Walleij45a86372007-03-07 09:36:19 +00001711 *devlist = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001712 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001713}
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001714
Linus Walleijd6a49972006-05-02 08:24:54 +00001715static 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 +00001716{
Linus Walleijd6a49972006-05-02 08:24:54 +00001717 int i,n;
1718 struct usb_endpoint_descriptor *ep;
1719
1720 ep = dev->config->interface->altsetting->endpoint;
1721 n=dev->config->interface->altsetting->bNumEndpoints;
1722
1723 for (i=0;i<n;i++) {
1724 if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1725 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1726 USB_ENDPOINT_DIR_MASK)
1727 {
1728 *inep=ep[i].bEndpointAddress;
1729 *inep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001730 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001731 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1732 {
1733 *outep=ep[i].bEndpointAddress;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001734 *outep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijd6a49972006-05-02 08:24:54 +00001735 }
1736 } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1737 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1738 USB_ENDPOINT_DIR_MASK)
1739 {
1740 *intep=ep[i].bEndpointAddress;
1741 }
1742 }
1743 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001744}
1745
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001746void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber)
1747{
Linus Walleijd6a49972006-05-02 08:24:54 +00001748 if (ptp_closesession(params)!=PTP_RC_OK)
1749 fprintf(stderr,"ERROR: Could not close session!\n");
1750 close_usb(ptp_usb, interfaceNumber);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001751}
1752
Linus Walleijd6a49972006-05-02 08:24:54 +00001753static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001754{
Linus Walleijd6a49972006-05-02 08:24:54 +00001755
1756 return (usb_control_msg(ptp_usb->handle,
1757 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
1758 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001759}
1760
Linus Walleijd6a49972006-05-02 08:24:54 +00001761static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001762{
Linus Walleijd6a49972006-05-02 08:24:54 +00001763 return (usb_control_msg(ptp_usb->handle,
1764 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
1765 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001766}