Tizen Native API  10.0
USB Interface

This API is used to handle tasks related to USB interfaces.

This module can be used to operate on one of the interfaces of the device.

Overview

Data structures and operations described here are related to USB interface. Each interface has number of endpoints used for performing transfer operations.

Related Features

This API is related with the following features:

  • http://tizen.org/feature/usb.host

It is recommended to design feature related codes in your application for reliability.

You can check if a device supports the related features for this API by using System Information, thereby controlling the procedure of your application.

To ensure your application is only running on the device with specific features, please define the features in your manifest file using the manifest editor in the SDK.

More details on featuring your application can be found from Feature List.

Functions

int usb_host_claim_interface (usb_host_interface_h interface, bool force)
 Claims a USB interface on a device for further I/O.
int usb_host_release_interface (usb_host_interface_h interface)
 Releases a previously claimed interface on a device.
int usb_host_interface_get_number (usb_host_interface_h interface, int *number)
 Gets the "number" of the given USB interface handle.
int usb_host_interface_get_num_endpoints (usb_host_interface_h interface, int *num_endpoints)
 Gets the number of endpoints in given USB interface.
int usb_host_interface_get_endpoint (usb_host_interface_h interface, int ep_index, usb_host_endpoint_h *ep)
 Gets an endpoint via index from given USB interface.
int usb_host_interface_set_altsetting (usb_host_interface_h interface, int altsetting)
 Sets an alternative setting for given USB interface.
int usb_host_interface_get_altsetting (usb_host_interface_h interface, int *altsetting)
 Gets current alternative setting from an interface.
int usb_host_interface_get_str (usb_host_interface_h interface, int *length, unsigned char *data)
 Gets a string describing the given USB interface into a buffer.

Typedefs

typedef struct
usb_host_interface_s * 
usb_host_interface_h
 An opaque handle representing a USB configuration interface.

Typedef Documentation

typedef struct usb_host_interface_s* usb_host_interface_h

An opaque handle representing a USB configuration interface.

This type represents USB interface. An interface is a part of configuration and can have multiple endpoints. This handle can be obtained by usb_host_config_get_interface().

Since :
3.0

Function Documentation

int usb_host_claim_interface ( usb_host_interface_h  interface,
bool  force 
)

Claims a USB interface on a device for further I/O.

Claims interface on a device. To perform I/O operations on interface user has to claim it. Remember to call usb_host_release_interface() when communication with the device is finished.

Since :
3.0
Parameters:
[in]interfaceThe bInterfaceNumber of interface to claim
[in]forceSet to true to auto detach kernel driver, set to false to not detach it
Returns:
0 on success, otherwise a negative error value
Return values:
USB_HOST_ERROR_NONESuccessful
USB_HOST_ERROR_NOT_FOUNDRequested interface does not exist
USB_HOST_ERROR_RESOURCE_BUSYAnother program or driver has claimed the interface
USB_HOST_ERROR_NO_SUCH_DEVICEDevice has been disconnected
USB_HOST_ERROR_INVALID_PARAMETERInvalid parameter was passed
USB_HOST_ERROR_DEVICE_NOT_OPENEDThe device was not opened
#include <usb_host.h>

void use_interface(usb_host_interface_h interface)
{
    int ret;

    ret = usb_host_claim_interface(interface, true);
    if (ret != USB_HOST_ERROR_NONE)
        handle_error(ret);

    // Use the interface here

    ret = usb_host_release_interface(interface, true);
    if (ret != USB_HOST_ERROR_NONE)
        handle_error(ret);
}
int usb_host_interface_get_altsetting ( usb_host_interface_h  interface,
int *  altsetting 
)

Gets current alternative setting from an interface.

Retrieves the currently selected alternative setting.

Since :
4.0
Parameters:
[in]interfaceInterface handle
[out]altsettingIndex of alternative setting set for the given interface
Returns:
0 on success, otherwise a negative error value
Return values:
USB_HOST_ERROR_NONESuccessful
USB_HOST_ERROR_NOT_SUPPORTEDNot supported
USB_HOST_ERROR_INVALID_PARAMETERInvalid parameter was passed
#include <usb_host.h>
#include <stdio.h>

void print_current_altsetting(usb_host_interface_h interface)
{
    int ret, altsetting;

    ret = usb_host_interface_get_altsetting(interface, &altsetting);
    if (ret != USB_HOST_ERROR_NONE)
        handle_error(ret);

    printf("Current alternative setting index: %d\n");
}
int usb_host_interface_get_endpoint ( usb_host_interface_h  interface,
int  ep_index,
usb_host_endpoint_h ep 
)

Gets an endpoint via index from given USB interface.

Returns the correct endpoint of the provided interface by its index.

Since :
3.0
Parameters:
[in]interfaceInterface handle
[in]ep_indexindex of endpoint to retrieve (counting from 0)
[out]epEndpoint handle
Remarks:
ep handle is no longer valid when config will be destroyed. There is no need to destroy it, it is done automatically when the configuration is destroyed.
Returns:
0 on success, otherwise a negative error value
Return values:
USB_HOST_ERROR_NONESuccessful
USB_HOST_ERROR_INVALID_PARAMETERInvalid parameter was passed
#include <usb_host.h>

#define ENDPOINT_INDEX 3

