We are writing a driver for a USB device. Pipe 0: what is usb?

    It so happened that I had to write from scratch a driver for usb display under windows. Having the opportunity - I will talk about the details of such an entertaining process.
    image

    USB is simple

    Despite the fact that writing drivers is considered quite difficult in comparison with application programming - and in this low-level world, programmers were not deprived of attention, the development of the usb bus was a big step in simplifying the creation of drivers.
    So, why is the usb bus so convenient, and what does the word Universal hide in the abbreviation?

    Previously, when a programmer was forced to write a driver to communicate with a device - he worked directly at the bus level, roughly speaking - if the device was connected with a 9-wire cable, because two of them are usually diverted for power - the programmer was manually forced to control up to 7 wires. Each device had its own idea of ​​how to use the port provided to it. For example, one device could transfer data to a computer through wires 1,2 and 5, use 3 and 7 for control, and 4 and 6 would be used to write data to the device, another device could have 2 wires for data transfer in both directions and 5 for control signals.

    All this had to be controlled in time, waiting for the reaction or readiness of the device. If the device performed more than one type of activity, it was necessary to know what functionality we are currently working with and, based on this, change the working methods. Under such conditions, debugging was difficult, and the programmer needed to learn his communication protocol on this port for each device. All this could not continue indefinitely, and the usb bus was invented.

    Tire structure

    The first thing you can see when working with usb devices in modern operating systems is that you no longer need to work physically with a data port. So - let's see what an arbitrary usb device is in a first approximation:

    image
    On the first line of defense we are waiting for the interface level, this is a logical separation of the device according to the tasks it performs, for example, if we have an external display, equipped in addition with a sound notification system - our device may well have a couple of interfaces, one for working with the display, another for working with sound, and, for example, a third for flashing the device itself.
    If we consider writing a driver for windows using the latest driver framework (WDF), then you can find out the number of interfaces and access them using a couple of lines of code.
    An interface is not the only separation level that exists in USB; each interface is divided into a set of so-called endpoints.

    Suppose we can both write a picture to the display and read the current picture from the display, in which case one endpoint will be used for reading, the other for recording. In addition to the direction of data transfer (In / Out), the endpoint has another rather useful property, it is the type of transfer, which has one of the following values:

    • control transfers - used by the host to configure the device during connection, to control the device and obtain status information during operation. The protocol provides guaranteed delivery of such packages;
    • Bulk Data Transfers - used when it is necessary to ensure guaranteed data delivery from host to function or from function to host, but the delivery time is unlimited;
    • interrupt transmissions - used when it is required to transmit single small data packets. Each packet needs to be transmitted for a limited time. Transmission operations are spontaneous and should be serviced no more slowly than the device requires;
    • isochronous transmissions - used for real-time data exchange, when at each time interval it is required to transmit a strictly defined amount of data, but information is not guaranteed (data transmission is carried out without repetition in case of failures, packet loss is allowed).

    In addition to the endpoints determined directly by the device logic, one (zero) endpoint is determined directly by the protocol, any usb device must have a zero endpoint. It is because of this that as soon as you connect the device to the computer, the operating system immediately recognizes the name of this device and tries to find a driver for it, without this standard endpoint, obtaining information about the device in the absence of a driver would be impossible.

    image

    Conclusion

    All data is transmitted via two conductors inside the usb cable (2 more are taken for power), this allows you to use a single connector for all usb devices.
    This is all you need to know about the bus in order to write your first driver, which is what we will do in the next article.
    Driver development kit for operating systems Windows XP, Vista, 7 - can be downloaded from the link: developer kit.

    In the next part: processing events related to the device, working with the backlight from the application, and a bit of reverse engineering.

    Photograph of the future patient:
    image

    Also popular now: