blob: 4b9b1a12dc1d7f383c03b5eeb4af1e1f83166ddf [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 },
Richard Lowa98d0542007-01-20 12:31:07 +0000125 // from XNJB user
126 { "Philips PSA235", 0x0471, 0x7e01, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000127
128 /*
129 * SanDisk
130 */
131 // Reported by Brian Robison
Linus Walleij0558ac52006-09-07 06:55:03 +0000132 { "SanDisk Sansa m240", 0x0781, 0x7400, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000133 // Reported by tangent_@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +0000134 { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000135 // From libgphoto2 source
Linus Walleij0558ac52006-09-07 06:55:03 +0000136 { "SanDisk Sansa e200", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000137 // Reported by gonkflea@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +0000138 { "SanDisk Sansa e260", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000139
140 /*
141 * iRiver
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000142 * we assume that PTP_OC_MTP_GetObjPropList is essentially
Linus Walleijfabffc02006-12-17 00:18:57 +0000143 * broken on all iRiver devices, meaning it simply won't return
144 * all properties for a file when asking for metadata 0xffffffff.
145 * Please test on your device if you believe it isn't broken!
146 * Some devices from http://www.mtp-ums.net/viewdeviceinfo.php
Linus Walleij3d2c7842006-08-18 09:35:08 +0000147 */
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000148 { "iRiver Portable Media Center", 0x1006, 0x4002, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
149 { "iRiver Portable Media Center", 0x1006, 0x4003, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000150 // From libgphoto2 source
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000151 { "iRiver T10", 0x4102, 0x1113, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
152 { "iRiver T20 FM", 0x4102, 0x1114, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleijfabffc02006-12-17 00:18:57 +0000153 // This appears at the MTP-UMS site
154 { "iRiver T20", 0x4102, 0x1115, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000155 { "iRiver U10", 0x4102, 0x1116, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
156 { "iRiver T10", 0x4102, 0x1117, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
157 { "iRiver T20", 0x4102, 0x1118, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
158 { "iRiver T30", 0x4102, 0x1119, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij77e047c2006-09-11 19:09:06 +0000159 // Reported by David Wolpoff
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000160 { "iRiver T10 2GB", 0x4102, 0x1120, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij87099812006-12-17 00:29:58 +0000161 // Rough guess this is the MTP device ID...
162 { "iRiver N12", 0x4102, 0x1122, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij04377da2006-08-30 13:03:35 +0000163 // Reported by Adam Torgerson
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000164 { "iRiver Clix", 0x4102, 0x112a, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000165 // Reported by Scott Call
Linus Walleij1a1b2d12006-11-23 13:32:17 +0000166 { "iRiver H10 20GB", 0x4102, 0x2101, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
167 { "iRiver H10", 0x4102, 0x2102, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000168
169 /*
170 * Dell
171 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000172 { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000173
174 /*
175 * Toshiba
176 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000177 { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE },
178 { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE },
Linus Walleij405e2392006-10-23 07:13:49 +0000179 // From libgphoto2
180 { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE },
Linus Walleij402e4bd2006-09-12 07:50:56 +0000181 // Reported by Rob Brown
182 { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE },
Linus Walleijb01d18b2006-12-13 12:49:15 +0000183
Linus Walleij6f680562006-08-19 22:49:33 +0000184 /*
185 * Archos
186 */
187 // Reported by gudul1@users.sourceforge.net
Linus Walleije5430902006-10-08 14:46:03 +0000188 { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE },
189
190 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000191 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000192 * This unit is falsely detected as USB mass storage in Linux
Linus Walleijcc4dd482006-11-25 21:58:30 +0000193 * prior to kernel 2.6.19 (fixed by patch from Alan Stern)
194 * so on older kernels special care is needed to remove the
195 * USB mass storage driver that erroneously binds to the device
196 * interface.
Linus Walleije5430902006-10-08 14:46:03 +0000197 */
Richard Lowc7d48d22006-11-29 18:24:55 +0000198 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER},
199
200 /*
201 * Microsoft
202 */
203 // Reported by Farooq Zaman
204 { "Microsoft Zune", 0x045e, 0x0710, DEVICE_FLAG_NONE },
205
206 /*
207 * Sirius
208 */
Linus Walleijb01d18b2006-12-13 12:49:15 +0000209 { "Sirius Stiletto", 0x18f6, 0x0102, DEVICE_FLAG_NONE },
210
211 /*
212 * Canon
213 * This is actually a camera, but it has a Microsoft device descriptor
214 * and reports itself as supporting the MTP extension.
215 */
216 {"Canon PowerShot A640 (PTP/MTP mode)", 0x04a9, 0x3139, DEVICE_FLAG_NONE }
Linus Walleije5430902006-10-08 14:46:03 +0000217
Linus Walleija5483642006-03-09 09:20:38 +0000218};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000219static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000220
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000221int ptpcam_usb_timeout = USB_TIMEOUT;
222
Linus Walleijd6a49972006-05-02 08:24:54 +0000223// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000224static struct usb_bus* init_usb();
225static struct usb_device *probe_usb_bus_for_mtp_devices(void);
Linus Walleijd6a49972006-05-02 08:24:54 +0000226static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);
227static struct usb_device* find_device (int busn, int devicen, short force);
228static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);
229static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000230static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000231static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000232static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000233static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
234static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000235
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000236
Linus Walleij6fd2f082006-03-28 07:19:22 +0000237int get_device_list(LIBMTP_device_entry_t ** const devices, int * const numdevs)
238{
239 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
240 *numdevs = mtp_device_table_size;
241 return 0;
242}
243
Linus Walleij1fd2d272006-05-08 09:22:01 +0000244static struct usb_bus* init_usb()
245{
246 usb_init();
247 usb_find_busses();
248 usb_find_devices();
249 return (usb_get_busses());
250}
251
252/**
253 * Check for the Microsoft OS device descriptor and returns device struct
254 * if the device is MTP-compliant. The function will only recognize
255 * a single device connected to the USB bus.
256 *
257 * @return an MTP-compliant USB device if one was found, else NULL.
258 */
259static struct usb_device *probe_usb_bus_for_mtp_devices(void)
260{
261 struct usb_bus *bus;
262
263 bus = init_usb();
264 for (; bus; bus = bus->next) {
265 struct usb_device *dev;
266
267 for (dev = bus->devices; dev; dev = dev->next) {
268 usb_dev_handle *devh;
269 unsigned char buf[1024], cmd;
270 int ret;
271
272 devh = usb_open(dev);
273 if (devh == NULL) {
274 continue;
275 }
276
277 // Read the special descripor, if possible...
278 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
279
280 if (ret < 10) {
281 // printf("Device: VID %04x/PID %04x: no extended device property...\n",
282 // dev->descriptor.idVendor,
283 // dev->descriptor.idProduct);
284 usb_close(devh);
285 continue;
286 }
Linus Walleijd5d51c82006-09-11 06:57:50 +0000287
288 // It is atleast 10 bytes...
Linus Walleij1fd2d272006-05-08 09:22:01 +0000289 if (!((buf[2] == 'M') && (buf[4]=='S') && (buf[6]=='F') && (buf[8]=='T'))) {
Linus Walleijd5d51c82006-09-11 06:57:50 +0000290 printf("This is not a Microsoft MTP descriptor...\n");
291 printf("Device response to read device property 0xee:\n");
292 data_dump_ascii (stdout, buf, ret, 0);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000293 usb_close(devh);
294 continue;
295 }
296
297 cmd = buf[16];
298 ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
299 cmd, 0, 4, (char *) buf, sizeof(buf), 1000);
300 if (ret == -1) {
301 //printf("Decice could not respond to control message 1.\n");
302 usb_close(devh);
Linus Walleijd5d51c82006-09-11 06:57:50 +0000303 continue;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000304 }
305
Linus Walleijd5d51c82006-09-11 06:57:50 +0000306 if (ret > 0x15) {
307 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
308 printf("The device has a Microsoft device descriptor, but it's not MTP.\n");
309 printf("This is not an MTP device. Presumable it is USB mass storage\n");
310 printf("with some additional Janus (DRM) support.\n");
311 printf("Device response to control message 1:\n");
312 data_dump_ascii (stdout, buf, ret, 0);
313 continue;
314 }
315 } else {
316 // Not MTP or broken
317 continue;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000318 }
319
320 ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
321 cmd, 0, 5, (char *) buf, sizeof(buf), 1000);
322 if (ret == -1) {
323 //printf("Device could not respond to control message 2.\n");
324 usb_close(devh);
Linus Walleijd5d51c82006-09-11 06:57:50 +0000325 // Return the device anyway, it said previously it was MTP, right?
Linus Walleij1fd2d272006-05-08 09:22:01 +0000326 return dev;
327 }
328
Linus Walleijd5d51c82006-09-11 06:57:50 +0000329 if (ret > 0x15) {
330 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
331 printf("This device does not respond with MTP characteristics on\n");
332 printf("the second device property read (0x05), but will be regarded\n");
333 printf("as MTP anyway.\n");
334 printf("Device response to control message 2:\n");
335 data_dump_ascii (stdout, buf, ret, 0);
336 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000337 }
Linus Walleijd5d51c82006-09-11 06:57:50 +0000338
Linus Walleij1fd2d272006-05-08 09:22:01 +0000339 usb_close(devh);
340 // We can return the device here, it will be the first.
341 // If it was not MTP, the loop continues before it reaches this point.
342 return dev;
343 }
344 }
345 // If nothing was found we end up here.
346 return NULL;
347}
348
349/**
350 * Detect the MTP device descriptor and return the VID and PID
351 * of the first device found. This is a very low-level function
352 * which is intended for use with <b>udev</b> or other hotplug
353 * mechanisms. The idea is that a script may want to know if the
354 * just plugged-in device was an MTP device or not.
355 * @param vid the Vendor ID (VID) of the first device found.
356 * @param pid the Product ID (PID) of the first device found.
357 * @return the number of detected devices or -1 if the call
358 * was unsuccessful.
359 */
360int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
361{
362 struct usb_device *dev = probe_usb_bus_for_mtp_devices();
363 if (dev == NULL) {
364 return 0;
365 }
366 *vid = dev->descriptor.idVendor;
367 *pid = dev->descriptor.idProduct;
368 return 1;
369}
370
Linus Walleijc6210fb2006-05-08 11:11:41 +0000371/**
372 * This routine just dumps out low-level
373 * USB information about the current device.
374 * @param ptp_usb the USB device to get information from.
375 */
376void dump_usbinfo(PTP_USB *ptp_usb)
377{
378 int res;
379 struct usb_device *dev;
380
381#ifdef LIBUSB_HAS_GET_DRIVER_NP
382 char devname[0x10];
383
384 devname[0] = '\0';
385 res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));
386 if (devname[0] != '\0') {
387 printf(" Using kernel interface \"%s\"\n", devname);
388 }
389#endif
390 dev = usb_device(ptp_usb->handle);
391 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
392 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
393 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
394 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000395 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
396 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000397 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
398 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000399 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000400 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
401}
402
Linus Walleija0323272007-01-07 12:21:30 +0000403static void
404ptp_debug (PTPParams *params, const char *format, ...)
405{
406 va_list args;
407
408 va_start (args, format);
409 if (params->debug_func!=NULL)
410 params->debug_func (params->data, format, args);
411 else
412 {
413 vfprintf (stderr, format, args);
414 fprintf (stderr,"\n");
415 fflush (stderr);
416 }
417 va_end (args);
418}
419
420static void
421ptp_error (PTPParams *params, const char *format, ...)
422{
423 va_list args;
424
425 va_start (args, format);
426 if (params->error_func!=NULL)
427 params->error_func (params->data, format, args);
428 else
429 {
430 vfprintf (stderr, format, args);
431 fprintf (stderr,"\n");
432 fflush (stderr);
433 }
434 va_end (args);
435}
436
Linus Walleijc6210fb2006-05-08 11:11:41 +0000437
Linus Walleij99ed83c2007-01-02 18:46:02 +0000438/*
439 * ptp_read_func() and ptp_write_func() are
440 * based on same functions in library.c in libgphoto2.
441 * Much reading packet logs and having fun with trials and errors
442 * reveals that WMP / Windows is probably using an algorithm like this
443 * for large transfers:
444 *
445 * 1. Send the command (0x0c bytes) if headers are split, else, send
446 * command plus sizeof(endpoint) - 0x0c bytes.
447 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
448 * split headers. Else goto 3.
449 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
450 * We call 0x10000 CONTEXT_BLOCK_SIZE.
451 * 4. Send remaining bytes MOD sizeof(endpoint)
452 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
453 * then also send a zero-length package.
454 */
Richard Low021421e2007-01-01 18:08:57 +0000455#define CONTEXT_BLOCK_SIZE 0x10000
Linus Walleijb02a0662006-04-25 08:05:09 +0000456static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000457ptp_read_func (
458 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000459 unsigned long *readbytes,
460 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000461) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000462 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000463 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000464 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000465 unsigned long curread = 0;
466 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000467 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000468
469 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000470 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000471 while (curread < size) {
472 toread = size - curread;
Linus Walleijc49c6372007-01-09 00:18:51 +0000473 if (toread > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000474 toread = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000475 } else if (toread > ptp_usb->outep_maxpacket) {
Richard Lowb89ea942007-01-02 11:47:19 +0000476 toread -= toread % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000477 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000478
479 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptpcam_usb_timeout);
Richard Low4df32f82007-01-11 20:04:39 +0000480
Linus Walleijc49c6372007-01-09 00:18:51 +0000481 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000482 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000483 }
Linus Walleije78de6d2006-10-31 14:46:36 +0000484#ifdef ENABLE_USB_BULK_DEBUG
485 printf("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000486 if (result == 0)
487 printf("Zero Read\n");
488 else
489 data_dump_ascii (stdout,bytes,result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000490#endif
Linus Walleijc49c6372007-01-09 00:18:51 +0000491 handler->putfunc(NULL, handler->private, result, bytes, &written);
492 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000493 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000494
495 // Increase counters, call callback
496 if (ptp_usb->callback_active) {
497 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
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 }
502 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 < toread) /* short reads are common */
510 break;
511 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000512 if (readbytes) *readbytes = curread;
513 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000514
Richard Low4df32f82007-01-11 20:04:39 +0000515 // there might be a zero packet waiting for us...
516 if (readzero && curread % ptp_usb->outep_maxpacket == 0) {
517 char temp;
518 int zeroresult = 0;
519#ifdef ENABLE_USB_BULK_DEBUG
520 printf("<==USB IN\n");
521 printf("Zero Read\n");
522#endif
523 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptpcam_usb_timeout);
524 if (zeroresult != 0)
525 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
526 }
527
Linus Walleijd6a49972006-05-02 08:24:54 +0000528 if (result > 0) {
Linus Walleijd6a49972006-05-02 08:24:54 +0000529 return (PTP_RC_OK);
530 } else {
531 return PTP_ERROR_IO;
532 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000533}
534
Linus Walleijb02a0662006-04-25 08:05:09 +0000535static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000536ptp_write_func (
537 unsigned long size,
538 PTPDataHandler *handler,
539 void *data,
540 unsigned long *written
541) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000542 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000543 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000544 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000545 unsigned long curwrite = 0;
546 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000547
548 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000549 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000550 if (!bytes) {
551 return PTP_ERROR_IO;
552 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000553 while (curwrite < size) {
554 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000555 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000556 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000557 } else {
558 // This magic makes packets the same size that WMP send them.
559 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000560 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000561 }
562 }
563 handler->getfunc(NULL, handler->private,towrite,bytes,&towrite);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000564 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000565#ifdef ENABLE_USB_BULK_DEBUG
566 printf("USB OUT==>\n");
Richard Lowedd61832006-12-30 12:38:18 +0000567 data_dump_ascii (stdout,bytes,towrite,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000568#endif
Linus Walleijc49c6372007-01-09 00:18:51 +0000569 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000570 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000571 }
572 // Increase counters
573 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000574 curwrite += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000575
576 // call callback
577 if (ptp_usb->callback_active) {
578 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
579 // send last update and disable callback.
580 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
581 ptp_usb->callback_active = 0;
582 }
583 if (ptp_usb->current_transfer_callback != NULL) {
584 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
585 ptp_usb->current_transfer_total,
586 ptp_usb->current_transfer_callback_data);
587 }
588 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000589 if (result < towrite) /* short writes happen */
590 break;
591 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000592 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000593 if (written) {
594 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000595 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000596
Linus Walleijc49c6372007-01-09 00:18:51 +0000597
Richard Lowef05b892007-01-03 21:18:56 +0000598 // If this is the last transfer send a zero write if required
599 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000600 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
Richard Low021421e2007-01-01 18:08:57 +0000601#ifdef ENABLE_USB_BULK_DEBUG
602 printf("USB OUT==>\n");
603 printf("Zero Write\n");
604#endif
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000605 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000606 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000607 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000608
Linus Walleijd6a49972006-05-02 08:24:54 +0000609 if (result < 0)
610 return PTP_ERROR_IO;
611 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000612}
613
Linus Walleija0323272007-01-07 12:21:30 +0000614/* memory data get/put handler */
615typedef struct {
616 unsigned char *data;
617 unsigned long size, curoff;
618} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000619
Linus Walleija0323272007-01-07 12:21:30 +0000620static uint16_t
621memory_getfunc(PTPParams* params, void* private,
622 unsigned long wantlen, unsigned char *data,
623 unsigned long *gotlen
624) {
625 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
626 unsigned long tocopy = wantlen;
627
628 if (priv->curoff + tocopy > priv->size)
629 tocopy = priv->size - priv->curoff;
630 memcpy (data, priv->data + priv->curoff, tocopy);
631 priv->curoff += tocopy;
632 *gotlen = tocopy;
633 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000634}
635
Linus Walleija0323272007-01-07 12:21:30 +0000636static uint16_t
637memory_putfunc(PTPParams* params, void* private,
638 unsigned long sendlen, unsigned char *data,
639 unsigned long *putlen
640) {
641 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
642
643 if (priv->curoff + sendlen > priv->size) {
644 priv->data = realloc (priv->data, priv->curoff+sendlen);
645 priv->size = priv->curoff + sendlen;
646 }
647 memcpy (priv->data + priv->curoff, data, sendlen);
648 priv->curoff += sendlen;
649 *putlen = sendlen;
650 return PTP_RC_OK;
651}
652
653/* init private struct for receiving data. */
654static uint16_t
655ptp_init_recv_memory_handler(PTPDataHandler *handler) {
656 PTPMemHandlerPrivate* priv;
657 priv = malloc (sizeof(PTPMemHandlerPrivate));
658 handler->private = priv;
659 handler->getfunc = memory_getfunc;
660 handler->putfunc = memory_putfunc;
661 priv->data = NULL;
662 priv->size = 0;
663 priv->curoff = 0;
664 return PTP_RC_OK;
665}
666
667/* init private struct and put data in for sending data.
668 * data is still owned by caller.
669 */
670static uint16_t
671ptp_init_send_memory_handler(PTPDataHandler *handler,
672 unsigned char *data, unsigned long len
673) {
674 PTPMemHandlerPrivate* priv;
675 priv = malloc (sizeof(PTPMemHandlerPrivate));
676 if (!priv)
677 return PTP_RC_GeneralError;
678 handler->private = priv;
679 handler->getfunc = memory_getfunc;
680 handler->putfunc = memory_putfunc;
681 priv->data = data;
682 priv->size = len;
683 priv->curoff = 0;
684 return PTP_RC_OK;
685}
686
687/* free private struct + data */
688static uint16_t
689ptp_exit_send_memory_handler (PTPDataHandler *handler) {
690 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
691 /* data is owned by caller */
692 free (priv);
693 return PTP_RC_OK;
694}
695
696/* hand over our internal data to caller */
697static uint16_t
698ptp_exit_recv_memory_handler (PTPDataHandler *handler,
699 unsigned char **data, unsigned long *size
700) {
701 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
702 *data = priv->data;
703 *size = priv->size;
704 free (priv);
705 return PTP_RC_OK;
706}
707
708/* send / receive functions */
709
710uint16_t
711ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
712{
713 uint16_t ret;
714 PTPUSBBulkContainer usbreq;
715 PTPDataHandler memhandler;
716 unsigned long written, towrite;
717
718 /* build appropriate USB container */
719 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
720 (sizeof(uint32_t)*(5-req->Nparam)));
721 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
722 usbreq.code=htod16(req->Code);
723 usbreq.trans_id=htod32(req->Transaction_ID);
724 usbreq.payload.params.param1=htod32(req->Param1);
725 usbreq.payload.params.param2=htod32(req->Param2);
726 usbreq.payload.params.param3=htod32(req->Param3);
727 usbreq.payload.params.param4=htod32(req->Param4);
728 usbreq.payload.params.param5=htod32(req->Param5);
729 /* send it to responder */
730 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
731 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
732 ret=ptp_write_func(
733 towrite,
734 &memhandler,
735 params->data,
736 &written
737 );
738 ptp_exit_send_memory_handler (&memhandler);
739 if (ret!=PTP_RC_OK) {
740 ret = PTP_ERROR_IO;
741/* ptp_error (params,
742 "PTP: request code 0x%04x sending req error 0x%04x",
743 req->Code,ret); */
744 }
745 if (written != towrite) {
746 ptp_error (params,
747 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
748 written, towrite
749 );
750 ret = PTP_ERROR_IO;
751 }
752 return ret;
753}
754
755uint16_t
756ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
757 unsigned long size, PTPDataHandler *handler
758) {
759 uint16_t ret;
760 int wlen, datawlen;
761 unsigned long written;
762 PTPUSBBulkContainer usbdata;
763 uint32_t bytes_left_to_transfer;
764 PTPDataHandler memhandler;
765
766 /* build appropriate USB container */
767 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
768 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
769 usbdata.code = htod16(ptp->Code);
770 usbdata.trans_id= htod32(ptp->Transaction_ID);
771
772 ((PTP_USB*)params->data)->current_transfer_complete = 0;
773 ((PTP_USB*)params->data)->current_transfer_total = size;
774
775 if (params->split_header_data) {
776 datawlen = 0;
777 wlen = PTP_USB_BULK_HDR_LEN;
778 } else {
779 unsigned long gotlen;
780 /* For all camera devices. */
781 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN)?size:PTP_USB_BULK_PAYLOAD_LEN;
782 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
783 ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen);
784 if (ret != PTP_RC_OK)
785 return ret;
786 if (gotlen != datawlen)
787 return PTP_RC_GeneralError;
788 }
789 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
790 /* send first part of data */
791 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
792 ptp_exit_send_memory_handler (&memhandler);
793 if (ret!=PTP_RC_OK) {
794 ret = PTP_ERROR_IO;
795/* ptp_error (params,
796 "PTP: request code 0x%04x sending data error 0x%04x",
797 ptp->Code,ret);*/
798 return ret;
799 }
800 if (size <= datawlen) return ret;
801 /* if everything OK send the rest */
802 bytes_left_to_transfer = size-datawlen;
803 ret = PTP_RC_OK;
804 while(bytes_left_to_transfer > 0) {
805 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
806 if (ret != PTP_RC_OK)
807 break;
808 if (written == 0) {
809 ret = PTP_ERROR_IO;
810 break;
811 }
812 bytes_left_to_transfer -= written;
813 }
814 if (ret!=PTP_RC_OK)
815 ret = PTP_ERROR_IO;
816 return ret;
817}
818
819static uint16_t ptp_usb_getpacket(PTPParams *params,
820 PTPUSBBulkContainer *packet, unsigned long *rlen)
821{
822 PTPDataHandler memhandler;
823 uint16_t ret;
824 unsigned char *x = NULL;
825
826 /* read the header and potentially the first data */
827 if (params->response_packet_size > 0) {
828 /* If there is a buffered packet, just use it. */
829 memcpy(packet, params->response_packet, params->response_packet_size);
830 *rlen = params->response_packet_size;
831 free(params->response_packet);
832 params->response_packet = NULL;
833 params->response_packet_size = 0;
834 /* Here this signifies a "virtual read" */
835 return PTP_RC_OK;
836 }
837 ptp_init_recv_memory_handler (&memhandler);
Richard Low4df32f82007-01-11 20:04:39 +0000838 ret = ptp_read_func( sizeof(*packet), &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +0000839 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
840 if (x) {
841 memcpy (packet, x, *rlen);
842 free (x);
843 }
844 return ret;
845}
846
847uint16_t
848ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
849{
850 uint16_t ret;
851 PTPUSBBulkContainer usbdata;
852 unsigned char *data;
853 unsigned long written;
854
855 memset(&usbdata,0,sizeof(usbdata));
856 do {
857 unsigned long len, rlen;
858
859 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
860 if (ret!=PTP_RC_OK) {
861 ret = PTP_ERROR_IO;
862 break;
863 } else
864 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
865 ret = PTP_ERROR_DATA_EXPECTED;
866 break;
867 } else
868 if (dtoh16(usbdata.code)!=ptp->Code) {
869 ret = dtoh16(usbdata.code);
870 break;
871 }
872 if (usbdata.length == 0xffffffffU) {
873 /* stuff data directly to passed data handler */
874 while (1) {
875 unsigned long readdata;
876 int xret;
877
878 xret = ptp_read_func(
879 PTP_USB_BULK_HS_MAX_PACKET_LEN,
880 handler,
881 params->data,
Richard Low4df32f82007-01-11 20:04:39 +0000882 &readdata,
883 0
Linus Walleija0323272007-01-07 12:21:30 +0000884 );
885 if (xret == -1)
886 return PTP_ERROR_IO;
887 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN)
888 break;
889 }
890 return PTP_RC_OK;
891 }
892 if (rlen > dtoh32(usbdata.length)) {
893 /*
894 * Buffer the surplus response packet if it is >=
895 * PTP_USB_BULK_HDR_LEN
896 * (i.e. it is probably an entire package)
897 * else discard it as erroneous surplus data.
898 * This will even work if more than 2 packets appear
899 * in the same transaction, they will just be handled
900 * iteratively.
901 *
902 * Marcus observed stray bytes on iRiver devices;
903 * these are still discarded.
904 */
905 unsigned int packlen = dtoh32(usbdata.length);
906 unsigned int surplen = rlen - packlen;
907
908 if (surplen >= PTP_USB_BULK_HDR_LEN) {
909 params->response_packet = malloc(surplen);
910 memcpy(params->response_packet,
911 (uint8_t *) &usbdata + packlen, surplen);
912 params->response_packet_size = surplen;
913 } else {
914 ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes too much, expect problems!", rlen - dtoh32(usbdata.length));
915 }
916 rlen = packlen;
917 }
918
919 /* For most PTP devices rlen is 512 == sizeof(usbdata)
920 * here. For MTP devices splitting header and data it might
921 * be 12.
922 */
923 /* Evaluate full data length. */
924 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
925
926 /* autodetect split header/data MTP devices */
927 if (dtoh32(usbdata.length) > 12 && (rlen==12))
928 params->split_header_data = 1;
929
930 data = malloc(PTP_USB_BULK_HS_MAX_PACKET_LEN);
931 /* Copy first part of data to 'data' */
932 handler->putfunc(
933 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
934 &written
935 );
936
937 /* Is that all of data? */
938 if (len+PTP_USB_BULK_HDR_LEN<=rlen) break;
939
940 /* If not read the rest of it. */
941 ret=ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
942 handler,
Richard Low4df32f82007-01-11 20:04:39 +0000943 params->data, &rlen, 1);
Linus Walleija0323272007-01-07 12:21:30 +0000944 if (ret!=PTP_RC_OK) {
945 ret = PTP_ERROR_IO;
946 break;
947 }
948 } while (0);
949/*
950 if (ret!=PTP_RC_OK) {
951 ptp_error (params,
952 "PTP: request code 0x%04x getting data error 0x%04x",
953 ptp->Code, ret);
954 }*/
955 return ret;
956}
957
958uint16_t
959ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
960{
961 uint16_t ret;
962 unsigned long rlen;
963 PTPUSBBulkContainer usbresp;
964
965 memset(&usbresp,0,sizeof(usbresp));
966 /* read response, it should never be longer than sizeof(usbresp) */
967 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
968
969 if (ret!=PTP_RC_OK) {
970 ret = PTP_ERROR_IO;
971 } else
972 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
973 ret = PTP_ERROR_RESP_EXPECTED;
974 } else
975 if (dtoh16(usbresp.code)!=resp->Code) {
976 ret = dtoh16(usbresp.code);
977 }
978 if (ret!=PTP_RC_OK) {
979/* ptp_error (params,
980 "PTP: request code 0x%04x getting resp error 0x%04x",
981 resp->Code, ret);*/
982 return ret;
983 }
984 /* build an appropriate PTPContainer */
985 resp->Code=dtoh16(usbresp.code);
986 resp->SessionID=params->session_id;
987 resp->Transaction_ID=dtoh32(usbresp.trans_id);
988 resp->Param1=dtoh32(usbresp.payload.params.param1);
989 resp->Param2=dtoh32(usbresp.payload.params.param2);
990 resp->Param3=dtoh32(usbresp.payload.params.param3);
991 resp->Param4=dtoh32(usbresp.payload.params.param4);
992 resp->Param5=dtoh32(usbresp.payload.params.param5);
993 return ret;
994}
995
996/* Event handling functions */
997
998/* PTP Events wait for or check mode */
999#define PTP_EVENT_CHECK 0x0000 /* waits for */
1000#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1001
1002static inline uint16_t
1003ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1004{
1005 uint16_t ret;
1006 int result;
1007 unsigned long rlen;
1008 PTPUSBEventContainer usbevent;
1009 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1010
1011 memset(&usbevent,0,sizeof(usbevent));
1012
1013 if ((params==NULL) || (event==NULL))
1014 return PTP_ERROR_BADPARAM;
1015 ret = PTP_RC_OK;
1016 switch(wait) {
1017 case PTP_EVENT_CHECK:
1018 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1019 if (result==0)
1020 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1021 if (result < 0) ret = PTP_ERROR_IO;
1022 break;
1023 case PTP_EVENT_CHECK_FAST:
1024 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptpcam_usb_timeout);
1025 if (result==0)
1026 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptpcam_usb_timeout);
1027 if (result < 0) ret = PTP_ERROR_IO;
1028 break;
1029 default:
1030 ret=PTP_ERROR_BADPARAM;
1031 break;
1032 }
1033 if (ret!=PTP_RC_OK) {
1034 ptp_error (params,
1035 "PTP: reading event an error 0x%04x occurred", ret);
1036 return PTP_ERROR_IO;
1037 }
1038 rlen = result;
1039 if (rlen < 8) {
1040 ptp_error (params,
1041 "PTP: reading event an short read of %ld bytes occurred", rlen);
1042 return PTP_ERROR_IO;
1043 }
1044 /* if we read anything over interrupt endpoint it must be an event */
1045 /* build an appropriate PTPContainer */
1046 event->Code=dtoh16(usbevent.code);
1047 event->SessionID=params->session_id;
1048 event->Transaction_ID=dtoh32(usbevent.trans_id);
1049 event->Param1=dtoh32(usbevent.param1);
1050 event->Param2=dtoh32(usbevent.param2);
1051 event->Param3=dtoh32(usbevent.param3);
1052 return ret;
1053}
1054
1055uint16_t
1056ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1057
1058 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1059}
1060
1061uint16_t
1062ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1063
1064 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1065}
1066
1067
Linus Walleij9eb3d312006-08-04 19:25:59 +00001068static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001069{
Linus Walleijd6a49972006-05-02 08:24:54 +00001070 usb_dev_handle *device_handle;
1071
Linus Walleijd6a49972006-05-02 08:24:54 +00001072 params->error_func=NULL;
1073 params->debug_func=NULL;
1074 params->sendreq_func=ptp_usb_sendreq;
1075 params->senddata_func=ptp_usb_senddata;
1076 params->getresp_func=ptp_usb_getresp;
1077 params->getdata_func=ptp_usb_getdata;
1078 params->data=ptp_usb;
1079 params->transaction_id=0;
1080 params->byteorder = PTP_DL_LE;
1081
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001082 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001083 if (!device_handle) {
1084 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001085 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001086 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001087 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001088#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1089 /*
1090 * If this device is known to be wrongfully claimed by other kernel
1091 * drivers (such as mass storage), then try to unload it to make it
1092 * accessible from user space.
1093 */
1094 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
1095 if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij7beba572006-11-29 08:56:12 +00001096 // Totally ignore this error!
1097 // perror("usb_detach_kernel_driver_np()");
Linus Walleij501ba4d2006-10-16 06:17:04 +00001098 }
1099 }
1100#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001101#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001102 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001103 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1104 perror("usb_set_configuration()");
1105 return -1;
1106 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001107#endif
Linus Walleijef5cca92006-05-05 07:19:08 +00001108 if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001109 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001110 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001111 }
Linus Walleijc6210fb2006-05-08 11:11:41 +00001112 ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber;
Linus Walleijd6a49972006-05-02 08:24:54 +00001113 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001114 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001115}
1116
Linus Walleijd6a49972006-05-02 08:24:54 +00001117static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001118{
Linus Walleij58b62792007-01-07 12:38:59 +00001119 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001120 int ret;
1121
1122 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001123 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001124 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001125 if (ret<0) {
1126 perror ("inep: usb_get_endpoint_status()");
1127 } else if (status) {
1128 printf("Clearing stall on IN endpoint\n");
1129 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1130 if (ret<0) {
1131 perror ("usb_clear_stall_feature()");
1132 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001133 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001134
1135 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001136 status=0;
1137 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1138 if (ret<0) {
1139 perror("outep: usb_get_endpoint_status()");
1140 } else if (status) {
1141 printf("Clearing stall on OUT endpoint\n");
1142 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1143 if (ret<0) {
1144 perror("usb_clear_stall_feature()");
1145 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001146 }
Linus Walleij58b62792007-01-07 12:38:59 +00001147
1148 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1149}
1150
1151static void clear_halt(PTP_USB* ptp_usb)
1152{
1153 int ret;
1154
1155 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1156 if (ret<0) {
1157 perror("usb_clear_halt() on IN endpoint");
1158 }
1159 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1160 if (ret<0) {
1161 perror("usb_clear_halt() on OUT endpoint");
1162 }
1163 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1164 if (ret<0) {
1165 perror("usb_clear_halt() on INTERRUPT endpoint");
1166 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001167}
1168
Linus Walleijd6a49972006-05-02 08:24:54 +00001169static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001170{
Linus Walleij58b62792007-01-07 12:38:59 +00001171 // Clear any stalled endpoints
Linus Walleijd6a49972006-05-02 08:24:54 +00001172 clear_stall(ptp_usb);
Linus Walleij58b62792007-01-07 12:38:59 +00001173 // Clear halts on any endpoints
1174 clear_halt(ptp_usb);
Linus Walleijd6a49972006-05-02 08:24:54 +00001175 // Added to clear some stuff on the OUT endpoint
1176 // TODO: is this good on the Mac too?
1177 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1178 usb_release_interface(ptp_usb->handle, interfaceNumber);
1179 // Brutally reset device
1180 // TODO: is this good on the Mac too?
1181 usb_reset(ptp_usb->handle);
1182 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001183}
1184
1185
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001186/*
1187 find_device() returns the pointer to a usb_device structure matching
1188 given busn, devicen numbers. If any or both of arguments are 0 then the
1189 first matching PTP device structure is returned.
1190 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001191static struct usb_device* find_device (int busn, int devn, short force)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001192{
Linus Walleijd6a49972006-05-02 08:24:54 +00001193 struct usb_bus *bus;
1194 struct usb_device *dev;
1195
1196 bus=init_usb();
1197 for (; bus; bus = bus->next)
1198 for (dev = bus->devices; dev; dev = dev->next)
1199 /* somtimes dev->config is null, not sure why... */
1200 if (dev->config != NULL)
1201 if (dev->descriptor.bDeviceClass!=USB_CLASS_HUB)
1202 {
1203 int curbusn, curdevn;
1204
1205 curbusn=strtol(bus->dirname,NULL,10);
1206 curdevn=strtol(dev->filename,NULL,10);
1207
1208 if (devn==0) {
1209 if (busn==0) return dev;
1210 if (curbusn==busn) return dev;
1211 } else {
1212 if ((busn==0)&&(curdevn==devn)) return dev;
1213 if ((curbusn==busn)&&(curdevn==devn)) return dev;
1214 }
1215 }
1216 return NULL;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001217}
1218
Linus Walleij6946ac52006-03-21 06:51:22 +00001219/**
1220 * This function scans through the device list to see if there are
1221 * some devices to connect to. The table at the top of this file is
1222 * used to identify potential devices.
1223 */
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001224uint16_t connect_first_device(PTPParams *params, PTP_USB *ptp_usb, uint8_t *interfaceNumber)
1225{
Linus Walleij6946ac52006-03-21 06:51:22 +00001226 struct usb_bus *bus;
1227 struct usb_device *dev;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001228 struct usb_endpoint_descriptor *ep;
1229 PTPDeviceInfo deviceinfo;
1230 uint16_t ret=0;
1231 int n;
Linus Walleij6946ac52006-03-21 06:51:22 +00001232
Linus Walleij0558ac52006-09-07 06:55:03 +00001233 // Reset device flags
1234 ptp_usb->device_flags = DEVICE_FLAG_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001235 // First try to locate the device using the extended
1236 // device descriptor.
1237 dev = probe_usb_bus_for_mtp_devices();
1238
1239 if (dev != NULL) {
1240 int i;
1241
1242 // See if we can find the name of this beast
1243 for (i = 0; i < mtp_device_table_size; i++) {
1244 LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i];
1245 if (dev->descriptor.idVendor == mtp_device->vendor_id &&
1246 dev->descriptor.idProduct == mtp_device->product_id ) {
1247 printf("Autodetected device \"%s\" (VID=%04x,PID=%04x) is known.\n",
1248 mtp_device->name, dev->descriptor.idVendor, dev->descriptor.idProduct);
Linus Walleij0558ac52006-09-07 06:55:03 +00001249 ptp_usb->device_flags = mtp_device->device_flags;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001250 break;
1251 }
1252 }
1253 if (i == mtp_device_table_size) {
1254 printf("Autodetected device with VID=%04x and PID=%04x is UNKNOWN.\n",
1255 dev->descriptor.idVendor, dev->descriptor.idProduct);
1256 printf("Please report this VID/PID and the device model name etc to the\n");
1257 printf("libmtp development team!\n");
1258 }
1259 }
1260
1261 // If autodetection fails, scan the bus for well known devices.
1262 if (dev == NULL) {
1263 bus = init_usb();
1264 for (; bus; bus = bus->next) {
1265 for (dev = bus->devices; dev; dev = dev->next) {
1266 int i;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001267
Linus Walleij1fd2d272006-05-08 09:22:01 +00001268 // Loop over the list of supported devices
1269 for (i = 0; i < mtp_device_table_size; i++) {
1270 LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i];
Linus Walleij6946ac52006-03-21 06:51:22 +00001271
Linus Walleij1fd2d272006-05-08 09:22:01 +00001272 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB &&
1273 dev->descriptor.idVendor == mtp_device->vendor_id &&
1274 dev->descriptor.idProduct == mtp_device->product_id ) {
1275
1276 printf("Found non-autodetected device \"%s\" on USB bus...\n", mtp_device->name);
Linus Walleij0558ac52006-09-07 06:55:03 +00001277 ptp_usb->device_flags = mtp_device->device_flags;
Linus Walleijd3fdd972006-05-30 15:51:37 +00001278 goto next_step;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001279
Linus Walleijb02a0662006-04-25 08:05:09 +00001280 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001281 }
Linus Walleij6946ac52006-03-21 06:51:22 +00001282 }
1283 }
1284 }
Linus Walleij1fd2d272006-05-08 09:22:01 +00001285
Linus Walleijd3fdd972006-05-30 15:51:37 +00001286next_step:
Linus Walleij1fd2d272006-05-08 09:22:01 +00001287 // Still not found any?
1288 if (dev == NULL) {
1289 return PTP_CD_RC_NO_DEVICES;
1290 }
1291
Linus Walleij0558ac52006-09-07 06:55:03 +00001292 // Found a device, then assign endpoints to ptp_usb...
Linus Walleij1fd2d272006-05-08 09:22:01 +00001293 ep = dev->config->interface->altsetting->endpoint;
1294 n = dev->config->interface->altsetting->bNumEndpoints;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001295 find_endpoints(dev, &ptp_usb->inep, &ptp_usb->inep_maxpacket,
1296 &ptp_usb->outep, &ptp_usb->outep_maxpacket, &ptp_usb->intep);
Linus Walleij1fd2d272006-05-08 09:22:01 +00001297
1298 // printf("Init PTP USB...\n");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001299 if (init_ptp_usb(params, ptp_usb, dev) < 0) {
1300 return PTP_CD_RC_ERROR_CONNECTING;
1301 }
Linus Walleij1fd2d272006-05-08 09:22:01 +00001302
1303 ret = ptp_opensession(params,1);
Linus Walleij48a2d462006-11-19 14:07:59 +00001304
1305 // This works in situations where previous bad applications have not used LIBMTP_Release_Device on exit
1306 if (ret == PTP_ERROR_IO) {
1307 printf("%s\n","PTP ERROR IO: Trying again after resetting USB");
1308 // printf("%s\n","Closing USB interface...");
1309 close_usb(ptp_usb,dev->config->interface->altsetting->bInterfaceNumber);
1310 // printf("%s\n","Init PTP USB...");
1311 if (init_ptp_usb(params, ptp_usb, dev) < 0) {
1312 return PTP_CD_RC_ERROR_CONNECTING;
1313 }
1314
1315 ret = ptp_opensession(params,1);
1316 }
1317
Linus Walleij1fd2d272006-05-08 09:22:01 +00001318 // printf("Session open (%d)...\n", ret);
1319 if (ret == PTP_RC_InvalidTransactionID) {
1320 params->transaction_id += 10;
1321 ret = ptp_opensession(params,1);
1322 }
1323 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
1324 printf("Could not open session! (Return code %d)\n Try to reset the device.\n", ret);
1325 usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber);
1326 }
1327
1328 // It is actually permissible to call this before opening the session
1329 ret = ptp_getdeviceinfo(params, &deviceinfo);
1330 if (ret != PTP_RC_OK) {
1331 printf("Could not get device info!\n");
1332 usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber);
1333 return PTP_CD_RC_ERROR_CONNECTING;
1334 }
1335
1336 // we're connected, return OK
1337 *interfaceNumber = dev->config->interface->altsetting->bInterfaceNumber;
1338 return PTP_CD_RC_CONNECTED;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001339}
1340
Linus Walleijd6a49972006-05-02 08:24:54 +00001341static 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 +00001342{
Linus Walleijd6a49972006-05-02 08:24:54 +00001343 int i,n;
1344 struct usb_endpoint_descriptor *ep;
1345
1346 ep = dev->config->interface->altsetting->endpoint;
1347 n=dev->config->interface->altsetting->bNumEndpoints;
1348
1349 for (i=0;i<n;i++) {
1350 if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1351 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1352 USB_ENDPOINT_DIR_MASK)
1353 {
1354 *inep=ep[i].bEndpointAddress;
1355 *inep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001356 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001357 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1358 {
1359 *outep=ep[i].bEndpointAddress;
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001360 *outep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijd6a49972006-05-02 08:24:54 +00001361 }
1362 } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1363 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1364 USB_ENDPOINT_DIR_MASK)
1365 {
1366 *intep=ep[i].bEndpointAddress;
1367 }
1368 }
1369 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001370}
1371
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001372int open_device (int busn, int devn, short force, PTP_USB *ptp_usb, PTPParams *params, struct usb_device **dev)
1373{
1374#ifdef DEBUG
Linus Walleijd6a49972006-05-02 08:24:54 +00001375 printf("dev %i\tbus %i\n",devn,busn);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001376#endif
Linus Walleijd6a49972006-05-02 08:24:54 +00001377
1378 *dev=find_device(busn,devn,force);
1379 if (*dev==NULL) {
1380 fprintf(stderr,"could not find any device matching given "
1381 "bus/dev numbers\n");
1382 exit(-1);
1383 }
1384 find_endpoints(*dev,&ptp_usb->inep,&ptp_usb->inep_maxpacket,&ptp_usb->outep,&ptp_usb->outep_maxpacket,&ptp_usb->intep);
1385
Linus Walleij9eb3d312006-08-04 19:25:59 +00001386 if (init_ptp_usb(params, ptp_usb, *dev) < 0) {
1387 return -1;
1388 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001389 if (ptp_opensession(params,1)!=PTP_RC_OK) {
1390 fprintf(stderr,"ERROR: Could not open session!\n");
1391 close_usb(ptp_usb, (*dev)->config->interface->altsetting->bInterfaceNumber);
1392 return -1;
1393 }
1394 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001395}
1396
1397void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber)
1398{
Linus Walleijd6a49972006-05-02 08:24:54 +00001399 if (ptp_closesession(params)!=PTP_RC_OK)
1400 fprintf(stderr,"ERROR: Could not close session!\n");
1401 close_usb(ptp_usb, interfaceNumber);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001402}
1403
Linus Walleijd6a49972006-05-02 08:24:54 +00001404static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001405{
Linus Walleijd6a49972006-05-02 08:24:54 +00001406
1407 return (usb_control_msg(ptp_usb->handle,
1408 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
1409 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001410}
1411
Linus Walleijd6a49972006-05-02 08:24:54 +00001412static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001413{
Linus Walleijd6a49972006-05-02 08:24:54 +00001414 return (usb_control_msg(ptp_usb->handle,
1415 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
1416 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001417}