Three methods for Linux-based USB devices

Source: Internet
Author: User
Article Title: three methods are supported for Linux-based USB devices. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   Introduction
  
Universal Serial Bus (USB) is a fast and flexible interface for connecting accessories and computer workstations. It is widely used. In addition to the driver for the USB host controller, Linux also contains the driver for the USB device controller, especially the Controller Integrated on the Strongarm SA1110 processor. These controller drivers allow Linux-based embedded systems to communicate with hosts (either Linux or not) by using USB. There are three ways to add USB support to the embedded system running Linux, one of which can communicate with the USB host.
First, the most complex devices use a specially compiled kernel module to parse the intricate high-level protocols of the standard USB bus, and the corresponding USB Host custom drivers and applications to complete the connection. Second, some Linux-based devices use the bus as a simple point-to-point serial connection running on the host. Host applications use the USB programming interface provided by the host operating system, however, its external performance seems to be communicating through a typical serial port. Third, some other devices regard USB as an Ethernet network. They use a host as a gateway and connect the USB device to the office LAN or Internet. The common practice is to use a dedicated host driver to implement it.
The best solution depends on the time required for R & D, and what kind of USB interface should be made for specific embedded applications. The following describes how to apply these three methods to Linux-based USB devices one by one. This article describes how to use Linux on USB devices such as cameras and PDAs Based on Linux. Here, USB is a USB device consisting of a square connector rather than a flat rectangle connector.
  
   Kernel module
  
