Transcript for:
Lecture on Linux Device Numbers and Files

welcome back in this video i will show you how to use linux device numbers and linux device files but before we start i want to show you i will push all my source code i'm producing here up to a github page so you can get it downloaded and do whatever you want with it but now let's go back to our raspberry pi am still connected over ssh to my pi and i think you all know the directory slash dev inside here you can see all the different devices on your system but when you have a device here and you have a linux kernel module or device driver on the other side how can you connect them together and the answer is over a device number so if you um just make others all and let's see only want to displace the t2is here you can see um for example here def t2i this theft device assigned to the major device number five and the miner device number zero you can see here we have a lot of devices which are assigned to the number four but they are they have different minor device numbers here and in a linux kernel module you can allocate or register such such a device number and if you connect them together if you open up a device file you will call the opening method or the opening function defined in your linux kernel module and so you have to link so if you write to a device file you are calling the right function within the linux kernel module which has registered the device number assigned to a device file okay so let's try to implement this so let me just copy um the folder with from the last time dev number and i will go into this folder i will move my module to death number c and i have to change it in the make file as well okay and now let's yeah let's change it here let's add a device number here okay to access the file system we need to include linux dot fs for file system and i will change the description as well um registers uh ui's number and implement some callback functions okay so now we have to add some callback functions for opening and closing the file okay so this function is called when we will open our device file and it has to be from the type static end i will call it driver open but you can use any name you want to and it must have the following arguments okay a variable from the type struct inode and a variable from the type struct file instance okay i won't use on these input parameters here but you can use them for example inside them you can find information if you want to open the file or read only or write only or read and write the ball and stuff like that but i won't focus on a bunch of videos if you like to have more information about it just look look on them up and you will find information what you can do with them i will just print your um open walls and i will return 0 to indicate everything fine if an error occurs you can return a negative value and now let's just let me copy the function here and i will change it to closed it needs exactly the same input parameters close was called okay and now we have to define a struct which contains our operations our file operations okay and the owner is so define this module um we want to set the open callback to driver open and we want to set the close callback which is called release here but never mind close okay and now all we have to do is register our device number okay and i will declare a integer variable up here and now i will say i will call the function register register okay and i have to pick uh i have to pick a device number because let me just close this file here and you can find all the device numbers and to what driver they assign in the file proc devices so here you can see it separated into block devices and character devices and let's look for a free number 60 iphone maybe 93 let's pick 90. okay let's pick 90. find my major so our major device number should be 90 and my nature and we have to give the register chart for the following arguments um some major device number we want to use then we have to give it a name number driver for example or death number let's just let's just write that number or my div number okay and so and we have to pass the file operations okay so now we have to check the return value if it was zero then the major device number was three and we could register it with the matrix device number um 90 and the minor device number nine um zero so number which is third death my true zero so if the return value is greater than zero there is already a device number this major number and in this case okay our if we shifted the whole thing by 20 we should get the major device number and if we we um only take the downward 20 bytes or is a minor device number so if you make this we should get the correct device number let's try it i think it should work and else if so if you get a negative value back something went wrong wise number and i will return minus 0 to indicate an error occurred okay um that's it the only thing we have to do is and if we unload our module we want to freeze the device number here so just let me it's a function on register on register my major picture and i have to pass the string number okay after that um it should be unregistered so now let's try to compile it hopefully i haven't made a mistake here let's see okay i have a typo here of course it has to be a return value okay let's try it again okay and now if we um look at our proc devices and grip for my dev number do i say sorry just straight oh i haven't loaded it sorry okay so now i will load it and now if i look for it okay we can see we have assigned the major device numbers number 90 to our kernel module okay so now let's create a device file in the bash for doing so we can use the command make note and here let's say i want to name it my device so this should be the name of our device file we want it to be a character device file as we saw in proc devices character device files and block device files and character device files are i will only use character device files in my tutorial but because because there are device files you can read to write from like a normal device and now we have to assign the device number let's make ninety and zero okay now if we look at dev my device we can see here we have a new device my device and it's assigned to the major device number 90 the miner device number zero okay so now let's create a simple um c program to open and close the file so we can see a little kernel lock here but before let's check the car lock okay here we have hello kernel and we have registered on our device with the number 90 and zero okay so we'll create a file called test.c i have to include some standard headers as this is a normal user space program i can use all sort of includes i'm used to okay and all we are doing here is and we will call so open function [Music] and we want to open it read only here but this should be uh no don't care here as we don't evaluate it and after and we check if the reading was successful there's opening sorry here and after that we'll just close it i think it should be yeah it should be that easy so now let's to let's try to compile it okay undeclared okay yeah sorry this is to be deaf okay and before we run it i have to change the permissions for our device file and now we execute it opening was successful and if we check the lock again okay we see here open was called closed was called and let's take a last look at the cat proc devices okay we have already done this okay so that's how to um create call callbacks and connect a device file with a device number in a linux kernel module and the next this time we created our device file by um using the mk node command but in the next video i will show you how to how to assign or how to create the device file within your within your linux kernel module thanks for watching and i hope i will see you in the next video