blob: 9de0cd263dce7977cd1f4610957e82912be04603 [file] [log] [blame]
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001/*
Linus Walleij15e344f2006-03-06 15:15:00 +00002 * libusb-glue.c
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00003 *
Linus Walleij15e344f2006-03-06 15:15:00 +00004 * Created by Richard Low on 24/12/2005. (as mtp-utils.c)
Linus Walleija5483642006-03-09 09:20:38 +00005 * Modified by Linus Walleij 2006-03-06
6 * (Notice that Anglo-Saxons use little-endian dates and Swedes use big-endian dates.)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00007 *
Linus Walleij2e1c8a32007-01-22 06:46:08 +00008 * Licensed under the LGPL GNU Lesser General Public License.
9 *
10 * Copyright Richard Low (c) 2005-2007
11 * Copyright Linus Walleij (c) 2006-2007
12 * Copyright Marcus Meissner (c) 2006-2007
13 * Copyright Ted Bullock (c) 2007
14 *
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000015 * This file adds some utils (many copied from ptpcam.c from libptp2) to
16 * use MTP devices. Include mtp-utils.h to use any of the ptp/mtp functions.
17 *
18 */
Linus Walleij7beba572006-11-29 08:56:12 +000019#include "libmtp.h"
20#include "libusb-glue.h"
21#include "util.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000022#include "ptp.h"
Linus Walleij7beba572006-11-29 08:56:12 +000023
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000024#include <errno.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000028#include <usb.h>
29
Linus Walleija0323272007-01-07 12:21:30 +000030#include "ptp-pack.c"
31
Linus Walleije78de6d2006-10-31 14:46:36 +000032/* To enable debug prints, switch on this */
33//#define ENABLE_USB_BULK_DEBUG
34
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000035/* OUR APPLICATION USB URB (2MB) ;) */
36#define PTPCAM_USB_URB 2097152
37
38/* this must not be too short - the original 4000 was not long
39 enough for big file transfers. I imagine the player spends a
40 bit of time gearing up to receiving lots of data. This also makes
41 connecting/disconnecting more reliable */
42#define USB_TIMEOUT 10000
43#define USB_CAPTURE_TIMEOUT 20000
44
45/* USB control message data phase direction */
46#ifndef USB_DP_HTD
47#define USB_DP_HTD (0x00 << 7) /* host to device */
48#endif
49#ifndef USB_DP_DTH
50#define USB_DP_DTH (0x01 << 7) /* device to host */
51#endif
52
53/* USB Feature selector HALT */
54#ifndef USB_FEATURE_HALT
55#define USB_FEATURE_HALT 0x00
56#endif
57
Linus Walleija5483642006-03-09 09:20:38 +000058/*
Linus Walleije8c54642006-03-28 09:45:00 +000059 * MTP device list, trying real bad to get all devices into
60 * this list by stealing from everyone I know.
Linus Walleija5483642006-03-09 09:20:38 +000061 */
Linus Walleij6fd2f082006-03-28 07:19:22 +000062static const LIBMTP_device_entry_t mtp_device_table[] = {
Linus Walleij3d2c7842006-08-18 09:35:08 +000063
64 /*
65 * Creative Technology
Linus Walleijcc4dd482006-11-25 21:58:30 +000066 * Initially the Creative devices was all we supported so these are
67 * the most thoroughly tested devices.
Linus Walleij3d2c7842006-08-18 09:35:08 +000068 */
Linus Walleij0558ac52006-09-07 06:55:03 +000069 { "Creative Zen Vision", 0x041e, 0x411f, DEVICE_FLAG_NONE },
70 { "Creative Portable Media Center", 0x041e, 0x4123, DEVICE_FLAG_NONE },
71 { "Creative Zen Xtra (MTP mode)", 0x041e, 0x4128, DEVICE_FLAG_NONE },
72 { "Second generation Dell DJ", 0x041e, 0x412f, DEVICE_FLAG_NONE },
73 { "Creative Zen Micro (MTP mode)", 0x041e, 0x4130, DEVICE_FLAG_NONE },
74 { "Creative Zen Touch (MTP mode)", 0x041e, 0x4131, DEVICE_FLAG_NONE },
75 { "Dell Pocket DJ (MTP mode)", 0x041e, 0x4132, DEVICE_FLAG_NONE },
76 { "Creative Zen Sleek (MTP mode)", 0x041e, 0x4137, DEVICE_FLAG_NONE },
77 { "Creative Zen MicroPhoto", 0x041e, 0x413c, DEVICE_FLAG_NONE },
78 { "Creative Zen Sleek Photo", 0x041e, 0x413d, DEVICE_FLAG_NONE },
79 { "Creative Zen Vision:M", 0x041e, 0x413e, DEVICE_FLAG_NONE },
Linus Walleijbdd006b2006-09-15 04:54:13 +000080 // Reported by marazm@o2.pl
81 { "Creative Zen V", 0x041e, 0x4150, DEVICE_FLAG_NONE },
Linus Walleij92a84962006-11-11 20:04:37 +000082 // Reported by danielw@iinet.net.au
83 { "Creative Zen Vision:M (DVP-HD0004)", 0x041e, 0x4151, DEVICE_FLAG_NONE },
Linus Walleij550bc672006-10-10 11:11:29 +000084 // Reported by Darel on the XNJB forums
Richard Lowe1f06922006-11-12 16:38:39 +000085 { "Creative Zen V Plus", 0x041e, 0x4152, DEVICE_FLAG_NONE },
86 { "Creative Zen Vision W", 0x041e, 0x4153, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000087
88 /*
89 * Samsung
Linus Walleij0558ac52006-09-07 06:55:03 +000090 * We suspect that more of these are dual mode.
Linus Walleij3d2c7842006-08-18 09:35:08 +000091 */
92 // From libgphoto2
Linus Walleij0558ac52006-09-07 06:55:03 +000093 { "Samsung YH-820", 0x04e8, 0x502e, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000094 // Contributed by polux2001@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +000095 { "Samsung YH-925", 0x04e8, 0x502f, DEVICE_FLAG_NONE },
Richard Low7059ed42007-01-21 09:33:34 +000096 // Contributed by aronvanammers on SourceForge
97 { "Samsung YH-925GS", 0x04e8, 0x5024, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000098 // Contributed by anonymous person on SourceForge
Linus Walleij0558ac52006-09-07 06:55:03 +000099 { "Samsung YP-T7J", 0x04e8, 0x5047, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000100 // Reported by cstrickler@gmail.com
Linus Walleij0558ac52006-09-07 06:55:03 +0000101 { "Samsung YP-U2J (YP-U2JXB/XAA)", 0x04e8, 0x5054, DEVICE_FLAG_NONE },
Linus Walleij82ed6612006-08-31 08:06:19 +0000102 // Reported by Andrew Benson
Linus Walleij0558ac52006-09-07 06:55:03 +0000103 { "Samsung YP-F2J", 0x04e8, 0x5057, DEVICE_FLAG_DUALMODE },
Linus Walleij19be4212006-11-16 09:00:48 +0000104 // Reported by Patrick <skibler@gmail.com>
Linus Walleijabdf6072006-11-24 07:34:02 +0000105 { "Samsung YP-K5", 0x04e8, 0x505a, DEVICE_FLAG_NONE },
Linus Walleija5073c72006-11-24 07:55:12 +0000106 // Reported by Matthew Wilcox <matthew@wil.cx>
Richard Lowc046fdf2006-11-24 09:19:33 +0000107 { "Samsung Yepp T9", 0x04e8, 0x507f, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000108 // From a rouge .INF file
Linus Walleij0558ac52006-09-07 06:55:03 +0000109 { "Samsung YH-999 Portable Media Center", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000110
111 /*
112 * Intel
113 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000114 { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000115
116 /*
117 * JVC
118 */
119 // From Mark Veinot
Linus Walleij0558ac52006-09-07 06:55:03 +0000120 { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000121
122 /*
Linus Walleij093ca0a2006-11-02 21:15:00 +0000123 * Philips
Linus Walleij3d2c7842006-08-18 09:35:08 +0000124 */
125 // From libgphoto2 source
Linus Walleij093ca0a2006-11-02 21:15:00 +0000126 { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE },
Richard Low7059ed42007-01-21 09:33:34 +0000127 { "Philips HDD6320/00 & HDD6330/17", 0x0471, 0x014b, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000128 // Anonymous SourceForge user
Linus Walleij093ca0a2006-11-02 21:15:00 +0000129 { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE },
130 // From Gerhard Mekenkamp
131 { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE },
Richard Lowa6c924c2006-12-29 17:44:28 +0000132 // from XNJB forum
133 { "Philips GoGear SA9200", 0x0471, 0x014f, DEVICE_FLAG_NONE },
Richard Lowa98d0542007-01-20 12:31:07 +0000134 // from XNJB user
135 { "Philips PSA235", 0x0471, 0x7e01, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000136
137 /*
138 * SanDisk
139 */
140 // Reported by Brian Robison
Linus Walleij0558ac52006-09-07 06:55:03 +0000141 { "SanDisk Sansa m240", 0x0781, 0x7400, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000142 // Reported by tangent_@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +0000143 { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000144 // From libgphoto2 source
Linus Walleij0558ac52006-09-07 06:55:03 +0000145 { "SanDisk Sansa e200", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000146 // Reported by gonkflea@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +0000147 { "SanDisk Sansa e260", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij1df47cd2007-01-24 10:19:52 +0000148 // Reported by anonymous user at sourceforge.net
149 { "SanDisk Sansa c250", 0x0781, 0x7450, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000150
151 /*
152 * iRiver
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000153 * we assume that PTP_OC_MTP_GetObjPropList is essentially
Linus Walleijfabffc02006-12-17 00:18:57 +0000154 * broken on all iRiver devices, meaning it simply won't return
155 * all properties for a file when asking for metadata 0xffffffff.
156 * Please test on your device if you believe it isn't broken!
157 * Some devices from http://www.mtp-ums.net/viewdeviceinfo.php
Linus Walleij3d2c7842006-08-18 09:35:08 +0000158 */
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000159 { "iRiver Portable Media Center", 0x1006, 0x4002, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
160 { "iRiver Portable Media Center", 0x1006, 0x4003, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000161 // From libgphoto2 source
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000162 { "iRiver T10", 0x4102, 0x1113, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
163 { "iRiver T20 FM", 0x4102, 0x1114, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleijfabffc02006-12-17 00:18:57 +0000164 // This appears at the MTP-UMS site
165 { "iRiver T20", 0x4102, 0x1115, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000166 { "iRiver U10", 0x4102, 0x1116, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
167 { "iRiver T10", 0x4102, 0x1117, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
168 { "iRiver T20", 0x4102, 0x1118, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
169 { "iRiver T30", 0x4102, 0x1119, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij77e047c2006-09-11 19:09:06 +0000170 // Reported by David Wolpoff
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000171 { "iRiver T10 2GB", 0x4102, 0x1120, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij87099812006-12-17 00:29:58 +0000172 // Rough guess this is the MTP device ID...
173 { "iRiver N12", 0x4102, 0x1122, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij04377da2006-08-30 13:03:35 +0000174 // Reported by Adam Torgerson
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000175 { "iRiver Clix", 0x4102, 0x112a, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000176 // Reported by Scott Call
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000177 { "iRiver H10 20GB", 0x4102, 0x2101, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
178 { "iRiver H10", 0x4102, 0x2102, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000179
180 /*
181 * Dell
182 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000183 { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000184
185 /*
186 * Toshiba
187 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000188 { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE },
189 { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE },
Linus Walleij405e2392006-10-23 07:13:49 +0000190 // From libgphoto2
191 { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE },
Linus Walleij402e4bd2006-09-12 07:50:56 +0000192 // Reported by Rob Brown
193 { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE },
Linus Walleijb01d18b2006-12-13 12:49:15 +0000194
Linus Walleij6f680562006-08-19 22:49:33 +0000195 /*
196 * Archos
197 */
198 // Reported by gudul1@users.sourceforge.net
Linus Walleije5430902006-10-08 14:46:03 +0000199 { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE },
200
201 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000202 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000203 * This unit is falsely detected as USB mass storage in Linux
Linus Walleijcc4dd482006-11-25 21:58:30 +0000204 * prior to kernel 2.6.19 (fixed by patch from Alan Stern)
205 * so on older kernels special care is needed to remove the
206 * USB mass storage driver that erroneously binds to the device
207 * interface.
Linus Walleije5430902006-10-08 14:46:03 +0000208 */
Richard Lowc7d48d22006-11-29 18:24:55 +0000209 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER},
210
211 /*
212 * Microsoft
213 */
214 // Reported by Farooq Zaman
215 { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE },
216
217 /*
218 * Sirius
219 */
Linus Walleijb01d18b2006-12-13 12:49:15 +0000220 { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE },
221
222 /*
223 * Canon
224 * This is actually a camera, but it has a Microsoft device descriptor
225 * and reports itself as supporting the MTP extension.
226 */
227 {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE }
Linus Walleije5430902006-10-08 14:46:03 +0000228
Linus Walleija5483642006-03-09 09:20:38 +0000229};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000230static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000231
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000232int ptpcam_usb_timeout = USB_TIMEOUT;
233
Linus Walleijd6a49972006-05-02 08:24:54 +0000234// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000235static struct usb_bus* init_usb();
236static struct usb_device *probe_usb_bus_for_mtp_devices(void);
Linus Walleijd6a49972006-05-02 08:24:54 +0000237static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);
238static struct usb_device* find_device (int busn, int devicen, short force);
239static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);
240static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000241static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000242static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000243static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000244static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
245static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000246
Linus Walleij552ba322007-01-22 11:49:59 +0000247/**
248 * Get a list of the supported USB devices.
249 *
250 * The developers depend on users of this library to constantly
251 * add in to the list of supported devices. What we need is the
252 * device name, USB Vendor ID (VID) and USB Product ID (PID).
253 * put this into a bug ticket at the project homepage, please.
254 * The VID/PID is used to let e.g. udev lift the device to
255 * console userspace access when it's plugged in.
256 *
257 * @param devices a pointer to a pointer that will hold a device
258 * list after the call to this function, if it was
259 * successful.
260 * @param numdevs a pointer to an integer that will hold the number
261 * of devices in the device list if the call was successful.
262 * @return 0 if the list was successfull retrieved, any other
263 * value means failure.
264 */
265int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000266{
267 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
268 *numdevs = mtp_device_table_size;
269 return 0;
270}
271
Linus Walleij552ba322007-01-22 11:49:59 +0000272
Linus Walleij1fd2d272006-05-08 09:22:01 +0000273static struct usb_bus* init_usb()
274{
275 usb_init();
276 usb_find_busses();
277 usb_find_devices();
278 return (usb_get_busses());
279}
280
281/**
282 * Check for the Microsoft OS device descriptor and returns device struct
283 * if the device is MTP-compliant. The function will only recognize
284 * a single device connected to the USB bus.
285 *
286 * @return an MTP-compliant USB device if one was found, else NULL.
287 */
288static struct usb_device *probe_usb_bus_for_mtp_devices(void)
289{
290 struct usb_bus *bus;
291
292 bus = init_usb();
293 for (; bus; bus = bus->next) {
294 struct usb_device *dev;
295
296 for (dev = bus->devices; dev; dev = dev->next) {
297 usb_dev_handle *devh;
298 unsigned char buf[1024], cmd;
299 int ret;
300
301 devh = usb_open(dev);
302 if (devh == NULL) {
303 continue;
304 }
305
306 // Read the special descripor, if possible...
307 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
308
309 if (ret < 10) {
310 // printf("Device: VID %04x/PID %04x: no extended device property...\n",
311 // dev->descriptor.idVendor,
312 // dev->descriptor.idProduct);
313 usb_close(devh);
314 continue;
315 }
Linus Walleijd5d51c82006-09-11 06:57:50 +0000316
317 // It is atleast 10 bytes...
Linus Walleij1fd2d272006-05-08 09:22:01 +0000318 if (!((buf[2] == 'M') && (buf[4]=='S') && (buf[6]=='F') && (buf[8]=='T'))) {
Linus Walleijd5d51c82006-09-11 06:57:50 +0000319 printf("This is not a Microsoft MTP descriptor...\n");
320 printf("Device response to read device property 0xee:\n");
321 data_dump_ascii (stdout, buf, ret, 0);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000322 usb_close(devh);
323 continue;
324 }
325
326 cmd = buf[16];
327 ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
328 cmd, 0, 4, (char *) buf, sizeof(buf), 1000);
329 if (ret == -1) {
330 //printf("Decice could not respond to control message 1.\n");
331 usb_close(devh);
Linus Walleijd5d51c82006-09-11 06:57:50 +0000332 continue;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000333 }
334
Linus Walleijd5d51c82006-09-11 06:57:50 +0000335 if (ret > 0x15) {
336 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
337 printf("The device has a Microsoft device descriptor, but it's not MTP.\n");
338 printf("This is not an MTP device. Presumable it is USB mass storage\n");
339 printf("with some additional Janus (DRM) support.\n");
340 printf("Device response to control message 1:\n");
341 data_dump_ascii (stdout, buf, ret, 0);
342 continue;
343 }
344 } else {
345 // Not MTP or broken
346 continue;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000347 }
348
349 ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
350 cmd, 0, 5, (char *) buf, sizeof(buf), 1000);
351 if (ret == -1) {
352 //printf("Device could not respond to control message 2.\n");
353 usb_close(devh);
Linus Walleijd5d51c82006-09-11 06:57:50 +0000354 // Return the device anyway, it said previously it was MTP, right?
Linus Walleij1fd2d272006-05-08 09:22:01 +0000355 return dev;
356 }
357
Linus Walleijd5d51c82006-09-11 06:57:50 +0000358 if (ret > 0x15) {
359 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
360 printf("This device does not respond with MTP characteristics on\n");
361 printf("the second device property read (0x05), but will be regarded\n");
362 printf("as MTP anyway.\n");
363 printf("Device response to control message 2:\n");
364 data_dump_ascii (stdout, buf, ret, 0);
365 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000366 }
Linus Walleijd5d51c82006-09-11 06:57:50 +0000367
Linus Walleij1fd2d272006-05-08 09:22:01 +0000368 usb_close(devh);
369 // We can return the device here, it will be the first.
370 // If it was not MTP, the loop continues before it reaches this point.
371 return dev;
372 }
373 }
374 // If nothing was found we end up here.
375 return NULL;
376}
377
378/**
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000379 * Check for the Microsoft OS device descriptor and return device struct
380 * if the device is MTP-compliant. The function will only recognize
381 * a single device connected to the USB bus.
382 * @param MTPDeviceList dynamic array of pointers to usb devices with MTP
383 * properties. Be sure to call free(MTPDeviceList).
384 * @param numdevices number of devices in MTPDeviceList Dynamic Array
385 * @return an MTP-compliant USB device if one was found, else NULL.
386 */
387static LIBMTP_error_number_t get_mtp_usb_device_list(
388 struct usb_device ** MTPDeviceList,
389 uint8_t *numdevices)
390{
391 /* Initialize number of MTP USB devices to 0 */
392 *numdevices = 0;
393 MTPDeviceList = NULL;
394
395 struct usb_bus *bus = init_usb();
396 for (; bus != NULL; bus = bus->next)
397 {
398 struct usb_device *dev = bus->devices;
399 for (; dev != NULL; dev = dev->next)
400 {
401 usb_dev_handle *devh;
402 unsigned char buf[1024], cmd;
403 int ret;
404
405 /* Attempt to open Device on this port */
406 devh = usb_open(dev);
407 if (devh == NULL)
408 {
409 /* Could not open this device, continue */
410 continue;
411 }
412
413 /* Read the special descriptor */
414 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
415
416 /* Check if descriptor length is at least 10 bytes */
417 if (ret < 10)
418 {
419 usb_close(devh);
420 continue;
421 }
422
423 /* Check if this device has a Microsoft Descriptor */
424 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
425 (buf[6] == 'F') && (buf[8] == 'T')))
426 {
427 usb_close(devh);
428 continue;
429 }
430
431 /* Check if device responds to control message 1 or if there is an error*/
432 cmd = buf[16];
433 ret = usb_control_msg (devh,
434 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
435 cmd,
436 0,
437 4,
438 (char *) buf,
439 sizeof(buf),
440 1000);
441
442 /* If this is true, the device either isn't MTP or there was an error */
443 if (ret <= 0x15)
444 {
445 /* TODO: If there was an error, flag it and let the user know somehow */
446 /* if(ret == -1) {} */
447 usb_close(devh);
448 continue;
449 }
450
451 /* Check if device is MTP or if it is something like a USB Mass Storage
452 device with Janus DRM support */
453 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P'))
454 {
455 usb_close(devh);
456 continue;
457 }
458
459 /* After this point we are probably dealing with an MTP device */
460
461 /* Check if device responds to control message 2 or if there is an error*/
462 ret = usb_control_msg (devh,
463 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
464 cmd,
465 0,
466 5,
467 (char *) buf,
468 sizeof(buf),
469 1000);
470
471 /* If this is true, the device errored against control message 2 */
472 if (ret == -1)
473 {
474 /* TODO: Implement callback function to let managing program know there
475 was a problem, along with description of the problem */
476 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
477 "ProductID:%04x encountered an error responding to "
478 "control message 2. Problems may arrise but continuing",
479 dev->descriptor.idVendor, dev->descriptor.idProduct);
480 }
481 else if (ret <= 0x15)
482 {
483 /* TODO: Implement callback function to let managing program know there
484 was a problem, along with description of the problem */
485 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
486 "ProductID:%04x responded to control message 2 with a "
487 "response that was too short. Problems may arrise but "
488 "continuing",
489 dev->descriptor.idVendor, dev->descriptor.idProduct);
490 }
491 else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P'))
492 {
493 /* TODO: Implement callback function to let managing program know there
494 was a problem, along with description of the problem */
495 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
496 "ProductID:%04x encountered an error responding to "
497 "control message 2. Problems may arrise but continuing",
498 dev->descriptor.idVendor, dev->descriptor.idProduct);
499 }
500
501 /* Close the USB device handle */
502 usb_close(devh);
503
504 /* Append this usb device to the MTP USB Device List */
505 if(MTPDeviceList == NULL)
506 {
507 MTPDeviceList = (struct usb_device **)malloc(sizeof(void *));
508 /* Check for allocation Error */
509 if(MTPDeviceList == NULL)
510 {
511 /* TODO: Implement callback function to let managing applications know
512 there was a memory allocation problem */
513 fprintf(stderr, "Memory Allocation Problem: unable to connect MTP "
514 "Device with VendorID:%04x and ProductID:%04x. ",
515 dev->descriptor.idVendor, dev->descriptor.idProduct);
516 return LIBMTP_ERROR_MEMORY_ALLOCATION;
517 }
518 MTPDeviceList[0] = dev;
519 (*numdevices)++;
520 }
521 else
522 {
523 MTPDeviceList = (struct usb_device **)realloc(
524 MTPDeviceList,
525 *numdevices + 1);
526
527 /* Check for allocation Error */
528 if(MTPDeviceList == NULL)
529 {
530 /* TODO: Implement callback function to let managing applications know
531 there was a memory allocation problem */
532 fprintf(stderr, "Memory Allocation Problem: unable to connect MTP "
533 "Device with VendorID:%04x and ProductID:%04x. ",
534 dev->descriptor.idVendor, dev->descriptor.idProduct);
535 return LIBMTP_ERROR_MEMORY_ALLOCATION;
536 }
537 MTPDeviceList[*numdevices] = dev;
538 (*numdevices)++;
539 }
540 }
541 }
542
543 /* If nothing was found we end up here. */
544 if(MTPDeviceList == NULL)
545 return LIBMTP_ERROR_N0_DEVICE_ATTACHED;
546 else
547 return LIBMTP_ERROR_NONE;
548}
549
550/**
Linus Walleij1fd2d272006-05-08 09:22:01 +0000551 * Detect the MTP device descriptor and return the VID and PID
552 * of the first device found. This is a very low-level function
553 * which is intended for use with <b>udev</b> or other hotplug
554 * mechanisms. The idea is that a script may want to know if the
555 * just plugged-in device was an MTP device or not.
556 * @param vid the Vendor ID (VID) of the first device found.
557 * @param pid the Product ID (PID) of the first device found.
558 * @return the number of detected devices or -1 if the call
559 * was unsuccessful.
560 */
561int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
562{
563 struct usb_device *dev = probe_usb_bus_for_mtp_devices();
564 if (dev == NULL) {
565 return 0;
566 }
567 *vid = dev->descriptor.idVendor;
568 *pid = dev->descriptor.idProduct;
569 return 1;
570}
571
Linus Walleijc6210fb2006-05-08 11:11:41 +0000572/**
573 * This routine just dumps out low-level
574 * USB information about the current device.
575 * @param ptp_usb the USB device to get information from.
576 */
577void dump_usbinfo(PTP_USB *ptp_usb)
578{
579 int res;
580 struct usb_device *dev;
581
582#ifdef LIBUSB_HAS_GET_DRIVER_NP
583 char devname[0x10];
584
585 devname[0] = '\0';
586 res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));
587 if (devname[0] != '\0') {
588 printf(" Using kernel interface \"%s\"\n", devname);
589 }
590#endif
591 dev = usb_device(ptp_usb->handle);
592 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
593 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
594 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
595 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000596 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
597 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000598 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
599 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000600 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000601 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
602}
603
Linus Walleija0323272007-01-07 12:21:30 +0000604static void
605ptp_debug (PTPParams *params, const char *format, ...)
606{
607 va_list args;
608
609 va_start (args, format);
610 if (params->debug_func!=NULL)
611 params->debug_func (params->data, format, args);
612 else
613 {
614 vfprintf (stderr, format, args);
615 fprintf (stderr,"\n");
616 fflush (stderr);
617 }
618 va_end (args);
619}
620
621static void
622ptp_error (PTPParams *params, const char *format, ...)
623{
624 va_list args;
625
626 va_start (args, format);
627 if (params->error_func!=NULL)
628 params->error_func (params->data, format, args);
629 else
630 {
631 vfprintf (stderr, format, args);
632 fprintf (stderr,"\n");
633 fflush (stderr);
634 }
635 va_end (args);
636}
637
Linus Walleijc6210fb2006-05-08 11:11:41 +0000638
Linus Walleij99ed83c2007-01-02 18:46:02 +0000639/*
640 * ptp_read_func() and ptp_write_func() are
641 * based on same functions in library.c in libgphoto2.
642 * Much reading packet logs and having fun with trials and errors
643 * reveals that WMP / Windows is probably using an algorithm like this
644 * for large transfers:
645 *
646 * 1. Send the command (0x0c bytes) if headers are split, else, send
647 * command plus sizeof(endpoint) - 0x0c bytes.
648 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
649 * split headers. Else goto 3.
650 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
651 * We call 0x10000 CONTEXT_BLOCK_SIZE.
652 * 4. Send remaining bytes MOD sizeof(endpoint)
653 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
654 * then also send a zero-length package.
655 */
Richard Low021421e2007-01-01 18:08:57 +0000656#define CONTEXT_BLOCK_SIZE 0x10000
Linus Walleijb02a0662006-04-25 08:05:09 +0000657static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000658ptp_read_func (
659 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000660 unsigned long *readbytes,
661 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000662) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000663 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000664 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000665 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000666 unsigned long curread = 0;
667 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000668 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000669
670 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000671 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000672 while (curread < size) {
673 toread = size - curread;
Linus Walleijc49c6372007-01-09 00:18:51 +0000674 if (toread > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000675 toread = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000676 } else if (toread > ptp_usb->outep_maxpacket) {
Richard Lowb89ea942007-01-02 11:47:19 +0000677 toread -= toread % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000678 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000679
680 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout);
Richard Low4df32f82007-01-11 20:04:39 +0000681
Linus Walleijc49c6372007-01-09 00:18:51 +0000682 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000683 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000684 }
Linus Walleije78de6d2006-10-31 14:46:36 +0000685#ifdef ENABLE_USB_BULK_DEBUG
686 printf("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000687 if (result == 0)
688 printf("Zero Read\n");
689 else
690 data_dump_ascii (stdout,bytes,result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000691#endif
Linus Walleijc49c6372007-01-09 00:18:51 +0000692 handler->putfunc(NULL, handler->private, result, bytes, &written);
693 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000694 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000695
696 // Increase counters, call callback
697 if (ptp_usb->callback_active) {
698 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
699 // send last update and disable callback.
700 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
701 ptp_usb->callback_active = 0;
702 }
703 if (ptp_usb->current_transfer_callback != NULL) {
704 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
705 ptp_usb->current_transfer_total,
706 ptp_usb->current_transfer_callback_data);
707 }
708 }
709
Linus Walleijd6a49972006-05-02 08:24:54 +0000710 if (result < toread) /* short reads are common */
711 break;
712 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000713 if (readbytes) *readbytes = curread;
714 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000715
Richard Low4df32f82007-01-11 20:04:39 +0000716 // there might be a zero packet waiting for us...
717 if (readzero && curread % ptp_usb->outep_maxpacket == 0) {
718 char temp;
719 int zeroresult = 0;
720#ifdef ENABLE_USB_BULK_DEBUG
721 printf("<==USB IN\n");
722 printf("Zero Read\n");
723#endif
724 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptpcam_usb_timeout);
725 if (zeroresult != 0)
726 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
727 }
728
Linus Walleijd6a49972006-05-02 08:24:54 +0000729 if (result > 0) {
Linus Walleijd6a49972006-05-02 08:24:54 +0000730 return (PTP_RC_OK);
731 } else {
732 return PTP_ERROR_IO;
733 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000734}
735
Linus Walleijb02a0662006-04-25 08:05:09 +0000736static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000737ptp_write_func (
738 unsigned long size,
739 PTPDataHandler *handler,
740 void *data,
741 unsigned long *written
742) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000743 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000744 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000745 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000746 unsigned long curwrite = 0;
747 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000748
749 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000750 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000751 if (!bytes) {
752 return PTP_ERROR_IO;
753 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000754 while (curwrite < size) {
755 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000756 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000757 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000758 } else {
759 // This magic makes packets the same size that WMP send them.
760 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000761 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000762 }
763 }
764 handler->getfunc(NULL, handler->private,towrite,bytes,&towrite);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000765 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000766#ifdef ENABLE_USB_BULK_DEBUG
767 printf("USB OUT==>\n");
Richard Lowedd61832006-12-30 12:38:18 +0000768 data_dump_ascii (stdout,bytes,towrite,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000769#endif
Linus Walleijc49c6372007-01-09 00:18:51 +0000770 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000771 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000772 }
773 // Increase counters
774 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000775 curwrite += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000776
777 // call callback
778 if (ptp_usb->callback_active) {
779 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
780 // send last update and disable callback.
781 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
782 ptp_usb->callback_active = 0;
783 }
784 if (ptp_usb->current_transfer_callback != NULL) {
785 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
786 ptp_usb->current_transfer_total,
787 ptp_usb->current_transfer_callback_data);
788 }
789 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000790 if (result < towrite) /* short writes happen */
791 break;
792 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000793 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000794 if (written) {
795 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000796 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000797
Linus Walleijc49c6372007-01-09 00:18:51 +0000798
Richard Lowef05b892007-01-03 21:18:56 +0000799 // If this is the last transfer send a zero write if required
800 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000801 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
Richard Low021421e2007-01-01 18:08:57 +0000802#ifdef ENABLE_USB_BULK_DEBUG
803 printf("USB OUT==>\n");
804 printf("Zero Write\n");
805#endif
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000806 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000807 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000808 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000809
Linus Walleijd6a49972006-05-02 08:24:54 +0000810 if (result < 0)
811 return PTP_ERROR_IO;
812 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000813}
814
Linus Walleija0323272007-01-07 12:21:30 +0000815/* memory data get/put handler */
816typedef struct {
817 unsigned char *data;
818 unsigned long size, curoff;
819} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000820
Linus Walleija0323272007-01-07 12:21:30 +0000821static uint16_t
822memory_getfunc(PTPParams* params, void* private,
823 unsigned long wantlen, unsigned char *data,
824 unsigned long *gotlen
825) {
826 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
827 unsigned long tocopy = wantlen;
828
829 if (priv->curoff + tocopy > priv->size)
830 tocopy = priv->size - priv->curoff;
831 memcpy (data, priv->data + priv->curoff, tocopy);
832 priv->curoff += tocopy;
833 *gotlen = tocopy;
834 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000835}
836
Linus Walleija0323272007-01-07 12:21:30 +0000837static uint16_t
838memory_putfunc(PTPParams* params, void* private,
839 unsigned long sendlen, unsigned char *data,
840 unsigned long *putlen
841) {
842 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
843
844 if (priv->curoff + sendlen > priv->size) {
845 priv->data = realloc (priv->data, priv->curoff+sendlen);
846 priv->size = priv->curoff + sendlen;
847 }
848 memcpy (priv->data + priv->curoff, data, sendlen);
849 priv->curoff += sendlen;
850 *putlen = sendlen;
851 return PTP_RC_OK;
852}
853
854/* init private struct for receiving data. */
855static uint16_t
856ptp_init_recv_memory_handler(PTPDataHandler *handler) {
857 PTPMemHandlerPrivate* priv;
858 priv = malloc (sizeof(PTPMemHandlerPrivate));
859 handler->private = priv;
860 handler->getfunc = memory_getfunc;
861 handler->putfunc = memory_putfunc;
862 priv->data = NULL;
863 priv->size = 0;
864 priv->curoff = 0;
865 return PTP_RC_OK;
866}
867
868/* init private struct and put data in for sending data.
869 * data is still owned by caller.
870 */
871static uint16_t
872ptp_init_send_memory_handler(PTPDataHandler *handler,
873 unsigned char *data, unsigned long len
874) {
875 PTPMemHandlerPrivate* priv;
876 priv = malloc (sizeof(PTPMemHandlerPrivate));
877 if (!priv)
878 return PTP_RC_GeneralError;
879 handler->private = priv;
880 handler->getfunc = memory_getfunc;
881 handler->putfunc = memory_putfunc;
882 priv->data = data;
883 priv->size = len;
884 priv->curoff = 0;
885 return PTP_RC_OK;
886}
887
888/* free private struct + data */
889static uint16_t
890ptp_exit_send_memory_handler (PTPDataHandler *handler) {
891 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
892 /* data is owned by caller */
893 free (priv);
894 return PTP_RC_OK;
895}
896
897/* hand over our internal data to caller */
898static uint16_t
899ptp_exit_recv_memory_handler (PTPDataHandler *handler,
900 unsigned char **data, unsigned long *size
901) {
902 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
903 *data = priv->data;
904 *size = priv->size;
905 free (priv);
906 return PTP_RC_OK;
907}
908
909/* send / receive functions */
910
911uint16_t
912ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
913{
914 uint16_t ret;
915 PTPUSBBulkContainer usbreq;
916 PTPDataHandler memhandler;
917 unsigned long written, towrite;
918
919 /* build appropriate USB container */
920 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
921 (sizeof(uint32_t)*(5-req->Nparam)));
922 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
923 usbreq.code=htod16(req->Code);
924 usbreq.trans_id=htod32(req->Transaction_ID);
925 usbreq.payload.params.param1=htod32(req->Param1);
926 usbreq.payload.params.param2=htod32(req->Param2);
927 usbreq.payload.params.param3=htod32(req->Param3);
928 usbreq.payload.params.param4=htod32(req->Param4);
929 usbreq.payload.params.param5=htod32(req->Param5);
930 /* send it to responder */
931 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
932 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
933 ret=ptp_write_func(
934 towrite,
935 &memhandler,
936 params->data,
937 &written
938 );
939 ptp_exit_send_memory_handler (&memhandler);
940 if (ret!=PTP_RC_OK) {
941 ret = PTP_ERROR_IO;
942/* ptp_error (params,
943 "PTP: request code 0x%04x sending req error 0x%04x",
944 req->Code,ret); */
945 }
946 if (written != towrite) {
947 ptp_error (params,
948 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
949 written, towrite
950 );
951 ret = PTP_ERROR_IO;
952 }
953 return ret;
954}
955
956uint16_t
957ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
958 unsigned long size, PTPDataHandler *handler
959) {
960 uint16_t ret;
961 int wlen, datawlen;
962 unsigned long written;
963 PTPUSBBulkContainer usbdata;
964 uint32_t bytes_left_to_transfer;
965 PTPDataHandler memhandler;
966
967 /* build appropriate USB container */
968 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
969 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
970 usbdata.code = htod16(ptp->Code);
971 usbdata.trans_id= htod32(ptp->Transaction_ID);
972
973 ((PTP_USB*)params->data)->current_transfer_complete = 0;
974 ((PTP_USB*)params->data)->current_transfer_total = size;
975
976 if (params->split_header_data) {
977 datawlen = 0;
978 wlen = PTP_USB_BULK_HDR_LEN;
979 } else {
980 unsigned long gotlen;
981 /* For all camera devices. */
982 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN)?size:PTP_USB_BULK_PAYLOAD_LEN;
983 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
984 ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen);
985 if (ret != PTP_RC_OK)
986 return ret;
987 if (gotlen != datawlen)
988 return PTP_RC_GeneralError;
989 }
990 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
991 /* send first part of data */
992 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
993 ptp_exit_send_memory_handler (&memhandler);
994 if (ret!=PTP_RC_OK) {
995 ret = PTP_ERROR_IO;
996/* ptp_error (params,
997 "PTP: request code 0x%04x sending data error 0x%04x",
998 ptp->Code,ret);*/
999 return ret;
1000 }
1001 if (size <= datawlen) return ret;
1002 /* if everything OK send the rest */
1003 bytes_left_to_transfer = size-datawlen;
1004 ret = PTP_RC_OK;
1005 while(bytes_left_to_transfer > 0) {
1006 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1007 if (ret != PTP_RC_OK)
1008 break;
1009 if (written == 0) {
1010 ret = PTP_ERROR_IO;
1011 break;
1012 }
1013 bytes_left_to_transfer -= written;
1014 }
1015 if (ret!=PTP_RC_OK)
1016 ret = PTP_ERROR_IO;
1017 return ret;
1018}
1019
1020static uint16_t ptp_usb_getpacket(PTPParams *params,
1021 PTPUSBBulkContainer *packet, unsigned long *rlen)
1022{
1023 PTPDataHandler memhandler;
1024 uint16_t ret;
1025 unsigned char *x = NULL;
1026
1027 /* read the header and potentially the first data */
1028 if (params->response_packet_size > 0) {
1029 /* If there is a buffered packet, just use it. */
1030 memcpy(packet, params->response_packet, params->response_packet_size);
1031 *rlen = params->response_packet_size;
1032 free(params->response_packet);
1033 params->response_packet = NULL;
1034 params->response_packet_size = 0;
1035 /* Here this signifies a "virtual read" */
1036 return PTP_RC_OK;
1037 }
1038 ptp_init_recv_memory_handler (&memhandler);
Richard Low4df32f82007-01-11 20:04:39 +00001039 ret = ptp_read_func( sizeof(*packet), &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001040 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1041 if (x) {
1042 memcpy (packet, x, *rlen);
1043 free (x);
1044 }
1045 return ret;
1046}
1047
1048uint16_t
1049ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1050{
1051 uint16_t ret;
1052 PTPUSBBulkContainer usbdata;
1053 unsigned char *data;
1054 unsigned long written;
1055
1056 memset(&usbdata,0,sizeof(usbdata));
1057 do {
1058 unsigned long len, rlen;
1059
1060 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1061 if (ret!=PTP_RC_OK) {
1062 ret = PTP_ERROR_IO;
1063 break;
1064 } else
1065 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1066 ret = PTP_ERROR_DATA_EXPECTED;
1067 break;
1068 } else
1069 if (dtoh16(usbdata.code)!=ptp->Code) {
1070 ret = dtoh16(usbdata.code);
1071 break;
1072 }
1073 if (usbdata.length == 0xffffffffU) {
1074 /* stuff data directly to passed data handler */
1075 while (1) {
1076 unsigned long readdata;
1077 int xret;
1078
1079 xret = ptp_read_func(
1080 PTP_USB_BULK_HS_MAX_PACKET_LEN,
1081 handler,
1082 params->data,
Richard Low4df32f82007-01-11 20:04:39 +00001083 &readdata,
1084 0
Linus Walleija0323272007-01-07 12:21:30 +00001085 );
1086 if (xret == -1)
1087 return PTP_ERROR_IO;
1088 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN)
1089 break;
1090 }
1091 return PTP_RC_OK;
1092 }
1093 if (rlen > dtoh32(usbdata.length)) {
1094 /*
1095 * Buffer the surplus response packet if it is >=
1096 * PTP_USB_BULK_HDR_LEN
1097 * (i.e. it is probably an entire package)
1098 * else discard it as erroneous surplus data.
1099 * This will even work if more than 2 packets appear
1100 * in the same transaction, they will just be handled
1101 * iteratively.
1102 *
1103 * Marcus observed stray bytes on iRiver devices;
1104 * these are still discarded.
1105 */
1106 unsigned int packlen = dtoh32(usbdata.length);
1107 unsigned int surplen = rlen - packlen;
1108
1109 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1110 params->response_packet = malloc(surplen);
1111 memcpy(params->response_packet,
1112 (uint8_t *) &usbdata + packlen, surplen);
1113 params->response_packet_size = surplen;
1114 } else {
1115 ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes too much, expect problems!", rlen - dtoh32(usbdata.length));
1116 }
1117 rlen = packlen;
1118 }
1119
1120 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1121 * here. For MTP devices splitting header and data it might
1122 * be 12.
1123 */
1124 /* Evaluate full data length. */
1125 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1126
1127 /* autodetect split header/data MTP devices */
1128 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1129 params->split_header_data = 1;
1130
1131 data = malloc(PTP_USB_BULK_HS_MAX_PACKET_LEN);
1132 /* Copy first part of data to 'data' */
1133 handler->putfunc(
1134 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1135 &written
1136 );
1137
1138 /* Is that all of data? */
1139 if (len+PTP_USB_BULK_HDR_LEN<=rlen) break;
1140
1141 /* If not read the rest of it. */
1142 ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1143 handler,
Richard Low4df32f82007-01-11 20:04:39 +00001144 params->data, &rlen, 1);
Linus Walleija0323272007-01-07 12:21:30 +00001145 if (ret!=PTP_RC_OK) {
1146 ret = PTP_ERROR_IO;
1147 break;
1148 }
1149 } while (0);
1150/*
1151 if (ret!=PTP_RC_OK) {
1152 ptp_error (params,
1153 "PTP: request code 0x%04x getting data error 0x%04x",
1154 ptp->Code, ret);
1155 }*/
1156 return ret;
1157}
1158
1159uint16_t
1160ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1161{
1162 uint16_t ret;
1163 unsigned long rlen;
1164 PTPUSBBulkContainer usbresp;
1165
1166 memset(&usbresp,0,sizeof(usbresp));
1167 /* read response, it should never be longer than sizeof(usbresp) */
1168 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1169
1170 if (ret!=PTP_RC_OK) {
1171 ret = PTP_ERROR_IO;
1172 } else
1173 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1174 ret = PTP_ERROR_RESP_EXPECTED;
1175 } else
1176 if (dtoh16(usbresp.code)!=resp->Code) {
1177 ret = dtoh16(usbresp.code);
1178 }
1179 if (ret!=PTP_RC_OK) {
1180/* ptp_error (params,
1181 "PTP: request code 0x%04x getting resp error 0x%04x",
1182 resp->Code, ret);*/
1183 return ret;
1184 }
1185 /* build an appropriate PTPContainer */
1186 resp->Code=dtoh16(usbresp.code);
1187 resp->SessionID=params->session_id;
1188 resp->Transaction_ID=dtoh32(usbresp.trans_id);
1189 resp->Param1=dtoh32(usbresp.payload.params.param1);
1190 resp->Param2=dtoh32(usbresp.payload.params.param2);
1191 resp->Param3=dtoh32(usbresp.payload.params.param3);
1192 resp->Param4=dtoh32(usbresp.payload.params.param4);
1193 resp->Param5=dtoh32(usbresp.payload.params.param5);
1194 return ret;
1195}
1196
1197/* Event handling functions */
1198
1199/* PTP Events wait for or check mode */
1200#define PTP_EVENT_CHECK 0x0000 /* waits for */
1201#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1202
1203static inline uint16_t
1204ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1205{
1206 uint16_t ret;
1207 int result;
1208 unsigned long rlen;
1209 PTPUSBEventContainer usbevent;
1210 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1211
1212 memset(&usbevent,0,sizeof(usbevent));
1213
1214 if ((params==NULL) || (event==NULL))
1215 return PTP_ERROR_BADPARAM;
1216 ret = PTP_RC_OK;
1217 switch(wait) {
1218 case PTP_EVENT_CHECK:
1219 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1220 if (result==0)
1221 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1222 if (result < 0) ret = PTP_ERROR_IO;
1223 break;
1224 case PTP_EVENT_CHECK_FAST:
1225 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1226 if (result==0)
1227 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1228 if (result < 0) ret = PTP_ERROR_IO;
1229 break;
1230 default:
1231 ret=PTP_ERROR_BADPARAM;
1232 break;
1233 }
1234 if (ret!=PTP_RC_OK) {
1235 ptp_error (params,
1236 "PTP: reading event an error 0x%04x occurred", ret);
1237 return PTP_ERROR_IO;
1238 }
1239 rlen = result;
1240 if (rlen < 8) {
1241 ptp_error (params,
1242 "PTP: reading event an short read of %ld bytes occurred", rlen);
1243 return PTP_ERROR_IO;
1244 }
1245 /* if we read anything over interrupt endpoint it must be an event */
1246 /* build an appropriate PTPContainer */
1247 event->Code=dtoh16(usbevent.code);
1248 event->SessionID=params->session_id;
1249 event->Transaction_ID=dtoh32(usbevent.trans_id);
1250 event->Param1=dtoh32(usbevent.param1);
1251 event->Param2=dtoh32(usbevent.param2);
1252 event->Param3=dtoh32(usbevent.param3);
1253 return ret;
1254}
1255
1256uint16_t
1257ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1258
1259 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1260}
1261
1262uint16_t
1263ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1264
1265 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1266}
1267
1268
Linus Walleij9eb3d312006-08-04 19:25:59 +00001269static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001270{
Linus Walleijd6a49972006-05-02 08:24:54 +00001271 usb_dev_handle *device_handle;
1272
Linus Walleijd6a49972006-05-02 08:24:54 +00001273 params->error_func=NULL;
1274 params->debug_func=NULL;
1275 params->sendreq_func=ptp_usb_sendreq;
1276 params->senddata_func=ptp_usb_senddata;
1277 params->getresp_func=ptp_usb_getresp;
1278 params->getdata_func=ptp_usb_getdata;
1279 params->data=ptp_usb;
1280 params->transaction_id=0;
1281 params->byteorder = PTP_DL_LE;
1282
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001283 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001284 if (!device_handle) {
1285 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001286 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001287 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001288 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001289#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1290 /*
1291 * If this device is known to be wrongfully claimed by other kernel
1292 * drivers (such as mass storage), then try to unload it to make it
1293 * accessible from user space.
1294 */
1295 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
1296 if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij7beba572006-11-29 08:56:12 +00001297 // Totally ignore this error!
1298 // perror("usb_detach_kernel_driver_np()");
Linus Walleij501ba4d2006-10-16 06:17:04 +00001299 }
1300 }
1301#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001302#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001303 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001304 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1305 perror("usb_set_configuration()");
1306 return -1;
1307 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001308#endif
Linus Walleijef5cca92006-05-05 07:19:08 +00001309 if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001310 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001311 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001312 }
Linus Walleijc6210fb2006-05-08 11:11:41 +00001313 ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber;
Linus Walleijd6a49972006-05-02 08:24:54 +00001314 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001315 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001316}
1317
Linus Walleijd6a49972006-05-02 08:24:54 +00001318static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001319{
Linus Walleij58b62792007-01-07 12:38:59 +00001320 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001321 int ret;
1322
1323 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001324 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001325 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001326 if (ret<0) {
1327 perror ("inep: usb_get_endpoint_status()");
1328 } else if (status) {
1329 printf("Clearing stall on IN endpoint\n");
1330 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1331 if (ret<0) {
1332 perror ("usb_clear_stall_feature()");
1333 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001334 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001335
1336 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001337 status=0;
1338 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1339 if (ret<0) {
1340 perror("outep: usb_get_endpoint_status()");
1341 } else if (status) {
1342 printf("Clearing stall on OUT endpoint\n");
1343 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1344 if (ret<0) {
1345 perror("usb_clear_stall_feature()");
1346 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001347 }
Linus Walleij58b62792007-01-07 12:38:59 +00001348
1349 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1350}
1351
1352static void clear_halt(PTP_USB* ptp_usb)
1353{
1354 int ret;
1355
1356 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1357 if (ret<0) {
1358 perror("usb_clear_halt() on IN endpoint");
1359 }
1360 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1361 if (ret<0) {
1362 perror("usb_clear_halt() on OUT endpoint");
1363 }
1364 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1365 if (ret<0) {
1366 perror("usb_clear_halt() on INTERRUPT endpoint");
1367 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001368}
1369
Linus Walleijd6a49972006-05-02 08:24:54 +00001370static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001371{
Linus Walleij58b62792007-01-07 12:38:59 +00001372 // Clear any stalled endpoints
Linus Walleijd6a49972006-05-02 08:24:54 +00001373 clear_stall(ptp_usb);
Linus Walleij58b62792007-01-07 12:38:59 +00001374 // Clear halts on any endpoints
1375 clear_halt(ptp_usb);
Linus Walleijd6a49972006-05-02 08:24:54 +00001376 // Added to clear some stuff on the OUT endpoint
1377 // TODO: is this good on the Mac too?
1378 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1379 usb_release_interface(ptp_usb->handle, interfaceNumber);
1380 // Brutally reset device
1381 // TODO: is this good on the Mac too?
1382 usb_reset(ptp_usb->handle);
1383 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001384}
1385
1386
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001387/*
1388 find_device() returns the pointer to a usb_device structure matching
1389 given busn, devicen numbers. If any or both of arguments are 0 then the
1390 first matching PTP device structure is returned.
1391 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001392static struct usb_device* find_device (int busn, int devn, short force)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001393{
Linus Walleijd6a49972006-05-02 08:24:54 +00001394 struct usb_bus *bus;
1395 struct usb_device *dev;
1396
1397 bus=init_usb();
1398 for (; bus; bus = bus->next)
1399 for (dev = bus->devices; dev; dev = dev->next)
1400 /* somtimes dev->config is null, not sure why... */
1401 if (dev->config != NULL)
1402 if (dev->descriptor.bDeviceClass!=USB_CLASS_HUB)
1403 {
1404 int curbusn, curdevn;
1405
1406 curbusn=strtol(bus->dirname,NULL,10);
1407 curdevn=strtol(dev->filename,NULL,10);
1408
1409 if (devn==0) {
1410 if (busn==0) return dev;
1411 if (curbusn==busn) return dev;
1412 } else {
1413 if ((busn==0)&&(curdevn==devn)) return dev;
1414 if ((curbusn==busn)&&(curdevn==devn)) return dev;
1415 }
1416 }
1417 return NULL;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001418}
1419
Linus Walleij6946ac52006-03-21 06:51:22 +00001420/**
1421 * This function scans through the device list to see if there are
1422 * some devices to connect to. The table at the top of this file is
1423 * used to identify potential devices.
1424 */
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001425uint16_t connect_first_device(PTPParams *params, PTP_USB *ptp_usb, uint8_t *interfaceNumber)
1426{
Linus Walleij6946ac52006-03-21 06:51:22 +00001427 struct usb_bus *bus;
1428 struct usb_device *dev;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001429 struct usb_endpoint_descriptor *ep;
1430 PTPDeviceInfo deviceinfo;
1431 uint16_t ret=0;
1432 int n;
Linus Walleij6946ac52006-03-21 06:51:22 +00001433
Linus Walleij0558ac52006-09-07 06:55:03 +00001434 // Reset device flags
1435 ptp_usb->device_flags = DEVICE_FLAG_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001436 // First try to locate the device using the extended
1437 // device descriptor.
1438 dev = probe_usb_bus_for_mtp_devices();
1439
1440 if (dev != NULL) {
1441 int i;
1442
1443 // See if we can find the name of this beast
1444 for (i = 0; i < mtp_device_table_size; i++) {
1445 LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i];
1446 if (dev->descriptor.idVendor == mtp_device->vendor_id &&
1447 dev->descriptor.idProduct == mtp_device->product_id ) {
1448 printf("Autodetected device \"%s\" (VID=%04x,PID=%04x) is known.\n",
1449 mtp_device->name, dev->descriptor.idVendor, dev->descriptor.idProduct);
Linus Walleij0558ac52006-09-07 06:55:03 +00001450 ptp_usb->device_flags = mtp_device->device_flags;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001451 break;
1452 }
1453 }
1454 if (i == mtp_device_table_size) {
1455 printf("Autodetected device with VID=%04x and PID=%04x is UNKNOWN.\n",
1456 dev->descriptor.idVendor, dev->descriptor.idProduct);
1457 printf("Please report this VID/PID and the device model name etc to the\n");
1458 printf("libmtp development team!\n");
1459 }
1460 }
1461
1462 // If autodetection fails, scan the bus for well known devices.
1463 if (dev == NULL) {
1464 bus = init_usb();
1465 for (; bus; bus = bus->next) {
1466 for (dev = bus->devices; dev; dev = dev->next) {
1467 int i;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001468
Linus Walleij1fd2d272006-05-08 09:22:01 +00001469 // Loop over the list of supported devices
1470 for (i = 0; i < mtp_device_table_size; i++) {
1471 LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i];
Linus Walleij6946ac52006-03-21 06:51:22 +00001472
Linus Walleij1fd2d272006-05-08 09:22:01 +00001473 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB &&
1474 dev->descriptor.idVendor == mtp_device->vendor_id &&
1475 dev->descriptor.idProduct == mtp_device->product_id ) {
1476
1477 printf("Found non-autodetected device \"%s\" on USB bus...\n", mtp_device->name);
Linus Walleij0558ac52006-09-07 06:55:03 +00001478 ptp_usb->device_flags = mtp_device->device_flags;
Linus Walleijd3fdd972006-05-30 15:51:37 +00001479 goto next_step;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001480
Linus Walleijb02a0662006-04-25 08:05:09 +00001481 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001482 }
Linus Walleij6946ac52006-03-21 06:51:22 +00001483 }
1484 }
1485 }
Linus Walleij1fd2d272006-05-08 09:22:01 +00001486
Linus Walleijd3fdd972006-05-30 15:51:37 +00001487next_step:
Linus Walleij1fd2d272006-05-08 09:22:01 +00001488 // Still not found any?
1489 if (dev == NULL) {
1490 return PTP_CD_RC_NO_DEVICES;
1491 }
1492
Linus Walleij0558ac52006-09-07 06:55:03 +00001493 // Found a device, then assign endpoints to ptp_usb...
Linus Walleij1fd2d272006-05-08 09:22:01 +00001494 ep = dev->config->interface->altsetting->endpoint;
1495 n = dev->config->interface->altsetting->bNumEndpoints;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001496 find_endpoints(dev, &ptp_usb->inep, &ptp_usb->inep_maxpacket,
1497 &ptp_usb->outep, &ptp_usb->outep_maxpacket, &ptp_usb->intep);
Linus Walleij1fd2d272006-05-08 09:22:01 +00001498
1499 // printf("Init PTP USB...\n");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001500 if (init_ptp_usb(params, ptp_usb, dev) < 0) {
1501 return PTP_CD_RC_ERROR_CONNECTING;
1502 }
Linus Walleij1fd2d272006-05-08 09:22:01 +00001503
1504 ret = ptp_opensession(params,1);
Linus Walleij48a2d462006-11-19 14:07:59 +00001505
1506 // This works in situations where previous bad applications have not used LIBMTP_Release_Device on exit
1507 if (ret == PTP_ERROR_IO) {
1508 printf("%s\n","PTP ERROR IO: Trying again after resetting USB");
1509 // printf("%s\n","Closing USB interface...");
1510 close_usb(ptp_usb,dev->config->interface->altsetting->bInterfaceNumber);
1511 // printf("%s\n","Init PTP USB...");
1512 if (init_ptp_usb(params, ptp_usb, dev) < 0) {
1513 return PTP_CD_RC_ERROR_CONNECTING;
1514 }
1515
1516 ret = ptp_opensession(params,1);
1517 }
1518
Linus Walleij1fd2d272006-05-08 09:22:01 +00001519 // printf("Session open (%d)...\n", ret);
1520 if (ret == PTP_RC_InvalidTransactionID) {
1521 params->transaction_id += 10;
1522 ret = ptp_opensession(params,1);
1523 }
1524 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
1525 printf("Could not open session! (Return code %d)\n Try to reset the device.\n", ret);
1526 usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber);
1527 }
1528
1529 // It is actually permissible to call this before opening the session
1530 ret = ptp_getdeviceinfo(params, &deviceinfo);
1531 if (ret != PTP_RC_OK) {
1532 printf("Could not get device info!\n");
1533 usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber);
1534 return PTP_CD_RC_ERROR_CONNECTING;
1535 }
1536
1537 // we're connected, return OK
1538 *interfaceNumber = dev->config->interface->altsetting->bInterfaceNumber;
1539 return PTP_CD_RC_CONNECTED;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001540}
1541
Linus Walleijd6a49972006-05-02 08:24:54 +00001542static 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 +00001543{
Linus Walleijd6a49972006-05-02 08:24:54 +00001544 int i,n;
1545 struct usb_endpoint_descriptor *ep;
1546
1547 ep = dev->config->interface->altsetting->endpoint;
1548 n=dev->config->interface->altsetting->bNumEndpoints;
1549
1550 for (i=0;i<n;i++) {
1551 if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1552 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1553 USB_ENDPOINT_DIR_MASK)
1554 {
1555 *inep=ep[i].bEndpointAddress;
1556 *inep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001557 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001558 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1559 {
1560 *outep=ep[i].bEndpointAddress;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001561 *outep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijd6a49972006-05-02 08:24:54 +00001562 }
1563 } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1564 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1565 USB_ENDPOINT_DIR_MASK)
1566 {
1567 *intep=ep[i].bEndpointAddress;
1568 }
1569 }
1570 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001571}
1572
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001573int open_device (int busn, int devn, short force, PTP_USB *ptp_usb, PTPParams *params, struct usb_device **dev)
1574{
1575#ifdef DEBUG
Linus Walleijd6a49972006-05-02 08:24:54 +00001576 printf("dev %i\tbus %i\n",devn,busn);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001577#endif
Linus Walleijd6a49972006-05-02 08:24:54 +00001578
1579 *dev=find_device(busn,devn,force);
1580 if (*dev==NULL) {
1581 fprintf(stderr,"could not find any device matching given "
1582 "bus/dev numbers\n");
1583 exit(-1);
1584 }
1585 find_endpoints(*dev,&ptp_usb->inep,&ptp_usb->inep_maxpacket,&ptp_usb->outep,&ptp_usb->outep_maxpacket,&ptp_usb->intep);
1586
Linus Walleij9eb3d312006-08-04 19:25:59 +00001587 if (init_ptp_usb(params, ptp_usb, *dev) < 0) {
1588 return -1;
1589 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001590 if (ptp_opensession(params,1)!=PTP_RC_OK) {
1591 fprintf(stderr,"ERROR: Could not open session!\n");
1592 close_usb(ptp_usb, (*dev)->config->interface->altsetting->bInterfaceNumber);
1593 return -1;
1594 }
1595 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001596}
1597
1598void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber)
1599{
Linus Walleijd6a49972006-05-02 08:24:54 +00001600 if (ptp_closesession(params)!=PTP_RC_OK)
1601 fprintf(stderr,"ERROR: Could not close session!\n");
1602 close_usb(ptp_usb, interfaceNumber);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001603}
1604
Linus Walleijd6a49972006-05-02 08:24:54 +00001605static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001606{
Linus Walleijd6a49972006-05-02 08:24:54 +00001607
1608 return (usb_control_msg(ptp_usb->handle,
1609 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
1610 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001611}
1612
Linus Walleijd6a49972006-05-02 08:24:54 +00001613static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001614{
Linus Walleijd6a49972006-05-02 08:24:54 +00001615 return (usb_control_msg(ptp_usb->handle,
1616 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
1617 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001618}