blob: efe2b3fbbc5414ad891dc75600307aa55e8efb70 [file] [log] [blame]
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001/*
Linus Walleij2f45d222007-02-02 22:47:39 +00002 * \file libusb-glue.c
3 * Low-level USB interface glue towards libusb.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00004 *
Linus Walleij2f45d222007-02-02 22:47:39 +00005 * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com>
6 * Copyright (C) 2005-2007 Linus Walleij <triad@df.lth.se>
7 * Copyright (C) 2006-2007 Marcus Meissner
8 * Copyright (C) 2007 Ted Bullock
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00009 *
Linus Walleij2f45d222007-02-02 22:47:39 +000010 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
Linus Walleij2e1c8a32007-01-22 06:46:08 +000014 *
Linus Walleij2f45d222007-02-02 22:47:39 +000015 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
Linus Walleij2e1c8a32007-01-22 06:46:08 +000019 *
Linus Walleij2f45d222007-02-02 22:47:39 +000020 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
24 *
25 * Created by Richard Low on 24/12/2005. (as mtp-utils.c)
26 * Modified by Linus Walleij 2006-03-06
27 * (Notice that Anglo-Saxons use little-endian dates and Swedes
28 * use big-endian dates.)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000029 *
30 */
Linus Walleij7beba572006-11-29 08:56:12 +000031#include "libmtp.h"
32#include "libusb-glue.h"
33#include "util.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000034#include "ptp.h"
Linus Walleij7beba572006-11-29 08:56:12 +000035
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000036#include <errno.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000040#include <usb.h>
41
Linus Walleija0323272007-01-07 12:21:30 +000042#include "ptp-pack.c"
43
Linus Walleije78de6d2006-10-31 14:46:36 +000044/* To enable debug prints, switch on this */
45//#define ENABLE_USB_BULK_DEBUG
46
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000047/* OUR APPLICATION USB URB (2MB) ;) */
48#define PTPCAM_USB_URB 2097152
49
50/* this must not be too short - the original 4000 was not long
51 enough for big file transfers. I imagine the player spends a
52 bit of time gearing up to receiving lots of data. This also makes
53 connecting/disconnecting more reliable */
54#define USB_TIMEOUT 10000
55#define USB_CAPTURE_TIMEOUT 20000
56
57/* USB control message data phase direction */
58#ifndef USB_DP_HTD
59#define USB_DP_HTD (0x00 << 7) /* host to device */
60#endif
61#ifndef USB_DP_DTH
62#define USB_DP_DTH (0x01 << 7) /* device to host */
63#endif
64
65/* USB Feature selector HALT */
66#ifndef USB_FEATURE_HALT
67#define USB_FEATURE_HALT 0x00
68#endif
69
Linus Walleija5483642006-03-09 09:20:38 +000070/*
Linus Walleije8c54642006-03-28 09:45:00 +000071 * MTP device list, trying real bad to get all devices into
72 * this list by stealing from everyone I know.
Linus Walleija5483642006-03-09 09:20:38 +000073 */
Linus Walleij6fd2f082006-03-28 07:19:22 +000074static const LIBMTP_device_entry_t mtp_device_table[] = {
Linus Walleij3d2c7842006-08-18 09:35:08 +000075
76 /*
77 * Creative Technology
Linus Walleijcc4dd482006-11-25 21:58:30 +000078 * Initially the Creative devices was all we supported so these are
79 * the most thoroughly tested devices.
Linus Walleij3d2c7842006-08-18 09:35:08 +000080 */
Linus Walleij0558ac52006-09-07 06:55:03 +000081 { "Creative Zen Vision", 0x041e, 0x411f, DEVICE_FLAG_NONE },
82 { "Creative Portable Media Center", 0x041e, 0x4123, DEVICE_FLAG_NONE },
83 { "Creative Zen Xtra (MTP mode)", 0x041e, 0x4128, DEVICE_FLAG_NONE },
84 { "Second generation Dell DJ", 0x041e, 0x412f, DEVICE_FLAG_NONE },
85 { "Creative Zen Micro (MTP mode)", 0x041e, 0x4130, DEVICE_FLAG_NONE },
86 { "Creative Zen Touch (MTP mode)", 0x041e, 0x4131, DEVICE_FLAG_NONE },
87 { "Dell Pocket DJ (MTP mode)", 0x041e, 0x4132, DEVICE_FLAG_NONE },
88 { "Creative Zen Sleek (MTP mode)", 0x041e, 0x4137, DEVICE_FLAG_NONE },
89 { "Creative Zen MicroPhoto", 0x041e, 0x413c, DEVICE_FLAG_NONE },
90 { "Creative Zen Sleek Photo", 0x041e, 0x413d, DEVICE_FLAG_NONE },
91 { "Creative Zen Vision:M", 0x041e, 0x413e, DEVICE_FLAG_NONE },
Linus Walleijbdd006b2006-09-15 04:54:13 +000092 // Reported by marazm@o2.pl
93 { "Creative Zen V", 0x041e, 0x4150, DEVICE_FLAG_NONE },
Linus Walleij92a84962006-11-11 20:04:37 +000094 // Reported by danielw@iinet.net.au
95 { "Creative Zen Vision:M (DVP-HD0004)", 0x041e, 0x4151, DEVICE_FLAG_NONE },
Linus Walleij550bc672006-10-10 11:11:29 +000096 // Reported by Darel on the XNJB forums
Richard Lowe1f06922006-11-12 16:38:39 +000097 { "Creative Zen V Plus", 0x041e, 0x4152, DEVICE_FLAG_NONE },
98 { "Creative Zen Vision W", 0x041e, 0x4153, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000099
100 /*
101 * Samsung
Linus Walleij0558ac52006-09-07 06:55:03 +0000102 * We suspect that more of these are dual mode.
Linus Walleij3d2c7842006-08-18 09:35:08 +0000103 */
104 // From libgphoto2
Linus Walleij0558ac52006-09-07 06:55:03 +0000105 { "Samsung YH-820", 0x04e8, 0x502e, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000106 // Contributed by polux2001@users.sourceforge.net
Linus Walleija29ed2a2007-02-25 22:01:37 +0000107 { "Samsung YH-925(-GS)", 0x04e8, 0x502f, DEVICE_FLAG_NONE },
Richard Low7059ed42007-01-21 09:33:34 +0000108 // Contributed by aronvanammers on SourceForge
109 { "Samsung YH-925GS", 0x04e8, 0x5024, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000110 // Contributed by anonymous person on SourceForge
Linus Walleij0558ac52006-09-07 06:55:03 +0000111 { "Samsung YP-T7J", 0x04e8, 0x5047, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000112 // Reported by cstrickler@gmail.com
Linus Walleij0558ac52006-09-07 06:55:03 +0000113 { "Samsung YP-U2J (YP-U2JXB/XAA)", 0x04e8, 0x5054, DEVICE_FLAG_NONE },
Linus Walleij82ed6612006-08-31 08:06:19 +0000114 // Reported by Andrew Benson
Linus Walleij0558ac52006-09-07 06:55:03 +0000115 { "Samsung YP-F2J", 0x04e8, 0x5057, DEVICE_FLAG_DUALMODE },
Linus Walleij19be4212006-11-16 09:00:48 +0000116 // Reported by Patrick <skibler@gmail.com>
Linus Walleijabdf6072006-11-24 07:34:02 +0000117 { "Samsung YP-K5", 0x04e8, 0x505a, DEVICE_FLAG_NONE },
Linus Walleija5073c72006-11-24 07:55:12 +0000118 // Reported by Matthew Wilcox <matthew@wil.cx>
Linus Walleij12413c02007-02-22 18:33:07 +0000119 { "Samsung YP-T9", 0x04e8, 0x507f, DEVICE_FLAG_NONE },
120 // From Paul Clinch
121 { "Samsung YP-K3", 0x04e8, 0x5081, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000122 // From a rouge .INF file
Linus Walleij0558ac52006-09-07 06:55:03 +0000123 { "Samsung YH-999 Portable Media Center", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE },
Linus Walleij8556f522007-02-14 09:47:50 +0000124 // From Lionel Bouton
125 { "Samsung X830 Mobile Phone", 0x04e8, 0x6702, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000126
127 /*
128 * Intel
129 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000130 { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000131
132 /*
133 * JVC
134 */
135 // From Mark Veinot
Linus Walleij0558ac52006-09-07 06:55:03 +0000136 { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000137
138 /*
Linus Walleij093ca0a2006-11-02 21:15:00 +0000139 * Philips
Linus Walleij3d2c7842006-08-18 09:35:08 +0000140 */
141 // From libgphoto2 source
Linus Walleij093ca0a2006-11-02 21:15:00 +0000142 { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE },
Richard Low7059ed42007-01-21 09:33:34 +0000143 { "Philips HDD6320/00 & HDD6330/17", 0x0471, 0x014b, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000144 // Anonymous SourceForge user
Linus Walleij093ca0a2006-11-02 21:15:00 +0000145 { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE },
146 // From Gerhard Mekenkamp
147 { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE },
Richard Lowa6c924c2006-12-29 17:44:28 +0000148 // from XNJB forum
149 { "Philips GoGear SA9200", 0x0471, 0x014f, DEVICE_FLAG_NONE },
Richard Lowa98d0542007-01-20 12:31:07 +0000150 // from XNJB user
151 { "Philips PSA235", 0x0471, 0x7e01, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000152
153 /*
154 * SanDisk
155 */
156 // Reported by Brian Robison
Linus Walleij0558ac52006-09-07 06:55:03 +0000157 { "SanDisk Sansa m240", 0x0781, 0x7400, DEVICE_FLAG_NONE },
Linus Walleijb6583182007-03-07 08:14:18 +0000158 /*
159 * Reported by tangent_@users.sourceforge.net
160 * this one is definately dual-mode and must be blocked from
161 * being taken by USB mass storage first.
162 */
163 { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_UNLOAD_DRIVER },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000164 // From libgphoto2 source
Linus Walleij0558ac52006-09-07 06:55:03 +0000165 { "SanDisk Sansa e200", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000166 // Reported by gonkflea@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +0000167 { "SanDisk Sansa e260", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij1df47cd2007-01-24 10:19:52 +0000168 // Reported by anonymous user at sourceforge.net
169 { "SanDisk Sansa c250", 0x0781, 0x7450, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000170
171 /*
172 * iRiver
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000173 * we assume that PTP_OC_MTP_GetObjPropList is essentially
Linus Walleijfabffc02006-12-17 00:18:57 +0000174 * broken on all iRiver devices, meaning it simply won't return
175 * all properties for a file when asking for metadata 0xffffffff.
176 * Please test on your device if you believe it isn't broken!
177 * Some devices from http://www.mtp-ums.net/viewdeviceinfo.php
Linus Walleij3d2c7842006-08-18 09:35:08 +0000178 */
Linus Walleij049f50c2007-03-03 20:30:43 +0000179 { "iRiver Portable Media Center", 0x1006, 0x4002, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
180 { "iRiver Portable Media Center", 0x1006, 0x4003, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000181 // From libgphoto2 source
Linus Walleij049f50c2007-03-03 20:30:43 +0000182 { "iRiver T10", 0x4102, 0x1113, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
183 { "iRiver T20 FM", 0x4102, 0x1114, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
Linus Walleijfabffc02006-12-17 00:18:57 +0000184 // This appears at the MTP-UMS site
Linus Walleij049f50c2007-03-03 20:30:43 +0000185 { "iRiver T20", 0x4102, 0x1115, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
186 { "iRiver U10", 0x4102, 0x1116, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
187 { "iRiver T10", 0x4102, 0x1117, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
188 { "iRiver T20", 0x4102, 0x1118, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
189 { "iRiver T30", 0x4102, 0x1119, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
Linus Walleij77e047c2006-09-11 19:09:06 +0000190 // Reported by David Wolpoff
Linus Walleij049f50c2007-03-03 20:30:43 +0000191 { "iRiver T10 2GB", 0x4102, 0x1120, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
Linus Walleij87099812006-12-17 00:29:58 +0000192 // Rough guess this is the MTP device ID...
Linus Walleij049f50c2007-03-03 20:30:43 +0000193 { "iRiver N12", 0x4102, 0x1122, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
Linus Walleij04377da2006-08-30 13:03:35 +0000194 // Reported by Adam Torgerson
Linus Walleij049f50c2007-03-03 20:30:43 +0000195 { "iRiver Clix", 0x4102, 0x112a, DEVICE_FLAG_NO_ZERO_READS },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000196 // Reported by Scott Call
Linus Walleij049f50c2007-03-03 20:30:43 +0000197 { "iRiver H10 20GB", 0x4102, 0x2101, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
198 { "iRiver H10", 0x4102, 0x2102, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000199
200 /*
201 * Dell
202 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000203 { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000204
205 /*
206 * Toshiba
207 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000208 { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE },
209 { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE },
Linus Walleij1e3d1762007-02-26 08:27:35 +0000210 // Reported by Nicholas Tripp
211 { "Toshiba Gigabeat P20", 0x0930, 0x000f, DEVICE_FLAG_NONE },
Linus Walleij405e2392006-10-23 07:13:49 +0000212 // From libgphoto2
213 { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE },
Linus Walleij402e4bd2006-09-12 07:50:56 +0000214 // Reported by Rob Brown
215 { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE },
Linus Walleijb01d18b2006-12-13 12:49:15 +0000216
Linus Walleij6f680562006-08-19 22:49:33 +0000217 /*
218 * Archos
219 */
220 // Reported by gudul1@users.sourceforge.net
Linus Walleije5430902006-10-08 14:46:03 +0000221 { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE },
Linus Walleijbbdf44e2007-02-14 09:36:00 +0000222 // Added by Jan Binder
223 { "Archos XS202 (MTP mode)", 0x0e79, 0x1208, DEVICE_FLAG_NONE },
Linus Walleije5430902006-10-08 14:46:03 +0000224
225 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000226 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000227 * This unit is falsely detected as USB mass storage in Linux
Linus Walleijcc4dd482006-11-25 21:58:30 +0000228 * prior to kernel 2.6.19 (fixed by patch from Alan Stern)
229 * so on older kernels special care is needed to remove the
230 * USB mass storage driver that erroneously binds to the device
231 * interface.
Linus Walleije5430902006-10-08 14:46:03 +0000232 */
Richard Lowc7d48d22006-11-29 18:24:55 +0000233 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER},
234
235 /*
236 * Microsoft
237 */
238 // Reported by Farooq Zaman
239 { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE },
240
241 /*
242 * Sirius
243 */
Linus Walleijb01d18b2006-12-13 12:49:15 +0000244 { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE },
245
246 /*
247 * Canon
248 * This is actually a camera, but it has a Microsoft device descriptor
249 * and reports itself as supporting the MTP extension.
250 */
Linus Walleijc1b49402007-02-07 08:00:38 +0000251 {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE },
252
253 /*
254 * Nokia
255 */
Linus Walleijde7ad542007-02-14 09:32:26 +0000256 {"Nokia Mobile Phones (MTP mode)", 0x0421, 0x04e1, DEVICE_FLAG_NONE },
257
258 /*
259 * LOGIK
260 * Sold in the UK, seem to be manufactured by CCTech in China.
261 */
262 {"Logik LOG DAX MP3 and DAB Player", 0x13d1, 0x7002, DEVICE_FLAG_NONE }
Linus Walleija5483642006-03-09 09:20:38 +0000263};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000264static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000265
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000266int ptpcam_usb_timeout = USB_TIMEOUT;
267
Linus Walleijd6a49972006-05-02 08:24:54 +0000268// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000269static struct usb_bus* init_usb();
Linus Walleijd6a49972006-05-02 08:24:54 +0000270static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);
Linus Walleijd6a49972006-05-02 08:24:54 +0000271static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);
272static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000273static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000274static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000275static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000276static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
277static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000278
Linus Walleij552ba322007-01-22 11:49:59 +0000279/**
280 * Get a list of the supported USB devices.
281 *
282 * The developers depend on users of this library to constantly
283 * add in to the list of supported devices. What we need is the
284 * device name, USB Vendor ID (VID) and USB Product ID (PID).
285 * put this into a bug ticket at the project homepage, please.
286 * The VID/PID is used to let e.g. udev lift the device to
287 * console userspace access when it's plugged in.
288 *
289 * @param devices a pointer to a pointer that will hold a device
290 * list after the call to this function, if it was
291 * successful.
292 * @param numdevs a pointer to an integer that will hold the number
293 * of devices in the device list if the call was successful.
294 * @return 0 if the list was successfull retrieved, any other
295 * value means failure.
296 */
297int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000298{
299 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
300 *numdevs = mtp_device_table_size;
301 return 0;
302}
303
Linus Walleij552ba322007-01-22 11:49:59 +0000304
Linus Walleij1fd2d272006-05-08 09:22:01 +0000305static struct usb_bus* init_usb()
306{
307 usb_init();
308 usb_find_busses();
309 usb_find_devices();
310 return (usb_get_busses());
311}
312
313/**
tedbullock0f033cb2007-02-14 20:56:54 +0000314 * Small recursive function to append a new usb_device to the linked list of
315 * USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000316 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000317 * properties.
318 * @param next New USB MTP device to be added to list
319 * @return nothing
320 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000321static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
322 struct usb_device *newdevice)
tedbullock0f033cb2007-02-14 20:56:54 +0000323{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000324 mtpdevice_list_t *new_list_entry;
tedbullock0f033cb2007-02-14 20:56:54 +0000325
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000326 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
327 if (new_list_entry == NULL) {
328 return NULL;
329 }
330 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
331 new_list_entry->libusb_device = newdevice;
332 new_list_entry->next = NULL;
333
334 if (devlist == NULL) {
335 return new_list_entry;
336 } else {
337 mtpdevice_list_t *tmp = devlist;
338 while (tmp->next != NULL) {
339 tmp = tmp->next;
340 }
341 tmp->next = new_list_entry;
342 }
343 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000344}
345
346/**
347 * Small recursive function to free dynamic memory allocated to the linked list
348 * of USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000349 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000350 * properties.
351 * @return nothing
352 */
Linus Walleij45a86372007-03-07 09:36:19 +0000353void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000354{
Linus Walleijac061862007-03-07 08:28:33 +0000355 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000356
357 if (devlist == NULL)
358 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000359 while (tmplist != NULL) {
360 mtpdevice_list_t *tmp = tmplist;
361 tmplist = tmplist->next;
Linus Walleij45a86372007-03-07 09:36:19 +0000362 // Do not free() the fields (ptp_usb, parms)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000363 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000364 }
tedbullock0f033cb2007-02-14 20:56:54 +0000365 return;
366}
367
368/**
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000369 * Check for the Microsoft OS device descriptor and return device struct
370 * if the device is MTP-compliant. The function will only recognize
371 * a single device connected to the USB bus.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000372 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000373 * properties (if this list is not empty, new entries will be appended
374 * to the list).
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000375 * @return an MTP-compliant USB device if one was found, else NULL.
376 */
Linus Walleij45a86372007-03-07 09:36:19 +0000377static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000378{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000379 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000380
381 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000382 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000383 for (; dev != NULL; dev = dev->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000384 usb_dev_handle *devh;
385 unsigned char buf[1024], cmd;
386 int ret;
387
Linus Walleij35c0ef12007-03-06 20:49:09 +0000388 /* Don't examine hubs (no point in that) */
389 if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) {
390 continue;
391 }
392
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000393 /* Attempt to open Device on this port */
394 devh = usb_open(dev);
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000395 if (devh == NULL) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000396 /* Could not open this device, continue */
397 continue;
398 }
399
400 /* Read the special descriptor */
401 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
402
403 /* Check if descriptor length is at least 10 bytes */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000404 if (ret < 10) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000405 usb_close(devh);
406 continue;
407 }
408
409 /* Check if this device has a Microsoft Descriptor */
410 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000411 (buf[6] == 'F') && (buf[8] == 'T'))) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000412 usb_close(devh);
413 continue;
414 }
415
416 /* Check if device responds to control message 1 or if there is an error*/
417 cmd = buf[16];
418 ret = usb_control_msg (devh,
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000419 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
420 cmd,
421 0,
422 4,
423 (char *) buf,
424 sizeof(buf),
425 1000);
426
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000427 /* If this is true, the device either isn't MTP or there was an error */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000428 if (ret <= 0x15) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000429 /* TODO: If there was an error, flag it and let the user know somehow */
430 /* if(ret == -1) {} */
431 usb_close(devh);
432 continue;
433 }
434
435 /* Check if device is MTP or if it is something like a USB Mass Storage
436 device with Janus DRM support */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000437 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000438 usb_close(devh);
439 continue;
440 }
441
442 /* After this point we are probably dealing with an MTP device */
443
444 /* Check if device responds to control message 2 or if there is an error*/
445 ret = usb_control_msg (devh,
446 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
447 cmd,
448 0,
449 5,
450 (char *) buf,
451 sizeof(buf),
452 1000);
453
454 /* If this is true, the device errored against control message 2 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000455 if (ret == -1) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000456 /* TODO: Implement callback function to let managing program know there
457 was a problem, along with description of the problem */
458 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000459 "ProductID:%04x encountered an error responding to "
460 "control message 2.\n"
461 "Problems may arrise but continuing\n",
462 dev->descriptor.idVendor, dev->descriptor.idProduct);
463 } else if (ret <= 0x15) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000464 /* TODO: Implement callback function to let managing program know there
465 was a problem, along with description of the problem */
466 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000467 "ProductID:%04x responded to control message 2 with a "
468 "response that was too short. Problems may arrise but "
469 "continuing\n",
470 dev->descriptor.idVendor, dev->descriptor.idProduct);
471 } else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000472 /* TODO: Implement callback function to let managing program know there
473 was a problem, along with description of the problem */
474 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000475 "ProductID:%04x encountered an error responding to "
476 "control message 2\n"
477 "Problems may arrise but continuing\n",
478 dev->descriptor.idVendor, dev->descriptor.idProduct);
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000479 }
480
481 /* Close the USB device handle */
482 usb_close(devh);
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000483
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000484 /* Append this usb device to the MTP USB Device List */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000485 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000486 }
487 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000488
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000489 /* If nothing was found we end up here. */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000490 if(*mtp_device_list == NULL)
tedbullock433d2172007-02-23 22:39:12 +0000491 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000492 else
493 return LIBMTP_ERROR_NONE;
494}
495
496/**
Linus Walleij1fd2d272006-05-08 09:22:01 +0000497 * Detect the MTP device descriptor and return the VID and PID
498 * of the first device found. This is a very low-level function
499 * which is intended for use with <b>udev</b> or other hotplug
500 * mechanisms. The idea is that a script may want to know if the
501 * just plugged-in device was an MTP device or not.
tedbullock2b45f2e2007-02-23 20:16:37 +0000502 *
503 * FUNCTION STUBBED OUT FOR FURTHER EXAMINITATION LATER
504 *
Linus Walleij1fd2d272006-05-08 09:22:01 +0000505 * @param vid the Vendor ID (VID) of the first device found.
506 * @param pid the Product ID (PID) of the first device found.
507 * @return the number of detected devices or -1 if the call
508 * was unsuccessful.
509 */
510int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
511{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000512 /* TODO: Find a way to make this work with the multiple device code */
513 *vid = *pid = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000514 return 1;
515}
516
Linus Walleijc6210fb2006-05-08 11:11:41 +0000517/**
518 * This routine just dumps out low-level
519 * USB information about the current device.
520 * @param ptp_usb the USB device to get information from.
521 */
522void dump_usbinfo(PTP_USB *ptp_usb)
523{
524 int res;
525 struct usb_device *dev;
526
527#ifdef LIBUSB_HAS_GET_DRIVER_NP
528 char devname[0x10];
529
530 devname[0] = '\0';
531 res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));
532 if (devname[0] != '\0') {
533 printf(" Using kernel interface \"%s\"\n", devname);
534 }
535#endif
536 dev = usb_device(ptp_usb->handle);
537 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
538 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
539 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
540 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000541 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
542 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000543 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
544 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000545 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000546 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
547}
548
Linus Walleija0323272007-01-07 12:21:30 +0000549static void
550ptp_debug (PTPParams *params, const char *format, ...)
551{
552 va_list args;
553
554 va_start (args, format);
555 if (params->debug_func!=NULL)
556 params->debug_func (params->data, format, args);
557 else
558 {
559 vfprintf (stderr, format, args);
560 fprintf (stderr,"\n");
561 fflush (stderr);
562 }
563 va_end (args);
564}
565
566static void
567ptp_error (PTPParams *params, const char *format, ...)
568{
569 va_list args;
570
571 va_start (args, format);
572 if (params->error_func!=NULL)
573 params->error_func (params->data, format, args);
574 else
575 {
576 vfprintf (stderr, format, args);
577 fprintf (stderr,"\n");
578 fflush (stderr);
579 }
580 va_end (args);
581}
582
Linus Walleijc6210fb2006-05-08 11:11:41 +0000583
Linus Walleij99ed83c2007-01-02 18:46:02 +0000584/*
585 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000586 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000587 * Much reading packet logs and having fun with trials and errors
588 * reveals that WMP / Windows is probably using an algorithm like this
589 * for large transfers:
590 *
591 * 1. Send the command (0x0c bytes) if headers are split, else, send
592 * command plus sizeof(endpoint) - 0x0c bytes.
593 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
594 * split headers. Else goto 3.
595 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
596 * We call 0x10000 CONTEXT_BLOCK_SIZE.
597 * 4. Send remaining bytes MOD sizeof(endpoint)
598 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
599 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000600 *
601 * Further there is some special quirks to handle zero reads from the
602 * device, since some devices can't do them at all due to shortcomings
603 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000604 */
Richard Low4fd59d62007-03-03 16:34:37 +0000605#define CONTEXT_BLOCK_SIZE_1 0x3e00
606#define CONTEXT_BLOCK_SIZE_2 0x200
607#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000608static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000609ptp_read_func (
610 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000611 unsigned long *readbytes,
612 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000613) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000614 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000615 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000616 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000617 unsigned long curread = 0;
618 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000619 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000620 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000621
622 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000623 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000624 while (curread < size) {
Richard Low4fd59d62007-03-03 16:34:37 +0000625
626#ifdef ENABLE_USB_BULK_DEBUG
627 printf("Remaining size to read: 0x%04x bytes\n", size - curread);
628#endif
629 // check equal to condition here
630 if (size - curread < CONTEXT_BLOCK_SIZE)
631 {
632 // this is the last packet
633 toread = size - curread;
634 // this is equivalent to zero read for these devices
Linus Walleij049f50c2007-03-03 20:30:43 +0000635 if (readzero && ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000636 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000637 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000638 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000639 }
Richard Low4fd59d62007-03-03 16:34:37 +0000640 else if (curread == 0)
641 // we are first packet, but not last packet
642 toread = CONTEXT_BLOCK_SIZE_1;
643 else if (toread == CONTEXT_BLOCK_SIZE_1)
644 toread = CONTEXT_BLOCK_SIZE_2;
645 else if (toread == CONTEXT_BLOCK_SIZE_2)
646 toread = CONTEXT_BLOCK_SIZE_1;
647 else
Linus Walleij4cec9872007-03-03 21:00:53 +0000648 printf("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
649 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000650
Richard Lowd284f072007-02-17 08:52:41 +0000651#ifdef ENABLE_USB_BULK_DEBUG
652 printf("Reading in 0x%04x bytes\n", toread);
653#endif
Linus Walleij784ac3f2006-12-29 10:36:51 +0000654 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout);
Richard Lowd284f072007-02-17 08:52:41 +0000655#ifdef ENABLE_USB_BULK_DEBUG
656 printf("Result of read: 0x%04x\n", result);
657#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000658
Linus Walleijc49c6372007-01-09 00:18:51 +0000659 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000660 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000661 }
Linus Walleije78de6d2006-10-31 14:46:36 +0000662#ifdef ENABLE_USB_BULK_DEBUG
663 printf("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000664 if (result == 0)
665 printf("Zero Read\n");
666 else
667 data_dump_ascii (stdout,bytes,result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000668#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000669
670 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000671 if (expect_terminator_byte && result == toread)
Richard Low4fd59d62007-03-03 16:34:37 +0000672 result--;
673
Linus Walleijc49c6372007-01-09 00:18:51 +0000674 handler->putfunc(NULL, handler->private, result, bytes, &written);
675 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000676 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000677
678 // Increase counters, call callback
679 if (ptp_usb->callback_active) {
680 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
681 // send last update and disable callback.
682 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
683 ptp_usb->callback_active = 0;
684 }
685 if (ptp_usb->current_transfer_callback != NULL) {
686 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
687 ptp_usb->current_transfer_total,
688 ptp_usb->current_transfer_callback_data);
689 }
690 }
691
Linus Walleijd6a49972006-05-02 08:24:54 +0000692 if (result < toread) /* short reads are common */
693 break;
694 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000695 if (readbytes) *readbytes = curread;
696 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000697
Richard Low4df32f82007-01-11 20:04:39 +0000698 // there might be a zero packet waiting for us...
Linus Walleij049f50c2007-03-03 20:30:43 +0000699 if (readzero &&
700 !(ptp_usb->device_flags & DEVICE_FLAG_NO_ZERO_READS) &&
701 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000702 char temp;
703 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000704
Richard Low4df32f82007-01-11 20:04:39 +0000705#ifdef ENABLE_USB_BULK_DEBUG
706 printf("<==USB IN\n");
707 printf("Zero Read\n");
708#endif
709 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptpcam_usb_timeout);
710 if (zeroresult != 0)
711 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
712 }
713
Linus Walleijd6a49972006-05-02 08:24:54 +0000714 if (result > 0) {
Linus Walleijd6a49972006-05-02 08:24:54 +0000715 return (PTP_RC_OK);
716 } else {
717 return PTP_ERROR_IO;
718 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000719}
720
Linus Walleijb02a0662006-04-25 08:05:09 +0000721static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000722ptp_write_func (
723 unsigned long size,
724 PTPDataHandler *handler,
725 void *data,
726 unsigned long *written
727) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000728 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000729 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000730 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000731 unsigned long curwrite = 0;
732 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000733
734 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000735 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000736 if (!bytes) {
737 return PTP_ERROR_IO;
738 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000739 while (curwrite < size) {
740 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000741 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000742 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000743 } else {
744 // This magic makes packets the same size that WMP send them.
745 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000746 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000747 }
748 }
749 handler->getfunc(NULL, handler->private,towrite,bytes,&towrite);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000750 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000751#ifdef ENABLE_USB_BULK_DEBUG
752 printf("USB OUT==>\n");
Richard Lowedd61832006-12-30 12:38:18 +0000753 data_dump_ascii (stdout,bytes,towrite,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000754#endif
Linus Walleijc49c6372007-01-09 00:18:51 +0000755 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000756 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000757 }
758 // Increase counters
759 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000760 curwrite += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000761
762 // call callback
763 if (ptp_usb->callback_active) {
764 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
765 // send last update and disable callback.
766 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
767 ptp_usb->callback_active = 0;
768 }
769 if (ptp_usb->current_transfer_callback != NULL) {
770 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
771 ptp_usb->current_transfer_total,
772 ptp_usb->current_transfer_callback_data);
773 }
774 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000775 if (result < towrite) /* short writes happen */
776 break;
777 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000778 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000779 if (written) {
780 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000781 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000782
Linus Walleijc49c6372007-01-09 00:18:51 +0000783
Richard Lowef05b892007-01-03 21:18:56 +0000784 // If this is the last transfer send a zero write if required
785 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000786 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
Richard Low021421e2007-01-01 18:08:57 +0000787#ifdef ENABLE_USB_BULK_DEBUG
788 printf("USB OUT==>\n");
789 printf("Zero Write\n");
790#endif
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000791 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000792 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000793 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000794
Linus Walleijd6a49972006-05-02 08:24:54 +0000795 if (result < 0)
796 return PTP_ERROR_IO;
797 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000798}
799
Linus Walleija0323272007-01-07 12:21:30 +0000800/* memory data get/put handler */
801typedef struct {
802 unsigned char *data;
803 unsigned long size, curoff;
804} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000805
Linus Walleija0323272007-01-07 12:21:30 +0000806static uint16_t
807memory_getfunc(PTPParams* params, void* private,
808 unsigned long wantlen, unsigned char *data,
809 unsigned long *gotlen
810) {
811 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
812 unsigned long tocopy = wantlen;
813
814 if (priv->curoff + tocopy > priv->size)
815 tocopy = priv->size - priv->curoff;
816 memcpy (data, priv->data + priv->curoff, tocopy);
817 priv->curoff += tocopy;
818 *gotlen = tocopy;
819 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000820}
821
Linus Walleija0323272007-01-07 12:21:30 +0000822static uint16_t
823memory_putfunc(PTPParams* params, void* private,
824 unsigned long sendlen, unsigned char *data,
825 unsigned long *putlen
826) {
827 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
828
829 if (priv->curoff + sendlen > priv->size) {
830 priv->data = realloc (priv->data, priv->curoff+sendlen);
831 priv->size = priv->curoff + sendlen;
832 }
833 memcpy (priv->data + priv->curoff, data, sendlen);
834 priv->curoff += sendlen;
835 *putlen = sendlen;
836 return PTP_RC_OK;
837}
838
839/* init private struct for receiving data. */
840static uint16_t
841ptp_init_recv_memory_handler(PTPDataHandler *handler) {
842 PTPMemHandlerPrivate* priv;
843 priv = malloc (sizeof(PTPMemHandlerPrivate));
844 handler->private = priv;
845 handler->getfunc = memory_getfunc;
846 handler->putfunc = memory_putfunc;
847 priv->data = NULL;
848 priv->size = 0;
849 priv->curoff = 0;
850 return PTP_RC_OK;
851}
852
853/* init private struct and put data in for sending data.
854 * data is still owned by caller.
855 */
856static uint16_t
857ptp_init_send_memory_handler(PTPDataHandler *handler,
858 unsigned char *data, unsigned long len
859) {
860 PTPMemHandlerPrivate* priv;
861 priv = malloc (sizeof(PTPMemHandlerPrivate));
862 if (!priv)
863 return PTP_RC_GeneralError;
864 handler->private = priv;
865 handler->getfunc = memory_getfunc;
866 handler->putfunc = memory_putfunc;
867 priv->data = data;
868 priv->size = len;
869 priv->curoff = 0;
870 return PTP_RC_OK;
871}
872
873/* free private struct + data */
874static uint16_t
875ptp_exit_send_memory_handler (PTPDataHandler *handler) {
876 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
877 /* data is owned by caller */
878 free (priv);
879 return PTP_RC_OK;
880}
881
882/* hand over our internal data to caller */
883static uint16_t
884ptp_exit_recv_memory_handler (PTPDataHandler *handler,
885 unsigned char **data, unsigned long *size
886) {
887 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
888 *data = priv->data;
889 *size = priv->size;
890 free (priv);
891 return PTP_RC_OK;
892}
893
894/* send / receive functions */
895
896uint16_t
897ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
898{
899 uint16_t ret;
900 PTPUSBBulkContainer usbreq;
901 PTPDataHandler memhandler;
902 unsigned long written, towrite;
903
904 /* build appropriate USB container */
905 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
906 (sizeof(uint32_t)*(5-req->Nparam)));
907 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
908 usbreq.code=htod16(req->Code);
909 usbreq.trans_id=htod32(req->Transaction_ID);
910 usbreq.payload.params.param1=htod32(req->Param1);
911 usbreq.payload.params.param2=htod32(req->Param2);
912 usbreq.payload.params.param3=htod32(req->Param3);
913 usbreq.payload.params.param4=htod32(req->Param4);
914 usbreq.payload.params.param5=htod32(req->Param5);
915 /* send it to responder */
916 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
917 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
918 ret=ptp_write_func(
919 towrite,
920 &memhandler,
921 params->data,
922 &written
923 );
924 ptp_exit_send_memory_handler (&memhandler);
925 if (ret!=PTP_RC_OK) {
926 ret = PTP_ERROR_IO;
927/* ptp_error (params,
928 "PTP: request code 0x%04x sending req error 0x%04x",
929 req->Code,ret); */
930 }
931 if (written != towrite) {
932 ptp_error (params,
933 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +0000934 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +0000935 );
936 ret = PTP_ERROR_IO;
937 }
938 return ret;
939}
940
941uint16_t
942ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
943 unsigned long size, PTPDataHandler *handler
944) {
945 uint16_t ret;
946 int wlen, datawlen;
947 unsigned long written;
948 PTPUSBBulkContainer usbdata;
949 uint32_t bytes_left_to_transfer;
950 PTPDataHandler memhandler;
951
952 /* build appropriate USB container */
953 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
954 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
955 usbdata.code = htod16(ptp->Code);
956 usbdata.trans_id= htod32(ptp->Transaction_ID);
957
958 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +0000959 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +0000960
961 if (params->split_header_data) {
962 datawlen = 0;
963 wlen = PTP_USB_BULK_HDR_LEN;
964 } else {
965 unsigned long gotlen;
966 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +0000967 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +0000968 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
969 ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen);
970 if (ret != PTP_RC_OK)
971 return ret;
972 if (gotlen != datawlen)
973 return PTP_RC_GeneralError;
974 }
975 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
976 /* send first part of data */
977 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
978 ptp_exit_send_memory_handler (&memhandler);
979 if (ret!=PTP_RC_OK) {
980 ret = PTP_ERROR_IO;
981/* ptp_error (params,
982 "PTP: request code 0x%04x sending data error 0x%04x",
983 ptp->Code,ret);*/
984 return ret;
985 }
986 if (size <= datawlen) return ret;
987 /* if everything OK send the rest */
988 bytes_left_to_transfer = size-datawlen;
989 ret = PTP_RC_OK;
990 while(bytes_left_to_transfer > 0) {
991 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
992 if (ret != PTP_RC_OK)
993 break;
994 if (written == 0) {
995 ret = PTP_ERROR_IO;
996 break;
997 }
998 bytes_left_to_transfer -= written;
999 }
1000 if (ret!=PTP_RC_OK)
1001 ret = PTP_ERROR_IO;
1002 return ret;
1003}
1004
1005static uint16_t ptp_usb_getpacket(PTPParams *params,
1006 PTPUSBBulkContainer *packet, unsigned long *rlen)
1007{
1008 PTPDataHandler memhandler;
1009 uint16_t ret;
1010 unsigned char *x = NULL;
1011
1012 /* read the header and potentially the first data */
1013 if (params->response_packet_size > 0) {
1014 /* If there is a buffered packet, just use it. */
1015 memcpy(packet, params->response_packet, params->response_packet_size);
1016 *rlen = params->response_packet_size;
1017 free(params->response_packet);
1018 params->response_packet = NULL;
1019 params->response_packet_size = 0;
1020 /* Here this signifies a "virtual read" */
1021 return PTP_RC_OK;
1022 }
1023 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001024 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001025 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1026 if (x) {
1027 memcpy (packet, x, *rlen);
1028 free (x);
1029 }
1030 return ret;
1031}
1032
1033uint16_t
1034ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1035{
1036 uint16_t ret;
1037 PTPUSBBulkContainer usbdata;
1038 unsigned char *data;
1039 unsigned long written;
1040
1041 memset(&usbdata,0,sizeof(usbdata));
1042 do {
1043 unsigned long len, rlen;
1044
1045 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1046 if (ret!=PTP_RC_OK) {
1047 ret = PTP_ERROR_IO;
1048 break;
1049 } else
1050 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1051 ret = PTP_ERROR_DATA_EXPECTED;
1052 break;
1053 } else
1054 if (dtoh16(usbdata.code)!=ptp->Code) {
1055 ret = dtoh16(usbdata.code);
1056 break;
1057 }
1058 if (usbdata.length == 0xffffffffU) {
1059 /* stuff data directly to passed data handler */
1060 while (1) {
1061 unsigned long readdata;
1062 int xret;
1063
1064 xret = ptp_read_func(
Richard Low02724f62007-02-17 09:47:26 +00001065 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
Linus Walleija0323272007-01-07 12:21:30 +00001066 handler,
1067 params->data,
Richard Low4df32f82007-01-11 20:04:39 +00001068 &readdata,
Linus Walleijc0a11432007-03-02 21:21:18 +00001069 0
Linus Walleija0323272007-01-07 12:21:30 +00001070 );
1071 if (xret == -1)
1072 return PTP_ERROR_IO;
Richard Low02724f62007-02-17 09:47:26 +00001073 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
Linus Walleija0323272007-01-07 12:21:30 +00001074 break;
1075 }
1076 return PTP_RC_OK;
1077 }
1078 if (rlen > dtoh32(usbdata.length)) {
1079 /*
1080 * Buffer the surplus response packet if it is >=
1081 * PTP_USB_BULK_HDR_LEN
1082 * (i.e. it is probably an entire package)
1083 * else discard it as erroneous surplus data.
1084 * This will even work if more than 2 packets appear
1085 * in the same transaction, they will just be handled
1086 * iteratively.
1087 *
1088 * Marcus observed stray bytes on iRiver devices;
1089 * these are still discarded.
1090 */
1091 unsigned int packlen = dtoh32(usbdata.length);
1092 unsigned int surplen = rlen - packlen;
1093
1094 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1095 params->response_packet = malloc(surplen);
1096 memcpy(params->response_packet,
1097 (uint8_t *) &usbdata + packlen, surplen);
1098 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001099 /* Ignore reading one extra byte if device flags have been set */
1100 } else if( !(((PTP_USB *)params->data)->device_flags &
1101 DEVICE_FLAG_NO_ZERO_READS &&
1102 rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001103 ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
1104 "too much, expect problems!",
1105 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001106 }
1107 rlen = packlen;
1108 }
1109
1110 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1111 * here. For MTP devices splitting header and data it might
1112 * be 12.
1113 */
1114 /* Evaluate full data length. */
1115 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1116
1117 /* autodetect split header/data MTP devices */
1118 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1119 params->split_header_data = 1;
1120
Richard Low02724f62007-02-17 09:47:26 +00001121 data = malloc(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ);
Linus Walleija0323272007-01-07 12:21:30 +00001122 /* Copy first part of data to 'data' */
1123 handler->putfunc(
1124 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1125 &written
1126 );
Richard Low4fd59d62007-03-03 16:34:37 +00001127
Linus Walleij049f50c2007-03-03 20:30:43 +00001128 if (((PTP_USB *)params->data)->device_flags & DEVICE_FLAG_NO_ZERO_READS &&
1129 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
Richard Low4fd59d62007-03-03 16:34:37 +00001130#ifdef ENABLE_USB_DEBUG
Linus Walleij049f50c2007-03-03 20:30:43 +00001131 printf("Reading in extra terminating byte\n");
Richard Low4fd59d62007-03-03 16:34:37 +00001132#endif
Linus Walleij049f50c2007-03-03 20:30:43 +00001133 // need to read in extra byte and discard it
1134 int result = 0;
1135 char byte = 0;
1136 PTP_USB *ptp_usb = (PTP_USB *)params->data;
1137 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptpcam_usb_timeout);
1138
1139 if (result != 1)
1140 printf("Could not read in extra byte for PTP_USB_BULK_HS_MAX_PACKET_LEN_READ long file, return value 0x%04x\n", result);
1141 }
1142
Linus Walleija0323272007-01-07 12:21:30 +00001143 /* Is that all of data? */
1144 if (len+PTP_USB_BULK_HDR_LEN<=rlen) break;
Linus Walleij049f50c2007-03-03 20:30:43 +00001145
1146 ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1147 handler,
1148 params->data, &rlen, 1);
1149
Linus Walleija0323272007-01-07 12:21:30 +00001150 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001151 ret = PTP_ERROR_IO;
1152 break;
Linus Walleija0323272007-01-07 12:21:30 +00001153 }
1154 } while (0);
Linus Walleij049f50c2007-03-03 20:30:43 +00001155 /*
Linus Walleija0323272007-01-07 12:21:30 +00001156 if (ret!=PTP_RC_OK) {
1157 ptp_error (params,
1158 "PTP: request code 0x%04x getting data error 0x%04x",
1159 ptp->Code, ret);
1160 }*/
1161 return ret;
1162}
1163
1164uint16_t
1165ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1166{
1167 uint16_t ret;
1168 unsigned long rlen;
1169 PTPUSBBulkContainer usbresp;
1170
1171 memset(&usbresp,0,sizeof(usbresp));
1172 /* read response, it should never be longer than sizeof(usbresp) */
1173 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1174
1175 if (ret!=PTP_RC_OK) {
1176 ret = PTP_ERROR_IO;
1177 } else
1178 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1179 ret = PTP_ERROR_RESP_EXPECTED;
1180 } else
1181 if (dtoh16(usbresp.code)!=resp->Code) {
1182 ret = dtoh16(usbresp.code);
1183 }
1184 if (ret!=PTP_RC_OK) {
1185/* ptp_error (params,
1186 "PTP: request code 0x%04x getting resp error 0x%04x",
1187 resp->Code, ret);*/
1188 return ret;
1189 }
1190 /* build an appropriate PTPContainer */
1191 resp->Code=dtoh16(usbresp.code);
1192 resp->SessionID=params->session_id;
1193 resp->Transaction_ID=dtoh32(usbresp.trans_id);
1194 resp->Param1=dtoh32(usbresp.payload.params.param1);
1195 resp->Param2=dtoh32(usbresp.payload.params.param2);
1196 resp->Param3=dtoh32(usbresp.payload.params.param3);
1197 resp->Param4=dtoh32(usbresp.payload.params.param4);
1198 resp->Param5=dtoh32(usbresp.payload.params.param5);
1199 return ret;
1200}
1201
1202/* Event handling functions */
1203
1204/* PTP Events wait for or check mode */
1205#define PTP_EVENT_CHECK 0x0000 /* waits for */
1206#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1207
1208static inline uint16_t
1209ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1210{
1211 uint16_t ret;
1212 int result;
1213 unsigned long rlen;
1214 PTPUSBEventContainer usbevent;
1215 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1216
1217 memset(&usbevent,0,sizeof(usbevent));
1218
1219 if ((params==NULL) || (event==NULL))
1220 return PTP_ERROR_BADPARAM;
1221 ret = PTP_RC_OK;
1222 switch(wait) {
1223 case PTP_EVENT_CHECK:
1224 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1225 if (result==0)
1226 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1227 if (result < 0) ret = PTP_ERROR_IO;
1228 break;
1229 case PTP_EVENT_CHECK_FAST:
1230 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1231 if (result==0)
1232 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1233 if (result < 0) ret = PTP_ERROR_IO;
1234 break;
1235 default:
1236 ret=PTP_ERROR_BADPARAM;
1237 break;
1238 }
1239 if (ret!=PTP_RC_OK) {
1240 ptp_error (params,
1241 "PTP: reading event an error 0x%04x occurred", ret);
1242 return PTP_ERROR_IO;
1243 }
1244 rlen = result;
1245 if (rlen < 8) {
1246 ptp_error (params,
1247 "PTP: reading event an short read of %ld bytes occurred", rlen);
1248 return PTP_ERROR_IO;
1249 }
1250 /* if we read anything over interrupt endpoint it must be an event */
1251 /* build an appropriate PTPContainer */
1252 event->Code=dtoh16(usbevent.code);
1253 event->SessionID=params->session_id;
1254 event->Transaction_ID=dtoh32(usbevent.trans_id);
1255 event->Param1=dtoh32(usbevent.param1);
1256 event->Param2=dtoh32(usbevent.param2);
1257 event->Param3=dtoh32(usbevent.param3);
1258 return ret;
1259}
1260
1261uint16_t
1262ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1263
1264 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1265}
1266
1267uint16_t
1268ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1269
1270 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1271}
1272
1273
Linus Walleij9eb3d312006-08-04 19:25:59 +00001274static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001275{
Linus Walleijd6a49972006-05-02 08:24:54 +00001276 usb_dev_handle *device_handle;
1277
Linus Walleijd6a49972006-05-02 08:24:54 +00001278 params->error_func=NULL;
1279 params->debug_func=NULL;
1280 params->sendreq_func=ptp_usb_sendreq;
1281 params->senddata_func=ptp_usb_senddata;
1282 params->getresp_func=ptp_usb_getresp;
1283 params->getdata_func=ptp_usb_getdata;
1284 params->data=ptp_usb;
1285 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001286 /*
1287 * This is hardcoded here since we have no devices whatsoever that are BE.
1288 * Change this the day we run into our first BE device (if ever).
1289 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001290 params->byteorder = PTP_DL_LE;
1291
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001292 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001293 if (!device_handle) {
1294 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001295 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001296 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001297 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001298#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1299 /*
1300 * If this device is known to be wrongfully claimed by other kernel
1301 * drivers (such as mass storage), then try to unload it to make it
1302 * accessible from user space.
1303 */
1304 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
1305 if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij7beba572006-11-29 08:56:12 +00001306 // Totally ignore this error!
1307 // perror("usb_detach_kernel_driver_np()");
Linus Walleij501ba4d2006-10-16 06:17:04 +00001308 }
1309 }
1310#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001311#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001312 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001313 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1314 perror("usb_set_configuration()");
1315 return -1;
1316 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001317#endif
Linus Walleijef5cca92006-05-05 07:19:08 +00001318 if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001319 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001320 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001321 }
Linus Walleijc6210fb2006-05-08 11:11:41 +00001322 ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber;
Linus Walleijd6a49972006-05-02 08:24:54 +00001323 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001324 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001325}
1326
Linus Walleijd6a49972006-05-02 08:24:54 +00001327static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001328{
Linus Walleij58b62792007-01-07 12:38:59 +00001329 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001330 int ret;
1331
1332 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001333 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001334 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001335 if (ret<0) {
1336 perror ("inep: usb_get_endpoint_status()");
1337 } else if (status) {
1338 printf("Clearing stall on IN endpoint\n");
1339 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1340 if (ret<0) {
1341 perror ("usb_clear_stall_feature()");
1342 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001343 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001344
1345 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001346 status=0;
1347 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1348 if (ret<0) {
1349 perror("outep: usb_get_endpoint_status()");
1350 } else if (status) {
1351 printf("Clearing stall on OUT endpoint\n");
1352 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1353 if (ret<0) {
1354 perror("usb_clear_stall_feature()");
1355 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001356 }
Linus Walleij58b62792007-01-07 12:38:59 +00001357
1358 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1359}
1360
1361static void clear_halt(PTP_USB* ptp_usb)
1362{
1363 int ret;
1364
1365 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1366 if (ret<0) {
1367 perror("usb_clear_halt() on IN endpoint");
1368 }
1369 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1370 if (ret<0) {
1371 perror("usb_clear_halt() on OUT endpoint");
1372 }
1373 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1374 if (ret<0) {
1375 perror("usb_clear_halt() on INTERRUPT endpoint");
1376 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001377}
1378
Linus Walleijd6a49972006-05-02 08:24:54 +00001379static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001380{
Linus Walleij58b62792007-01-07 12:38:59 +00001381 // Clear any stalled endpoints
Linus Walleijd6a49972006-05-02 08:24:54 +00001382 clear_stall(ptp_usb);
Linus Walleij58b62792007-01-07 12:38:59 +00001383 // Clear halts on any endpoints
1384 clear_halt(ptp_usb);
Linus Walleijd6a49972006-05-02 08:24:54 +00001385 // Added to clear some stuff on the OUT endpoint
1386 // TODO: is this good on the Mac too?
1387 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1388 usb_release_interface(ptp_usb->handle, interfaceNumber);
1389 // Brutally reset device
1390 // TODO: is this good on the Mac too?
1391 usb_reset(ptp_usb->handle);
1392 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001393}
1394
Linus Walleij6946ac52006-03-21 06:51:22 +00001395/**
Linus Walleij550d6d52007-01-24 14:32:51 +00001396 * This function scans through the connected usb devices on a machine and
1397 * if they match known Vendor and Product identifiers appends them to the
Linus Walleij45a86372007-03-07 09:36:19 +00001398 * dynamic array mtp_device_list. Be sure to call
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001399 * <code>free(mtp_device_list)</code> when you are done with it, assuming it
Linus Walleij550d6d52007-01-24 14:32:51 +00001400 * is not NULL.
1401 *
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001402 * @param mtp_device_list dynamic array of pointers to usb devices known to
1403 * be valid MTP devices
Linus Walleij550d6d52007-01-24 14:32:51 +00001404 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001405 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no devices have
Linus Walleij45a86372007-03-07 09:36:19 +00001406 * been found.
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001407 */
Linus Walleij45a86372007-03-07 09:36:19 +00001408static LIBMTP_error_number_t get_mtp_usb_known_devices(mtpdevice_list_t **mtp_device_list)
Linus Walleij550d6d52007-01-24 14:32:51 +00001409{
1410 /* Scan through all attached usb and devices */
1411 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001412 for(; bus != NULL; bus = bus->next) {
Linus Walleij550d6d52007-01-24 14:32:51 +00001413 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001414 for(; dev != NULL; dev = dev->next) {
Linus Walleij27c50c52007-03-06 20:34:48 +00001415 int i;
1416
1417 for(i = 0; i < mtp_device_table_size; i++) {
1418 if(dev->descriptor.bDeviceClass != USB_CLASS_HUB &&
1419 dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
1420 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
1421 /* Append this usb device to the MTP device list */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001422 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev);
Linus Walleij27c50c52007-03-06 20:34:48 +00001423 break;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001424 }
1425 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001426 }
Linus Walleij550d6d52007-01-24 14:32:51 +00001427 }
1428
1429 /* If nothing was found we end up here. */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001430 if(*mtp_device_list == NULL)
tedbullock433d2172007-02-23 22:39:12 +00001431 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleij550d6d52007-01-24 14:32:51 +00001432 else
1433 return LIBMTP_ERROR_NONE;
1434}
1435
Linus Walleij45a86372007-03-07 09:36:19 +00001436static LIBMTP_error_number_t prime_device_memory(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001437{
Linus Walleij45a86372007-03-07 09:36:19 +00001438 mtpdevice_list_t *tmplist = devlist;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001439
Linus Walleij45a86372007-03-07 09:36:19 +00001440 while (tmplist != NULL) {
tedbullock0f033cb2007-02-14 20:56:54 +00001441 /* Allocate a parameter box */
Linus Walleij45a86372007-03-07 09:36:19 +00001442 tmplist->params = (PTPParams *) malloc(sizeof(PTPParams));
1443 tmplist->ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleijbfcb7922007-03-06 20:14:04 +00001444
tedbullock0f033cb2007-02-14 20:56:54 +00001445 /* Check for allocation Error */
Linus Walleij45a86372007-03-07 09:36:19 +00001446 if(tmplist->params == NULL || tmplist->ptp_usb == NULL) {
Linus Walleijbfcb7922007-03-06 20:14:04 +00001447 /* Error and deallocation of memory will be handled by caller. */
tedbullock0f033cb2007-02-14 20:56:54 +00001448 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1449 }
1450
tedbullock69a445b2007-02-15 07:41:04 +00001451 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleij45a86372007-03-07 09:36:19 +00001452 memset(tmplist->params, 0, sizeof(PTPParams));
1453 memset(tmplist->ptp_usb, 0, sizeof(PTP_USB));
1454 tmplist = tmplist->next;
tedbullock0f033cb2007-02-14 20:56:54 +00001455 }
tedbullock0f033cb2007-02-14 20:56:54 +00001456 return LIBMTP_ERROR_NONE;
1457}
1458
Linus Walleij45a86372007-03-07 09:36:19 +00001459static void assign_known_device_flags(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +00001460{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001461 int i;
Linus Walleij27c50c52007-03-06 20:34:48 +00001462 mtpdevice_list_t *tmplist = devlist;
1463 uint8_t current_device = 0;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001464
tedbullock0f033cb2007-02-14 20:56:54 +00001465 /* Search through known device list and set correct device flags */
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001466 while (tmplist != NULL) {
1467 int device_known = 0;
1468
1469 for(i = 0; i < mtp_device_table_size; i++) {
1470 if(tmplist->libusb_device->descriptor.idVendor == mtp_device_table[i].vendor_id &&
1471 tmplist->libusb_device->descriptor.idProduct == mtp_device_table[i].product_id) {
1472 /* This device is known, assign the correct device flags */
1473 /* Note that ptp_usb[current_device] could potentially be NULL */
Linus Walleij45a86372007-03-07 09:36:19 +00001474 if(tmplist->ptp_usb != NULL) {
1475 tmplist->ptp_usb->device_flags = mtp_device_table[i].device_flags;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001476
1477 /*
1478 * TODO:
1479 * Preferable to not do this with #ifdef ENABLE_USB_BULK_DEBUG but there is
1480 * currently no other compile time debug option
1481 */
1482
1483 device_known = 1;
tedbullock65a05772007-03-03 22:56:56 +00001484#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001485 /* This device is known to the developers */
1486 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is a %s.\n",
1487 current_device + 1,
1488 tmplist->libusb_device->descriptor.idVendor,
1489 tmplist->libusb_device->descriptor.idProduct,
1490 mtp_device_table[i].name);
tedbullock65a05772007-03-03 22:56:56 +00001491#endif
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001492 }
1493 break;
1494 }
tedbullock0f033cb2007-02-14 20:56:54 +00001495 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001496 if (!device_known) {
1497 /* This device is unknown to the developers */
1498 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
1499 current_device + 1,
1500 tmplist->libusb_device->descriptor.idVendor,
1501 tmplist->libusb_device->descriptor.idProduct);
1502 fprintf(stderr, "Please report this VID/PID and the device model to the "
1503 "libmtp development team\n");
1504 }
1505 tmplist = tmplist->next;
Linus Walleijbfcb7922007-03-06 20:14:04 +00001506 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001507 }
tedbullock0f033cb2007-02-14 20:56:54 +00001508}
1509
tedbullock0f033cb2007-02-14 20:56:54 +00001510
Linus Walleij45a86372007-03-07 09:36:19 +00001511static LIBMTP_error_number_t configure_usb_devices(mtpdevice_list_t *devicelist)
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001512{
1513 mtpdevice_list_t *tmplist = devicelist;
1514 uint16_t ret = 0;
1515 uint8_t current_device = 0;
1516
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001517 while (tmplist != NULL) {
Linus Walleij45a86372007-03-07 09:36:19 +00001518 /* This is erroneous, there must be a PTP_USB instance that we can initialize. */
1519 if(tmplist->ptp_usb == NULL) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001520 return LIBMTP_ERROR_MEMORY_ALLOCATION;
tedbullock0f033cb2007-02-14 20:56:54 +00001521 }
1522
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001523 /* TODO: Will this always be little endian? */
Linus Walleij45a86372007-03-07 09:36:19 +00001524 tmplist->params->byteorder = PTP_DL_LE;
1525 tmplist->params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8");
1526 tmplist->params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE");
tedbullock0f033cb2007-02-14 20:56:54 +00001527
Linus Walleij45a86372007-03-07 09:36:19 +00001528 if(tmplist->params->cd_locale_to_ucs2 == (iconv_t) -1 ||
1529 tmplist->params->cd_ucs2_to_locale == (iconv_t) -1) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001530 fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n"
1531 "Too old stdlibc, glibc and libiconv?\n");
Linus Walleij35c0ef12007-03-06 20:49:09 +00001532 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001533 }
1534
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001535 // ep = device->config->interface->altsetting->endpoint;
1536 // no_of_ep = device->config->interface->altsetting->bNumEndpoints;
tedbullock0f033cb2007-02-14 20:56:54 +00001537
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001538 /* Assign endpoints to usbinfo... */
1539 find_endpoints(tmplist->libusb_device,
Linus Walleij45a86372007-03-07 09:36:19 +00001540 &tmplist->ptp_usb->inep,
1541 &tmplist->ptp_usb->inep_maxpacket,
1542 &tmplist->ptp_usb->outep,
1543 &tmplist->ptp_usb->outep_maxpacket,
1544 &tmplist->ptp_usb->intep);
tedbullock0f033cb2007-02-14 20:56:54 +00001545
Linus Walleij45a86372007-03-07 09:36:19 +00001546 /* Attempt to initialize this device */
1547 if (init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) < 0) {
1548 fprintf(stderr, "LIBMTP PANIC: Unable to initialize device %d\n", current_device+1);
1549 // FIXME: perhaps use "continue" to keep trying the other devices.
Linus Walleij35c0ef12007-03-06 20:49:09 +00001550 return LIBMTP_ERROR_CONNECTING;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001551 }
1552
1553 /*
1554 * This works in situations where previous bad applications
1555 * have not used LIBMTP_Release_Device on exit
1556 */
Linus Walleij45a86372007-03-07 09:36:19 +00001557 if ((ret = ptp_opensession(tmplist->params, 1)) == PTP_ERROR_IO) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001558 fprintf(stderr, "PTP_ERROR_IO: Trying again after resetting USB\n");
Linus Walleij45a86372007-03-07 09:36:19 +00001559 close_usb(tmplist->ptp_usb, tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001560
Linus Walleij45a86372007-03-07 09:36:19 +00001561 if(init_ptp_usb(tmplist->params, tmplist->ptp_usb, tmplist->libusb_device) <0) {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001562 fprintf(stderr, "LIBMTP PANIC: Could not open session on device %d\n", current_device+1);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001563 return LIBMTP_ERROR_CONNECTING;
tedbullock0f033cb2007-02-14 20:56:54 +00001564 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001565
1566 /* Device has been reset, try again */
Linus Walleij45a86372007-03-07 09:36:19 +00001567 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001568 }
1569
1570 /* Was the transaction id invalid? Try again */
1571 if (ret == PTP_RC_InvalidTransactionID) {
Linus Walleij45a86372007-03-07 09:36:19 +00001572 fprintf(stderr, "LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
1573 tmplist->params->transaction_id += 10;
1574 ret = ptp_opensession(tmplist->params, 1);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001575 }
1576
1577 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
Linus Walleij45a86372007-03-07 09:36:19 +00001578 fprintf(stderr, "LIBMTP PANIC: Could not open session! "
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001579 "(Return code %d)\n Try to reset the device.\n",
1580 ret);
Linus Walleij45a86372007-03-07 09:36:19 +00001581 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001582 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
tedbullock0f033cb2007-02-14 20:56:54 +00001583 return LIBMTP_ERROR_CONNECTING;
1584 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001585
1586 /* It is permissible to call this before opening the session */
Linus Walleij45a86372007-03-07 09:36:19 +00001587 if (ptp_getdeviceinfo(tmplist->params,
1588 &tmplist->params->deviceinfo) != PTP_RC_OK) {
1589 fprintf(stderr, "LIBMTP PANIC: Could not get device info!\n");
1590 usb_release_interface(tmplist->ptp_usb->handle,
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001591 tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber);
Linus Walleij35c0ef12007-03-06 20:49:09 +00001592 return LIBMTP_ERROR_CONNECTING;
1593 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001594
1595 tmplist = tmplist->next;
1596 current_device++;
tedbullock0f033cb2007-02-14 20:56:54 +00001597 }
1598
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001599 /* Exit with the nice list */
1600 return LIBMTP_ERROR_NONE;
tedbullock0f033cb2007-02-14 20:56:54 +00001601}
1602
Linus Walleij550d6d52007-01-24 14:32:51 +00001603/**
1604 * This function scans through the results of the get_mtp_usb_device_list
1605 * function and attempts to connect to those devices listed using the
1606 * mtp_device_table at the top of the file. Returns a LIBMTP_error_number_t.
1607 *
Linus Walleij45a86372007-03-07 09:36:19 +00001608 * @param devlist a list of devices with primed PTP_USB and params structs.
Linus Walleij550d6d52007-01-24 14:32:51 +00001609 * @return Error Codes as per the type definition
1610 */
Linus Walleij45a86372007-03-07 09:36:19 +00001611LIBMTP_error_number_t find_usb_devices(mtpdevice_list_t **devlist)
Linus Walleij550d6d52007-01-24 14:32:51 +00001612{
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001613 mtpdevice_list_t *mtp_device_list = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001614 LIBMTP_error_number_t ret;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001615
Linus Walleij550d6d52007-01-24 14:32:51 +00001616 /* Recover list of attached USB devices that match MTP criteria */
Linus Walleij45a86372007-03-07 09:36:19 +00001617 ret = get_mtp_usb_device_list (&mtp_device_list);
Linus Walleijac061862007-03-07 08:28:33 +00001618 if (ret == LIBMTP_ERROR_NO_DEVICE_ATTACHED) {
1619 /* Auto-detection did not find any MTP devices, then search known device
1620 * list.
1621 *
1622 * FIXME: if there are both auto-detected and non-autodetected devices
1623 * attached, the non-autodetected ones will not be found!!
1624 * This behaviour is a leftover from the old function that could
1625 * only retrieve ONE device. What we need to do is make a union
1626 * of both lists.
1627 */
Linus Walleij45a86372007-03-07 09:36:19 +00001628 ret = get_mtp_usb_known_devices (&mtp_device_list);
Linus Walleijac061862007-03-07 08:28:33 +00001629 }
1630 if (ret != LIBMTP_ERROR_NONE) {
1631 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001632 }
1633
Linus Walleij45a86372007-03-07 09:36:19 +00001634 // Then prime them
1635 ret = prime_device_memory(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001636 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001637 fprintf(stderr, "LIBMTP PANIC: prime_device_memory() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001638 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001639 }
tedbullock0f033cb2007-02-14 20:56:54 +00001640
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001641 /* Assign specific device flags and detect unknown devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001642 assign_known_device_flags(mtp_device_list);
Linus Walleij550d6d52007-01-24 14:32:51 +00001643
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001644 /* Configure the devices */
Linus Walleij45a86372007-03-07 09:36:19 +00001645 ret = configure_usb_devices(mtp_device_list);
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001646 if(ret) {
Linus Walleijac061862007-03-07 08:28:33 +00001647 fprintf(stderr, "LIBMTP PANIC: configure_usb_devices() error code: %d on line %d\n", ret, __LINE__);
Linus Walleij45a86372007-03-07 09:36:19 +00001648 goto find_usb_devices_error_exit;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001649 }
Linus Walleij550d6d52007-01-24 14:32:51 +00001650
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001651 /* Configure interface numbers */
Linus Walleij550d6d52007-01-24 14:32:51 +00001652 {
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001653 mtpdevice_list_t *tmplist = mtp_device_list;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001654
Linus Walleij45a86372007-03-07 09:36:19 +00001655 while (tmplist != NULL) {
1656 tmplist->interface_number = tmplist->libusb_device->config->interface->altsetting->bInterfaceNumber;
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001657 tmplist = tmplist->next;
Linus Walleij550d6d52007-01-24 14:32:51 +00001658 }
1659 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001660
Linus Walleij45a86372007-03-07 09:36:19 +00001661 /* we're connected to all devices, return the list and OK */
1662 *devlist = mtp_device_list;
1663 return LIBMTP_ERROR_NONE;
1664
1665 find_usb_devices_error_exit:
Linus Walleijf27d1cd2007-03-05 14:23:00 +00001666 if(mtp_device_list != NULL) {
1667 free_mtpdevice_list(mtp_device_list);
1668 mtp_device_list = NULL;
Linus Walleij550d6d52007-01-24 14:32:51 +00001669 }
Linus Walleij45a86372007-03-07 09:36:19 +00001670 *devlist = NULL;
tedbullock0f033cb2007-02-14 20:56:54 +00001671 return ret;
Linus Walleij550d6d52007-01-24 14:32:51 +00001672}
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001673
Linus Walleijd6a49972006-05-02 08:24:54 +00001674static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int *outep_maxpacket, int* intep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001675{
Linus Walleijd6a49972006-05-02 08:24:54 +00001676 int i,n;
1677 struct usb_endpoint_descriptor *ep;
1678
1679 ep = dev->config->interface->altsetting->endpoint;
1680 n=dev->config->interface->altsetting->bNumEndpoints;
1681
1682 for (i=0;i<n;i++) {
1683 if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1684 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1685 USB_ENDPOINT_DIR_MASK)
1686 {
1687 *inep=ep[i].bEndpointAddress;
1688 *inep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001689 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001690 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1691 {
1692 *outep=ep[i].bEndpointAddress;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001693 *outep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijd6a49972006-05-02 08:24:54 +00001694 }
1695 } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1696 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1697 USB_ENDPOINT_DIR_MASK)
1698 {
1699 *intep=ep[i].bEndpointAddress;
1700 }
1701 }
1702 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001703}
1704
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001705void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber)
1706{
Linus Walleijd6a49972006-05-02 08:24:54 +00001707 if (ptp_closesession(params)!=PTP_RC_OK)
1708 fprintf(stderr,"ERROR: Could not close session!\n");
1709 close_usb(ptp_usb, interfaceNumber);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001710}
1711
Linus Walleijd6a49972006-05-02 08:24:54 +00001712static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001713{
Linus Walleijd6a49972006-05-02 08:24:54 +00001714
1715 return (usb_control_msg(ptp_usb->handle,
1716 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
1717 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001718}
1719
Linus Walleijd6a49972006-05-02 08:24:54 +00001720static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001721{
Linus Walleijd6a49972006-05-02 08:24:54 +00001722 return (usb_control_msg(ptp_usb->handle,
1723 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
1724 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001725}