blob: edb0b3db42e88ad7976cc3c89861b097ac414ef1 [file] [log] [blame]
Linus Walleij0dd71e92006-05-04 18:47:07 +00001Building and Installing
2-----------------------
Linus Walleij6fd2f082006-03-28 07:19:22 +00003
Linus Walleij0dd71e92006-05-04 18:47:07 +00004See the "INSTALL" file.
5
6
Linus Walleij3f7529c2010-07-24 20:33:41 +00007Initiator and Responder
8-----------------------
9
10libmtp implements an MTP initiator, which means it initiate
11MTP sessions with devices. The devices responding are known
12as MTP responders. libmtp runs on something with a USB host
13controller interface, using libusb to access the host
14controller.
15
16If you're more interested in the MTP responders, gadgets like
17MP3 players, mobile phones etc, look into MeeGo:s Buteo Sync:
18http://wiki.meego.com/Buteo - these guys are creating a fully
19open source MTP responder.
20
21
Linus Walleij0dd71e92006-05-04 18:47:07 +000022Heritage
23--------
24
25libmtp is based on several ancestors:
26
27* libptp2 by Mariusz Woloszyn was the starting point used
28 by Richard A. Low for the initial starter port. You can
29 find it at http://libptp.sourceforge.net/
30
31* libgphoto2 by Mariusz Woloszyn and Marcus Meissner was
32 used at a later stage since it was (is) more actively
33 maintained. libmtp tracks the PTP implementation in
34 libgphoto2 and considers it an upstream project. We will
35 try to submit anything generally useful back to libgphoto2
36 and not make double efforts. In practice this means we
37 use ptp.c, ptp.h and ptp-pack.c verbatim from the libgphoto2
38 source code. If you need to change things in these files,
39 make sure it is so general that libgphoto2 will want to
40 merge it to their codebase too. You find libgphoto2 as part
41 of gPhoto: http://gphoto.sourceforge.net/
42
43* libnjb was a project that Richard and Linus were working
Linus Walleijfcf88912006-06-05 13:23:33 +000044 on before libmtp. When Linus took Richards initial port
Linus Walleij0dd71e92006-05-04 18:47:07 +000045 and made an generic C API he re-used the philosophy and
46 much code from libnjb. Many of the sample programs are for
47 example taken quite literally from libnjb. You find it here:
48 http://libnjb.sourceforge.net/
49
50
Linus Walleijea7d45b2009-02-23 22:26:09 +000051Contacting and Contributing
52---------------------------
53
54See the project page at http://libmtp.sourceforge.net/
55We always need your help. There is a mailinglist and a
56bug report system there.
57
58People who want to discuss MTP devices in fora seem to
59hang out on the forums at AnythingbutiPod:
60http://www.anythingbutipod.com/forum/
61
62
Linus Walleij0dd71e92006-05-04 18:47:07 +000063Compiling programs for libmtp
64-----------------------------
65
66libmtp has support for the pkg-config script by adding a libmtp.pc
67entry in $(prefix)/lib/pkgconfig. To compile a libmtp program,
68"just" write:
69
70gcc -o foo `pkg-config --cflags --libs libmtp` foo.c
71
72This also simplifies compilation using autoconf and pkg-config: just
73write e.g.
74
75PKG_CHECK_MODULES(MTP, libmtp)
76AC_SUBST(MTP_CFLAGS)
77AC_SUBST(MTP_LIBS)
78
79To have libmtp LIBS and CFLAGS defined. Needless to say, this will
80only work if you have pkgconfig installed on your system, but most
81people have nowadays.
82
83If your library is installed in e.g. /usr/local you may have to tell
84this to pkgconfig by setting the PKG_CONFIG_PATH thus:
85
86export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
87
88
89Documentation
90-------------
91
92Read the API documentation that can be generated with doxygen.
93It will be output in doc/html if you have Doxygen properly
94installed. (It will not be created unless you have Doxygen!)
95
96For information about the Media Transfer Protocol, see:
97http://en.wikipedia.org/wiki/Media_Transfer_Protocol
98
Linus Walleij7a83e552008-07-29 21:30:43 +000099The official 1.0 specification for MTP was released by the
100USB Implementers Forum in may, 2008. Prior to this, only a
101proprietary Microsoft version was available, and quite a few
102devices out there still use some aspects of the Microsoft
103version, which deviates from the specified standard. You can
104find the official specification here:
105http://www.usb.org/developers/devclass_docs/MTP_1.0.zip
Linus Walleij0dd71e92006-05-04 18:47:07 +0000106
Linus Walleij1b91ca62008-10-17 07:07:56 +0000107
108The Examples
109------------
110
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000111In the subdirectory "examples" you find a number of
Linus Walleij1b91ca62008-10-17 07:07:56 +0000112command-line tools, illustrating the use of libmtp in very
113simple terms.
114
115Please do not complain about the usability or documentation
116of these examples, they look like they do for two reasons:
117
1181. They are examples, not tools. If they were intended for
119 day-to-day usage by commandline freaks, I would have
120 called them "tools" not "examples".
121
1222. The MTP usage paradigm is that a daemon should hook
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000123 the device upon connection, and that it should be
Linus Walleij1b91ca62008-10-17 07:07:56 +0000124 released by unplugging. GUI tools utilizing HAL (hald)
125 and D-Bus do this much better than any commandline
126 program ever can. (See below on bugs.) Specificationwise
127 this is a bug, however it is present in many, many
128 devices.
129
130That said, if you want to pick up and maintain the examples,
131please volunteer.
132
133
Linus Walleijc36e2a62011-12-29 21:31:49 +0100134FAQ: Common Problems
135--------------------
136
137Some MTP devices have strange pecularities. We try to work around
138these whenever we can, sometimes we cannot work around it or we
139cannot test your solution.
140
141* mtp-* tools doesn't work because someone else is already hogging
142 the device
143
144 This is a common problem, the most common case could be that
145 gphoto2 (which can also talk PTP/MTP) is taking over the device
146 as soon as it's plugged in. Some distributions are configured that
147 way. Counter it like this:
148
149 gvfs-mount -s gphoto2
150
151 Then re-attach the device.
152
153* Generic MTP/PTP disconnect misbehaviour: we have noticed that
154 Windows Media Player apparently never close the session to an MTP
155 device. There is a daemon in Windows that "hooks" the device
156 by opening a PTP session to any MTP device, whenever it is
157 plugged in. This daemon proxies any subsequent transactions
158 to/from the device and will never close the session, thus
159 Windows simply does not close sessions at all.
160
161 Typical sign of this illness: broken pipes on closing sessions,
162 on the main transfer pipes(s) or the interrupt pipe:
163
164 Closing session
165 usb_clear_halt() on INTERRUPT endpoint: Broken pipe
166 OK.
167
168 This means that device manufacturers doesn't notice any problems
169 with devices that do not correctly handle closing PTP/MTP
170 sessions, since Windows never do it. The proper way of closing
171 a session in Windows is to unplug the device, simply put.
172
173 Since libmtp actually tries to close sessions, some devices
174 may fail since the close session functionality has never been
175 properly tested, and "it works with Windows" is sort of the
176 testing criteria at some companies.
177
178 You can get Windows-like behaviour on Linux by running a HAL-aware
179 libmtp GUI client like Rhythmbox or Gnomad2, which will "hook"
180 the device when you plug it in, and "release" it if you unplug
181 it.
182
183 If this bug in your device annoys you, contact your device
184 manufacturer and ask them to test their product with some libmtp
185 program.
186
187* Generic USB misbehaviour: some devices behave badly under MTP
188 and USB mass storage alike, even down to the lowest layers
189 of USB. You can always discuss such issues at the linux-usb
190 mailing list if you're using Linux:
191 http://www.linux-usb.org/mailing.html
192
193 If you have a problem specific to USB mass storage mode, there
194 is a list of strange behaving devices in the Linux kernel:
195 http://lxr.linux.no/linux/drivers/usb/storage/unusual_devs.h
196 You can discuss this too on the mentioned list, for understanding
197 the quirks, see:
198 http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt
199
200* Generic certificate misbehaviour. All devices are actually
201 required to support a device certificate to be able to
202 encrypt Windows Media (WMA/WMV) files. However there are
203 obviously a lot of devices out there which doesn't support
204 this at all but instead crash. Typical printout:
205
206 Error 2: PTP Layer error 02ff: get_device_unicode_property(): failed
207 to get unicode property.
208
209 This should only affect "mtp-detect", there is no other
210 application currently retrieveing the certificate (not that we
211 know anyway).
212
213* Kernel bug on Linux. Linux 2.6.16 is generally speaking required
214 to use any MTP device under USB 2.0. This is because the EHCI
215 driver previously did not support zero-length writes to endpoints.
216 It should work in most cases however, or if you connect it
217 to an UHCI/OHCI port instead (yielding lower speed). But
218 please just use a recent kernel.
219
220* Zen models AVI file seeking problem: the Zens cannot parse the
221 files for the runlength metadata. Do not transfer file with e.g.
222 mtp-sendfile, use mtp-sendtr and set the length of the track to
223 the apropriate number of seconds and it will work. In graphical
224 clients, use a "track transfer" function to send these AVI files,
225 the Zens need the metadata associated with tracks to play back
226 movies properly. Movies are considered "tracks" in the MTP world.
227
228* Some devices that disregard the metadata sent with the MTP
229 commands will parse the files for e.g. ID3 metadata. Some still
230 of these devices expect only ID3v2.3 metadata and will fail with
231 a modern ID3v2,4 tag writer, like many of those found in Linux
232 applications. Windows Media Player use ID3v2.3 only, so many
233 manufacturers only test this version.
234
235* The Zen Vision:M (possibly more Creative Zens) has a firmware bug
236 that makes it drop the last two characters off a playlist name.
237 It is fixed in later firmware.
238
239* For Creative Technology devices, there are hard limits on how
240 many files can be put onto the device. For a 30 GiB device (like
241 the Zen Xtra) the limit is 6000, for a 60 GiB device the limit
242 is 15000 files. For further Creative pecularities, see the
243 FAQ sections at www.nomadness.net.
244
245* Sandisk sansa c150 and probably several other Sandisk devices
246 (and possibly devices from other manufacturers) have a dual
247 mode with MTP and USB mass storage. The device will initially
248 claim to be mass storage so udev will capture is and make the
249 use of MTP mode impossible. One way of avoiding it could be to
250 be to blacklist the "usb-storage" module in
251 /etc/modprobe.c/blacklist with a row like this:
252 "blacklist usb-storage". Some have even removed the
253 "usb-storage.ko" (kernel module file) to avoid loading.
254
255* Sandisk Sansa Fuze has three modes: auto, MTP or mass storage
256 (MSC). Please set it to MTP to avoid problems with libmtp.
257
258* The iriver devices (possibly all of them) cannot handle the
259 enhanced GetObjectPropList MTP command (0x9805) properly. So
260 they have been banned from using it.
261
262* iriver devices have problems with older versions of libmtp and
263 with new devices libmtp does not know of as of yet, since it
264 has an oldstyle USB device controller that cannot handle zero
265 writes. (Register your device with us!) All their devices are
266 likely to need a special device flag in the src/libusb-glue.c
267 database.
268
269* The Samsung Yepp T9 has several strange characteristics, some
270 that we've managed to work around. (For example it will return
271 multiple PTP packages in a single transaction.)
272
273* The early firmware for Philips HDD players is known to be
274 problematic. Please upgrade to as new firmware as you can get.
275 (Yes this requires some kind of Windows Installation I think.)
276
277* Philips HDD 1630/16 or 1630/17 etc may lock themselves up,
278 turning inresponsive due to internal corruption. This typically
279 gives an error in opening the PTP session. Apparently you can
280 do a "repair" with the firmware utility (Windows only) which
281 will often fix this problem and make the device responsive
282 again.
283
284* Some devices that implement GetObjectPropList (0x9805) will
285 not return the entire object list if you request a list for object
286 0xffffffffu. (But they should.) So they may need the special
287 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL.
288
289* Some (smaller) subset of devices cannot even get all the
290 properties for a single object in one go, these need the
291 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST. Currently only the
292 iriver devices seem to have this bug.
293
294* The Toshiba Gigabeat S (and probably its sibling the
295 Microsoft Zune and other Toshiba devices) will only display
296 album information tags for a song in case there is also
297 an abstract album (created with the album interface) with
298 the exact same name.
299
300* The Zen Vision:M has an older firmware which is very corrupt,
301 it is incompatible with the Linux USB stack altogether. The
302 kernel dmesg will look something like this, and you have to
303 upgrade the firmware using Windows:
304 usb 4-5: new high speed USB device using ehci_hcd and address 5
305 usb 4-5: configuration #1 chosen from 1 choice
306 usb 4-5: can't set config #1, error -110
307
308* The Sirus Stiletto does not seem to allow you to copy any files
309 off the device. This may be someone's idea of copy protection.
310
311* The Samsung P2 assigns parent folder ID 0 to all unknown file
312 types.(i.e. moves them to the root folder)
313
314* The Sandisk Sansa Clip+ needs a firmware upgrade in earlier
315 versions in order to work properly.
316
317
Linus Walleija1b66f22007-05-10 20:02:16 +0000318New Devices
319-----------
320
Linus Walleijfcf88912006-06-05 13:23:33 +0000321If you happen upon a device which libmtp claims it cannot
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000322autodetect, please submit the vendor ID and device ID
Linus Walleij9ee29402007-10-31 20:24:48 +0000323(these can be obtained from the "lsusb" and "lsusb -n"
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000324commands run as root) as a bug, patch or feature request
325on the Sourceforge bug tracker at our homepage. If it
326gives a sensible output from "mtp-detect" then please attach
327the result as well as it teach us some stuff about your
328device. If you've done some additional hacking, join our
Linus Walleij9ee29402007-10-31 20:24:48 +0000329mailinglist and post your experiences there.
Linus Walleijda558be2007-03-10 21:42:25 +0000330
Linus Walleija1b66f22007-05-10 20:02:16 +0000331If you want to be able to hack some more and you're not
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000332afraid of C hacking, add an entry for your device's
333vendor/product ID and a descriptive string to the database
Linus Walleij6dc01682007-11-15 21:23:46 +0000334in the file src/music-players.h.
Linus Walleija1b66f22007-05-10 20:02:16 +0000335
336If you want to poke around to see if your device has some
337special pecularities, you can test some special device
Linus Walleij6dc01682007-11-15 21:23:46 +0000338flags (defined in src/device-flags.h) by inserting them
339together with your device entry in src/music-players.h.
Linus Walleija1b66f22007-05-10 20:02:16 +0000340Flags can be tested in isolation or catenated with "|"
341(binary OR). If relatives to your device use a certain
342flag, chances are high that a new device will need it
343too, typically from the same manufacturer.
344
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000345The most common flag that needs to be set is the
Linus Walleija1b66f22007-05-10 20:02:16 +0000346DEVICE_FLAG_UNLOAD_DRIVER that detach any Linux kernel
347drivers that may have attached to the device making
Linus Walleij94f23d52007-08-04 19:37:28 +0000348MTP access impossible. This is however not expected to
349really work: this is a problem being tracked as of
350now (2007-08-04). See the "last resort" solutions below
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000351if you really need to get your dual-mode device to work
Linus Walleij94f23d52007-08-04 19:37:28 +0000352with MTP.
Linus Walleija1b66f22007-05-10 20:02:16 +0000353
Linus Walleijcc2cf972007-11-22 20:23:43 +0000354Another flag which is easy to identify is the
355DEVICE_FLAG_NO_ZERO_READS, which remedies connection
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000356timeouts when getting files, and some timeouts on e.g.
Linus Walleijcc2cf972007-11-22 20:23:43 +0000357successive "mtp-connect" calls.
358
Linus Walleija1b66f22007-05-10 20:02:16 +0000359If your device is very problematic we are curious of how it
Linus Walleijda558be2007-03-10 21:42:25 +0000360works under Windows, so we enjoy reading USB packet sniffs
361that reveal the low-level traffic carried out between
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000362Windows Media Player and your device. This can be done
Linus Walleij61c25682007-09-04 14:46:21 +0000363using e.g.:
364
365* USBsnoop:
366 http://benoit.papillault.free.fr/usbsnoop/
367
368* The trial version of HHD Softwares software-only
369 USB monitor. You need to get a copy of version 2.37 since
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000370 the newer trial versions won't let you carry out the
Linus Walleij61c25682007-09-04 14:46:21 +0000371 needed packet sniffs. (As of 2007-03-10 a copy can be found
372 at: http://www.cobbleware.com/files/usb-monitor-237.exe)
373
Linus Walleijda558be2007-03-10 21:42:25 +0000374There are other USB monitors as well, some more expensive
375alternatives use hardware and even measure electronic
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000376characteristics of the traffic (which is far too much
Linus Walleijda558be2007-03-10 21:42:25 +0000377detail for us).
378
Linus Walleij91fb0282007-09-03 21:16:08 +0000379Device sniffs are an easy read since the PTP/MTP protocol
380is nicely structured. All commands will have a structure such
381as this in the log, we examplify with a object list request:
382
383PTP REQEUST:
384000120: Bulk or Interrupt Transfer (UP), 03.09.2007 12:49:25.9843750 +0.0
385Pipe Handle: 0x863ce234 (Endpoint Address: 0x2)
386Send 0x20 bytes to the device:
387 20 00 00 00 01 00 05 98 23 00 00 00 27 03 00 10 ......?#...'...
388 Length TYPE CMD Trans# Param1
389
390 00 00 00 00 02 DC 00 00 00 00 00 00 00 00 00 00 .....Ü..........
391 Param2 Param3 Param4 Param5
392
393[OPTIONAL] DATA PHASE:
394000121: Bulk or Interrupt Transfer (UP), 03.09.2007 12:49:26.0 +0.0156250
395Pipe Handle: 0x863ce214 (Endpoint Address: 0x81)
396Get 0x1a bytes from the device:
397 1A 00 00 00 02 00 05 98 23 00 00 00 01 00 00 00 .......?#.......
398 Length TYPE CMD Trans# DATA
399
400 27 03 00 10 02 DC 04 00 00 30 '....Ü...0
401
402RESPONSE:
403000122: Bulk or Interrupt Transfer (UP), 03.09.2007 12:49:26.0 +0.0
404Pipe Handle: 0x863ce214 (Endpoint Address: 0x81)
405Get 0xc bytes from the device:
406 0C 00 00 00 03 00 01 20 23 00 00 00 ....... #...
407 Length TYPE CODE Trans#
408
409* One send (OUT to the device), two reads (IN from the device).
410
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000411* All three byte chunks commands are
412 sent/recieved/recieeved by the function ptp_transaction()
Linus Walleij91fb0282007-09-03 21:16:08 +0000413 in the file ptp.c.
414
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000415* It boils down to ptp_usb_sendreq(), optionally ptp_usb_senddata()
416 or ptp_usb_getdata() and finally ptp_usb_getresp() in the file
Linus Walleij91fb0282007-09-03 21:16:08 +0000417 libusb-glue.c. Notice ptp_usb_sendreq() and ptp_usb_getresp()
418 are ALWAYS called. The TYPE field correspond to this, so the
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000419 TYPES in this case are "COMMAND" (0x0001), "DATA" (0x0002),
Linus Walleij91fb0282007-09-03 21:16:08 +0000420 and "RESPONSE" (0x0003).
421
422* Notice that the byte order is little endian, so you need to read
423 each field from right to left.
424
425* This COMMAND has:
426 CMD 0x99805, we see in ptp.h that this is PTP_OC_MTP_GetObjPropList.
427 Transaction# 0x00000023.
428 REQUEST parameters 0x10000327, 0x00000000, 0x0000DC02, 0x00000000
429 0x00000000, in this case it means "get props for object 0x10000327",
430 "any format", "property 0xDC02" (PTP_OPC_ObjectFormat), then two
431 parameters that are always zero (no idea what they mean or their
432 use).
433
434* The DATA has:
435 CMD 0x99805, we see in ptp.h that this is PTP_OC_MTP_GetObjPropList.
436 Transaction# 0x00000023.
437 Then comes data 0x00000001, 0x10000327, 0xDC02, 0x0004, 0x3000
438 Which means in this case, (and this is the tricky part) "here
439 you have 1 property", "for object 0x10000327", "it is property
440 0xDC02" (PTP_OPC_ObjectFormat), "which is of type 0x0004"
441 (PTP_DTC_UINT16), "and set to 0x3000" (PTP_OFC_Undefined, it
442 is perfectly valid to have undefined object formats, since it
443 is a legal value defining this).
444
445* This RESPONSE has:
446 CMD 0x99805, we see in ptp.h that this is PTP_OC_MTP_GetObjPropList.
447 Return Code ("RC") = 0x2001, PTP_RC_OK, all went fine.
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000448 Transaction# 0x00000023.
Linus Walleijfcf88912006-06-05 13:23:33 +0000449
Linus Walleijd05fce62007-09-29 20:17:23 +0000450If you want to compare the Windows behaviour with a similar
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000451operation using libmtp you can go into the src/libusb-glue.c
Linus Walleij6dc01682007-11-15 21:23:46 +0000452file and uncomment the row that reads:
Linus Walleijd05fce62007-09-29 20:17:23 +0000453
454//#define ENABLE_USB_BULK_DEBUG
455
456(I.e. remove the two //.)
457
458This will make libmtp print out a hex dump of every bulk USB
459transaction. The bulk transactions contain all the PTP/MTP layer
460data, which is usually where the problems appear.
461
Linus Walleij6fd2f082006-03-28 07:19:22 +0000462
Darran Kartaschewa476ae92011-08-08 09:07:30 +0200463Notes to assist with debugging new devices:
464-------------------------------------------
465
Linus Walleijc36e2a62011-12-29 21:31:49 +0100466In debugging new hardware, we highly recommend that you only
Darran Kartaschewa476ae92011-08-08 09:07:30 +0200467use the example mtp-* applications that come with libmtp, as other
468applications may have their own bugs that may interfere with your
469new device working correctly. Using another application instead of
470those that come with libmtp just adds another point of failure.
471
472For debugging, there are 3 main options:
473
Linus Walleijc36e2a62011-12-29 21:31:49 +01004741. Use the env variable: LIBMTP_DEBUG to increase the
475verboseness of the debugging output for any application using
Darran Kartaschewa476ae92011-08-08 09:07:30 +0200476libmtp. Relevant codes are:
477* 0x00 [0000 0000] : no debug (default)
478* 0x01 [0000 0001] : PTP debug
479* 0x02 [0000 0010] : Playlist debug
480* 0x04 [0000 0100] : USB debug
481* 0x08 [0000 1000] : USB data debug
482// Codes are hex and binary respectively. Simple add them togther
483// to get your desired level of output.
484
485(Assuming bash)
Linus Walleijc36e2a62011-12-29 21:31:49 +0100486eg:
Darran Kartaschewa476ae92011-08-08 09:07:30 +0200487$ export LIBMTP_DEBUG=12
488$ mtp-detect
489 // To get USB debug and USB data debug information.
490
491$ export LIBMTP_DEBUG=2
492$ mtp-detect
493 // To get Playlist debug information.
Linus Walleijc36e2a62011-12-29 21:31:49 +0100494
Darran Kartaschewa476ae92011-08-08 09:07:30 +0200495Also note, an application may also use the LIBMTP_debug() API
496function to achieve the same options as listed above.
497
Linus Walleijc36e2a62011-12-29 21:31:49 +01004982. Use "strace" on the various mtp-* commands to see where/what
Darran Kartaschewa476ae92011-08-08 09:07:30 +0200499is falling over or getting stuck at.
500* On Solaris and FreeBSD, use "truss" or "dtrace" instead on "strace".
501* On Mac OS X, use "ktrace" or "dtrace" instead of "strace".
502* On OpenBSD and NetBSD, use "ktrace" instead of "strace".
503
504This will at least help pinpoint where the application is failing, or
Linus Walleijc36e2a62011-12-29 21:31:49 +0100505a device is reporting incorrect information. (This is extremely helpful
Darran Kartaschewa476ae92011-08-08 09:07:30 +0200506with devices that have odd disconnection requirements).
507
Linus Walleijc36e2a62011-12-29 21:31:49 +0100508The use of these tools may also pinpoint issues with libusb as
Darran Kartaschewa476ae92011-08-08 09:07:30 +0200509implemented by each OS vendor or issues with the MTP implementation
510on the new device as well, so please be prepared for either case.
511
5123. Use "gdb" or similar debugger to step through the code as it is
513run. This is time consuming, and not needed just to pinpoint where
Linus Walleijc36e2a62011-12-29 21:31:49 +0100514the fault is.
Darran Kartaschewa476ae92011-08-08 09:07:30 +0200515
Linus Walleijc36e2a62011-12-29 21:31:49 +0100516The use of gdb or another debugger may also miss or actually cause
517command and data timing issues with some devices, leading to false
Darran Kartaschewa476ae92011-08-08 09:07:30 +0200518information. So please consider this a last resort option.
519
520Also please read the "It's Not Our Bug!" section below, as it does
521contain some useful information that may assist with your device.
522
523
Linus Walleij8d799eb2009-07-23 22:58:06 +0000524Dual-mode devices does not work - last resort:
525----------------------------------------------
Linus Walleijbd7624c2007-05-28 10:48:54 +0000526
527Some devices that are dual-mode are simply impossible to get
528to work under Linux because the usb-storage(.ko) kernel
529module hook them first, and refuse to release them, even
Linus Walleij94f23d52007-08-04 19:37:28 +0000530when we specify the DEVICE_FLAG_UNLOAD_DRIVER flag. (Maybe
531it DOES release it but the device will immediately be probed
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000532at the USB mass storage interface AGAIN because it
Linus Walleij94f23d52007-08-04 19:37:28 +0000533enumerates.)
Linus Walleijbd7624c2007-05-28 10:48:54 +0000534
Linus Walleij8d799eb2009-07-23 22:58:06 +0000535Here is what some people do:
536
537 1. Plug in the device.
538 2. USB-mass storage folder will open automatically.
539 3. Unmount the device.
540 4. Run mtp-detect. It will most likely fail the first time.
541 5. Run mtp-detect again, it might work this time, or fail. Keep running
542 till it works. 99% it works by the third try.
543 6. Once mtp-detect gives you an "Ok", open either Rhythmbox or Gnomad2,
544 everything should work.
545
Linus Walleije20abaf2007-12-10 11:20:34 +0000546Linux: Try this, if you have a recent 2.6.x Linux kernel,
Linus Walleij584eb8d2007-09-05 19:51:27 +0000547run (as root) something like:
548
549> rmmod usb_storage ; mtp-detect
550
551You can run most any command or a client like gnomad2 or
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000552Amarok immediately after the rmmod command. This works
Linus Walleij584eb8d2007-09-05 19:51:27 +0000553sometimes. Another way:
Linus Walleijbd7624c2007-05-28 10:48:54 +0000554
Linus Walleij94f23d52007-08-04 19:37:28 +0000555* Edit /etc/modprobe.d/blacklist
Linus Walleijbd7624c2007-05-28 10:48:54 +0000556
557* Add the line "blacklist usb-storage"
558
559* Reboot.
560
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000561Now none of you USB disks, flash memory sticks etc will be
Linus Walleijbd7624c2007-05-28 10:48:54 +0000562working (you just disabled them all). However you *can* try
563your device, and it might have started working because there
564is no longer a USB mass storage driver that tries to hook onto
565the mass storage interface of your device.
566
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000567If not even blacklisting works (check with
Linus Walleij94f23d52007-08-04 19:37:28 +0000568"lsmod | grep usb-storage"), there is some problem with
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000569something else and you may need to remove or rename the file
Linus Walleij94f23d52007-08-04 19:37:28 +0000570/lib/modules/<VERSION>/kernel/drivers/usb/storage/usb-storage.ko
571manually.
572
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000573If you find the PerfectSolution(TM) to this dilemma, so you
Linus Walleijbd7624c2007-05-28 10:48:54 +0000574can properly switch for individual devices whether to use it
575as USB mass storage or not, please tell us how you did it. We
576know we cannot use udev, because udev is called after-the-fact:
577the device is already configured for USB mass storage when
578udev is called.
579
Linus Walleije20abaf2007-12-10 11:20:34 +0000580On Mac OS there is another ugly hack:
581
5821. Open up a terminal window
5832. Type:
584sudo mv /System/Library/Extensions/IOUSBMassStorageClass.kext
585/System/Library/Extensions/IOUSBMassStorageClass.kext.disabled
586
587and when prompted enter your password.
588
5893. Restart.
590
591To reverse this change, just reverse the filenames:
592
593sudo mv /System/Library/Extensions/
594IOUSBMassStorageClass.kext.disabled /System/Library/Extensions/
595IOUSBMassStorageClass.kext
596
597and restart.
598
Linus Walleijbd7624c2007-05-28 10:48:54 +0000599
Linus Walleij15def332006-09-19 14:27:02 +0000600Calendar and contact support:
601-----------------------------
Linus Walleijd3bdf762006-02-20 22:21:56 +0000602
Linus Walleij3c16fe42006-04-30 07:53:41 +0000603The Creative Zen series can read VCALENDAR2 (.ics) files
Linus Walleij15def332006-09-19 14:27:02 +0000604and VCard (.vcf) files from programs like for example
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000605Evolution with the following limitations/conditions:
Linus Walleijd3bdf762006-02-20 22:21:56 +0000606
Linus Walleij3c16fe42006-04-30 07:53:41 +0000607- The file must be in DOS (CR/LF) format, use the unix2dos
608 program to convert if needed
Linus Walleij15def332006-09-19 14:27:02 +0000609
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000610- Repeat events in calendar files do not seem to be supported,
Linus Walleij15def332006-09-19 14:27:02 +0000611 entries will only appear once.
612
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000613- Calendar (.ics) files should be stored in the folder "My Organizer"
Linus Walleij15def332006-09-19 14:27:02 +0000614 when sent to the device (this directory should be autodetected
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000615 for use with calendar files, otherwise use the option
Linus Walleij15def332006-09-19 14:27:02 +0000616 -f "My Organizer" to sendfile for this) Apparently this file can
617 also contain tasklists.
618
619- Contact (.vcf) files should be stored in the folder "My Contacts"
620 when sent to the device. (-f "My Contacts")
621
622- Some devices are picky about the name of the calendar and
623 contact files. For example the Zen Microphoto wants:
624
Linus Walleijb1318d12006-09-25 14:59:26 +0000625 Calendar: My Organizer/6651416.ics
626 Contacts: My Organizer/6651416.vcf
627
628
629Syncing in with Evolution and Creative Devices
630----------------------------------------------
631
632Evolution can easily export .ics an .vcf files, but you currently
633need some command-line hacking to get you stuff copied over in
634one direction host -> device. The examples/ directory contains a script
635created for the Creative Zen Microphoto by Nicolas Tetreault.
636
Linus Walleij6e8cef42006-12-03 20:45:04 +0000637
Linus Walleijd132d8e2007-04-03 23:24:54 +0000638Lost symbols
639------------
640
641Shared libraries can be troublesome to users not experienced with
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000642them. The following is a condensed version of a generic question
Linus Walleijd132d8e2007-04-03 23:24:54 +0000643that has appeared on the libmtp mailing list from time to time.
644
645> PTP: Opening session
646> Queried Creative Zen Vision:M
647> gnomad2: relocation error: gnomad2: undefined symbol:
648> LIBMTP_Get_Storageinfo
649> (...)
650> Are these type of errors related to libmtp or something else?
651
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000652The problem is of a generic nature, and related to dynamic library
Linus Walleijd132d8e2007-04-03 23:24:54 +0000653loading. It is colloquially known as "dependency hell".
654(http://en.wikipedia.org/wiki/Dependency_hell)
655
656The gnomad2 application calls upon the dynamic linker in Linux to
657resolve the symbol "LIBMTP_Get_Storageinfo" or any other symbol
658(ELF symbol, or link point or whatever you want to call them, a
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000659symbol is a label on a memory address that the linker shall
Linus Walleijd132d8e2007-04-03 23:24:54 +0000660resolve from label to actual address.)
661For generic information on this subject see the INSTALL file and
662this Wikipedia page:
663
664http://en.wikipedia.org/wiki/Library_(computing)
665
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000666When Linux /lib/ld-linux.so.X is called to link the symbols compiled
667into gnomad2 (or any other executable using libmtp), it examines the
668ELF file for the libmtp.so.X file it finds first and cannot resolve
Linus Walleijd132d8e2007-04-03 23:24:54 +0000669the symbol "LIBMTP_Get_Storageinfo" (or whichever symbol you have a
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000670problem witj) from it, since it's probably not there. There are many
Linus Walleijd132d8e2007-04-03 23:24:54 +0000671possible causes of this symbol breakage:
672
6731) You installed precompiled libmtp and gnomad2 packages (RPMs, debs
674 whatever) that do not match up. Typical cause: your gnomad2 package was
675 built against a newer version of libmtp than what's installed on your
676 machine. Another typical cause: you installed a package you found on
677 the web, somewhere, the dependency resolution system did not protest
678 properly (as it should) or you forced it to install anyway, ignoring
679 some warnings.
680
6812) You compiled libmtp and/or gnomad2 from source, installing both or
682 either in /usr/local/lib and /usr/local/bin. This means at compile-time
683 gnomad2 finds the libmtp library in /usr/local/lib but at runtime, it
684 depends on the Linux system wide library loader (/lib/ld-linux.so.X) in
685 order to resolve the symbols. This loader will look into the file
686 /etc/ld.so.conf and/or the folder /etc/ld.so.conf.d in order to find
687 paths to libraries to be used for resolving the symbols. If you have
688 some older version of libmtp in e.g. /usr/lib (typically installed by a
689 package manager) it will take precedence over the new version you just
690 installed in /usr/local/lib and the newly compiled library in
691 /usr/local/lib will *not* be used, resulting in this error message.
692
6933) You really did install the very latest versions (as of writing libmtp
694 0.1.5 and gnomad2 2.8.11) from source and there really is no
695 pre-installed package of either on your machine. In that case I'm
696 totally lost, I have no idea what's causing this.
697
698Typical remedies:
699
7001) If you don't want to mess around with your system and risk these
701 situations, only use pre-packaged software that came with the
702 distribution or its official support channels. If it still breaks,
703 blame your distribution, they're not packaging correctly. Relying on
704 properly packaged software and not installing things yourself *is* the
705 Linux solution to the "dependency hell" problem.
706
7072) Read about dynamically linked library handling until the stuff I wrote
708 about in the previous list sounds like music to your ears, inspect
709 your /lib, /usr/lib, /usr/local/lib, /etc/ld.so.conf and the
710 /etc/ld.so.conf.d, remove all pre-packed versions using RPM, APT,
711 YaST or whatever your distribution uses, compile libmtp and gnomad2
712 (or whatever) from source only and you will be enlighted.
713
714I don't know if this helps you, it's the best answer we can give.
Linus Walleij387e37a2008-10-29 17:22:22 +0000715
716
717API is obscure - I want plain files!
718------------------------------------
719
720PTP/MTP devices does not actually contain "files", they contain
721objects. These objects have file names, but that is actually
722just a name tag on the object.
723
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000724Folders/directories aren't really such entities: they are just
Linus Walleij387e37a2008-10-29 17:22:22 +0000725objects too, albeit objects that can act as parent to other
Linus Walleij8aba06d2008-12-28 08:26:57 +0000726objects. They are called "associations" and are created in atomic
Linus Walleij387e37a2008-10-29 17:22:22 +0000727fashion and even though there is an MTP command to get all the
Linus Walleij8aba06d2008-12-28 08:26:57 +0000728associations of a certain association, this command is optional
729so it is perfectly possible (and most common, actually) to create
730devices where the "folders" (which are actually associations) have
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000731no idea whatsoever of what files they are associated as parents to
Linus Walleij387e37a2008-10-29 17:22:22 +0000732(i.e. which files they contain). This is very easy for device
Linus Walleij8aba06d2008-12-28 08:26:57 +0000733manufacturers to implement, all the association (i.e. finding out
734which files are in a certain folder) has to be done by the MTP
735Initiator / host computer.
Linus Walleij387e37a2008-10-29 17:22:22 +0000736
737Moving a file to a new folder is for example very simple in a
738"real" file system. In PTP/MTP devices it is often not even possible,
739some devices *may* be able to do that. But actually the only
740reliable way of doing that is to upload the file to the host,
Linus Walleij8aba06d2008-12-28 08:26:57 +0000741download it with the new parent, then delete the old file.
Linus Walleij387e37a2008-10-29 17:22:22 +0000742We have played with the idea of implementing this time consuming
743function, perhaps we will.
744
Linus Walleij8aba06d2008-12-28 08:26:57 +0000745Then the issue that in PTP/MTP it is legal for two files to have
746exactly the same path as long as their object IDs differ. A
747folder/association can contain two files with the exact same name.
748(And on the Creative devices this even works, too, though most devices
749implicitly fail at this.) Perhaps one could add some custom hook for
750handling that, so they become /Foo.mp3 and /Foo.mp3(1) or something
751similar, but it's really a bit kludgy.
752
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000753Playlists and albums aren't really files, thinking about
Linus Walleij387e37a2008-10-29 17:22:22 +0000754them as files like the hacks in libgphoto2 is really backwards. They are
755called associations and are more like a symbolic link that links in a
756star-shaped pattern to all the files that are part of the album/playlist.
757Some devices (Samsung) thought that was too complicated and have a
758different way of storing playlists in an UTF-16 encoded .spl-like file
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000759instead! This is why playlists/albums must have their own structs and
Linus Walleij387e37a2008-10-29 17:22:22 +0000760functions.
761
Linus Walleij8aba06d2008-12-28 08:26:57 +0000762Plain file access also assumes to be able to write files of an
763undetermined size, which is simply not possible in a transactional
764file system like PTP/MTP. (See further below.)
765
Linus Walleij387e37a2008-10-29 17:22:22 +0000766
767I Want Streaming!
768-----------------
769
770Streaming reads is easy. Just connect the output file descriptor from
771LIBMTP_Get_File_To_File_Descriptor() (and a similar function for tracks)
772wherever you want.
773
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000774People have connected this to TCP sockets for streaming web servers
775etc, works like a charm. Some devices will even survive if the callback
Linus Walleij387e37a2008-10-29 17:22:22 +0000776functions return non-zero and cancel the download. Some devices will
777lock up and even require a reset if you do that. Devices are poorly
778implemented so that's life. If you want to stream off a device, the
779best idea is always to stream the entire file and discard the stuff
780at the end you don't want. It will incur a delay if you e.g. want to
781skip between tracks, sadly.
782
783Then we get to the complicated things: streaming WRITES...
784
785There is a function:
786LIBMTP_Send_File_From_File_Descriptor() (and similar for tracks)
787which will write a file to a device from a file descriptor, which may
788be a socket or whatever.
789
790HOWEVER: this requires a piece of metadata with the .filesize properly
791set first.
792
793This is not because we think it is funny to require that, the protocol
794requires it. The reason is that PTP/MTP is a transactional file system
795and it wants to be able to deny file transfer if the file won't fit on
796the device, so the transaction never even starts, it's impossible to
797start a transaction without giving file length.
798
799People really want streaming so I tried a lot of hacks to see if they
800would work, such as setting file size to 0xffffffffU or something other
801unnaturally big and then aborting the file transfer when the stream ends.
802It doesn't work: either the device crashes or the file simply disappears
803since the device rolls back all failed transactions.
804
805So this is an inherent limitation of the PTP/MTP protocol.
Linus Walleijbe8b03b2009-10-16 21:19:35 +0000806
807
808I make MTP devices!
809-------------------
810
811If you are a device vendor there is a lot you can do for libmtp:
812
813* Please consider assigning one of your employees as a contact person
814 for libmtp, have them sign up to the libmtp development list and answer
815 questions and post new device ID:s as they are released to our
816 mailing list.
817
818* If you want to help even more, assign someone to look deeper into
819 error reports on your specific devices, understand why your firmware
820 may require some special device flags and what can be done about it.
821
822* Do you have spare devices you can give us? Send them to Richard (Mac
823 support) or Linus (Linux support). (So far nobody did that except for
824 Microsoft who sent us a Zune by proxy!)
825
826Vendors do need help from libmtp too, especially we want to help
827vendors improve their MTP stacks, because they all suffer from the
828same problem: the lack of a proper conformance test has made many devices
829incompliant with the MTP specification as it is published today: most
830devices are just compliant with the Windows MTP stack, and don't work
831out-of-the-box with libmtp. We need someone on the inside to help in
832bug reporting vendors MTP stacks internally so these issues are raised.
833A good way to go toward better MTP compliance is to test with an
834alternative implementation of the stack. In e.g. IETF standardization
835it is compulsory for an RFC to have atleast two independent implementations
836for it to reach the status as standard.
837
838Being compliant with libmtp is also more and more important for
839vendors: libmtp is being deployed in some embedded systems like
840set-top-boxes etc. It will be very irritating for customers if a device
841will not dock properly with some home entertainment equipment just because
842it is based on Linux and libmtp and not the Windows MTP stack.
Linus Walleij5f5c69f2011-06-26 14:34:13 +0200843
844Autodetect with gudev
845---------------------
846
847Previously you would use HAL to detect devices being plugged in. Nowadays
848we use udev directly, or though the GNOME libgudev library. LIBMTPs
849default udev rules export the proper properties to detect any MTP device
850automatically, here is a verbose example derived from gnomad2:
851
852#define G_UDEV_API_IS_SUBJECT_TO_CHANGE
853#include <gudev/gudev.h>
854const char * const gudev_subsystems[] = { "usb", NULL };
855GUdevClient *gudev_client;
856guint uevent_id;
857guint uevent_bus_hooked = 0;
858guint uevent_device_hooked = 0;
859
860
861static void uevent_cb(GUdevClient *client, const char *action, GUdevDevice *device, void *data)
862{
863 guint64 devicenum;
864 guint vendor;
865 guint model;
866 guint busnum;
867 guint devnum;
868 guint mtpdevice;
869
870 devicenum = (guint64) g_udev_device_get_device_number(device);
871 g_print("%s event for %s (%"G_GINT64_MODIFIER"x)", action,
872 g_udev_device_get_sysfs_path (device), devicenum);
873
874 /* get device info */
875 vendor = get_property_as_int(device, "ID_VENDOR_ID", 16);
876 model = get_property_as_int(device, "ID_MODEL_ID", 16);
877 busnum = get_property_as_int(device, "BUSNUM", 10);
878 devnum = get_property_as_int(device, "DEVNUM", 10);
879 mtpdevice = get_property_as_int(device, "ID_MTP_DEVICE", 10);
880
881 if (vendor == 0 || model == 0) {
882 g_print("couldn't get vendor or model ID for device at (%x:%x)\n",
883 busnum, devnum);
884 return;
885 } else {
886 g_print("vendor = %x, model = %x, bus = %x, device = %x\n",
887 vendor, model, busnum, devnum);
888 }
889
890 if (mtpdevice) {
891 g_print("device is MTP compliant\n");
892
893 if (g_str_equal(action, "add") &&
894 uevent_bus_hooked == 0 &&
895 uevent_device_hooked == 0) {
896 g_print(MTP device plugged in!\n");
897 uevent_bus_hooked = busnum;
898 uevent_device_hooked = devnum;
899 scan_jukebox(NULL);
900 } else if (g_str_equal (action, "remove") &&
901 uevent_bus_hooked == busnum &&
902 uevent_device_hooked == devnum) {
903 g_print("MTP device removed!\n");
904 uevent_bus_hooked = 0;
905 uevent_device_hooked = 0;
906 }
907 }
908}
909
910
911
912(...)
913 /*
914 * Monitor udev device events - we're only really interested in events
915 * for USB devices.
916 */
917 gudev_client = g_udev_client_new(gudev_subsystems);
918 uevent_id = g_signal_connect_object(gudev_client,
919 "uevent",
920 G_CALLBACK(uevent_cb),
921 NULL, 0);