Usb Gadget Serial Driver

When working as USB device, the embedded platform may connect to a development computer and work as any previously loaded profile such as a serial port, network card, or mass-storage device. The USB device driver exposes the connected devices through the file system at /dev/bus/usb/. The interface can expose different USB device gadgets.

  1. Usb Gadget Serial Drivers
  2. Linux Usb Gadget Driver
  3. Usb Gadget Driver

Serial gadget

The serial gadget exposes a TTY-style serial line interface, usable with minicom and similar tools. Most Linux hosts can talk to this interface using the generic usb-serial driver. The latest versions of this driver implement the CDC ACM class. This driver works with MS-Windows usbser.sys driver, the Linux cdc-acm driver, and many other USB host systems.

For detailed information on how to set up the driver on both Windows and Linux systems, see Documentation/usb/gadget_serial.txt. Follow the instructions in this file for exposing your target as a serial port to the eyes of a USB host.

In Windows 8.1 and earlier versions of the operating system, Usbser.sys is not automatically loaded when a USB-to-serial device is attached to a computer. To load the driver, you need to write an INF that references the modem INF (mdmcpq.inf) by using the Include directive. The directive is required for instantiating the service, copying inbox binaries, and registering a device interface GUID that applications require to find the device and talk to it. Choose 'Browse my computer for driver software in the pop-up window. Browse to the folder that you saved the archive contents to and select that folder. Click 'Install' when prompted. Once the installer finishes you will see 'SEL USB Ethernet/RNDIS Gadget' under Network Adapters in.

Loading the serial gadget

Load the serial gadget driver module (full details at Documentation/usb/gadget_serial.txt):

Connect the target platform to a Linux host computer via the USB cable. Linux will create a device node called /dev/ttyGS0 on the target side and a device node called /dev/ttyACM0 on the host side. (Node names may vary in different distributions.)

The USB connection between target and host now works as a serial line.

Testing the serial gadget

You can verify the serial connection of the two ports:

  • Configure the serial port of the host computer:
  • Configure the serial port of the target platform:
  • On the host, read the contents arriving at the new serial port:
  • On the target, write a text line to the serial port:

You will see the text line arrive at the host computer.

Opening a console shell via USB serial

The following instructions describe how to open a console shell via the USB serial port.

  • On the target side, execute a console on the serial gadget with the getty command (baudrate is mandatory but irrelevant):

The setsid command avoids permission problems when running getty from an interactive shell.

Usb Gadget Serial Drivers

  • On the host side, open a minicom or putty session on the USB serial port /dev/ttyACM0 to access the shell.

Note Settings like speed or parity for the serial terminal session (and for the getty command) are irrelevant, as the communication takes place at USB speed. The serial communication settings are emulated.

Unloading the serial gadget

Linux Usb Gadget Driver

To unload the USB serial gadget from the system:

Ethernet gadget

With the Ethernet gadget, the target platform enumerates to the host computer as an Ethernet device. It uses the usbnet driver on Linux hosts or Microsoft's RNDIS driver on Windows hosts.

Loading the Ethernet gadget

To load the Ethernet gadget:

Linux creates an Ethernet interface in the target called usb0 and assigns random MAC addresses to the target and the host.

The last line appears when you connect the USB cable between the target and the host. Connect it now if you haven't done so already.

Configuring the Ethernet interface on the target

Give the network interface usb0 an IP address, for example:

Configure the Ethernet interface on the host computer

Linux hosts

Load the usbnet driver on the host:

Linux creates an Ethernet interface on the host side, also called usb0.

Give this new Ethernet interface an IP address in the same subnet as the target's IP address. (The following command requires root permissions:)

Now the target can be accessed via the USB cable as if it was an Ethernet port. You can send a ping or open a telnet session from the host to the target, or vice versa.

Windows hosts

Connect the USB cable from the target to an MS-Windows host computer. MS-Windows detects a new USB RNDIS/Ethernet Gadget, installs the appropriate driver, and creates a new network adapter on the system.

Configure the IP address of this network adapter by going to Control Panel > Network and Internet > Network Connections. Set, for example, an IP address of 192.168.44.1, subnet mask 255.255.255.0.

Now the target can be accessed via the USB cable as if it was an Ethernet port. You can send a ping or open a telnet session from the host to the target, or vice versa.

Unloading the Ethernet gadget

To unload the USB Device gadget:

Mass-storage gadget

