blob: 92e72ef979b5277040b499e8b0932ea72745f5ae [file] [log] [blame]
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001/*
Linus Walleij2f45d222007-02-02 22:47:39 +00002 * \file libusb-glue.c
3 * Low-level USB interface glue towards libusb.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00004 *
Linus Walleij2f45d222007-02-02 22:47:39 +00005 * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com>
Linus Walleij00411ee2008-01-27 12:24:51 +00006 * Copyright (C) 2005-2008 Linus Walleij <triad@df.lth.se>
Linus Walleij2f45d222007-02-02 22:47:39 +00007 * Copyright (C) 2006-2007 Marcus Meissner
8 * Copyright (C) 2007 Ted Bullock
Linus Walleij2f622812008-08-30 22:06:58 +00009 * Copyright (C) 2008 Chris Bagwell <chris@cnpbagwell.com>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000010 *
Linus Walleij2f45d222007-02-02 22:47:39 +000011 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
Linus Walleij2e1c8a32007-01-22 06:46:08 +000015 *
Linus Walleij2f45d222007-02-02 22:47:39 +000016 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
Linus Walleij2e1c8a32007-01-22 06:46:08 +000020 *
Linus Walleij2f45d222007-02-02 22:47:39 +000021 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 *
26 * Created by Richard Low on 24/12/2005. (as mtp-utils.c)
27 * Modified by Linus Walleij 2006-03-06
28 * (Notice that Anglo-Saxons use little-endian dates and Swedes
29 * use big-endian dates.)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000030 *
31 */
Linus Walleij7beba572006-11-29 08:56:12 +000032#include "libmtp.h"
33#include "libusb-glue.h"
Linus Walleij3e667ae2007-10-29 23:29:39 +000034#include "device-flags.h"
Linus Walleij7beba572006-11-29 08:56:12 +000035#include "util.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000036#include "ptp.h"
Linus Walleij7beba572006-11-29 08:56:12 +000037
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000038#include <errno.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000042#include <usb.h>
43
Linus Walleija0323272007-01-07 12:21:30 +000044#include "ptp-pack.c"
45
Linus Walleije78de6d2006-10-31 14:46:36 +000046/* To enable debug prints, switch on this */
Linus Walleij1018c162008-03-07 23:11:58 +000047//#define ENABLE_USB_BULK_DEBUG
Linus Walleije78de6d2006-10-31 14:46:36 +000048
Linus Walleij2f622812008-08-30 22:06:58 +000049/* Default USB timeout length. This can be overridden as needed
50 * but should start with a reasonable value so most common
51 * requests can be completed. The original value of 4000 was
52 * not long enough for large file transfer. Also, players can
53 * spend a bit of time collecting data. Higher values also
54 * make connecting/disconnecting more reliable.
55 */
56#define USB_TIMEOUT_DEFAULT 10000
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000057
58/* USB control message data phase direction */
59#ifndef USB_DP_HTD
60#define USB_DP_HTD (0x00 << 7) /* host to device */
61#endif
62#ifndef USB_DP_DTH
63#define USB_DP_DTH (0x01 << 7) /* device to host */
64#endif
65
66/* USB Feature selector HALT */
67#ifndef USB_FEATURE_HALT
68#define USB_FEATURE_HALT 0x00
69#endif
70
Linus Walleija700d222008-05-28 23:06:14 +000071/* Internal data types */
72struct mtpdevice_list_struct {
73 struct usb_device *libusb_device;
74 PTPParams *params;
75 PTP_USB *ptp_usb;
76 uint32_t bus_location;
77 struct mtpdevice_list_struct *next;
78};
79typedef struct mtpdevice_list_struct mtpdevice_list_t;
80
Linus Walleij6fd2f082006-03-28 07:19:22 +000081static const LIBMTP_device_entry_t mtp_device_table[] = {
Linus Walleij1a673de2007-10-29 23:10:05 +000082/* We include an .h file which is shared between us and libgphoto2 */
83#include "music-players.h"
Linus Walleija5483642006-03-09 09:20:38 +000084};
Linus Walleij6fd2f082006-03-28 07:19:22 +000085static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +000086
Linus Walleijd6a49972006-05-02 08:24:54 +000087// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +000088static struct usb_bus* init_usb();
Linus Walleijb0ab5482007-08-29 21:08:54 +000089static void close_usb(PTP_USB* ptp_usb);
90static void find_interface_and_endpoints(struct usb_device *dev,
91 uint8_t *interface,
92 int* inep,
93 int* inep_maxpacket,
94 int* outep,
95 int* outep_maxpacket,
96 int* intep);
Linus Walleijd6a49972006-05-02 08:24:54 +000097static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +000098static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +000099static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000100static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000101static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
102static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000103
Linus Walleij552ba322007-01-22 11:49:59 +0000104/**
105 * Get a list of the supported USB devices.
106 *
107 * The developers depend on users of this library to constantly
108 * add in to the list of supported devices. What we need is the
109 * device name, USB Vendor ID (VID) and USB Product ID (PID).
110 * put this into a bug ticket at the project homepage, please.
111 * The VID/PID is used to let e.g. udev lift the device to
112 * console userspace access when it's plugged in.
113 *
114 * @param devices a pointer to a pointer that will hold a device
115 * list after the call to this function, if it was
116 * successful.
117 * @param numdevs a pointer to an integer that will hold the number
118 * of devices in the device list if the call was successful.
119 * @return 0 if the list was successfull retrieved, any other
120 * value means failure.
121 */
122int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000123{
124 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
125 *numdevs = mtp_device_table_size;
126 return 0;
127}
128
Linus Walleij552ba322007-01-22 11:49:59 +0000129
Linus Walleij1fd2d272006-05-08 09:22:01 +0000130static struct usb_bus* init_usb()
131{
132 usb_init();
133 usb_find_busses();
134 usb_find_devices();
135 return (usb_get_busses());
136}
137
138/**
tedbullock0f033cb2007-02-14 20:56:54 +0000139 * Small recursive function to append a new usb_device to the linked list of
140 * USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000141 * @param devlist dynamic linked list of pointers to usb devices with MTP
Linus Walleij63fd1e62008-04-23 23:49:43 +0000142 * properties, to be extended with new device.
143 * @param newdevice the new device to add.
144 * @param bus_location bus for this device.
145 * @return an extended array or NULL on failure.
tedbullock0f033cb2007-02-14 20:56:54 +0000146 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000147static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000148 struct usb_device *newdevice,
149 uint32_t bus_location)
tedbullock0f033cb2007-02-14 20:56:54 +0000150{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000151 mtpdevice_list_t *new_list_entry;
tedbullock0f033cb2007-02-14 20:56:54 +0000152
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000153 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
154 if (new_list_entry == NULL) {
155 return NULL;
156 }
157 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
158 new_list_entry->libusb_device = newdevice;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000159 new_list_entry->bus_location = bus_location;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000160 new_list_entry->next = NULL;
161
162 if (devlist == NULL) {
163 return new_list_entry;
164 } else {
165 mtpdevice_list_t *tmp = devlist;
166 while (tmp->next != NULL) {
167 tmp = tmp->next;
168 }
169 tmp->next = new_list_entry;
170 }
171 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000172}
173
174/**
175 * Small recursive function to free dynamic memory allocated to the linked list
176 * of USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000177 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000178 * properties.
179 * @return nothing
180 */
Linus Walleija700d222008-05-28 23:06:14 +0000181static void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000182{
Linus Walleijac061862007-03-07 08:28:33 +0000183 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000184
185 if (devlist == NULL)
186 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000187 while (tmplist != NULL) {
188 mtpdevice_list_t *tmp = tmplist;
189 tmplist = tmplist->next;
Richard Low61edc1a2007-09-23 10:35:48 +0000190 // Do not free() the fields (ptp_usb, params)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000191 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000192 }
tedbullock0f033cb2007-02-14 20:56:54 +0000193 return;
194}
195
196/**
Linus Walleij9521e2b2007-07-10 21:50:42 +0000197 * This checks if a device has an MTP descriptor. The descriptor was
198 * elaborated about in gPhoto bug 1482084, and some official documentation
199 * with no strings attached was published by Microsoft at
200 * http://www.microsoft.com/whdc/system/bus/USB/USBFAQ_intermed.mspx#E3HAC
201 *
Linus Walleija4942fc2007-03-12 19:23:21 +0000202 * @param dev a device struct from libusb.
Linus Walleij9521e2b2007-07-10 21:50:42 +0000203 * @param dumpfile set to non-NULL to make the descriptors dump out
204 * to this file in human-readable hex so we can scruitinze them.
Linus Walleija4942fc2007-03-12 19:23:21 +0000205 * @return 1 if the device is MTP compliant, 0 if not.
206 */
Linus Walleij9521e2b2007-07-10 21:50:42 +0000207static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
Linus Walleija4942fc2007-03-12 19:23:21 +0000208{
209 usb_dev_handle *devh;
210 unsigned char buf[1024], cmd;
Linus Walleijbc550bc2007-11-08 23:25:09 +0000211 int i;
Linus Walleija4942fc2007-03-12 19:23:21 +0000212 int ret;
213
214 /* Don't examine hubs (no point in that) */
215 if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) {
216 return 0;
217 }
218
219 /* Attempt to open Device on this port */
220 devh = usb_open(dev);
221 if (devh == NULL) {
222 /* Could not open this device */
223 return 0;
224 }
Linus Walleijbc550bc2007-11-08 23:25:09 +0000225
226 /*
227 * Loop over the device configurations and interfaces. Nokia MTP-capable
228 * handsets (possibly others) typically have the string "MTP" in their
229 * MTP interface descriptions, that's how they can be detected, before
230 * we try the more esoteric "OS descriptors" (below).
231 */
232 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
233 uint8_t j;
234
235 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
236 int k;
237 for (k = 0; k < dev->config[i].interface[j].num_altsetting; k++) {
238 buf[0] = '\0';
239 ret = usb_get_string_simple(devh,
240 dev->config[i].interface[j].altsetting[k].iInterface,
241 (char *) buf,
242 1024);
243 if (ret < 3)
244 continue;
245 if (strcmp((char *) buf, "MTP") == 0) {
246 if (dumpfile != NULL) {
247 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\n", i, j, k);
248 fprintf(dumpfile, " Interface description contains the string \"MTP\"\n");
249 fprintf(dumpfile, " Device recognized as MTP, no further probing.\n");
250 }
251 usb_close(devh);
252 return 1;
253 }
254 }
255 }
256 }
Linus Walleija4942fc2007-03-12 19:23:21 +0000257
258 /* Read the special descriptor */
259 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
Linus Walleij9521e2b2007-07-10 21:50:42 +0000260
261 // Dump it, if requested
262 if (dumpfile != NULL && ret > 0) {
263 fprintf(dumpfile, "Microsoft device descriptor 0xee:\n");
264 data_dump_ascii(dumpfile, buf, ret, 16);
265 }
Linus Walleija4942fc2007-03-12 19:23:21 +0000266
267 /* Check if descriptor length is at least 10 bytes */
268 if (ret < 10) {
269 usb_close(devh);
270 return 0;
271 }
272
273 /* Check if this device has a Microsoft Descriptor */
274 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
275 (buf[6] == 'F') && (buf[8] == 'T'))) {
276 usb_close(devh);
277 return 0;
278 }
279
280 /* Check if device responds to control message 1 or if there is an error */
281 cmd = buf[16];
282 ret = usb_control_msg (devh,
283 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
284 cmd,
285 0,
286 4,
287 (char *) buf,
288 sizeof(buf),
Linus Walleij2f622812008-08-30 22:06:58 +0000289 USB_TIMEOUT_DEFAULT);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000290
291 // Dump it, if requested
292 if (dumpfile != NULL && ret > 0) {
293 fprintf(dumpfile, "Microsoft device response to control message 1, CMD 0x%02x:\n", cmd);
294 data_dump_ascii(dumpfile, buf, ret, 16);
295 }
Linus Walleija4942fc2007-03-12 19:23:21 +0000296
297 /* If this is true, the device either isn't MTP or there was an error */
298 if (ret <= 0x15) {
299 /* TODO: If there was an error, flag it and let the user know somehow */
300 /* if(ret == -1) {} */
301 usb_close(devh);
302 return 0;
303 }
304
305 /* Check if device is MTP or if it is something like a USB Mass Storage
306 device with Janus DRM support */
307 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
308 usb_close(devh);
309 return 0;
310 }
311
312 /* After this point we are probably dealing with an MTP device */
313
314 /* Check if device responds to control message 2 or if there is an error*/
315 ret = usb_control_msg (devh,
316 USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR,
317 cmd,
318 0,
319 5,
320 (char *) buf,
321 sizeof(buf),
Linus Walleij2f622812008-08-30 22:06:58 +0000322 USB_TIMEOUT_DEFAULT);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000323
324 // Dump it, if requested
325 if (dumpfile != NULL && ret > 0) {
326 fprintf(dumpfile, "Microsoft device response to control message 2, CMD 0x%02x:\n", cmd);
327 data_dump_ascii(dumpfile, buf, ret, 16);
328 }
Linus Walleija4942fc2007-03-12 19:23:21 +0000329
330 /* If this is true, the device errored against control message 2 */
331 if (ret == -1) {
332 /* TODO: Implement callback function to let managing program know there
333 was a problem, along with description of the problem */
334 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
335 "ProductID:%04x encountered an error responding to "
336 "control message 2.\n"
337 "Problems may arrise but continuing\n",
338 dev->descriptor.idVendor, dev->descriptor.idProduct);
339 } else if (ret <= 0x15) {
340 /* TODO: Implement callback function to let managing program know there
341 was a problem, along with description of the problem */
342 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
343 "ProductID:%04x responded to control message 2 with a "
344 "response that was too short. Problems may arrise but "
345 "continuing\n",
346 dev->descriptor.idVendor, dev->descriptor.idProduct);
347 } else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
348 /* TODO: Implement callback function to let managing program know there
349 was a problem, along with description of the problem */
350 fprintf(stderr, "Potential MTP Device with VendorID:%04x and "
351 "ProductID:%04x encountered an error responding to "
352 "control message 2\n"
353 "Problems may arrise but continuing\n",
354 dev->descriptor.idVendor, dev->descriptor.idProduct);
355 }
356
357 /* Close the USB device handle */
358 usb_close(devh);
359 return 1;
360}
361
362/**
363 * This function scans through the connected usb devices on a machine and
364 * if they match known Vendor and Product identifiers appends them to the
365 * dynamic array mtp_device_list. Be sure to call
Linus Walleij63fd1e62008-04-23 23:49:43 +0000366 * <code>free_mtpdevice_list(mtp_device_list)</code> when you are done
367 * with it, assuming it is not NULL.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000368 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000369 * properties (if this list is not empty, new entries will be appended
370 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000371 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
Linus Walleij9c6c1542007-09-19 13:43:06 +0000372 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no
373 * devices have been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000374 */
Linus Walleij45a86372007-03-07 09:36:19 +0000375static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000376{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000377 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000378 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000379 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000380 for (; dev != NULL; dev = dev->next) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000381 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) {
Linus Walleij9c6c1542007-09-19 13:43:06 +0000382 int i;
383 int found = 0;
384
385 // First check if we know about the device already.
386 // Devices well known to us will not have their descriptors
387 // probed, it caused problems with some devices.
Richard Lowe9f205f2007-09-16 16:25:50 +0000388 for(i = 0; i < mtp_device_table_size; i++) {
389 if(dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
390 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
391 /* Append this usb device to the MTP device list */
Linus Walleij63fd1e62008-04-23 23:49:43 +0000392 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
393 dev,
394 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000395 found = 1;
396 break;
397 }
398 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000399 // If we didn't know it, try probing the "OS Descriptor".
Richard Lowe9f205f2007-09-16 16:25:50 +0000400 if (!found) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000401 if (probe_device_descriptor(dev, NULL)) {
402 /* Append this usb device to the MTP USB Device List */
Linus Walleij63fd1e62008-04-23 23:49:43 +0000403 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
404 dev,
405 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000406 }
407 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000408 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000409 }
410 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000411
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000412 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000413 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000414 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000415 }
416 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000417}
418
419/**
Linus Walleij63fd1e62008-04-23 23:49:43 +0000420 * Detect the raw MTP device descriptors and return a list of
421 * of the devices found.
tedbullock2b45f2e2007-02-23 20:16:37 +0000422 *
Linus Walleij63fd1e62008-04-23 23:49:43 +0000423 * @param devices a pointer to a variable that will hold
424 * the list of raw devices found. This may be NULL
425 * on return if the number of detected devices is zero.
426 * The user shall simply <code>free()</code> this
427 * variable when finished with the raw devices,
428 * in order to release memory.
429 * @param numdevs a pointer to an integer that will hold
430 * the number of devices in the list. This may
431 * be 0.
432 * @return 0 if successful, any other value means failure.
Linus Walleij1fd2d272006-05-08 09:22:01 +0000433 */
Linus Walleija700d222008-05-28 23:06:14 +0000434LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t ** devices,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000435 int * numdevs)
Linus Walleij1fd2d272006-05-08 09:22:01 +0000436{
Linus Walleij63fd1e62008-04-23 23:49:43 +0000437 mtpdevice_list_t *devlist = NULL;
438 mtpdevice_list_t *dev;
439 LIBMTP_error_number_t ret;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000440 LIBMTP_raw_device_t *retdevs;
441 int devs = 0;
Linus Walleijf106aa62008-05-04 22:55:51 +0000442 int i, j;
Linus Walleijb6b69132007-03-07 09:45:05 +0000443
444 ret = get_mtp_usb_device_list(&devlist);
Linus Walleijef2fb362008-05-28 20:59:09 +0000445 if (ret == LIBMTP_ERROR_NO_DEVICE_ATTACHED) {
446 *devices = NULL;
447 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000448 return ret;
Linus Walleijef2fb362008-05-28 20:59:09 +0000449 } else if (ret != LIBMTP_ERROR_NONE) {
Linus Walleijf106aa62008-05-04 22:55:51 +0000450 fprintf(stderr, "LIBMTP PANIC: get_mtp_usb_device_list() "
451 "error code: %d on line %d\n", ret, __LINE__);
Linus Walleija700d222008-05-28 23:06:14 +0000452 return ret;
Linus Walleijb6b69132007-03-07 09:45:05 +0000453 }
Linus Walleijf106aa62008-05-04 22:55:51 +0000454
Linus Walleij63fd1e62008-04-23 23:49:43 +0000455 // Get list size
456 dev = devlist;
457 while (dev != NULL) {
458 devs++;
459 dev = dev->next;
460 }
461 if (devs == 0) {
462 *devices = NULL;
463 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000464 return LIBMTP_ERROR_NONE;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000465 }
466 // Conjure a device list
467 retdevs = (LIBMTP_raw_device_t *) malloc(sizeof(LIBMTP_raw_device_t) * devs);
468 if (retdevs == NULL) {
469 // Out of memory
470 *devices = NULL;
471 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000472 return LIBMTP_ERROR_MEMORY_ALLOCATION;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000473 }
474 dev = devlist;
475 i = 0;
476 while (dev != NULL) {
Linus Walleijf106aa62008-05-04 22:55:51 +0000477 int device_known = 0;
478
Linus Walleij63fd1e62008-04-23 23:49:43 +0000479 // Assign default device info
480 retdevs[i].device_entry.vendor = NULL;
481 retdevs[i].device_entry.vendor_id = dev->libusb_device->descriptor.idVendor;
482 retdevs[i].device_entry.product = NULL;
483 retdevs[i].device_entry.product_id = dev->libusb_device->descriptor.idProduct;
484 retdevs[i].device_entry.device_flags = 0x00000000U;
Linus Walleijfec4d562008-06-01 22:30:36 +0000485 // See if we can locate some additional vendor info and device flags
Linus Walleijf106aa62008-05-04 22:55:51 +0000486 for(j = 0; j < mtp_device_table_size; j++) {
487 if(dev->libusb_device->descriptor.idVendor == mtp_device_table[j].vendor_id &&
488 dev->libusb_device->descriptor.idProduct == mtp_device_table[j].product_id) {
489 device_known = 1;
490 retdevs[i].device_entry.vendor = mtp_device_table[j].vendor;
491 retdevs[i].device_entry.product = mtp_device_table[j].product;
492 retdevs[i].device_entry.device_flags = mtp_device_table[j].device_flags;
493#ifdef ENABLE_USB_BULK_DEBUG
494 // This device is known to the developers
495 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is a %s %s.\n",
496 i,
497 dev->libusb_device->descriptor.idVendor,
498 dev->libusb_device->descriptor.idProduct,
499 mtp_device_table[j].vendor,
500 mtp_device_table[j].product);
501#endif
502 break;
503 }
504 }
505 if (!device_known) {
506 // This device is unknown to the developers
507 fprintf(stderr, "Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
508 i,
509 dev->libusb_device->descriptor.idVendor,
510 dev->libusb_device->descriptor.idProduct);
511 fprintf(stderr, "Please report this VID/PID and the device model to the "
512 "libmtp development team\n");
Linus Walleijfec4d562008-06-01 22:30:36 +0000513 /*
514 * Trying to get iManufacturer or iProduct from the device at this
515 * point would require opening a device handle, that we don't want
516 * to do right now. (Takes time for no good enough reason.)
517 */
Linus Walleijf106aa62008-05-04 22:55:51 +0000518 }
519 // Save the location on the bus
Linus Walleij63fd1e62008-04-23 23:49:43 +0000520 retdevs[i].bus_location = dev->bus_location;
521 retdevs[i].devnum = dev->libusb_device->devnum;
522 i++;
523 dev = dev->next;
524 }
525 *devices = retdevs;
526 *numdevs = i;
Linus Walleijb6b69132007-03-07 09:45:05 +0000527 free_mtpdevice_list(devlist);
Linus Walleija700d222008-05-28 23:06:14 +0000528 return LIBMTP_ERROR_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000529}
530
Linus Walleijc6210fb2006-05-08 11:11:41 +0000531/**
532 * This routine just dumps out low-level
533 * USB information about the current device.
534 * @param ptp_usb the USB device to get information from.
535 */
536void dump_usbinfo(PTP_USB *ptp_usb)
537{
538 int res;
539 struct usb_device *dev;
540
541#ifdef LIBUSB_HAS_GET_DRIVER_NP
542 char devname[0x10];
543
544 devname[0] = '\0';
Linus Walleijb0ab5482007-08-29 21:08:54 +0000545 res = usb_get_driver_np(ptp_usb->handle, (int) ptp_usb->interface, devname, sizeof(devname));
Linus Walleijc6210fb2006-05-08 11:11:41 +0000546 if (devname[0] != '\0') {
547 printf(" Using kernel interface \"%s\"\n", devname);
548 }
549#endif
550 dev = usb_device(ptp_usb->handle);
551 printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
552 printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
553 printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
554 printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000555 printf(" idVendor: %04x\n", dev->descriptor.idVendor);
556 printf(" idProduct: %04x\n", dev->descriptor.idProduct);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000557 printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
558 printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
Linus Walleijfec4d562008-06-01 22:30:36 +0000559 printf(" Raw device info:\n");
560 printf(" Bus location: %d\n", ptp_usb->rawdevice.bus_location);
561 printf(" Device number: %d\n", ptp_usb->rawdevice.devnum);
562 printf(" Device entry info:\n");
563 printf(" Vendor: %s\n", ptp_usb->rawdevice.device_entry.vendor);
564 printf(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.vendor_id);
565 printf(" Product: %s\n", ptp_usb->rawdevice.device_entry.product);
566 printf(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.product_id);
567 printf(" Device flags: 0x%08x\n", ptp_usb->rawdevice.device_entry.device_flags);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000568 (void) probe_device_descriptor(dev, stdout);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000569}
570
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000571/**
572 * Retrieve the apropriate playlist extension for this
573 * device. Rather hacky at the moment. This is probably
574 * desired by the managing software, but when creating
575 * lists on the device itself you notice certain preferences.
576 * @param ptp_usb the USB device to get suggestion for.
577 * @return the suggested playlist extension.
578 */
579char const * const get_playlist_extension(PTP_USB *ptp_usb)
580{
581 struct usb_device *dev;
582 static char creative_pl_extension[] = ".zpl";
583 static char default_pl_extension[] = ".pla";
584
585 dev = usb_device(ptp_usb->handle);
586 if (dev->descriptor.idVendor == 0x041e) {
587 return creative_pl_extension;
588 }
589 return default_pl_extension;
590}
591
Linus Walleija0323272007-01-07 12:21:30 +0000592static void
593ptp_debug (PTPParams *params, const char *format, ...)
594{
595 va_list args;
596
597 va_start (args, format);
598 if (params->debug_func!=NULL)
599 params->debug_func (params->data, format, args);
600 else
601 {
602 vfprintf (stderr, format, args);
603 fprintf (stderr,"\n");
604 fflush (stderr);
605 }
606 va_end (args);
607}
608
609static void
610ptp_error (PTPParams *params, const char *format, ...)
611{
612 va_list args;
613
614 va_start (args, format);
615 if (params->error_func!=NULL)
616 params->error_func (params->data, format, args);
617 else
618 {
619 vfprintf (stderr, format, args);
620 fprintf (stderr,"\n");
621 fflush (stderr);
622 }
623 va_end (args);
624}
625
Linus Walleijc6210fb2006-05-08 11:11:41 +0000626
Linus Walleij99ed83c2007-01-02 18:46:02 +0000627/*
628 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000629 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000630 * Much reading packet logs and having fun with trials and errors
631 * reveals that WMP / Windows is probably using an algorithm like this
632 * for large transfers:
633 *
634 * 1. Send the command (0x0c bytes) if headers are split, else, send
635 * command plus sizeof(endpoint) - 0x0c bytes.
636 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
637 * split headers. Else goto 3.
638 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
639 * We call 0x10000 CONTEXT_BLOCK_SIZE.
640 * 4. Send remaining bytes MOD sizeof(endpoint)
641 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
642 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000643 *
644 * Further there is some special quirks to handle zero reads from the
645 * device, since some devices can't do them at all due to shortcomings
646 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000647 */
Richard Low4fd59d62007-03-03 16:34:37 +0000648#define CONTEXT_BLOCK_SIZE_1 0x3e00
649#define CONTEXT_BLOCK_SIZE_2 0x200
650#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000651static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000652ptp_read_func (
653 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000654 unsigned long *readbytes,
655 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000656) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000657 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000658 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000659 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000660 unsigned long curread = 0;
661 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000662 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000663 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000664
665 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000666 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000667 while (curread < size) {
Richard Low4fd59d62007-03-03 16:34:37 +0000668
669#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleijb8f78c72008-03-02 20:22:56 +0000670 printf("Remaining size to read: 0x%04lx bytes\n", size - curread);
Richard Low4fd59d62007-03-03 16:34:37 +0000671#endif
672 // check equal to condition here
673 if (size - curread < CONTEXT_BLOCK_SIZE)
674 {
675 // this is the last packet
676 toread = size - curread;
677 // this is equivalent to zero read for these devices
Linus Walleijfec4d562008-06-01 22:30:36 +0000678 if (readzero && FLAG_NO_ZERO_READS(ptp_usb) && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000679 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000680 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000681 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000682 }
Richard Low4fd59d62007-03-03 16:34:37 +0000683 else if (curread == 0)
684 // we are first packet, but not last packet
685 toread = CONTEXT_BLOCK_SIZE_1;
686 else if (toread == CONTEXT_BLOCK_SIZE_1)
687 toread = CONTEXT_BLOCK_SIZE_2;
688 else if (toread == CONTEXT_BLOCK_SIZE_2)
689 toread = CONTEXT_BLOCK_SIZE_1;
690 else
Linus Walleij4cec9872007-03-03 21:00:53 +0000691 printf("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
692 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000693
Richard Lowd284f072007-02-17 08:52:41 +0000694#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleijb8f78c72008-03-02 20:22:56 +0000695 printf("Reading in 0x%04lx bytes\n", toread);
Richard Lowd284f072007-02-17 08:52:41 +0000696#endif
Linus Walleij2f622812008-08-30 22:06:58 +0000697 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptp_usb->timeout);
Richard Lowd284f072007-02-17 08:52:41 +0000698#ifdef ENABLE_USB_BULK_DEBUG
699 printf("Result of read: 0x%04x\n", result);
700#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000701
Linus Walleijc49c6372007-01-09 00:18:51 +0000702 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000703 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000704 }
Linus Walleije78de6d2006-10-31 14:46:36 +0000705#ifdef ENABLE_USB_BULK_DEBUG
706 printf("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000707 if (result == 0)
708 printf("Zero Read\n");
709 else
710 data_dump_ascii (stdout,bytes,result,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000711#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000712
713 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000714 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000715 {
716#ifdef ENABLE_USB_BULK_DEBUG
717 printf("<==USB IN\nDiscarding extra byte\n");
718#endif
Richard Low4fd59d62007-03-03 16:34:37 +0000719 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000720 }
Richard Low4fd59d62007-03-03 16:34:37 +0000721
Linus Walleijc49c6372007-01-09 00:18:51 +0000722 handler->putfunc(NULL, handler->private, result, bytes, &written);
Richard Lowbf2675c2007-03-18 18:20:26 +0000723
Linus Walleijc49c6372007-01-09 00:18:51 +0000724 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000725 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000726
727 // Increase counters, call callback
728 if (ptp_usb->callback_active) {
729 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
730 // send last update and disable callback.
731 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
732 ptp_usb->callback_active = 0;
733 }
734 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000735 int ret;
736 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
737 ptp_usb->current_transfer_total,
738 ptp_usb->current_transfer_callback_data);
739 if (ret != 0) {
740 return PTP_ERROR_CANCEL;
741 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000742 }
743 }
744
Linus Walleijd6a49972006-05-02 08:24:54 +0000745 if (result < toread) /* short reads are common */
746 break;
747 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000748 if (readbytes) *readbytes = curread;
749 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000750
Richard Low4df32f82007-01-11 20:04:39 +0000751 // there might be a zero packet waiting for us...
Linus Walleij049f50c2007-03-03 20:30:43 +0000752 if (readzero &&
Linus Walleijfec4d562008-06-01 22:30:36 +0000753 !FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +0000754 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000755 char temp;
756 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000757
Richard Low4df32f82007-01-11 20:04:39 +0000758#ifdef ENABLE_USB_BULK_DEBUG
759 printf("<==USB IN\n");
760 printf("Zero Read\n");
761#endif
Linus Walleij2f622812008-08-30 22:06:58 +0000762 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptp_usb->timeout);
Richard Low4df32f82007-01-11 20:04:39 +0000763 if (zeroresult != 0)
764 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
765 }
766
Linus Walleijd6a49972006-05-02 08:24:54 +0000767 if (result > 0) {
Linus Walleijd6a49972006-05-02 08:24:54 +0000768 return (PTP_RC_OK);
769 } else {
770 return PTP_ERROR_IO;
771 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000772}
773
Linus Walleijb02a0662006-04-25 08:05:09 +0000774static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000775ptp_write_func (
776 unsigned long size,
777 PTPDataHandler *handler,
778 void *data,
779 unsigned long *written
780) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000781 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000782 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000783 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000784 unsigned long curwrite = 0;
785 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000786
787 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000788 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000789 if (!bytes) {
790 return PTP_ERROR_IO;
791 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000792 while (curwrite < size) {
793 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000794 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000795 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000796 } else {
797 // This magic makes packets the same size that WMP send them.
798 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000799 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000800 }
801 }
802 handler->getfunc(NULL, handler->private,towrite,bytes,&towrite);
Linus Walleij2f622812008-08-30 22:06:58 +0000803 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptp_usb->timeout);
Linus Walleije78de6d2006-10-31 14:46:36 +0000804#ifdef ENABLE_USB_BULK_DEBUG
805 printf("USB OUT==>\n");
Richard Lowedd61832006-12-30 12:38:18 +0000806 data_dump_ascii (stdout,bytes,towrite,16);
Linus Walleije78de6d2006-10-31 14:46:36 +0000807#endif
Linus Walleijc49c6372007-01-09 00:18:51 +0000808 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000809 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000810 }
811 // Increase counters
812 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000813 curwrite += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000814
815 // call callback
816 if (ptp_usb->callback_active) {
817 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
818 // send last update and disable callback.
819 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
820 ptp_usb->callback_active = 0;
821 }
822 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000823 int ret;
824 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
825 ptp_usb->current_transfer_total,
826 ptp_usb->current_transfer_callback_data);
827 if (ret != 0) {
828 return PTP_ERROR_CANCEL;
829 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000830 }
831 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000832 if (result < towrite) /* short writes happen */
833 break;
834 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000835 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000836 if (written) {
837 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000838 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000839
Linus Walleijc49c6372007-01-09 00:18:51 +0000840
Richard Lowef05b892007-01-03 21:18:56 +0000841 // If this is the last transfer send a zero write if required
842 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000843 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
Richard Low021421e2007-01-01 18:08:57 +0000844#ifdef ENABLE_USB_BULK_DEBUG
845 printf("USB OUT==>\n");
846 printf("Zero Write\n");
847#endif
Linus Walleij2f622812008-08-30 22:06:58 +0000848 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptp_usb->timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000849 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000850 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000851
Linus Walleijd6a49972006-05-02 08:24:54 +0000852 if (result < 0)
853 return PTP_ERROR_IO;
854 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000855}
856
Linus Walleija0323272007-01-07 12:21:30 +0000857/* memory data get/put handler */
858typedef struct {
859 unsigned char *data;
860 unsigned long size, curoff;
861} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000862
Linus Walleija0323272007-01-07 12:21:30 +0000863static uint16_t
864memory_getfunc(PTPParams* params, void* private,
865 unsigned long wantlen, unsigned char *data,
866 unsigned long *gotlen
867) {
868 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
869 unsigned long tocopy = wantlen;
870
871 if (priv->curoff + tocopy > priv->size)
872 tocopy = priv->size - priv->curoff;
873 memcpy (data, priv->data + priv->curoff, tocopy);
874 priv->curoff += tocopy;
875 *gotlen = tocopy;
876 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000877}
878
Linus Walleija0323272007-01-07 12:21:30 +0000879static uint16_t
880memory_putfunc(PTPParams* params, void* private,
881 unsigned long sendlen, unsigned char *data,
882 unsigned long *putlen
883) {
884 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
885
886 if (priv->curoff + sendlen > priv->size) {
887 priv->data = realloc (priv->data, priv->curoff+sendlen);
888 priv->size = priv->curoff + sendlen;
889 }
890 memcpy (priv->data + priv->curoff, data, sendlen);
891 priv->curoff += sendlen;
892 *putlen = sendlen;
893 return PTP_RC_OK;
894}
895
896/* init private struct for receiving data. */
897static uint16_t
898ptp_init_recv_memory_handler(PTPDataHandler *handler) {
899 PTPMemHandlerPrivate* priv;
900 priv = malloc (sizeof(PTPMemHandlerPrivate));
901 handler->private = priv;
902 handler->getfunc = memory_getfunc;
903 handler->putfunc = memory_putfunc;
904 priv->data = NULL;
905 priv->size = 0;
906 priv->curoff = 0;
907 return PTP_RC_OK;
908}
909
910/* init private struct and put data in for sending data.
911 * data is still owned by caller.
912 */
913static uint16_t
914ptp_init_send_memory_handler(PTPDataHandler *handler,
915 unsigned char *data, unsigned long len
916) {
917 PTPMemHandlerPrivate* priv;
918 priv = malloc (sizeof(PTPMemHandlerPrivate));
919 if (!priv)
920 return PTP_RC_GeneralError;
921 handler->private = priv;
922 handler->getfunc = memory_getfunc;
923 handler->putfunc = memory_putfunc;
924 priv->data = data;
925 priv->size = len;
926 priv->curoff = 0;
927 return PTP_RC_OK;
928}
929
930/* free private struct + data */
931static uint16_t
932ptp_exit_send_memory_handler (PTPDataHandler *handler) {
933 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
934 /* data is owned by caller */
935 free (priv);
936 return PTP_RC_OK;
937}
938
939/* hand over our internal data to caller */
940static uint16_t
941ptp_exit_recv_memory_handler (PTPDataHandler *handler,
942 unsigned char **data, unsigned long *size
943) {
944 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->private;
945 *data = priv->data;
946 *size = priv->size;
947 free (priv);
948 return PTP_RC_OK;
949}
950
951/* send / receive functions */
952
953uint16_t
954ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
955{
956 uint16_t ret;
957 PTPUSBBulkContainer usbreq;
958 PTPDataHandler memhandler;
Linus Walleij5ba40402008-05-23 21:36:54 +0000959 unsigned long written = 0;
960 unsigned long towrite;
Linus Walleijb8f78c72008-03-02 20:22:56 +0000961#ifdef ENABLE_USB_BULK_DEBUG
962 char txt[256];
Linus Walleija0323272007-01-07 12:21:30 +0000963
Linus Walleijb8f78c72008-03-02 20:22:56 +0000964 (void) ptp_render_opcode (params, req->Code, sizeof(txt), txt);
965 printf("REQUEST: 0x%04x, %s\n", req->Code, txt);
966#endif
Linus Walleija0323272007-01-07 12:21:30 +0000967 /* build appropriate USB container */
968 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
969 (sizeof(uint32_t)*(5-req->Nparam)));
970 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
971 usbreq.code=htod16(req->Code);
972 usbreq.trans_id=htod32(req->Transaction_ID);
973 usbreq.payload.params.param1=htod32(req->Param1);
974 usbreq.payload.params.param2=htod32(req->Param2);
975 usbreq.payload.params.param3=htod32(req->Param3);
976 usbreq.payload.params.param4=htod32(req->Param4);
977 usbreq.payload.params.param5=htod32(req->Param5);
978 /* send it to responder */
979 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
980 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
981 ret=ptp_write_func(
982 towrite,
983 &memhandler,
984 params->data,
985 &written
986 );
987 ptp_exit_send_memory_handler (&memhandler);
Linus Walleijff01cb12007-09-16 19:49:48 +0000988 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) {
Linus Walleija0323272007-01-07 12:21:30 +0000989 ret = PTP_ERROR_IO;
Linus Walleija0323272007-01-07 12:21:30 +0000990 }
Linus Walleij724f0102007-09-17 20:10:12 +0000991 if (written != towrite && ret != PTP_ERROR_CANCEL && ret != PTP_ERROR_IO) {
Linus Walleija0323272007-01-07 12:21:30 +0000992 ptp_error (params,
993 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +0000994 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +0000995 );
996 ret = PTP_ERROR_IO;
997 }
998 return ret;
999}
1000
1001uint16_t
1002ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1003 unsigned long size, PTPDataHandler *handler
1004) {
1005 uint16_t ret;
1006 int wlen, datawlen;
1007 unsigned long written;
1008 PTPUSBBulkContainer usbdata;
1009 uint32_t bytes_left_to_transfer;
1010 PTPDataHandler memhandler;
1011
Linus Walleijb8f78c72008-03-02 20:22:56 +00001012#ifdef ENABLE_USB_BULK_DEBUG
1013 printf("SEND DATA PHASE\n");
1014#endif
Linus Walleija0323272007-01-07 12:21:30 +00001015 /* build appropriate USB container */
1016 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1017 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1018 usbdata.code = htod16(ptp->Code);
1019 usbdata.trans_id= htod32(ptp->Transaction_ID);
1020
1021 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001022 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001023
1024 if (params->split_header_data) {
1025 datawlen = 0;
1026 wlen = PTP_USB_BULK_HDR_LEN;
1027 } else {
1028 unsigned long gotlen;
1029 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001030 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001031 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
1032 ret = handler->getfunc(params, handler->private, datawlen, usbdata.payload.data, &gotlen);
1033 if (ret != PTP_RC_OK)
1034 return ret;
1035 if (gotlen != datawlen)
1036 return PTP_RC_GeneralError;
1037 }
1038 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1039 /* send first part of data */
1040 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1041 ptp_exit_send_memory_handler (&memhandler);
1042 if (ret!=PTP_RC_OK) {
Linus Walleija0323272007-01-07 12:21:30 +00001043 return ret;
1044 }
1045 if (size <= datawlen) return ret;
1046 /* if everything OK send the rest */
1047 bytes_left_to_transfer = size-datawlen;
1048 ret = PTP_RC_OK;
1049 while(bytes_left_to_transfer > 0) {
1050 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1051 if (ret != PTP_RC_OK)
1052 break;
1053 if (written == 0) {
1054 ret = PTP_ERROR_IO;
1055 break;
1056 }
1057 bytes_left_to_transfer -= written;
1058 }
Linus Walleijff01cb12007-09-16 19:49:48 +00001059 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL)
Linus Walleija0323272007-01-07 12:21:30 +00001060 ret = PTP_ERROR_IO;
1061 return ret;
1062}
1063
1064static uint16_t ptp_usb_getpacket(PTPParams *params,
1065 PTPUSBBulkContainer *packet, unsigned long *rlen)
1066{
1067 PTPDataHandler memhandler;
1068 uint16_t ret;
1069 unsigned char *x = NULL;
1070
1071 /* read the header and potentially the first data */
1072 if (params->response_packet_size > 0) {
1073 /* If there is a buffered packet, just use it. */
1074 memcpy(packet, params->response_packet, params->response_packet_size);
1075 *rlen = params->response_packet_size;
1076 free(params->response_packet);
1077 params->response_packet = NULL;
1078 params->response_packet_size = 0;
1079 /* Here this signifies a "virtual read" */
1080 return PTP_RC_OK;
1081 }
1082 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001083 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001084 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1085 if (x) {
1086 memcpy (packet, x, *rlen);
1087 free (x);
1088 }
1089 return ret;
1090}
1091
1092uint16_t
1093ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1094{
1095 uint16_t ret;
1096 PTPUSBBulkContainer usbdata;
Linus Walleija0323272007-01-07 12:21:30 +00001097 unsigned long written;
Linus Walleij8e3af002007-09-28 19:21:54 +00001098 PTP_USB *ptp_usb = (PTP_USB *) params->data;
Linus Walleija0323272007-01-07 12:21:30 +00001099
Linus Walleijb8f78c72008-03-02 20:22:56 +00001100#ifdef ENABLE_USB_BULK_DEBUG
1101 printf("GET DATA PHASE\n");
1102#endif
Linus Walleija0323272007-01-07 12:21:30 +00001103 memset(&usbdata,0,sizeof(usbdata));
1104 do {
1105 unsigned long len, rlen;
1106
1107 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1108 if (ret!=PTP_RC_OK) {
1109 ret = PTP_ERROR_IO;
1110 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001111 }
Linus Walleija0323272007-01-07 12:21:30 +00001112 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1113 ret = PTP_ERROR_DATA_EXPECTED;
1114 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001115 }
Linus Walleija0323272007-01-07 12:21:30 +00001116 if (dtoh16(usbdata.code)!=ptp->Code) {
Linus Walleijfec4d562008-06-01 22:30:36 +00001117 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleij8e3af002007-09-28 19:21:54 +00001118 ptp_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001119 "PTP header, code field insane, expect problems! (But continuing)");
Linus Walleij06542122007-10-14 22:13:48 +00001120 // Repair the header, so it won't wreak more havoc, don't just ignore it.
1121 // Typically these two fields will be broken.
1122 usbdata.code = htod16(ptp->Code);
1123 usbdata.trans_id = htod32(ptp->Transaction_ID);
Linus Walleij94cd9722007-09-28 21:29:53 +00001124 ret = PTP_RC_OK;
Linus Walleij8e3af002007-09-28 19:21:54 +00001125 } else {
Linus Walleij913a3062007-09-28 21:42:39 +00001126 ret = dtoh16(usbdata.code);
1127 // This filters entirely insane garbage return codes, but still
1128 // makes it possible to return error codes in the code field when
1129 // getting data. It appears Windows ignores the contents of this
1130 // field entirely.
1131 if (ret < PTP_RC_Undefined || ret > PTP_RC_SpecificationOfDestinationUnsupported) {
1132 ptp_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
1133 "PTP header, code field insane.");
1134 ret = PTP_ERROR_IO;
1135 }
Linus Walleij8e3af002007-09-28 19:21:54 +00001136 break;
1137 }
Linus Walleija0323272007-01-07 12:21:30 +00001138 }
1139 if (usbdata.length == 0xffffffffU) {
Linus Walleij06e42322008-08-16 23:34:32 +00001140 /* Copy first part of data to 'data' */
1141 handler->putfunc(
1142 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1143 &written
1144 );
Linus Walleija0323272007-01-07 12:21:30 +00001145 /* stuff data directly to passed data handler */
1146 while (1) {
1147 unsigned long readdata;
Linus Walleije1be4b92008-08-18 08:49:22 +00001148 uint16_t xret;
Linus Walleija0323272007-01-07 12:21:30 +00001149
1150 xret = ptp_read_func(
Richard Low02724f62007-02-17 09:47:26 +00001151 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
Linus Walleija0323272007-01-07 12:21:30 +00001152 handler,
1153 params->data,
Richard Low4df32f82007-01-11 20:04:39 +00001154 &readdata,
Linus Walleijc0a11432007-03-02 21:21:18 +00001155 0
Linus Walleija0323272007-01-07 12:21:30 +00001156 );
Linus Walleijff01cb12007-09-16 19:49:48 +00001157 if (xret != PTP_RC_OK)
Linus Walleije1be4b92008-08-18 08:49:22 +00001158 return xret;
Richard Low02724f62007-02-17 09:47:26 +00001159 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
Linus Walleija0323272007-01-07 12:21:30 +00001160 break;
1161 }
1162 return PTP_RC_OK;
1163 }
1164 if (rlen > dtoh32(usbdata.length)) {
1165 /*
1166 * Buffer the surplus response packet if it is >=
1167 * PTP_USB_BULK_HDR_LEN
1168 * (i.e. it is probably an entire package)
1169 * else discard it as erroneous surplus data.
1170 * This will even work if more than 2 packets appear
1171 * in the same transaction, they will just be handled
1172 * iteratively.
1173 *
1174 * Marcus observed stray bytes on iRiver devices;
1175 * these are still discarded.
1176 */
1177 unsigned int packlen = dtoh32(usbdata.length);
1178 unsigned int surplen = rlen - packlen;
1179
1180 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1181 params->response_packet = malloc(surplen);
1182 memcpy(params->response_packet,
1183 (uint8_t *) &usbdata + packlen, surplen);
1184 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001185 /* Ignore reading one extra byte if device flags have been set */
Linus Walleijfec4d562008-06-01 22:30:36 +00001186 } else if(!FLAG_NO_ZERO_READS(ptp_usb) &&
1187 (rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleij8e3af002007-09-28 19:21:54 +00001188 ptp_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
1189 "too much, expect problems!",
1190 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001191 }
1192 rlen = packlen;
1193 }
1194
1195 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1196 * here. For MTP devices splitting header and data it might
1197 * be 12.
1198 */
1199 /* Evaluate full data length. */
1200 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1201
1202 /* autodetect split header/data MTP devices */
1203 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1204 params->split_header_data = 1;
1205
Linus Walleija0323272007-01-07 12:21:30 +00001206 /* Copy first part of data to 'data' */
1207 handler->putfunc(
1208 params, handler->private, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1209 &written
1210 );
Richard Low4fd59d62007-03-03 16:34:37 +00001211
Linus Walleijfec4d562008-06-01 22:30:36 +00001212 if (FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +00001213 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
Richard Low057ea772007-03-24 13:37:38 +00001214#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleij049f50c2007-03-03 20:30:43 +00001215 printf("Reading in extra terminating byte\n");
Richard Low4fd59d62007-03-03 16:34:37 +00001216#endif
Linus Walleij049f50c2007-03-03 20:30:43 +00001217 // need to read in extra byte and discard it
1218 int result = 0;
1219 char byte = 0;
Linus Walleij2f622812008-08-30 22:06:58 +00001220 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptp_usb->timeout);
Linus Walleij049f50c2007-03-03 20:30:43 +00001221
1222 if (result != 1)
1223 printf("Could not read in extra byte for PTP_USB_BULK_HS_MAX_PACKET_LEN_READ long file, return value 0x%04x\n", result);
Richard Low057ea772007-03-24 13:37:38 +00001224 } else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ && params->split_header_data == 0) {
Linus Walleij94cd9722007-09-28 21:29:53 +00001225 int zeroresult = 0;
1226 char zerobyte = 0;
1227
Richard Low057ea772007-03-24 13:37:38 +00001228#ifdef ENABLE_USB_BULK_DEBUG
Linus Walleij94cd9722007-09-28 21:29:53 +00001229 printf("Reading in zero packet after header\n");
Richard Low057ea772007-03-24 13:37:38 +00001230#endif
Linus Walleij2f622812008-08-30 22:06:58 +00001231 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptp_usb->timeout);
Linus Walleij94cd9722007-09-28 21:29:53 +00001232
1233 if (zeroresult != 0)
1234 printf("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
1235 }
1236
Linus Walleija0323272007-01-07 12:21:30 +00001237 /* Is that all of data? */
Linus Walleij94cd9722007-09-28 21:29:53 +00001238 if (len+PTP_USB_BULK_HDR_LEN<=rlen) {
1239 break;
1240 }
Linus Walleij049f50c2007-03-03 20:30:43 +00001241
Linus Walleij913a3062007-09-28 21:42:39 +00001242 ret = ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1243 handler,
1244 params->data, &rlen, 1);
Linus Walleij049f50c2007-03-03 20:30:43 +00001245
Linus Walleija0323272007-01-07 12:21:30 +00001246 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001247 break;
Linus Walleija0323272007-01-07 12:21:30 +00001248 }
1249 } while (0);
Linus Walleija0323272007-01-07 12:21:30 +00001250 return ret;
1251}
1252
1253uint16_t
1254ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1255{
1256 uint16_t ret;
1257 unsigned long rlen;
1258 PTPUSBBulkContainer usbresp;
Linus Walleij3e4b1142007-11-01 23:54:16 +00001259 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
Linus Walleija0323272007-01-07 12:21:30 +00001260
Linus Walleijb8f78c72008-03-02 20:22:56 +00001261#ifdef ENABLE_USB_BULK_DEBUG
1262 printf("RESPONSE: ");
1263#endif
Linus Walleija0323272007-01-07 12:21:30 +00001264 memset(&usbresp,0,sizeof(usbresp));
1265 /* read response, it should never be longer than sizeof(usbresp) */
1266 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1267
Linus Walleijb8f78c72008-03-02 20:22:56 +00001268 // Fix for bevahiour reported by Scott Snyder on Samsung YP-U3. The player
1269 // sends a packet containing just zeroes of length 2 (up to 4 has been seen too)
1270 // after a NULL packet when it should send the response. This code ignores
1271 // such illegal packets.
Linus Walleij3191bba2008-02-10 21:16:25 +00001272 while (ret==PTP_RC_OK && rlen<PTP_USB_BULK_HDR_LEN && usbresp.length==0) {
1273 ptp_debug (params, "ptp_usb_getresp: detected short response "
1274 "of %d bytes, expect problems! (re-reading "
1275 "response), rlen");
Linus Walleij00411ee2008-01-27 12:24:51 +00001276 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1277 }
1278
Linus Walleija0323272007-01-07 12:21:30 +00001279 if (ret!=PTP_RC_OK) {
1280 ret = PTP_ERROR_IO;
1281 } else
1282 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1283 ret = PTP_ERROR_RESP_EXPECTED;
1284 } else
1285 if (dtoh16(usbresp.code)!=resp->Code) {
1286 ret = dtoh16(usbresp.code);
1287 }
Linus Walleijb8f78c72008-03-02 20:22:56 +00001288#ifdef ENABLE_USB_BULK_DEBUG
1289 printf("%04x\n", ret);
1290#endif
Linus Walleija0323272007-01-07 12:21:30 +00001291 if (ret!=PTP_RC_OK) {
1292/* ptp_error (params,
1293 "PTP: request code 0x%04x getting resp error 0x%04x",
1294 resp->Code, ret);*/
1295 return ret;
1296 }
1297 /* build an appropriate PTPContainer */
1298 resp->Code=dtoh16(usbresp.code);
1299 resp->SessionID=params->session_id;
1300 resp->Transaction_ID=dtoh32(usbresp.trans_id);
Linus Walleijfec4d562008-06-01 22:30:36 +00001301 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleij3e4b1142007-11-01 23:54:16 +00001302 if (resp->Transaction_ID != params->transaction_id-1) {
1303 ptp_debug (params, "ptp_usb_getresp: detected a broken "
Linus Walleij00411ee2008-01-27 12:24:51 +00001304 "PTP header, transaction ID insane, expect "
1305 "problems! (But continuing)");
Linus Walleij3e4b1142007-11-01 23:54:16 +00001306 // Repair the header, so it won't wreak more havoc.
1307 resp->Transaction_ID = params->transaction_id-1;
1308 }
1309 }
Linus Walleija0323272007-01-07 12:21:30 +00001310 resp->Param1=dtoh32(usbresp.payload.params.param1);
1311 resp->Param2=dtoh32(usbresp.payload.params.param2);
1312 resp->Param3=dtoh32(usbresp.payload.params.param3);
1313 resp->Param4=dtoh32(usbresp.payload.params.param4);
1314 resp->Param5=dtoh32(usbresp.payload.params.param5);
1315 return ret;
1316}
1317
1318/* Event handling functions */
1319
1320/* PTP Events wait for or check mode */
1321#define PTP_EVENT_CHECK 0x0000 /* waits for */
1322#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1323
1324static inline uint16_t
1325ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1326{
1327 uint16_t ret;
1328 int result;
1329 unsigned long rlen;
1330 PTPUSBEventContainer usbevent;
1331 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1332
1333 memset(&usbevent,0,sizeof(usbevent));
1334
1335 if ((params==NULL) || (event==NULL))
1336 return PTP_ERROR_BADPARAM;
1337 ret = PTP_RC_OK;
1338 switch(wait) {
1339 case PTP_EVENT_CHECK:
Linus Walleij2f622812008-08-30 22:06:58 +00001340 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001341 if (result==0)
Linus Walleij2f622812008-08-30 22:06:58 +00001342 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001343 if (result < 0) ret = PTP_ERROR_IO;
1344 break;
1345 case PTP_EVENT_CHECK_FAST:
Linus Walleij2f622812008-08-30 22:06:58 +00001346 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001347 if (result==0)
Linus Walleij2f622812008-08-30 22:06:58 +00001348 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001349 if (result < 0) ret = PTP_ERROR_IO;
1350 break;
1351 default:
1352 ret=PTP_ERROR_BADPARAM;
1353 break;
1354 }
1355 if (ret!=PTP_RC_OK) {
1356 ptp_error (params,
1357 "PTP: reading event an error 0x%04x occurred", ret);
1358 return PTP_ERROR_IO;
1359 }
1360 rlen = result;
1361 if (rlen < 8) {
1362 ptp_error (params,
1363 "PTP: reading event an short read of %ld bytes occurred", rlen);
1364 return PTP_ERROR_IO;
1365 }
1366 /* if we read anything over interrupt endpoint it must be an event */
1367 /* build an appropriate PTPContainer */
1368 event->Code=dtoh16(usbevent.code);
1369 event->SessionID=params->session_id;
1370 event->Transaction_ID=dtoh32(usbevent.trans_id);
1371 event->Param1=dtoh32(usbevent.param1);
1372 event->Param2=dtoh32(usbevent.param2);
1373 event->Param3=dtoh32(usbevent.param3);
1374 return ret;
1375}
1376
1377uint16_t
1378ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1379
1380 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1381}
1382
1383uint16_t
1384ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1385
1386 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1387}
1388
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001389uint16_t
1390ptp_usb_control_cancel_request (PTPParams *params, uint32_t transactionid) {
1391 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1392 int ret;
1393 unsigned char buffer[6];
1394
1395 htod16a(&buffer[0],PTP_EC_CancelTransaction);
1396 htod32a(&buffer[2],transactionid);
1397 ret = usb_control_msg(ptp_usb->handle,
1398 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Linus Walleij2f622812008-08-30 22:06:58 +00001399 0x64, 0x0000, 0x0000, (char *) buffer, sizeof(buffer), ptp_usb->timeout);
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001400 if (ret < sizeof(buffer))
1401 return PTP_ERROR_IO;
1402 return PTP_RC_OK;
1403}
Linus Walleija0323272007-01-07 12:21:30 +00001404
Linus Walleij9eb3d312006-08-04 19:25:59 +00001405static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001406{
Linus Walleijd6a49972006-05-02 08:24:54 +00001407 usb_dev_handle *device_handle;
1408
Linus Walleijd6a49972006-05-02 08:24:54 +00001409 params->error_func=NULL;
1410 params->debug_func=NULL;
1411 params->sendreq_func=ptp_usb_sendreq;
1412 params->senddata_func=ptp_usb_senddata;
1413 params->getresp_func=ptp_usb_getresp;
1414 params->getdata_func=ptp_usb_getdata;
Linus Walleijff01cb12007-09-16 19:49:48 +00001415 params->cancelreq_func=ptp_usb_control_cancel_request;
Linus Walleijd6a49972006-05-02 08:24:54 +00001416 params->data=ptp_usb;
1417 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001418 /*
1419 * This is hardcoded here since we have no devices whatsoever that are BE.
1420 * Change this the day we run into our first BE device (if ever).
1421 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001422 params->byteorder = PTP_DL_LE;
1423
Linus Walleij2f622812008-08-30 22:06:58 +00001424 ptp_usb->timeout = USB_TIMEOUT_DEFAULT;
1425
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001426 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001427 if (!device_handle) {
1428 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001429 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001430 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001431 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001432#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1433 /*
1434 * If this device is known to be wrongfully claimed by other kernel
1435 * drivers (such as mass storage), then try to unload it to make it
1436 * accessible from user space.
1437 */
Linus Walleijfec4d562008-06-01 22:30:36 +00001438 if (FLAG_UNLOAD_DRIVER(ptp_usb)) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001439 if (usb_detach_kernel_driver_np(device_handle, (int) ptp_usb->interface)) {
Linus Walleij7beba572006-11-29 08:56:12 +00001440 // Totally ignore this error!
1441 // perror("usb_detach_kernel_driver_np()");
Linus Walleij501ba4d2006-10-16 06:17:04 +00001442 }
1443 }
1444#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001445#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001446 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001447 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1448 perror("usb_set_configuration()");
1449 return -1;
1450 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001451#endif
Linus Walleijb0ab5482007-08-29 21:08:54 +00001452 if (usb_claim_interface(device_handle, (int) ptp_usb->interface)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001453 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001454 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001455 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001456 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001457 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001458}
1459
Linus Walleijd6a49972006-05-02 08:24:54 +00001460static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001461{
Linus Walleij58b62792007-01-07 12:38:59 +00001462 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001463 int ret;
1464
1465 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001466 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001467 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001468 if (ret<0) {
1469 perror ("inep: usb_get_endpoint_status()");
1470 } else if (status) {
1471 printf("Clearing stall on IN endpoint\n");
1472 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1473 if (ret<0) {
1474 perror ("usb_clear_stall_feature()");
1475 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001476 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001477
1478 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001479 status=0;
1480 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1481 if (ret<0) {
1482 perror("outep: usb_get_endpoint_status()");
1483 } else if (status) {
1484 printf("Clearing stall on OUT endpoint\n");
1485 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1486 if (ret<0) {
1487 perror("usb_clear_stall_feature()");
1488 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001489 }
Linus Walleij58b62792007-01-07 12:38:59 +00001490
1491 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1492}
1493
1494static void clear_halt(PTP_USB* ptp_usb)
1495{
1496 int ret;
1497
1498 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1499 if (ret<0) {
1500 perror("usb_clear_halt() on IN endpoint");
1501 }
1502 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1503 if (ret<0) {
1504 perror("usb_clear_halt() on OUT endpoint");
1505 }
1506 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1507 if (ret<0) {
1508 perror("usb_clear_halt() on INTERRUPT endpoint");
1509 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001510}
1511
Linus Walleijb0ab5482007-08-29 21:08:54 +00001512static void close_usb(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001513{
Linus Walleijda17cda2007-09-18 21:12:05 +00001514 // Commented out since it was confusing some
1515 // devices to do these things.
Linus Walleijfec4d562008-06-01 22:30:36 +00001516 if (!FLAG_NO_RELEASE_INTERFACE(ptp_usb)) {
Linus Walleijeabc2312008-02-13 00:02:00 +00001517 /*
1518 * Clear any stalled endpoints
1519 * On misbehaving devices designed for Windows/Mac, quote from:
1520 * http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt
1521 * Device does Bad Things(tm) when it gets a GET_STATUS after CLEAR_HALT
1522 * (...) Windows, when clearing a stall, only sends the CLEAR_HALT command,
1523 * and presumes that the stall has cleared. Some devices actually choke
1524 * if the CLEAR_HALT is followed by a GET_STATUS (used to determine if the
1525 * STALL is persistant or not).
1526 */
Linus Walleij29f03ba2007-09-25 19:22:12 +00001527 clear_stall(ptp_usb);
1528 // Clear halts on any endpoints
1529 clear_halt(ptp_usb);
1530 // Added to clear some stuff on the OUT endpoint
1531 // TODO: is this good on the Mac too?
1532 // HINT: some devices may need that you comment these two out too.
1533 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1534 usb_release_interface(ptp_usb->handle, (int) ptp_usb->interface);
1535 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001536 // Brutally reset device
1537 // TODO: is this good on the Mac too?
1538 usb_reset(ptp_usb->handle);
1539 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001540}
1541
Linus Walleij550d6d52007-01-24 14:32:51 +00001542/**
Linus Walleija700d222008-05-28 23:06:14 +00001543 * Self-explanatory?
1544 */
Linus Walleijb0ab5482007-08-29 21:08:54 +00001545static void find_interface_and_endpoints(struct usb_device *dev,
1546 uint8_t *interface,
1547 int* inep,
1548 int* inep_maxpacket,
1549 int* outep,
1550 int *outep_maxpacket,
1551 int* intep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001552{
Linus Walleijd7373cd2007-08-28 21:42:05 +00001553 int i;
1554
1555 // Loop over the device configurations
1556 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001557 uint8_t j;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001558
1559 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001560 uint8_t k;
1561 uint8_t no_ep;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001562 struct usb_endpoint_descriptor *ep;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001563
Linus Walleijb0ab5482007-08-29 21:08:54 +00001564 if (dev->descriptor.bNumConfigurations > 1 || dev->config[i].bNumInterfaces > 1) {
1565 // OK This device has more than one interface, so we have to find out
1566 // which one to use!
1567 // FIXME: Probe the interface.
1568 // FIXME: Release modules attached to all other interfaces in Linux...?
1569 }
1570
1571 *interface = dev->config[i].interface[j].altsetting->bInterfaceNumber;
1572 ep = dev->config[i].interface[j].altsetting->endpoint;
1573 no_ep = dev->config[i].interface[j].altsetting->bNumEndpoints;
1574
1575 for (k = 0; k < no_ep; k++) {
Linus Walleijd7373cd2007-08-28 21:42:05 +00001576 if (ep[k].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1577 if ((ep[k].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1578 USB_ENDPOINT_DIR_MASK)
1579 {
1580 *inep=ep[k].bEndpointAddress;
1581 *inep_maxpacket=ep[k].wMaxPacketSize;
1582 }
1583 if ((ep[k].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1584 {
1585 *outep=ep[k].bEndpointAddress;
1586 *outep_maxpacket=ep[k].wMaxPacketSize;
1587 }
1588 } else if (ep[k].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1589 if ((ep[k].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1590 USB_ENDPOINT_DIR_MASK)
1591 {
1592 *intep=ep[k].bEndpointAddress;
1593 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001594 }
Linus Walleijd7373cd2007-08-28 21:42:05 +00001595 }
1596 // We assigned the endpoints so return here.
1597 return;
Linus Walleijd6a49972006-05-02 08:24:54 +00001598 }
1599 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001600}
1601
Linus Walleija700d222008-05-28 23:06:14 +00001602/**
1603 * This function assigns params and usbinfo given a raw device
1604 * as input.
1605 * @param device the device to be assigned.
Linus Walleija700d222008-05-28 23:06:14 +00001606 * @param usbinfo a pointer to the new usbinfo.
1607 * @return an error code.
1608 */
1609LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
Linus Walleij3c643252008-05-31 23:22:28 +00001610 PTPParams *params,
Linus Walleija700d222008-05-28 23:06:14 +00001611 void **usbinfo)
1612{
Linus Walleija700d222008-05-28 23:06:14 +00001613 PTP_USB *ptp_usb;
1614 struct usb_device *libusb_device;
1615 uint16_t ret = 0;
Linus Walleija700d222008-05-28 23:06:14 +00001616 struct usb_bus *bus;
1617 int found = 0;
1618
1619 /* See if we can find this raw device again... */
1620 bus = init_usb();
1621 for (; bus != NULL; bus = bus->next) {
1622 if (bus->location == device->bus_location) {
1623 struct usb_device *dev = bus->devices;
1624
1625 for (; dev != NULL; dev = dev->next) {
1626 if(dev->devnum == device->devnum &&
1627 dev->descriptor.idVendor == device->device_entry.vendor_id &&
1628 dev->descriptor.idProduct == device->device_entry.product_id ) {
1629 libusb_device = dev;
1630 found = 1;
1631 break;
1632 }
1633 }
1634 if (found)
1635 break;
1636 }
1637 }
1638 /* Device has gone since detecting raw devices! */
1639 if (!found) {
1640 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
1641 }
1642
1643 /* Allocate structs */
Linus Walleija700d222008-05-28 23:06:14 +00001644 ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleij3c643252008-05-31 23:22:28 +00001645 if (ptp_usb == NULL) {
Linus Walleija700d222008-05-28 23:06:14 +00001646 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1647 }
1648 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleija700d222008-05-28 23:06:14 +00001649 memset(ptp_usb, 0, sizeof(PTP_USB));
1650
Linus Walleijfec4d562008-06-01 22:30:36 +00001651 /* Copy the raw device */
1652 memcpy(&ptp_usb->rawdevice, device, sizeof(LIBMTP_raw_device_t));
Linus Walleij335a81c2008-06-02 23:01:00 +00001653
1654 /*
1655 * Some devices must have their "OS Descriptor" massaged in order
1656 * to work.
1657 */
1658 if (FLAG_ALWAYS_PROBE_DESCRIPTOR(ptp_usb)) {
1659 // Massage the device descriptor
1660 (void) probe_device_descriptor(libusb_device, NULL);
1661 }
Linus Walleija700d222008-05-28 23:06:14 +00001662
Linus Walleija700d222008-05-28 23:06:14 +00001663 /* Assign endpoints to usbinfo... */
1664 find_interface_and_endpoints(libusb_device,
1665 &ptp_usb->interface,
1666 &ptp_usb->inep,
1667 &ptp_usb->inep_maxpacket,
1668 &ptp_usb->outep,
1669 &ptp_usb->outep_maxpacket,
1670 &ptp_usb->intep);
1671
1672 /* Attempt to initialize this device */
Linus Walleij3c643252008-05-31 23:22:28 +00001673 if (init_ptp_usb(params, ptp_usb, libusb_device) < 0) {
Linus Walleija700d222008-05-28 23:06:14 +00001674 fprintf(stderr, "LIBMTP PANIC: Unable to initialize device\n");
1675 return LIBMTP_ERROR_CONNECTING;
1676 }
1677
1678 /*
1679 * This works in situations where previous bad applications
1680 * have not used LIBMTP_Release_Device on exit
1681 */
Linus Walleij3c643252008-05-31 23:22:28 +00001682 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
Linus Walleija700d222008-05-28 23:06:14 +00001683 fprintf(stderr, "PTP_ERROR_IO: Trying again after re-initializing USB interface\n");
1684 close_usb(ptp_usb);
1685
Linus Walleij3c643252008-05-31 23:22:28 +00001686 if(init_ptp_usb(params, ptp_usb, libusb_device) <0) {
Linus Walleija700d222008-05-28 23:06:14 +00001687 fprintf(stderr, "LIBMTP PANIC: Could not open session on device\n");
1688 return LIBMTP_ERROR_CONNECTING;
1689 }
1690
1691 /* Device has been reset, try again */
Linus Walleij3c643252008-05-31 23:22:28 +00001692 ret = ptp_opensession(params, 1);
Linus Walleija700d222008-05-28 23:06:14 +00001693 }
1694
1695 /* Was the transaction id invalid? Try again */
1696 if (ret == PTP_RC_InvalidTransactionID) {
1697 fprintf(stderr, "LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
Linus Walleij3c643252008-05-31 23:22:28 +00001698 params->transaction_id += 10;
1699 ret = ptp_opensession(params, 1);
Linus Walleija700d222008-05-28 23:06:14 +00001700 }
1701
1702 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
1703 fprintf(stderr, "LIBMTP PANIC: Could not open session! "
1704 "(Return code %d)\n Try to reset the device.\n",
1705 ret);
1706 usb_release_interface(ptp_usb->handle,
1707 (int) ptp_usb->interface);
1708 return LIBMTP_ERROR_CONNECTING;
1709 }
1710
1711 /* OK configured properly */
Linus Walleija700d222008-05-28 23:06:14 +00001712 *usbinfo = (void *) ptp_usb;
1713 return LIBMTP_ERROR_NONE;
1714}
1715
1716
Linus Walleijb0ab5482007-08-29 21:08:54 +00001717void close_device (PTP_USB *ptp_usb, PTPParams *params)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001718{
Linus Walleijd6a49972006-05-02 08:24:54 +00001719 if (ptp_closesession(params)!=PTP_RC_OK)
1720 fprintf(stderr,"ERROR: Could not close session!\n");
Linus Walleijb0ab5482007-08-29 21:08:54 +00001721 close_usb(ptp_usb);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001722}
1723
Linus Walleij2f622812008-08-30 22:06:58 +00001724void set_usb_device_timeout(PTP_USB *ptp_usb, int timeout)
1725{
1726 ptp_usb->timeout = timeout;
1727}
1728
1729void get_usb_device_timeout(PTP_USB *ptp_usb, int *timeout)
1730{
1731 *timeout = ptp_usb->timeout;
1732}
1733
Linus Walleijd6a49972006-05-02 08:24:54 +00001734static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001735{
Linus Walleijd6a49972006-05-02 08:24:54 +00001736
1737 return (usb_control_msg(ptp_usb->handle,
1738 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
Linus Walleij2f622812008-08-30 22:06:58 +00001739 ep, NULL, 0, ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001740}
1741
Linus Walleijd6a49972006-05-02 08:24:54 +00001742static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001743{
Linus Walleijd6a49972006-05-02 08:24:54 +00001744 return (usb_control_msg(ptp_usb->handle,
1745 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
Linus Walleij2f622812008-08-30 22:06:58 +00001746 USB_FEATURE_HALT, ep, (char *)status, 2, ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001747}