The first way to add USB to a Linux-based device is to compile a customized Linux kernel module. This method usually requires the development of the driver for the host operating system (Windows, Linux, and other OS.
With the help of customized kernel modules installed on devices, you can perform File System Simulation and so on, so that embedded applications treat their USB hosts as remote storage devices. Another potential use of this method is to form a device that stores forwarding characters, buffering data streams from embedded applications until the USB Host connection is established.
For Linux devices based on Strongarm, the USB application kernel module calls sa1100_usb_open () to initialize the kernel code of the peripheral USB device controller on the management chip. The module then calls sa1100_usb_get_descriptor_ptr () and sa1100_usb_set_string_descriptor () to set the USB host's given USB descriptor through the enumeration process. These descriptors include digital identifiers, body strings, and other information that hosts can use to identify devices. There is even a serial number field so that the host can uniquely identify the device or distinguish multiple instances of the same device on the USB.
The kernel module must establish the USB descriptor before starting USB communication, because the enumeration process is driven by the USB device controller and will be automatically executed once the USB host is connected. After everything is ready, the USB device module calls sa1100_usb_start () to tell the kernel to accept USB connection requests from the host. If the module calls sa1100_set_configured _ callback () before the USB Host connection, the kernel calls the provided callback function at the end of the enumeration process. The callback function visualizes the connection status of the device.
If USB communication is no longer needed, the kernel module of the device calls sa1100_usb_stop () and then sa1100_usb_close () to disable the USB controller of SA1100.
The Strongarm USB controller supports bulk-in and bulk-out for data transmission jobs. When receiving data packets from a USB host, the kernel module calls sa1100_usb_recv () to pass the data buffer and callback function address to it. Then, the underlying USB device control code of the kernel retrieves the bulk-out package from the host, stores the content in the buffer, and calls the callback function.
The callback function must extract data from the receiving buffer and store the data in other locations or add the buffer space to a queue to allocate a new buffer for receiving the next data packet. Then the callback function calls sa1100_usb_recv () twice to receive the next packet as needed. The process is similar to data transmission on a USB host. After a frame of data is aggregated, the kernel module transmits the data address, length, and callback address to sa1100_usb_send (). When the transfer is complete, the kernel calls the callback function.
Several examples of the USB driver on the host are provided in mainstream Linux versions and the original kernel source allocated by the Linux Kernel File organization. Used for Handspring Visor (drivers/usb/serial/visor. c) the module is one of the more concise and easy to understand, as a USB Host module template (drivers/usb/usb-skeleton.c.
  
   High-speed serial
  
For most practical applications, the USB bus can be considered as a high-speed serial port. It makes sense to perform prototype simulation on some types of embedded devices and applications. The Linux kernel of the Strongarm processor provides a ready-made USB device driver dedicated here, called usb-char.
When you want to communicate with a USB host, the Linux USB device application only opens a connection to its usb-char device node (character type, maximum 10, minimum 240), and then starts reading and writing data. Read () and write () operations will always return an error value until the USB host is connected. Once the connection is established and enumerated, communication starts, just as USB is a point-to-point serial port.
This USB data transfer method is very direct and practical, so the usb-char device is used efficiently. It also provides an important reference for the implementation of other USB communication methods.
The actual action of usb-char starts from the usbc_open () function, and some content is shown in List 1.
List 1: Enable the serial connection on the USB
Static int usbc_open (struct inode * pInode, struct file * pFile)
{
Int retval = 0;
/* Start usb core */
Sa1100_usb_open (_ sb-char ?;
/* Allocate memory for in-transit USB packets */
Tx_buf = (char *) kmalloc (TX_PACKET_SIZE, GFP_KERNEL | GFP_DMA );
Packet_buffer = (char *) kmalloc (RX_PACKET_SIZE, GFP_KERNEL | GFP_DMA );
/* Allocate memory for the receive buffer; the contents of this
Buffer are provided during read ()*/
Rx_ring.buf = (char *) kmalloc (RBUF_SIZE, GFP_KERNEL );
/* Set up USB descriptors */
Twiddle_descriptors ();
/* Enable USB I/o */
Sa1100_usb_start ();
/* Set up to receive a packet */
Kick_start_rx ();
Return 0;
The twiddle_descriptors () function establishes the USB descriptor of the device. After all descriptors are created, you are going to enumerate from the USB host and receive a data frame. The Code required by kick_start_rx () is generally a call to sa1100_usb_recv () to establish a callback. When a USB Host sends a data packet, the kernel of the device calls the rx_done_callback_packet_buffer () function through a callback to transfer the data packet content to the FIFO queue returned by read () on the usb-char device.
  
   Host
  
For a USB host running Linux, the usb-char USB Host module is called the USB serial module. Most Linux versions include the Usbserial module, though not automatically loaded. Before USB is connected to the device, USB serial is loaded by modprobe or insmod.
Once the USB device starts enumeration, the application on the host uses one of the USB serial device points (serial, maximum 188, minimum 0 or more) to communicate with the device. These nodes are usually named as/dev/ttyUSBn. The USB serial module reports the node it specifies to the USB device in the kernel message log record:
Usbserial. c: delete a universal Converter
USB serial. c: the universal converter is currently connected to ttyUSB0. After the connection is established, the application on the USB Host communicates with the USB device by reading and writing the specified node.
An alternative to the USB serial module on a Linux host is a library called libusb (libusb.sourceforge.net. This type of library uses a low-level kernel system call for USB data transmission, instead of using the USB serial module, which is more convenient for cross-Linux kernel version establishment and use. The Libusb Library also provides a large number of useful debugging functions, which can be helpful in debugging complex communication protocols running on the USB link. When libusb is used to communicate with a usb device using USB-char, the Linux host application uses the usb_open () function to establish a connection with the device. Then the application uses usb_bulk_read () and usb_bulk_write () to exchange data with the device.
  
   Ethernet on USB
  
Another option is to treat USB as an Ethernet network. Linux has a module that can implement this function on both the host and device. The iPAQ hardware does not have a serial port or a dedicated network interface. Therefore, the iPAQ Linux kernel adopts this communication policy. In the Linux kernel of StrongARM, the usb-eth module (arch/arm/mach-sa1100/usb-eth.c) simulates fictitious Ethernet devices using USB as physical media. Once created, this network interface is assigned an IP address, otherwise it will be treated as a common Ethernet hardware. Once the USB host is connected, the usb-eth module enables the USB device to "see" the Internet (if there is an Internet), ping other IP addresses, or even "talk" about "DHCP, HTTP, NFS, telnet, and e-mail. In short, any application running on the actual Ethernet interface will be fully implemented on the usb-eth interface, because they cannot tell whether they are using real Ethernet hardware.
On a Linux host, the corresponding Ethernet-over-USB Kernel module is called USB net. When the USB net module is installed and the USB connection of the device is established, the USB net module creates a fictitious Ethernet interface similar to the actual hardware for the host kernel and user applications, the Host application can check the existence of the USB device by running the ping test on the IP address of the device. If the ping test is successful, the device is added.
  
   Conclusion
  
Linux is no longer just a USB host. Today it is also a suitable choice for USB devices. USB communication in Linux is flexible and easy to use.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.