This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Tiva Lauchpad as USB Host for Android



While there have been a few posts in the past on this topic, there hasn't been much clarity. I understand that the Tiva board from where it stands now with regards to software will only communication via a traditional usb mass storage device class (usb msc).

Unfortunately ever since Honeycomb, Android has moved on from usb msc to mtp/ptp. Is it possible to extend the existing USB driver classes to support MTP? How ambitious of a project is this? I can't seem to find a suitable library to start from aside from libmtp which seems to primarily support POSIX compliant OS. 

The other option of course is Android Open Accessory protocol but I'm concerned about the future of that protocol given that there haven't been a great many devices that use them and the hype has sort of slumped since 2011. 

Thoughts? 

  • Hi Krithik


    Krithik Chandrashekar said:
    Is it possible to extend the existing USB driver classes to support MTP? How ambitious of a project is this?


    It’s rather easy, unless you would like to cover most of enormous MTP commands.
    To exchange files with Android MTP device, small set of MTP commands is enough.
    For example,
    to list up WAV files on the target Android,
    - GetNumObjects( WAV(0x3008) )   - number of WAV files (objects)
    - GetObjectHandles( WAV(0x3008) ) - array of WAV file handles
    - GetObjectInfo( handle ) - file name, size, etc.
    To read out one of above WAV files,
    - GetPartialObject( handle, offset, len )

    Tsuneo

  • Here is outline of Android USB MTP device

    0. References
    (**1) USB Still Image spec
    http://www.usb.org/developers/docs/devclass_docs/usb_still_img10.zip
    (**2) MS OS descriptors spec 1.0
    http://msdn.microsoft.com/en-us/library/windows/hardware/gg463179.aspx
    - OS_Desc_Ext_Prop\OS_Desc_Intro.doc
    (**3) MTP spec v1.1
    http://www.usb.org/developers/devclass_docs/MTPv1_1.zip
    (**4) Android MTP gadget source code (f_mtp.c)
    https://android.googlesource.com/kernel/common/+/cf7addf29b41f90d979267b2c5111ec6d137231a/drivers/usb/gadget/f_mtp.c


    1. USB Device
    1.1 Device Configuration
    USB configuration of Android MTP device almost follows the USB Still Image spec (**1) (or PTP: ISO 15740, PIMA 15740:2000). It exposes an interface with three endpoints, bulk IN/OUT and an interrupt IN. The only difference is, the interface triad is assigned to Vendor specific (0xFF, 0xFF, 0x00), instead of Still Image (0x06, 0x01, 0x01)

    Here is a typical descriptors, taken from Nexus7 (Android 4.4.3) in high-speed

    const U8 DeviceDesc[] =
    {
        0x12,        // bLength
        0x01,        // bDescriptorType (DEVICE)
        0x00,        // bcdUSB (ls byte)
        0x02,        // bcdUSB (ms byte)
        0x00,        // bDeviceClass (Defined in Interface)
        0x00,        // bDeviceSubClass (Defined in Interface)
        0x00,        // bDeviceProtocol
        0x40,        // bMaxPacketSize0
        0xD1,        // idVendor (ls byte)
        0x18,        // idVendor (ms byte)
        0xE1,        // idProduct (ls byte)
        0x4E,        // idProduct (ms byte)
        0x28,        // bcdDevice (ls byte)
        0x02,        // bcdDevice (ms byte)
        0x01,        // iManufacturer
        0x02,        // iProduct
        0x03,        // iSerialNumber
        0x01,        // bNumConfiguration
    };

    const U8 ConfigDesc[] =
    {
        // Configuration Descriptor =================================
        0x09,        // bLength
        0x02,        // bDescriptorType (CONFIGURATION)
        0x27,        // wTotalLength (ls byte)
        0x00,        // wTotalLength (ms byte)
        0x01,        // bNumInterfaces
        0x01,        // bConfigurationValue
        0x00,        // iConfiguration
        0x80,        // bmAttributes
        0xFA,        // bMaxPower

        // Interface 0 (alt 0) Descriptor --------------------------
        0x09,        // bLength
        0x04,        // bDescriptorType (INTERFACE)
        0x00,        // bInterfaceNumber
        0x00,        // bAlternateSetting
        0x03,        // bNumEndpoints
        0xFF,        // bInterfaceClass (Vendor)
        0xFF,        // bInterfaceSubClass (Vendor)
        0x00,        // bInterfaceProtocol
        0x04,        // iInterface

        // Endpoint 1-IN Descriptor
        0x07,        // bLength
        0x05,        // bDescriptorType (ENDPOINT)
        0x81,        // bEndpointAddress
        0x02,        // bmAttributes
        0x00,        // wMaxPacketSize (ls byte)
        0x02,        // wMaxPacketSize (ms byte)
        0x00,        // bInterval

        // Endpoint 1-OUT Descriptor
        0x07,        // bLength
        0x05,        // bDescriptorType (ENDPOINT)
        0x01,        // bEndpointAddress
        0x02,        // bmAttributes
        0x00,        // wMaxPacketSize (ls byte)
        0x02,        // wMaxPacketSize (ms byte)
        0x00,        // bInterval

        // Endpoint 2-IN Descriptor
        0x07,        // bLength
        0x05,        // bDescriptorType (ENDPOINT)
        0x82,        // bEndpointAddress
        0x03,        // bmAttributes
        0x1C,        // wMaxPacketSize (ls byte)
        0x00,        // wMaxPacketSize (ms byte)
        0x06,        // bInterval
    };

    1.2 Device identification
    To identify Android MTP device, MS OS descriptors spec 1.0 (**2) is applied.
    Firstly, MS OS String Descriptor is retrieved, and then, MS OS Feature Descriptor identifies MTP device.

    - MS OS String Descriptor
    The standard Get_Descriptor( String: index 0xEE ) retrieves MS OS String Descriptor
     bmRequestType:  0x80    (Device-to-Host, Standard, Device)
     bRequest     :  0x06    (GET_DESCRIPTOR)
     wValue       :  0x03EE  (Type:String / index:0xEE)
     wIndex       :  0x0000  (Language ID)
     wLength      :  0x0012

    The device returns this string descriptor.
    const U8 MS_OS_String[] = {
      0x12,   // bLength
      0x03,   // bDescriptorType (STRING)
              // qwSignature
      'M’, 0x00, ’S’, 0x00, 'F’, 0x00, ’T’, 0x00, '1’, 0x00, ’0’, 0x00, '0', 0x00,
      0x01,   // bMS_VendorCode
      0x00    // bPad
    };

    - MS OS Feature Descriptor
    This vendor request retrieves MS OS Feature Descriptor
     bmRequestType:  0xC0    (Device-to-Host, Vendor, Device)
     bRequest     :  0x01    (bMS_VendorCode, given by MS OS String Descriptor)
     wValue       :  0x0000  (0x00 / interface)
     wIndex       :  0x0004  (Extended compat ID)
     wLength      :  0x0028

    The device returns this feature descriptor.
    const U8 MS_OS_Feature_MTP[] = {
      0x28, 0x00, 0x00, 0x00,  // dwLength
      0x00, 0x01,              // bcdVersion (1.0)
      0x04, 0x00,              // wIndex (Extended compat ID)
      0x01,                    // bCount (number of sections)
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // RESERVED
      0x00,                    // bFirstInterfaceNumber
      0x01,                    // RESERVED
      0x4D, 0x54, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // compatibleID ('MTP')
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // subcompatibleID (Secondary ID)
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00              // RESERVED
    };


  • 2. USB Transport layer

    This layer follows Still Image Bulk-only Protocol (7 Still Image Ca pture Device Class-Specific Protocol, usb_still_img10.pdf).
    Like USB MSC-BOT, two- or three- phase (with or without Data phase) transaction is assigned.

    Command (bulk OUT)  --  Data (bulk IN or OUT), if any  --  Response (bulk IN)

    Each phase exchanges a Container packet of this format (7.1 Bulk-Pipe Containers, usb_still_img10.pdf)

    Container Structure
    Offset  Length  Field Name  (all fields are LSB-first)
    __________________________________________________
      0       4     Container Length
      4       2     Container Type   0 undefined
                                     1 Command Block
                                     2 Data Block
                                     3 Response Block
                                     4 Event Block
      6       2     Code
      8       4     TransactionID
     12      ??     Payloads[]       variable length array, if any
                                     each payload is 4 bytes word
    ___________________________________________________

    In the Command phase, a Container of Container Type = 0x0001 (Command Block) is sent from host. So does for Data phase (0x0002: Data Block), and in the Response phase Response Container (0x0003: Response Block) is returned from device.

    In this sequence, the Containers share the same TransactionID. The TransactionID used
    for the OpenSession operation shall be 0x00000000. The first operation issued by an
    Initiator after an OpenSession operation shall possess a TransactionID of 0x00000001 (PIMA1674:2000, 9.3.1 TransactionID)


    2.1. MTP Command/Data/Response examples

    OpenSession - Command/Response
    Initiator (host) sends this packet to the device over bulk OUT endpoint.
    Bulk OUT: 10 00 00 00 01 00 02 10 00 00 00 00 01 00 00 00
      Length  : 0x0000 0010
      Type    : 0x0001 Command Block
      Code    : 0x1002 OpenSession
      TransactionID : 0x0000 0000
      Payload : SessionID - 0x0000 0001

    Responder (device) returns this packet over bulk IN endpoint
    Bulk IN : 0C 00 00 00 03 00 01 20 00 00 00 00
      Length  : 0x0000 000C
      Type    : 0x0003 Response Block
      Code    : 0x2001 OK
      TransactionID : 0x0000 0000


    GetObjectPropsSupported - Command/Data/Response
    Initiator sends this block for GetObjectPropsSupported( MP4 Container ) command
    Bulk OUT: 10 00 00 00 01 00 01 98 02 00 00 00 82 B9 00 00
      Length  : 0x0000 0010
      Type    : 0x0001 Command Block
      Code    : 0x9801 GetObjectPropsSupported
      TransactionID : 0x0000 0002
      Payload : 0x0000 B982 - MP4 Container

    Responder returns this Data phase
    Bulk IN : 24 00 00 00 02 00 01 98 02 00 00 00 0A 00 00 00
              01 DC 02 DC 03 DC 04 DC 07 DC 09 DC 0B DC 41 DC
              44 DC 4E DC
      Length  : 0x0000 0024
      Type    : 0x0002 Data Block
      Code    : 0x9801 GetObjectPropsSupported
      TransactionID : 0x0000 0002
      ObjectPropCode Array
        Array size: 0x0000 000A
          0xDC01     StorageID
          0xDC02     Object Format
          0xDC03     Protection Status
          0xDC04     Object Size
          0xDC07     Object File Name
          0xDC09     Date Modified
          0xDC0B     Parent Object
          0xDC41     Persistent Unique Object Identifier
          0xDC44     Name
          0xDC4E     Date Added

    Also, responder returns this Response phase
    Bulk IN : 0C 00 00 00 03 00 01 20 02 00 00 00
      Length  : 0x0000 000C
      Type    : 0x0003 Response Block
      Code    : 0x2001 OK
      TransactionID : 0x0000 0002

    3. Development Tools
    MS distributes this (legacy, but useful) kit for development of MTP device/host

    Media Transfer Protocol Porting Kit, version 12
    http://www.microsoft.com/en-us/download/details.aspx?id=19153

    This kit includes,
    - WpdMon
     is a traffic monitoring tool for WPD API and MTP on a PC
    This tool is useful to know the way how Windows applications manipulate MTP device using MTP commands

    - DirectMTP
     is a tool used to access MTP devices with direct MTP commands on a PC
    It works as a test bench for MTP device implementation,
    also as a test bench of MTP command sequence for MTP host development.

    Tsuneo

  • Hi Tsuneo, 

    Really appreciate the feedback so far. I'll take a look at them in detail and get back if I have questions. 

    Thank you!
    Krithik