USB device driver guide (8)

Source: Internet
Author: User
With the last question, we continue to learn how the Operating System reads and writes USB devices through USB disk. Let's first look at the USB/class/storage/Disk/sci2/usbdisk6.def file. In this file, we can see that the DLL has exported a total of 14 functions, two of which are the USB diskattach and USB diskdetach called by the device driver in the previous content, the rest is a set of stream-driven interfaces starting with DSK. It is easy to see that USB disk provides services to the operating system in the form of stream-driven.

For the sake of clarity, we do not learn the following large numbers of programs, but only care about reading and writing devices. So let's look at the program file disk. C. We are disappointed to find the dsk_read and dsk_write functions, because both functions are like
Unreferenced_parameter (pdevice );
Unreferenced_parameter (pbuffer );
Unreferenced_parameter (bufferlength );

Debugmsg (zone_err, (text ("dsk_read/N ")));
Setlasterror (error_invalid_function );
Return 0;
This implementation means that the user cannot use this device through the regular readfile and writefile functions. What should I do? Does it mean that the disk cannot be read or written? Of course not. We should immediately think of the dsk_iocontrol () function. When some devices cannot use conventional file operation functions, we can use the deviceiocontrol () User Function, this function calls the dsk_iocontrol function in the driver.

In this function, we find the handler for commands such as ioctl_disk_read. The most critical sentence is scsirwsg (pdevice, psgreq, pdevice-> Lun, bread ), that is, a scsirwsg function is called.

In the scsi2.c program, we found this function, in which SG refers to a Data Structure for reading and writing the buffer, which is actually a structure with a buffer and length, it is a general read/write data structure of the disk device under CE, which can be in diskio. h. In this function, we find that it calls scsireadwrite () again to perform read and write operations. Find this function, which contains our most important call line, that is, the usbsdatatransfer () function is called, do you remember where I met this function? That's right, it's in the driver of the USB device.

Through this process, we found that all the SCSI functions are only preparing some buffers and data structures, and they are not operating on the hardware, the driver is used to operate hardware. It can be seen that the device driver has a strong hierarchy, and the lower layer is dedicated to physical devices, the upper layer is an abstract device for the operating system, the lower layer is a physical entity such as a USB flash disk, and the upper layer is a folder. The two complete the normal operation of the device in the operating system through a certain communication or call mechanism.
Return to the usbmsc. C program and find the usbsdatatransfer function. This function is very simple. Call cbit_datatransfer () or bot_datatransfer () based on the transmission protocol.

 

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.