blob: 51c5e1a53028361f9eb55eb157109ac13c3e52f4 [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 *
8 * This file adds some utils (many copied from ptpcam.c from libptp2) to
9 * use MTP devices. Include mtp-utils.h to use any of the ptp/mtp functions.
10 *
11 */
Linus Walleij7beba572006-11-29 08:56:12 +000012#include "libmtp.h"
13#include "libusb-glue.h"
14#include "util.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000015#include "ptp.h"
Linus Walleij7beba572006-11-29 08:56:12 +000016
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000017#include <errno.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000021#include <usb.h>
22
Linus Walleija0323272007-01-07 12:21:30 +000023#include "ptp-pack.c"
24
Linus Walleije78de6d2006-10-31 14:46:36 +000025/* To enable debug prints, switch on this */
26//#define ENABLE_USB_BULK_DEBUG
27
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000028/* OUR APPLICATION USB URB (2MB) ;) */
29#define PTPCAM_USB_URB 2097152
30
31/* this must not be too short - the original 4000 was not long
32 enough for big file transfers. I imagine the player spends a
33 bit of time gearing up to receiving lots of data. This also makes
34 connecting/disconnecting more reliable */
35#define USB_TIMEOUT 10000
36#define USB_CAPTURE_TIMEOUT 20000
37
38/* USB control message data phase direction */
39#ifndef USB_DP_HTD
40#define USB_DP_HTD (0x00 << 7) /* host to device */
41#endif
42#ifndef USB_DP_DTH
43#define USB_DP_DTH (0x01 << 7) /* device to host */
44#endif
45
46/* USB Feature selector HALT */
47#ifndef USB_FEATURE_HALT
48#define USB_FEATURE_HALT 0x00
49#endif
50
Linus Walleija5483642006-03-09 09:20:38 +000051/*
Linus Walleije8c54642006-03-28 09:45:00 +000052 * MTP device list, trying real bad to get all devices into
53 * this list by stealing from everyone I know.
Linus Walleija5483642006-03-09 09:20:38 +000054 */
Linus Walleij6fd2f082006-03-28 07:19:22 +000055static const LIBMTP_device_entry_t mtp_device_table[] = {
Linus Walleij3d2c7842006-08-18 09:35:08 +000056
57 /*
58 * Creative Technology
Linus Walleijcc4dd482006-11-25 21:58:30 +000059 * Initially the Creative devices was all we supported so these are
60 * the most thoroughly tested devices.
Linus Walleij3d2c7842006-08-18 09:35:08 +000061 */
Linus Walleij0558ac52006-09-07 06:55:03 +000062 { "Creative Zen Vision", 0x041e, 0x411f, DEVICE_FLAG_NONE },
63 { "Creative Portable Media Center", 0x041e, 0x4123, DEVICE_FLAG_NONE },
64 { "Creative Zen Xtra (MTP mode)", 0x041e, 0x4128, DEVICE_FLAG_NONE },
65 { "Second generation Dell DJ", 0x041e, 0x412f, DEVICE_FLAG_NONE },
66 { "Creative Zen Micro (MTP mode)", 0x041e, 0x4130, DEVICE_FLAG_NONE },
67 { "Creative Zen Touch (MTP mode)", 0x041e, 0x4131, DEVICE_FLAG_NONE },
68 { "Dell Pocket DJ (MTP mode)", 0x041e, 0x4132, DEVICE_FLAG_NONE },
69 { "Creative Zen Sleek (MTP mode)", 0x041e, 0x4137, DEVICE_FLAG_NONE },
70 { "Creative Zen MicroPhoto", 0x041e, 0x413c, DEVICE_FLAG_NONE },
71 { "Creative Zen Sleek Photo", 0x041e, 0x413d, DEVICE_FLAG_NONE },
72 { "Creative Zen Vision:M", 0x041e, 0x413e, DEVICE_FLAG_NONE },
Linus Walleijbdd006b2006-09-15 04:54:13 +000073 // Reported by marazm@o2.pl
74 { "Creative Zen V", 0x041e, 0x4150, DEVICE_FLAG_NONE },
Linus Walleij92a84962006-11-11 20:04:37 +000075 // Reported by danielw@iinet.net.au
76 { "Creative Zen Vision:M (DVP-HD0004)", 0x041e, 0x4151, DEVICE_FLAG_NONE },
Linus Walleij550bc672006-10-10 11:11:29 +000077 // Reported by Darel on the XNJB forums
Richard Lowe1f06922006-11-12 16:38:39 +000078 { "Creative Zen V Plus", 0x041e, 0x4152, DEVICE_FLAG_NONE },
79 { "Creative Zen Vision W", 0x041e, 0x4153, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000080
81 /*
82 * Samsung
Linus Walleij0558ac52006-09-07 06:55:03 +000083 * We suspect that more of these are dual mode.
Linus Walleij3d2c7842006-08-18 09:35:08 +000084 */
85 // From libgphoto2
Linus Walleij0558ac52006-09-07 06:55:03 +000086 { "Samsung YH-820", 0x04e8, 0x502e, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000087 // Contributed by polux2001@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +000088 { "Samsung YH-925", 0x04e8, 0x502f, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000089 // Contributed by anonymous person on SourceForge
Linus Walleij0558ac52006-09-07 06:55:03 +000090 { "Samsung YP-T7J", 0x04e8, 0x5047, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000091 // Reported by cstrickler@gmail.com
Linus Walleij0558ac52006-09-07 06:55:03 +000092 { "Samsung YP-U2J (YP-U2JXB/XAA)", 0x04e8, 0x5054, DEVICE_FLAG_NONE },
Linus Walleij82ed6612006-08-31 08:06:19 +000093 // Reported by Andrew Benson
Linus Walleij0558ac52006-09-07 06:55:03 +000094 { "Samsung YP-F2J", 0x04e8, 0x5057, DEVICE_FLAG_DUALMODE },
Linus Walleij19be4212006-11-16 09:00:48 +000095 // Reported by Patrick <skibler@gmail.com>
Linus Walleijabdf6072006-11-24 07:34:02 +000096 { "Samsung YP-K5", 0x04e8, 0x505a, DEVICE_FLAG_NONE },
Linus Walleija5073c72006-11-24 07:55:12 +000097 // Reported by Matthew Wilcox <matthew@wil.cx>
Richard Lowc046fdf2006-11-24 09:19:33 +000098 { "Samsung Yepp T9", 0x04e8, 0x507f, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000099 // From a rouge .INF file
Linus Walleij0558ac52006-09-07 06:55:03 +0000100 { "Samsung YH-999 Portable Media Center", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000101
102 /*
103 * Intel
104 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000105 { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000106
107 /*
108 * JVC
109 */
110 // From Mark Veinot
Linus Walleij0558ac52006-09-07 06:55:03 +0000111 { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000112
113 /*
Linus Walleij093ca0a2006-11-02 21:15:00 +0000114 * Philips
Linus Walleij3d2c7842006-08-18 09:35:08 +0000115 */
116 // From libgphoto2 source
Linus Walleij093ca0a2006-11-02 21:15:00 +0000117 { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE },
Linus Walleijbf7fc272006-11-10 21:35:06 +0000118 { "Philips HDD6320/00", 0x0471, 0x014b, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000119 // Anonymous SourceForge user
Linus Walleij093ca0a2006-11-02 21:15:00 +0000120 { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE },
121 // From Gerhard Mekenkamp
122 { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE },
Richard Lowa6c924c2006-12-29 17:44:28 +0000123 // from XNJB forum
124 { "Philips GoGear SA9200", 0x0471, 0x014f, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000125
126 /*
127 * SanDisk
128 */
129 // Reported by Brian Robison
Linus Walleij0558ac52006-09-07 06:55:03 +0000130 { "SanDisk Sansa m240", 0x0781, 0x7400, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000131 // Reported by tangent_@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +0000132 { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000133 // From libgphoto2 source
Linus Walleij0558ac52006-09-07 06:55:03 +0000134 { "SanDisk Sansa e200", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000135 // Reported by gonkflea@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +0000136 { "SanDisk Sansa e260", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000137
138 /*
139 * iRiver
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000140 * we assume that PTP_OC_MTP_GetObjPropList is essentially
Linus Walleijfabffc02006-12-17 00:18:57 +0000141 * broken on all iRiver devices, meaning it simply won't return
142 * all properties for a file when asking for metadata 0xffffffff.
143 * Please test on your device if you believe it isn't broken!
144 * Some devices from http://www.mtp-ums.net/viewdeviceinfo.php
Linus Walleij3d2c7842006-08-18 09:35:08 +0000145 */
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000146 { "iRiver Portable Media Center", 0x1006, 0x4002, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
147 { "iRiver Portable Media Center", 0x1006, 0x4003, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000148 // From libgphoto2 source
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000149 { "iRiver T10", 0x4102, 0x1113, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
150 { "iRiver T20 FM", 0x4102, 0x1114, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleijfabffc02006-12-17 00:18:57 +0000151 // This appears at the MTP-UMS site
152 { "iRiver T20", 0x4102, 0x1115, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000153 { "iRiver U10", 0x4102, 0x1116, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
154 { "iRiver T10", 0x4102, 0x1117, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
155 { "iRiver T20", 0x4102, 0x1118, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
156 { "iRiver T30", 0x4102, 0x1119, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij77e047c2006-09-11 19:09:06 +0000157 // Reported by David Wolpoff
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000158 { "iRiver T10 2GB", 0x4102, 0x1120, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij87099812006-12-17 00:29:58 +0000159 // Rough guess this is the MTP device ID...
160 { "iRiver N12", 0x4102, 0x1122, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij04377da2006-08-30 13:03:35 +0000161 // Reported by Adam Torgerson
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000162 { "iRiver Clix", 0x4102, 0x112a, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000163 // Reported by Scott Call
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000164 { "iRiver H10 20GB", 0x4102, 0x2101, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
165 { "iRiver H10", 0x4102, 0x2102, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000166
167 /*
168 * Dell
169 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000170 { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000171
172 /*
173 * Toshiba
174 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000175 { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE },
176 { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE },
Linus Walleij405e2392006-10-23 07:13:49 +0000177 // From libgphoto2
178 { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE },
Linus Walleij402e4bd2006-09-12 07:50:56 +0000179 // Reported by Rob Brown
180 { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE },
Linus Walleijb01d18b2006-12-13 12:49:15 +0000181
Linus Walleij6f680562006-08-19 22:49:33 +0000182 /*
183 * Archos
184 */
185 // Reported by gudul1@users.sourceforge.net
Linus Walleije5430902006-10-08 14:46:03 +0000186 { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE },
187
188 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000189 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000190 * This unit is falsely detected as USB mass storage in Linux
Linus Walleijcc4dd482006-11-25 21:58:30 +0000191 * prior to kernel 2.6.19 (fixed by patch from Alan Stern)
192 * so on older kernels special care is needed to remove the
193 * USB mass storage driver that erroneously binds to the device
194 * interface.
Linus Walleije5430902006-10-08 14:46:03 +0000195 */
Richard Lowc7d48d22006-11-29 18:24:55 +0000196 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER},
197
198 /*
199 * Microsoft
200 */
201 // Reported by Farooq Zaman
202 { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE },
203
204 /*
205 * Sirius
206 */
Linus Walleijb01d18b2006-12-13 12:49:15 +0000207 { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE },
208
209 /*
210 * Canon
211 * This is actually a camera, but it has a Microsoft device descriptor
212 * and reports itself as supporting the MTP extension.
213 */
214 {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE }
Linus Walleije5430902006-10-08 14:46:03 +0000215
Linus Walleija5483642006-03-09 09:20:38 +0000216};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000217static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000218
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000219int ptpcam_usb_timeout = USB_TIMEOUT;
220
Linus Walleijd6a49972006-05-02 08:24:54 +0000221// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000222static struct usb_bus* init_usb();
223static struct usb_device *probe_usb_bus_for_mtp_devices(void);
Linus Walleijd6a49972006-05-02 08:24:54 +0000224static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);
225static struct usb_device* find_device (int busn, int devicen, short force);
226static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);
227static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000228static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000229static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
230static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Linus Walleijd6a49972006-05-02 08:24:54 +0000231static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
232static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000233
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000234
Linus Walleij6fd2f082006-03-28 07:19:22 +0000235int get_device_list(LIBMTP_device_entry_t ** const devices, int * const numdevs)
236{
237 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
238 *numdevs = mtp_device_table_size;
239 return 0;
240}
241
Linus Walleij1fd2d272006-05-08 09:22:01 +0000242static struct usb_bus* init_usb()
243{
244 usb_init();
245 usb_find_busses();
246 usb_find_devices();
247 return (usb_get_busses());
248}
249
250/**
251 * Check for the Microsoft OS device descriptor and returns device struct
252 * if the device is MTP-compliant. The function will only recognize
253 * a single device connected to the USB bus.
254 *
255 * @return an MTP-compliant USB device if one was found, else NULL.
256 */
257static struct usb_device *probe_usb_bus_for_mtp_devices(void)
258{
259 struct usb_bus *bus;
260
261 bus = init_usb();
262 for (; bus; bus = bus->next) {
263 struct usb_device *dev;
264
265 for (dev = bus->devices; dev; dev = dev->next) {
266 usb_dev_handle *devh;
267 unsigned char buf[1024], cmd;
268 int ret;
269
270 devh = usb_open(dev);
271 if (devh == NULL) {
272 continue;
273 }
274
275 // Read the special descripor, if possible...
276 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
277
278 if (ret < 10) {
279 // printf("Device: VID %04x/PID %04x: no extended device property...\n",
280 // dev->descriptor.idVendor,
281 // dev->descriptor.idProduct);
282 usb_close(devh);
283 continue;
284 }
Linus Walleijd5d51c82006-09-11 06:57:50 +0000285
286 // It is atleast 10 bytes...
Linus Walleij1fd2d272006-05-08 09:22:01 +0000287 if (!((buf[2] == 'M') && (buf[4]=='S') && (buf[6]=='F') && (buf[8]=='T'))) {
Linus Walleijd5d51c82006-09-11 06:57:50 +0000288 printf("This is not a Microsoft MTP descriptor...\n");
289 printf("Device response to read device property 0xee:\n");
290 data_dump_ascii (stdout, buf, ret, 0);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000291 usb_close(devh);
292 continue;
293 }
294
295 cmd = buf[16];
296 ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
297 cmd, 0, 4, (char *) buf, sizeof(buf), 1000);
298 if (ret == -1) {
299 //printf("Decice could not respond to control message 1.\n");
300 usb_close(devh);
Linus Walleijd5d51c82006-09-11 06:57:50 +0000301 continue;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000302 }
303
Linus Walleijd5d51c82006-09-11 06:57:50 +0000304 if (ret > 0x15) {
305 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
306 printf("The device has a Microsoft device descriptor, but it's not MTP.\n");
307 printf("This is not an MTP device. Presumable it is USB mass storage\n");
308 printf("with some additional Janus (DRM) support.\n");
309 printf("Device response to control message 1:\n");
310 data_dump_ascii (stdout, buf, ret, 0);
311 continue;
312 }
313 } else {
314 // Not MTP or broken
315 continue;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000316 }
317
318 ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
319 cmd, 0, 5, (char *) buf, sizeof(buf), 1000);
320 if (ret == -1) {
321 //printf("Device could not respond to control message 2.\n");
322 usb_close(devh);
Linus Walleijd5d51c82006-09-11 06:57:50 +0000323 // Return the device anyway, it said previously it was MTP, right?
Linus Walleij1fd2d272006-05-08 09:22:01 +0000324 return dev;
325 }
326
Linus Walleijd5d51c82006-09-11 06:57:50 +0000327 if (ret > 0x15) {
328 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
329 printf("This device does not respond with MTP characteristics on\n");
330 printf("the second device property read (0x05), but will be regarded\n");
331 printf("as MTP anyway.\n");
332 printf("Device response to control message 2:\n");
333 data_dump_ascii (stdout, buf, ret, 0);
334 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000335 }
Linus Walleijd5d51c82006-09-11 06:57:50 +0000336
Linus Walleij1fd2d272006-05-08 09:22:01 +0000337 usb_close(devh);
338 // We can return the device here, it will be the first.
339 // If it was not MTP, the loop continues before it reaches this point.
340 return dev;
341 }
342 }
343 // If nothing was found we end up here.
344 return NULL;
345}
346
347/**
348 * Detect the MTP device descriptor and return the VID and PID
349 * of the first device found. This is a very low-level function
350 * which is intended for use with <b>udev</b> or other hotplug
351 * mechanisms. The idea is that a script may want to know if the
352 * just plugged-in device was an MTP device or not.
353 * @param vid the Vendor ID (VID) of the first device found.
354 * @param pid the Product ID (PID) of the first device found.
355 * @return the number of detected devices or -1 if the call
356 * was unsuccessful.
357 */
358int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
359{
360 struct usb_device *dev = probe_usb_bus_for_mtp_devices();
361 if (dev == NULL) {
362 return 0;
363 }
364 *vid = dev->descriptor.idVendor;
365 *pid = dev->descriptor.idProduct;
366 return 1;
367}
368
Linus Walleijc6210fb2006-05-08 11:11:41 +0000369/**
370 * This routine just dumps out low-level
371 * USB information about the current device.
372 * @param ptp_usb the USB device to get information from.
373 */
374void dump_usbinfo(PTP_USB *ptp_usb)
375{
376 int res;
377 struct usb_device *dev;
378
379#ifdef LIBUSB_HAS_GET_DRIVER_NP
380 char devname[0x10];
381
382 devname[0] = '\0';
383 res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));
384 if (devname[0] != '\0') {
385 printf(" Using kernel interface \"%s\"\n", devname);
386 }
387#endif
388 dev = usb_device(ptp_usb->handle);
389 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
390 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
391 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
392 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000393 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
394 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000395 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
396 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000397 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000398 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
399}
400
Linus Walleija0323272007-01-07 12:21:30 +0000401static void
402ptp_debug (PTPParams *params, const char *format, ...)
403{
404 va_list args;
405
406 va_start (args, format);
407 if (params->debug_func!=NULL)
408 params->debug_func (params->data, format, args);
409 else
410 {
411 vfprintf (stderr, format, args);
412 fprintf (stderr,"\n");
413 fflush (stderr);
414 }
415 va_end (args);
416}
417
418static void
419ptp_error (PTPParams *params, const char *format, ...)
420{
421 va_list args;
422
423 va_start (args, format);
424 if (params->error_func!=NULL)
425 params->error_func (params->data, format, args);
426 else
427 {
428 vfprintf (stderr, format, args);
429 fprintf (stderr,"\n");
430 fflush (stderr);
431 }
432 va_end (args);
433}
434
Linus Walleijc6210fb2006-05-08 11:11:41 +0000435
Linus Walleij99ed83c2007-01-02 18:46:02 +0000436/*
437 * ptp_read_func() and ptp_write_func() are
438 * based on same functions in library.c in libgphoto2.
439 * Much reading packet logs and having fun with trials and errors
440 * reveals that WMP / Windows is probably using an algorithm like this
441 * for large transfers:
442 *
443 * 1. Send the command (0x0c bytes) if headers are split, else, send
444 * command plus sizeof(endpoint) - 0x0c bytes.
445 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
446 * split headers. Else goto 3.
447 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
448 * We call 0x10000 CONTEXT_BLOCK_SIZE.
449 * 4. Send remaining bytes MOD sizeof(endpoint)
450 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
451 * then also send a zero-length package.
452 */
Richard Low021421e2007-01-01 18:08:57 +0000453#define CONTEXT_BLOCK_SIZE 0x10000
Linus Walleijb02a0662006-04-25 08:05:09 +0000454static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000455ptp_read_func (
456 unsigned long size, PTPDataHandler *handler,void *data,
457 unsigned long *readbytes
458) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000459 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000460 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000461 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000462 unsigned long curread = 0;
463 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000464 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000465
466 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000467 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000468 while (curread < size) {
469 toread = size - curread;
Richard Low43fdb882006-09-06 16:19:05 +0000470 if (toread > CONTEXT_BLOCK_SIZE)
471 toread = CONTEXT_BLOCK_SIZE;
Richard Lowb89ea942007-01-02 11:47:19 +0000472 else if (toread > ptp_usb->outep_maxpacket)
473 toread -= toread % ptp_usb->outep_maxpacket;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000474
475 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout);
Linus Walleijd6a49972006-05-02 08:24:54 +0000476 if (result == 0) {
Linus Walleij784ac3f2006-12-29 10:36:51 +0000477 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout);
Linus Walleijd6a49972006-05-02 08:24:54 +0000478 }
479 if (result < 0)
Richard Low43fdb882006-09-06 16:19:05 +0000480 return PTP_ERROR_IO;
Linus Walleije78de6d2006-10-31 14:46:36 +0000481#ifdef ENABLE_USB_BULK_DEBUG
482 printf("<==USB IN\n");
Linus Walleij784ac3f2006-12-29 10:36:51 +0000483 data_dump_ascii (stdout,bytes,result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000484#endif
Linus Walleij784ac3f2006-12-29 10:36:51 +0000485 handler->putfunc (NULL, handler->private, result, bytes, &written);
Linus Walleijd6a49972006-05-02 08:24:54 +0000486 curread += result;
487 if (result < toread) /* short reads are common */
488 break;
489 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000490 if (readbytes) *readbytes = curread;
491 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000492
493 // Increase counters, call callback
494 if (ptp_usb->callback_active) {
495 ptp_usb->current_transfer_complete += curread;
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000496 if (ptp_usb->current_transfer_complete > ptp_usb->current_transfer_total) {
497 // Fishy... but some commands have unpredictable lengths.
498 // send last update and disable callback.
499 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
500 ptp_usb->callback_active = 0;
501 }
Linus Walleijee73ef22006-08-27 19:56:00 +0000502 if (ptp_usb->current_transfer_callback != NULL) {
503 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
504 ptp_usb->current_transfer_total,
505 ptp_usb->current_transfer_callback_data);
506 }
507 }
508
Linus Walleijd6a49972006-05-02 08:24:54 +0000509 if (result > 0) {
Linus Walleijd6a49972006-05-02 08:24:54 +0000510 return (PTP_RC_OK);
511 } else {
512 return PTP_ERROR_IO;
513 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000514}
515
Linus Walleijb02a0662006-04-25 08:05:09 +0000516static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000517ptp_write_func (
518 unsigned long size,
519 PTPDataHandler *handler,
520 void *data,
521 unsigned long *written
522) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000523 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000524 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000525 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000526 unsigned long curwrite = 0;
527 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000528
529 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000530 bytes = malloc(CONTEXT_BLOCK_SIZE);
531 if (!bytes) return PTP_ERROR_IO;
Linus Walleijd6a49972006-05-02 08:24:54 +0000532 while (curwrite < size) {
533 towrite = size-curwrite;
Richard Low43fdb882006-09-06 16:19:05 +0000534 if (towrite > CONTEXT_BLOCK_SIZE)
535 towrite = CONTEXT_BLOCK_SIZE;
Richard Low021421e2007-01-01 18:08:57 +0000536 else
537 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0)
538 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000539 handler->getfunc (NULL, handler->private,towrite,bytes,&towrite);
540 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000541#ifdef ENABLE_USB_BULK_DEBUG
542 printf("USB OUT==>\n");
Richard Lowedd61832006-12-30 12:38:18 +0000543 data_dump_ascii (stdout,bytes,towrite,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000544#endif
Linus Walleijd6a49972006-05-02 08:24:54 +0000545 if (result < 0)
Richard Low43fdb882006-09-06 16:19:05 +0000546 return PTP_ERROR_IO;
Linus Walleijd6a49972006-05-02 08:24:54 +0000547 curwrite += result;
548 if (result < towrite) /* short writes happen */
549 break;
550 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000551 free (bytes);
552 if (written) *written = curwrite;
Linus Walleijd214b9b2006-08-26 22:08:37 +0000553
Richard Lowef05b892007-01-03 21:18:56 +0000554 // Increase counters
555 ptp_usb->current_transfer_complete += curwrite;
556
557 // call callback
Linus Walleijd214b9b2006-08-26 22:08:37 +0000558 if (ptp_usb->callback_active) {
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000559 if (ptp_usb->current_transfer_complete > ptp_usb->current_transfer_total) {
560 // Fishy... but some commands have unpredictable lengths.
561 // send last update and disable callback.
562 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
563 ptp_usb->callback_active = 0;
564 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000565 if (ptp_usb->current_transfer_callback != NULL) {
566 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
567 ptp_usb->current_transfer_total,
568 ptp_usb->current_transfer_callback_data);
569 }
Linus Walleij80d134a2006-08-22 21:41:37 +0000570 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000571
Richard Lowef05b892007-01-03 21:18:56 +0000572 // If this is the last transfer send a zero write if required
573 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000574 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
Richard Low021421e2007-01-01 18:08:57 +0000575#ifdef ENABLE_USB_BULK_DEBUG
576 printf("USB OUT==>\n");
577 printf("Zero Write\n");
578#endif
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000579 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000580 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000581 }
Richard Lowef05b892007-01-03 21:18:56 +0000582
583 if (ptp_usb->current_transfer_complete == ptp_usb->current_transfer_total)
584 ptp_usb->callback_active = 0;
585
Linus Walleijd6a49972006-05-02 08:24:54 +0000586 if (result < 0)
587 return PTP_ERROR_IO;
588 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000589}
590
Linus Walleija0323272007-01-07 12:21:30 +0000591/* memory data get/put handler */
592typedef struct {
593 unsigned char *data;
594 unsigned long size, curoff;
595} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000596
Linus Walleija0323272007-01-07 12:21:30 +0000597static uint16_t
598memory_getfunc(PTPParams* params, void* private,
599 unsigned long wantlen, unsigned char *data,
600 unsigned long *gotlen
601) {
602 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
603 unsigned long tocopy = wantlen;
604
605 if (priv->curoff + tocopy > priv->size)
606 tocopy = priv->size - priv->curoff;
607 memcpy (data, priv->data + priv->curoff, tocopy);
608 priv->curoff += tocopy;
609 *gotlen = tocopy;
610 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000611}
612
Linus Walleija0323272007-01-07 12:21:30 +0000613static uint16_t
614memory_putfunc(PTPParams* params, void* private,
615 unsigned long sendlen, unsigned char *data,
616 unsigned long *putlen
617) {
618 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
619
620 if (priv->curoff + sendlen > priv->size) {
621 priv->data = realloc (priv->data, priv->curoff+sendlen);
622 priv->size = priv->curoff + sendlen;
623 }
624 memcpy (priv->data + priv->curoff, data, sendlen);
625 priv->curoff += sendlen;
626 *putlen = sendlen;
627 return PTP_RC_OK;
628}
629
630/* init private struct for receiving data. */
631static uint16_t
632ptp_init_recv_memory_handler(PTPDataHandler *handler) {
633 PTPMemHandlerPrivate* priv;
634 priv = malloc (sizeof(PTPMemHandlerPrivate));
635 handler->private = priv;
636 handler->getfunc = memory_getfunc;
637 handler->putfunc = memory_putfunc;
638 priv->data = NULL;
639 priv->size = 0;
640 priv->curoff = 0;
641 return PTP_RC_OK;
642}
643
644/* init private struct and put data in for sending data.
645 * data is still owned by caller.
646 */
647static uint16_t
648ptp_init_send_memory_handler(PTPDataHandler *handler,
649 unsigned char *data, unsigned long len
650) {
651 PTPMemHandlerPrivate* priv;
652 priv = malloc (sizeof(PTPMemHandlerPrivate));
653 if (!priv)
654 return PTP_RC_GeneralError;
655 handler->private = priv;
656 handler->getfunc = memory_getfunc;
657 handler->putfunc = memory_putfunc;
658 priv->data = data;
659 priv->size = len;
660 priv->curoff = 0;
661 return PTP_RC_OK;
662}
663
664/* free private struct + data */
665static uint16_t
666ptp_exit_send_memory_handler (PTPDataHandler *handler) {
667 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
668 /* data is owned by caller */
669 free (priv);
670 return PTP_RC_OK;
671}
672
673/* hand over our internal data to caller */
674static uint16_t
675ptp_exit_recv_memory_handler (PTPDataHandler *handler,
676 unsigned char **data, unsigned long *size
677) {
678 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
679 *data = priv->data;
680 *size = priv->size;
681 free (priv);
682 return PTP_RC_OK;
683}
684
685/* send / receive functions */
686
687uint16_t
688ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
689{
690 uint16_t ret;
691 PTPUSBBulkContainer usbreq;
692 PTPDataHandler memhandler;
693 unsigned long written, towrite;
694
695 /* build appropriate USB container */
696 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
697 (sizeof(uint32_t)*(5-req->Nparam)));
698 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
699 usbreq.code=htod16(req->Code);
700 usbreq.trans_id=htod32(req->Transaction_ID);
701 usbreq.payload.params.param1=htod32(req->Param1);
702 usbreq.payload.params.param2=htod32(req->Param2);
703 usbreq.payload.params.param3=htod32(req->Param3);
704 usbreq.payload.params.param4=htod32(req->Param4);
705 usbreq.payload.params.param5=htod32(req->Param5);
706 /* send it to responder */
707 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
708 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
709 ret=ptp_write_func(
710 towrite,
711 &memhandler,
712 params->data,
713 &written
714 );
715 ptp_exit_send_memory_handler (&memhandler);
716 if (ret!=PTP_RC_OK) {
717 ret = PTP_ERROR_IO;
718/* ptp_error (params,
719 "PTP: request code 0x%04x sending req error 0x%04x",
720 req->Code,ret); */
721 }
722 if (written != towrite) {
723 ptp_error (params,
724 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
725 written, towrite
726 );
727 ret = PTP_ERROR_IO;
728 }
729 return ret;
730}
731
732uint16_t
733ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
734 unsigned long size, PTPDataHandler *handler
735) {
736 uint16_t ret;
737 int wlen, datawlen;
738 unsigned long written;
739 PTPUSBBulkContainer usbdata;
740 uint32_t bytes_left_to_transfer;
741 PTPDataHandler memhandler;
742
743 /* build appropriate USB container */
744 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
745 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
746 usbdata.code = htod16(ptp->Code);
747 usbdata.trans_id= htod32(ptp->Transaction_ID);
748
749 ((PTP_USB*)params->data)->current_transfer_complete = 0;
750 ((PTP_USB*)params->data)->current_transfer_total = size;
751
752 if (params->split_header_data) {
753 datawlen = 0;
754 wlen = PTP_USB_BULK_HDR_LEN;
755 } else {
756 unsigned long gotlen;
757 /* For all camera devices. */
758 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN)?size:PTP_USB_BULK_PAYLOAD_LEN;
759 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
760 ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen);
761 if (ret != PTP_RC_OK)
762 return ret;
763 if (gotlen != datawlen)
764 return PTP_RC_GeneralError;
765 }
766 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
767 /* send first part of data */
768 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
769 ptp_exit_send_memory_handler (&memhandler);
770 if (ret!=PTP_RC_OK) {
771 ret = PTP_ERROR_IO;
772/* ptp_error (params,
773 "PTP: request code 0x%04x sending data error 0x%04x",
774 ptp->Code,ret);*/
775 return ret;
776 }
777 if (size <= datawlen) return ret;
778 /* if everything OK send the rest */
779 bytes_left_to_transfer = size-datawlen;
780 ret = PTP_RC_OK;
781 while(bytes_left_to_transfer > 0) {
782 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
783 if (ret != PTP_RC_OK)
784 break;
785 if (written == 0) {
786 ret = PTP_ERROR_IO;
787 break;
788 }
789 bytes_left_to_transfer -= written;
790 }
791 if (ret!=PTP_RC_OK)
792 ret = PTP_ERROR_IO;
793 return ret;
794}
795
796static uint16_t ptp_usb_getpacket(PTPParams *params,
797 PTPUSBBulkContainer *packet, unsigned long *rlen)
798{
799 PTPDataHandler memhandler;
800 uint16_t ret;
801 unsigned char *x = NULL;
802
803 /* read the header and potentially the first data */
804 if (params->response_packet_size > 0) {
805 /* If there is a buffered packet, just use it. */
806 memcpy(packet, params->response_packet, params->response_packet_size);
807 *rlen = params->response_packet_size;
808 free(params->response_packet);
809 params->response_packet = NULL;
810 params->response_packet_size = 0;
811 /* Here this signifies a "virtual read" */
812 return PTP_RC_OK;
813 }
814 ptp_init_recv_memory_handler (&memhandler);
815 ret = ptp_read_func( sizeof(*packet), &memhandler, params->data, rlen);
816 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
817 if (x) {
818 memcpy (packet, x, *rlen);
819 free (x);
820 }
821 return ret;
822}
823
824uint16_t
825ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
826{
827 uint16_t ret;
828 PTPUSBBulkContainer usbdata;
829 unsigned char *data;
830 unsigned long written;
831
832 memset(&usbdata,0,sizeof(usbdata));
833 do {
834 unsigned long len, rlen;
835
836 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
837 if (ret!=PTP_RC_OK) {
838 ret = PTP_ERROR_IO;
839 break;
840 } else
841 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
842 ret = PTP_ERROR_DATA_EXPECTED;
843 break;
844 } else
845 if (dtoh16(usbdata.code)!=ptp->Code) {
846 ret = dtoh16(usbdata.code);
847 break;
848 }
849 if (usbdata.length == 0xffffffffU) {
850 /* stuff data directly to passed data handler */
851 while (1) {
852 unsigned long readdata;
853 int xret;
854
855 xret = ptp_read_func(
856 PTP_USB_BULK_HS_MAX_PACKET_LEN,
857 handler,
858 params->data,
859 &readdata
860 );
861 if (xret == -1)
862 return PTP_ERROR_IO;
863 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN)
864 break;
865 }
866 return PTP_RC_OK;
867 }
868 if (rlen > dtoh32(usbdata.length)) {
869 /*
870 * Buffer the surplus response packet if it is >=
871 * PTP_USB_BULK_HDR_LEN
872 * (i.e. it is probably an entire package)
873 * else discard it as erroneous surplus data.
874 * This will even work if more than 2 packets appear
875 * in the same transaction, they will just be handled
876 * iteratively.
877 *
878 * Marcus observed stray bytes on iRiver devices;
879 * these are still discarded.
880 */
881 unsigned int packlen = dtoh32(usbdata.length);
882 unsigned int surplen = rlen - packlen;
883
884 if (surplen >= PTP_USB_BULK_HDR_LEN) {
885 params->response_packet = malloc(surplen);
886 memcpy(params->response_packet,
887 (uint8_t *) &usbdata + packlen, surplen);
888 params->response_packet_size = surplen;
889 } else {
890 ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes too much, expect problems!", rlen - dtoh32(usbdata.length));
891 }
892 rlen = packlen;
893 }
894
895 /* For most PTP devices rlen is 512 == sizeof(usbdata)
896 * here. For MTP devices splitting header and data it might
897 * be 12.
898 */
899 /* Evaluate full data length. */
900 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
901
902 /* autodetect split header/data MTP devices */
903 if (dtoh32(usbdata.length) > 12 && (rlen==12))
904 params->split_header_data = 1;
905
906 data = malloc(PTP_USB_BULK_HS_MAX_PACKET_LEN);
907 /* Copy first part of data to 'data' */
908 handler->putfunc(
909 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
910 &written
911 );
912
913 /* Is that all of data? */
914 if (len+PTP_USB_BULK_HDR_LEN<=rlen) break;
915
916 /* If not read the rest of it. */
917 ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
918 handler,
919 params->data, &rlen);
920 if (ret!=PTP_RC_OK) {
921 ret = PTP_ERROR_IO;
922 break;
923 }
924 } while (0);
925/*
926 if (ret!=PTP_RC_OK) {
927 ptp_error (params,
928 "PTP: request code 0x%04x getting data error 0x%04x",
929 ptp->Code, ret);
930 }*/
931 return ret;
932}
933
934uint16_t
935ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
936{
937 uint16_t ret;
938 unsigned long rlen;
939 PTPUSBBulkContainer usbresp;
940
941 memset(&usbresp,0,sizeof(usbresp));
942 /* read response, it should never be longer than sizeof(usbresp) */
943 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
944
945 if (ret!=PTP_RC_OK) {
946 ret = PTP_ERROR_IO;
947 } else
948 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
949 ret = PTP_ERROR_RESP_EXPECTED;
950 } else
951 if (dtoh16(usbresp.code)!=resp->Code) {
952 ret = dtoh16(usbresp.code);
953 }
954 if (ret!=PTP_RC_OK) {
955/* ptp_error (params,
956 "PTP: request code 0x%04x getting resp error 0x%04x",
957 resp->Code, ret);*/
958 return ret;
959 }
960 /* build an appropriate PTPContainer */
961 resp->Code=dtoh16(usbresp.code);
962 resp->SessionID=params->session_id;
963 resp->Transaction_ID=dtoh32(usbresp.trans_id);
964 resp->Param1=dtoh32(usbresp.payload.params.param1);
965 resp->Param2=dtoh32(usbresp.payload.params.param2);
966 resp->Param3=dtoh32(usbresp.payload.params.param3);
967 resp->Param4=dtoh32(usbresp.payload.params.param4);
968 resp->Param5=dtoh32(usbresp.payload.params.param5);
969 return ret;
970}
971
972/* Event handling functions */
973
974/* PTP Events wait for or check mode */
975#define PTP_EVENT_CHECK 0x0000 /* waits for */
976#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
977
978static inline uint16_t
979ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
980{
981 uint16_t ret;
982 int result;
983 unsigned long rlen;
984 PTPUSBEventContainer usbevent;
985 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
986
987 memset(&usbevent,0,sizeof(usbevent));
988
989 if ((params==NULL) || (event==NULL))
990 return PTP_ERROR_BADPARAM;
991 ret = PTP_RC_OK;
992 switch(wait) {
993 case PTP_EVENT_CHECK:
994 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
995 if (result==0)
996 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
997 if (result < 0) ret = PTP_ERROR_IO;
998 break;
999 case PTP_EVENT_CHECK_FAST:
1000 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1001 if (result==0)
1002 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1003 if (result < 0) ret = PTP_ERROR_IO;
1004 break;
1005 default:
1006 ret=PTP_ERROR_BADPARAM;
1007 break;
1008 }
1009 if (ret!=PTP_RC_OK) {
1010 ptp_error (params,
1011 "PTP: reading event an error 0x%04x occurred", ret);
1012 return PTP_ERROR_IO;
1013 }
1014 rlen = result;
1015 if (rlen < 8) {
1016 ptp_error (params,
1017 "PTP: reading event an short read of %ld bytes occurred", rlen);
1018 return PTP_ERROR_IO;
1019 }
1020 /* if we read anything over interrupt endpoint it must be an event */
1021 /* build an appropriate PTPContainer */
1022 event->Code=dtoh16(usbevent.code);
1023 event->SessionID=params->session_id;
1024 event->Transaction_ID=dtoh32(usbevent.trans_id);
1025 event->Param1=dtoh32(usbevent.param1);
1026 event->Param2=dtoh32(usbevent.param2);
1027 event->Param3=dtoh32(usbevent.param3);
1028 return ret;
1029}
1030
1031uint16_t
1032ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1033
1034 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1035}
1036
1037uint16_t
1038ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1039
1040 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1041}
1042
1043
Linus Walleij9eb3d312006-08-04 19:25:59 +00001044static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001045{
Linus Walleijd6a49972006-05-02 08:24:54 +00001046 usb_dev_handle *device_handle;
1047
Linus Walleijd6a49972006-05-02 08:24:54 +00001048 params->error_func=NULL;
1049 params->debug_func=NULL;
1050 params->sendreq_func=ptp_usb_sendreq;
1051 params->senddata_func=ptp_usb_senddata;
1052 params->getresp_func=ptp_usb_getresp;
1053 params->getdata_func=ptp_usb_getdata;
1054 params->data=ptp_usb;
1055 params->transaction_id=0;
1056 params->byteorder = PTP_DL_LE;
1057
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001058 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001059 if (!device_handle) {
1060 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001061 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001062 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001063 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001064#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1065 /*
1066 * If this device is known to be wrongfully claimed by other kernel
1067 * drivers (such as mass storage), then try to unload it to make it
1068 * accessible from user space.
1069 */
1070 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
1071 if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij7beba572006-11-29 08:56:12 +00001072 // Totally ignore this error!
1073 // perror("usb_detach_kernel_driver_np()");
Linus Walleij501ba4d2006-10-16 06:17:04 +00001074 }
1075 }
1076#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001077#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001078 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001079 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1080 perror("usb_set_configuration()");
1081 return -1;
1082 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001083#endif
Linus Walleijef5cca92006-05-05 07:19:08 +00001084 if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001085 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001086 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001087 }
Linus Walleijc6210fb2006-05-08 11:11:41 +00001088 ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber;
Linus Walleijd6a49972006-05-02 08:24:54 +00001089 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001090 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001091}
1092
Linus Walleijd6a49972006-05-02 08:24:54 +00001093static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001094{
Linus Walleij58b62792007-01-07 12:38:59 +00001095 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001096 int ret;
1097
1098 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001099 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001100 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001101 if (ret<0) {
1102 perror ("inep: usb_get_endpoint_status()");
1103 } else if (status) {
1104 printf("Clearing stall on IN endpoint\n");
1105 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1106 if (ret<0) {
1107 perror ("usb_clear_stall_feature()");
1108 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001109 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001110
1111 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001112 status=0;
1113 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1114 if (ret<0) {
1115 perror("outep: usb_get_endpoint_status()");
1116 } else if (status) {
1117 printf("Clearing stall on OUT endpoint\n");
1118 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1119 if (ret<0) {
1120 perror("usb_clear_stall_feature()");
1121 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001122 }
Linus Walleij58b62792007-01-07 12:38:59 +00001123
1124 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1125}
1126
1127static void clear_halt(PTP_USB* ptp_usb)
1128{
1129 int ret;
1130
1131 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1132 if (ret<0) {
1133 perror("usb_clear_halt() on IN endpoint");
1134 }
1135 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1136 if (ret<0) {
1137 perror("usb_clear_halt() on OUT endpoint");
1138 }
1139 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1140 if (ret<0) {
1141 perror("usb_clear_halt() on INTERRUPT endpoint");
1142 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001143}
1144
Linus Walleijd6a49972006-05-02 08:24:54 +00001145static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001146{
Linus Walleij58b62792007-01-07 12:38:59 +00001147 // Clear any stalled endpoints
Linus Walleijd6a49972006-05-02 08:24:54 +00001148 clear_stall(ptp_usb);
Linus Walleij58b62792007-01-07 12:38:59 +00001149 // Clear halts on any endpoints
1150 clear_halt(ptp_usb);
Linus Walleijd6a49972006-05-02 08:24:54 +00001151 // Added to clear some stuff on the OUT endpoint
1152 // TODO: is this good on the Mac too?
1153 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1154 usb_release_interface(ptp_usb->handle, interfaceNumber);
1155 // Brutally reset device
1156 // TODO: is this good on the Mac too?
1157 usb_reset(ptp_usb->handle);
1158 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001159}
1160
1161
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001162/*
1163 find_device() returns the pointer to a usb_device structure matching
1164 given busn, devicen numbers. If any or both of arguments are 0 then the
1165 first matching PTP device structure is returned.
1166 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001167static struct usb_device* find_device (int busn, int devn, short force)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001168{
Linus Walleijd6a49972006-05-02 08:24:54 +00001169 struct usb_bus *bus;
1170 struct usb_device *dev;
1171
1172 bus=init_usb();
1173 for (; bus; bus = bus->next)
1174 for (dev = bus->devices; dev; dev = dev->next)
1175 /* somtimes dev->config is null, not sure why... */
1176 if (dev->config != NULL)
1177 if (dev->descriptor.bDeviceClass!=USB_CLASS_HUB)
1178 {
1179 int curbusn, curdevn;
1180
1181 curbusn=strtol(bus->dirname,NULL,10);
1182 curdevn=strtol(dev->filename,NULL,10);
1183
1184 if (devn==0) {
1185 if (busn==0) return dev;
1186 if (curbusn==busn) return dev;
1187 } else {
1188 if ((busn==0)&&(curdevn==devn)) return dev;
1189 if ((curbusn==busn)&&(curdevn==devn)) return dev;
1190 }
1191 }
1192 return NULL;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001193}
1194
Linus Walleij6946ac52006-03-21 06:51:22 +00001195/**
1196 * This function scans through the device list to see if there are
1197 * some devices to connect to. The table at the top of this file is
1198 * used to identify potential devices.
1199 */
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001200uint16_t connect_first_device(PTPParams *params, PTP_USB *ptp_usb, uint8_t *interfaceNumber)
1201{
Linus Walleij6946ac52006-03-21 06:51:22 +00001202 struct usb_bus *bus;
1203 struct usb_device *dev;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001204 struct usb_endpoint_descriptor *ep;
1205 PTPDeviceInfo deviceinfo;
1206 uint16_t ret=0;
1207 int n;
Linus Walleij6946ac52006-03-21 06:51:22 +00001208
Linus Walleij0558ac52006-09-07 06:55:03 +00001209 // Reset device flags
1210 ptp_usb->device_flags = DEVICE_FLAG_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001211 // First try to locate the device using the extended
1212 // device descriptor.
1213 dev = probe_usb_bus_for_mtp_devices();
1214
1215 if (dev != NULL) {
1216 int i;
1217
1218 // See if we can find the name of this beast
1219 for (i = 0; i < mtp_device_table_size; i++) {
1220 LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i];
1221 if (dev->descriptor.idVendor == mtp_device->vendor_id &&
1222 dev->descriptor.idProduct == mtp_device->product_id ) {
1223 printf("Autodetected device \"%s\" (VID=%04x,PID=%04x) is known.\n",
1224 mtp_device->name, dev->descriptor.idVendor, dev->descriptor.idProduct);
Linus Walleij0558ac52006-09-07 06:55:03 +00001225 ptp_usb->device_flags = mtp_device->device_flags;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001226 break;
1227 }
1228 }
1229 if (i == mtp_device_table_size) {
1230 printf("Autodetected device with VID=%04x and PID=%04x is UNKNOWN.\n",
1231 dev->descriptor.idVendor, dev->descriptor.idProduct);
1232 printf("Please report this VID/PID and the device model name etc to the\n");
1233 printf("libmtp development team!\n");
1234 }
1235 }
1236
1237 // If autodetection fails, scan the bus for well known devices.
1238 if (dev == NULL) {
1239 bus = init_usb();
1240 for (; bus; bus = bus->next) {
1241 for (dev = bus->devices; dev; dev = dev->next) {
1242 int i;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001243
Linus Walleij1fd2d272006-05-08 09:22:01 +00001244 // Loop over the list of supported devices
1245 for (i = 0; i < mtp_device_table_size; i++) {
1246 LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i];
Linus Walleij6946ac52006-03-21 06:51:22 +00001247
Linus Walleij1fd2d272006-05-08 09:22:01 +00001248 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB &&
1249 dev->descriptor.idVendor == mtp_device->vendor_id &&
1250 dev->descriptor.idProduct == mtp_device->product_id ) {
1251
1252 printf("Found non-autodetected device \"%s\" on USB bus...\n", mtp_device->name);
Linus Walleij0558ac52006-09-07 06:55:03 +00001253 ptp_usb->device_flags = mtp_device->device_flags;
Linus Walleijd3fdd972006-05-30 15:51:37 +00001254 goto next_step;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001255
Linus Walleijb02a0662006-04-25 08:05:09 +00001256 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001257 }
Linus Walleij6946ac52006-03-21 06:51:22 +00001258 }
1259 }
1260 }
Linus Walleij1fd2d272006-05-08 09:22:01 +00001261
Linus Walleijd3fdd972006-05-30 15:51:37 +00001262next_step:
Linus Walleij1fd2d272006-05-08 09:22:01 +00001263 // Still not found any?
1264 if (dev == NULL) {
1265 return PTP_CD_RC_NO_DEVICES;
1266 }
1267
Linus Walleij0558ac52006-09-07 06:55:03 +00001268 // Found a device, then assign endpoints to ptp_usb...
Linus Walleij1fd2d272006-05-08 09:22:01 +00001269 ep = dev->config->interface->altsetting->endpoint;
1270 n = dev->config->interface->altsetting->bNumEndpoints;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001271 find_endpoints(dev, &ptp_usb->inep, &ptp_usb->inep_maxpacket,
1272 &ptp_usb->outep, &ptp_usb->outep_maxpacket, &ptp_usb->intep);
Linus Walleij1fd2d272006-05-08 09:22:01 +00001273
1274 // printf("Init PTP USB...\n");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001275 if (init_ptp_usb(params, ptp_usb, dev) < 0) {
1276 return PTP_CD_RC_ERROR_CONNECTING;
1277 }
Linus Walleij1fd2d272006-05-08 09:22:01 +00001278
1279 ret = ptp_opensession(params,1);
Linus Walleij48a2d462006-11-19 14:07:59 +00001280
1281 // This works in situations where previous bad applications have not used LIBMTP_Release_Device on exit
1282 if (ret == PTP_ERROR_IO) {
1283 printf("%s\n","PTP ERROR IO: Trying again after resetting USB");
1284 // printf("%s\n","Closing USB interface...");
1285 close_usb(ptp_usb,dev->config->interface->altsetting->bInterfaceNumber);
1286 // printf("%s\n","Init PTP USB...");
1287 if (init_ptp_usb(params, ptp_usb, dev) < 0) {
1288 return PTP_CD_RC_ERROR_CONNECTING;
1289 }
1290
1291 ret = ptp_opensession(params,1);
1292 }
1293
Linus Walleij1fd2d272006-05-08 09:22:01 +00001294 // printf("Session open (%d)...\n", ret);
1295 if (ret == PTP_RC_InvalidTransactionID) {
1296 params->transaction_id += 10;
1297 ret = ptp_opensession(params,1);
1298 }
1299 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
1300 printf("Could not open session! (Return code %d)\n Try to reset the device.\n", ret);
1301 usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber);
1302 }
1303
1304 // It is actually permissible to call this before opening the session
1305 ret = ptp_getdeviceinfo(params, &deviceinfo);
1306 if (ret != PTP_RC_OK) {
1307 printf("Could not get device info!\n");
1308 usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber);
1309 return PTP_CD_RC_ERROR_CONNECTING;
1310 }
1311
1312 // we're connected, return OK
1313 *interfaceNumber = dev->config->interface->altsetting->bInterfaceNumber;
1314 return PTP_CD_RC_CONNECTED;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001315}
1316
Linus Walleijd6a49972006-05-02 08:24:54 +00001317static 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 +00001318{
Linus Walleijd6a49972006-05-02 08:24:54 +00001319 int i,n;
1320 struct usb_endpoint_descriptor *ep;
1321
1322 ep = dev->config->interface->altsetting->endpoint;
1323 n=dev->config->interface->altsetting->bNumEndpoints;
1324
1325 for (i=0;i<n;i++) {
1326 if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1327 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1328 USB_ENDPOINT_DIR_MASK)
1329 {
1330 *inep=ep[i].bEndpointAddress;
1331 *inep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001332 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001333 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1334 {
1335 *outep=ep[i].bEndpointAddress;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001336 *outep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijd6a49972006-05-02 08:24:54 +00001337 }
1338 } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1339 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1340 USB_ENDPOINT_DIR_MASK)
1341 {
1342 *intep=ep[i].bEndpointAddress;
1343 }
1344 }
1345 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001346}
1347
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001348int open_device (int busn, int devn, short force, PTP_USB *ptp_usb, PTPParams *params, struct usb_device **dev)
1349{
1350#ifdef DEBUG
Linus Walleijd6a49972006-05-02 08:24:54 +00001351 printf("dev %i\tbus %i\n",devn,busn);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001352#endif
Linus Walleijd6a49972006-05-02 08:24:54 +00001353
1354 *dev=find_device(busn,devn,force);
1355 if (*dev==NULL) {
1356 fprintf(stderr,"could not find any device matching given "
1357 "bus/dev numbers\n");
1358 exit(-1);
1359 }
1360 find_endpoints(*dev,&ptp_usb->inep,&ptp_usb->inep_maxpacket,&ptp_usb->outep,&ptp_usb->outep_maxpacket,&ptp_usb->intep);
1361
Linus Walleij9eb3d312006-08-04 19:25:59 +00001362 if (init_ptp_usb(params, ptp_usb, *dev) < 0) {
1363 return -1;
1364 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001365 if (ptp_opensession(params,1)!=PTP_RC_OK) {
1366 fprintf(stderr,"ERROR: Could not open session!\n");
1367 close_usb(ptp_usb, (*dev)->config->interface->altsetting->bInterfaceNumber);
1368 return -1;
1369 }
1370 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001371}
1372
1373void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber)
1374{
Linus Walleijd6a49972006-05-02 08:24:54 +00001375 if (ptp_closesession(params)!=PTP_RC_OK)
1376 fprintf(stderr,"ERROR: Could not close session!\n");
1377 close_usb(ptp_usb, interfaceNumber);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001378}
1379
Linus Walleijd6a49972006-05-02 08:24:54 +00001380static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001381{
Linus Walleijd6a49972006-05-02 08:24:54 +00001382
1383 return (usb_control_msg(ptp_usb->handle,
1384 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
1385 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001386}
1387
Linus Walleijd6a49972006-05-02 08:24:54 +00001388static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001389{
Linus Walleijd6a49972006-05-02 08:24:54 +00001390 return (usb_control_msg(ptp_usb->handle,
1391 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
1392 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001393}