blob: f06d51b1ea912f131976bf45bdd20c0edc725d93 [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 */
12#include "ptp.h"
13#include <errno.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <getopt.h>
18#include <unistd.h>
19#include <sys/types.h>
20#include <utime.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <sys/mman.h>
24#include <usb.h>
25
Linus Walleij15e344f2006-03-06 15:15:00 +000026#include "libmtp.h"
27#include "libusb-glue.h"
Linus Walleij1fd2d272006-05-08 09:22:01 +000028#include "util.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000029
Linus Walleije78de6d2006-10-31 14:46:36 +000030/* To enable debug prints, switch on this */
31//#define ENABLE_USB_BULK_DEBUG
32
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000033/* OUR APPLICATION USB URB (2MB) ;) */
34#define PTPCAM_USB_URB 2097152
35
36/* this must not be too short - the original 4000 was not long
37 enough for big file transfers. I imagine the player spends a
38 bit of time gearing up to receiving lots of data. This also makes
39 connecting/disconnecting more reliable */
40#define USB_TIMEOUT 10000
41#define USB_CAPTURE_TIMEOUT 20000
42
43/* USB control message data phase direction */
44#ifndef USB_DP_HTD
45#define USB_DP_HTD (0x00 << 7) /* host to device */
46#endif
47#ifndef USB_DP_DTH
48#define USB_DP_DTH (0x01 << 7) /* device to host */
49#endif
50
51/* USB Feature selector HALT */
52#ifndef USB_FEATURE_HALT
53#define USB_FEATURE_HALT 0x00
54#endif
55
Linus Walleija5483642006-03-09 09:20:38 +000056/*
Linus Walleije8c54642006-03-28 09:45:00 +000057 * MTP device list, trying real bad to get all devices into
58 * this list by stealing from everyone I know.
Linus Walleija5483642006-03-09 09:20:38 +000059 */
Linus Walleij6fd2f082006-03-28 07:19:22 +000060static const LIBMTP_device_entry_t mtp_device_table[] = {
Linus Walleij3d2c7842006-08-18 09:35:08 +000061
62 /*
63 * Creative Technology
64 */
Linus Walleij0558ac52006-09-07 06:55:03 +000065 { "Creative Zen Vision", 0x041e, 0x411f, DEVICE_FLAG_NONE },
66 { "Creative Portable Media Center", 0x041e, 0x4123, DEVICE_FLAG_NONE },
67 { "Creative Zen Xtra (MTP mode)", 0x041e, 0x4128, DEVICE_FLAG_NONE },
68 { "Second generation Dell DJ", 0x041e, 0x412f, DEVICE_FLAG_NONE },
69 { "Creative Zen Micro (MTP mode)", 0x041e, 0x4130, DEVICE_FLAG_NONE },
70 { "Creative Zen Touch (MTP mode)", 0x041e, 0x4131, DEVICE_FLAG_NONE },
71 { "Dell Pocket DJ (MTP mode)", 0x041e, 0x4132, DEVICE_FLAG_NONE },
72 { "Creative Zen Sleek (MTP mode)", 0x041e, 0x4137, DEVICE_FLAG_NONE },
73 { "Creative Zen MicroPhoto", 0x041e, 0x413c, DEVICE_FLAG_NONE },
74 { "Creative Zen Sleek Photo", 0x041e, 0x413d, DEVICE_FLAG_NONE },
75 { "Creative Zen Vision:M", 0x041e, 0x413e, DEVICE_FLAG_NONE },
Linus Walleijbdd006b2006-09-15 04:54:13 +000076 // Reported by marazm@o2.pl
77 { "Creative Zen V", 0x041e, 0x4150, DEVICE_FLAG_NONE },
Linus Walleij92a84962006-11-11 20:04:37 +000078 // Reported by danielw@iinet.net.au
79 { "Creative Zen Vision:M (DVP-HD0004)", 0x041e, 0x4151, DEVICE_FLAG_NONE },
Linus Walleij550bc672006-10-10 11:11:29 +000080 // Reported by Darel on the XNJB forums
Richard Lowe1f06922006-11-12 16:38:39 +000081 { "Creative Zen V Plus", 0x041e, 0x4152, DEVICE_FLAG_NONE },
82 { "Creative Zen Vision W", 0x041e, 0x4153, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000083
84 /*
85 * Samsung
Linus Walleij0558ac52006-09-07 06:55:03 +000086 * We suspect that more of these are dual mode.
Linus Walleij3d2c7842006-08-18 09:35:08 +000087 */
88 // From libgphoto2
Linus Walleij0558ac52006-09-07 06:55:03 +000089 { "Samsung YH-820", 0x04e8, 0x502e, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000090 // Contributed by polux2001@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +000091 { "Samsung YH-925", 0x04e8, 0x502f, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000092 // Contributed by anonymous person on SourceForge
Linus Walleij0558ac52006-09-07 06:55:03 +000093 { "Samsung YP-T7J", 0x04e8, 0x5047, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000094 // Reported by cstrickler@gmail.com
Linus Walleij0558ac52006-09-07 06:55:03 +000095 { "Samsung YP-U2J (YP-U2JXB/XAA)", 0x04e8, 0x5054, DEVICE_FLAG_NONE },
Linus Walleij82ed6612006-08-31 08:06:19 +000096 // Reported by Andrew Benson
Linus Walleij0558ac52006-09-07 06:55:03 +000097 { "Samsung YP-F2J", 0x04e8, 0x5057, DEVICE_FLAG_DUALMODE },
Linus Walleij19be4212006-11-16 09:00:48 +000098 // Reported by Patrick <skibler@gmail.com>
99 { "Samsung YP-K5", 0x04e8, 0x505a, DEVICE_FLAG_DUALMODE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000100 // From a rouge .INF file
Linus Walleij0558ac52006-09-07 06:55:03 +0000101 { "Samsung YH-999 Portable Media Center", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000102
103 /*
104 * Intel
105 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000106 { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000107
108 /*
109 * JVC
110 */
111 // From Mark Veinot
Linus Walleij0558ac52006-09-07 06:55:03 +0000112 { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000113
114 /*
Linus Walleij093ca0a2006-11-02 21:15:00 +0000115 * Philips
Linus Walleij3d2c7842006-08-18 09:35:08 +0000116 */
117 // From libgphoto2 source
Linus Walleij093ca0a2006-11-02 21:15:00 +0000118 { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE },
Linus Walleijbf7fc272006-11-10 21:35:06 +0000119 { "Philips HDD6320/00", 0x0471, 0x014b, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000120 // Anonymous SourceForge user
Linus Walleij093ca0a2006-11-02 21:15:00 +0000121 { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE },
122 // From Gerhard Mekenkamp
123 { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000124
125 /*
126 * SanDisk
127 */
128 // Reported by Brian Robison
Linus Walleij0558ac52006-09-07 06:55:03 +0000129 { "SanDisk Sansa m240", 0x0781, 0x7400, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000130 // Reported by tangent_@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +0000131 { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000132 // From libgphoto2 source
Linus Walleij0558ac52006-09-07 06:55:03 +0000133 { "SanDisk Sansa e200", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000134 // Reported by gonkflea@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +0000135 { "SanDisk Sansa e260", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000136
137 /*
138 * iRiver
139 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000140 { "iRiver Portable Media Center", 0x1006, 0x4002, DEVICE_FLAG_NONE },
141 { "iRiver Portable Media Center", 0x1006, 0x4003, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000142 // From libgphoto2 source
Linus Walleij0558ac52006-09-07 06:55:03 +0000143 { "iRiver T10", 0x4102, 0x1113, DEVICE_FLAG_NONE },
144 { "iRiver T20 FM", 0x4102, 0x1114, DEVICE_FLAG_NONE },
145 { "iRiver U10", 0x4102, 0x1116, DEVICE_FLAG_NONE },
146 { "iRiver T10", 0x4102, 0x1117, DEVICE_FLAG_NONE },
147 { "iRiver T20", 0x4102, 0x1118, DEVICE_FLAG_NONE },
148 { "iRiver T30", 0x4102, 0x1119, DEVICE_FLAG_NONE },
Linus Walleij77e047c2006-09-11 19:09:06 +0000149 // Reported by David Wolpoff
150 { "iRiver T10 2GB", 0x4102, 0x1120, DEVICE_FLAG_NONE },
Linus Walleij04377da2006-08-30 13:03:35 +0000151 // Reported by Adam Torgerson
Linus Walleij0558ac52006-09-07 06:55:03 +0000152 { "iRiver Clix", 0x4102, 0x112a, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000153 // Reported by Scott Call
Linus Walleij0558ac52006-09-07 06:55:03 +0000154 { "iRiver H10 20GB", 0x4102, 0x2101, DEVICE_FLAG_NONE },
155 { "iRiver H10", 0x4102, 0x2102, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000156
157 /*
158 * Dell
159 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000160 { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000161
162 /*
163 * Toshiba
164 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000165 { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE },
166 { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE },
Linus Walleij405e2392006-10-23 07:13:49 +0000167 // From libgphoto2
168 { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE },
Linus Walleij402e4bd2006-09-12 07:50:56 +0000169 // Reported by Rob Brown
170 { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE },
Linus Walleij6f680562006-08-19 22:49:33 +0000171
172 /*
173 * Archos
174 */
175 // Reported by gudul1@users.sourceforge.net
Linus Walleije5430902006-10-08 14:46:03 +0000176 { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE },
177
178 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000179 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000180 * This unit is falsely detected as USB mass storage in Linux
181 * so special care is needed to blacklist the device in kernel
182 * or similar to get it properly userspace:d.
Linus Walleije5430902006-10-08 14:46:03 +0000183 */
Linus Walleij501ba4d2006-10-16 06:17:04 +0000184 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER}
Linus Walleije5430902006-10-08 14:46:03 +0000185
Linus Walleija5483642006-03-09 09:20:38 +0000186};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000187static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000188
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000189int ptpcam_usb_timeout = USB_TIMEOUT;
190
Linus Walleijd6a49972006-05-02 08:24:54 +0000191// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000192static struct usb_bus* init_usb();
193static struct usb_device *probe_usb_bus_for_mtp_devices(void);
Linus Walleijd6a49972006-05-02 08:24:54 +0000194static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);
195static struct usb_device* find_device (int busn, int devicen, short force);
196static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);
197static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000198static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000199static short ptp_write_func (unsigned char *bytes, unsigned int size, void *data);
Linus Walleijb02a0662006-04-25 08:05:09 +0000200static short ptp_read_func (unsigned char *bytes, unsigned int size, void *data, unsigned int *readbytes);
201static short ptp_check_int (unsigned char *bytes, unsigned int size, void *data, unsigned int *rlen);
Linus Walleijd6a49972006-05-02 08:24:54 +0000202static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
203static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000204
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000205
Linus Walleij6fd2f082006-03-28 07:19:22 +0000206int get_device_list(LIBMTP_device_entry_t ** const devices, int * const numdevs)
207{
208 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
209 *numdevs = mtp_device_table_size;
210 return 0;
211}
212
Linus Walleij1fd2d272006-05-08 09:22:01 +0000213static struct usb_bus* init_usb()
214{
215 usb_init();
216 usb_find_busses();
217 usb_find_devices();
218 return (usb_get_busses());
219}
220
221/**
222 * Check for the Microsoft OS device descriptor and returns device struct
223 * if the device is MTP-compliant. The function will only recognize
224 * a single device connected to the USB bus.
225 *
226 * @return an MTP-compliant USB device if one was found, else NULL.
227 */
228static struct usb_device *probe_usb_bus_for_mtp_devices(void)
229{
230 struct usb_bus *bus;
231
232 bus = init_usb();
233 for (; bus; bus = bus->next) {
234 struct usb_device *dev;
235
236 for (dev = bus->devices; dev; dev = dev->next) {
237 usb_dev_handle *devh;
238 unsigned char buf[1024], cmd;
239 int ret;
240
241 devh = usb_open(dev);
242 if (devh == NULL) {
243 continue;
244 }
245
246 // Read the special descripor, if possible...
247 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
248
249 if (ret < 10) {
250 // printf("Device: VID %04x/PID %04x: no extended device property...\n",
251 // dev->descriptor.idVendor,
252 // dev->descriptor.idProduct);
253 usb_close(devh);
254 continue;
255 }
Linus Walleijd5d51c82006-09-11 06:57:50 +0000256
257 // It is atleast 10 bytes...
Linus Walleij1fd2d272006-05-08 09:22:01 +0000258 if (!((buf[2] == 'M') && (buf[4]=='S') && (buf[6]=='F') && (buf[8]=='T'))) {
Linus Walleijd5d51c82006-09-11 06:57:50 +0000259 printf("This is not a Microsoft MTP descriptor...\n");
260 printf("Device response to read device property 0xee:\n");
261 data_dump_ascii (stdout, buf, ret, 0);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000262 usb_close(devh);
263 continue;
264 }
265
266 cmd = buf[16];
267 ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
268 cmd, 0, 4, (char *) buf, sizeof(buf), 1000);
269 if (ret == -1) {
270 //printf("Decice could not respond to control message 1.\n");
271 usb_close(devh);
Linus Walleijd5d51c82006-09-11 06:57:50 +0000272 continue;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000273 }
274
Linus Walleijd5d51c82006-09-11 06:57:50 +0000275 if (ret > 0x15) {
276 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
277 printf("The device has a Microsoft device descriptor, but it's not MTP.\n");
278 printf("This is not an MTP device. Presumable it is USB mass storage\n");
279 printf("with some additional Janus (DRM) support.\n");
280 printf("Device response to control message 1:\n");
281 data_dump_ascii (stdout, buf, ret, 0);
282 continue;
283 }
284 } else {
285 // Not MTP or broken
286 continue;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000287 }
288
289 ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
290 cmd, 0, 5, (char *) buf, sizeof(buf), 1000);
291 if (ret == -1) {
292 //printf("Device could not respond to control message 2.\n");
293 usb_close(devh);
Linus Walleijd5d51c82006-09-11 06:57:50 +0000294 // Return the device anyway, it said previously it was MTP, right?
Linus Walleij1fd2d272006-05-08 09:22:01 +0000295 return dev;
296 }
297
Linus Walleijd5d51c82006-09-11 06:57:50 +0000298 if (ret > 0x15) {
299 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
300 printf("This device does not respond with MTP characteristics on\n");
301 printf("the second device property read (0x05), but will be regarded\n");
302 printf("as MTP anyway.\n");
303 printf("Device response to control message 2:\n");
304 data_dump_ascii (stdout, buf, ret, 0);
305 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000306 }
Linus Walleijd5d51c82006-09-11 06:57:50 +0000307
Linus Walleij1fd2d272006-05-08 09:22:01 +0000308 usb_close(devh);
309 // We can return the device here, it will be the first.
310 // If it was not MTP, the loop continues before it reaches this point.
311 return dev;
312 }
313 }
314 // If nothing was found we end up here.
315 return NULL;
316}
317
318/**
319 * Detect the MTP device descriptor and return the VID and PID
320 * of the first device found. This is a very low-level function
321 * which is intended for use with <b>udev</b> or other hotplug
322 * mechanisms. The idea is that a script may want to know if the
323 * just plugged-in device was an MTP device or not.
324 * @param vid the Vendor ID (VID) of the first device found.
325 * @param pid the Product ID (PID) of the first device found.
326 * @return the number of detected devices or -1 if the call
327 * was unsuccessful.
328 */
329int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
330{
331 struct usb_device *dev = probe_usb_bus_for_mtp_devices();
332 if (dev == NULL) {
333 return 0;
334 }
335 *vid = dev->descriptor.idVendor;
336 *pid = dev->descriptor.idProduct;
337 return 1;
338}
339
Linus Walleijc6210fb2006-05-08 11:11:41 +0000340/**
341 * This routine just dumps out low-level
342 * USB information about the current device.
343 * @param ptp_usb the USB device to get information from.
344 */
345void dump_usbinfo(PTP_USB *ptp_usb)
346{
347 int res;
348 struct usb_device *dev;
349
350#ifdef LIBUSB_HAS_GET_DRIVER_NP
351 char devname[0x10];
352
353 devname[0] = '\0';
354 res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));
355 if (devname[0] != '\0') {
356 printf(" Using kernel interface \"%s\"\n", devname);
357 }
358#endif
359 dev = usb_device(ptp_usb->handle);
360 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
361 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
362 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
363 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000364 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
365 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000366 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
367 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000368 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000369 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
370}
371
372
Linus Walleijd6a49972006-05-02 08:24:54 +0000373// Based on same function on library.c in libgphoto2
Richard Low43fdb882006-09-06 16:19:05 +0000374#define CONTEXT_BLOCK_SIZE 0x100000
Linus Walleijb02a0662006-04-25 08:05:09 +0000375static short
376ptp_read_func (unsigned char *bytes, unsigned int size, void *data, unsigned int *readbytes)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000377{
Linus Walleijb02a0662006-04-25 08:05:09 +0000378 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleijd6a49972006-05-02 08:24:54 +0000379 int toread = 0;
380 int result = 0;
381 int curread = 0;
382 /* Split into small blocks. Too large blocks (>1x MB) would
383 * timeout.
384 */
385 while (curread < size) {
386 toread = size - curread;
Richard Low43fdb882006-09-06 16:19:05 +0000387 if (toread > CONTEXT_BLOCK_SIZE)
388 toread = CONTEXT_BLOCK_SIZE;
Linus Walleijd6a49972006-05-02 08:24:54 +0000389
390 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep,(char *)(bytes+curread), toread, ptpcam_usb_timeout);
391 if (result == 0) {
392 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep,(char *)(bytes+curread), toread, ptpcam_usb_timeout);
393 }
394 if (result < 0)
Richard Low43fdb882006-09-06 16:19:05 +0000395 return PTP_ERROR_IO;
Linus Walleije78de6d2006-10-31 14:46:36 +0000396#ifdef ENABLE_USB_BULK_DEBUG
397 printf("<==USB IN\n");
Linus Walleij42907f22006-11-07 16:00:39 +0000398 data_dump_ascii (stdout,(bytes+curread),result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000399#endif
Linus Walleijd6a49972006-05-02 08:24:54 +0000400 curread += result;
401 if (result < toread) /* short reads are common */
402 break;
403 }
Linus Walleijee73ef22006-08-27 19:56:00 +0000404
405 // Increase counters, call callback
406 if (ptp_usb->callback_active) {
407 ptp_usb->current_transfer_complete += curread;
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000408 if (ptp_usb->current_transfer_complete > ptp_usb->current_transfer_total) {
409 // Fishy... but some commands have unpredictable lengths.
410 // send last update and disable callback.
411 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
412 ptp_usb->callback_active = 0;
413 }
Linus Walleijee73ef22006-08-27 19:56:00 +0000414 if (ptp_usb->current_transfer_callback != NULL) {
415 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
416 ptp_usb->current_transfer_total,
417 ptp_usb->current_transfer_callback_data);
418 }
419 }
420
Linus Walleijd6a49972006-05-02 08:24:54 +0000421 if (result > 0) {
422 *readbytes = curread;
423 return (PTP_RC_OK);
424 } else {
425 return PTP_ERROR_IO;
426 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000427}
428
Linus Walleijd6a49972006-05-02 08:24:54 +0000429// Based on same function on library.c in libgphoto2
Linus Walleijb02a0662006-04-25 08:05:09 +0000430static short
431ptp_write_func (unsigned char *bytes, unsigned int size, void *data)
432{
433 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleijd6a49972006-05-02 08:24:54 +0000434 int towrite = 0;
435 int result = 0;
436 int curwrite = 0;
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000437
Linus Walleijd6a49972006-05-02 08:24:54 +0000438 /*
439 * gp_port_write returns (in case of success) the number of bytes
Linus Walleijd214b9b2006-08-26 22:08:37 +0000440 * written. Too large blocks (>5x MB) could timeout.
Linus Walleijd6a49972006-05-02 08:24:54 +0000441 */
442 while (curwrite < size) {
443 towrite = size-curwrite;
Richard Low43fdb882006-09-06 16:19:05 +0000444 if (towrite > CONTEXT_BLOCK_SIZE)
445 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijd6a49972006-05-02 08:24:54 +0000446 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)(bytes+curwrite),towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000447#ifdef ENABLE_USB_BULK_DEBUG
448 printf("USB OUT==>\n");
449 data_dump_ascii (stdout,(bytes+curwrite),towrite,16);
450#endif
Linus Walleijd6a49972006-05-02 08:24:54 +0000451 if (result < 0)
Richard Low43fdb882006-09-06 16:19:05 +0000452 return PTP_ERROR_IO;
Linus Walleijd6a49972006-05-02 08:24:54 +0000453 curwrite += result;
454 if (result < towrite) /* short writes happen */
455 break;
456 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000457
458 // Increase counters, call callback
459 if (ptp_usb->callback_active) {
Linus Walleij4af6fbe2006-08-27 09:33:52 +0000460 ptp_usb->current_transfer_complete += curwrite;
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000461 if (ptp_usb->current_transfer_complete > ptp_usb->current_transfer_total) {
462 // Fishy... but some commands have unpredictable lengths.
463 // send last update and disable callback.
464 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
465 ptp_usb->callback_active = 0;
466 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000467 if (ptp_usb->current_transfer_callback != NULL) {
468 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
469 ptp_usb->current_transfer_total,
470 ptp_usb->current_transfer_callback_data);
471 }
Linus Walleij80d134a2006-08-22 21:41:37 +0000472 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000473
474 // If this is the last transfer (callbacks only active if this function called repeatedly with
475 // new data, otherwise this is a single large transaction which ends here).
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000476 // a request and the bulk send header.
477 if (!ptp_usb->callback_active) {
Linus Walleijd214b9b2006-08-26 22:08:37 +0000478 // Then terminate an even packet boundary write with a zero length packet
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000479 if ((size % ptp_usb->outep_maxpacket) == 0) {
Linus Walleijd214b9b2006-08-26 22:08:37 +0000480 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
481 }
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000482 } else if (ptp_usb->current_transfer_complete == ptp_usb->current_transfer_total) {
483 // This is the amount actually transfered in this large transaction.
484 uint64_t actual_xfer_size = ptp_usb->current_transfer_total - 2*PTP_USB_BULK_HDR_LEN;
485
486 if ((actual_xfer_size % ptp_usb->outep_maxpacket) == 0) {
487 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000488 }
489 // Set as complete and disable callback, just as good.
490 // This also blocks callbacks from the following response command.
491 if (ptp_usb->current_transfer_callback != NULL) {
492 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_total, // Both total = 100%
493 ptp_usb->current_transfer_total, // Both total = 100%
494 ptp_usb->current_transfer_callback_data);
495 }
496 ptp_usb->callback_active = 0;
Linus Walleijd214b9b2006-08-26 22:08:37 +0000497 }
498
Linus Walleijd6a49972006-05-02 08:24:54 +0000499 if (result < 0)
500 return PTP_ERROR_IO;
501 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000502}
503
Linus Walleijd6a49972006-05-02 08:24:54 +0000504// This is a bit hackish at the moment. I wonder if it even works.
Linus Walleijb02a0662006-04-25 08:05:09 +0000505static short ptp_check_int (unsigned char *bytes, unsigned int size, void *data, unsigned int *rlen)
506{
Linus Walleijd6a49972006-05-02 08:24:54 +0000507 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleijb02a0662006-04-25 08:05:09 +0000508 int result;
Linus Walleijb02a0662006-04-25 08:05:09 +0000509
510 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)bytes,size,ptpcam_usb_timeout);
511 if (result==0)
512 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) bytes, size, ptpcam_usb_timeout);
513 if (result >= 0) {
514 *rlen = result;
515 return (PTP_RC_OK);
516 } else {
517 return PTP_ERROR_IO;
518 }
519}
520
Linus Walleij9eb3d312006-08-04 19:25:59 +0000521static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000522{
Linus Walleijd6a49972006-05-02 08:24:54 +0000523 usb_dev_handle *device_handle;
524
525 params->write_func=ptp_write_func;
526 params->read_func=ptp_read_func;
527 params->check_int_func=ptp_check_int;
528 params->check_int_fast_func=ptp_check_int;
529 params->error_func=NULL;
530 params->debug_func=NULL;
531 params->sendreq_func=ptp_usb_sendreq;
532 params->senddata_func=ptp_usb_senddata;
533 params->getresp_func=ptp_usb_getresp;
534 params->getdata_func=ptp_usb_getdata;
535 params->data=ptp_usb;
536 params->transaction_id=0;
537 params->byteorder = PTP_DL_LE;
538
Linus Walleij0ed2a9f2006-05-04 05:31:34 +0000539 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +0000540 if (!device_handle) {
541 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +0000542 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +0000543 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +0000544 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +0000545#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
546 /*
547 * If this device is known to be wrongfully claimed by other kernel
548 * drivers (such as mass storage), then try to unload it to make it
549 * accessible from user space.
550 */
551 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
552 if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
553 perror("usb_detach_kernel_driver_np()");
554 }
555 }
556#endif
Linus Walleijef5cca92006-05-05 07:19:08 +0000557 if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +0000558 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +0000559 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +0000560 }
Linus Walleijc6210fb2006-05-08 11:11:41 +0000561 ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber;
Linus Walleijd6a49972006-05-02 08:24:54 +0000562 }
Linus Walleij9eb3d312006-08-04 19:25:59 +0000563 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000564}
565
Linus Walleijd6a49972006-05-02 08:24:54 +0000566static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000567{
Linus Walleijd6a49972006-05-02 08:24:54 +0000568 uint16_t status=0;
569 int ret;
570
571 /* check the inep status */
Linus Walleij1fd2d272006-05-08 09:22:01 +0000572 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleijd6a49972006-05-02 08:24:54 +0000573 if (ret<0) perror ("inep: usb_get_endpoint_status()");
574 /* and clear the HALT condition if happend */
575 else if (status) {
576 printf("Resetting input pipe!\n");
577 ret=usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
578 /*usb_clear_halt(ptp_usb->handle,ptp_usb->inep); */
579 if (ret<0)perror ("usb_clear_stall_feature()");
580 }
581 status=0;
582
583 /* check the outep status */
584 ret=usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
585 if (ret<0) perror ("outep: usb_get_endpoint_status()");
586 /* and clear the HALT condition if happend */
587 else if (status) {
588 printf("Resetting output pipe!\n");
589 ret=usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
590 /*usb_clear_halt(ptp_usb->handle,ptp_usb->outep); */
591 if (ret<0)perror ("usb_clear_stall_feature()");
592 }
593
594 /*usb_clear_halt(ptp_usb->handle,ptp_usb->intep); */
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000595}
596
Linus Walleijd6a49972006-05-02 08:24:54 +0000597static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000598{
Linus Walleijd6a49972006-05-02 08:24:54 +0000599 clear_stall(ptp_usb);
600 // Added to clear some stuff on the OUT endpoint
601 // TODO: is this good on the Mac too?
602 usb_resetep(ptp_usb->handle, ptp_usb->outep);
603 usb_release_interface(ptp_usb->handle, interfaceNumber);
604 // Brutally reset device
605 // TODO: is this good on the Mac too?
606 usb_reset(ptp_usb->handle);
607 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000608}
609
610
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000611/*
612 find_device() returns the pointer to a usb_device structure matching
613 given busn, devicen numbers. If any or both of arguments are 0 then the
614 first matching PTP device structure is returned.
615 */
Linus Walleijd6a49972006-05-02 08:24:54 +0000616static struct usb_device* find_device (int busn, int devn, short force)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000617{
Linus Walleijd6a49972006-05-02 08:24:54 +0000618 struct usb_bus *bus;
619 struct usb_device *dev;
620
621 bus=init_usb();
622 for (; bus; bus = bus->next)
623 for (dev = bus->devices; dev; dev = dev->next)
624 /* somtimes dev->config is null, not sure why... */
625 if (dev->config != NULL)
626 if (dev->descriptor.bDeviceClass!=USB_CLASS_HUB)
627 {
628 int curbusn, curdevn;
629
630 curbusn=strtol(bus->dirname,NULL,10);
631 curdevn=strtol(dev->filename,NULL,10);
632
633 if (devn==0) {
634 if (busn==0) return dev;
635 if (curbusn==busn) return dev;
636 } else {
637 if ((busn==0)&&(curdevn==devn)) return dev;
638 if ((curbusn==busn)&&(curdevn==devn)) return dev;
639 }
640 }
641 return NULL;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000642}
643
Linus Walleij6946ac52006-03-21 06:51:22 +0000644/**
645 * This function scans through the device list to see if there are
646 * some devices to connect to. The table at the top of this file is
647 * used to identify potential devices.
648 */
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000649uint16_t connect_first_device(PTPParams *params, PTP_USB *ptp_usb, uint8_t *interfaceNumber)
650{
Linus Walleij6946ac52006-03-21 06:51:22 +0000651 struct usb_bus *bus;
652 struct usb_device *dev;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000653 struct usb_endpoint_descriptor *ep;
654 PTPDeviceInfo deviceinfo;
655 uint16_t ret=0;
656 int n;
Linus Walleij6946ac52006-03-21 06:51:22 +0000657
Linus Walleij0558ac52006-09-07 06:55:03 +0000658 // Reset device flags
659 ptp_usb->device_flags = DEVICE_FLAG_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000660 // First try to locate the device using the extended
661 // device descriptor.
662 dev = probe_usb_bus_for_mtp_devices();
663
664 if (dev != NULL) {
665 int i;
666
667 // See if we can find the name of this beast
668 for (i = 0; i < mtp_device_table_size; i++) {
669 LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i];
670 if (dev->descriptor.idVendor == mtp_device->vendor_id &&
671 dev->descriptor.idProduct == mtp_device->product_id ) {
672 printf("Autodetected device \"%s\" (VID=%04x,PID=%04x) is known.\n",
673 mtp_device->name, dev->descriptor.idVendor, dev->descriptor.idProduct);
Linus Walleij0558ac52006-09-07 06:55:03 +0000674 ptp_usb->device_flags = mtp_device->device_flags;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000675 break;
676 }
677 }
678 if (i == mtp_device_table_size) {
679 printf("Autodetected device with VID=%04x and PID=%04x is UNKNOWN.\n",
680 dev->descriptor.idVendor, dev->descriptor.idProduct);
681 printf("Please report this VID/PID and the device model name etc to the\n");
682 printf("libmtp development team!\n");
683 }
684 }
685
686 // If autodetection fails, scan the bus for well known devices.
687 if (dev == NULL) {
688 bus = init_usb();
689 for (; bus; bus = bus->next) {
690 for (dev = bus->devices; dev; dev = dev->next) {
691 int i;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000692
Linus Walleij1fd2d272006-05-08 09:22:01 +0000693 // Loop over the list of supported devices
694 for (i = 0; i < mtp_device_table_size; i++) {
695 LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i];
Linus Walleij6946ac52006-03-21 06:51:22 +0000696
Linus Walleij1fd2d272006-05-08 09:22:01 +0000697 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB &&
698 dev->descriptor.idVendor == mtp_device->vendor_id &&
699 dev->descriptor.idProduct == mtp_device->product_id ) {
700
701 printf("Found non-autodetected device \"%s\" on USB bus...\n", mtp_device->name);
Linus Walleij0558ac52006-09-07 06:55:03 +0000702 ptp_usb->device_flags = mtp_device->device_flags;
Linus Walleijd3fdd972006-05-30 15:51:37 +0000703 goto next_step;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000704
Linus Walleijb02a0662006-04-25 08:05:09 +0000705 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000706 }
Linus Walleij6946ac52006-03-21 06:51:22 +0000707 }
708 }
709 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000710
Linus Walleijd3fdd972006-05-30 15:51:37 +0000711next_step:
Linus Walleij1fd2d272006-05-08 09:22:01 +0000712 // Still not found any?
713 if (dev == NULL) {
714 return PTP_CD_RC_NO_DEVICES;
715 }
716
Linus Walleij0558ac52006-09-07 06:55:03 +0000717 // Found a device, then assign endpoints to ptp_usb...
Linus Walleij1fd2d272006-05-08 09:22:01 +0000718 ep = dev->config->interface->altsetting->endpoint;
719 n = dev->config->interface->altsetting->bNumEndpoints;
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000720 find_endpoints(dev, &ptp_usb->inep, &ptp_usb->inep_maxpacket,
721 &ptp_usb->outep, &ptp_usb->outep_maxpacket, &ptp_usb->intep);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000722
723 // printf("Init PTP USB...\n");
Linus Walleij9eb3d312006-08-04 19:25:59 +0000724 if (init_ptp_usb(params, ptp_usb, dev) < 0) {
725 return PTP_CD_RC_ERROR_CONNECTING;
726 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000727
728 ret = ptp_opensession(params,1);
Linus Walleij48a2d462006-11-19 14:07:59 +0000729
730 // This works in situations where previous bad applications have not used LIBMTP_Release_Device on exit
731 if (ret == PTP_ERROR_IO) {
732 printf("%s\n","PTP ERROR IO: Trying again after resetting USB");
733 // printf("%s\n","Closing USB interface...");
734 close_usb(ptp_usb,dev->config->interface->altsetting->bInterfaceNumber);
735 // printf("%s\n","Init PTP USB...");
736 if (init_ptp_usb(params, ptp_usb, dev) < 0) {
737 return PTP_CD_RC_ERROR_CONNECTING;
738 }
739
740 ret = ptp_opensession(params,1);
741 }
742
Linus Walleij1fd2d272006-05-08 09:22:01 +0000743 // printf("Session open (%d)...\n", ret);
744 if (ret == PTP_RC_InvalidTransactionID) {
745 params->transaction_id += 10;
746 ret = ptp_opensession(params,1);
747 }
748 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
749 printf("Could not open session! (Return code %d)\n Try to reset the device.\n", ret);
750 usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber);
751 }
752
753 // It is actually permissible to call this before opening the session
754 ret = ptp_getdeviceinfo(params, &deviceinfo);
755 if (ret != PTP_RC_OK) {
756 printf("Could not get device info!\n");
757 usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber);
758 return PTP_CD_RC_ERROR_CONNECTING;
759 }
760
761 // we're connected, return OK
762 *interfaceNumber = dev->config->interface->altsetting->bInterfaceNumber;
763 return PTP_CD_RC_CONNECTED;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000764}
765
Linus Walleijd6a49972006-05-02 08:24:54 +0000766static 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 +0000767{
Linus Walleijd6a49972006-05-02 08:24:54 +0000768 int i,n;
769 struct usb_endpoint_descriptor *ep;
770
771 ep = dev->config->interface->altsetting->endpoint;
772 n=dev->config->interface->altsetting->bNumEndpoints;
773
774 for (i=0;i<n;i++) {
775 if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
776 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
777 USB_ENDPOINT_DIR_MASK)
778 {
779 *inep=ep[i].bEndpointAddress;
780 *inep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000781 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000782 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
783 {
784 *outep=ep[i].bEndpointAddress;
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000785 *outep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijd6a49972006-05-02 08:24:54 +0000786 }
787 } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
788 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
789 USB_ENDPOINT_DIR_MASK)
790 {
791 *intep=ep[i].bEndpointAddress;
792 }
793 }
794 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000795}
796
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000797int open_device (int busn, int devn, short force, PTP_USB *ptp_usb, PTPParams *params, struct usb_device **dev)
798{
799#ifdef DEBUG
Linus Walleijd6a49972006-05-02 08:24:54 +0000800 printf("dev %i\tbus %i\n",devn,busn);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000801#endif
Linus Walleijd6a49972006-05-02 08:24:54 +0000802
803 *dev=find_device(busn,devn,force);
804 if (*dev==NULL) {
805 fprintf(stderr,"could not find any device matching given "
806 "bus/dev numbers\n");
807 exit(-1);
808 }
809 find_endpoints(*dev,&ptp_usb->inep,&ptp_usb->inep_maxpacket,&ptp_usb->outep,&ptp_usb->outep_maxpacket,&ptp_usb->intep);
810
Linus Walleij9eb3d312006-08-04 19:25:59 +0000811 if (init_ptp_usb(params, ptp_usb, *dev) < 0) {
812 return -1;
813 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000814 if (ptp_opensession(params,1)!=PTP_RC_OK) {
815 fprintf(stderr,"ERROR: Could not open session!\n");
816 close_usb(ptp_usb, (*dev)->config->interface->altsetting->bInterfaceNumber);
817 return -1;
818 }
819 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000820}
821
822void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber)
823{
Linus Walleijd6a49972006-05-02 08:24:54 +0000824 if (ptp_closesession(params)!=PTP_RC_OK)
825 fprintf(stderr,"ERROR: Could not close session!\n");
826 close_usb(ptp_usb, interfaceNumber);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000827}
828
Linus Walleijd6a49972006-05-02 08:24:54 +0000829static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000830{
Linus Walleijd6a49972006-05-02 08:24:54 +0000831
832 return (usb_control_msg(ptp_usb->handle,
833 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
834 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000835}
836
Linus Walleijd6a49972006-05-02 08:24:54 +0000837static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000838{
Linus Walleijd6a49972006-05-02 08:24:54 +0000839 return (usb_control_msg(ptp_usb->handle,
840 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
841 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000842}