Video shows how to do a minimal driver for a USB device that utilizes the classes within the Linux kernal. It is simple to follow with minimal knowledge.
Hello YouTube welcome to another video tutorial on USB Linux programming in this video we're going to talk about the basic file arrangement to write a driver that communicates with the device all start off with the USB driver itself it's actually called it's a structure and its name is USB driver and this is the basic definition of the USB driver it's going to have a data type called name and that's going to be responsible for naming this driver as a whole next we're going to talk about the ID table the ID table is used to match this driver with any device that is attached to the USB bus this is how the colonel knows how to this is how the colonel knows which driver to call to handle a particular device when it's plugged into the computer and this is of type USB device ID so let's go ahead and declare this variable right here so what we so what we have here is basically a way for the kernel to match any currently attached device against all the available drivers to find the right driver to call to use that driver to manage that newly installed and attached device so that may be rather confusing so if we go over to the console we can tap again LS USB this does is list all the available drivers currently attached to your system and like I had mentioned earlier in the video I'm going to be writing a device driver for a USB pendrive so currently I don't have the pendrive attached to my computer at this time I'm going to insert the USB pendrive to my computer and then run the command again and I find that the pendrive is installed at this point so I need to be able to whenever my pendrive is attached to computer I need to have the kernel call upon my particular driver to manage this device so what I'm gonna need is this USB device ID right here to control this drive some copy these numbers and use a macro and it's definition is as follows here this is going to represent the vendor ID and this represents the product ID also if you want to get more specific information about a particular device you can use LS USB – V and this will spit out a whole slew of information from endpoint addresses to tell you exactly which one which of these two numbers is a vendor ID and which one is a product ID the vendor ID is the zero 7 8 1 and the product ID is 5400 6 and as what we enter over at USB device macro and this whole statement is initializing an array of pendrives you can have more than one and you can say USB device and then the number vendor and their products if you want your particular driver to support more than one device by this time we're only supporting one which is the pen drive that I have just now is inserted to my computer okay so continuing on now we talked about the ID table let's go ahead and talk about the probe function and specifically we're going to implement its definition up here the proof function of your driver is called whenever a device is plugged into your computer however your probe function will not be called if another driver currently owns that particular device so that means if another driver is currently installed and an is indicating to the kernel that it is responsible for managing the particular device that's plugged into computer your proof function will not get cold and as a result if you want your device if you want your device driver to manage a device instead you'll have to unload that other driver and install yours in its place but let's go ahead and define the pro function ok so this is the pro function it takes a structure of type USB interface and an interface from the previous video is meant to represent a logical and device such as a speaker USB camera and a whole number of other things and the USB ID is the same as the structure defined here so what's going on is that the device is plugged into the computer and the computer or the kernel calls your particular Pro function and wants to see if this particular driver is willing to handle this device with the ID supplied here at which points the pro function initializes local information structures memory and then return zero indicating that it's willing to manage it but like I just said the probe function will not get called if another driver already is taking claim to the device next we have the disconnect function the disconnect function functions to clean up memory and is called upon whenever the device is removed from the system and its definition is going to be implemented here so so the pen disconnect function is called whenever the pen is removed and we don't need to return any values and that's the definition of the USB driver now that we have our driver properly filled out we just now have to register this pendrive to the USB cord it's much like registering a character device however the difference is were registering with the USB cord for initialization and all we're going to do is display some information to ourselves for debugging purposes and as well as to register this pendrive to the USB core and then over in the exit function we're going to D and we're going to deregister our pendrive and again we just display some simple information to occur to the log file and we D register the USB driver that we previously registered over here and then we return and that's going to be its and then we have our make file so for the make file is going to present as this simply you have the module that you want to create and then the directory to the source file of the kernel in this case I'm only going to compile this module against my actively running kernel at this time the path to the directory and then we simply just create the module and here we clean out the module directory okay let's jump over to command-line now we're going to stay clear then sudo make okay so it seems like everything compiles successfully and as indicated here this is the module that we want to insert I'm you say pseudo insert mod stick driver Vecchio everything is now inserted so we do D message and as you can see here my the driver is inserted into the kernel however the problem now is that our probe function is not getting cold and just to ensure I'm going to unplug my USB pendrive so the pendrive is not disconnected I plug it back in but yeah my pro function is still not getting cold so what I need to do now is say sudo remove mod USB storage USB storage is a driver it's a module driver that manages USB pendrive and storage devices so at this time I'm going to go ahead and remove it so that my driver will be able to handle the pendrive that I stick into my computer so I'm gonna go ahead and remove it it says Edison use and so what that means is I basically just need to remove my pendrive so I now remove my pendrive and I'm a type of command again on USB at this point I'm going to go ahead and remove my module and reinsert it again to do remove mod and then we insert the module so this is the statement when the module is inserted I'm gonna plug my pendrive in now and then I'm going to do D message and as you can see this was when the module was inserted and then I inserted the USB stick and at this point it's now my module my driver is now in charge of managing the pendrive attached to the computer ok well that's going to do it for this tutorial join me next time and will actually write information to the USB pendrive as well as we get information back from pendrive as always please remember to subscribe and rate this video thank you
0 Comments