Posts

Showing posts from May, 2019

How to write your own USB host device driver in Linux?

Image
In this post, I will show you how you can easily write minimal USB device driver. Objective: I have a 16 GB SanDisk USB thumb drive which should be detected by my driver when connected to system and my driver should be notified when this thumb drive gets disconnected from system. This thumb drive has Vendor id - 0X0781 and Product id - 0x5567 . Here is the minimal driver sample code(rax_usb.c): Let me elaborate above driver code: First we need to define usb_driver structure which identifies USB interface driver to USB core.  usb_drive Following fields are ** MUST ** for any USB interface driver and others are optional. @ name : The driver name should be unique among USB drivers,  and should normally be the same as the module name @ probe : Called to see if the driver is willing to manage a particular interface on a device.  If it is, probe returns zero and uses usb_set_intfdata() to associate driver-specific data with the interface. It may also u

What are Endpoints in USB?

Image
What are Endpoints?  The mechanisms used for USB communication are data buffers called Endpoints and used for data transfers. Endpoints are unidirectional which means it can carry data into only one direction(except control endpoint), either from the host to device and vice versa  In USB terminology, the direction of an endpoint ( and transfers to or from them) is based on the host. Thus, IN always refers to transfers to the host from a device and OUT always refers to transfers from the host to a device. USB devices can also support bi-directional transfers of control data  Endpoint 0 is reserved in every devices for control purposes and used in enumeration process to get information of device, configuration, interface and endpoint descriptors Following are four types of USB Endpoints:  CONTROL INTERRUPT BULK  ISOCHRONOUS  CONTROL  Small in size  Used to allow access different parts of the USB devices  Used for configuring device

Probe function in Linux USB device driver in brief

Image
The probe function is called when a device is installed that the USB core thinks this driver should handle; the probe function should perform checks on the information passed to it about the device and decide whether the driver is really appropriate for that device. In the probe function callback, the USB driver should initialize any local structures that it might use to manage the USB device. It should also save any information that it needs about the device to the local structure, as it is usually easier to do so at this time. As an example, USB drivers usually want to detect what the endpoint address and buffer sizes are for the device, as they are needed in order to communicate with the device.  The probe function has following prototype, static int test_probe(struct usb_interface *interface, const struct usb_device_id *id) Typical flow of detecting the endpoints' information, driver: loops over every endpoint that is present in provided interface 

How USB device is detected, identified and ready to use for applications?

Image
What is USB device enumeration? USB device enumeration is the process of detecting, identifying and loading drivers for USB device.  USB host controller queries the device in order to decide what type of device it is in order to attempt to assign an appropriate driver for it.  Some of the basic commands issued by the host to the device are: Set Address – Instructs the device change it’s current address settings Get Device Descriptor – Overall information about the device (manufacture, firmware version …) Get Configuration Descriptor – How the endpoints will be used Get Interface Descriptor – Various different interface that the device may use Get String Descriptor – Unicode strings for Manufacture and Product This process is a crucial for every USB device and without it the device would never be able to be used by the OS. We will go step by step to understand the whole process. Detection of device As you may know that USB interface has 4 wires.

What is GDB?

Image
What is GDB? GDB stands for G NU project D e B ugger which helps you to debug your binary object file created in compilation process. GDB allows to you see what is happening in your program which really helps much when program crashes, especially when segmentation fault occurs,😅😅 GDB can do four main things: Start your program, specifying anything that might affect its behavior Make your program stop on specified conditions through breakpoints Examine what has happened, when your program has stopped through checking the values of variables etc Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another Those programs might be executing on the same machine as GDB (native), on another machine (remote), or on a simulator. GDB can run on most popular UNIX and Microsoft Windows variants, as well as on Mac OS X. Languages supported by GDB Ada, Assembly, C, C++, D, Fortran, Go, Objective-C, OpenCL, Modula-2, Pasc