blob: 5b64910c62d5e5cba6f956188959d9c7b59dca3b [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 Walleij968ec012007-09-01 18:44:55 +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
Linus Walleij968ec012007-09-01 18:44:55 +000093 { "Creative Zen V", 0x041e, 0x4150, DEVICE_FLAG_NONE },
Linus Walleij92a84962006-11-11 20:04:37 +000094 // Reported by danielw@iinet.net.au
Linus Walleij968ec012007-09-01 18:44:55 +000095 { "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
Linus Walleij968ec012007-09-01 18:44:55 +000097 { "Creative Zen V Plus", 0x041e, 0x4152, DEVICE_FLAG_NONE },
98 { "Creative Zen Vision W", 0x041e, 0x4153, DEVICE_FLAG_NONE },
Linus Walleijd1d60ee2007-09-03 23:51:53 +000099 // Reported by Ringofan <mcroman@users.sourceforge.net>
100 { "Creative Zen V 2GB", 0x041e, 0x4158, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000101
102 /*
103 * Samsung
Linus Walleij0558ac52006-09-07 06:55:03 +0000104 * We suspect that more of these are dual mode.
Linus Walleij3d2c7842006-08-18 09:35:08 +0000105 */
Linus Walleij5beb1cf2007-06-17 19:10:42 +0000106 // From Soren O'Neill
107 { "Samsung YH-920", 0x04e8, 0x5022, DEVICE_FLAG_UNLOAD_DRIVER },
108 // Contributed by aronvanammers on SourceForge
109 { "Samsung YH-925GS", 0x04e8, 0x5024, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000110 // From libgphoto2
Richard Low5224b1c2007-03-24 12:56:34 +0000111 { "Samsung YH-820", 0x04e8, 0x502e, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000112 // Contributed by polux2001@users.sourceforge.net
Richard Low5224b1c2007-03-24 12:56:34 +0000113 { "Samsung YH-925(-GS)", 0x04e8, 0x502f, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000114 // Contributed by anonymous person on SourceForge
Linus Walleijfe036562007-06-17 19:04:59 +0000115 { "Samsung YH-J70J", 0x04e8, 0x5033, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij5beb1cf2007-06-17 19:10:42 +0000116 // From XNJB user
117 { "Samsung YP-Z5", 0x04e8, 0x503c, DEVICE_FLAG_NONE },
118 // From XNJB user
119 { "Samsung YP-Z5 2GB", 0x04e8, 0x5041, DEVICE_FLAG_NONE },
Linus Walleijfe036562007-06-17 19:04:59 +0000120 // Contributed by anonymous person on SourceForge
Richard Low5224b1c2007-03-24 12:56:34 +0000121 { "Samsung YP-T7J", 0x04e8, 0x5047, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000122 // Reported by cstrickler@gmail.com
Linus Walleijbd7624c2007-05-28 10:48:54 +0000123 { "Samsung YP-U2J (YP-U2JXB/XAA)", 0x04e8, 0x5054, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij82ed6612006-08-31 08:06:19 +0000124 // Reported by Andrew Benson
Linus Walleij4fa9d5e2007-05-05 20:35:56 +0000125 { "Samsung YP-F2J", 0x04e8, 0x5057, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij19be4212006-11-16 09:00:48 +0000126 // Reported by Patrick <skibler@gmail.com>
Richard Low6c8fea42007-03-18 19:11:52 +0000127 { "Samsung YP-K5", 0x04e8, 0x505a, DEVICE_FLAG_NO_ZERO_READS },
Linus Walleij91fba482007-08-15 15:59:09 +0000128 // From dev.local@gmail.com - 0x4e8/0x507c is the UMS mode, don't add this.
Linus Walleijd7d896f2007-07-26 20:09:35 +0000129 // From m.eik michalke
130 { "Samsung YP-U3", 0x04e8, 0x507d, DEVICE_FLAG_NONE },
Linus Walleija5073c72006-11-24 07:55:12 +0000131 // Reported by Matthew Wilcox <matthew@wil.cx>
Richard Low5224b1c2007-03-24 12:56:34 +0000132 { "Samsung YP-T9", 0x04e8, 0x507f, DEVICE_FLAG_NONE },
Linus Walleij12413c02007-02-22 18:33:07 +0000133 // From Paul Clinch
Richard Low5224b1c2007-03-24 12:56:34 +0000134 { "Samsung YP-K3", 0x04e8, 0x5081, DEVICE_FLAG_NONE },
Linus Walleijf06189e2007-04-16 13:52:42 +0000135 // From a rouge .INF file,
136 // this device ID seems to have been recycled for the Samsung SGH-A707 Cingular cellphone
137 { "Samsung YH-999 Portable Media Center / Samsung SGH-A707", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE },
Linus Walleij8556f522007-02-14 09:47:50 +0000138 // From Lionel Bouton
Richard Low5224b1c2007-03-24 12:56:34 +0000139 { "Samsung X830 Mobile Phone", 0x04e8, 0x6702, DEVICE_FLAG_NONE },
Linus Walleij5beb1cf2007-06-17 19:10:42 +0000140 // From James <jamestech@gmail.com>
141 { "Samsung U600 Mobile Phone", 0x04e8, 0x6709, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000142
143 /*
144 * Intel
145 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000146 { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000147
148 /*
149 * JVC
150 */
151 // From Mark Veinot
Linus Walleij0558ac52006-09-07 06:55:03 +0000152 { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000153
154 /*
Linus Walleij093ca0a2006-11-02 21:15:00 +0000155 * Philips
Linus Walleij3d2c7842006-08-18 09:35:08 +0000156 */
Linus Walleij2b218852007-07-07 20:16:36 +0000157 { "Philips HDD6320/00 and HDD6330/17", 0x0471, 0x014b, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000158 // Anonymous SourceForge user
Linus Walleij093ca0a2006-11-02 21:15:00 +0000159 { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE },
Richard Lowbf2675c2007-03-18 18:20:26 +0000160 // from discussion forum
jefferaif4e68712007-05-25 15:47:15 +0000161 { "Philips HDD085/00 and HDD082/17", 0x0471, 0x014d, DEVICE_FLAG_NONE },
Linus Walleij2b218852007-07-07 20:16:36 +0000162 // from XNJB forum
163 { "Philips GoGear SA9200", 0x0471, 0x014f, DEVICE_FLAG_NONE },
164 // From Gerhard Mekenkamp
165 { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE },
Linus Walleijc4ce3602007-08-15 10:20:12 +0000166 // from David Holm <wormie@alberg.dk>
Linus Walleijd3b78572007-08-24 21:28:24 +0000167 { "Philips Shoqbox", 0x0471, 0x0172, DEVICE_FLAG_ONLY_7BIT_FILENAMES },
Richard Low6711f442007-05-05 19:00:59 +0000168 // from npedrosa
169 { "Philips PSA610", 0x0471, 0x0181, DEVICE_FLAG_NONE },
Linus Walleij2b218852007-07-07 20:16:36 +0000170 // From libgphoto2 source
171 { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE },
172 // from XNJB user
173 { "Philips PSA235", 0x0471, 0x7e01, DEVICE_FLAG_NONE },
174
Linus Walleij3d2c7842006-08-18 09:35:08 +0000175
176 /*
177 * SanDisk
Linus Walleijf41abe52007-03-20 19:25:18 +0000178 * several devices (c150 for sure) are definately dual-mode and must
179 * have the USB mass storage driver that hooks them unloaded first.
Linus Walleijdf88ee42007-08-18 20:47:03 +0000180 * They all have problematic dual-mode making the device unload effect
Linus Walleij6d0cf972007-08-28 09:48:19 +0000181 * uncertain on these devices. All except for the Linux based ones seem
182 * to need DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL.
Linus Walleij3d2c7842006-08-18 09:35:08 +0000183 */
184 // Reported by Brian Robison
Linus Walleij6d0cf972007-08-28 09:48:19 +0000185 { "SanDisk Sansa m230/m240", 0x0781, 0x7400,
186 DEVICE_FLAG_UNLOAD_DRIVER | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL},
Linus Walleijf41abe52007-03-20 19:25:18 +0000187 // Reported by tangent_@users.sourceforge.net
Linus Walleij6d0cf972007-08-28 09:48:19 +0000188 { "SanDisk Sansa c150", 0x0781, 0x7410,
189 DEVICE_FLAG_UNLOAD_DRIVER | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000190 // From libgphoto2 source
Linus Walleijd6b67022007-05-10 07:34:11 +0000191 // Reported by <gonkflea@users.sourceforge.net>
192 // Reported by Mike Owen <mikeowen@computerbaseusa.com>
Linus Walleij6d0cf972007-08-28 09:48:19 +0000193 { "SanDisk Sansa e200/e250/e260/e270/e280", 0x0781, 0x7420,
194 DEVICE_FLAG_UNLOAD_DRIVER | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL },
Richard Lowbf2675c2007-03-18 18:20:26 +0000195 // Reported by XNJB user
Linus Walleij6d0cf972007-08-28 09:48:19 +0000196 { "SanDisk Sansa e280", 0x0781, 0x7421,
197 DEVICE_FLAG_UNLOAD_DRIVER | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL },
Linus Walleijef8c1172007-04-30 21:35:50 +0000198 // Reported by anonymous user at sourceforge.net
Linus Walleij6d0cf972007-08-28 09:48:19 +0000199 { "SanDisk Sansa c240/c250", 0x0781, 0x7450,
200 DEVICE_FLAG_UNLOAD_DRIVER | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL },
Linus Walleija927f432007-04-18 20:24:34 +0000201 // Reported by XNJB user, and Miguel de Icaza <miguel@gnome.org>
Linus Walleija20a2432007-04-19 08:41:51 +0000202 // This has no dual-mode so no need to unload any driver.
Linus Walleij1ecab692007-05-09 22:07:45 +0000203 // This is a Linux based device!
Linus Walleija20a2432007-04-19 08:41:51 +0000204 { "SanDisk Sansa Connect", 0x0781, 0x7480, DEVICE_FLAG_NONE },
Linus Walleij853da3c2007-07-17 19:46:50 +0000205 // Reported by Troy Curtis Jr.
Linus Walleij6d0cf972007-08-28 09:48:19 +0000206 { "SanDisk Sansa Express", 0x0781, 0x7460,
207 DEVICE_FLAG_UNLOAD_DRIVER | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Richard Low25bf8002007-08-04 08:36:53 +0000208 // Reported by XNJB user
Linus Walleij6d0cf972007-08-28 09:48:19 +0000209 { "SanDisk Sansa m240", 0x0781, 0x7430,
210 DEVICE_FLAG_UNLOAD_DRIVER | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL },
Richard Low856f3452007-04-13 11:21:26 +0000211
Linus Walleij3d2c7842006-08-18 09:35:08 +0000212
213 /*
214 * iRiver
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000215 * we assume that PTP_OC_MTP_GetObjPropList is essentially
Linus Walleijfabffc02006-12-17 00:18:57 +0000216 * broken on all iRiver devices, meaning it simply won't return
217 * all properties for a file when asking for metadata 0xffffffff.
218 * Please test on your device if you believe it isn't broken!
219 * Some devices from http://www.mtp-ums.net/viewdeviceinfo.php
Linus Walleij3d2c7842006-08-18 09:35:08 +0000220 */
Linus Walleij2be40c72007-03-16 15:19:44 +0000221 { "iRiver Portable Media Center", 0x1006, 0x4002,
222 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
223 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
224 { "iRiver Portable Media Center", 0x1006, 0x4003,
225 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
226 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleijfe036562007-06-17 19:04:59 +0000227 // From an anonymous person at SourceForge
228 { "iRiver iFP-880", 0x4102, 0x1008,
229 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
230 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000231 // From libgphoto2 source
Linus Walleij2be40c72007-03-16 15:19:44 +0000232 { "iRiver T10", 0x4102, 0x1113,
233 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
234 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
235 { "iRiver T20 FM", 0x4102, 0x1114,
236 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
237 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleijfabffc02006-12-17 00:18:57 +0000238 // This appears at the MTP-UMS site
Linus Walleij2be40c72007-03-16 15:19:44 +0000239 { "iRiver T20", 0x4102, 0x1115,
240 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
241 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
242 { "iRiver U10", 0x4102, 0x1116,
243 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
244 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
245 { "iRiver T10", 0x4102, 0x1117,
246 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
247 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
248 { "iRiver T20", 0x4102, 0x1118,
249 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
250 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
251 { "iRiver T30", 0x4102, 0x1119,
252 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
253 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij77e047c2006-09-11 19:09:06 +0000254 // Reported by David Wolpoff
Linus Walleij2be40c72007-03-16 15:19:44 +0000255 { "iRiver T10 2GB", 0x4102, 0x1120,
256 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
257 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij87099812006-12-17 00:29:58 +0000258 // Rough guess this is the MTP device ID...
Linus Walleij2be40c72007-03-16 15:19:44 +0000259 { "iRiver N12", 0x4102, 0x1122,
260 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
261 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij81bf3cb2007-07-07 20:24:58 +0000262 // Reported by Philip Antoniades <philip@mysql.com>
Linus Walleijcfcb6d72007-07-09 22:10:13 +0000263 // Newer iriver devices seem to have shaped-up firmware without any
264 // of the annoying bugs.
265 { "iRiver Clix2", 0x4102, 0x1126, DEVICE_FLAG_NONE },
Linus Walleij04377da2006-08-30 13:03:35 +0000266 // Reported by Adam Torgerson
Linus Walleij2be40c72007-03-16 15:19:44 +0000267 { "iRiver Clix", 0x4102, 0x112a,
268 DEVICE_FLAG_NO_ZERO_READS | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij1ecab692007-05-09 22:07:45 +0000269 // Reported by Douglas Roth <dougaus@gmail.com>
270 { "iRiver X20", 0x4102, 0x1132,
271 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
272 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3dd298d2007-08-04 05:53:42 +0000273 // Reported by Robert Ugo <robert_ugo@users.sourceforge.net>
274 { "iRiver T60", 0x4102, 0x1134,
275 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
276 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000277 // Reported by Scott Call
Linus Walleij2be40c72007-03-16 15:19:44 +0000278 { "iRiver H10 20GB", 0x4102, 0x2101,
279 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
280 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
281 { "iRiver H10", 0x4102, 0x2102,
282 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS |
283 DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000284
Linus Walleij1ecab692007-05-09 22:07:45 +0000285
Linus Walleij3d2c7842006-08-18 09:35:08 +0000286 /*
287 * Dell
288 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000289 { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000290
291 /*
292 * Toshiba
293 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000294 { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE },
295 { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE },
Linus Walleij1e3d1762007-02-26 08:27:35 +0000296 // Reported by Nicholas Tripp
297 { "Toshiba Gigabeat P20", 0x0930, 0x000f, DEVICE_FLAG_NONE },
Linus Walleij405e2392006-10-23 07:13:49 +0000298 // From libgphoto2
Richard Low25bf8002007-08-04 08:36:53 +0000299 { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL },
Linus Walleij402e4bd2006-09-12 07:50:56 +0000300 // Reported by Rob Brown
301 { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE },
Linus Walleij85f274c2007-07-26 20:53:07 +0000302 // Reported by Michael Davis <slithy@yahoo.com>
303 { "Toshiba Gigabeat U", 0x0930, 0x0016, DEVICE_FLAG_NONE },
Linus Walleijb01d18b2006-12-13 12:49:15 +0000304
Linus Walleij6f680562006-08-19 22:49:33 +0000305 /*
306 * Archos
Linus Walleijdf88ee42007-08-18 20:47:03 +0000307 * These devices have some dual-mode interfaces which will really
308 * respect the driver unloading, so DEVICE_FLAG_UNLOAD_DRIVER
309 * really work on these devices!
Linus Walleij6f680562006-08-19 22:49:33 +0000310 */
Linus Walleij1327a852007-05-06 19:47:38 +0000311 // Reported by Alexander Haertig <AlexanderHaertig@gmx.de>
Linus Walleij9455a9a2007-05-09 21:29:49 +0000312 { "Archos Gmini XS100", 0x0e79, 0x1207, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleijdf88ee42007-08-18 20:47:03 +0000313 // Added by Jan Binder
314 { "Archos XS202 (MTP mode)", 0x0e79, 0x1208, DEVICE_FLAG_NONE },
315 // Reported by gudul1@users.sourceforge.net
316 { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE },
317 // Reported by Etienne Chauchot <chauchot.etienne@free.fr>
318 { "Archos 504 (MTP mode)", 0x0e79, 0x1307, DEVICE_FLAG_UNLOAD_DRIVER },
319 // Reported by Kay McCormick <kaym@modsystems.com>
320 { "Archos 704 mobile dvr", 0x0e79, 0x130d, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleije5430902006-10-08 14:46:03 +0000321
322 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000323 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000324 * This unit is falsely detected as USB mass storage in Linux
Linus Walleijcc4dd482006-11-25 21:58:30 +0000325 * prior to kernel 2.6.19 (fixed by patch from Alan Stern)
326 * so on older kernels special care is needed to remove the
327 * USB mass storage driver that erroneously binds to the device
328 * interface.
Linus Walleije5430902006-10-08 14:46:03 +0000329 */
Richard Lowc7d48d22006-11-29 18:24:55 +0000330 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER},
331
332 /*
333 * Microsoft
334 */
335 // Reported by Farooq Zaman
336 { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE },
337
338 /*
339 * Sirius
340 */
Linus Walleijb01d18b2006-12-13 12:49:15 +0000341 { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE },
342
343 /*
344 * Canon
345 * This is actually a camera, but it has a Microsoft device descriptor
346 * and reports itself as supporting the MTP extension.
347 */
Linus Walleijc1b49402007-02-07 08:00:38 +0000348 {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE },
349
350 /*
351 * Nokia
352 */
Linus Walleij42d63562007-06-24 18:25:07 +0000353 // From: Mitchell Hicks <mitchix@yahoo.com>
354 {"Nokia 5300 Mobile Phone", 0x0421, 0x04ba, DEVICE_FLAG_NONE },
Linus Walleijedd9ef92007-09-01 00:03:37 +0000355 // From Christian Arnold <webmaster@arctic-media.de>
356 {"Nokia N73 Mobile Phone", 0x0421, 0x04d1, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleijae182b02007-08-07 07:36:07 +0000357 // From Swapan <swapan@yahoo.com>
358 {"Nokia N75 Mobile Phone", 0x0421, 0x04e1, DEVICE_FLAG_NONE },
Linus Walleij0f8a6f52007-06-14 08:37:58 +0000359 // From: Pat Nicholls <pat@patandannie.co.uk>
360 {"Nokia N80 Internet Edition (Media Player)", 0x0421, 0x04f1, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleijde7ad542007-02-14 09:32:26 +0000361
362 /*
363 * LOGIK
364 * Sold in the UK, seem to be manufactured by CCTech in China.
365 */
Linus Walleijb26e0f52007-04-24 07:40:04 +0000366 {"Logik LOG DAX MP3 and DAB Player", 0x13d1, 0x7002, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleijd2ff9e32007-03-16 15:27:45 +0000367
368 /*
Linus Walleijedb45332007-04-26 18:54:47 +0000369 * RCA / Thomson
370 */
371 {"Thomson Opal / RCA Lyra MC4002", 0x069b, 0x0777, DEVICE_FLAG_NONE },
Richard Low1869d272007-04-28 13:55:17 +0000372
373 /*
374 * NTT DoCoMo
375 */
Linus Walleij41b2bb82007-05-08 18:23:31 +0000376 {"FOMA F903iX HIGH-SPEED", 0x04c5, 0x1140, DEVICE_FLAG_NONE },
Richard Low6711f442007-05-05 19:00:59 +0000377
Linus Walleij41b2bb82007-05-08 18:23:31 +0000378 /*
Linus Walleijb2c1b482007-05-09 21:43:54 +0000379 * Palm device userland program named Pocket Tunes
Linus Walleij41b2bb82007-05-08 18:23:31 +0000380 * Reported by Peter Gyongyosi <gyp@impulzus.com>
381 */
Linus Walleijb2c1b482007-05-09 21:43:54 +0000382 {"Palm / Handspring Pocket Tunes", 0x1703, 0x0001, DEVICE_FLAG_NONE },
Linus Walleijd1d60ee2007-09-03 23:51:53 +0000383 // Reported by anonymous submission
384 {"Palm / Handspring Pocket Tunes 4", 0x1703, 0x0002, DEVICE_FLAG_NONE },
Linus Walleij41b2bb82007-05-08 18:23:31 +0000385
386 /*
Linus Walleij1ca24c52007-05-10 19:53:17 +0000387 * TrekStor devices
388 * Their datasheet claims their devices are dualmode so probably needs to
389 * unload the attached drivers here.
390 */
Linus Walleij03f6f272007-08-03 23:07:22 +0000391 // Reported by Cristi Magherusan <majeru@gentoo.ro>
392 {"TrekStor Vibez i.Beat sweez FM", 0x0402, 0x0611, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij1ca24c52007-05-10 19:53:17 +0000393 // Reported by Stefan Voss <svoss@web.de>
394 {"TrekStor Vibez 8/12GB", 0x066f, 0x842a, DEVICE_FLAG_UNLOAD_DRIVER },
Richard Low14afeb72007-07-13 19:11:56 +0000395
396 /*
397 * Disney (have had no reports of this actually working.)
398 */
399 // Reported by XNJB user
400 {"Disney MixMax", 0x0aa6, 0x6021, DEVICE_FLAG_NONE },
Linus Walleij1ca24c52007-05-10 19:53:17 +0000401
402 /*
Linus Walleije1965542007-07-27 06:23:01 +0000403 * Cowon Systems, Inc.
404 * The iAudio audiophile devices don't encourage the use of MTP.
405 */
Linus Walleijd3b78572007-08-24 21:28:24 +0000406 // Reported by Patrik Johansson <Patrik.Johansson@qivalue.com>
407 {"Cowon iAudio U3 (MTP mode)", 0x0e21, 0x0701, DEVICE_FLAG_NONE },
Linus Walleijec9a8a32007-08-04 06:03:05 +0000408 // Reported by Roberth Karman
409 {"Cowon iAudio 7 (MTP mode)", 0x0e21, 0x0751, DEVICE_FLAG_NONE },
410 // Reported by TJ Something <tjbk_tjb@users.sourceforge.net>
Linus Walleij6bf68b42007-09-03 22:50:02 +0000411 {"Cowon iAudio D2 (MTP mode)", 0x0e21, 0x0801,
412 DEVICE_FLAG_UNLOAD_DRIVER | DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL },
Linus Walleije1965542007-07-27 06:23:01 +0000413
414 /*
Linus Walleij7f9d0752007-08-21 19:19:47 +0000415 * Insignia, dual-mode.
416 */
417 { "Insignia NS-DV45", 0x19ff, 0x0303, DEVICE_FLAG_UNLOAD_DRIVER },
418
419 /*
Linus Walleijd1d60ee2007-09-03 23:51:53 +0000420 * LG Electronics
421 */
422 // Not verified - anonymous submission
423 { "LG UP3", 0x043e, 0x70b1, DEVICE_FLAG_NONE },
Richard Lowe9f205f2007-09-16 16:25:50 +0000424
Linus Walleijd1d60ee2007-09-03 23:51:53 +0000425 /*
Linus Walleij41b2bb82007-05-08 18:23:31 +0000426 * Other strange stuff.
427 */
428 {"Isabella's prototype", 0x0b20, 0xddee, DEVICE_FLAG_NONE }
Linus Walleija5483642006-03-09 09:20:38 +0000429};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000430static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000431
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000432int ptpcam_usb_timeout = USB_TIMEOUT;
433
Linus Walleijd6a49972006-05-02 08:24:54 +0000434// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000435static struct usb_bus* init_usb();
Linus Walleijb0ab5482007-08-29 21:08:54 +0000436static void close_usb(PTP_USB* ptp_usb);
437static void find_interface_and_endpoints(struct usb_device *dev,
438 uint8_t *interface,
439 int* inep,
440 int* inep_maxpacket,
441 int* outep,
442 int* outep_maxpacket,
443 int* intep);
Linus Walleijd6a49972006-05-02 08:24:54 +0000444static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000445static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000446static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000447static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000448static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
449static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000450
Linus Walleij552ba322007-01-22 11:49:59 +0000451/**
452 * Get a list of the supported USB devices.
453 *
454 * The developers depend on users of this library to constantly
455 * add in to the list of supported devices. What we need is the
456 * device name, USB Vendor ID (VID) and USB Product ID (PID).
457 * put this into a bug ticket at the project homepage, please.
458 * The VID/PID is used to let e.g. udev lift the device to
459 * console userspace access when it's plugged in.
460 *
461 * @param devices a pointer to a pointer that will hold a device
462 * list after the call to this function, if it was
463 * successful.
464 * @param numdevs a pointer to an integer that will hold the number
465 * of devices in the device list if the call was successful.
466 * @return 0 if the list was successfull retrieved, any other
467 * value means failure.
468 */
469int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000470{
471 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
472 *numdevs = mtp_device_table_size;
473 return 0;
474}
475
Linus Walleij552ba322007-01-22 11:49:59 +0000476
Linus Walleij1fd2d272006-05-08 09:22:01 +0000477static struct usb_bus* init_usb()
478{
479 usb_init();
480 usb_find_busses();
481 usb_find_devices();
482 return (usb_get_busses());
483}
484
485/**
tedbullock0f033cb2007-02-14 20:56:54 +0000486 * Small recursive function to append a new usb_device to the linked list of
487 * USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000488 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000489 * properties.
490 * @param next New USB MTP device to be added to list
491 * @return nothing
492 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000493static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
494 struct usb_device *newdevice)
tedbullock0f033cb2007-02-14 20:56:54 +0000495{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000496 mtpdevice_list_t *new_list_entry;
tedbullock0f033cb2007-02-14 20:56:54 +0000497
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000498 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
499 if (new_list_entry == NULL) {
500 return NULL;
501 }
502 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
503 new_list_entry->libusb_device = newdevice;
504 new_list_entry->next = NULL;
505
506 if (devlist == NULL) {
507 return new_list_entry;
508 } else {
509 mtpdevice_list_t *tmp = devlist;
510 while (tmp->next != NULL) {
511 tmp = tmp->next;
512 }
513 tmp->next = new_list_entry;
514 }
515 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000516}
517
518/**
519 * Small recursive function to free dynamic memory allocated to the linked list
520 * of USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000521 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000522 * properties.
523 * @return nothing
524 */
Linus Walleij45a86372007-03-07 09:36:19 +0000525void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000526{
Linus Walleijac061862007-03-07 08:28:33 +0000527 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000528
529 if (devlist == NULL)
530 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000531 while (tmplist != NULL) {
532 mtpdevice_list_t *tmp = tmplist;
533 tmplist = tmplist->next;
Linus Walleij45a86372007-03-07 09:36:19 +0000534 // Do not free() the fields (ptp_usb, parms)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000535 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000536 }
tedbullock0f033cb2007-02-14 20:56:54 +0000537 return;
538}
539
540/**
Linus Walleij9521e2b2007-07-10 21:50:42 +0000541 * This checks if a device has an MTP descriptor. The descriptor was
542 * elaborated about in gPhoto bug 1482084, and some official documentation
543 * with no strings attached was published by Microsoft at
544 * http://www.microsoft.com/whdc/system/bus/USB/USBFAQ_intermed.mspx#E3HAC
545 *
Linus Walleija4942fc2007-03-12 19:23:21 +0000546 * @param dev a device struct from libusb.
Linus Walleij9521e2b2007-07-10 21:50:42 +0000547 * @param dumpfile set to non-NULL to make the descriptors dump out
548 * to this file in human-readable hex so we can scruitinze them.
Linus Walleija4942fc2007-03-12 19:23:21 +0000549 * @return 1 if the device is MTP compliant, 0 if not.
550 */
Linus Walleij9521e2b2007-07-10 21:50:42 +0000551static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
Linus Walleija4942fc2007-03-12 19:23:21 +0000552{
553 usb_dev_handle *devh;
554 unsigned char buf[1024], cmd;
555 int ret;
556
557 /* Don't examine hubs (no point in that) */
558 if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) {
559 return 0;
560 }
561
562 /* Attempt to open Device on this port */
563 devh = usb_open(dev);
564 if (devh == NULL) {
565 /* Could not open this device */
566 return 0;
567 }
568
569 /* Read the special descriptor */
570 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
Linus Walleij9521e2b2007-07-10 21:50:42 +0000571
572 // Dump it, if requested
573 if (dumpfile != NULL && ret > 0) {
574 fprintf(dumpfile, "Microsoft device descriptor 0xee:\n");
575 data_dump_ascii(dumpfile, buf, ret, 16);
576 }
Linus Walleija4942fc2007-03-12 19:23:21 +0000577
578 /* Check if descriptor length is at least 10 bytes */
579 if (ret < 10) {
580 usb_close(devh);
581 return 0;
582 }
583
584 /* Check if this device has a Microsoft Descriptor */
585 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
586 (buf[6] == 'F') && (buf[8] == 'T'))) {
587 usb_close(devh);
588 return 0;
589 }
590
591 /* Check if device responds to control message 1 or if there is an error */
592 cmd = buf[16];
593 ret = usb_control_msg (devh,
594 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
595 cmd,
596 0,
597 4,
598 (char *) buf,
599 sizeof(buf),
600 1000);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000601
602 // Dump it, if requested
603 if (dumpfile != NULL && ret > 0) {
604 fprintf(dumpfile, "Microsoft device response to control message 1, CMD 0x%02x:\n", cmd);
605 data_dump_ascii(dumpfile, buf, ret, 16);
606 }
Linus Walleija4942fc2007-03-12 19:23:21 +0000607
608 /* If this is true, the device either isn't MTP or there was an error */
609 if (ret <= 0x15) {
610 /* TODO: If there was an error, flag it and let the user know somehow */
611 /* if(ret == -1) {} */
612 usb_close(devh);
613 return 0;
614 }
615
616 /* Check if device is MTP or if it is something like a USB Mass Storage
617 device with Janus DRM support */
618 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
619 usb_close(devh);
620 return 0;
621 }
622
623 /* After this point we are probably dealing with an MTP device */
624
625 /* Check if device responds to control message 2 or if there is an error*/
626 ret = usb_control_msg (devh,
627 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
628 cmd,
629 0,
630 5,
631 (char *) buf,
632 sizeof(buf),
633 1000);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000634
635 // Dump it, if requested
636 if (dumpfile != NULL && ret > 0) {
637 fprintf(dumpfile, "Microsoft device response to control message 2, CMD 0x%02x:\n", cmd);
638 data_dump_ascii(dumpfile, buf, ret, 16);
639 }
Linus Walleija4942fc2007-03-12 19:23:21 +0000640
641 /* If this is true, the device errored against control message 2 */
642 if (ret == -1) {
643 /* TODO: Implement callback function to let managing program know there
644 was a problem, along with description of the problem */
645 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
646 "ProductID:%04x encountered an error responding to "
647 "control message 2.\n"
648 "Problems may arrise but continuing\n",
649 dev->descriptor.idVendor, dev->descriptor.idProduct);
650 } else if (ret <= 0x15) {
651 /* TODO: Implement callback function to let managing program know there
652 was a problem, along with description of the problem */
653 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
654 "ProductID:%04x responded to control message 2 with a "
655 "response that was too short. Problems may arrise but "
656 "continuing\n",
657 dev->descriptor.idVendor, dev->descriptor.idProduct);
658 } else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
659 /* TODO: Implement callback function to let managing program know there
660 was a problem, along with description of the problem */
661 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
662 "ProductID:%04x encountered an error responding to "
663 "control message 2\n"
664 "Problems may arrise but continuing\n",
665 dev->descriptor.idVendor, dev->descriptor.idProduct);
666 }
667
668 /* Close the USB device handle */
669 usb_close(devh);
670 return 1;
671}
672
673/**
674 * This function scans through the connected usb devices on a machine and
675 * if they match known Vendor and Product identifiers appends them to the
676 * dynamic array mtp_device_list. Be sure to call
677 * <code>free(mtp_device_list)</code> when you are done with it, assuming it
678 * is not NULL.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000679 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000680 * properties (if this list is not empty, new entries will be appended
681 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000682 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
683 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no devices have
684 * been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000685 */
Linus Walleij45a86372007-03-07 09:36:19 +0000686static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000687{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000688 struct usb_bus *bus = init_usb();
Richard Lowe9f205f2007-09-16 16:25:50 +0000689 int found = 0;
690 int i;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000691
692 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000693 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000694 for (; dev != NULL; dev = dev->next) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000695 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) {
696 found = 0;
697 for(i = 0; i < mtp_device_table_size; i++) {
698 if(dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
699 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
700 /* Append this usb device to the MTP device list */
701 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
702 found = 1;
703 break;
704 }
705 }
706 if (!found) {
707 printf("probing device descriptor\n");
708 if (probe_device_descriptor(dev, NULL)) {
709 /* Append this usb device to the MTP USB Device List */
710 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
711 }
712 }
713 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000714 }
715 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000716
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000717 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000718 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000719 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000720 }
721 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000722}
723
724/**
Linus Walleij1fd2d272006-05-08 09:22:01 +0000725 * Detect the MTP device descriptor and return the VID and PID
726 * of the first device found. This is a very low-level function
727 * which is intended for use with <b>udev</b> or other hotplug
728 * mechanisms. The idea is that a script may want to know if the
729 * just plugged-in device was an MTP device or not.
tedbullock2b45f2e2007-02-23 20:16:37 +0000730 *
Linus Walleij1fd2d272006-05-08 09:22:01 +0000731 * @param vid the Vendor ID (VID) of the first device found.
732 * @param pid the Product ID (PID) of the first device found.
733 * @return the number of detected devices or -1 if the call
734 * was unsuccessful.
735 */
736int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
737{
Linus Walleijb6b69132007-03-07 09:45:05 +0000738 mtpdevice_list_t *devlist;
739 LIBMTP_error_number_t ret;
740
741 ret = get_mtp_usb_device_list(&devlist);
742 if (ret != LIBMTP_ERROR_NONE) {
743 *vid = *pid = 0;
744 return -1;
745 }
746 *vid = devlist->libusb_device->descriptor.idVendor;
747 *pid = devlist->libusb_device->descriptor.idProduct;
748 free_mtpdevice_list(devlist);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000749 return 1;
750}
751
Linus Walleijc6210fb2006-05-08 11:11:41 +0000752/**
753 * This routine just dumps out low-level
754 * USB information about the current device.
755 * @param ptp_usb the USB device to get information from.
756 */
757void dump_usbinfo(PTP_USB *ptp_usb)
758{
759 int res;
760 struct usb_device *dev;
761
762#ifdef LIBUSB_HAS_GET_DRIVER_NP
763 char devname[0x10];
764
765 devname[0] = '\0';
Linus Walleijb0ab5482007-08-29 21:08:54 +0000766 res = usb_get_driver_np(ptp_usb->handle, (int) ptp_usb->interface, devname, sizeof(devname));
Linus Walleijc6210fb2006-05-08 11:11:41 +0000767 if (devname[0] != '\0') {
768 printf(" Using kernel interface \"%s\"\n", devname);
769 }
770#endif
771 dev = usb_device(ptp_usb->handle);
772 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
773 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
774 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
775 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000776 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
777 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000778 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
779 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000780 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000781 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
Linus Walleij9521e2b2007-07-10 21:50:42 +0000782 (void) probe_device_descriptor(dev, stdout);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000783}
784
Linus Walleija0323272007-01-07 12:21:30 +0000785static void
786ptp_debug (PTPParams *params, const char *format, ...)
787{
788 va_list args;
789
790 va_start (args, format);
791 if (params->debug_func!=NULL)
792 params->debug_func (params->data, format, args);
793 else
794 {
795 vfprintf (stderr, format, args);
796 fprintf (stderr,"\n");
797 fflush (stderr);
798 }
799 va_end (args);
800}
801
802static void
803ptp_error (PTPParams *params, const char *format, ...)
804{
805 va_list args;
806
807 va_start (args, format);
808 if (params->error_func!=NULL)
809 params->error_func (params->data, format, args);
810 else
811 {
812 vfprintf (stderr, format, args);
813 fprintf (stderr,"\n");
814 fflush (stderr);
815 }
816 va_end (args);
817}
818
Linus Walleijc6210fb2006-05-08 11:11:41 +0000819
Linus Walleij99ed83c2007-01-02 18:46:02 +0000820/*
821 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000822 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000823 * Much reading packet logs and having fun with trials and errors
824 * reveals that WMP / Windows is probably using an algorithm like this
825 * for large transfers:
826 *
827 * 1. Send the command (0x0c bytes) if headers are split, else, send
828 * command plus sizeof(endpoint) - 0x0c bytes.
829 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
830 * split headers. Else goto 3.
831 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
832 * We call 0x10000 CONTEXT_BLOCK_SIZE.
833 * 4. Send remaining bytes MOD sizeof(endpoint)
834 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
835 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000836 *
837 * Further there is some special quirks to handle zero reads from the
838 * device, since some devices can't do them at all due to shortcomings
839 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000840 */
Richard Low4fd59d62007-03-03 16:34:37 +0000841#define CONTEXT_BLOCK_SIZE_1 0x3e00
842#define CONTEXT_BLOCK_SIZE_2 0x200
843#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000844static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000845ptp_read_func (
846 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000847 unsigned long *readbytes,
848 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000849) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000850 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000851 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000852 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000853 unsigned long curread = 0;
854 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000855 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000856 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000857
858 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000859 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000860 while (curread < size) {
Richard Low4fd59d62007-03-03 16:34:37 +0000861
862#ifdef ENABLE_USB_BULK_DEBUG
863 printf("Remaining size to read: 0x%04x bytes\n", size - curread);
864#endif
865 // check equal to condition here
866 if (size - curread < CONTEXT_BLOCK_SIZE)
867 {
868 // this is the last packet
869 toread = size - curread;
870 // this is equivalent to zero read for these devices
Linus Walleij049f50c2007-03-03 20:30:43 +0000871 if (readzero && ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000872 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000873 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000874 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000875 }
Richard Low4fd59d62007-03-03 16:34:37 +0000876 else if (curread == 0)
877 // we are first packet, but not last packet
878 toread = CONTEXT_BLOCK_SIZE_1;
879 else if (toread == CONTEXT_BLOCK_SIZE_1)
880 toread = CONTEXT_BLOCK_SIZE_2;
881 else if (toread == CONTEXT_BLOCK_SIZE_2)
882 toread = CONTEXT_BLOCK_SIZE_1;
883 else
Linus Walleij4cec9872007-03-03 21:00:53 +0000884 printf("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
885 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000886
Richard Lowd284f072007-02-17 08:52:41 +0000887#ifdef ENABLE_USB_BULK_DEBUG
888 printf("Reading in 0x%04x bytes\n", toread);
889#endif
Linus Walleij784ac3f2006-12-29 10:36:51 +0000890 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout);
Richard Lowd284f072007-02-17 08:52:41 +0000891#ifdef ENABLE_USB_BULK_DEBUG
892 printf("Result of read: 0x%04x\n", result);
893#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000894
Linus Walleijc49c6372007-01-09 00:18:51 +0000895 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000896 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000897 }
Linus Walleije78de6d2006-10-31 14:46:36 +0000898#ifdef ENABLE_USB_BULK_DEBUG
899 printf("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000900 if (result == 0)
901 printf("Zero Read\n");
902 else
903 data_dump_ascii (stdout,bytes,result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000904#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000905
906 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000907 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000908 {
909#ifdef ENABLE_USB_BULK_DEBUG
910 printf("<==USB IN\nDiscarding extra byte\n");
911#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000912 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000913 }
Richard Low4fd59d62007-03-03 16:34:37 +0000914
Linus Walleijc49c6372007-01-09 00:18:51 +0000915 handler->putfunc(NULL, handler->private, result, bytes, &written);
Richard Lowbf2675c2007-03-18 18:20:26 +0000916
Linus Walleijc49c6372007-01-09 00:18:51 +0000917 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000918 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000919
920 // Increase counters, call callback
921 if (ptp_usb->callback_active) {
922 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
923 // send last update and disable callback.
924 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
925 ptp_usb->callback_active = 0;
926 }
927 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000928 int ret;
929 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
930 ptp_usb->current_transfer_total,
931 ptp_usb->current_transfer_callback_data);
932 if (ret != 0) {
933 return PTP_ERROR_CANCEL;
934 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000935 }
936 }
937
Linus Walleijd6a49972006-05-02 08:24:54 +0000938 if (result < toread) /* short reads are common */
939 break;
940 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000941 if (readbytes) *readbytes = curread;
942 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000943
Richard Low4df32f82007-01-11 20:04:39 +0000944 // there might be a zero packet waiting for us...
Linus Walleij049f50c2007-03-03 20:30:43 +0000945 if (readzero &&
946 !(ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS) &&
947 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000948 char temp;
949 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000950
Richard Low4df32f82007-01-11 20:04:39 +0000951#ifdef ENABLE_USB_BULK_DEBUG
952 printf("<==USB IN\n");
953 printf("Zero Read\n");
954#endif
955 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptpcam_usb_timeout);
956 if (zeroresult != 0)
957 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
958 }
959
Linus Walleijd6a49972006-05-02 08:24:54 +0000960 if (result > 0) {
Linus Walleijd6a49972006-05-02 08:24:54 +0000961 return (PTP_RC_OK);
962 } else {
963 return PTP_ERROR_IO;
964 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000965}
966
Linus Walleijb02a0662006-04-25 08:05:09 +0000967static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000968ptp_write_func (
969 unsigned long size,
970 PTPDataHandler *handler,
971 void *data,
972 unsigned long *written
973) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000974 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000975 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000976 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000977 unsigned long curwrite = 0;
978 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000979
980 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000981 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000982 if (!bytes) {
983 return PTP_ERROR_IO;
984 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000985 while (curwrite < size) {
986 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000987 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000988 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000989 } else {
990 // This magic makes packets the same size that WMP send them.
991 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000992 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000993 }
994 }
995 handler->getfunc(NULL, handler->private,towrite,bytes,&towrite);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000996 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000997#ifdef ENABLE_USB_BULK_DEBUG
998 printf("USB OUT==>\n");
Richard Lowedd61832006-12-30 12:38:18 +0000999 data_dump_ascii (stdout,bytes,towrite,16);
Linus Walleije78de6d2006-10-31 14:46:36 +00001000#endif
Linus Walleijc49c6372007-01-09 00:18:51 +00001001 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +00001002 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +00001003 }
1004 // Increase counters
1005 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +00001006 curwrite += result;
Linus Walleijc49c6372007-01-09 00:18:51 +00001007
1008 // call callback
1009 if (ptp_usb->callback_active) {
1010 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
1011 // send last update and disable callback.
1012 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
1013 ptp_usb->callback_active = 0;
1014 }
1015 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001016 int ret;
1017 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
1018 ptp_usb->current_transfer_total,
1019 ptp_usb->current_transfer_callback_data);
1020 if (ret != 0) {
1021 return PTP_ERROR_CANCEL;
1022 }
Linus Walleijc49c6372007-01-09 00:18:51 +00001023 }
1024 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001025 if (result < towrite) /* short writes happen */
1026 break;
1027 }
Linus Walleij784ac3f2006-12-29 10:36:51 +00001028 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +00001029 if (written) {
1030 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +00001031 }
Linus Walleijd214b9b2006-08-26 22:08:37 +00001032
Linus Walleijc49c6372007-01-09 00:18:51 +00001033
Richard Lowef05b892007-01-03 21:18:56 +00001034 // If this is the last transfer send a zero write if required
1035 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +00001036 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
Richard Low021421e2007-01-01 18:08:57 +00001037#ifdef ENABLE_USB_BULK_DEBUG
1038 printf("USB OUT==>\n");
1039 printf("Zero Write\n");
1040#endif
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001041 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001042 }
Linus Walleijd214b9b2006-08-26 22:08:37 +00001043 }
Linus Walleijc49c6372007-01-09 00:18:51 +00001044
Linus Walleijd6a49972006-05-02 08:24:54 +00001045 if (result < 0)
1046 return PTP_ERROR_IO;
1047 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001048}
1049
Linus Walleija0323272007-01-07 12:21:30 +00001050/* memory data get/put handler */
1051typedef struct {
1052 unsigned char *data;
1053 unsigned long size, curoff;
1054} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +00001055
Linus Walleija0323272007-01-07 12:21:30 +00001056static uint16_t
1057memory_getfunc(PTPParams* params, void* private,
1058 unsigned long wantlen, unsigned char *data,
1059 unsigned long *gotlen
1060) {
1061 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1062 unsigned long tocopy = wantlen;
1063
1064 if (priv->curoff + tocopy > priv->size)
1065 tocopy = priv->size - priv->curoff;
1066 memcpy (data, priv->data + priv->curoff, tocopy);
1067 priv->curoff += tocopy;
1068 *gotlen = tocopy;
1069 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001070}
1071
Linus Walleija0323272007-01-07 12:21:30 +00001072static uint16_t
1073memory_putfunc(PTPParams* params, void* private,
1074 unsigned long sendlen, unsigned char *data,
1075 unsigned long *putlen
1076) {
1077 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1078
1079 if (priv->curoff + sendlen > priv->size) {
1080 priv->data = realloc (priv->data, priv->curoff+sendlen);
1081 priv->size = priv->curoff + sendlen;
1082 }
1083 memcpy (priv->data + priv->curoff, data, sendlen);
1084 priv->curoff += sendlen;
1085 *putlen = sendlen;
1086 return PTP_RC_OK;
1087}
1088
1089/* init private struct for receiving data. */
1090static uint16_t
1091ptp_init_recv_memory_handler(PTPDataHandler *handler) {
1092 PTPMemHandlerPrivate* priv;
1093 priv = malloc (sizeof(PTPMemHandlerPrivate));
1094 handler->private = priv;
1095 handler->getfunc = memory_getfunc;
1096 handler->putfunc = memory_putfunc;
1097 priv->data = NULL;
1098 priv->size = 0;
1099 priv->curoff = 0;
1100 return PTP_RC_OK;
1101}
1102
1103/* init private struct and put data in for sending data.
1104 * data is still owned by caller.
1105 */
1106static uint16_t
1107ptp_init_send_memory_handler(PTPDataHandler *handler,
1108 unsigned char *data, unsigned long len
1109) {
1110 PTPMemHandlerPrivate* priv;
1111 priv = malloc (sizeof(PTPMemHandlerPrivate));
1112 if (!priv)
1113 return PTP_RC_GeneralError;
1114 handler->private = priv;
1115 handler->getfunc = memory_getfunc;
1116 handler->putfunc = memory_putfunc;
1117 priv->data = data;
1118 priv->size = len;
1119 priv->curoff = 0;
1120 return PTP_RC_OK;
1121}
1122
1123/* free private struct + data */
1124static uint16_t
1125ptp_exit_send_memory_handler (PTPDataHandler *handler) {
1126 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
1127 /* data is owned by caller */
1128 free (priv);
1129 return PTP_RC_OK;
1130}
1131
1132/* hand over our internal data to caller */
1133static uint16_t
1134ptp_exit_recv_memory_handler (PTPDataHandler *handler,
1135 unsigned char **data, unsigned long *size
1136) {
1137 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
1138 *data = priv->data;
1139 *size = priv->size;
1140 free (priv);
1141 return PTP_RC_OK;
1142}
1143
1144/* send / receive functions */
1145
1146uint16_t
1147ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
1148{
1149 uint16_t ret;
1150 PTPUSBBulkContainer usbreq;
1151 PTPDataHandler memhandler;
1152 unsigned long written, towrite;
1153
1154 /* build appropriate USB container */
1155 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
1156 (sizeof(uint32_t)*(5-req->Nparam)));
1157 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
1158 usbreq.code=htod16(req->Code);
1159 usbreq.trans_id=htod32(req->Transaction_ID);
1160 usbreq.payload.params.param1=htod32(req->Param1);
1161 usbreq.payload.params.param2=htod32(req->Param2);
1162 usbreq.payload.params.param3=htod32(req->Param3);
1163 usbreq.payload.params.param4=htod32(req->Param4);
1164 usbreq.payload.params.param5=htod32(req->Param5);
1165 /* send it to responder */
1166 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
1167 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
1168 ret=ptp_write_func(
1169 towrite,
1170 &memhandler,
1171 params->data,
1172 &written
1173 );
1174 ptp_exit_send_memory_handler (&memhandler);
Linus Walleijff01cb12007-09-16 19:49:48 +00001175 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) {
Linus Walleija0323272007-01-07 12:21:30 +00001176 ret = PTP_ERROR_IO;
Linus Walleija0323272007-01-07 12:21:30 +00001177 }
Linus Walleij724f0102007-09-17 20:10:12 +00001178 if (written != towrite && ret != PTP_ERROR_CANCEL && ret != PTP_ERROR_IO) {
Linus Walleija0323272007-01-07 12:21:30 +00001179 ptp_error (params,
1180 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001181 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001182 );
1183 ret = PTP_ERROR_IO;
1184 }
1185 return ret;
1186}
1187
1188uint16_t
1189ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1190 unsigned long size, PTPDataHandler *handler
1191) {
1192 uint16_t ret;
1193 int wlen, datawlen;
1194 unsigned long written;
1195 PTPUSBBulkContainer usbdata;
1196 uint32_t bytes_left_to_transfer;
1197 PTPDataHandler memhandler;
1198
1199 /* build appropriate USB container */
1200 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1201 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1202 usbdata.code = htod16(ptp->Code);
1203 usbdata.trans_id= htod32(ptp->Transaction_ID);
1204
1205 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001206 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001207
1208 if (params->split_header_data) {
1209 datawlen = 0;
1210 wlen = PTP_USB_BULK_HDR_LEN;
1211 } else {
1212 unsigned long gotlen;
1213 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001214 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001215 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
1216 ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen);
1217 if (ret != PTP_RC_OK)
1218 return ret;
1219 if (gotlen != datawlen)
1220 return PTP_RC_GeneralError;
1221 }
1222 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1223 /* send first part of data */
1224 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1225 ptp_exit_send_memory_handler (&memhandler);
1226 if (ret!=PTP_RC_OK) {
Linus Walleija0323272007-01-07 12:21:30 +00001227 return ret;
1228 }
1229 if (size <= datawlen) return ret;
1230 /* if everything OK send the rest */
1231 bytes_left_to_transfer = size-datawlen;
1232 ret = PTP_RC_OK;
1233 while(bytes_left_to_transfer > 0) {
1234 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1235 if (ret != PTP_RC_OK)
1236 break;
1237 if (written == 0) {
1238 ret = PTP_ERROR_IO;
1239 break;
1240 }
1241 bytes_left_to_transfer -= written;
1242 }
Linus Walleijff01cb12007-09-16 19:49:48 +00001243 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL)
Linus Walleija0323272007-01-07 12:21:30 +00001244 ret = PTP_ERROR_IO;
1245 return ret;
1246}
1247
1248static uint16_t ptp_usb_getpacket(PTPParams *params,
1249 PTPUSBBulkContainer *packet, unsigned long *rlen)
1250{
1251 PTPDataHandler memhandler;
1252 uint16_t ret;
1253 unsigned char *x = NULL;
1254
1255 /* read the header and potentially the first data */
1256 if (params->response_packet_size > 0) {
1257 /* If there is a buffered packet, just use it. */
1258 memcpy(packet, params->response_packet, params->response_packet_size);
1259 *rlen = params->response_packet_size;
1260 free(params->response_packet);
1261 params->response_packet = NULL;
1262 params->response_packet_size = 0;
1263 /* Here this signifies a "virtual read" */
1264 return PTP_RC_OK;
1265 }
1266 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001267 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001268 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1269 if (x) {
1270 memcpy (packet, x, *rlen);
1271 free (x);
1272 }
1273 return ret;
1274}
1275
1276uint16_t
1277ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1278{
1279 uint16_t ret;
1280 PTPUSBBulkContainer usbdata;
Linus Walleija0323272007-01-07 12:21:30 +00001281 unsigned long written;
1282
1283 memset(&usbdata,0,sizeof(usbdata));
1284 do {
1285 unsigned long len, rlen;
1286
1287 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1288 if (ret!=PTP_RC_OK) {
1289 ret = PTP_ERROR_IO;
1290 break;
1291 } else
1292 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1293 ret = PTP_ERROR_DATA_EXPECTED;
1294 break;
1295 } else
1296 if (dtoh16(usbdata.code)!=ptp->Code) {
1297 ret = dtoh16(usbdata.code);
1298 break;
1299 }
1300 if (usbdata.length == 0xffffffffU) {
1301 /* stuff data directly to passed data handler */
1302 while (1) {
1303 unsigned long readdata;
1304 int xret;
1305
1306 xret = ptp_read_func(
Richard Low02724f62007-02-17 09:47:26 +00001307 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
Linus Walleija0323272007-01-07 12:21:30 +00001308 handler,
1309 params->data,
Richard Low4df32f82007-01-11 20:04:39 +00001310 &readdata,
Linus Walleijc0a11432007-03-02 21:21:18 +00001311 0
Linus Walleija0323272007-01-07 12:21:30 +00001312 );
Linus Walleijff01cb12007-09-16 19:49:48 +00001313 if (xret != PTP_RC_OK)
1314 return ret;
Richard Low02724f62007-02-17 09:47:26 +00001315 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
Linus Walleija0323272007-01-07 12:21:30 +00001316 break;
1317 }
1318 return PTP_RC_OK;
1319 }
1320 if (rlen > dtoh32(usbdata.length)) {
1321 /*
1322 * Buffer the surplus response packet if it is >=
1323 * PTP_USB_BULK_HDR_LEN
1324 * (i.e. it is probably an entire package)
1325 * else discard it as erroneous surplus data.
1326 * This will even work if more than 2 packets appear
1327 * in the same transaction, they will just be handled
1328 * iteratively.
1329 *
1330 * Marcus observed stray bytes on iRiver devices;
1331 * these are still discarded.
1332 */
1333 unsigned int packlen = dtoh32(usbdata.length);
1334 unsigned int surplen = rlen - packlen;
1335
1336 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1337 params->response_packet = malloc(surplen);
1338 memcpy(params->response_packet,
1339 (uint8_t *) &usbdata + packlen, surplen);
1340 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001341 /* Ignore reading one extra byte if device flags have been set */
1342 } else if( !(((PTP_USB *)params->data)->device_flags &
1343 DEVICE_FLAG_NO_ZERO_READS &&
1344 rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001345 ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
1346 "too much, expect problems!",
1347 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001348 }
1349 rlen = packlen;
1350 }
1351
1352 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1353 * here. For MTP devices splitting header and data it might
1354 * be 12.
1355 */
1356 /* Evaluate full data length. */
1357 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1358
1359 /* autodetect split header/data MTP devices */
1360 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1361 params->split_header_data = 1;
1362
Linus Walleija0323272007-01-07 12:21:30 +00001363 /* Copy first part of data to 'data' */
1364 handler->putfunc(
1365 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1366 &written
1367 );
Richard Low4fd59d62007-03-03 16:34:37 +00001368
Linus Walleij049f50c2007-03-03 20:30:43 +00001369 if (((PTP_USB *)params->data)->device_flags & DEVICE_FLAG_NO_ZERO_READS &&
1370 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
Richard Low057ea772007-03-24 13:37:38 +00001371#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleij049f50c2007-03-03 20:30:43 +00001372 printf("Reading in extra terminating byte\n");
Richard Low4fd59d62007-03-03 16:34:37 +00001373#endif
Linus Walleij049f50c2007-03-03 20:30:43 +00001374 // need to read in extra byte and discard it
1375 int result = 0;
1376 char byte = 0;
1377 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1378 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptpcam_usb_timeout);
1379
1380 if (result != 1)
1381 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 +00001382 } else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ && params->split_header_data == 0) {
1383#ifdef ENABLE_USB_BULK_DEBUG
1384 printf("Reading in zero packet after header\n");
1385#endif
1386 int zeroresult = 0;
1387 char zerobyte = 0;
1388 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1389 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptpcam_usb_timeout);
1390
1391 if (zeroresult != 0)
1392 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
1393 }
1394
Linus Walleija0323272007-01-07 12:21:30 +00001395 /* Is that all of data? */
1396 if (len+PTP_USB_BULK_HDR_LEN<=rlen) break;
Linus Walleij049f50c2007-03-03 20:30:43 +00001397
1398 ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1399 handler,
1400 params->data, &rlen, 1);
1401
Linus Walleija0323272007-01-07 12:21:30 +00001402 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001403 break;
Linus Walleija0323272007-01-07 12:21:30 +00001404 }
1405 } while (0);
Linus Walleija0323272007-01-07 12:21:30 +00001406 return ret;
1407}
1408
1409uint16_t
1410ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1411{
1412 uint16_t ret;
1413 unsigned long rlen;
1414 PTPUSBBulkContainer usbresp;
1415
1416 memset(&usbresp,0,sizeof(usbresp));
1417 /* read response, it should never be longer than sizeof(usbresp) */
1418 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1419
1420 if (ret!=PTP_RC_OK) {
1421 ret = PTP_ERROR_IO;
1422 } else
1423 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1424 ret = PTP_ERROR_RESP_EXPECTED;
1425 } else
1426 if (dtoh16(usbresp.code)!=resp->Code) {
1427 ret = dtoh16(usbresp.code);
1428 }
1429 if (ret!=PTP_RC_OK) {
1430/* ptp_error (params,
1431 "PTP: request code 0x%04x getting resp error 0x%04x",
1432 resp->Code, ret);*/
1433 return ret;
1434 }
1435 /* build an appropriate PTPContainer */
1436 resp->Code=dtoh16(usbresp.code);
1437 resp->SessionID=params->session_id;
1438 resp->Transaction_ID=dtoh32(usbresp.trans_id);
1439 resp->Param1=dtoh32(usbresp.payload.params.param1);
1440 resp->Param2=dtoh32(usbresp.payload.params.param2);
1441 resp->Param3=dtoh32(usbresp.payload.params.param3);
1442 resp->Param4=dtoh32(usbresp.payload.params.param4);
1443 resp->Param5=dtoh32(usbresp.payload.params.param5);
1444 return ret;
1445}
1446
1447/* Event handling functions */
1448
1449/* PTP Events wait for or check mode */
1450#define PTP_EVENT_CHECK 0x0000 /* waits for */
1451#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1452
1453static inline uint16_t
1454ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1455{
1456 uint16_t ret;
1457 int result;
1458 unsigned long rlen;
1459 PTPUSBEventContainer usbevent;
1460 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1461
1462 memset(&usbevent,0,sizeof(usbevent));
1463
1464 if ((params==NULL) || (event==NULL))
1465 return PTP_ERROR_BADPARAM;
1466 ret = PTP_RC_OK;
1467 switch(wait) {
1468 case PTP_EVENT_CHECK:
1469 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1470 if (result==0)
1471 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1472 if (result < 0) ret = PTP_ERROR_IO;
1473 break;
1474 case PTP_EVENT_CHECK_FAST:
1475 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1476 if (result==0)
1477 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1478 if (result < 0) ret = PTP_ERROR_IO;
1479 break;
1480 default:
1481 ret=PTP_ERROR_BADPARAM;
1482 break;
1483 }
1484 if (ret!=PTP_RC_OK) {
1485 ptp_error (params,
1486 "PTP: reading event an error 0x%04x occurred", ret);
1487 return PTP_ERROR_IO;
1488 }
1489 rlen = result;
1490 if (rlen < 8) {
1491 ptp_error (params,
1492 "PTP: reading event an short read of %ld bytes occurred", rlen);
1493 return PTP_ERROR_IO;
1494 }
1495 /* if we read anything over interrupt endpoint it must be an event */
1496 /* build an appropriate PTPContainer */
1497 event->Code=dtoh16(usbevent.code);
1498 event->SessionID=params->session_id;
1499 event->Transaction_ID=dtoh32(usbevent.trans_id);
1500 event->Param1=dtoh32(usbevent.param1);
1501 event->Param2=dtoh32(usbevent.param2);
1502 event->Param3=dtoh32(usbevent.param3);
1503 return ret;
1504}
1505
1506uint16_t
1507ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1508
1509 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1510}
1511
1512uint16_t
1513ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1514
1515 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1516}
1517
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001518uint16_t
1519ptp_usb_control_cancel_request (PTPParams *params, uint32_t transactionid) {
1520 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1521 int ret;
1522 unsigned char buffer[6];
1523
1524 htod16a(&buffer[0],PTP_EC_CancelTransaction);
1525 htod32a(&buffer[2],transactionid);
1526 ret = usb_control_msg(ptp_usb->handle,
1527 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1528 0x64, 0x0000, 0x0000, (char *) buffer, sizeof(buffer), 3000);
1529 if (ret < sizeof(buffer))
1530 return PTP_ERROR_IO;
1531 return PTP_RC_OK;
1532}
Linus Walleija0323272007-01-07 12:21:30 +00001533
Linus Walleij9eb3d312006-08-04 19:25:59 +00001534static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001535{
Linus Walleijd6a49972006-05-02 08:24:54 +00001536 usb_dev_handle *device_handle;
1537
Linus Walleijd6a49972006-05-02 08:24:54 +00001538 params->error_func=NULL;
1539 params->debug_func=NULL;
1540 params->sendreq_func=ptp_usb_sendreq;
1541 params->senddata_func=ptp_usb_senddata;
1542 params->getresp_func=ptp_usb_getresp;
1543 params->getdata_func=ptp_usb_getdata;
Linus Walleijff01cb12007-09-16 19:49:48 +00001544 params->cancelreq_func=ptp_usb_control_cancel_request;
Linus Walleijd6a49972006-05-02 08:24:54 +00001545 params->data=ptp_usb;
1546 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001547 /*
1548 * This is hardcoded here since we have no devices whatsoever that are BE.
1549 * Change this the day we run into our first BE device (if ever).
1550 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001551 params->byteorder = PTP_DL_LE;
1552
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001553 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001554 if (!device_handle) {
1555 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001556 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001557 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001558 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001559#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1560 /*
1561 * If this device is known to be wrongfully claimed by other kernel
1562 * drivers (such as mass storage), then try to unload it to make it
1563 * accessible from user space.
1564 */
1565 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001566 if (usb_detach_kernel_driver_np(device_handle, (int) ptp_usb->interface)) {
Linus Walleij7beba572006-11-29 08:56:12 +00001567 // Totally ignore this error!
1568 // perror("usb_detach_kernel_driver_np()");
Linus Walleij501ba4d2006-10-16 06:17:04 +00001569 }
1570 }
1571#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001572#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001573 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001574 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1575 perror("usb_set_configuration()");
1576 return -1;
1577 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001578#endif
Linus Walleijb0ab5482007-08-29 21:08:54 +00001579 if (usb_claim_interface(device_handle, (int) ptp_usb->interface)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001580 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001581 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001582 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001583 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001584 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001585}
1586
Linus Walleijd6a49972006-05-02 08:24:54 +00001587static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001588{
Linus Walleij58b62792007-01-07 12:38:59 +00001589 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001590 int ret;
1591
1592 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001593 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001594 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001595 if (ret<0) {
1596 perror ("inep: usb_get_endpoint_status()");
1597 } else if (status) {
1598 printf("Clearing stall on IN endpoint\n");
1599 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1600 if (ret<0) {
1601 perror ("usb_clear_stall_feature()");
1602 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001603 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001604
1605 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001606 status=0;
1607 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1608 if (ret<0) {
1609 perror("outep: usb_get_endpoint_status()");
1610 } else if (status) {
1611 printf("Clearing stall on OUT endpoint\n");
1612 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1613 if (ret<0) {
1614 perror("usb_clear_stall_feature()");
1615 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001616 }
Linus Walleij58b62792007-01-07 12:38:59 +00001617
1618 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1619}
1620
1621static void clear_halt(PTP_USB* ptp_usb)
1622{
1623 int ret;
1624
1625 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1626 if (ret<0) {
1627 perror("usb_clear_halt() on IN endpoint");
1628 }
1629 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1630 if (ret<0) {
1631 perror("usb_clear_halt() on OUT endpoint");
1632 }
1633 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1634 if (ret<0) {
1635 perror("usb_clear_halt() on INTERRUPT endpoint");
1636 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001637}
1638
Linus Walleijb0ab5482007-08-29 21:08:54 +00001639static void close_usb(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001640{
Linus Walleij58b62792007-01-07 12:38:59 +00001641 // Clear any stalled endpoints
Linus Walleijd6a49972006-05-02 08:24:54 +00001642 clear_stall(ptp_usb);
Linus Walleij58b62792007-01-07 12:38:59 +00001643 // Clear halts on any endpoints
1644 clear_halt(ptp_usb);
Linus Walleijd6a49972006-05-02 08:24:54 +00001645 // Added to clear some stuff on the OUT endpoint
1646 // TODO: is this good on the Mac too?
1647 usb_resetep(ptp_usb->handle, ptp_usb->outep);
Linus Walleijb0ab5482007-08-29 21:08:54 +00001648 usb_release_interface(ptp_usb->handle, (int) ptp_usb->interface);
Linus Walleijd6a49972006-05-02 08:24:54 +00001649 // Brutally reset device
1650 // TODO: is this good on the Mac too?
1651 usb_reset(ptp_usb->handle);
1652 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001653}
1654
Linus Walleij45a86372007-03-07 09:36:19 +00001655static LIBMTP_error_number_t prime_device_memory(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001656{
Linus Walleij45a86372007-03-07 09:36:19 +00001657 mtpdevice_list_t *tmplist = devlist;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001658
Linus Walleij45a86372007-03-07 09:36:19 +00001659 while (tmplist != NULL) {
tedbullock0f033cb2007-02-14 20:56:54 +00001660 /* Allocate a parameter box */
Linus Walleij45a86372007-03-07 09:36:19 +00001661 tmplist->params = (PTPParams *) malloc(sizeof(PTPParams));
1662 tmplist->ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleijbfcb7922007-03-06 20:14:04 +00001663
tedbullock0f033cb2007-02-14 20:56:54 +00001664 /* Check for allocation Error */
Linus Walleij45a86372007-03-07 09:36:19 +00001665 if(tmplist->params == NULL || tmplist->ptp_usb == NULL) {
Linus Walleijbfcb7922007-03-06 20:14:04 +00001666 /* Error and deallocation of memory will be handled by caller. */
tedbullock0f033cb2007-02-14 20:56:54 +00001667 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1668 }
1669
tedbullock69a445b2007-02-15 07:41:04 +00001670 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleij45a86372007-03-07 09:36:19 +00001671 memset(tmplist->params, 0, sizeof(PTPParams));
1672 memset(tmplist->ptp_usb, 0, sizeof(PTP_USB));
1673 tmplist = tmplist->next;
tedbullock0f033cb2007-02-14 20:56:54 +00001674 }
tedbullock0f033cb2007-02-14 20:56:54 +00001675 return LIBMTP_ERROR_NONE;
1676}
1677
Linus Walleij45a86372007-03-07 09:36:19 +00001678static void assign_known_device_flags(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001679{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001680 int i;
Linus Walleij27c50c52007-03-06 20:34:48 +00001681 mtpdevice_list_t *tmplist = devlist;
1682 uint8_t current_device = 0;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001683
tedbullock0f033cb2007-02-14 20:56:54 +00001684 /* Search through known device list and set correct device flags */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001685 while (tmplist != NULL) {
1686 int device_known = 0;
1687
1688 for(i = 0; i < mtp_device_table_size; i++) {
1689 if(tmplist->libusb_device->descriptor.idVendor == mtp_device_table[i].vendor_id &&
1690 tmplist->libusb_device->descriptor.idProduct == mtp_device_table[i].product_id) {
1691 /* This device is known, assign the correct device flags */
1692 /* Note that ptp_usb[current_device] could potentially be NULL */
Linus Walleij45a86372007-03-07 09:36:19 +00001693 if(tmplist->ptp_usb != NULL) {
1694 tmplist->ptp_usb->device_flags = mtp_device_table[i].device_flags;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001695
1696 /*
1697 * TODO:
1698 * Preferable to not do this with #ifdef ENABLE_USB_BULK_DEBUG but there is
1699 * currently no other compile time debug option
1700 */
1701
1702 device_known = 1;
tedbullock65a05772007-03-03 22:56:56 +00001703#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001704 /* This device is known to the developers */
1705 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is a %s.\n",
1706 current_device + 1,
1707 tmplist->libusb_device->descriptor.idVendor,
1708 tmplist->libusb_device->descriptor.idProduct,
1709 mtp_device_table[i].name);
tedbullock65a05772007-03-03 22:56:56 +00001710#endif
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001711 }
1712 break;
1713 }
tedbullock0f033cb2007-02-14 20:56:54 +00001714 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001715 if (!device_known) {
1716 /* This device is unknown to the developers */
1717 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
1718 current_device + 1,
1719 tmplist->libusb_device->descriptor.idVendor,
1720 tmplist->libusb_device->descriptor.idProduct);
1721 fprintf(stderr, "Please report this VID/PID and the device model to the "
1722 "libmtp development team\n");
1723 }
1724 tmplist = tmplist->next;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001725 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001726 }
tedbullock0f033cb2007-02-14 20:56:54 +00001727}
1728
tedbullock0f033cb2007-02-14 20:56:54 +00001729
Linus Walleij45a86372007-03-07 09:36:19 +00001730static LIBMTP_error_number_t configure_usb_devices(mtpdevice_list_t *devicelist)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001731{
1732 mtpdevice_list_t *tmplist = devicelist;
1733 uint16_t ret = 0;
1734 uint8_t current_device = 0;
1735
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001736 while (tmplist != NULL) {
Linus Walleij45a86372007-03-07 09:36:19 +00001737 /* This is erroneous, there must be a PTP_USB instance that we can initialize. */
1738 if(tmplist->ptp_usb == NULL) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001739 return LIBMTP_ERROR_MEMORY_ALLOCATION;
tedbullock0f033cb2007-02-14 20:56:54 +00001740 }
1741
Linus Walleij3e418e22007-09-15 20:30:14 +00001742 /* Pointer back to params */
1743 tmplist->ptp_usb->params = tmplist->params;
1744
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001745 /* TODO: Will this always be little endian? */
Linus Walleij45a86372007-03-07 09:36:19 +00001746 tmplist->params->byteorder = PTP_DL_LE;
1747 tmplist->params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8");
1748 tmplist->params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE");
tedbullock0f033cb2007-02-14 20:56:54 +00001749
Linus Walleij45a86372007-03-07 09:36:19 +00001750 if(tmplist->params->cd_locale_to_ucs2 == (iconv_t) -1 ||
1751 tmplist->params->cd_ucs2_to_locale == (iconv_t) -1) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001752 fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
1753 "Too old stdlibc, glibc and libiconv?\n");
Linus Walleij35c0ef12007-03-06 20:49:09 +00001754 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001755 }
1756
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001757 // ep = device->config->interface->altsetting->endpoint;
1758 // no_of_ep = device->config->interface->altsetting->bNumEndpoints;
tedbullock0f033cb2007-02-14 20:56:54 +00001759
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001760 /* Assign endpoints to usbinfo... */
Linus Walleijb0ab5482007-08-29 21:08:54 +00001761 find_interface_and_endpoints(tmplist->libusb_device,
1762 &tmplist->ptp_usb->interface,
Linus Walleij45a86372007-03-07 09:36:19 +00001763 &tmplist->ptp_usb->inep,
1764 &tmplist->ptp_usb->inep_maxpacket,
1765 &tmplist->ptp_usb->outep,
1766 &tmplist->ptp_usb->outep_maxpacket,
1767 &tmplist->ptp_usb->intep);
tedbullock0f033cb2007-02-14 20:56:54 +00001768
Linus Walleij45a86372007-03-07 09:36:19 +00001769 /* Attempt to initialize this device */
1770 if (init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) < 0) {
1771 fprintf(stderr, "LIBMTP PANIC: Unable to initialize device %d\n", current_device+1);
1772 // FIXME: perhaps use "continue" to keep trying the other devices.
Linus Walleij35c0ef12007-03-06 20:49:09 +00001773 return LIBMTP_ERROR_CONNECTING;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001774 }
1775
1776 /*
1777 * This works in situations where previous bad applications
1778 * have not used LIBMTP_Release_Device on exit
1779 */
Linus Walleij45a86372007-03-07 09:36:19 +00001780 if ((ret = ptp_opensession(tmplist->params, 1)) == PTP_ERROR_IO) {
Linus Walleijf1cfc5e2007-08-29 11:14:36 +00001781 fprintf(stderr, "PTP_ERROR_IO: Trying again after re-initializing USB interface\n");
Linus Walleijb0ab5482007-08-29 21:08:54 +00001782 close_usb(tmplist->ptp_usb);
tedbullock0f033cb2007-02-14 20:56:54 +00001783
Linus Walleij45a86372007-03-07 09:36:19 +00001784 if(init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) <0) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001785 fprintf(stderr, "LIBMTP PANIC: Could not open session on device %d\n", current_device+1);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001786 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001787 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001788
1789 /* Device has been reset, try again */
Linus Walleij45a86372007-03-07 09:36:19 +00001790 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001791 }
1792
1793 /* Was the transaction id invalid? Try again */
1794 if (ret == PTP_RC_InvalidTransactionID) {
Linus Walleij45a86372007-03-07 09:36:19 +00001795 fprintf(stderr, "LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
1796 tmplist->params->transaction_id += 10;
1797 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001798 }
1799
1800 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
Linus Walleij45a86372007-03-07 09:36:19 +00001801 fprintf(stderr, "LIBMTP PANIC: Could not open session! "
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001802 "(Return code %d)\n Try to reset the device.\n",
1803 ret);
Linus Walleij45a86372007-03-07 09:36:19 +00001804 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijb0ab5482007-08-29 21:08:54 +00001805 (int) tmplist->ptp_usb->interface);
tedbullock0f033cb2007-02-14 20:56:54 +00001806 return LIBMTP_ERROR_CONNECTING;
1807 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001808
1809 /* It is permissible to call this before opening the session */
Linus Walleij45a86372007-03-07 09:36:19 +00001810 if (ptp_getdeviceinfo(tmplist->params,
1811 &tmplist->params->deviceinfo) != PTP_RC_OK) {
1812 fprintf(stderr, "LIBMTP PANIC: Could not get device info!\n");
1813 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijb0ab5482007-08-29 21:08:54 +00001814 (int) tmplist->ptp_usb->interface);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001815 return LIBMTP_ERROR_CONNECTING;
1816 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001817
1818 tmplist = tmplist->next;
1819 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001820 }
1821
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001822 /* Exit with the nice list */
1823 return LIBMTP_ERROR_NONE;
tedbullock0f033cb2007-02-14 20:56:54 +00001824}
1825
Linus Walleij550d6d52007-01-24 14:32:51 +00001826/**
1827 * This function scans through the results of the get_mtp_usb_device_list
1828 * function and attempts to connect to those devices listed using the
1829 * mtp_device_table at the top of the file. Returns a LIBMTP_error_number_t.
1830 *
Linus Walleij45a86372007-03-07 09:36:19 +00001831 * @param devlist a list of devices with primed PTP_USB and params structs.
Linus Walleij550d6d52007-01-24 14:32:51 +00001832 * @return Error Codes as per the type definition
1833 */
Linus Walleij45a86372007-03-07 09:36:19 +00001834LIBMTP_error_number_t find_usb_devices(mtpdevice_list_t **devlist)
Linus Walleij550d6d52007-01-24 14:32:51 +00001835{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001836 mtpdevice_list_t *mtp_device_list = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001837 LIBMTP_error_number_t ret;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001838
Linus Walleija4942fc2007-03-12 19:23:21 +00001839 /*
1840 * Recover list of attached USB devices that match MTP criteria, i.e.
1841 * it either has an MTP device descriptor or it is in the known
1842 * devices list.
1843 */
Linus Walleij45a86372007-03-07 09:36:19 +00001844 ret = get_mtp_usb_device_list (&mtp_device_list);
Linus Walleijac061862007-03-07 08:28:33 +00001845 if (ret != LIBMTP_ERROR_NONE) {
1846 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001847 }
1848
Linus Walleij45a86372007-03-07 09:36:19 +00001849 // Then prime them
1850 ret = prime_device_memory(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001851 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001852 fprintf(stderr, "LIBMTP PANIC: prime_device_memory() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001853 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001854 }
tedbullock0f033cb2007-02-14 20:56:54 +00001855
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001856 /* Assign specific device flags and detect unknown devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001857 assign_known_device_flags(mtp_device_list);
Linus Walleij550d6d52007-01-24 14:32:51 +00001858
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001859 /* Configure the devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001860 ret = configure_usb_devices(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001861 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001862 fprintf(stderr, "LIBMTP PANIC: configure_usb_devices() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001863 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001864 }
Linus Walleij550d6d52007-01-24 14:32:51 +00001865
Linus Walleij45a86372007-03-07 09:36:19 +00001866 /* we're connected to all devices, return the list and OK */
1867 *devlist = mtp_device_list;
1868 return LIBMTP_ERROR_NONE;
1869
1870 find_usb_devices_error_exit:
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001871 if(mtp_device_list != NULL) {
1872 free_mtpdevice_list(mtp_device_list);
1873 mtp_device_list = NULL;
Linus Walleij550d6d52007-01-24 14:32:51 +00001874 }
Linus Walleij45a86372007-03-07 09:36:19 +00001875 *devlist = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001876 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001877}
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001878
Linus Walleijb0ab5482007-08-29 21:08:54 +00001879static void find_interface_and_endpoints(struct usb_device *dev,
1880 uint8_t *interface,
1881 int* inep,
1882 int* inep_maxpacket,
1883 int* outep,
1884 int *outep_maxpacket,
1885 int* intep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001886{
Linus Walleijd7373cd2007-08-28 21:42:05 +00001887 int i;
1888
1889 // Loop over the device configurations
1890 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001891 uint8_t j;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001892
1893 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001894 uint8_t k;
1895 uint8_t no_ep;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001896 struct usb_endpoint_descriptor *ep;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001897
Linus Walleijb0ab5482007-08-29 21:08:54 +00001898 if (dev->descriptor.bNumConfigurations > 1 || dev->config[i].bNumInterfaces > 1) {
1899 // OK This device has more than one interface, so we have to find out
1900 // which one to use!
1901 // FIXME: Probe the interface.
1902 // FIXME: Release modules attached to all other interfaces in Linux...?
1903 }
1904
1905 *interface = dev->config[i].interface[j].altsetting->bInterfaceNumber;
1906 ep = dev->config[i].interface[j].altsetting->endpoint;
1907 no_ep = dev->config[i].interface[j].altsetting->bNumEndpoints;
1908
1909 for (k = 0; k < no_ep; k++) {
Linus Walleijd7373cd2007-08-28 21:42:05 +00001910 if (ep[k].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1911 if ((ep[k].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1912 USB_ENDPOINT_DIR_MASK)
1913 {
1914 *inep=ep[k].bEndpointAddress;
1915 *inep_maxpacket=ep[k].wMaxPacketSize;
1916 }
1917 if ((ep[k].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1918 {
1919 *outep=ep[k].bEndpointAddress;
1920 *outep_maxpacket=ep[k].wMaxPacketSize;
1921 }
1922 } else if (ep[k].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1923 if ((ep[k].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1924 USB_ENDPOINT_DIR_MASK)
1925 {
1926 *intep=ep[k].bEndpointAddress;
1927 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001928 }
Linus Walleijd7373cd2007-08-28 21:42:05 +00001929 }
1930 // We assigned the endpoints so return here.
1931 return;
Linus Walleijd6a49972006-05-02 08:24:54 +00001932 }
1933 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001934}
1935
Linus Walleijb0ab5482007-08-29 21:08:54 +00001936void close_device (PTP_USB *ptp_usb, PTPParams *params)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001937{
Linus Walleijd6a49972006-05-02 08:24:54 +00001938 if (ptp_closesession(params)!=PTP_RC_OK)
1939 fprintf(stderr,"ERROR: Could not close session!\n");
Linus Walleijb0ab5482007-08-29 21:08:54 +00001940 close_usb(ptp_usb);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001941}
1942
Linus Walleijd6a49972006-05-02 08:24:54 +00001943static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001944{
Linus Walleijd6a49972006-05-02 08:24:54 +00001945
1946 return (usb_control_msg(ptp_usb->handle,
1947 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
1948 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001949}
1950
Linus Walleijd6a49972006-05-02 08:24:54 +00001951static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001952{
Linus Walleijd6a49972006-05-02 08:24:54 +00001953 return (usb_control_msg(ptp_usb->handle,
1954 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
1955 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001956}