Samsung Galaxy Nexus USB connection to Linux
by bats on Jan.11, 2012, under Android, Fedora, Linux, RedHat
Originally Published Dec 20, 2011 @ 10:19. Updated frequently as we learn more about this device and how it interfaces with Linux.
This first section is geared towards Linux distributions that use the rpm package manager. Either way, it should give you an idea of how to accomplish the same with any other distribution.
This phone and many others are doing away with the mass storage device method of connecting to a pc for transferring files. MTP is the new method, but each vendor and device id must be added to the mtp library before the device is recognized. In Fedora, this is relatively easy to do. Pull down the source rpm using yumdownloader. Install yumdownloader if you don’t have it already.
# yumdownloader –source libmtp
# rpm -ivh libmtp*.src.rpm
# cd ~/rpmbuild/SOURCES
RedHat has already created a diff to append a bunch of new devices to the code. We just need to add ours to the list. The US model of the Samsung Galaxy Nexus is 04e8:685c. You can verify yours by typing lsusb. There are many ways to do this but this should give you an idea of how it works. Add the following lines to the diff file. I’d add it with the other Samsung devices, then find the diff hunk above that section and increment 5 to the destination file range (since we’re adding five lines to the total patch, not just the two we’re inserting). In my case this works..
Edit device-db.patch.
//@@ -248,13 +254,28 @@ <– old
@@ -248,13 +254,33 @@
Then append this to the bottom of the Samsung section.
+ // From: Ryan Sweat <rsweat@gmail.com>
+ { “Samsung”, 0x04e8, “Samsung Galaxy Nexus”, 0x685c, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL },
Save the file, then recompile the rpm.
# cd ../SPECS
# rpmbuild -bb libmtp.spec
If successful, it should build the packages with US Samsung Galaxy Nexus device support. Remove the current rpms and install the new versions.
# rpm -e libmtp libmtp-hal libmtp-devel –nodeps
# yum localinstall ~/rpmbuild/RPMS/x86_64/libmtp-*.x86_64.rpm ~/rpmbuild/RPMS/x86_64/libmtp-hal-*.x86_64.rpm ~/rpmbuild/RPMS/x86_64/libmtp-devel-*.x86_64.rpm
That should be enough for apps like Amarok to detect and use the device. If you want to mount the device’s filesystem, you’ll need mtpfs as well. Here’s how to install mtpfs. It has a few dependencies. The configure process will complain if others are needed.
# sudo yum install fuse fuse-devel libmad libmad-devel libid3tag-devel libid3tag
# wget http://www.adebenham.com/debian/mtpfs-1.0.tar.gz
# tar xvzf mtpfs-1.0.tar.gz
# cd mtpfs-1.0
# ./configure && make && sudo make install
Edit /etc/fuse.conf as root and append or uncomment this line.
user_allow_other
Create a mountpoint, owned by your regular userid.
# sudo mkdir /mnt/nexus
# sudo chown user:user /mnt/nexus
Mount the device.
# mtpfs -o allow_other /mnt/nexus
To unmount it.
# fusermount -u /mnt/nexus
** EDIT 20111223 **
To make this device auto mount when connected, edit /lib/udev/rules.d/60-libmtp.rules or create it if it doesn’t already exist. Find or add the entry for the Samsung Galaxy Nexus and the two ACTION lines beneath it.
# Samsung Galaxy Nexus
ATTR{idVendor}==”04e8″, ATTR{idProduct}==”685c”, SYMLINK+=”libmtp-%k”, ENV{ID_MTP_DEVICE}=”1″, ENV{ID_MEDIA_PLAYER}=”1″
ACTION==”add”, RUN+=”/usr/local/bin/mtpfs -o allow_other /mnt/nexus”
ACTION==”remove”, RUN+=”fusermount -u /mnt/nexus”
I’ve found that if the screen is unlocked when connected, it mounts at what would normally be the external storage (sd card). Otherwise, it mounts where only the Playlist directory is viewable or accessible. This is likely a security feature. I’m surprised it’s able to mount at all with the phone locked or turned off, as a commenter below has mentioned.
** End edit 20111223 **
** Edit 20111228 **
Interestingly, the Product ID changes when this device is put into USB debugging mode from PID=685c to PID=6860. The latter is recognized by libmtp as a Samsung GT-P7510/Galaxy Tab 10.1/S2 which, according to the patch, was “Reported by anonymous sourceforge user”. It mounts successfully with this PID with flags DEVICE_FLAG_UNLOAD_DRIVER and DEVICE_FLAG_LONG_TIMEOUT, even though the device isn’t an Galaxy Tablet. I’m curious as to how two Samsung devices can have identical USB PIDs, and if it’s standard behaviour for a product id to change when put in usb debugging mode.
** End edit 20111228 **
** Update 20120111 **
A commenter, gofasterplease, discovered an issue with writing certain multimedia files to the mounted filesystem. I started testing myself after reading his comment and I was also experiencing this as well.
What I did notice is that certain file extensions seem to be blocked from being written to certain locations on the filesystem. The source file is opened O_RDONLY, the destination file descriptor is opened, O_WRONLY|O_CREAT|O_EXCL, written to, then closed and exits with status 0, meaning successful, at least from the standpoint of the application performing the write. I tested 30 well known audio types, including proprietary ones, and mp3 is the only one I could find that exhibits this behaviour. I initially thought they were blocking the proprietary mp3 format for licensing reasons, but that’s not possible since I’ve uploaded 19k+ mp3s to Google Music and constantly steam them throughout the day (which caches the mp3s locally).
After further testing, I finally determined files ending in .mp3 are automatically moved to the Music/ directory. I’m ashamed it took me an hour or so of testing to figure this out, but this activity is difficult to detect when it’s done on the android side after the file is successfully written. Hope this helps explain some odd behavior when copying files around.
** End Update 20120111 **
December 21st, 2011 on 5:03 am
Thanks for the walk-through!
There are a few things with the presentation though. Somehow the WordPress software translates double dashes into em-dashes, and converts ” into typographically correct quotations marks.
(I tried to copy-paste into a terminal, and couldn’t work out why it didn’t work…)
I’m still stuck though. I can mount it with mtpfs, but I only ever see one directory, called ‘Playlist’, which is empty. (If I connect it to a Windows PC I see a lot more.)
Any ideas?
December 21st, 2011 on 9:15 am
Wow, now it suddenly works!
I have no idea what I did differently, but it suddenly works.
I saw another post (here: http://forum.xda-developers.com/showthread.php?t=1360173&page=6 ) that recommended setting the flags DEVICE_FLAG_UNLOAD_DRIVER and DEVICE_FLAG_LONG_TIMEOUT, instead of the DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL.
Do you know what the difference might be?
December 21st, 2011 on 10:46 am
Did you change the flags to DEVICE_FLAG_UNLOAD_DRIVER and DEVICE_FLAG_LONG_TIMEOUT and recompile to get it working? Or did it just start working with DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL? I’ll check the code to see what those flags are for. The flag I used was taken from a device very similar to the Galaxy Nexus. I’ll see if I can fix the html issues so users can copy/paste without problems.
December 21st, 2011 on 5:53 pm
Thorsten, thanks for the info. I checked the source code, and I can’t find anything defined for “DEVICE_FLAG_LONG_TIMEOUT”. I’d be surprised if it compiles, much less works at all with that flag in libmtp-1.0.6-5.
Here’s the details on the other flag you mentioned…
#define DEVICE_FLAG_UNLOAD_DRIVER 0×00000002
/**
* This means that the PTP_OC_MTP_GetObjPropList is broken and
* won’t properly return all object properties if parameter 3
* is set to 0xFFFFFFFFU.
*/
I don’t think that’s necessary on this phone. I haven’t run into this issue.
Also, related to your question about my default flag, this is what DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST_ALL does. It basically prevents two kernel modules from using the usb device at the same time. IE: mass storage and mtplib at the same time, which isn’t applicable here, so this flag probably isn’t even necessary, but it doesn’t hurt.
Also, I believe I fixed the wordpress char issues above so that users trying to copy and paste the source code should work now. Good luck and let me know your results! If necessary I’ll just provide a patch file and details on how to implement it.
December 22nd, 2011 on 3:19 am
Just to clarify:
It now works fairly reliably using only your modifications (I haven’t tried all the available flags in any systematic way – but I’d love to do something like that.)
One thing that caught me by surprise though was that the phone must be switched on for the mount to work. Once it’s connected it’s OK to switch the phone off, but if the phone was switched off it often only shows the ‘Playlist’ directory, and it’s read-only.
December 23rd, 2011 on 3:19 am
Edited to add a section explaining how to have this device automatically mount and unmount using udev rules.
January 9th, 2012 on 9:54 pm
So bizarre, I got mtpfs to work on fedora 14… I mounted it as /mnt/gnex
I can access it but when i copy movie files, it works, and then the files disappear after the copy has completed… wierd.
January 11th, 2012 on 8:03 pm
gofasterplease, I was surprisingly able to reproduce your issue with multimedia files. I did some debugging and appended to the blog post above with my findings. Let me know if this answers or explains the behavior you’re seeing.
January 26th, 2012 on 11:06 pm
I have gone through the above steps several times and I keep getting:
Device 0 (VID=04e8 and PID=685c) is UNKNOWN.
I’m sure it has something to do with syntax in the patch file. Can you please post yours?
January 27th, 2012 on 2:18 am
Sure. The patch for 1.0.6-5.fc15 is here: http://blog.offenders.org/device-db.patch
January 27th, 2012 on 7:30 pm
Thanks. Works now.
February 20th, 2012 on 5:34 am
I got this error “warning: user mockbuild does not exist – using root”
February 21st, 2012 on 12:25 am
it’s not a fatal error and should continue. best practice is to not install or build the thing as root.
August 7th, 2012 on 10:28 pm
I feel like I’m missing something. Where is the devices-db.patch file located?
October 16th, 2012 on 6:20 pm
You can eliminate the step about rebuilding libmtp on Fedora 17 but still need to install mtpfs. I got it working but had to make sure “Enable USB Debugging” on my phone was disabled. It would just hang but never mount when running the mtpfs command. Also, when debugging was enabled the device showed up with an ID of 04e8:6860 then 04e8:685c when debugging was disabled.
Though, when browsing the filesystem it appears all the directories are laid out properly but then when browsing in them no files are appearing. Any suggestions on this?
November 6th, 2012 on 11:40 pm
Why?
April 29th, 2013 on 5:50 am
I comment each time I appreciate a article on a website or if I
have something to add to the discussion. Usually it’s triggered by the passion displayed in the article I browsed. And after this post Samsung Galaxy Nexus USB connection to Linux