blob: 0ea6b29c801de4f70d68c161c0618c887c507137 [file] [log] [blame]
Linus Walleij7b7a0e22007-02-05 18:22:27 +00001/**
Linus Walleij7cf02642011-11-16 23:44:58 +01002 * \file libusb-glue.h
Linus Walleij7b7a0e22007-02-05 18:22:27 +00003 * Low-level USB interface glue towards libusb.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00004 *
Linus Walleij7b7a0e22007-02-05 18:22:27 +00005 * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com>
Linus Walleij15d18e32012-08-20 02:00:34 +02006 * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se>
Linus Walleijfbbef8b2011-11-17 19:19:51 +01007 * Copyright (C) 2006-2011 Marcus Meissner
Linus Walleij7b7a0e22007-02-05 18:22:27 +00008 * Copyright (C) 2007 Ted Bullock
Linus Walleij2f622812008-08-30 22:06:58 +00009 * Copyright (C) 2008 Chris Bagwell <chris@cnpbagwell.com>
Linus Walleij7b7a0e22007-02-05 18:22:27 +000010 *
11 * 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.
15 *
16 * 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.
20 *
21 * 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.
27 * Modified by Linus Walleij
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000028 *
29 */
Linus Walleij7cf02642011-11-16 23:44:58 +010030#ifndef LIBUSB_GLUE_H
31#define LIBUSB_GLUE_H
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000032
Linus Walleijb02a0662006-04-25 08:05:09 +000033#include "ptp.h"
Linus Walleijfbbef8b2011-11-17 19:19:51 +010034#ifdef HAVE_LIBUSB1
Linus Walleij0976e942013-11-06 04:44:23 +010035#include <libusb.h>
Linus Walleijfbbef8b2011-11-17 19:19:51 +010036#endif
37#ifdef HAVE_LIBUSB0
Linus Walleij7cf02642011-11-16 23:44:58 +010038#include <usb.h>
Linus Walleijfbbef8b2011-11-17 19:19:51 +010039#endif
Darran Kartaschew11082d32011-12-13 00:42:01 +010040#ifdef HAVE_LIBOPENUSB
41#include <openusb.h>
42#endif
Linus Walleij15e344f2006-03-06 15:15:00 +000043#include "libmtp.h"
Linus Walleijfec4d562008-06-01 22:30:36 +000044#include "device-flags.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000045
Linus Walleijd866d242009-08-23 21:50:39 +000046/* Make functions available for C++ */
47#ifdef __cplusplus
48extern "C" {
49#endif /* __cplusplus */
50
nicklas79daadbf22009-09-28 18:19:34 +000051/**
52 * Debug macro
53 */
54#define LIBMTP_USB_DEBUG(format, args...) \
55 do { \
nicklas7903584082009-09-28 18:20:16 +000056 if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0) \
nicklas79daadbf22009-09-28 18:19:34 +000057 fprintf(stdout, "LIBMTP %s[%d]: " format, __FUNCTION__, __LINE__, ##args); \
58 } while (0)
59
60#define LIBMTP_USB_DATA(buffer, length, base) \
61 do { \
nicklas7903584082009-09-28 18:20:16 +000062 if ((LIBMTP_debug & LIBMTP_DEBUG_DATA) != 0) \
nicklas79daadbf22009-09-28 18:19:34 +000063 data_dump_ascii (stdout, buffer, length, base); \
64 } while (0)
65
Linus Walleijfbbef8b2011-11-17 19:19:51 +010066#ifdef HAVE_LIBUSB1
67#define USB_BULK_READ libusb_bulk_transfer
68#define USB_BULK_WRITE libusb_bulk_transfer
69#endif
70#ifdef HAVE_LIBUSB0
Linus Walleij7cf02642011-11-16 23:44:58 +010071#define USB_BULK_READ usb_bulk_read
72#define USB_BULK_WRITE usb_bulk_write
Linus Walleijfbbef8b2011-11-17 19:19:51 +010073#endif
Darran Kartaschew11082d32011-12-13 00:42:01 +010074#ifdef HAVE_LIBOPENUSB
75#define USB_BULK_READ openusb_bulk_xfer
76#define USB_BULK_WRITE openusb_bulk_xfer
77#endif
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000078
Linus Walleij2d411db2006-03-22 12:13:09 +000079/**
Linus Walleij0558ac52006-09-07 06:55:03 +000080 * Internal USB struct.
Linus Walleij2d411db2006-03-22 12:13:09 +000081 */
82typedef struct _PTP_USB PTP_USB;
83struct _PTP_USB {
Linus Walleij3e418e22007-09-15 20:30:14 +000084 PTPParams *params;
Linus Walleijfbbef8b2011-11-17 19:19:51 +010085#ifdef HAVE_LIBUSB1
86 libusb_device_handle* handle;
87#endif
88#ifdef HAVE_LIBUSB0
Linus Walleij7cf02642011-11-16 23:44:58 +010089 usb_dev_handle* handle;
Linus Walleijfbbef8b2011-11-17 19:19:51 +010090#endif
Darran Kartaschew11082d32011-12-13 00:42:01 +010091#ifdef HAVE_LIBOPENUSB
92 openusb_dev_handle_t* handle;
93#endif
Linus Walleij15d18e32012-08-20 02:00:34 +020094 uint8_t config;
Linus Walleijb0ab5482007-08-29 21:08:54 +000095 uint8_t interface;
Linus Walleij15d18e32012-08-20 02:00:34 +020096 uint8_t altsetting;
Linus Walleijc6210fb2006-05-08 11:11:41 +000097 int inep;
98 int inep_maxpacket;
99 int outep;
100 int outep_maxpacket;
101 int intep;
Linus Walleijd214b9b2006-08-26 22:08:37 +0000102 /** File transfer callbacks and counters */
103 int callback_active;
Linus Walleije04a1b92011-03-09 18:00:24 +0100104 int timeout;
105 uint16_t bcdusb;
Linus Walleijd214b9b2006-08-26 22:08:37 +0000106 uint64_t current_transfer_total;
107 uint64_t current_transfer_complete;
108 LIBMTP_progressfunc_t current_transfer_callback;
Linus Walleijee73ef22006-08-27 19:56:00 +0000109 void const * current_transfer_callback_data;
Linus Walleij0558ac52006-09-07 06:55:03 +0000110 /** Any special device flags, only used internally */
Linus Walleijfec4d562008-06-01 22:30:36 +0000111 LIBMTP_raw_device_t rawdevice;
Linus Walleij2d411db2006-03-22 12:13:09 +0000112};
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000113
Linus Walleijc6210fb2006-05-08 11:11:41 +0000114void dump_usbinfo(PTP_USB *ptp_usb);
Richard Lowe029eba2009-04-11 12:36:25 +0000115const char *get_playlist_extension(PTP_USB *ptp_usb);
Linus Walleij3c643252008-05-31 23:22:28 +0000116void close_device(PTP_USB *ptp_usb, PTPParams *params);
Linus Walleij549f49a2010-12-05 14:00:34 +0000117LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
Linus Walleij3c643252008-05-31 23:22:28 +0000118 PTPParams *params,
Linus Walleija700d222008-05-28 23:06:14 +0000119 void **usbinfo);
Linus Walleij2f622812008-08-30 22:06:58 +0000120void set_usb_device_timeout(PTP_USB *ptp_usb, int timeout);
121void get_usb_device_timeout(PTP_USB *ptp_usb, int *timeout);
Linus Walleije04a1b92011-03-09 18:00:24 +0100122int guess_usb_speed(PTP_USB *ptp_usb);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000123
Linus Walleijfec4d562008-06-01 22:30:36 +0000124/* Flag check macros */
125#define FLAG_BROKEN_MTPGETOBJPROPLIST_ALL(a) \
126 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL)
127#define FLAG_UNLOAD_DRIVER(a) \
128 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_UNLOAD_DRIVER)
129#define FLAG_BROKEN_MTPGETOBJPROPLIST(a) \
130 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST)
131#define FLAG_NO_ZERO_READS(a) \
132 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_NO_ZERO_READS)
133#define FLAG_IRIVER_OGG_ALZHEIMER(a) \
134 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_IRIVER_OGG_ALZHEIMER)
135#define FLAG_ONLY_7BIT_FILENAMES(a) \
136 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_ONLY_7BIT_FILENAMES)
137#define FLAG_NO_RELEASE_INTERFACE(a) \
138 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_NO_RELEASE_INTERFACE)
139#define FLAG_IGNORE_HEADER_ERRORS(a) \
140 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_IGNORE_HEADER_ERRORS)
141#define FLAG_BROKEN_SET_OBJECT_PROPLIST(a) \
142 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_BROKEN_SET_OBJECT_PROPLIST)
143#define FLAG_OGG_IS_UNKNOWN(a) \
144 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_OGG_IS_UNKNOWN)
145#define FLAG_BROKEN_SET_SAMPLE_DIMENSIONS(a) \
146 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_BROKEN_SET_SAMPLE_DIMENSIONS)
Linus Walleij335a81c2008-06-02 23:01:00 +0000147#define FLAG_ALWAYS_PROBE_DESCRIPTOR(a) \
148 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_ALWAYS_PROBE_DESCRIPTOR)
Linus Walleijf3c44052008-08-16 21:14:56 +0000149#define FLAG_PLAYLIST_SPL_V1(a) \
150 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_PLAYLIST_SPL_V1)
151#define FLAG_PLAYLIST_SPL_V2(a) \
152 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_PLAYLIST_SPL_V2)
153#define FLAG_PLAYLIST_SPL(a) \
154 ((a)->rawdevice.device_entry.device_flags & (DEVICE_FLAG_PLAYLIST_SPL_V1 | DEVICE_FLAG_PLAYLIST_SPL_V2))
Linus Walleijcf8dc2b2008-10-21 13:58:36 +0000155#define FLAG_CANNOT_HANDLE_DATEMODIFIED(a) \
156 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_CANNOT_HANDLE_DATEMODIFIED)
Linus Walleijf67c1ad2009-01-14 21:39:50 +0000157#define FLAG_BROKEN_SEND_OBJECT_PROPLIST(a) \
158 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_BROKEN_SEND_OBJECT_PROPLIST)
Linus Walleij4096c882009-03-16 23:32:34 +0000159#define FLAG_BROKEN_BATTERY_LEVEL(a) \
160 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_BROKEN_BATTERY_LEVEL)
Linus Walleij89bb1cd2009-07-24 21:03:36 +0000161#define FLAG_FLAC_IS_UNKNOWN(a) \
162 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_FLAC_IS_UNKNOWN)
Linus Walleij094b4502009-09-22 22:28:33 +0000163#define FLAG_UNIQUE_FILENAMES(a) \
164 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_UNIQUE_FILENAMES)
Linus Walleijfea4f532009-09-22 22:08:35 +0000165#define FLAG_SWITCH_MODE_BLACKBERRY(a) \
166 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_SWITCH_MODE_BLACKBERRY)
Linus Walleije04a1b92011-03-09 18:00:24 +0100167#define FLAG_LONG_TIMEOUT(a) \
168 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_LONG_TIMEOUT)
Linus Walleijf6913172011-03-10 22:10:26 +0100169#define FLAG_FORCE_RESET_ON_CLOSE(a) \
170 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_FORCE_RESET_ON_CLOSE)
Linus Walleij6ccbe962011-11-25 09:40:00 +0100171#define FLAG_BROKEN_GET_OBJECT_PROPVAL(a) \
172 ((a)->rawdevice.device_entry.device_flags & DEVICE_FLAG_BROKEN_GET_OBJECT_PROPVAL)
Linus Walleijfec4d562008-06-01 22:30:36 +0000173
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000174/* connect_first_device return codes */
175#define PTP_CD_RC_CONNECTED 0
176#define PTP_CD_RC_NO_DEVICES 1
177#define PTP_CD_RC_ERROR_CONNECTING 2
Linus Walleijd866d242009-08-23 21:50:39 +0000178
179#ifdef __cplusplus
180}
181#endif /* __cplusplus */
182
Linus Walleij7cf02642011-11-16 23:44:58 +0100183#endif // LIBUSB-GLUE_H