Saturday, November 8, 2014

Adding rules for MTP and getting your device to work on OpenSUSE

MTP is the Media transfer protocol. It allows files to be transferred to and from media devices. MTP does not allow operations such as editting and modifying the file. MTP  is different from USB mass storage. With MTP the device still has full control over the file system.

Of late Android devices only allow you to transfer data to/from them via MTP/PTP. New devices may or may not be detected.

In order to ensure that a device is detected on openSuse you will need to learn to add and manage udev rules.

We will take the example of a pretty new budget android phone called the Xaomi RedMi 1s. 
When we plug it into Suse and enable MTP nothing appears to happen. Though we expect to be able to access the internal storage and SD Card via MTP.

This is because there are no rules on how to handle the MTP device.

I found that guides are very sparse online when it comes to MTP. So I decided to make a small write up to guide people.  

First we will install a package called "mtp-tools" this provides a number of command line tools for MTP.

zypper in mtp-tools

Once installed we make sure our device has MTP enabled and then we type "mtp-detect" command as root.

Sample output is shown below.
>mtp-detect
Unable to open ~/.mtpz-data for reading, MTPZ disabled.libmtp version: 1.1.6

Listing raw device(s)
Device 0 (VID=2717 and PID=1260) is UNKNOWN.
Please report this VID/PID and the device model to the libmtp development team
   Found 1 device(s):
   2717:1260 @ bus 2, dev 52
Attempting to connect device(s)
ignoring libusb_claim_interface() = -6PTP_ERROR_IO: failed to open session, trying again after resetting USB interface
LIBMTP libusb: Attempt to reset device
Android device detected, assigning default bug flags

<----- Output Clipped ----->

What is most important here is the VID and the PID with this information rules can be formulated.

By adding an MTP rule we can get KDE to pickup the device.

MTP rules as of libMTP version 0.9 onward are stored in /usr/lib/udev/rules.d/69-libmtp.rules

The udev rules for MTP devices follow the below syntax typically.

ATTR{idVendor}=="<VID here>", ATTR{idProduct}=="<PID here>", SYMLINK+="libmtp-%k", ENV{ID_MTP_DEVICE}="1", ENV{ID_MEDIA_PLAYER}="1"

So in our case assuming VID of 27217 and PID of 1260 the udev rule becomes:

ATTR{idVendor}=="2717", ATTR{idProduct}=="1260", SYMLINK+="libmtp-%k", ENV{ID_MTP_DEVICE}="1", ENV{ID_MEDIA_PLAYER}="1"

There are more options which can be added here such as "mode" and "group" but these are rarely required. In most cases setting ENV{ID_MTP_DEVICE} and ENV{ID_MEDIA_PLAYER} to "1" should suffice.

After the rule line has been added along with the existing rule lines in /usr/lib/udev/rules.d/69-libmtp.rules you are almost set.

Now all that is left is to apply the new rules and restart the udev service with the below 2 commands.

udevadm control --reload-rules
service udev restart

These two commands are used to reload rules and restart the udev process with the new rules. Now disabling and enabling MTP on the device should allow you to connect to the device via MTP from OpenSuse.

Hope this short guide helps.

Troubleshooting:
Q 1) I have followed allt he above rules, the MTP device(s) do not show up on KDE still. What do I do ?
1) Test if mtp works from terminal.
 as user type the command mtp-filetree wait for the command to complete initially you will only see few string containing device info. Once the command finishes (may take a minute or more even) you should see a list of files on your device. If you do then that means MTP is working properly.

Sample output below:

Device 0 (VID=2717 and PID=1260) is UNKNOWN.
Please report this VID/PID and the device model to the libmtp development team
Attempting to connect device(s)
Android device detected, assigning default bug flags
Device: (NULL)
Storage: Internal storage
1 FolderFoo
  2 .data
    478 b697b2b6ae559e26f3fc90abb449faa2
    480 7c7320086a99708c28138fd086553e33
    481 eb0d265b69315673573eb5a47d4b8df4
    8534 58074bc9565a44d9c57b11394be867e0
    8535 230d3b2452c6b7e32b74b2beae943a49
    13204 ed38625755998a993a67dd1e93a5fac6
    13205 02fd4baa976f28991a35bfd05a5fa269

Here you can see a folder called  "FolderFoo" and the contents of another sub-folder folder inside "FolderFoo" called  ".data" and its contents. If you see this kind of output that means MTP protocol level communication between your system and your device and your linux PC is working fine.

This indicates the problem is most likely a DE problem. For KDE you need to install a package called "kio_mtp" by using the code below.

zypper in kio_mtp

Note: the package may be called by a slightly different name if so just run

zypper se mtp

and look for a package with the 'kio' words in it and install.

No comments:

Post a Comment