usb_host_endpoint_h get_endpoint(usb_host_interface_h interface)
{
    int ret;
    usb_host_endpoint_h ep;

    ret = usb_host_interface_get_endpoint(interface, ENDPOINT_INDEX, &ep);
    if (ret != USB_HOST_ERROR_NONE)
        handle_error(ret);

    return ret;
}
int usb_host_interface_get_num_endpoints ( usb_host_interface_h  interface,
int *  num_endpoints 
)

Gets the number of endpoints in given USB interface.

Gets the number of endpoints supported by the provided interface.

Since :
3.0
Parameters:
[in]interfaceAn interface
[out]num_endpointsNumber of endpoints in interface
Returns:
0 on success, otherwise a negative error value
Return values:
USB_HOST_ERROR_NONESuccessful
USB_HOST_ERROR_NOT_SUPPORTEDNot supported
USB_HOST_ERROR_INVALID_PARAMETERInvalid parameter was passed
#include <usb_host.h>
#include <stdio.h>

void print_num_endpoints(usb_host_interface_h interface)
{
    int ret, num_endpoints;

    ret = usb_host_interface_get_num_endpoints(inteface, &num_endpoints);
    if (ret != USB_HOST_ERROR_NONE)
        handle_error(ret);

    printf("Number of endpoints: %d\n", num_endpoints);
}
int usb_host_interface_get_number ( usb_host_interface_h  interface,
int *  number 
)

Gets the "number" of the given USB interface handle.

Gets the index of the interface, as passed to usb_host_config_get_interface().

Since :
3.0
Parameters:
[in]interfaceAn interface
[out]numberNumber of given interface
Returns:
0 on success, otherwise a negative error value
Return values:
USB_HOST_ERROR_NONESuccessful
USB_HOST_ERROR_NOT_SUPPORTEDNot supported
USB_HOST_ERROR_INVALID_PARAMETERInvalid parameter was passed
#include <usb_host.h>
#include <stdio.h>

void print_interface_index(usb_host_interface_h interface)
{
    int ret, number;

    ret = usb_host_interface_get_number(dev, &number);
    if (ret != USB_HOST_ERROR_NONE)
        handle_error(ret);

    printf("Interface index: %d\n", number);
}
int usb_host_interface_get_str ( usb_host_interface_h  interface,
int *  length,
unsigned char *  data 
)

Gets a string describing the given USB interface into a buffer.

Returns a string representation of the given USB interface.

Since :
3.0
Parameters:
[in]interfaceAn interface
[in,out]lengthPointer to a variable containing the current size of the buffer, which will be modified to actual usage
[out]dataBuffer to store string
Returns:
0 on success, otherwise a negative error value
Return values:
USB_HOST_ERROR_NONESuccessful
USB_HOST_ERROR_NOT_SUPPORTEDNot supported
USB_HOST_ERROR_OVERFLOWThere was no space in buffer
USB_HOST_ERROR_INVALID_PARAMETERInvalid parameter was passed
Precondition:
device which interface is part of must be opened by usb_host_device_open() or usb_host_device_open_with_vid_pid()
#include <usb_host.h>
#include <stdio.h>

#define BUFFER_SIZE 128

void print_interface_str(usb_host_interface_h interface)
{
    int ret, length = BUFFER_SIZE - 1;
    unsigned char data[BUFFER_SIZE];

    ret = usb_host_interface_get_str(dev, &length, data);
    if (ret != USB_HOST_ERROR_NONE)
        handle_error(ret);

    data[length] = '\0';

    printf("Interface: %s\n", data);
}
int usb_host_interface_set_altsetting ( usb_host_interface_h  interface,
int  altsetting 
)

Sets an alternative setting for given USB interface.

Selects an alternative setting represented by the provided index.

Since :
3.0
Parameters:
[in]interfaceInterface handle
[in]altsettingIndex of new alternative setting for given interface
Returns:
0 on success, otherwise a negative error value
Return values:
USB_HOST_ERROR_NONESuccessful
USB_HOST_ERROR_INVALID_PARAMETERInvalid parameter was passed
#include <usb_host.h>

void set_0_altsetting(usb_host_interface_h interface)
{
    int ret = usb_host_interface_set_altsetting(interface, 0);
    if (ret != USB_HOST_ERROR_NONE)
        handle_error(ret);
}

Releases a previously claimed interface on a device.

Releases interface previously claimed by usb_host_claim_interface(). This is a blocking function.

Since :
3.0
Parameters:
[in]interfaceThe bInterfaceNumber of interface to release
Returns:
0 on success, otherwise a negative error value
Return values:
USB_HOST_ERROR_NONESuccessful
USB_HOST_ERROR_NOT_FOUNDInterface was not claimed
USB_HOST_ERROR_NO_SUCH_DEVICEDevice has been disconnected
USB_HOST_ERROR_INVALID_PARAMETERInvalid parameter was passed
USB_HOST_ERROR_DEVICE_NOT_OPENEDThe device was not opened
USB_HOST_ERROR_NOT_SUPPORTEDNot supported
#include <usb_host.h>

void use_interface(usb_host_interface_h interface)
{
    int ret;

    ret = usb_host_claim_interface(interface, true);
    if (ret != USB_HOST_ERROR_NONE)
        handle_error(ret);

    // Use the interface here

    ret = usb_host_release_interface(interface, true);
    if (ret != USB_HOST_ERROR_NONE)
        handle_error(ret);
}