blob: 23937b63df6de977ea727e65c4b629182f5a7683 [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 Walleij550bc672006-10-10 11:11:29 +000078 // Reported by Darel on the XNJB forums
79 { "Creative Zen V plus", 0x041e, 0x4152, 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 Walleij3d2c7842006-08-18 09:35:08 +000095 // From a rouge .INF file
Linus Walleij0558ac52006-09-07 06:55:03 +000096 { "Samsung YH-999 Portable Media Center", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +000097
98 /*
99 * Intel
100 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000101 { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000102
103 /*
104 * JVC
105 */
106 // From Mark Veinot
Linus Walleij0558ac52006-09-07 06:55:03 +0000107 { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000108
109 /*
Linus Walleij093ca0a2006-11-02 21:15:00 +0000110 * Philips
Linus Walleij3d2c7842006-08-18 09:35:08 +0000111 */
112 // From libgphoto2 source
Linus Walleij093ca0a2006-11-02 21:15:00 +0000113 { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE },
114 { "Philips HDD6320 2", 0x0471, 0x014b, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000115 // Anonymous SourceForge user
Linus Walleij093ca0a2006-11-02 21:15:00 +0000116 { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE },
117 // From Gerhard Mekenkamp
118 { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000119
120 /*
121 * SanDisk
122 */
123 // Reported by Brian Robison
Linus Walleij0558ac52006-09-07 06:55:03 +0000124 { "SanDisk Sansa m240", 0x0781, 0x7400, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000125 // Reported by tangent_@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +0000126 { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000127 // From libgphoto2 source
Linus Walleij0558ac52006-09-07 06:55:03 +0000128 { "SanDisk Sansa e200", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000129 // Reported by gonkflea@users.sourceforge.net
Linus Walleij0558ac52006-09-07 06:55:03 +0000130 { "SanDisk Sansa e260", 0x0781, 0x7420, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000131
132 /*
133 * iRiver
134 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000135 { "iRiver Portable Media Center", 0x1006, 0x4002, DEVICE_FLAG_NONE },
136 { "iRiver Portable Media Center", 0x1006, 0x4003, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000137 // From libgphoto2 source
Linus Walleij0558ac52006-09-07 06:55:03 +0000138 { "iRiver T10", 0x4102, 0x1113, DEVICE_FLAG_NONE },
139 { "iRiver T20 FM", 0x4102, 0x1114, DEVICE_FLAG_NONE },
140 { "iRiver U10", 0x4102, 0x1116, DEVICE_FLAG_NONE },
141 { "iRiver T10", 0x4102, 0x1117, DEVICE_FLAG_NONE },
142 { "iRiver T20", 0x4102, 0x1118, DEVICE_FLAG_NONE },
143 { "iRiver T30", 0x4102, 0x1119, DEVICE_FLAG_NONE },
Linus Walleij77e047c2006-09-11 19:09:06 +0000144 // Reported by David Wolpoff
145 { "iRiver T10 2GB", 0x4102, 0x1120, DEVICE_FLAG_NONE },
Linus Walleij04377da2006-08-30 13:03:35 +0000146 // Reported by Adam Torgerson
Linus Walleij0558ac52006-09-07 06:55:03 +0000147 { "iRiver Clix", 0x4102, 0x112a, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000148 // Reported by Scott Call
Linus Walleij0558ac52006-09-07 06:55:03 +0000149 { "iRiver H10 20GB", 0x4102, 0x2101, DEVICE_FLAG_NONE },
150 { "iRiver H10", 0x4102, 0x2102, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000151
152 /*
153 * Dell
154 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000155 { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE },
Linus Walleij3d2c7842006-08-18 09:35:08 +0000156
157 /*
158 * Toshiba
159 */
Linus Walleij0558ac52006-09-07 06:55:03 +0000160 { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE },
161 { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE },
Linus Walleij405e2392006-10-23 07:13:49 +0000162 // From libgphoto2
163 { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE },
Linus Walleij402e4bd2006-09-12 07:50:56 +0000164 // Reported by Rob Brown
165 { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE },
Linus Walleij6f680562006-08-19 22:49:33 +0000166
167 /*
168 * Archos
169 */
170 // Reported by gudul1@users.sourceforge.net
Linus Walleije5430902006-10-08 14:46:03 +0000171 { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE },
172
173 /*
Linus Walleij2a9d2f02006-10-15 20:44:27 +0000174 * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad
Linus Walleij550bc672006-10-10 11:11:29 +0000175 * This unit is falsely detected as USB mass storage in Linux
176 * so special care is needed to blacklist the device in kernel
177 * or similar to get it properly userspace:d.
Linus Walleije5430902006-10-08 14:46:03 +0000178 */
Linus Walleij501ba4d2006-10-16 06:17:04 +0000179 { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER}
Linus Walleije5430902006-10-08 14:46:03 +0000180
Linus Walleija5483642006-03-09 09:20:38 +0000181};
Linus Walleij6fd2f082006-03-28 07:19:22 +0000182static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +0000183
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000184int ptpcam_usb_timeout = USB_TIMEOUT;
185
Linus Walleijd6a49972006-05-02 08:24:54 +0000186// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000187static struct usb_bus* init_usb();
188static struct usb_device *probe_usb_bus_for_mtp_devices(void);
Linus Walleijd6a49972006-05-02 08:24:54 +0000189static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber);
190static struct usb_device* find_device (int busn, int devicen, short force);
191static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep);
192static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000193static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000194static short ptp_write_func (unsigned char *bytes, unsigned int size, void *data);
Linus Walleijb02a0662006-04-25 08:05:09 +0000195static short ptp_read_func (unsigned char *bytes, unsigned int size, void *data, unsigned int *readbytes);
196static short ptp_check_int (unsigned char *bytes, unsigned int size, void *data, unsigned int *rlen);
Linus Walleijd6a49972006-05-02 08:24:54 +0000197static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
198static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000199
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000200
Linus Walleij6fd2f082006-03-28 07:19:22 +0000201int get_device_list(LIBMTP_device_entry_t ** const devices, int * const numdevs)
202{
203 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
204 *numdevs = mtp_device_table_size;
205 return 0;
206}
207
Linus Walleij1fd2d272006-05-08 09:22:01 +0000208static struct usb_bus* init_usb()
209{
210 usb_init();
211 usb_find_busses();
212 usb_find_devices();
213 return (usb_get_busses());
214}
215
216/**
217 * Check for the Microsoft OS device descriptor and returns device struct
218 * if the device is MTP-compliant. The function will only recognize
219 * a single device connected to the USB bus.
220 *
221 * @return an MTP-compliant USB device if one was found, else NULL.
222 */
223static struct usb_device *probe_usb_bus_for_mtp_devices(void)
224{
225 struct usb_bus *bus;
226
227 bus = init_usb();
228 for (; bus; bus = bus->next) {
229 struct usb_device *dev;
230
231 for (dev = bus->devices; dev; dev = dev->next) {
232 usb_dev_handle *devh;
233 unsigned char buf[1024], cmd;
234 int ret;
235
236 devh = usb_open(dev);
237 if (devh == NULL) {
238 continue;
239 }
240
241 // Read the special descripor, if possible...
242 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
243
244 if (ret < 10) {
245 // printf("Device: VID %04x/PID %04x: no extended device property...\n",
246 // dev->descriptor.idVendor,
247 // dev->descriptor.idProduct);
248 usb_close(devh);
249 continue;
250 }
Linus Walleijd5d51c82006-09-11 06:57:50 +0000251
252 // It is atleast 10 bytes...
Linus Walleij1fd2d272006-05-08 09:22:01 +0000253 if (!((buf[2] == 'M') && (buf[4]=='S') && (buf[6]=='F') && (buf[8]=='T'))) {
Linus Walleijd5d51c82006-09-11 06:57:50 +0000254 printf("This is not a Microsoft MTP descriptor...\n");
255 printf("Device response to read device property 0xee:\n");
256 data_dump_ascii (stdout, buf, ret, 0);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000257 usb_close(devh);
258 continue;
259 }
260
261 cmd = buf[16];
262 ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
263 cmd, 0, 4, (char *) buf, sizeof(buf), 1000);
264 if (ret == -1) {
265 //printf("Decice could not respond to control message 1.\n");
266 usb_close(devh);
Linus Walleijd5d51c82006-09-11 06:57:50 +0000267 continue;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000268 }
269
Linus Walleijd5d51c82006-09-11 06:57:50 +0000270 if (ret > 0x15) {
271 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
272 printf("The device has a Microsoft device descriptor, but it's not MTP.\n");
273 printf("This is not an MTP device. Presumable it is USB mass storage\n");
274 printf("with some additional Janus (DRM) support.\n");
275 printf("Device response to control message 1:\n");
276 data_dump_ascii (stdout, buf, ret, 0);
277 continue;
278 }
279 } else {
280 // Not MTP or broken
281 continue;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000282 }
283
284 ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
285 cmd, 0, 5, (char *) buf, sizeof(buf), 1000);
286 if (ret == -1) {
287 //printf("Device could not respond to control message 2.\n");
288 usb_close(devh);
Linus Walleijd5d51c82006-09-11 06:57:50 +0000289 // Return the device anyway, it said previously it was MTP, right?
Linus Walleij1fd2d272006-05-08 09:22:01 +0000290 return dev;
291 }
292
Linus Walleijd5d51c82006-09-11 06:57:50 +0000293 if (ret > 0x15) {
294 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
295 printf("This device does not respond with MTP characteristics on\n");
296 printf("the second device property read (0x05), but will be regarded\n");
297 printf("as MTP anyway.\n");
298 printf("Device response to control message 2:\n");
299 data_dump_ascii (stdout, buf, ret, 0);
300 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000301 }
Linus Walleijd5d51c82006-09-11 06:57:50 +0000302
Linus Walleij1fd2d272006-05-08 09:22:01 +0000303 usb_close(devh);
304 // We can return the device here, it will be the first.
305 // If it was not MTP, the loop continues before it reaches this point.
306 return dev;
307 }
308 }
309 // If nothing was found we end up here.
310 return NULL;
311}
312
313/**
314 * Detect the MTP device descriptor and return the VID and PID
315 * of the first device found. This is a very low-level function
316 * which is intended for use with <b>udev</b> or other hotplug
317 * mechanisms. The idea is that a script may want to know if the
318 * just plugged-in device was an MTP device or not.
319 * @param vid the Vendor ID (VID) of the first device found.
320 * @param pid the Product ID (PID) of the first device found.
321 * @return the number of detected devices or -1 if the call
322 * was unsuccessful.
323 */
324int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid)
325{
326 struct usb_device *dev = probe_usb_bus_for_mtp_devices();
327 if (dev == NULL) {
328 return 0;
329 }
330 *vid = dev->descriptor.idVendor;
331 *pid = dev->descriptor.idProduct;
332 return 1;
333}
334
Linus Walleijc6210fb2006-05-08 11:11:41 +0000335/**
336 * This routine just dumps out low-level
337 * USB information about the current device.
338 * @param ptp_usb the USB device to get information from.
339 */
340void dump_usbinfo(PTP_USB *ptp_usb)
341{
342 int res;
343 struct usb_device *dev;
344
345#ifdef LIBUSB_HAS_GET_DRIVER_NP
346 char devname[0x10];
347
348 devname[0] = '\0';
349 res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname));
350 if (devname[0] != '\0') {
351 printf(" Using kernel interface \"%s\"\n", devname);
352 }
353#endif
354 dev = usb_device(ptp_usb->handle);
355 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
356 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
357 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
358 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000359 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
360 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000361 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
362 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleij0558ac52006-09-07 06:55:03 +0000363 printf(" Device flags: 0x%08x\n", ptp_usb->device_flags);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000364 // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber...
365}
366
367
Linus Walleijd6a49972006-05-02 08:24:54 +0000368// Based on same function on library.c in libgphoto2
Richard Low43fdb882006-09-06 16:19:05 +0000369#define CONTEXT_BLOCK_SIZE 0x100000
Linus Walleijb02a0662006-04-25 08:05:09 +0000370static short
371ptp_read_func (unsigned char *bytes, unsigned int size, void *data, unsigned int *readbytes)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000372{
Linus Walleijb02a0662006-04-25 08:05:09 +0000373 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleijd6a49972006-05-02 08:24:54 +0000374 int toread = 0;
375 int result = 0;
376 int curread = 0;
377 /* Split into small blocks. Too large blocks (>1x MB) would
378 * timeout.
379 */
380 while (curread < size) {
381 toread = size - curread;
Richard Low43fdb882006-09-06 16:19:05 +0000382 if (toread > CONTEXT_BLOCK_SIZE)
383 toread = CONTEXT_BLOCK_SIZE;
Linus Walleijd6a49972006-05-02 08:24:54 +0000384
385 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep,(char *)(bytes+curread), toread, ptpcam_usb_timeout);
386 if (result == 0) {
387 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep,(char *)(bytes+curread), toread, ptpcam_usb_timeout);
388 }
389 if (result < 0)
Richard Low43fdb882006-09-06 16:19:05 +0000390 return PTP_ERROR_IO;
Linus Walleije78de6d2006-10-31 14:46:36 +0000391#ifdef ENABLE_USB_BULK_DEBUG
392 printf("<==USB IN\n");
393 data_dump_ascii (stdout,(bytes+curread),toread,16);
394#endif
Linus Walleijd6a49972006-05-02 08:24:54 +0000395 curread += result;
396 if (result < toread) /* short reads are common */
397 break;
398 }
Linus Walleijee73ef22006-08-27 19:56:00 +0000399
400 // Increase counters, call callback
401 if (ptp_usb->callback_active) {
402 ptp_usb->current_transfer_complete += curread;
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000403 if (ptp_usb->current_transfer_complete > ptp_usb->current_transfer_total) {
404 // Fishy... but some commands have unpredictable lengths.
405 // send last update and disable callback.
406 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
407 ptp_usb->callback_active = 0;
408 }
Linus Walleijee73ef22006-08-27 19:56:00 +0000409 if (ptp_usb->current_transfer_callback != NULL) {
410 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
411 ptp_usb->current_transfer_total,
412 ptp_usb->current_transfer_callback_data);
413 }
414 }
415
Linus Walleijd6a49972006-05-02 08:24:54 +0000416 if (result > 0) {
417 *readbytes = curread;
418 return (PTP_RC_OK);
419 } else {
420 return PTP_ERROR_IO;
421 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000422}
423
Linus Walleijd6a49972006-05-02 08:24:54 +0000424// Based on same function on library.c in libgphoto2
Linus Walleijb02a0662006-04-25 08:05:09 +0000425static short
426ptp_write_func (unsigned char *bytes, unsigned int size, void *data)
427{
428 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleijd6a49972006-05-02 08:24:54 +0000429 int towrite = 0;
430 int result = 0;
431 int curwrite = 0;
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000432
Linus Walleijd6a49972006-05-02 08:24:54 +0000433 /*
434 * gp_port_write returns (in case of success) the number of bytes
Linus Walleijd214b9b2006-08-26 22:08:37 +0000435 * written. Too large blocks (>5x MB) could timeout.
Linus Walleijd6a49972006-05-02 08:24:54 +0000436 */
437 while (curwrite < size) {
438 towrite = size-curwrite;
Richard Low43fdb882006-09-06 16:19:05 +0000439 if (towrite > CONTEXT_BLOCK_SIZE)
440 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijd6a49972006-05-02 08:24:54 +0000441 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)(bytes+curwrite),towrite,ptpcam_usb_timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000442#ifdef ENABLE_USB_BULK_DEBUG
443 printf("USB OUT==>\n");
444 data_dump_ascii (stdout,(bytes+curwrite),towrite,16);
445#endif
Linus Walleijd6a49972006-05-02 08:24:54 +0000446 if (result < 0)
Richard Low43fdb882006-09-06 16:19:05 +0000447 return PTP_ERROR_IO;
Linus Walleijd6a49972006-05-02 08:24:54 +0000448 curwrite += result;
449 if (result < towrite) /* short writes happen */
450 break;
451 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000452
453 // Increase counters, call callback
454 if (ptp_usb->callback_active) {
Linus Walleij4af6fbe2006-08-27 09:33:52 +0000455 ptp_usb->current_transfer_complete += curwrite;
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000456 if (ptp_usb->current_transfer_complete > ptp_usb->current_transfer_total) {
457 // Fishy... but some commands have unpredictable lengths.
458 // send last update and disable callback.
459 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
460 ptp_usb->callback_active = 0;
461 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000462 if (ptp_usb->current_transfer_callback != NULL) {
463 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
464 ptp_usb->current_transfer_total,
465 ptp_usb->current_transfer_callback_data);
466 }
Linus Walleij80d134a2006-08-22 21:41:37 +0000467 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000468
469 // If this is the last transfer (callbacks only active if this function called repeatedly with
470 // new data, otherwise this is a single large transaction which ends here).
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000471 // a request and the bulk send header.
472 if (!ptp_usb->callback_active) {
Linus Walleijd214b9b2006-08-26 22:08:37 +0000473 // Then terminate an even packet boundary write with a zero length packet
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000474 if ((size % ptp_usb->outep_maxpacket) == 0) {
Linus Walleijd214b9b2006-08-26 22:08:37 +0000475 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
476 }
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000477 } else if (ptp_usb->current_transfer_complete == ptp_usb->current_transfer_total) {
478 // This is the amount actually transfered in this large transaction.
479 uint64_t actual_xfer_size = ptp_usb->current_transfer_total - 2*PTP_USB_BULK_HDR_LEN;
480
481 if ((actual_xfer_size % ptp_usb->outep_maxpacket) == 0) {
482 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000483 }
484 // Set as complete and disable callback, just as good.
485 // This also blocks callbacks from the following response command.
486 if (ptp_usb->current_transfer_callback != NULL) {
487 (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_total, // Both total = 100%
488 ptp_usb->current_transfer_total, // Both total = 100%
489 ptp_usb->current_transfer_callback_data);
490 }
491 ptp_usb->callback_active = 0;
Linus Walleijd214b9b2006-08-26 22:08:37 +0000492 }
493
Linus Walleijd6a49972006-05-02 08:24:54 +0000494 if (result < 0)
495 return PTP_ERROR_IO;
496 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000497}
498
Linus Walleijd6a49972006-05-02 08:24:54 +0000499// This is a bit hackish at the moment. I wonder if it even works.
Linus Walleijb02a0662006-04-25 08:05:09 +0000500static short ptp_check_int (unsigned char *bytes, unsigned int size, void *data, unsigned int *rlen)
501{
Linus Walleijd6a49972006-05-02 08:24:54 +0000502 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleijb02a0662006-04-25 08:05:09 +0000503 int result;
Linus Walleijb02a0662006-04-25 08:05:09 +0000504
505 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)bytes,size,ptpcam_usb_timeout);
506 if (result==0)
507 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) bytes, size, ptpcam_usb_timeout);
508 if (result >= 0) {
509 *rlen = result;
510 return (PTP_RC_OK);
511 } else {
512 return PTP_ERROR_IO;
513 }
514}
515
Linus Walleij9eb3d312006-08-04 19:25:59 +0000516static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000517{
Linus Walleijd6a49972006-05-02 08:24:54 +0000518 usb_dev_handle *device_handle;
519
520 params->write_func=ptp_write_func;
521 params->read_func=ptp_read_func;
522 params->check_int_func=ptp_check_int;
523 params->check_int_fast_func=ptp_check_int;
524 params->error_func=NULL;
525 params->debug_func=NULL;
526 params->sendreq_func=ptp_usb_sendreq;
527 params->senddata_func=ptp_usb_senddata;
528 params->getresp_func=ptp_usb_getresp;
529 params->getdata_func=ptp_usb_getdata;
530 params->data=ptp_usb;
531 params->transaction_id=0;
532 params->byteorder = PTP_DL_LE;
533
Linus Walleij0ed2a9f2006-05-04 05:31:34 +0000534 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +0000535 if (!device_handle) {
536 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +0000537 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +0000538 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +0000539 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +0000540#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
541 /*
542 * If this device is known to be wrongfully claimed by other kernel
543 * drivers (such as mass storage), then try to unload it to make it
544 * accessible from user space.
545 */
546 if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) {
547 if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
548 perror("usb_detach_kernel_driver_np()");
549 }
550 }
551#endif
Linus Walleijef5cca92006-05-05 07:19:08 +0000552 if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +0000553 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +0000554 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +0000555 }
Linus Walleijc6210fb2006-05-08 11:11:41 +0000556 ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber;
Linus Walleijd6a49972006-05-02 08:24:54 +0000557 }
Linus Walleij9eb3d312006-08-04 19:25:59 +0000558 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000559}
560
Linus Walleijd6a49972006-05-02 08:24:54 +0000561static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000562{
Linus Walleijd6a49972006-05-02 08:24:54 +0000563 uint16_t status=0;
564 int ret;
565
566 /* check the inep status */
Linus Walleij1fd2d272006-05-08 09:22:01 +0000567 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleijd6a49972006-05-02 08:24:54 +0000568 if (ret<0) perror ("inep: usb_get_endpoint_status()");
569 /* and clear the HALT condition if happend */
570 else if (status) {
571 printf("Resetting input pipe!\n");
572 ret=usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
573 /*usb_clear_halt(ptp_usb->handle,ptp_usb->inep); */
574 if (ret<0)perror ("usb_clear_stall_feature()");
575 }
576 status=0;
577
578 /* check the outep status */
579 ret=usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
580 if (ret<0) perror ("outep: usb_get_endpoint_status()");
581 /* and clear the HALT condition if happend */
582 else if (status) {
583 printf("Resetting output pipe!\n");
584 ret=usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
585 /*usb_clear_halt(ptp_usb->handle,ptp_usb->outep); */
586 if (ret<0)perror ("usb_clear_stall_feature()");
587 }
588
589 /*usb_clear_halt(ptp_usb->handle,ptp_usb->intep); */
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000590}
591
Linus Walleijd6a49972006-05-02 08:24:54 +0000592static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000593{
Linus Walleijd6a49972006-05-02 08:24:54 +0000594 clear_stall(ptp_usb);
595 // Added to clear some stuff on the OUT endpoint
596 // TODO: is this good on the Mac too?
597 usb_resetep(ptp_usb->handle, ptp_usb->outep);
598 usb_release_interface(ptp_usb->handle, interfaceNumber);
599 // Brutally reset device
600 // TODO: is this good on the Mac too?
601 usb_reset(ptp_usb->handle);
602 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000603}
604
605
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000606/*
607 find_device() returns the pointer to a usb_device structure matching
608 given busn, devicen numbers. If any or both of arguments are 0 then the
609 first matching PTP device structure is returned.
610 */
Linus Walleijd6a49972006-05-02 08:24:54 +0000611static struct usb_device* find_device (int busn, int devn, short force)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000612{
Linus Walleijd6a49972006-05-02 08:24:54 +0000613 struct usb_bus *bus;
614 struct usb_device *dev;
615
616 bus=init_usb();
617 for (; bus; bus = bus->next)
618 for (dev = bus->devices; dev; dev = dev->next)
619 /* somtimes dev->config is null, not sure why... */
620 if (dev->config != NULL)
621 if (dev->descriptor.bDeviceClass!=USB_CLASS_HUB)
622 {
623 int curbusn, curdevn;
624
625 curbusn=strtol(bus->dirname,NULL,10);
626 curdevn=strtol(dev->filename,NULL,10);
627
628 if (devn==0) {
629 if (busn==0) return dev;
630 if (curbusn==busn) return dev;
631 } else {
632 if ((busn==0)&&(curdevn==devn)) return dev;
633 if ((curbusn==busn)&&(curdevn==devn)) return dev;
634 }
635 }
636 return NULL;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000637}
638
Linus Walleij6946ac52006-03-21 06:51:22 +0000639/**
640 * This function scans through the device list to see if there are
641 * some devices to connect to. The table at the top of this file is
642 * used to identify potential devices.
643 */
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000644uint16_t connect_first_device(PTPParams *params, PTP_USB *ptp_usb, uint8_t *interfaceNumber)
645{
Linus Walleij6946ac52006-03-21 06:51:22 +0000646 struct usb_bus *bus;
647 struct usb_device *dev;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000648 struct usb_endpoint_descriptor *ep;
649 PTPDeviceInfo deviceinfo;
650 uint16_t ret=0;
651 int n;
Linus Walleij6946ac52006-03-21 06:51:22 +0000652
Linus Walleij0558ac52006-09-07 06:55:03 +0000653 // Reset device flags
654 ptp_usb->device_flags = DEVICE_FLAG_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000655 // First try to locate the device using the extended
656 // device descriptor.
657 dev = probe_usb_bus_for_mtp_devices();
658
659 if (dev != NULL) {
660 int i;
661
662 // See if we can find the name of this beast
663 for (i = 0; i < mtp_device_table_size; i++) {
664 LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i];
665 if (dev->descriptor.idVendor == mtp_device->vendor_id &&
666 dev->descriptor.idProduct == mtp_device->product_id ) {
667 printf("Autodetected device \"%s\" (VID=%04x,PID=%04x) is known.\n",
668 mtp_device->name, dev->descriptor.idVendor, dev->descriptor.idProduct);
Linus Walleij0558ac52006-09-07 06:55:03 +0000669 ptp_usb->device_flags = mtp_device->device_flags;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000670 break;
671 }
672 }
673 if (i == mtp_device_table_size) {
674 printf("Autodetected device with VID=%04x and PID=%04x is UNKNOWN.\n",
675 dev->descriptor.idVendor, dev->descriptor.idProduct);
676 printf("Please report this VID/PID and the device model name etc to the\n");
677 printf("libmtp development team!\n");
678 }
679 }
680
681 // If autodetection fails, scan the bus for well known devices.
682 if (dev == NULL) {
683 bus = init_usb();
684 for (; bus; bus = bus->next) {
685 for (dev = bus->devices; dev; dev = dev->next) {
686 int i;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000687
Linus Walleij1fd2d272006-05-08 09:22:01 +0000688 // Loop over the list of supported devices
689 for (i = 0; i < mtp_device_table_size; i++) {
690 LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i];
Linus Walleij6946ac52006-03-21 06:51:22 +0000691
Linus Walleij1fd2d272006-05-08 09:22:01 +0000692 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB &&
693 dev->descriptor.idVendor == mtp_device->vendor_id &&
694 dev->descriptor.idProduct == mtp_device->product_id ) {
695
696 printf("Found non-autodetected device \"%s\" on USB bus...\n", mtp_device->name);
Linus Walleij0558ac52006-09-07 06:55:03 +0000697 ptp_usb->device_flags = mtp_device->device_flags;
Linus Walleijd3fdd972006-05-30 15:51:37 +0000698 goto next_step;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000699
Linus Walleijb02a0662006-04-25 08:05:09 +0000700 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000701 }
Linus Walleij6946ac52006-03-21 06:51:22 +0000702 }
703 }
704 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000705
Linus Walleijd3fdd972006-05-30 15:51:37 +0000706next_step:
Linus Walleij1fd2d272006-05-08 09:22:01 +0000707 // Still not found any?
708 if (dev == NULL) {
709 return PTP_CD_RC_NO_DEVICES;
710 }
711
Linus Walleij0558ac52006-09-07 06:55:03 +0000712 // Found a device, then assign endpoints to ptp_usb...
Linus Walleij1fd2d272006-05-08 09:22:01 +0000713 ep = dev->config->interface->altsetting->endpoint;
714 n = dev->config->interface->altsetting->bNumEndpoints;
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000715 find_endpoints(dev, &ptp_usb->inep, &ptp_usb->inep_maxpacket,
716 &ptp_usb->outep, &ptp_usb->outep_maxpacket, &ptp_usb->intep);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000717
718 // printf("Init PTP USB...\n");
Linus Walleij9eb3d312006-08-04 19:25:59 +0000719 if (init_ptp_usb(params, ptp_usb, dev) < 0) {
720 return PTP_CD_RC_ERROR_CONNECTING;
721 }
Linus Walleij1fd2d272006-05-08 09:22:01 +0000722
723 ret = ptp_opensession(params,1);
724 // printf("Session open (%d)...\n", ret);
725 if (ret == PTP_RC_InvalidTransactionID) {
726 params->transaction_id += 10;
727 ret = ptp_opensession(params,1);
728 }
729 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
730 printf("Could not open session! (Return code %d)\n Try to reset the device.\n", ret);
731 usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber);
732 }
733
734 // It is actually permissible to call this before opening the session
735 ret = ptp_getdeviceinfo(params, &deviceinfo);
736 if (ret != PTP_RC_OK) {
737 printf("Could not get device info!\n");
738 usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber);
739 return PTP_CD_RC_ERROR_CONNECTING;
740 }
741
742 // we're connected, return OK
743 *interfaceNumber = dev->config->interface->altsetting->bInterfaceNumber;
744 return PTP_CD_RC_CONNECTED;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000745}
746
Linus Walleijd6a49972006-05-02 08:24:54 +0000747static 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 +0000748{
Linus Walleijd6a49972006-05-02 08:24:54 +0000749 int i,n;
750 struct usb_endpoint_descriptor *ep;
751
752 ep = dev->config->interface->altsetting->endpoint;
753 n=dev->config->interface->altsetting->bNumEndpoints;
754
755 for (i=0;i<n;i++) {
756 if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
757 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
758 USB_ENDPOINT_DIR_MASK)
759 {
760 *inep=ep[i].bEndpointAddress;
761 *inep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000762 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000763 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
764 {
765 *outep=ep[i].bEndpointAddress;
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000766 *outep_maxpacket=ep[i].wMaxPacketSize;
Linus Walleijd6a49972006-05-02 08:24:54 +0000767 }
768 } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
769 if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
770 USB_ENDPOINT_DIR_MASK)
771 {
772 *intep=ep[i].bEndpointAddress;
773 }
774 }
775 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000776}
777
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000778int open_device (int busn, int devn, short force, PTP_USB *ptp_usb, PTPParams *params, struct usb_device **dev)
779{
780#ifdef DEBUG
Linus Walleijd6a49972006-05-02 08:24:54 +0000781 printf("dev %i\tbus %i\n",devn,busn);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000782#endif
Linus Walleijd6a49972006-05-02 08:24:54 +0000783
784 *dev=find_device(busn,devn,force);
785 if (*dev==NULL) {
786 fprintf(stderr,"could not find any device matching given "
787 "bus/dev numbers\n");
788 exit(-1);
789 }
790 find_endpoints(*dev,&ptp_usb->inep,&ptp_usb->inep_maxpacket,&ptp_usb->outep,&ptp_usb->outep_maxpacket,&ptp_usb->intep);
791
Linus Walleij9eb3d312006-08-04 19:25:59 +0000792 if (init_ptp_usb(params, ptp_usb, *dev) < 0) {
793 return -1;
794 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000795 if (ptp_opensession(params,1)!=PTP_RC_OK) {
796 fprintf(stderr,"ERROR: Could not open session!\n");
797 close_usb(ptp_usb, (*dev)->config->interface->altsetting->bInterfaceNumber);
798 return -1;
799 }
800 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000801}
802
803void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber)
804{
Linus Walleijd6a49972006-05-02 08:24:54 +0000805 if (ptp_closesession(params)!=PTP_RC_OK)
806 fprintf(stderr,"ERROR: Could not close session!\n");
807 close_usb(ptp_usb, interfaceNumber);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000808}
809
Linus Walleijd6a49972006-05-02 08:24:54 +0000810static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000811{
Linus Walleijd6a49972006-05-02 08:24:54 +0000812
813 return (usb_control_msg(ptp_usb->handle,
814 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
815 ep, NULL, 0, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000816}
817
Linus Walleijd6a49972006-05-02 08:24:54 +0000818static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000819{
Linus Walleijd6a49972006-05-02 08:24:54 +0000820 return (usb_control_msg(ptp_usb->handle,
821 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
822 USB_FEATURE_HALT, ep, (char *)status, 2, 3000));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000823}