USB Driver Program Summary

Source: Internet
Author: User

USB devices are very complex and consist of a number of different logical units, and the relationships between these logical units can be described simply as follows:

L devices (Usb_device) typically have one or more configurations (Usb_host_config)

L configuration usually has one or more interfaces (Usb_interface)

The L interface usually has one or more settings

L setting has no or more than one endpoint (Usb_host_endpoint)

Detailed descriptions of various body bodies are at INCLUDE/LINUX/USB. H

The most essential step in writing a device driver is the initialization of the module,

USB Driver Module Initialization:

Module_init (Usb_***_init);

Module_exit (Usb_***_exit);

Usb_***_init Register USB Driver

Usb_register (&usbdriver)

Usb_***_exit Uninstall the driver.

Usb_deregister (&usbdriver)

The Usb_driver body includes the following main elements:

static struct Usb_driver Skel_driver = {

. Owner = This_module,

. Name = "Skeleton",//driver name

. id_table = skel_table,//list of devices supported by the driver

. Probe = Skel_probe,//Probe function

. Disconnect = Skel_disconnect,//Break function

};

Detailed structure:

struct Usb_driver {

struct module *owner;

const char *name;

Int (*probe) (struct usb_interface *intf,

const struct USB_DEVICE_ID *id);

void (*disconnect) (struct usb_interface *intf);

Int (*ioctl) (struct usb_interface *intf, unsigned int code, void *buf);

Int (*suspend) (struct usb_interface *intf, pm_message_t message);

Int (*resume) (struct usb_interface *intf);

const struct USB_DEVICE_ID *id_table;

struct Device_driver driver;

};

The list of supported devices can be driven by a variety of method definitions, and the example on LDD (3rd) uses this macro to obtain the USB_DEVICE_ID structure:

Usb_device (vendor,product)

It matches the driver according to the manufacturer ID, and the device ID.

Probe function probe:

When a device is installed and the USB core believes that the driver should be processed, the probe function is invoked and the probe function first checks the device information passed to it to determine if the driver is really appropriate for the device.

When the driver is not supposed to control the device for some reason, the port function is invoked and it can do some cleanup work.

The driver typically probes for the endpoint address, type, and buffer size of the device.

The Usb_register_dev (interface,&usb_class) function is generally invoked in the probe function to register the device to the USB core. As long as the function is invoked, make sure that both the device and the driver are in the right state to handle the requirements of the user's access device.

The two parameter structures are as follows:

The USB_INTERFACE structure describes the USB interface, where important fields are:

struct Usb_host_interface *altsetting: An array of interface structures that contains all the optional settings that may be used for the interface.

unsigned num_altsetting; The number of optional settings that the altsetting pointer refers to.

Unsigned usb_host_interface *cur_altsetting: A pointer to the inside of the altsetting array that represents the current active setting of the interface.

int minor: Contains the secondary device number that the USB core assigns to the interface.

Other fields in the USB_INTERFACE structure do not need to be considered in the USB driver.

The Usb_class_driver structure Body contains the following:

Char *name; SYSFS is used to describe the name of the device.

struct File_operations *fops;

mode_t mode; The access mode for the Devfs file created for this driver.

int minor_base; This is the starting value for the secondary device number range assigned to the driver.

Usb_class_driver instance:

static struct Usb_class_driver Skel_class = {

. Name = "usb/skel%d",

. FoPs = &skel_fops,

. Mode = S_IFCHR | S_IRUSR | S_IWUSR | S_irgrp | S_iwgrp | S_iroth,

. minor_base = Usb_skel_minor_base,

};

FoPs Structure Body Example:

static struct File_operations Skel_fops = {

. Owner = This_module,

. Read = Skel_read,

. write = Skel_write,

. open = Skel_open,

. Release = Skel_release,

};

The device is registered in the Porbe function, and the driver is registered in the driver module initialization, which is clearly divided

: Register_usb_dev (usb_interface,&usb_class_driver); Register USB device to USB core

: Usb_register (&usb_driver); Registering the driver in the USB subsystem

Looking back, we're going to write the functions in File_operations, and the simple Read function example is as follows:

Static ssize_t skel_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos)

{

struct Usb_skel *dev;

int retval = 0;

dev = (struct Usb_skel *) file->private_data;

/* Do a blocking bulk read to get data from the device * *

retval = Usb_bulk_msg (Dev->udev,

Usb_rcvbulkpipe (Dev->udev, dev->bulk_in_endpointaddr),

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.