1Building and Installing
2-----------------------
3
4See the "INSTALL" file.
5
6
7Initiator 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:
18- MeeGo:s Buteo Sync:
19 https://github.com/nemomobile/buteo-mtp
20 https://wiki.merproject.org/wiki/Buteo/MTP
21- Android has an MTP responder implementation:
22 https://android.googlesource.com/platform/frameworks/base/+/master/media/jni/
23- Ubuntu/Ricardo Salveti has mtp-server and libmtp-server going:
24 https://code.launchpad.net/~phablet-team/mtp/trunk
25 http://bazaar.launchpad.net/~phablet-team/mtp/trunk/files
26
27Heritage
28--------
29
30libmtp is based on several ancestors:
31
32* libptp2 by Mariusz Woloszyn was the starting point used
33 by Richard A. Low for the initial starter port. You can
34 find it at http://libptp.sourceforge.net/
35
36* libgphoto2 by Mariusz Woloszyn and Marcus Meissner was
37 used at a later stage since it was (is) more actively
38 maintained. libmtp tracks the PTP implementation in
39 libgphoto2 and considers it an upstream project. We will
40 try to submit anything generally useful back to libgphoto2
41 and not make double efforts. In practice this means we
42 use ptp.c, ptp.h and ptp-pack.c verbatim from the libgphoto2
43 source code. If you need to change things in these files,
44 make sure it is so general that libgphoto2 will want to
45 merge it to their codebase too. You find libgphoto2 as part
46 of gPhoto: http://gphoto.sourceforge.net/
47
48* libnjb was a project that Richard and Linus were working
49 on before libmtp. When Linus took Richards initial port
50 and made an generic C API he re-used the philosophy and
51 much code from libnjb. Many of the sample programs are for
52 example taken quite literally from libnjb. You find it here:
53 http://libnjb.sourceforge.net/
54
55
56Contacting and Contributing
57---------------------------
58
59See the project page at http://libmtp.sourceforge.net/
60We always need your help. There is a mailinglist and a
61bug report system there.
62
63People who want to discuss MTP devices in fora seem to
64hang out on the forums at AnythingbutiPod:
65http://www.anythingbutipod.com/forum/
66
67
68Compiling programs for libmtp
69-----------------------------
70
71libmtp has support for the pkg-config script by adding a libmtp.pc
72entry in $(prefix)/lib/pkgconfig. To compile a libmtp program,
73"just" write:
74
75gcc -o foo `pkg-config --cflags --libs libmtp` foo.c
76
77This also simplifies compilation using autoconf and pkg-config: just
78write e.g.
79
80PKG_CHECK_MODULES(MTP, libmtp)
81AC_SUBST(MTP_CFLAGS)
82AC_SUBST(MTP_LIBS)
83
84To have libmtp LIBS and CFLAGS defined. Needless to say, this will
85only work if you have pkgconfig installed on your system, but most
86people have nowadays.
87
88If your library is installed in e.g. /usr/local you may have to tell
89this to pkgconfig by setting the PKG_CONFIG_PATH thus:
90
91export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
92
93
94Documentation
95-------------
96
97Read the API documentation that can be generated with doxygen.
98It will be output in doc/html if you have Doxygen properly
99installed. (It will not be created unless you have Doxygen!)
100
101For information about the Media Transfer Protocol, see:
102http://en.wikipedia.org/wiki/Media_Transfer_Protocol
103
104The official 1.0 specification for MTP was released by the
105USB Implementers Forum in may, 2008. Prior to this, only a
106proprietary Microsoft version was available, and quite a few
107devices out there still use some aspects of the Microsoft
108version, which deviates from the specified standard. You can
109find the official specification here:
110http://www.usb.org/developers/devclass_docs/MTP_1.0.zip
111
112
113The Examples
114------------
115
116In the subdirectory "examples" you find a number of
117command-line tools, illustrating the use of libmtp in very
118simple terms.
119
120Please do not complain about the usability or documentation
121of these examples, they look like they do for two reasons:
122
1231. They are examples, not tools. If they were intended for
124 day-to-day usage by commandline freaks, I would have
125 called them "tools" not "examples".
126
1272. The MTP usage paradigm is that a daemon should hook
128 the device upon connection, and that it should be
129 released by unplugging. GUI tools utilizing HAL (hald)
130 and D-Bus do this much better than any commandline
131 program ever can. (See below on bugs.) Specificationwise
132 this is a bug, however it is present in many, many
133 devices.
134
135That said, if you want to pick up and maintain the examples,
136please volunteer.
137
138
139FAQ: Common Problems
140--------------------
141
142Some MTP devices have strange pecularities. We try to work around
143these whenever we can, sometimes we cannot work around it or we
144cannot test your solution.
145
146* Android locked screen: some devices just report zero files
147 and no storages when the device screen is locked, it looks like
148 so:
149
150 mtp-detect
151 Device 0 (VID=04e8 and PID=6860) is a Samsung Galaxy models (MTP).
152 Attempting to connect device(s)
153 Error 1: Get Storage information failed.
154 Device: SHV-E210K
155 LIBMTP_Get_Storage(): No data available
156 OK.
157
158 This is probably so as not to allow the MTP access to be used
159 as a "backdoor" into the device. Unlock the device before listing
160 files, set the autolock to some large value or disabled if it
161 disturbs you, you are causing this to yourself, or should we say
162 that your vendor is prioritizing security and privacy over
163 ease-of-use. (You may talk to your vendor about this.)
164
165* mtp-* tools doesn't work because someone else is already hogging
166 the device
167
168 This is a common problem, the most common case could be that
169 gphoto2 (which can also talk PTP/MTP) is taking over the device
170 as soon as it's plugged in. Some distributions are configured that
171 way. Counter it like this:
172
173 gvfs-mount -s gphoto2
174
175 Then re-attach the device.
176
177 Sometimes some gvfs daemons are running on the
178 system and hogging the device, try stopping them
179 with something like these commands:
180
181 killall gvfs-mtp-volume-monitor
182 killall gvfs-gphoto2-volume-monitor
183
184 Then plug in the device and issue "mtp-detect" to figure out if
185 this may be the case.
186
187* Generic MTP/PTP disconnect misbehaviour: we have noticed that
188 Windows Media Player apparently never close the session to an MTP
189 device. There is a daemon in Windows that "hooks" the device
190 by opening a PTP session to any MTP device, whenever it is
191 plugged in. This daemon proxies any subsequent transactions
192 to/from the device and will never close the session, thus
193 Windows simply does not close sessions at all.
194
195 For example this means that a device may work the first time
196 you run some command-line example like "mtp-detect" while
197 subsequent runs fail.
198
199 Typical sign of this illness: broken pipes on closing sessions,
200 on the main transfer pipes(s) or the interrupt pipe:
201
202 Closing session
203 usb_clear_halt() on INTERRUPT endpoint: Broken pipe
204 OK.
205
206 This means that device manufacturers doesn't notice any problems
207 with devices that do not correctly handle closing PTP/MTP
208 sessions, since Windows never do it. The proper way of closing
209 a session in Windows is to unplug the device, simply put.
210
211 Since libmtp actually tries to close sessions, some devices
212 may fail since the close session functionality has never been
213 properly tested, and "it works with Windows" is sort of the
214 testing criteria at some companies.
215
216 You can get Windows-like behaviour on Linux by running a udev-aware
217 libmtp GUI client like Rhythmbox or Gnomad2, which will "hook"
218 the device when you plug it in, and "release" it if you unplug
219 it, and you start/end you transfer sessions by plugging/unplugging
220 the USB cable.
221
222 The "Unix way" of running small programs that open the device,
223 do something, then close the device, isn't really working with
224 such devices and you cannot expect to have command line tools
225 like the mtp examples work with them. You could implement new
226 example programs that just call to a mediating daemon like the
227 Windows MTP stack does. (And change all programs using libmtp
228 directly today.)
229
230 If this bug in your device annoys you, contact your device
231 manufacturer and ask them to test their product with some libmtp
232 program.
233
234* Samsung Android 2.3.x devices: these have a special MTP stack
235 with some specific bugs that we have maybe nailed down now.
236 It suffers from an "immediate connect" syndrome, i.e. you have
237 to connect to the device within 7 seconds of plugging in, or it
238 will go numb. This also goes for command-line activity with
239 the example programs, so this device is better used with a
240 GUI tool like Rhythmbox, gnomad2...
241
242* Generic USB misbehaviour: some devices behave badly under MTP
243 and USB mass storage alike, even down to the lowest layers
244 of USB. You can always discuss such issues at the linux-usb
245 mailing list if you're using Linux:
246 http://www.linux-usb.org/mailing.html
247
248 If you have a problem specific to USB mass storage mode, there
249 is a list of strange behaving devices in the Linux kernel:
250 http://lxr.linux.no/linux/drivers/usb/storage/unusual_devs.h
251 You can discuss this too on the mentioned list, for understanding
252 the quirks, see:
253 http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt
254
255* Generic certificate misbehaviour. All devices are actually
256 required to support a device certificate to be able to
257 encrypt Windows Media (WMA/WMV) files. However there are
258 obviously a lot of devices out there which doesn't support
259 this at all but instead crash. Typical printout:
260
261 Error 2: PTP Layer error 02ff: get_device_unicode_property(): failed
262 to get unicode property.
263
264 This should only affect "mtp-detect", there is no other
265 application currently retrieveing the certificate (not that we
266 know anyway).
267
268* Kernel bug on Linux. Linux 2.6.16 is generally speaking required
269 to use any MTP device under USB 2.0. This is because the EHCI
270 driver previously did not support zero-length writes to endpoints.
271 It should work in most cases however, or if you connect it
272 to an UHCI/OHCI port instead (yielding lower speed). But
273 please just use a recent kernel.
274
275* Zen models AVI file seeking problem: the Zens cannot parse the
276 files for the runlength metadata. Do not transfer file with e.g.
277 mtp-sendfile, use mtp-sendtr and set the length of the track to
278 the apropriate number of seconds and it will work. In graphical
279 clients, use a "track transfer" function to send these AVI files,
280 the Zens need the metadata associated with tracks to play back
281 movies properly. Movies are considered "tracks" in the MTP world.
282
283* Some devices that disregard the metadata sent with the MTP
284 commands will parse the files for e.g. ID3 metadata. Some still
285 of these devices expect only ID3v2.3 metadata and will fail with
286 a modern ID3v2,4 tag writer, like many of those found in Linux
287 applications. Windows Media Player use ID3v2.3 only, so many
288 manufacturers only test this version.
289
290* The Zen Vision:M (possibly more Creative Zens) has a firmware bug
291 that makes it drop the last two characters off a playlist name.
292 It is fixed in later firmware.
293
294* For Creative Technology devices, there are hard limits on how
295 many files can be put onto the device. For a 30 GiB device (like
296 the Zen Xtra) the limit is 6000, for a 60 GiB device the limit
297 is 15000 files. For further Creative pecularities, see the
298 FAQ sections at www.nomadness.net.
299
300* Sandisk sansa c150 and probably several other Sandisk devices
301 (and possibly devices from other manufacturers) have a dual
302 mode with MTP and USB mass storage. The device will initially
303 claim to be mass storage so udev will capture is and make the
304 use of MTP mode impossible. One way of avoiding it could be to
305 be to blacklist the "usb-storage" module in
306 /etc/modprobe.c/blacklist with a row like this:
307 "blacklist usb-storage". Some have even removed the
308 "usb-storage.ko" (kernel module file) to avoid loading.
309
310* Sandisk Sansa Fuze has three modes: auto, MTP or mass storage
311 (MSC). Please set it to MTP to avoid problems with libmtp.
312
313* The iriver devices (possibly all of them) cannot handle the
314 enhanced GetObjectPropList MTP command (0x9805) properly. So
315 they have been banned from using it.
316
317* iriver devices have problems with older versions of libmtp and
318 with new devices libmtp does not know of as of yet, since it
319 has an oldstyle USB device controller that cannot handle zero
320 writes. (Register your device with us!) All their devices are
321 likely to need a special device flag in the src/libusb-glue.c
322 database.
323
324* The Samsung Yepp T9 has several strange characteristics, some
325 that we've managed to work around. (For example it will return
326 multiple PTP packages in a single transaction.)
327
328* The early firmware for Philips HDD players is known to be
329 problematic. Please upgrade to as new firmware as you can get.
330 (Yes this requires some kind of Windows Installation I think.)
331
332* Philips HDD 1630/16 or 1630/17 etc may lock themselves up,
333 turning inresponsive due to internal corruption. This typically
334 gives an error in opening the PTP session. Apparently you can
335 do a "repair" with the firmware utility (Windows only) which
336 will often fix this problem and make the device responsive
337 again.
338
339* Some devices that implement GetObjectPropList (0x9805) will
340 not return the entire object list if you request a list for object
341 0xffffffffu. (But they should.) So they may need the special
342 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL.
343
344* Some (smaller) subset of devices cannot even get all the
345 properties for a single object in one go, these need the
346 DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST. Currently only the
347 iriver devices seem to have this bug.
348
349* The Toshiba Gigabeat S (and probably its sibling the
350 Microsoft Zune and other Toshiba devices) will only display
351 album information tags for a song in case there is also
352 an abstract album (created with the album interface) with
353 the exact same name.
354
355* The Zen Vision:M has an older firmware which is very corrupt,
356 it is incompatible with the Linux USB stack altogether. The
357 kernel dmesg will look something like this, and you have to
358 upgrade the firmware using Windows:
359 usb 4-5: new high speed USB device using ehci_hcd and address 5
360 usb 4-5: configuration #1 chosen from 1 choice
361 usb 4-5: can't set config #1, error -110
362
363* The Sirus Stiletto does not seem to allow you to copy any files
364 off the device. This may be someone's idea of copy protection.
365
366* The Samsung P2 assigns parent folder ID 0 to all unknown file
367 types.(i.e. moves them to the root folder)
368
369* The Sandisk Sansa Clip+ needs a firmware upgrade in earlier
370 versions in order to work properly.
371
372
373New Devices
374-----------
375
376If you happen upon a device which libmtp claims it cannot
377autodetect, please submit the vendor ID and device ID
378(these can be obtained from the "lsusb" and "lsusb -n"
379commands run as root) as a bug, patch or feature request
380on the Sourceforge bug tracker at our homepage. If it
381gives a sensible output from "mtp-detect" then please attach
382the result as well as it teach us some stuff about your
383device. If you've done some additional hacking, join our
384mailinglist and post your experiences there.
385
386If you want to be able to hack some more and you're not
387afraid of C hacking, add an entry for your device's
388vendor/product ID and a descriptive string to the database
389in the file src/music-players.h.
390
391If you want to poke around to see if your device has some
392special pecularities, you can test some special device
393flags (defined in src/device-flags.h) by inserting them
394together with your device entry in src/music-players.h.
395Flags can be tested in isolation or catenated with "|"
396(binary OR). If relatives to your device use a certain
397flag, chances are high that a new device will need it
398too, typically from the same manufacturer.
399
400The most common flag that needs to be set is the
401DEVICE_FLAG_UNLOAD_DRIVER that detach any Linux kernel
402drivers that may have attached to the device making
403MTP access impossible. This is however not expected to
404really work: this is a problem being tracked as of
405now (2007-08-04). See the "last resort" solutions below
406if you really need to get your dual-mode device to work
407with MTP.
408
409Another flag which is easy to identify is the
410DEVICE_FLAG_NO_ZERO_READS, which remedies connection
411timeouts when getting files, and some timeouts on e.g.
412successive "mtp-connect" calls.
413
414If your device is very problematic we are curious of how it
415works under Windows, so we enjoy reading USB packet sniffs
416that reveal the low-level traffic carried out between
417Windows Media Player and your device. This can be done
418using e.g.:
419
420* USBsnoop:
421 http://benoit.papillault.free.fr/usbsnoop/
422
423* The trial version of HHD Softwares software-only
424 USB monitor. You need to get a copy of version 2.37 since
425 the newer trial versions won't let you carry out the
426 needed packet sniffs. (As of 2007-03-10 a copy can be found
427 at: http://www.cobbleware.com/files/usb-monitor-237.exe)
428
429There are other USB monitors as well, some more expensive
430alternatives use hardware and even measure electronic
431characteristics of the traffic (which is far too much
432detail for us).
433
434Device sniffs are an easy read since the PTP/MTP protocol
435is nicely structured. All commands will have a structure such
436as this in the log, we examplify with a object list request:
437
438PTP REQEUST:
439000120: Bulk or Interrupt Transfer (UP), 03.09.2007 12:49:25.9843750 +0.0
440Pipe Handle: 0x863ce234 (Endpoint Address: 0x2)
441Send 0x20 bytes to the device:
442 20 00 00 00 01 00 05 98 23 00 00 00 27 03 00 10 ......?#...'...
443 Length TYPE CMD Trans# Param1
444
445 00 00 00 00 02 DC 00 00 00 00 00 00 00 00 00 00 .....Ü..........
446 Param2 Param3 Param4 Param5
447
448[OPTIONAL] DATA PHASE:
449000121: Bulk or Interrupt Transfer (UP), 03.09.2007 12:49:26.0 +0.0156250
450Pipe Handle: 0x863ce214 (Endpoint Address: 0x81)
451Get 0x1a bytes from the device:
452 1A 00 00 00 02 00 05 98 23 00 00 00 01 00 00 00 .......?#.......
453 Length TYPE CMD Trans# DATA
454
455 27 03 00 10 02 DC 04 00 00 30 '....Ü...0
456
457RESPONSE:
458000122: Bulk or Interrupt Transfer (UP), 03.09.2007 12:49:26.0 +0.0
459Pipe Handle: 0x863ce214 (Endpoint Address: 0x81)
460Get 0xc bytes from the device:
461 0C 00 00 00 03 00 01 20 23 00 00 00 ....... #...
462 Length TYPE CODE Trans#
463
464* One send (OUT to the device), two reads (IN from the device).
465
466* All three byte chunks commands are
467 sent/recieved/recieeved by the function ptp_transaction()
468 in the file ptp.c.
469
470* It boils down to ptp_usb_sendreq(), optionally ptp_usb_senddata()
471 or ptp_usb_getdata() and finally ptp_usb_getresp() in the file
472 libusb-glue.c. Notice ptp_usb_sendreq() and ptp_usb_getresp()
473 are ALWAYS called. The TYPE field correspond to this, so the
474 TYPES in this case are "COMMAND" (0x0001), "DATA" (0x0002),
475 and "RESPONSE" (0x0003).
476
477* Notice that the byte order is little endian, so you need to read
478 each field from right to left.
479
480* This COMMAND has:
481 CMD 0x99805, we see in ptp.h that this is PTP_OC_MTP_GetObjPropList.
482 Transaction# 0x00000023.
483 REQUEST parameters 0x10000327, 0x00000000, 0x0000DC02, 0x00000000
484 0x00000000, in this case it means "get props for object 0x10000327",
485 "any format", "property 0xDC02" (PTP_OPC_ObjectFormat), then two
486 parameters that are always zero (no idea what they mean or their
487 use).
488
489* The DATA has:
490 CMD 0x99805, we see in ptp.h that this is PTP_OC_MTP_GetObjPropList.
491 Transaction# 0x00000023.
492 Then comes data 0x00000001, 0x10000327, 0xDC02, 0x0004, 0x3000
493 Which means in this case, (and this is the tricky part) "here
494 you have 1 property", "for object 0x10000327", "it is property
495 0xDC02" (PTP_OPC_ObjectFormat), "which is of type 0x0004"
496 (PTP_DTC_UINT16), "and set to 0x3000" (PTP_OFC_Undefined, it
497 is perfectly valid to have undefined object formats, since it
498 is a legal value defining this).
499
500* This RESPONSE has:
501 CMD 0x99805, we see in ptp.h that this is PTP_OC_MTP_GetObjPropList.
502 Return Code ("RC") = 0x2001, PTP_RC_OK, all went fine.
503 Transaction# 0x00000023.
504
505If you want to compare the Windows behaviour with a similar
506operation using libmtp you can go into the src/libusb-glue.c
507file and uncomment the row that reads:
508
509//#define ENABLE_USB_BULK_DEBUG
510
511(I.e. remove the two //.)
512
513This will make libmtp print out a hex dump of every bulk USB
514transaction. The bulk transactions contain all the PTP/MTP layer
515data, which is usually where the problems appear.
516
517
518Notes to assist with debugging new devices:
519-------------------------------------------
520
521In debugging new hardware, we highly recommend that you only
522use the example mtp-* applications that come with libmtp, as other
523applications may have their own bugs that may interfere with your
524new device working correctly. Using another application instead of
525those that come with libmtp just adds another point of failure.
526
527For debugging, there are 3 main options:
528
5291. Use the env variable: LIBMTP_DEBUG to increase the
530verboseness of the debugging output for any application using
531libmtp. Relevant codes are:
532* 0x00 [0000 0000] : no debug (default)
533* 0x01 [0000 0001] : PTP debug
534* 0x02 [0000 0010] : Playlist debug
535* 0x04 [0000 0100] : USB debug
536* 0x08 [0000 1000] : USB data debug
537// Codes are hex and binary respectively. Simple add them togther
538// to get your desired level of output.
539
540(Assuming bash)
541eg:
542$ export LIBMTP_DEBUG=12
543$ mtp-detect
544 // To get USB debug and USB data debug information.
545
546$ export LIBMTP_DEBUG=2
547$ mtp-detect
548 // To get Playlist debug information.
549
550Also note, an application may also use the LIBMTP_debug() API
551function to achieve the same options as listed above.
552
5532. Use "strace" on the various mtp-* commands to see where/what
554is falling over or getting stuck at.
555* On Solaris and FreeBSD, use "truss" or "dtrace" instead on "strace".
556* On Mac OS X, use "ktrace" or "dtrace" instead of "strace".
557* On OpenBSD and NetBSD, use "ktrace" instead of "strace".
558
559This will at least help pinpoint where the application is failing, or
560a device is reporting incorrect information. (This is extremely helpful
561with devices that have odd disconnection requirements).
562
563The use of these tools may also pinpoint issues with libusb as
564implemented by each OS vendor or issues with the MTP implementation
565on the new device as well, so please be prepared for either case.
566
5673. Use "gdb" or similar debugger to step through the code as it is
568run. This is time consuming, and not needed just to pinpoint where
569the fault is.
570
571The use of gdb or another debugger may also miss or actually cause
572command and data timing issues with some devices, leading to false
573information. So please consider this a last resort option.
574
575Also please read the "It's Not Our Bug!" section below, as it does
576contain some useful information that may assist with your device.
577
578
579Dual-mode devices does not work - last resort:
580----------------------------------------------
581
582Some devices that are dual-mode are simply impossible to get
583to work under Linux because the usb-storage(.ko) kernel
584module hook them first, and refuse to release them, even
585when we specify the DEVICE_FLAG_UNLOAD_DRIVER flag. (Maybe
586it DOES release it but the device will immediately be probed
587at the USB mass storage interface AGAIN because it
588enumerates.)
589
590Here is what some people do:
591
592 1. Plug in the device.
593 2. USB-mass storage folder will open automatically.
594 3. Unmount the device.
595 4. Run mtp-detect. It will most likely fail the first time.
596 5. Run mtp-detect again, it might work this time, or fail. Keep running
597 till it works. 99% it works by the third try.
598 6. Once mtp-detect gives you an "Ok", open either Rhythmbox or Gnomad2,
599 everything should work.
600
601Linux: Try this, if you have a recent Linux kernel,
602add the file (as root):
603
604/etc/modprobe.d/no-usb-storage.conf
605
606With the contents:
607
608options usb-storage quirks=1234:4321:i
609
610This will tell usb-storage to ignore this device when it's inserted
611so it is not hogged by the mass storage interfaces. Remove and re-insert
612the device and see if it works. Usually this does the trick.
613
614For older systems, or as a bigger hammer, run (as root) something
615like:
616
617> rmmod usb_storage ; mtp-detect
618
619You can run most any command or a client like gnomad2 or
620Amarok immediately after the rmmod command. This works
621sometimes. Another even more brutal approach is this:
622
623* Edit /etc/modprobe.d/blacklist
624* Add the line "blacklist usb-storage"
625* Reboot.
626
627Now none of you USB disks, flash memory sticks etc will be
628working (you just disabled them all). However you *can* try
629your device, and it might have started working because there
630is no longer a USB mass storage driver that tries to hook onto
631the mass storage interface of your device.
632
633If not even blacklisting works (check with
634"lsmod | grep usb-storage"), there is some problem with
635something else and you may need to remove or rename the file
636/lib/modules/<VERSION>/kernel/drivers/usb/storage/usb-storage.ko
637manually.
638
639If you find the PerfectSolution(TM) to this dilemma, so you
640can properly switch for individual devices whether to use it
641as USB mass storage or not, please tell us how you did it. We
642know we cannot use udev, because udev is called after-the-fact:
643the device is already configured for USB mass storage when
644udev is called.
645
646On Mac OS there is another ugly hack:
647
6481. Open up a terminal window
6492. Type:
650sudo mv /System/Library/Extensions/IOUSBMassStorageClass.kext
651/System/Library/Extensions/IOUSBMassStorageClass.kext.disabled
652
653and when prompted enter your password.
654
6553. Restart.
656
657To reverse this change, just reverse the filenames:
658
659sudo mv /System/Library/Extensions/
660IOUSBMassStorageClass.kext.disabled /System/Library/Extensions/
661IOUSBMassStorageClass.kext
662
663and restart.
664
665
666Calendar and contact support:
667-----------------------------
668
669The Creative Zen series can read VCALENDAR2 (.ics) files
670and VCard (.vcf) files from programs like for example
671Evolution with the following limitations/conditions:
672
673- The file must be in DOS (CR/LF) format, use the unix2dos
674 program to convert if needed
675
676- Repeat events in calendar files do not seem to be supported,
677 entries will only appear once.
678
679- Calendar (.ics) files should be stored in the folder "My Organizer"
680 when sent to the device (this directory should be autodetected
681 for use with calendar files, otherwise use the option
682 -f "My Organizer" to sendfile for this) Apparently this file can
683 also contain tasklists.
684
685- Contact (.vcf) files should be stored in the folder "My Contacts"
686 when sent to the device. (-f "My Contacts")
687
688- Some devices are picky about the name of the calendar and
689 contact files. For example the Zen Microphoto wants:
690
691 Calendar: My Organizer/6651416.ics
692 Contacts: My Organizer/6651416.vcf
693
694
695Syncing in with Evolution and Creative Devices
696----------------------------------------------
697
698Evolution can easily export .ics an .vcf files, but you currently
699need some command-line hacking to get you stuff copied over in
700one direction host -> device. The examples/ directory contains a script
701created for the Creative Zen Microphoto by Nicolas Tetreault.
702
703
704Lost symbols
705------------
706
707Shared libraries can be troublesome to users not experienced with
708them. The following is a condensed version of a generic question
709that has appeared on the libmtp mailing list from time to time.
710
711> PTP: Opening session
712> Queried Creative Zen Vision:M
713> gnomad2: relocation error: gnomad2: undefined symbol:
714> LIBMTP_Get_Storageinfo
715> (...)
716> Are these type of errors related to libmtp or something else?
717
718The problem is of a generic nature, and related to dynamic library
719loading. It is colloquially known as "dependency hell".
720(http://en.wikipedia.org/wiki/Dependency_hell)
721
722The gnomad2 application calls upon the dynamic linker in Linux to
723resolve the symbol "LIBMTP_Get_Storageinfo" or any other symbol
724(ELF symbol, or link point or whatever you want to call them, a
725symbol is a label on a memory address that the linker shall
726resolve from label to actual address.)
727For generic information on this subject see the INSTALL file and
728this Wikipedia page:
729
730http://en.wikipedia.org/wiki/Library_(computing)
731
732When Linux /lib/ld-linux.so.X is called to link the symbols compiled
733into gnomad2 (or any other executable using libmtp), it examines the
734ELF file for the libmtp.so.X file it finds first and cannot resolve
735the symbol "LIBMTP_Get_Storageinfo" (or whichever symbol you have a
736problem witj) from it, since it's probably not there. There are many
737possible causes of this symbol breakage:
738
7391) You installed precompiled libmtp and gnomad2 packages (RPMs, debs
740 whatever) that do not match up. Typical cause: your gnomad2 package was
741 built against a newer version of libmtp than what's installed on your
742 machine. Another typical cause: you installed a package you found on
743 the web, somewhere, the dependency resolution system did not protest
744 properly (as it should) or you forced it to install anyway, ignoring
745 some warnings.
746
7472) You compiled libmtp and/or gnomad2 from source, installing both or
748 either in /usr/local/lib and /usr/local/bin. This means at compile-time
749 gnomad2 finds the libmtp library in /usr/local/lib but at runtime, it
750 depends on the Linux system wide library loader (/lib/ld-linux.so.X) in
751 order to resolve the symbols. This loader will look into the file
752 /etc/ld.so.conf and/or the folder /etc/ld.so.conf.d in order to find
753 paths to libraries to be used for resolving the symbols. If you have
754 some older version of libmtp in e.g. /usr/lib (typically installed by a
755 package manager) it will take precedence over the new version you just
756 installed in /usr/local/lib and the newly compiled library in
757 /usr/local/lib will *not* be used, resulting in this error message.
758
7593) You really did install the very latest versions (as of writing libmtp
760 0.1.5 and gnomad2 2.8.11) from source and there really is no
761 pre-installed package of either on your machine. In that case I'm
762 totally lost, I have no idea what's causing this.
763
764Typical remedies:
765
7661) If you don't want to mess around with your system and risk these
767 situations, only use pre-packaged software that came with the
768 distribution or its official support channels. If it still breaks,
769 blame your distribution, they're not packaging correctly. Relying on
770 properly packaged software and not installing things yourself *is* the
771 Linux solution to the "dependency hell" problem.
772
7732) Read about dynamically linked library handling until the stuff I wrote
774 about in the previous list sounds like music to your ears, inspect
775 your /lib, /usr/lib, /usr/local/lib, /etc/ld.so.conf and the
776 /etc/ld.so.conf.d, remove all pre-packed versions using RPM, APT,
777 YaST or whatever your distribution uses, compile libmtp and gnomad2
778 (or whatever) from source only and you will be enlighted.
779
780I don't know if this helps you, it's the best answer we can give.
781
782
783API is obscure - I want plain files!
784------------------------------------
785
786PTP/MTP devices does not actually contain "files", they contain
787objects. These objects have file names, but that is actually
788just a name tag on the object.
789
790Folders/directories aren't really such entities: they are just
791objects too, albeit objects that can act as parent to other
792objects. They are called "associations" and are created in atomic
793fashion and even though there is an MTP command to get all the
794associations of a certain object, this command is optional
795so it is perfectly possible (and most common, actually) to create
796devices where the "folders" (which are actually associations) have
797no idea whatsoever of what files they are associated as parents to
798(i.e. which files they contain). This is very easy for device
799manufacturers to implement, all the association (i.e. finding out
800which files are in a certain folder) has to be done by the MTP
801Initiator / host computer.
802
803Moving a file to a new folder is for example very simple in a
804"real" file system. In PTP/MTP devices it is often not even possible,
805some devices *may* be able to do that, if they support command
8060x1019 "Move Object", but actually the only reliable way of executing
807file movement is to upload the file to the host, download it with
808the new parent, then delete the old file. We have played with the
809idea of implementing this time consuming function as a fallback
810in case the device does not support command 0x1019, perhaps one day
811we will do that. (Some devices also support command 0x101a
812"Copy Object".)
813
814Then the issue that in PTP/MTP it is legal for two files to have
815exactly the same path as long as their object IDs differ. A
816folder/association can contain two files with the exact same name.
817(And on the Creative devices this even works, too, though most devices
818implicitly fail at this.) Perhaps one could add some custom hook for
819handling that, so they become /Foo.mp3 and /Foo.mp3(1) or something
820similar, but it's really a bit kludgy.
821
822Playlists and albums aren't really files, thinking about
823them as files like the hacks in libgphoto2 is really backwards. They are
824called associations and are more like a symbolic link that links in a
825star-shaped pattern to all the files that are part of the album/playlist.
826Some devices (Samsung) thought that was too complicated and have a
827different way of storing playlists in an UTF-16 encoded .spl-like file
828instead! This is why playlists/albums must have their own structs and
829functions.
830
831Plain file access also assumes to be able to write files of an
832undetermined size, which is simply not possible in a transactional
833file system like PTP/MTP. (See further below.)
834
835
836I Want Streaming!
837-----------------
838
839Streaming reads is easy. Just connect the output file descriptor from
840LIBMTP_Get_File_To_File_Descriptor() (and a similar function for tracks)
841wherever you want.
842
843People have connected this to TCP sockets for streaming web servers
844etc, works like a charm. Some devices will even survive if the callback
845functions return non-zero and cancel the download. Some devices will
846lock up and even require a reset if you do that. Devices are poorly
847implemented so that's life. If you want to stream off a device, the
848best idea is always to stream the entire file and discard the stuff
849at the end you don't want. It will incur a delay if you e.g. want to
850skip between tracks, sadly.
851
852Then we get to the complicated things: streaming WRITES...
853
854There is a function:
855LIBMTP_Send_File_From_File_Descriptor() (and similar for tracks)
856which will write a file to a device from a file descriptor, which may
857be a socket or whatever.
858
859HOWEVER: this requires a piece of metadata with the .filesize properly
860set first.
861
862This is not because we think it is funny to require that, the protocol
863requires it. The reason is that PTP/MTP is a transactional file system
864and it wants to be able to deny file transfer if the file won't fit on
865the device, so the transaction never even starts, it's impossible to
866start a transaction without giving file length.
867
868People really want streaming so I tried a lot of hacks to see if they
869would work, such as setting file size to 0xffffffffU or something other
870unnaturally big and then aborting the file transfer when the stream ends.
871It doesn't work: either the device crashes or the file simply disappears
872since the device rolls back all failed transactions.
873
874So this is an inherent limitation of the PTP/MTP protocol.
875
876
877I want to remote control my device!
878-----------------------------------
879
880I have both good and bad news for you.
881
882The good news is that the MTP protocol has well-defined commands to play
883back content on a device. Operation 0xD411 (PTP_DPC_MTP_PlaybackObject)
884will start playing back a file on the device (whatever that may mean if
885this is not a music or video file), and operation 0xD403 can set the
886playback volume to save your ears. Then there are operations to
887determine how far into the current file you currently are, so as to
888support say progress bars.
889
890Since these commands have been around since the dawn of the MTP protocol
891and since it was developed in cooperation with Creative Technology, this
892is probably a requested feature from the Creative people who already had
893support for playback on their devices using the PDE protocol back then.
894
895Anyway, here are the bad news:
896[logs]$ grep d411 *
897mtp-detect-trekstor-vibez.txt: 0xd411: Playback Object
898
899Aha there is only one known device in the world which actually supports
900playback on the device. So either you go buy the Trekstor Vibez, or you
901can forget about this. You could always try asking your hardware vendor
902of choice to go implement this.
903
904Since none of the core developers of libmtp has the Trekstor device, this
905is not yet implemented in libmtp.
906
907
908I make MTP devices!
909-------------------
910
911If you are a device vendor there is a lot you can do for libmtp:
912
913* Please consider assigning one of your employees as a contact person
914 for libmtp, have them sign up to the libmtp development list and answer
915 questions and post new device ID:s as they are released to our
916 mailing list.
917
918* If you want to help even more, assign someone to look deeper into
919 error reports on your specific devices, understand why your firmware
920 may require some special device flags and what can be done about it.
921
922* Do you have spare devices you can give us? Send them to Richard (Mac
923 support) or Linus (Linux support). (So far nobody did that except for
924 Microsoft who sent us a Zune by proxy!)
925
926Vendors do need help from libmtp too, especially we want to help
927vendors improve their MTP stacks, because they all suffer from the
928same problem: the lack of a proper conformance test has made many devices
929incompliant with the MTP specification as it is published today: most
930devices are just compliant with the Windows MTP stack, and don't work
931out-of-the-box with libmtp. We need someone on the inside to help in
932bug reporting vendors MTP stacks internally so these issues are raised.
933A good way to go toward better MTP compliance is to test with an
934alternative implementation of the stack. In e.g. IETF standardization
935it is compulsory for an RFC to have atleast two independent implementations
936for it to reach the status as standard.
937
938Being compliant with libmtp is also more and more important for
939vendors: libmtp is being deployed in some embedded systems like
940set-top-boxes etc. It will be very irritating for customers if a device
941will not dock properly with some home entertainment equipment just because
942it is based on Linux and libmtp and not the Windows MTP stack.
943
944Autodetect with gudev
945---------------------
946
947Previously you would use HAL to detect devices being plugged in. Nowadays
948we use udev directly, or though the GNOME libgudev library. LIBMTPs
949default udev rules export the proper properties to detect any MTP device
950automatically, here is a verbose example derived from gnomad2:
951
952#define G_UDEV_API_IS_SUBJECT_TO_CHANGE
953#include <gudev/gudev.h>
954const char * const gudev_subsystems[] = { "usb", NULL };
955GUdevClient *gudev_client;
956guint uevent_id;
957guint uevent_bus_hooked = 0;
958guint uevent_device_hooked = 0;
959
960
961static void uevent_cb(GUdevClient *client, const char *action, GUdevDevice *device, void *data)
962{
963 guint64 devicenum;
964 guint vendor;
965 guint model;
966 guint busnum;
967 guint devnum;
968 guint mtpdevice;
969
970 devicenum = (guint64) g_udev_device_get_device_number(device);
971 g_print("%s event for %s (%"G_GINT64_MODIFIER"x)", action,
972 g_udev_device_get_sysfs_path (device), devicenum);
973
974 /* get device info */
975 vendor = get_property_as_int(device, "ID_VENDOR_ID", 16);
976 model = get_property_as_int(device, "ID_MODEL_ID", 16);
977 busnum = get_property_as_int(device, "BUSNUM", 10);
978 devnum = get_property_as_int(device, "DEVNUM", 10);
979 mtpdevice = get_property_as_int(device, "ID_MTP_DEVICE", 10);
980
981 if (vendor == 0 || model == 0) {
982 g_print("couldn't get vendor or model ID for device at (%x:%x)\n",
983 busnum, devnum);
984 return;
985 } else {
986 g_print("vendor = %x, model = %x, bus = %x, device = %x\n",
987 vendor, model, busnum, devnum);
988 }
989
990 if (mtpdevice) {
991 g_print("device is MTP compliant\n");
992
993 if (g_str_equal(action, "add") &&
994 uevent_bus_hooked == 0 &&
995 uevent_device_hooked == 0) {
996 g_print(MTP device plugged in!\n");
997 uevent_bus_hooked = busnum;
998 uevent_device_hooked = devnum;
999 scan_jukebox(NULL);
1000 } else if (g_str_equal (action, "remove") &&
1001 uevent_bus_hooked == busnum &&
1002 uevent_device_hooked == devnum) {
1003 g_print("MTP device removed!\n");
1004 uevent_bus_hooked = 0;
1005 uevent_device_hooked = 0;
1006 }
1007 }
1008}
1009
1010
1011
1012(...)
1013 /*
1014 * Monitor udev device events - we're only really interested in events
1015 * for USB devices.
1016 */
1017 gudev_client = g_udev_client_new(gudev_subsystems);
1018 uevent_id = g_signal_connect_object(gudev_client,
1019 "uevent",
1020 G_CALLBACK(uevent_cb),
1021 NULL, 0);
1022
1023SKETCH OF AN OVERVIEW
1024---------------------
1025
1026Draft agenda for a talk on MTP devices submitted for the Android
1027builders summit, might come to recycle this:
1028
1029- Protocol overview
1030 - Transactional filesystem - no corruption due to unplugged cables!
1031 - The host and the device can access the files simultaneously, the
1032 device will always "own" the physical file system and proxy the
1033 host (MTP initiator).
1034- libmtp interface
1035- relation to libgphoto2
1036- User expectations fall short:
1037 - Not really a mountable filesystem.
1038 - Streaming does not work. (Size needs to be known beforehand due to
1039 transactional nature.)
1040 - GVFS MTP backend to the rescue.
1041- Device sins
1042 - Using the same VID/PID for several modes, some of which are not MTP.
1043 HTC Zopo, HD2, Bird (0x0bb4/0x0c02). Thanks for that, now we cannot
1044 detect the protocol from just VID+PID but have to examine the interfaces.
1045 - Android bugs
1046 - Samsungs special Android MTP stack
1047 - SonyEricsson Aricent stack for Xperia Androids pre 4.0, broken headers!
1048 - Flat access model vs hierarchical, how Android uses MTP as an hierachical
1049 file system while it was previously a flat database.
1050 - Old paradigm: scan the entire non-hierarchical storage for all content,
1051 build a cache to speed up the (USB 1.1!) link. Usually all files were
1052 stored in the root folder or a single folder named "/Music" or similar.
1053 - Android introduced deeply nested folder hierarchies, not seen before
1054 on MTP devices.
1055 - Microsoft not using the complete metadata dump feature of the MTP
1056 protocol (once introduced by creative) instead they walk directories
1057 separately.
1058 - So caching a big device will take long time and/or timeout.
1059 - Go-MTPFS (FUSE) and GVFS MTP - doing the partial directory walk rather
1060 than caching all files.
1061 - Especially Android devices nowadays assume that
1062 you want to index a folder at the time, whereas older MTP devices (such
1063 as those from Creative) would assume that you wanted to index the entire
1064 device as it was plugged in, and device firmware is now ever more tailored
1065 toward per-folder filetree walking. This makes it harder for the library
1066 to provide the right abstractions: do we provide an API for indexing the
1067 whole device which is unacceptably slow on new devices, or do we provide
1068 an API for indexing a directory at the time which will somehow work on
1069 older devices too? Shall we deprecate the older API?
1070- Detecting from vendor extension, can fix in newer extensions!
1071- Autoprobing on Linux
1072 - Color devices do not like autoprobing
1073 - Devices need different PIDs for every alternative interface due to
1074 the Windows USB stack.
1075 - Multimode USB - one PID for each mode due to Windows limitations not
1076 applicable to Linux, SONY devices have ~5 different PIDs for a single
1077 device.
1078 - Mode switch devices? Maybe we do this wrong.
1079- MTPZ, came and went. Apparently deprecated by Microsoft with Windows
1080 Phone 8.
1081- Ideas??
1082
1083