Hardware driver in Linux-USB device

Source: Internet
Author: User
Tags ide hard drive lenovo

Hardware driver in Linux system-USB device (on)

 

 

There are more and more USB devices, while Linux is still not completely plug-and-play in hardware configuration. how to configure and use Linux is becoming a major problem for us. This article describes how to use and configure the driver and how to compile the driver. For general users, we can clarify the Linux device driver mode and provide convenience for better configuration and use of USB devices. For programmers who want to develop USB driver for Linux systems, it provides a preliminary opportunity to learn about the USB drive architecture.

Preface

USB stands for "Universal Serial Bus", meaning "Universal Serial Bus ". It was proposed jointly by Compaq (Compaq), Dec, IBM, Intel, NEC, Microsoft, and Northern Telecom (Northern Telecom) in November 1994, the main purpose is to solve the drawbacks of too many interface standards. USB uses a 4-pin connector as the standard connector to connect all peripherals in the form of chrysanthemum petals. It transmits data in serial mode. Currently, the maximum data transmission rate is 12 Mbps, supports concurrent operations on multiple data streams and multiple devices, and supports hot swapping of peripherals.

At present, although the USB interface has only developed 2 generations (USB 1.1/, USB), USB integrates all the advantages of a multi-platform standard-including cost reduction and compatibility increase, A large number of external devices can be connected, combining advanced features and quality. It gradually became a PC interface standard and entered a period of rapid development.

Correctly supporting and configuring common USB devices for Linux is an essential step.

Related technical basics

Module (driver)

A module is a program running in the kernel space. It is actually a target object file and cannot be run independently without links. However, it can be loaded into the system and run as part of the kernel, in this way, the kernel functions can be dynamically expanded. The main use of the module is to implement the device driver.

In Linux, a hardware driver can be directly loaded into the kernel code. When the kernel is started, the hardware device can be driven. The other is to compile a. o file in the module mode. When the application needs to be added to the kernel space for running. Therefore, a hardware driver usually refers to a driver module.

Device Files

For a device, it can have a corresponding logical device node under/dev. The node is saved as a file, but it is not a file in the general sense, it is a device file. More specifically, it is a device node. This node is created using the mknod command, which specifies the master device number and secondary device number. The primary device number indicates a certain type of device, which generally corresponds to a specific driver. The secondary device number generally distinguishes different attributes, such as different usage methods, locations, and operations. This device number is obtained from the/proc/devices file. Therefore, the device node is in the directory only when the driver is in the kernel. The main function of this device number (especially the master device number) is to declare the drive program used by the device. Drivers correspond to device numbers one by one. When you open a device file, the operating system knows the driver corresponding to the device.

SCSI Device

SCSI is a standard computer interface different from IDE. Nowadays, most flat-board scanners, CD-R recorders, MO optical transceiver and so on gradually tend to use the SCSI interface, coupled with SCSI and can provide a high-speed transmission channel, so, access to SCSI devices more and more users. Linux supports many SCSI devices, such as SCSI hard drives, SCSI optical drives, and SCSI tape drives. More importantly, Linux provides an IDE device for SCSI simulation (ide-scsi.o module), we usually simulate the IDE optical drive for access to the SCSI Optical Drive. In Linux, many software can only operate the SCSI Optical Drive. For example, most of the recording software and some media playing software. Generally, our USB storage device simulates access to the SCSI hard disk.

Linux hardware driver architecture

For a hardware, the Linux driver is like this: first, we must provide a. o driver module File (Here we only describe the module mode, in fact, the kernel mode is similar ). To use this driver, first load and run it (insmod *. O ). In this way, the driver will register with the system based on its own type (character device type or block device type, for example, the mouse is a character device and the hard disk is a block device, after successful registration, the system will return a primary device number, which is the unique identifier of the system, the main device we see with LS-L/dev/had must be 3 ). The driver creates a device file that is generally placed in the/dev directory based on the master device number (mknod command is used to create it, it must use the master device Number Parameter ). To access this hardware, you can run open, read, write, and other commands on the device file. The driver will receive the corresponding read and write operations and proceed according to the corresponding functions in its module.

There are also a few more related things: one is the/lib/modules/2.4.xx Directory, which is the module for the current kernel version. As long as your module dependency is correct (you can set it through depmod), you can load it using the modprobe command without knowing the location of the specific module File. The other is the/etc/modules. conf file, which defines aliases of some common devices. The system can find the driver module correctly when this device is needed. For example, alias eth0 E100 indicates that the driver module of the first ENI is E100.

Configure a USB device

To enable Linux USB support in the kernel, first go to the "USB support" section and enable the "Support for USB" option (the corresponding module is USB core. O ). Although this step is quite straightforward, the subsequent Linux USB settings will be confusing. In particular, you now need to select the correct USB master controller driver for the system. The options are "EHCI" (ehci-hcd.o for the corresponding module), "uhci" (usb-uhci.o for the corresponding module), "uhci (alternate driver)", and "OHCI" (usb-ohci.o for the corresponding module ). This is where many people are confused about Linux USB.

To understand "EHCI" and what it is like, you must first know that each motherboard or PCI Card that supports inserting a USB device requires a USB master controller chipset. This special chipset operates with the USB device that is inserted into the system and handles all low-level details necessary to allow the USB device to communicate with other parts of the system.

Linux USB drivers have three different USB master controller options because there are three different types of USB chips on the motherboard and PCI Card. The "EHCI" driver is designed to support new high-speed USB 2.0 chips. The "OHCI" driver is used to provide support for USB chips on non-PC systems (as well as on PC boards with sis and Ali chipsets. The "uhci" driver is used to support USB implementations on most other PC boards, including Intel and. You only need to select the type corresponding to the USB support you want to enable "? HCI driver. If you are confused, you can enable "EHCI", "uhci" (either of them, there is no obvious difference between them) and "OHCI" for the sake of insurance ". (Zhao Ming Note: According to the document, EHCI already includes uhci and OHCI. However, in my personal test, it is impossible to add EHCI separately, generally, I load uhci or OHCI Based on the motherboard type, and then load EHCI to support the USB2.0 device ).

Enable "USB support" and appropriate "? After HCI "USB master controller driver, you only need to perform a few steps to enable and run USB. You should enable "Preliminary USB device filesystem" and ensure that all drivers specific to the actual USB Peripheral that will be used with Linux are enabled. For example, to enable support for USB game controllers, I enabled "USB Human Interface Device (full hid) support ". I also enabled "input core support" and "joystick support" under the main "input core support" section ".

If no USB device information is available under/proc/bus/USB, enter the following command to manually mount the USB device file system to/proc/bus/USB:

# Mount-T usbdevfs NONE/proc/bus/USB

To automatically mount the USB file system during system boot, add the following line to the/proc mounting line in/etc/fstab:

None/proc/bus/USB usbdevfs defaults 0 0

Module configuration method

In many cases, our USB driver is not included in the kernel. In fact, we only need to load them one by one based on the modules it needs. You can enable it.

First, make sure that the corresponding support is selected in the module mode during kernel compilation. In this way, we can see the corresponding. o file in the/lib/modules/2.4.xx directory. When loading a module, we only need to run modprobe XXX. O. (modprobe mainly loads the modules that have been registered by the depmod system. insmod is generally used to load specific. O files)

It is critical to correspond to some modules under the USB device.

USB core. O must support the most basic modules required by USB;

Usb-uhci.o (already mentioned );

Usb-ohci.o (already mentioned );

Uhci. o another uhci driver. I don't know what it is for. Generally, I don't want to load it. It will crash;

Ehci-hcd.o (USB2.0 has been mentioned );

Hid. o usb man-machine interface device, like a mouse or a keyboard;

Usb-storage.o USB storage equipment, U disk and other use;

Related modules:

Ide-disk.o IDE hard drive;

The ide-scsi.o simulates the SCSI interface of the IDE device;

Scsi_mod.o SCSI support.

Note one of the following:

Probe all Luns on each SCSI Device

It is recommended that you choose not to display only one card reader that supports multiple ports at the same time. If the module mode is used, install it with parameters or add the following items to/etc/modules. conf in advance to support multiple Luns.

Add options scsi_mod max_scsi_luns = 9

Sd_mod.o SCSI hard drive;

Sr_mod.o SCSI disc;

General support for SG. o scsi (used in some USB flash drives and SCSI probes );

Common USB devices and their configurations.

Linux 2.4 kernel supports no more than 20 devices. It supports almost all general-purpose devices, such as the keyboard, mouse, modem, and printer, and constantly adds new devices from the manufacturer, such as digital cameras, MP3, and NICs. The following describes how to use the most common devices:

USB mouse

The keyboard and mouse are low-speed input devices. For PS/2 interfaces that have been recognized by the user, the USB keyboard and USB mouse do not seem to be much more advantageous. Most of the current mouse uses the PS/2 interface, but the USB interface also has more and more mouse, the two have their own advantages: Generally, the bandwidth of the USB mouse interface is greater than the PS/2 mouse, that is to say, in the same period of time, the number of USB mouse scans is more than PS/2, so that the USB mouse is more precise in positioning. At the same time, the default sampling rate of USB interface mouse is also relatively high, up to 125Hz, while the PS/2 interface only has 40Hz (Windows 9x/ME) or 60Hz (Windows NT/2000 ).

For USB devices you must first Insert the corresponding USB controller module: usb-uhci.o or usb-ohci.o

Modprobe USB-uhci

To make the USB mouse work properly, you must first Insert the module USB mouse. O and mousedev. o

Modprobe USB mouse

Modprobe mousedev

If you install the hid input layer and input core as modules, it is also necessary to start the hid and input modules.

Modprobe hid

Modprobe Input

 

 

Hardware driver in Linux system-USB device (lower)

 

USB keyboard

Generally, most of the keyboards we use today are PS/2, and USB keyboards are rare. However, the development of the keyboard will move closer to the USB interface. There are basically no many requirements for using a USB keyboard. You only need to set the support for the USB keyboard in the BIOS of the motherboard, and then you can use the USB keyboard completely in various systems, it can also be used in plug-and-play and hot swapping, and provides two USB ports: allowing you to easily connect devices with USB connectors to your keyboard, not the back of a computer.

Similarly you must of course First insert the corresponding USB controller module: usb-uhci.o or usb-ohci.o

Modprobe USB-uhci

Then you must insert the usb kbd. O and keybdev. O modules into the keyboard so that the USB keyboard can work properly. In this case, the system command is as follows:

Modprobe usbkbd

Modprobe keybdev

If you install the hid input layer and input core as modules, it is also necessary to start the hid and input modules.

USB flash drive and USB card reader

Digital Storage devices are now quite common for us. CF cards, SD cards, memory stick and other memory cards are everywhere. Generally, their card readers are all USB interfaces. In addition, many MP3 and digital cameras use USB interfaces and computers for data transmission. Our USB flash drive and USB hard drive have become our essential equipment as mobile storage devices.

In Linux, these devices are usually driven in a way called USB-storage. To use them, you must load this module.

Modprobe USB-storage

Of course, USB core. O and usb-uhci.o or USB-OHCI are also indispensable. In addition, if the SCSI support in your system is also a module method, the following modules should also be loaded.

Modprobe scsi_mod

Modprobe sd_mod

After loading these modules, We can insert a USB flash disk or a memory card to find a SCSI hard disk in the system and mount it correctly, (The SCSI hard disk is usually/dev/sd ?, Refer to the FAQ below ).

Mount/dev/sda1/mnt

Other USB devices supported by Linux.

Modem -- (common ).

Network Device

Camera-(common) for example, ov511;

Connecting cable-enables your two computers to use USB cables for network functions. Usbnet .;

Monitor -- (I have never seen it ).

Game pole

TV box -- (more common );

Tablet -- (more common );

Scanner -- (more common );

Burner -- (more common );

Printer -- (more common ).

Note: not all driver modules mentioned above need to be manually loaded. Many systems will automatically load these modules when starting or when your application needs them, it is easy for you to check when you cannot use a USB device. You only need to use lsmod to ensure that the above modules have been loaded by the system, and your device should be able to work properly. Of course, note that some modules already exist in the kernel startup mode (these module files cannot be found in/lib/modules/2.4.xx ).

Most common USB Problems

1. The RedHat 7.3 is suspended after the system of the USB device is installed.

With a USB device, when you have just installed RedHat 7.3 and started it for the first time, it will always die. The main reason is that there are two controllers, USB-uhci and EHCI-HCD, detected during Linux installation. However, there is a conflict after the USB-uhci is loaded and then EHCI-HCd is started. The analysis shows that the redhat7.3 system kernel has problems in supporting the USB2.0 standard. This problem does not exist in other versions of Linux.

Solution: Use the command line to pass the init =/sbin/init parameter when LILO or grub is started. In this case, the shell is started directly without running other services. Then run Mount-O remount and RW/enable/write. By default, only mount/is read-only for the system directly started by init, and then VI/etc/modules. config File, delete a line of the alias usb-controller1 EHCI-HCD. Or add # comment out before, and then mount-O remount, RO/enable/read-only, to avoid direct shutdown and damage to the file system, then you can press Ctrl-alt-Delete to directly restart.

Perhaps you have a simpler method: Change the USB keyboard and mouse to The PS2 interface, and modify the/etc/modules. config file after startup.

2. We already know that a USB flash disk is simulated as a SCSI device in Linux. How can we know it corresponds to that SCSI device?

Method 1: speculation. Usually you insert a SCSI device for the first time, which is SDA, and the second is SDB. When you start Linux to insert a USB flash drive, you can try SDA. If you change the USB flash drive, it may be SDB. Note the following two special cases: 1) You are using a Lenovo USB flash disk, which may have two device zones (one for encryption or startup of the computer), so that two SDA and SDB may be used at a time, change the USB flash drive to SDC or sdd. 2) Lenovo digital computers may already have a six-in-one card reader. It is also a USB storage device. It occupies one or two SCSI device numbers.

Method 2: View information. In fact, as long as you advance the usb-storage.o, scsi_mod.o, sd_mod.o module load (directly in the kernel can also be), when you insert and unplug the U disk, the system will automatically play the information as follows:

SCSI device SDA: 60928 512-byte hdwr sectors (31 MB

SDA: write protect is on

Based on this information, you will know that it is on SDA. Of course, your system information level may be relatively high, and the above information may not be displayed. At this time, you only need to tail/var/log/messages to see it.

Method 3: Similarly, CAT/proc/partitions can also view partition information, where SD? It is the corresponding USB flash drive. If there is no SD device, check whether your SCSI module and USB-storage module are correctly loaded.

3. When I use a USB flash drive or a memory card, should I mount/dev/SDA or/dev/sda1?

This is a historical issue. The initial size of the memory card is very small. Many manufacturers directly use the storage when using it, excluding partition table information. With the constant expansion of the memory card size, it introduces a concept similar to hard disk partition. For example, you can divide/dev/hda into primary partitions hda1 and hda2 extended partitions hda3, and then the extended partition hda3 into logical partitions hda5, hda6, and hda7. In this way, the general USB flash drive is divided into a partition sda1, similar to dividing the entire partition of the hard disk into a primary partition hda1. In fact, we can use fdisk/dev/SDA to partition the storage card into sda1, sda2, or even sda5 and sda6 logical partitions. In fact, you do need this for a USB hard disk because it usually has a capacity of GB. Generally, it contains a laptop hard disk.

A fun question. In Linux, you use fdisk/dev/SDA to partition the USB flash disk. In Windows, you will find out how to find and format it, you can only find the size of the first partition on the USB flash drive. This is mainly because Windows drivers only support one partition for USB flash drives. Can you use it to hide and protect some files? Can you joke with some people who haven't played Linux: Your USB flash drive capacity is reduced by J.

Nowadays, many digital devices, like Windows, divide all USB flash drives into one. Therefore, when you treat USB flash drives, you usually Mount sda1. But for some special digital devices formatted U disks or memory cards (I have found a Lenovo USB flash disk that supports analog USB floppy disk and a digital camera ), you need to mount/dev/SDA. Because it does not have a partition table at all (if Mount/dev/sda1 normally works as a dead table ). In fact, as long as you have noticed the/proc/Partitions file, you should have noticed this information.

4. Each time you insert a USB flash drive, you need to find the file name of the corresponding device and manually mount the file. Can I insert a USB flash drive as in windows.

Yes, but you need to do some work. Here I only provide some information to help you try to complete the settings: the Linux Kernel provides a kind of support called hotplug, it allows your system to perform some work during the insertion and removal of PCI devices, USB and other devices. The automount function allows you to automatically mount and detach partitions of your drive, CD, and other devices. You can even create corresponding icons on the KDE Desktop for your convenience. The specific setting method will allow you to try it yourself. I am numb to using Linux. I just need to run a line of commands.

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.