With the mass-storage gadget, the target platform enumerates to the host computer as a SCSI disk drive. A file or block device can be used as a backing store for the drive.

Using a file as backing storage

Creating a backing storage file

You must prepare the backing storage file before the gadget can use it. The backing storage is a regular file that must be created with its full desired size. This must be done before loading the gadget driver, but it only has to be done once.

The following example shows how to create a backing file of 32 MiB in the root directory of the target:

Partitioning the backing storage

Creating the backing storage file is like having a raw disk; you still need to create partitions and install a file system before you can use it.

Use the fdisk program to partition the backing file and to specify the values of Cylinders, Heads, and Sectors of the backing file. These values are arbitrary, but since the storage gadget driver uses a sector size of 512 bytes, the formula is:

512 * Cylinders * Heads * Sectors = Total bytes of backing file storage

For our example of 32 MiB storage file, we can use values of:

Cylinders = 32

Heads = 16

Sectors = 128

So that 512*32*16*128 = 32 MiB

On the target we run fdisk with the CHS values over the backing file:

To create a primary partition that occupies the full size of the backing file:

  • Press n for 'new'.
  • Press p for 'primary'.
  • Press 1 for partition number 1.
  • Press INTRO to accept default values for any remaining parameters.

The new partition is created by default as a Linux (ext4 file system) partition. To change the partition type to FAT32:

  • Press t for 'type'.
  • Press b to select Win95 FAT32 file system.

Press p to print the partition table details and verify everything is correct:

Press w to write out the partition table to the backing storage:

At this point, the partition has been created but it doesn't hold a file system yet.

The easiest way to create a file system in the partition is to load the driver, connect the USB cable to the host PC, and create the file system on the recently created partition from the host computer.

Loading the mass-storage gadget

On the target, load the driver with:

The last line appears when you connect the USB cable between the target and the host. Connect it now if you haven't done so already.

Creating and mounting the file system

Linux hosts

A Linux host computer recognizes the target as a new SCSI device. Linux assigns a device node in the form sdx, where x is a letter index, and the partition appears as sdx1. To see exactly which device the host assigned, print out the system log messages with:

In the example, the host assigned sdb1 as the device node for the partition of our backing file.

On the host, create a VFAT file system into that partition (you might need root permissions):

Host PC
When formatting a device on your Host PC, verify that the device node dev/sdX corresponds to the connected USB device and not to your PC hard disk. Running the mkfs.vfat command on the wrong device node might format your hard disk.

Now, mount the device in the host so the host has read/write access to it. If your host PC is prepared to automatically mount devices, you can simply disconnect the USB cable and connect it again. After a few seconds, the PC automatically detects and mounts the device as a USB storage drive, with the size of the partition we created. Initially, the disk is empty.

If the PC doesn't auto mount the device you need to do it by hand (you might need root permissions):

In this example we've mounted the device into the folder /media/usbdisk. Now we can create files and folders within this folder.

The placeholder for the files and folders that we create in this folder is the 32 MiB file /backing_file that we created in the first steps.

Usb Gadget Driver

As with other USB devices, before removing the USB cable you need to unmount this folder:

Windows hosts

An MS-Windows host computer recognizes the target as a new USB mass storage device. A warning message appears to inform you that the disk is not yet formatted. Click Yes to enter the formatting tool.

Select FAT file system and an optional volume label (less than 11 characters). Then click Start.

Once the volume has been formatted with FAT file system you can use it as any other removable media and create folders, copy files, etc.

The placeholder for the files and folders that we create in the unit is the 32 MiB file /backing_file that we created in the first steps.

Using a block device as backing file

An alternative to using a backing file is to use an existing block device like a Compact Flash card, an SD card, a USB stick, or even a Flash partition. For example, if you have plugged in an SD card that contains two partitions, the Linux system on the target will have populated them similar to this:

where mmcblk1 is the card and mmcblk1p1 and mmcblk1p2 are the partitions.

Unmount the block device on the target

When using a block device for the file storage gadget, we recommended you first unmount the media on the target side. Otherwise, access from the host won't be synchronized with accesses from the target, as the system would behave like having the same media mounted twice in different mount points.

Make sure any partitions of the block device are unmounted, for example:

Load the mass-storage gadget

Driver

Load the mass-storage gadget passing the block device node (in the example the SD card device node) as backing file:

Now, when you connect the USB cable to a host computer, the two partitions of the SD card will be accessible from the host.

Unloading the mass-storage gadget

To unload the USB mass-storage gadget from the system: