Monday, December 10, 2012

MTP: The new way to exchange the data


This blog explores the differences between the older Mass storage Class MSC and the newer Media Transfer Protocol MTP methods of sharing embedded device memory when connected to a PC.
Storage is a key component of any embedded handheld device a mobile phone or other convergence devices. To transfer the data stored in these devices, technologies such as USB or Bluetooth are used, the former being more predominant. Generally when the USB shares the memory of a mobile device, it is shared as a drive on the host PC. The USB class used is known as Mass Storage Class (MSC), and it allows you to manage the drive completely like a local hard drive connected to the PC. But in recent Android versions (Ice cream, Sandwich and later), a newer class named MTP Media Transfer Protocol is made default to share mobile memory over USB to a host PC. So what necessitated this change? Let’s take a brief look at the two USB classes to understand why MTP is preferred over MSC.
Before going into advantages of MTP, let us first explore how these two protocols work, with the help of Linux.
An overview of USB MSC
The MSC protocol defines how data exchange can take place between USB host and a USB compliant device. Whenever you see the ubiquitous pen drive for all your data transfer activities, MSC is the underlying protocol. USB devices using the MSC class are equivalent to an external hard drive connected to host. These devices can be used just like in-PC drives, to drag and drop files. Thus, the USB host can interact with different flash/hard drives without having any knowledge about the underlying storage system.
Linux USB MSC architecture
Figure 1below shows the block level architecture when an MSC device is connected to a standard Linux host. The Linux device’s MSC architecture consists of two parts: the storage subsystem and the USB subsystem. In the Linux framework, the Virtual File System (VFS) layer is used to abstract the storage and the USB layers. The gadget driver, being USB controller specific, collects the USB transfers and identifies to which function the transfer belongs (e.g. the Ethernet packet, the MSC packet, etc). If it is an MSC packet, the gadget driver sends the packet mass storage driver. Inside the class driver, the SCSI commands are decoded, and appropriate storage operations are done using the VFS layer. SCSI is a standard for the transfer of data between two devices. The entire blocks are implemented in kernel space.
Fig. 1 Linux USB MSC Architecture
On the host side, the USB core drivers detect the USB device and expose it as a block device. The data is transferred to the block layer as SCSIcommands, the data is transferred to the block layer as SCSI commands, with the help of the storage driver.
An overview of the Media Transfer Protocol (MTP)
MTP was introduced by Microsoft to enable data exchange between a USB host (initiators) and devices (responders) with ‘intelligent ‘storage capabilities (those with some sort of user interactivity). These include smartphones, tablets and portable audio players. ‘Media’ in Media Transfer Protocol does not just mean audio/video; it encompasses all binary data, including text files. Devices supporting MTP come up as ‘portable device’ and not a drive as in USB MSC. All the content (songs, images, videos) on devices are represented by objects, and object handles are used in order to reference a logical object on the device. Once the device is connected to the USB host, enumeration takes place, and all associated metadata, such as the file creation, the time of modification, size, folder details, etc, are passed to the host PC. After this, data exchange can begin.

Fig. 2 Architecture of MTP
Android implementation of MTP
In Linux, sysfs is an interface used for communication between user space and kernel space. Figure illustrates the block-level architecture when an MTP device is connected to a standard host. The MTP implementation on the device side is divided across user space and kernel space.User space components: The mtp daemon (mtpd) runs in the JVM (Java Virtual Machine) of Android. It loads and calls the USB Device MTP Library via JNI (Java Native Interface). The USB device MTP Library takes care of decoding and responding to MTP commands.Kernel space components: The USB device MTP Function Driver handles the MTP class-specific control requests like opening or closing the session as per the user’sinstructions. It also controls reading/writing of files from the storage media through the Virtual File System.   On the host side, the USB core drivers communicate with the USB device. Applications like Windows Media Player on Windows use the libmtp library to communicate with device. Based on the requests from the applications, mtpd on the device would respond using the USB drivers.
The advantages of MTP   
From both the architectures, you can see that MSC works on SCSI commands, and the host has unrestricted access to the device. The device cannot modify the data without releasing its connection from the host, if mounted. If the device is given permissions to modify the data when there is a connection with the host, there are chances of corrupting the data, or even the file system. That is why when you mount an SD card of an Android device to a PC, you cannot access the SD card using the Android device until you unmount it from the PC. MTP has a solution to this problem. It manages the files, unlike MSC, which manages storage. The media player on the host reads and writes entire files on the device.With MTP, Windows Media Player has an option to sync media content on the PC and the device. You can even customize the data content for the sync options based on the ratings etc. With MISC, this can be achieved only with special applications.     Unlike MSC, MTP enables the monitoring of device initiated events, and the changes in device properties.                                                                       
MTP does not allow the transfer of unsupported file formats, unlike mass storage, where the user needs to check for compatibility after the file transfer.            
Lastly, MSC and MTP represent two efficient data exchange methods. While MSC has been the de-facto standard followed until the recent past, it fails to address the needs posed by modern-day intelligent storage devices. While flash-based devices will continue to use MSC, we will see more and more portable devices with hard drives switching over to MTP. However, it is always the user’s prerogative to choose between the free way or the safe way to manage data i.e., whether to use MSC or MTP.