Home

documentation

image

Contents

1. su Usually in GNU Linux we log in as simple user for security purposes In order to change our login to super user root we must type su without log off and log on again and the system will prompt as for the root s password su user Change to user login usr sbin adduser In order to add a new user to our system usr bin passwd In order to change modify passwords pwd This will return the current path that we are working on ntsysv Adjustments of what is going to start at boot time RedHat ps adl This will display the processes currently running detailed export PS1 In order to change the prompt e g export PS1 Yes Master who Lists all users currently logged in to the this machine whoami Display information for the current user login id Display id number and privileges of the current user login find name Searches the file system for the requested filename Wild filename characters such as and can be used whereis command Displays in which directories included in the PATH the command is situated echo PATH Displays the path directories usr bin pine A program that will allow as to access our e mails usr bin elm A program that will allow as to access our e mails sbin ifconfig It s the same command as ipconfig for Windows ifconfig ethO up Enable the first Ethernet card The ifconfig ethO down will disable it sbin netconf
2. ELE SE ck kk kk kk Xk kk kk k kk kk k kk kk k kk kk kk k kk kk R W R c 2 Sk ek cue ok wen ki e static int open card struct inode inode struct file file printk lt 1 gt OPEN in if MOD IN USE 0 return EBUSY if the card is beeing accesed already return Device or Resource Busy increment usage count MOD INC USE COUNT printk lt 1 gt OPEN2 Mn return 0 static int close card struct inode inode struct file file decrement usage count MOD DEC USE COUNT printk lt 1 gt CLOSE return 0 static int write card struct file filp const char ptr size t count loff t f pos int address base port 100 int buff ptr outb buff address write to device return 0 static int read card struct file filp char ptr size t count loff t f pos unsigned char address base port 4 int n count while n IATE kk x rz printk lt 1 gt READ ptrt inb address printk lt 1 gt READEN d from device ptr get data from device return int count 101 Progress Reports Linux Device Drivers Progress Report of Level 3 Project 1 8 November 2002 Review Tasks Set Last Week e This is the first week Tasks Set This Week e Preparation for the project presentation Forward look to tasks for next stage e Familiarize w
3. printf Enter a byte to write to device scanf s amp byte printf You entered c n byte byte 0x3 write fd amp byte 1 return 0 133 pL Linux Device Drivers Progress Report of Level 3 Project 7 14 March 2003 Review Tasks Set Last Week e Writing source code for the module e Started writing the second sub report Tasks Set This Week e Working on the source code that has been submitted the previous week to the supervisor in order to implement the write file operation e The deliverables of the second stage of the project will be discussed with the supervisor e The student is trying to combine all the research and the work that has been done up to date in order to write the second sub report that is to be submitted on week 22 Forward look to tasks for next stage e Trying to make the write file operation to work correctly e Continue on writing the second sub report of the project 134 Linux Device Drivers Progress Report of Level 3 Project 14 21 March 2003 Review Tasks Set Last Week e Testing the source of the module e Continue writing the second sub report Tasks Set This Week e Working on the source code that has been submitted the previous week to the supervisor in order to test and debug the program e The main structure of the module has finished and some testing has been done also e The writing of the second sub report is in progress e
4. address return retval static int read_card struct file filp char ptr size_t count loff t f pos int address base port x x X x o o int n count printk lt 1 gt READ ptr inb p address get data from device 125 while n ptrt inb p address get data from device printk lt 1 gt READ2 return int count 126 Linux Device Drivers Progress Report of Level 3 Project 28 7 March 2003 Review Tasks Set Last Week e Writing source code for the module Tasks Set This Week e Working on the source code that has been submitted the previous week to the supervisor in order to implement the read and write file operations e The stage of the code will be submitted to the supervisor on the next meeting e Finally the student achieved to make the read function to work e The student made progress on the source code that is trying to write about the user space program e The write function sends a number successfully to the card but we cannot see that value using the read function Probably a wrong connection has been made to the cable of the card or we are using the wrong base address Forward look to tasks for next stage e Trying to find out why we can t read the value that we are sending to the ISA I O card using the write function Using the dmesg command in order to see the outputs of the kernel we can see that the module had successfully send a val
5. static int close card struct inode inode struct file file decrement usage count MOD DEC USE COUNT printk lt 1 gt CLOSE return 0 static int write card struct file filp const char ptr size t count loff t f pos int address base port int buff ptr outb buff address write to device return 0 static int read card struct file filp char ptr size t count loff t f pos unsigned char address base port 4 of Be SRR Ke e int n count while n printk lt 1 gt READ ptrt inb p address get data from device printk lt 1 gt READEN d from device ptr printk lt 1 gt READ return int count 143 The following script files were used to compile the module and the user space program compile the user space program sercom alt sh bin bash gcc Wall user alt c o user alt exe compile the module compile alt sh bin bash gcc D KERNEL 02 Wall I usr src linux include alt c o labcard alt o 144 c labcard Linux Device Drivers Progress Report of Level 3 Project 21 28 March 2003 Review Tasks Set Last Week e Testing the source code of the module e Finishing the writing of the second sub report Tasks Set This Week e All functions of the module work The write function works perfect and it has been tested also using Leds The read functio
6. S 2x portC inportb base 42 gotoxy 63 10 printf 2x portC if portC j printf 7 beep gotoxy 30 13 textattr 0x09 cprintf PortCl readback error getch exit 1 end of for return 0 end of main The source code of the program that is trying to write to the device from MS DOS include lt stdio h gt include lt stdlib h gt include lt conio h gt include dos h int main int argc char argv int port byte char stop stopl if argc gt 3 printf nThis program supports only two options n Type testp h for help else if argv 1 h argc 1 printf This program is used to write a byte to the Specified port n usage testp port to write hex byte to write else if argv 1 r 1 port strtol argv 2 stop 16 8 byte inport 0x110 printf VALUE d byte 137 7 else port strtol argv 1 stop 16 byte strtol argv 2 stopl 2 printf nport x byte d port byte printf Ok n outport port byte getch return 0 The source code of the program that is trying to read from the device MS DOS include lt stdio h gt include lt stdlib h gt include lt conio h gt main int byte bytel byte2 byte3 byte4 byte5 printf Reading n int base 0x200 byte inportb base bytel inportb base 1 byte2 inp
7. sse 89 Step through the source code of the Module 9 Writing a script for easier manipulation eese eene ene 92 Tater pts noeneen te e NO eee te edicere ere eee cepto ede evt E 93 BeferenoeS eege eege 94 Appendix etc esa tice t e dc bod een odis 95 Zser space Drogramisu 6 soot e ES 95 UNE ID SEE E E E nere mmt t edes 97 Module EE 97 Progress RepOLFts 5 oiueeceecee seevecseocessescesesesevocosacesessnaecesoustessseaneeesocuo Ur sesa eies looo 102 Objectives Settings Report ecce ee eere enne e teen nna EE ERRRR ete a n tete e nno EN 150 Agreed Marking Scheme Weightings e eee ee esee eee n teen a neto teea no 152 All R f rences iiio iie eese ste seu een eoe a esos eege 154 IERT e 154 Webhlo eraphy 5 e e Rt m hut ee M 155 EXTRAS PCL 731 48 Sit Digital I O Card User s Manual Introduction and Objectives Aim of the project is to construct a Linux Kernel module to drive an ISA input output card This concept for final year project was an original idea proposed by the student to the final year tutor Mr Phil Davies This project gives the opportunity of researching the structure of an operating system and particularly the relationship between user space and kernel space Furthermore it familiarizes the student with concepts of Linux Kernel programming The principles of the ISA bus interface and how a software program can access ha
8. init start void int check int address base port 4 const char testbuff 0x00 int addressl base port Pf e R KK char ptr check register device device name amp FileOps if check 0 printk 1 Can t register Device n return check check allocate ports base port port range 140 if check 0 printk lt 1 gt Can t load Module Mn else printk 1 Module loaded succesfully Mn set operating mode return check JAMY EXIT E CAE TIO TR Ra RS ES CEE SIS Ue e Pole STS rige Ue SI ies ES RS aen IEE nue static void exit clean void unregister device device name release region base port port range release ports Set Orerating Mode for ISA I O card PortA will be an INPUT PortB will be an OUTPUT static void set operating mode int register _addressl base port 3 int register address2 base port 7 outb p mod byte2 register addressl outb p mod bytel register address2 outb p 0x80 0x203 printk lt 1 gt SETOPMOD n Allocating Ports to the Registered Device x kx X x xk x static int allocate ports unsigned int base port unsigned int port range int err if err check region base port port range lt 0 return err device busy request region base port port range device name return 0 141 REGIS
9. Below we can see what is happening to the operating system as it passes from the one stage run level to the other init 0 init 0 and init 6 are the same thing init 1 single user not multi user no network init 2 always empty in order to develop a custom run level init 3 multi user without x plus network init 4 Depends of the distribution maybe either empty or represent multi user graphical environment with networking init 5 Depends of the distribution maybe either empty or represent multi user graphical environment with networking init 6 Reboots the machine 70 Recompile your kernel In order to use some hardware devices that we might have or to be able to see the ntfs file system of Windows from GNU Linux we must recompile our kernel Actually that recompile will add into the kernel or link to the kernel modules that are necessary in order to be able to perform the above operations These modules are either drivers for the hardware devices or pieces of kernel code in purpose to do a specific task In the following pages it will be explained as better as we can how to recompile the kernel First of all we have to make sure that linux s source code is installed into our system If we are running an rpm based distribution such as Red Hat and the kernel source package is linux source rpm we can type rpm v linux source rpm Another way used on every distribution to check
10. By pressing w which stands for Write to the card the program will prompt to enter a Decimal number to be written to the card The program loops continually in order to perform new operations read write but by pressing the q letter it exits real pictures of the ISA I O card connected on the computer can be found in Appendix 5 45 The following picture is showing how a value can be read from the device Unfortunately the ISA card sends always the default value 255 Ee plett bash 2 05a user exe Enter device driver name path or press d for default dev isa731 d Read or Write R M r Readen 255 Enter device driver name path or press d for default dev isa731 q Application stopped bash 2 05at rmmod labcard alt bash 2 05at lsmod Module Size Used bu Tainted P pcmcia core 40896 O bash 2 05at B The last step is to remove the linked module from the kernel using the rmmod command rmmod labcard alt Once again using the smod command we can see the loaded modules and verify that the module was successfully removed Conclusions In conclusion the project can be considered successful as the primary target of it That is building a device driver for the ISA I O card and a user space program have been met in a satisfactory level There are things that could be improved such the quality of the source code like being more optimized add the module in the kernel source
11. Except the fdisk partition table manipulator GNU Linux uses some other similar yet more user friendly programs such as the Disk Druid and cfdisk in order to help as partition our hard drive Red Hat and Slackware have a user friendly interface in order to help us install GNU Linux into our system The file systems supported by GNU Linux are plenty like reiserfs fat32 minix amp ISO9660 udf ext2 3 and MS DOS MP 8 mount Usually we use the console environment as the basic environment which is something like MS DOS command line in Windows but with furthermore capabilities and functionalities The administrator login in Linux is called root and we navigate through the directories using the following commands cd cd cd ls etc In order to find a program or command that is suitable for a specific operation we can search through the apropos MP 1 apropos database and then use it s manpage MP 1 man to understand the way it works In order to exit a manpage press the Q key on your keyboard 14 All devices in Linux have a special file to be represented This special files are into the dev directory and the following table have some of them In general everything from a device to a memory map in GNU Linux is represented by a file This means in order to access it we have to use the normal file operations we would use for a simple text document Speci
12. KERNEL define _ KERNEL endif ifndef MODULE define MODULE endif include linux kernel h include lt linux init h gt include linux module h include linux fs h include linux errno h less usr src linux include asm errno h include asm uaccess h include linux param h include lt linux ioport h gt include asm io h define _ NO VERSION EXPORT NO SYMBOLS declare device nam char device name isa731 char buff int MajorNum 42 int base port 0x200 int port range 8 int mod bytel 0x90 int mod byte2 0x80 dymanic name declaration module load time MODULE PARM device name s MODULE PARM MajorNum i MODULE PARM base port i MODULE PARM 3j mod bytel i MODULE PARM mod byte2 i Declaration of functions static struct file operations FileOps static int init start void static void exit clean void static int register device char device name struct file operations FileOps static int unregister device char device name static int open card struct inode inode struct file file static int close card struct inode inode struct file file static int write card struct file filp const char ptr size t count loff t f pos static int read card struct file filp char ptr size t count loff t f pos 97 st
13. and PAOO will be set to logical 1 The pins PA03 PA02 PAOI will be set to logical 0 Sending to the base address 0x200 a binary number after we set which port range will be used for output it will be written to the pins PA00 PAO7 At this point we must mention that when we write a value to the device that value stays to the device until we overwrite it with a new value or if we reset the card reboot the computer In order to read from the device we use the CN2 IDC connector and we must set the port range that we will use for that operation The base address 0x207 base address 7 will be used for read input from the device Sending the binary value 10010000 to the base address 0x207 will be set to read When we read from the device we are reading the value that exist on the pins PA10 PA17 Reading the 0x204 address base address 4 will return the input from the device Reference b7 b8 90 Step through the source code of the Module In this section of the report we will step through and explain some basic points of the module s source code The source code of the module is included in the appendix 2 In order for the user space to be able to access the ISA I O card which is a character device it must use the file operations The file operations used in this module are Open Release Write and Read At the top of the module s source code we can see the libraries used and defining some constant variable
14. piece of hardware The ete directory contains system configuration files and executables A very important and useful directory is that which called lost found Disk errors or incorrect system shutdown may cause files to become lost Lost files refer to disk locations that are marked as in use in the data structures on the disk but are not listed in any directory The home directory is a conventional location for user s home directories The mnt directory is a directory implemented in order for the users to mount some media like our CD ROM drive or a hard drive etc into their file system We will explain more about the mnt directory in the following page 67 The following directory is the usr which in the old days was used to store personal configuration files for each user Nowadays it s initial usage has been replaced by the home directory It s current use is to store configuration files for most of the applications The tmp directory is available to all users and programs as a scratch directory Last is the var directory which we can think as the program files directory of Windows In order to access a device like the CD Rom drive we must first mount the device That means we must allocate a directory that will be a link to the filesystem of the specific device Our devices are in the dev folder If for example our first hard disk drive is called hda the second hdb etc then in order to access the second
15. static struct file operations FileOps release close card open open card read read card write write card D I Me ENTE EE e E E e E ROR E No pk cs QUK e iens Oe static int init start void int check check register devic device name amp FileOps if check 0 printk 1 Can t register Device Mn return check check allocate ports base port port range if check 0 printk 1 Can t load Module Mn else printk 1 Module loaded succesfully Mn set operating mode return check VIMY EXIT function x e gr ee ek we D ode eR Wee AD et Res ok static void _ exit clean void unregister device device name release region base port port range release ports Set Orerating Mode for ISA I O card PortA0 will be an OUTPUT PortAl will be an INPUT 54 static void set operating mode int register addressl base port 3 int register address2 base port 7 outb p mod byte2 register addressl outb p mod bytel register address2 printk lt 1 gt SETOPMOD Mn Allocating Ports to the Registered Device X X X x x X x static int allocate ports unsigned int base port unsigned int port range int err if err check region base port port range lt 0 return err device busy request region base port port range device name return 0
16. th 13 10 January 2003 Review Tasks Set Last Week e Research on How Linux Works is in progress Tasks Set This Week e Start writing the sub report Forward look to tasks for next stage e Start the second part of the project 108 Linux Device Drivers Progress Report of Level 3 Project th 10 17 January 2003 Review Tasks Set Last Week e Working on Linux architecture Tasks Set This Week e Trying to understand important parts of the Kernel e Started writing some C code for the kernel e Understanding how read and write functions works along with the init and exit Forward look to tasks for next stage e Working on major numbers and adding devices into the dev directory 109 Linux Device Drivers Progress Report of Level 3 Project th 175 954 January 2003 Review Tasks Set Last Week e Review the work that has been done on kernel programming and trying to write some source code Tasks Set This Week e Achunk of source code had been produced in order to develop a simple module This module is using the file operations and is attempting to read and write in a buffer through the kernel e The module has been compiled in order to make it a module o file using the following command gcc Wall O2 c main c o greg o e A device file has been added into the dev directory using the following command mknod dev greg c 42 1 e Using the following command we are trying to link
17. In order to unload the module we would have to restart the computer The write function using the base address that was set for output will send to the device a value That value is the incoming value from the user space program In order for the module to read the value that is entered in the user space program we save that value to a variable The module will read this value using the pointer of the value and finally output it to the device The read function is trying to get the data from the device reading the specific base address that is set for read It then passes it to a pointer which is returned to the user space Reference b1 b3 b5 b6 Writing a script for easier manipulation During the research for the first sub report the author learned how to write simple script files for Linux That came to be a very helpful when the time of compiling the modules the student wrote a script file for easier manipulation bin bash gcc D KERNEL 02 Wall I usr src linux include c isa731 c o 1sa731 o bin bash gcc Wall user c o user o 92 If we copy the script files into the sbin directory we can run them without typing the path That means that will make the operation system to execute the script files as another system command e g ifconfig Reference b5 Interrupts Interrupts are system calls that are being send to the CPU to let the processor know when something has happened The hardw
18. Linux Works is in progress e Research on How to write a Kernel Driver is in progress Tasks Set This Week e Familiarization with Red Had Linux environment is in progress e Research required C source code for Linux e Writing some simple shell scripts to familiarize how the shells work e Trying to write some very simple C programs in console environment using the Jed editor Forward look to tasks for next stage e Red Hat Linux has a customized Kernel and patched In order to avoid future problem about Kernel versions I would like to use the official Kernel That s why I choused slackware 8 1 which uses the 2 4 18 official Kernel 105 Linux Device Drivers Progress Report of Level 3 Project 29 6 November 2002 Review Tasks Set Last Week e Research on How Linux Works is in progress e Research on How to write a Kernel Driver is in progress Tasks Set This Week e Familiarization with Red Had Linux environment is in progress e Research required C source code for Linux e Learning how to recompile the Linux Kernel in order to make it work with all hardware devices and be able to see the NTFS partition of windows e The I O card has arrived which has been ordered from the web site www advantech com but unfortunately they have send the wrong card Instead of a PCI I O card they ve send me an ISA I O card e Searching for some information in the Linux Journals Forward look to tasks for nex
19. MajorNum device name FileOps if check 0 printk 1 Can t register device n return check return 0 UNREGISTER DEVICE kx x kx Xx x k x X kk x static int unregister device char device name int check check unregister chrdev MajorNum device name if check 0 printk lt 1 gt Can t unregister device Maybe in use n return check return 0 kk KR KR KR KR KR KK KK KK KKK KK KK KK OK OK Re OR Se ke Be ge de FILE OPERATIONS OPEN READ WRITE RELEASE OK KR 0 KR KR KK KR KK KK KK KR KK KK KK X X KK X T ko k k kk kk kk static int open struct inode inode struct file file increment usage count MOD_INC_USE_COUNT return 0 static int close struct inode inode struct file file 112 decrement usage count MOD DEC USE COUNT return 0 static int write struct file filp const char ptr size t count loff t f pos lt Int snp F if MOD IN USE 1 printk 1 Can t write to the device n return EBUSY do if copy from user buff ptr count return EFAULT inp int buff printk lt 1 gt The input was d inp return 0 static int read struct file filp char ptr size t count loff t f pos if copy to user ptr buff count return EFAULT retu
20. QOR6C TET html TITLE html All the web links used were last accessed on Monday 12 May 2003 48 e Task Name Project Time Plan 1 Appendix E Linux Device Drivers i i 4st Stage Milestone I Install Fa ise Red Hat Linux Research Project Requirements Familiarise Linux environment Familiarise with module programming Familiarise with PCI bus amp Card O Start writing code Work on Journals and Magazines Combine all research write report i i 2nd Stage Milestone II Working on Linux architecture Module handling and structure Working Understanding the Kernel Working with Linux in a good level Write code Write report Working with UO card architecture E 3rd Stage Milestone III Combine all the previous research Combine write final code Testing and error checking Quality review Correcting mistakes Write Final Report Review Combine Sign off work Slag Supervisor meetings 82 days Mon 21 10 02 10 days 10 days 40 days 40 days 40 days 25 days 25 days 22 days 7 days 15 days 30 days 20 days 30 days 20 days 20 days 12 days 42 days 15 days 10 days 8 days 5 days 15 days 2days 2 days Mon 2110 02 Thu 31 10 02 Sun 10 11 02 Sun 10 11 02 Sun 10 11 02 Sun 10 11 02 Sun 10 11 02 Fri 20 1 2 02 Sat 11 01 03 Sat 11 01 03 Sun 26 01 03 Sun 26 01 03 Sun 26 01 03 Tue 25 02 03 Tue 25 02 03 Mon 17 03 03 Sat 29 03 03 Sat 29 03 03 Sat 29 03 03 Sun 1
21. REGISTER DEVICE x x X Xx Xx X SS kk X X Sr kr static int register device char device name struct file operations FileOps int check if device name NULL device name isa731 register device with kernel and return gt 0 for true lt 0 for false use the current Major Number for the specific device according the File Operations check register chrdev MajorNum device name FileOps if check 0 printk 1 Can t register device Mn return check return 0 UNREGISTER DEVICE x Xx X Xx X X SS k X X X X kk static int unregister device char device name int check check unregister chrdev MajorNum device name if check 0 d printk 1 Can t unregister device Maybe in use n return check return 0 55 ff NK KR OR 0 0k KR OR OR RR RR Ok Rk kK OK KR kK X X FILE OPERATIONS OPEN READ WRITE RELEASE ff NK KR OR KR KR RR RR kk RR 00k OR kK X kK Ok X static int open card struct inode inode struct file file printk lt 1 gt OPEN Mn if MOD IN USE 0 return EBUSY if the card is beeing accesed already return Device or Resource Busy increment usage count MOD INC USE COUNT printk 1 5OPEN2 Mn return 0 static int close card struct inode inode struct file file decrement usage count MOD DEC USE COUNT printk lt 1 gt CLOSE Mn ret
22. The student in order to test the card has bought some LEDs a 50 pin IDC cable and some extra electronic material Connecting the 50 pin cable to the card and having 8 leds connected at the end of the cable the student tried to test if we he is sending the commands to the correct base address of the card In order to do that more easily the student boot the PC with MS DOS 6 22 and having installed to a second PC the Borland C compiler wrote some programs for the ISA card e The base address was changed to 0x200 because it seemed to be a problem with the previous 0x300 e A demo program that was in the user s manual of the card was used also e The custom made programs of the student and the program from the user s manual had the same result The student was able to write to the device and see the leds on but he couldn t read from the device for some reason e Finally a last modification has been made to the module and to the user space program and the write operation to the device was successful and the output was able to be seen at the LEDs e There is a chance that there is some kind of problem with the card and that is the reason that we cannot read from the device This will be examined during the testing process in for the third sub report e The source code of the programs that were used to test the card the latest source code of the module and the user space program are included below Forward look to tasks for next stage e Co
23. and the apropos database were one of the most important tools in order to get started with Linux The development started by writing small pieces of code in order to see how the C programming language works with Linux The gcc program as long as a great amount of shared libraries from 1ib where used for that purpose In order to develop the source code for these small applications the jed editor in emacs mode was used After familiarizing with user space programming in GNU Linux kernel programming was introduced The student did a lot of research on the matter before attempting to actually write any pieces of code The next step of the research was to find out what the structure of a device driver is and how a device driver works After accomplishing that goal and being able to identify the basic parts of a device driver and understand their purpose the model of a very simple kernel module was designed The module was supposed to send a simple message from the kernel to the user space each time it was loaded and or unloaded After spending some time on that the module was finally doing what it should be when insmoded to the kernel After altering the code of the module and observing the results kernel messages kernel panics kernel oops for a short period of time the kernel module architecture was finally well understood 41 Consequently the file system s structure was studied in order to better understand the Way a us
24. code The source code of the module is included in the appendix 3 Page 53 37 In order for the user space to be able to access the ISA I O card which is a character device it must use the file operations The file operations used in this module are Open Release Write and Read At the top of the module s source code we can see the libraries used and defining some constant variables Defining the module as NO VERSION means that the module has no problem to cooperate with any kernel version Then we can see the functions that were used and the declaration of the init and exit functions The init function is the first function that will run when the module is loaded into the kernel correspondently the exit function is the function that will run when we unload the module from the kernel These functions init exit will register and un register the device using the corresponding functions register device and unregister device The set operation mode function will write a specific byte to specific ports of the card in order to set the input and output ports of the card Those ports are called the registers Finally the most important part of the module is the actions that are being performed by the file operations In the first case we have the open card file operation which checks if the device is in use in order to print a message to the kernel if it is and then increases the usa
25. dev directory and the following table have some of them In general everything from a device to a memory map in GNU Linux is represented by a file This means in order to access it we have to use the normal file operations we would use for a simple text document Special File Form Example Device Use dev fdn dev fdO Floppy Disk dev rmtn dev rmt0 Generic tape devices dev rstn dev rstO SCSI tape drive dev cdn dev cdO CD ROM devices dev ttyn dev tty01 Serial Line hardware terminal modem dev pts n dev pts 0 Pseudo terminal used for network sessions dev console dev console Console Device dev mem dev mem Map of physical memory dev kmem dev kmem Map of Kernel virtual memory dev mouse dev mouse Mouse Interface dev ramn dev ram00 Ram Disk dev sd a g n dev sda0 SCSI Disk dev swap dev swap Swap device dev null dev null Null device output written to it is discarded Introduction to UNIX security Dr Andrew Blyth Lecture Notes 2002 MSc Information Systems and Network Security University of Glamorgan A simple PS 2 mouse will work perfectly in GNU Linux but in case we have a wheel PS 2 mouse the following hint might be very useful for the users that has get used to working with the wheel in order to scroll through the pages of a document Suppose we have an IntelliMouse mice In order to make the mouse wheel to work we should open the X1186
26. device will use Jumpers JP1 and JP2 will identify the type of the interrupt triggering There are two types of interrupt triggering the rising edge and the falling edge In the rising edge the interrupt of the device is set to send the logical 0 and when an interrupt occurs it rises to the logical 1 In the falling edge when an interrupt occurs the signal falls from the logical 1 to the logical 0 Using the jumpers JP3 JP4 we can enable disable or use the S W setting of the interrupts The S W option is there to allow us to control the interrupts by other external interrupts Reference a1 a5 a11 P http choices cs uiuc edu f kon RoundRobin nodel html 40 The overall progress summary The student had no previous experience in programming with Linux Actually the first time that he used Linux was at the second year of his studies in one of his modules about Operating Systems The main idea of the project seemed exciting and challenging The only knowledge the student had around the subject when the project started was the C programming language used in the Windows environment The student did his first installation on Linux partially completed during the summer vacation before he started the third year of his degree At the beginning of the project the student had to install Linux successfully this time and research how it works learn the basic commands and understand the structure of the operating system The man files
27. following lines in the lilo conf file prompt delay 50 As a final point we have to type the command lilo in the command line Reference a2 a4 25 Kernel programming As it 1s already mentioned in the introduction most of the kernel source code is written in C programming language The best approach by the author s point of view to develop a Linux kernel module is by using C When programming for the kernel the standards of ANSI C still exist However there are some differences between kernel space and user space programming There are some libraries used only to be linked with kernel modules and consequently there are some functions that will only be recognized by the kernel First of all we have to go over the structure of standard kernel module Each module should have one init function and one exit function Each time the module is loaded the kernel executes the init function which usually checks for the availability of some resources and or registers some of them When the module is unloaded the exit cleanup function is executed in order to un register everything that was previously registered by the module and leave the kernel as it was before If the init function fails returns a minus integer number the module is considered not to be loaded On the other hand a cleanup function should never fail that s why the programmer should make sure that every procedure incl
28. if we have the kernel source installed would be to change into the kernel source directory using cd usr src linux and check if the source code is there As soon as we verify that we have the source code we can do a make clean 71 to remove any temporary and unwanted files from that directory Then we can run one of the three config editors by typing one of the above command lines make config OR make menuconfig OR make xconfig only for X The config file is a configuration file which we can see by typing less config lining in the usr src linux directory and it stores every information the make program needs to know about which kernel modules to compile with the kernel and which to compile as loadable modules After editing this file we can save the new kernel configuration and check if there any modules included in the kernel that are depended from some others which were not included with make dep If the previous command is successful we can move into compiling a compressed image of our new kernel by typing make bzImage and then compiling and installing the modules with 16 files starting with a dot are hidden and can be seen with the Is 1 command 72 make modules amp amp make modules install Finally if everything has gone right during the compiling and installing process we can add the new kernel image bzImage situated into usr src linuc arc 1386 boot into
29. it dynamically that is let the kernel decide about the e g loopback interface Z e g eth0 32 major number kernel 2 4 x or manually by specifying the number in the source code of the module or at load time In the current design of the module the second approach was used This is because in order to make a node for the card in the file system we must know the major number of the device assign during the load of the module In case we had used the third option let the kernel decide the major number we should look into the proc devices file in order to find the major number Instead the manual allocation of the major number was used and the only thing we should be aware of is that this number is not in the usr src linux Documentation devices txt and is not already registered to the kernel The chosen number to be used as major number for the ISA card is 42 Reference a3 a5 Device name A unique name should be invented in order to associate it with the device This would be the modules name as appeared at the output of the command 1smod and the node s name appeared under dev The author of this project chooses the name isa731 asa device name Reference a3 a5 Usage Count Usage count is a variable that we change each time we using the device This is for the reason that we don t want no other module to access the device when we are using it Using the functions MOD INC USE COUNT and MOD DEC USE COUNT we can i
30. kernel configuration is the kernel recompile that we discussed in the first sub repost of this project Reference b3 b4 User Space Program In this section of the report we would like step through and explain some basic points of the user s space source code 34 The source code of the user space is included in the appendix 2 Page 50 The user space program in order to send or receive data from the device must use the module as the middle man The module in order to access the device uses the special function to write to ports From the user space program we must use normal file operations on the devices representative file In order to use the file operations through the module that we developed we must open the file This will open the device in order to be used the open card function of the module will be executed or if the device does not exist it will print a message Then we have a loop in order for the program to prompt us continuously if we would like to read from or write to the device Pressing the Ctrl C the program will exit In each case read or write we call a function These functions are read and write Inthewrite function using the file descriptor acquired from the open function we write a value to the file At that time the write card function of the module is executed The read function reads from the file the input value from the device and causes the read card function of the module to execute W
31. labcard alt o bin bash gcc Wall user c o user exe If we copy the script files into the sbin directory we can run them without typing the path That means that will make the operation system to execute the script files as another system command e g ifconfig Reference a5 Interrupts Interrupts are system calls that are being send to the CPU to let the processor know when something has happened The hardware a device sends a signal to the processor in order to requests for the processor s attention Generating and manipulating interrupts is a very complex procedure For that reason interrupts 39 weren t used for the development of the module but this small paragraph was included in order to explain in general how interrupts work When a hardware interrupt is generated by a device the CPU sends an IRQ signal to the operating system s kernel The kernel then according to it s configuration can ignore the interrupt and continue it s normal schedule of operations e g Round Robin or check it s IRQ table to see if any of the kernel modules has been registered with that interrupt value If that is the case it will then probe the module in order to inform it about the situation It s up to the module then to take further action if needed The ISA I O card provides some jumpers that will allow us to set the interrupts The first two jumpers JP5 JP6 will help us to register what kind of IRQ levels the
32. our boot loader such as lilo or grub and then reboot into it Reference a4 a5 The LILO boot loader One of the most widespread boot loaders for Linux partitions is LILO Lilo reads the configuration file etc lilo conf and then stores the boot parameters in the first 512 bytes of a partition or in a hard drive s MBR Master Boot Record When a PC boots it reads off those 512 bytes and then loads the kernel image described there In order to change the configuration file we can either run liloconfig or manually edit it by typing the following command jed etc lilo conf In order to add another kernel image option for example bzImage we have to declare some variables the author places them at the end of file First of all the image s path This can be done by typing into the lilo conf image bzImage Then the root partition should be stated with root dev hda3 ES Additionally we must assign a name to the new kernel image in order to recognize it at boot time in the lilo prompt label Slackware new Finally because Linux at boot time runs some diagnostic tools on the root partition in order to prevent any damage to the file system we initially mount it as read only So we add the following command read only If we want lilo to be installed on the MBR the following line should exist on the top of the file H boot dev hda Moreover in order to have a time period equal to 50 ms in order for th
33. partition of our 1 hard drive we must type hda2 Of course the floppy disk drive is fd0 and if we have a second one it is called fd1 the devices are explained in the table at page 6 The following command will connect the device fd0 1st floppy disk drive to the floppy directory inside the mnt directory mount dev fd0 mnt floppy To disconnect the device we must type umount dev fd0 or umount mnt floppy We could use mnt directory to mount a device If we do that the mnt directory will be our device now until we unmount it When we unmount the mnt directory we will be able again to see the contents of the original directory We can think of mount operation as a stack We can mount several devices one above the other on a single directory The last device we mounted e g CD ROM is the device we are going to use from that directory Unmounting the last device automatically the directory is mounted to the previous device we had mounted e g Floppy Drive We will continue unmounting devices until we get the original directory mnt This is not useful very often and thats why we use different directories for different devices MP 8 mount Reference a2 a3 a4 a5 a8 68 Shell Scripting The shell scripting in Linux is similar to vbscript of windows and is very easy to use in order to implement rapid scripts At the Appendix B there is a simple example th
34. size t count loff t f pos static int read card struct file filp char ptr size t count loff t f pos static int allocate ports unsigned int base port unsigned int port range static void set operating mode module init start module exit clean File Operations structure X k X X k static struct file operations FileOps release close card open open card read read card write write card Zut INIT function I vetu desc ve de RAD See SORTS sp sexe Senses dove static int init start void 128 My I int check int address base port 4 const char testbuff 0x00 int addressl base port fof RR EB RES char ptr i check register device device name amp FileOps if check 0 printk 1 Can t register Device Mn return check check allocate ports base port port range if check 0 printk lt 1 gt Can t load Module Mn else printk 1 Module loaded succesfully n set operating mode return check EXIT function x x x x x kx x x kk x Xx k k static void exit clean void unregister device device name release region base port port range release ports Set Orerating Mode for ISA I O card PortA will be an INPUT PortB will be an OUTPUT static void set operating mode int register _addressl b
35. space environment where all the applications are executed by the user In order for the user space program to communicate with the hardware from the application layer the Real Time Embedded Computer Systems 10 interception of a kernel is vital The kernel looks for the particular driver for the device we want to access and according to the instructions on that driver will allow the program to communicate with the specific piece of hardware e g Hard Drive CD Rom ISA card This driver is a module for the kernel usually written in C programming language Actually the most of the kernel is made up from too many kernel modules linked together The Peripheral Component Interconnect PCI bus allows us to connect a hardware card into our computer An example of a PCI card is the well known soundcard we have in our computers Nowadays the PCI standard as a result of its cost effectiveness backward compatibility scalability and forward thinking design has overcome ISA Industry Standard Architecture standard which is the ancestor of the PCI bus Even thought the ISA bus architecture is still used by most manufacturers this is due to the fact that the transaction from the one architecture to the other is very expensive for the most of the industry and the managers think that is this is not a value for money solution The object of this project is to familiarize the student with the concepts of Linux Kernel programming the GN
36. the following points it is possible to be seen as they are assigned or not in each type of bus architecture gt Direct Memory Access 24 The first bus architecture that was implemented in PC architecture was ISA At first it was an 8 bit medium but it quickly evolved in 32 bits This indicates that every time in each clock cycle the bus was able to transfer one and two bytes respectively Even thought technology enables to increase the bus frequency for ISA backward compatibly issues force it to be stable at 8 33 MHz A 16 bit ISA card is able to support a maximum of 16 megabytes of transference per second for all the devices simultaneously a record which has been overcome long time ago The PCI architecture is the newest and most widespread now days bus architecture Norm PCI indicates that a 4 BUS PCI is solely of 4 slots but allows us to connect another 4 slots in groups by means of PCI bridging Also PCI is a 32 bit bus with a frequency of 33 MHz This means that the maximum transfer rate can be 133 megabytes per second This is more than double the maximum speed of an ISA bus Additionally IRQ sharing can be supported by this architecture something that PnP and Windows take a great advantage of The author initially had chosen to use a PCI card for the purposes of this project Unfortunately a posting error forced the use of an ISA Input Output card whose specifications will follow The exact model
37. the same thing init 1 single user not multi user no network init 2 always empty in order to develop a custom run level init 3 multi user without x plus network init 4 Depends of the distribution maybe either empty or represent multi user graphical environment with networking init 5 Depends of the distribution maybe either empty or represent multi user graphical environment with networking 21 init 6 Reboots the machine Recompile your kernel In order to use some hardware devices that we might have or to be able to see the ntfs file system of Windows from GNU Linux we must recompile our kernel Actually that recompile will add into the kernel or link to the kernel modules that are necessary in order to be able to perform the above operations These modules are either drivers for the hardware devices or pieces of kernel code in purpose to do a specific task In the following pages it will be explained as better as we can how to recompile the kernel First of all we have to make sure that linux s source code is installed into our system If we are running an rpm based distribution such as Red Hat and the kernel source package is linux source rpm we can type rpm v linux source rpm Another way used on every distribution to check if we have the kernel source installed would be to change into the kernel source directory using cd usr src linux and check if the source code
38. to have a real representation See figure below Connectors on the 50 pin cable 50 pin cable ISA I O card PCL731 LED light See Appendix 5 at Page 59 for a real picture 43 The led lights proved that the correct values were sending to the card but the read function wasn t working properly Several tests occurred in order for this mistake to be fixed and the advice of the supervisor was asked The author also contact the manufacturer company of the ISA card for any special tips they might have for that problem but no answer was received The User s manual of the ISA card had the source code of a very simple application written in C that was writing reading values from to the card in order to test it This program needed an MS DOS partition in order to be used The author using the Borland turbo C compiler and the source code from the user s manual created the executable file of that program Also an MS DOS 6 22 boot disk was created including the executable file in order to test the card Finally the output of that executable file was identical to the one from the Linux user space program The supervisor was informed for that result and after a couple of meetings with the student come up with the result that there should be a hardware problem with the ISA card and that this is the reason for receiving the same results from both applications Linux Kernel module MS DOS executable file Consequently it was agreed bot
39. x static int unregister device char device name int check check unregister chrdev MajorNum device name if check 0 printk 1 Can t unregister device Maybe in use n return check return 0 kk Ck KR KK KR KR KK RK RR kk KK KK kK OK X ae kk e ck Xe Xo oce FILE OPERATIONS OPEN READ WRITE RELEASE Jf OK 0 X 0 KR KR KK KR KK KK RK KR KK KK KK kK OK X SES zk 2 2 ok Ske ces CX 124 static int open_card struct inode inode struct file file printk 1 Module loaded succesfully OPEN Mn if MOD_IN_USE gt 0 return EBUSY if the card is beeing accesed already return Device or Resource Busy increment usage count MOD_INC_USE_COUNT printk 1 Module loaded succesfully OPEN2 Mn return 0 static int close card struct inode inode struct file file decrement usage count MOD DEC USE COUNT printk lt 1 gt CLOSE return 0 static int write card struct file filp const char ptr size t count loff t f pos int address base port 4 Ajo vo UPC ea ne ee See printk lt 1 gt WRITE outb p ptr address write to device printk 1 Output Data n printk lt 1 gt WRITE2 int retval count int address base port 4 while count outb p ptrt
40. 0 In order to set the CN1 IDC connector that will be used for writing to the device we send to the address 0x203 base address 3 the binary number 10000000 When we write a byte to the device we are setting the pins PA00 PAO7 to logical 1 or 0 depending to the byte we want to write For example if we write to the card the binary number 11110001 the pins PA07 PA06 PAO5 PA04 and PAQO will be set to logical 1 The pins PA03 PAO2 PAOI will be set to logical 0 Sending to the base address 0x200 a binary number after we set which port range will be used for output it will be written to the pins PA00 PAO7 At this point we must mention that when we write a value to the device that value stays to the device until we overwrite it with a new value or if we reset the card reboot the computer In order to read from the device we use the CN2 IDC connector and we must set the port range that we will use for that operation The base address 0x207 base address 7 will be used for read input from the device Sending the binary value 10010000 to the base address 0x207 will be set to read When we read from the device we are reading the value that exist on the pins PA10 PATI Reading the 0x204 address base address 4 will return the input from the device Reference a7 a8 Step through the source code of the Module In this section of the report we will step through and explain some basic points of the module s source
41. 3 04 03 Tue 08 04 03 Mon 21 04 03 Tue 06 05 03 Thu 08 05 03 Fri 09 05 03 Fri 10 01 03 Wed 30 10 02 Sat 09 11 02 Thu 19 12 02 Thu 19 12 02 Thu 19 12 02 Wed 04 12 02 Wed 04 12 02 Fri 10 01 03 Fri 28 03 03 Sat 25 01 03 Mon 24 02 03 Fri 14 02 03 Mon 24 02 03 Sun 16 03 03 Sun 16 03 03 Fri 28 03 03 Fri 09 05 03 Sat 12 04 03 Mon 07 04 03 Sun 20 04 03 Sat 12 04 03 Mon 05 05 03 Wed 07 05 03 Fri 09 05 03 49 Appendix 2 User Space program source code final version J EKK KK K K K K K Kok kk kk A kk Kok kk kk kk Kok kk kk ok A Ck kk ok K K ke ke Kok kk kk ok A ko kk ke ek ek koe user c User Space Application for the module labcard alt c which is a Device Driver for ISA I O card PCL731 begin 01 Nov 2002 copyright C 2002 by Grigorios Fragkos email djgreg linuxmail org JRA CKCk kk Ck Ck A Kok kk kk Ck kk ek Kok kk kk Sk ko A ko kk kk Sk ck kk kk Ck kk AA ke ek A A ek eek f S CK CK KKK KKK ck Sk Ck Ck kk k kk Ck Ck Ok Sk CK Kk Ck Ck Sk hh Ck Ck e ke ko k kk Ck ko kk ko ke kk ke ke kk ok kk ke K k ke ek k k k k This program is free software you can redistribute it and or modify it under the terms of the GNU General Public License as published by the Free Software Foundation either version 2 of the License or at your option any later version F F F KKEKKKKKKKKKKKKKKKKK KKK KK KK KKK KKK KK KK KKK KKK KK KKK KK KKKKKKKKKKKKKKKK KKK KKKK include lt stdio h gt inclu
42. 8 printf PortBO output value gt gotoxy 11 10 printf PortCO output value gt gotoxy 43 6 printf PortAl Readback gt gotoxy 43 8 printf portBl1 Readback gt gotoxy 43 10 printf portC1 Readback gt initialization outportb base 3 0x80 set 8255 portO0 all as output for 3 0 3 lt 0x100 3 outportb base j out j to port AO gotoxy 34 6 printf S 2x portA inportb base gotoxy 63 6 printf 2x portA if portA j printf 7 beep gotoxy 30 13 textattr 0x09 cprintf PortAl readback error 57 getch exit 1 quit to dos outportb base 1 j out j to port BO gotoxy 34 8 printf 2x j3 portB inportb base 1 gotoxy 63 8 printf 2x portB if portB t j printf 7 beep gotoxy 30 13 textattr 0x09 cprintf PortBl readback error getch exit 1 quit to dos outportb base 2 j out j to port CO gotoxy 34 10 printf S 2x portC inportb base 42 gotoxy 63 10 printf 2x portC if porte t 9 printf 7 beep gotoxy 30 13 textattr 0x09 cprintf PortCl readback error getch exit 1 quit to dos end of for end of main 58 Appendix 5 The ISA I O card PCL731 59 1 Sub Report About the report This is the first sub report of th
43. Categor Ranges Weighting Allocated Project Management 50 80 Originality amp Self Direction 30 60 Technical Complexity 20 80 Solutions Evaluation amp 40 80 Conclusions Final amp Sub Report Quality nus Prototype System Demo Or Project Deliverable Sponsor Mark Sub Total Marks Sub Total Percentage Research Milestone 1 Design amp Development Progress Milestone 2 Final Presentation TOTAL PERCENTAGE 152 Research Milestone One Design amp Development Milestone Two Presentation Viva Student s Conclusion amp Evaluation Milestone Three Overall Supervisor Comments 153 All References Bibliography 1 Daniel P Bovet Marco Cesati 2000 Understanding the Linux Kernel O Reilly 2 Bill Ball 1998 Teach Yourself Linux in 24 Hours Second Edition Sams Publishing 3 Alessandro Rubini amp Jonathan Corbet 2001 Linux Device Drivers Second Edition O Reilly 4 Matt Welsh Matthias Kalle Dalheimer Lar Kaufman 1999 Running Linux OReilly 5 Richard Stones amp Neil Matthew 2001 Beginning Linux Programming Wrox 6 Jesse Liberty David Hovarth 2000 Teach Yourself C for Linux in 21 Days Sams 7 Don Anderson Tom Shanley 1995 ISA System Architecture Addison Wesley 8 Dr Andrew Blyth Lecture Notes 2002 Introduction to UNIX security MSc Information Systems and Network Security University of Glamorgan 9 Linux Jou
44. Config file with the jed editor by typing the following command Remember that is case sensitive 64 jed etc X11 XF86Config We are looking for the line that into the file that has the following command Option Protocol PS 2 and we replace it by Option Protocol IMPS 2 After 2 lines we add the following command also Option ZAxisMapping 4 5 We can press Ctrl X C in order to exit and we press y to save the changes If we did that through the X Windows we must restart our X Server The editors that we can use in order to view or edit files in GNU Linux are plenty The most well known are jed emacs pico nano vi and vim which can be used to modify files and more or less which can be used only to view a file read only mode In the following table we will see some of the most important commands and files of GNU Linux along with a small description in order to have a generic idea of what we are going to talk about in the following pages Before we go along we must describe another helpful functionality of GNU Linux Typing the name of a folder file or command we can auto complete the rest of the word by clicking the tab button If we double click the tab button will return all the possible commands and pathnames that can be used from that user at the specific location starting with the letter s we ve typed File Command Description jed etc passwd Will open the passwords file int
45. Linux Device Drivers Level 3 Project Final Report Student Grigorios Fragkos E N 01066900 BSc Software Engineering Supervisors Dr Gaius Mulley Mr Keith Verheyden University of Glamorgan Wales 2002 2003 Statement of Originality SCHOOL OF COMPUTING DEGREE SCHEME IN COMPUTING LEVEL THREE PROJECT This is to certify that except where specific reference is made the work described within this project is the result of the investigation carried out by myself and that neither this project nor any part of it has been submitted in candidature for any other award other than this being presently studied Any material taken from published texts or computerised sources have been fully referenced and I fully realise the consequences of plagiarising any of these sources Student Name Printed Grigorios Fragkos Student Signature 25 ee Registered Scheme of Study BSc Hons Software Engineering Date of Signing 09 May 2003 sess Abstract The title of the project is Linux Device Drivers and is about how to design and develop a device driver from scratch for an ISA I O card in Linux It also includes a basic introduction on Linux Operating System and analyses the various concepts of the Linux kernel Slackware 8 1 was the Linux distribution used for this project running on the 2 4 18 official Linux kernel The module and the user space program were written in C programming langua
46. TER DEVICE x x x Xx x Xx kX x X k k X k k X static int register device char device name struct file operations FileOps int check if device name NULL device name greg register device with kernel and return gt 0 for true lt 0 for false use the current Major Number for the specific device according the File Operations check register chrdev MajorNum device name FileOps if check 0 printk 1 Can t register device Mn return check return 0 UNREGISTER DEVICE x kx X x X x k kk x static int unregister device char device name int check check unregister chrdev MajorNum device name if check 0 printk lt 1 gt Can t unregister device Maybe in use n return check return 0 kk KR KR kk KR Ak kk kk kk zk kk zk kk SL E E OK OK ox c We zk wx k ge e FILE OPERATIONS OPEN EAD RITE ELEASE ER R E E E E KD ouk SS E Ee Ter E E e E Se SS es ues SE e R W R ko k k kk kk kk static int open card struct inode inode struct file file printk lt 1 gt OPEN in if MOD IN USE 0 142 return EBUSY if the card is beeing accesed already return Device or Resource Busy increment usage count MOD INC USE COUNT printk 1 0PEN2 Win return 0
47. U Linux Operating System ISA bus interface and C programming in order to develop implement and test a kernel module which will provide the appropriate environment to a user space application to access the ISA I O card In the second part of the report we are going to cover the major aspects concerning a device module implementation We will start approaching the kernel structure and how it handles the modules in order to access devices Furthermore we will see how a module links to the kernel and how it performs the various operations Additionally we will explain the role of the user space program in order to access the device In order to make use of the I O card we will analyze how the ISA card was programmed using the specification of the user s manual The issues concerning the C code that was produced and the source code structure will be covered Reference a2 a4 11 Project Analysis Time Plan At the beginning of the project a time plan was produced in order to organize the time spend to each task for the project The time plan used as a guideline for each task and it helped meeting the deadlines successfully The figure found in appendix 1 shows the time plan that produced using a Gantt chart The total duration for the project was 201 days started on Monday 21 Oct 2002 and has ended on Friday 9 May 2003 Appendix 1 Page 49 The time plan of the actual work of the project as it happened during th
48. a module to the kernel insmod greg o e The module has been compiled successfully after many attempts but with a few warnings Unfortunately even when the module was linked to the kernel it didn t work as it should e Another problem was that the module could not be unloaded from the kernel so we had to reboot the system in order to try again e The module that was produce is attached to the minutes of this meeting Forward look to tasks for next stage e Working on the source code in order to understand the kernel in a better level e Further research on kernel programming Example of source code ifndef KERNEL define _ KERNE fendif El ifndef MODULE define MODULI dendif Lu 110 include linux kernel h include lt linux init h gt include linux module h include linux fs h include lt linux errno h gt include lt asm uaccess h gt include linux param h define _ NO VERSION EXPORT NO SYMBOLS declare device nam char device name greg char buff int MajorNum 42 dymanic name declaration module load time MODULE PARM device name s MODULE PARM MajorNum i Declaration of functions static struct file operations FileOps static int init start void static void exit clean void static int regist r device char device name struct file operations FileOps static int unregist
49. addressl outb p mod bytel register address2 printk lt 1 gt SETOPMOD Mn kk k k kk kk k k kk kk Allocating Ports to the Registered Devic xk k static int allocate ports unsigned int base port unsigned int port range int err if err check region base port port range lt 0 return err device busy request region base port port range device name return 0 REGISTER DEVICE x kk X x Xx X x X k k k k X static int register device char device name struct file operations FileOps int check if device_name NULL device name isa731 register device with kernel and return gt 0 for true lt 0 for false use the current Major Number for the specific device according the File Operations check register chrdev MajorNum device name FileOps if check 0 printk 1 Can t register device n 99 return check return 0 UNREGISTER DEVICE kx x kx X x X x k kk X static int unregister device char device name int check check unregister chrdev MajorNum device name if check 0 printk lt l gt Can t unregister device Maybe in use n return check return 0 Jf OK X KR 0 0k 0k RK RR RK KK KKK KR KKK KO x x k k kk kk k X FILE OPERATIONS OPEN EAD RITE
50. al File Form Example Device Use dev fdn dev fdO Floppy Disk dev rmtn dev rmt0 Generic tape devices dev rstn dev rstO SCSI tape drive dev cdn dev cdO CD ROM devices dev ttyn dev tty01 Serial Line hardware terminal modem dev pts n dev pts 0 Pseudo terminal used for network sessions dev console dev console Console Device dev mem dev mem Map of physical memory dev kmem dev kmem Map of Kernel virtual memory dev mouse dev mouse Mouse Interface dev ramn dev ram00 Ram Disk dev sd a g n dev sda0 SCSI Disk dev swap dev swap Swap device dev null dev null Null device output written to it is discarded Introduction to UNIX security Dr Andrew Blyth Lecture Notes 2002 MSc Information Systems and Network Security University of Glamorgan A simple PS 2 mouse will work perfectly in GNU Linux but in case we have a wheel PS 2 mouse the following hint might be very useful for the users that has get used to working with the wheel in order to scroll through the pages of a document Suppose we have an IntelliMouse mice In order to make the mouse wheel to work we should open the X1186Config file with the jed editor by typing the following command Remember that is case sensitive 15 jed etc X11 XF86Config We are looking for the line that into the file that has the following command Option Protocol pS 2 and we replace it by Option Protocol IMPS 2 Af
51. ame char ar int fd int rcode parameter rtr while TRUE printf Enter devic driver nam or press d for default dev isa731 scanf s dev if dev d device name dev isa731 else device_name dev file descriptor fd open device name O_RDWR if device doesn t exist print an error message if fd 1 95 printf Error cannot open device or device does not exist n maybe a wrong device name n return EBUSY rtr getc stdin parameter FALSE while parameter printf R or W scanf c amp ar if ar r ar R rcode read fd parameter TRUE else if ar w ar W rcode write fd parameter TRUE else printf Wrong parameter n parameter FALSE rtr close fd return rtr end of main int read int fd read a byte from the opened fil unsigned char byte read fd amp byte 1 printf Readen d n byte return 0 int write int fd write a byte that the user enters to the opened file unsigned char byte int panos printf Enter a byte to write to device scanf d amp panos printf You entered d n panos byte panos byte OxFF byte strtol argv 1 stop 160 write fd amp byte 1 return 0 96 Appendix 2 Module Source Code ifndef
52. are a device sends a signal to the processor in order to requests for the processor s attention Generating and manipulating interrupts is a very complex procedure For that reason interrupts weren t used for the development of the module but this small paragraph was included in order to explain in general how interrupts work When a hardware interrupt is generated by a device the CPU sends an IRQ signal to the operating system s kernel The kernel then according to its configuration can ignore the interrupt and continue its normal schedule of operations e g Round Robin or check its IRQ table to see if any of the kernel modules has been registered with that interrupt value If that is the case it will then probe the module in order to inform it about the situation It s up to the module then to take further action if needed The ISA I O card provides some jumpers that will allow us to set the interrupts The first two jumpers JP5 JP6 will help us to register what kind of IRQ levels the device will use Jumpers JP1 and JP2 will identify the type of the interrupt triggering There are two types of interrupt triggering the rising edge and the falling edge In the rising edge the interrupt of the device is set to send the logical 0 and when an interrupt occurs it rises to the logical 1 In the falling edge when an interrupt occurs the signal falls from the logical 1 to the logical 0 Using the jumpers JP3 JP4 we can enable 6 http c
53. ase port 3 int register address2 base port 7 outb p register addressl mod bytel outb p register address2 mod byte2 printk 1 Module loaded succesfully SETOPMOD Mn 129 Allocating Ports to the Registered Device x kx x x xk x static int allocate ports unsigned int base port unsigned int port range int err if err check region base port port range 0 return err device busy request region base port port range device name return 0 REGISTER DEVICE x x Xx X X X x X k k k k X static int register device char device name struct file operations FileOps int check if device name NULL device name greg register device with kernel and return gt 0 for true 0 for false use the current Major Number for the specific device according the File Operations check register chrdev MajorNum device name FileOps if check 0 printk 1 Can t register device Mn return check return 0 UNREGISTER DEVICE x kx Xx x k x k kk x static int unregister device char device name int check check unregister chrdev MajorNum device name if check 0 printk lt 1 gt Can t unregister device Maybe in use n return check return 0 130 Zb c ve e ce ue k
54. at shows some useful commands in shell scripting After writing our script we must change the privileges of the file in order to make it executable and be able to run it A visual representation of the privileges of a file is showed below rWxXrwxrwx r Stands for read w Stands for write x Stands for execute We can see the privileges of a file typing the command ls 1 As we can see there are three triplets of rwx The first triplet is the owner s privileges The second triplet is the privileges of the owner s group and the third triplet is the other groups triplets Assigning a number for each letter e g r gt 4 w gt 2 x gt 1 we can create a unique number for each privileges we want to give If we want read write and execute for the owner we add 4 2 and 1 which gives us 7 If we want read only access for the other groups triplets we use the number 4 In our case we came up with the number 764 rWxrwxrwx 42 1 421 421 Xr ee 7 6 4 We must type the following command to change privileges chmod 764 file in order to run the script we have just created we will type 69 myFile or we can move it into a directory that is included in the path and just type myFile Reference a5 Kernel Init runlevels From the moment that an operating system starts booting until it reaches the run level we wish it passes through the init faces This faces are the init run levels which are 6 from init 0 to init 6
55. atic int allocate ports unsigned int base port unsigned int port range static void set operating mode module init start module exit clean File Operations structure x X X X x x static struct file operations FileOps release close card open open card read read card write write card 44 My INIT function AoE od E E DEO OS OI de E ce sce Uer xe OK Ser ces cole static int init start void int check int address base port 4 const char testbuff 0x00 int addressl base port o Mo ME E E NES char ptr check register device device name amp FileOps if check 0 printk lt 1l gt Can t register Device n return check check allocate ports base port port range if check 0 printk 1 Can t load Module Mn else printk 1 Module loaded succesfully Mn set operating mode return check LIMY EXIT EUR GEO I Rak JC ks OR RAE XA RS SR EIST IR ok Joke ISS ok cae oe oe static void exit clean void unregister device device name 98 release region base port port range release ports Set Orerating Mode for ISA I O card PortAO will be an OUTPUT PortAl will be an INPUT static void set operating mode int register addressl int register address2 base_port 3 base_port 7 ER outb p mod byte2 register
56. ations e The stage of the code will be submitted to the supervisor on the next meeting e Even though the file operations read and write are in the implementation stage for about 3 weeks still we are not able to read from the ISA card successfully e The student started writing a user space application Forward look to tasks for next stage e Trying to implement the file operations write and read The following is the source code of the ISA I O card module that has implemented up to date ifndef _ KERNEL define _ KERNEL _ endif ifndef MODULE define MODULE endif include linux kernel h include lt linux init h gt include linux module h include linux fs h include linux errno h less usr src linux include asm errno h include lt asm uaccess h gt include linux param h include lt linux ioport h gt 121 include asm io h define _ NO VERSION EXPORT NO SYMBOLS declare device nam char device name greg char buff int MajorNum 42 int base port 0x300 int port range 8 int mod bytel 0x90 int mod byte2 0x80 dymanic name declaration module load time MODULE PARM device name s MODULE PARM MajorNum i MODULE PARM base port i MODULE PARM mod bytel i MODULE PARM mod byte2 i anaana Declaration of functions static struct file operations FileOps stat
57. cations that are marked as in use in the data structures on the disk but are not listed in any directory The home directory is a conventional 18 location for user s home directories The mnt directory is a directory implemented in order for the users to mount some media like our CD ROM drive or a hard drive etc into their file system We will explain more about the mnt directory in the following page The following directory is the usr which in the old days was used to store personal configuration files for each user Nowadays it s initial usage has been replaced by the home directory It s current use is to store configuration files for most of the applications The tmp directory is available to all users and programs as a scratch directory Last is the var directory which we can think as the program files directory of Windows In order to access a device like the CD Rom drive we must first mount the device That means we must allocate a directory that will be a link to the filesystem of the specific device Our devices are in the dev folder If for example our first hard disk drive is called hda the second hdb etc then in order to access the second partition of our 1 hard drive we must type hda2 Of course the floppy disk drive is fd0 and if we have a second one it is called e D fd1 the devices are explained in the table at page 15 The following command will connect
58. code of the card is PCL 731 which is a 48 bit Digital Input Output DIO card This means that it is equipped with 48 pins which can be used as inputs or outputs to and from the card Several configurations can be made in order to set some pins as inputs and some others as outputs In addition a 8250 family microcontroller is attached to the card and is responsible for the algorithm of the digital signal processing of the data coming into and going out of the card This microcontroller is fully programmable and can change completely the functionality of the card Still the two IRQ s of the card as well the trigger level of the them can be manually selected with the use of two jumpers Interrupt Request Line 7 Plug and Play 28 Furthermore the card has two 50 pin OPTO 22 compatible connectors where the 48 input outputs are situated divided into six 8 bit ports I O ports along with some registers A 50 way IDC connector may be easily connected to them and then transfer the data into an IDC cable connected to an external device Also the card is equipped with 48 LED s corresponding to the 48 I O pins showing the high or low logic for each pin The logic High voltage for each input varies between 2 0 and 5 25V and the logic Low voltage for the previous is above 0 0 and below 0 8V The logic High output signal is 2 4V and the logic Low of the same output is 0 4V As it was mentioned before the 48 I O pins are divided into si
59. d interface which is invisible to the user In addition it can offer applications opening the dev entry points However this interface is crucial to have the ability to mount a file system Every network transaction is made through an interface This means that there is a device that can exchange data with other hosts In most cases an interface is a hardware device however it is possible to be a pure software device The role of the network interface is to send and receive data packers driven by the network subsystem of the kernel with no knowledge of how individual transactions map to the actual packets being transmitted A network interface is not easily mapped to a node in the file system due to the fact that it is not a stream oriented device The Linux way to supply access to interfaces is also by assigning a unique name to them Nevertheless that name does not have a corresponding entry in the file system The kernel and a network device driver have a completely different communication from char and block drivers The kernel calls functions are associated to packet transmission as a replacement for reading and writing Reference a3 a4 Device Major Number In order for a device to be recognized by the kernel among the other devices each device is assigned a unique number This number is called the device s major number This can be set during the init function of the module The programmer has the choice to either allocate
60. de lt stdlib h gt include lt string h gt include lt unistd h gt include lt sys types h gt include lt sys stat h gt include lt fcntl h gt include lt linux errno h gt ifndef TRUE define TRUE 1 endif ifndef FALSE define FALSE 0 endif int read int fd int write int fd int main int argc char argv char dev char device name char ar i int fd int rcode parameter rtr while TRUE printf n Enter device driver name path n or press d for default dev isa731 scanf Ss dev if dev d device name dev isa731 50 else if dev q dev Q printf Nn Application stopped n n return 0 else device name dev file descriptor fd open device name O RDWR if device doesn t exist print an error message if fd 1 printf n Error cannot open device or device does not exist n maybe a wrong device name n n end int int file return EBUSY rtr getc stdin parameter FALSE while parameter printf Read or Write R W scanf c amp ar if aras trt hl ak ce TRY rcode read_ fd parameter TRUE else if ar w ar W rcode write fd parameter TRUE else printf Wrong parameter n parameter FALSE rtr close fd return rtr of main read int fd read a byte from the ope
61. dlib h de lt string h gt de lt unistd h gt de lt sys types h gt de lt sys stat h gt de fcntl h de lt linux errno h gt incl incl incl incl incl incl incl incl ei Gm Ge ue eO ifndef TRUI define TRUE 1 endif rtu ifndef FALSE define FALSE 0 endif int read X int fd int write int fd int main int argc char argv char dev 11 char device name char ar int fd int rcode LE while TRUE Ins printf Enter device driver nam dev greg scanf c dev if dev d device name dev greg else Ley device _name dev device name dev greg or press d for default xist print an error message or device does not fd open device name O RDWR if device doesn t if fd 1 printf Error cannot open devic exist n maybe a wrong device name n 132 return EBUSY printf R or W scanf c amp ar if ar r ar RD rcode read fd else if ar w ar rcode write fd else printf Wrong parameter nUsage t t user w t write to device n return 0 end of main int read X int fd unsigned char byte read fd amp byte 1 printf Readen d n byte return 0 int write int fd unsigned char byte user r t read from device n
62. e 201 days of implementation is almost similar to the planed work that is showed in the Gantt chart Any differences between the plan work and the actual word took place during the implementation has been reported in the progress reports Any changes to the time plan were totally agreed with the supervisor in advance 12 Linux Linux is a UNIX clone written from scratch by Linus Torvalds in 1992 with assistance from a team of developers across the network It was first developed for 386 486 based PCs These days it also runs in several other machines like DEC Alphas SUN Sparcs M68000 machines like Atari and Amiga MIPS and PowerPC During this 10 years that Linux exists it has become a very wide use operating system mostly used by well experienced users and for server or embedded computers use However it still can t overcome the Windows monopoly on the market Therefore most device manufacturers for the above platforms do not make any drivers to support their products for the Linux environment This leaves the Linux users with two options either not buy the product or build the drivers by themselves C is a function oriented programming language which was devised in the early 1970s as a system implementation language for the nascent UNIX operating system Nowadays it has become one of the dominant languages The Linux kernel and most of its modules were built in C platform By 1973 UNIX OS was almost totally wr
63. e device however it is possible to be a pure software device The role of the network interface is to send and receive data packers driven by the network subsystem of the kernel with no knowledge of how individual transactions map to the actual packets being transmitted A network interface is not easily mapped to a node in the file system due to the fact that it is not a stream oriented device The Linux way to supply access to interfaces is also by assigning a unique name to them Nevertheless that name does not have a corresponding entry in the file system The kernel and a network device driver have a completely different communication from char and block drivers The kernel calls functions are associated to packet transmission as a replacement for reading and writing Reference b3 b4 Device Major Number In order for a device to be recognized by the kernel among the other devices each device is assigned a unique number This number is called the device s major number This can be set during the init function of the module The programmer has the choice to either allocate it dynamically that is let the kernel decide about the major number kernel 2 4 x or manually by specifying the number in the source 4 e g loopback interface 35 e g ethO 85 code of the module or at load time In the current design of the module the second approach was used This is because in order to make a node for the card in the file s
64. e eee ee eee e oet ns tese stet etae esee ee ee an 2 ADSEEACU EE 3 Acknowledgements eee e ee eere eee eene entes tnn hast idon stesa Ssss 4 LG OC 5 Introduction and Objectives eee tnnnnt e ennnnntnn 9 Abbreviations used eossseeeossssecoseeeccoseessoccoeeecosseeccosseccoseeecosssoccoeeecosseeeossssocsoseeeoe 9 Project Analysis Time Plan 12 TES Le edd ped recae esteri e e a cem ordeo EE os ab acacia re ON 13 Familiarization with Li 13 KEE 20 Keme EE 21 Tnit r nlevels suas sod OR RUNE TUN RR T UN OR HC UFU ORE 21 Recompile your kernel iii lestie geed 22 The LILO boot lo d er eendeitege 24 Kernel programming 26 ISA Input Output Card 27 Ee 30 DOVICES E 31 Device Major Number 32 Device name ocx sete 33 Usage Count re uites eese itor tse et 33 How can we create a modeisaaioi ciendum ipsa ee qu Bel Us erasa ded as 34 Loading modules automatically esses 34 User Space Program rete ree vete Eet Tees Debet reo tae PvE de e 34 How the ISA I O card was programmed sssssesssseeeeeerenee 36 Step through the source code of the Module 37 Writing a script for easier manipulation eese eene een 39 Interfupts eee te o eer to ees ede terere rud rebel sectae puede T est igesqe desee aed 39 The overall progress gummara eene ener nennen 41 T s ng o
65. e project According to the students handbook this must be a sub report detailing the research undertaken in the first term of study The research for the project Linux Device Drivers consist of the following areas Familiarization with the GNU Linux operating system kernel module programming and understanding the specifications of an I O ISA card The distribution of Slackware 8 1 and the 2 4 18 official Linux kernel were used for the familiarization with GNU Linux operating system Introduction Linux is an operating system that is spreading fast into the computing market We can consider of Linux as the rising star amongst operation systems A major question must be answered before we continue why people like Linux The other operating systems have a lot of limitations they are designed for low end home users and do not deliver the performance or flexibility that we expect Furthermore Linux is free stable and runs on the majority of processors like Pentium Pro Pentium II Pentium III Pentium IV AMD Cyrix chips and even older 386 486 machines as well as some other platforms In fact Linux can be ported for almost every platform even for R T E C S At this point we should mention that Linux is a redistributable clone of the UNIX operating system which it uses the X window system graphical user interface GUI and offers several different integrated desktop environments such as KDE and GNOME Despite the fact that Linux supports a gr
66. e user to be able to choose which kernel to boot we must add the following lines in the lilo conf file prompt delay 50 As a final point we have to type the command lilo in the command line Reference a2 a4 74 Kernel programming As it 1s already mentioned in the introduction most of the kernel source code is written in C programming language The best approach by the author s point of view to develop a Linux kernel module is by using C When programming for the kernel the standards of ANSI C still exist However there are some differences between kernel space and user space programming There are some libraries used only to be linked with kernel modules and consequently there are some functions that will only be recognized by the kernel First of all we have to go over the structure of standard kernel module Each module should have one init function and one exit function Each time the module is loaded the kernel executes the init function which usually checks for the availability of some resources and or registers some of them When the module is unloaded the exit cleanup function is executed in order to un register everything that was previously registered by the module and leave the kernel as it was before If the init function fails returns a minus integer number the module is considered not to be loaded On the other hand a cleanup function should never fail that
67. eat majority of devices a great part of the manufacturing companies that produce computer related hardware do not support their products with Linux device drivers yet Subsequently development of those drivers is needed to be done by the users themselves H Real Time Embedded Computer Systems 60 The Kernel of the operating system is a bunch of code that acts as an interface between the application layer and the hardware The application layer is the user space environment where all the applications are executed by the user In order for the user space program to communicate with the hardware from the application layer the interception of a kernel is vital The kernel looks for the particular driver for the device we want to access and according to the instructions on that driver will allow the program to communicate with the specific piece of hardware e g Hard Drive CD Rom ISA card This driver is a module for the kernel usually written in C programming language Actually the most of the kernel is made up from too many kernel modules linked together The Peripheral Component Interconnect PCI bus allows us to connect a hardware card into our computer An example of a PCI card is the well known soundcard we have in our computers Nowadays the PCI standard as a result of its cost effectiveness backward compatibility scalability and forward thinking design has overcome ISA Industry Standard Architecture standard which is
68. ehavior is implemented by a char device This driver usually implements four types of system calls the open close read and write system calls Char devices are accesses by means of file system nodes A char device differs from a regular file just in the fact that in the regular file you are always able to move back and forth while char devices are just data channels This means that you can only access sequentially E g dev ttyl and dev Ip0 84 A block device as well as char devices are accessed by file system nodes in the dev directory A block device is able to host a file system for example a disk Linux permits the application to read and write a block device like a char device and allows the transfer of any number of bytes at time Consequently block and char devices are different just in the way the kernel manages internally the data and therefore in the kernel driver software interface The differences between them are noticeable to the user A block driver gives the kernel the same interface with a char driver Furthermore it offers an additional block oriented interface which is invisible to the user In addition it can offer applications opening the dev entry points However this interface is crucial to have the ability to mount a file system Every network transaction is made through an interface This means that there is a device that can exchange data with other hosts In most cases an interface is a hardwar
69. er device char device name Static int open struct inode inode struct file file static int close struct inode inode struct file file static int write struct file filp const char ptr size t count loff t f pos static int read struct file filp char ptr size t count loff t f pos module init start module exit clean File Operations structure x k X X x static struct file operations FileOps releas open read write close open read write YV4XwMy INET Bunt Lm 9e UK uec de Oe eee uer oko ecce de s dk oe oS Ke static int init start void int check check register device device name amp FileOps if check 0 printk lt 1 gt Can t load Module Mn else printk 1 Module loaded succesfully Mn 111 return check LIMY EXIT FUNCION X RR GE SR SR OKDGWe HE ose OR static void exit clean void unregister device device name REGISTER DEVICE x x x Xx x X kX x X k k k KOK static int register device char device name struct file operations FileOps int check if device name NULL device name greg register device with kernel and return gt 0 for true lt 0 for false use the current Major Number for the specific device according the File Operations check register chrdev
70. er space program access a resource and the means that the kernel uses to represent them to the file system Concepts like major and minor number were introduced and the whole picture was getting clearer After being able to create a node into the dev directory and link unlink a module to the kernel according to the research undertaken Reference a3 a5 chapter 21 the next stage was to add pieces of code to the basic structure of the file operations Several mistakes occurred during the implementation and they were solved using the examples of the books that were used as prototypes During the implementation of the project several pieces of source code were shown to the supervisor in order to be sure that the project was on the right path to accomplish its target Fortunately the module was implemented successfully Testing of the project During the implementation of the module several tests took place The read function was the first that was tried to be implemented After consulting the supervisor of the project it was decided that the read function should be implemented first Several tries later the function was returning the value OxFF 255 which were assumed 1s the default value coming from the card The next step was to send a default value to the card from the module without using the user space program When that goal was accomplished pieces of code started to be added into the user space program in order to make it communicate e
71. ers by themselves C is a function oriented programming language which was devised in the early 1970s as a system implementation language for the nascent UNIX operating system Nowadays it has become one of the dominant languages The Linux kernel and most of its modules were built in C platform By 1973 UNIX OS was almost totally written in C Thus in case that someone needs to update the kernel or implement a device driver C language is the best approximation to do that Reference a2 a4 Familiarization with Linux Linux has many distributions the most popular are Red Hat SuSe Slackware and Debian Usually you choose the right distribution for the right job In the following pages we will explore the world of Linux through the Slackware 8 1 distribution which uses the 2 4 18 official Linux kernel The first thing we must describe is how to install Slackware into our system 5 http mirror ati com support faq linux html 62 Considering that the most users have Windows installed into their system we must say that those two operating systems can co exist into the same operation system for that reason there is no need of uninstalling Windows in order to install any of the distributions of Linux The only thing we must be aware is that we should install Linux in a separate partition on our hard drive In that separate partition we should create two partitions the first is the root partition where all the GNU files am
72. ess Report of Level 3 Project th 14 21 February 2003 Review Tasks Set Last Week e Review the work that has been done on kernel programming e Writing source code for the module Tasks Set This Week e Working on the source code that has been submitted in the previous weeks for review and feedback e Anexample of the new version of the source code will be submitted to the supervisor on the next meeting e The following command was used in order to compile the new version of the module into an executable script file bin bash gcc D KERNEL Wall I usr src linux include c labcard c o labcard o Forward look to tasks for next stage e Trying to develop and use the file operations write and read Updated Source Code The libraries include linux kernel h include lt linux init h gt include linux module h include linux fs h include linux errno h less usr src linux include asm errno h include lt asm uaccess h gt include linux param h include lt linux ioport h gt include asm io h The declaration of variables char device name greg char buff int MajorNum 42 int base port 0x300 int port range 8 117 int mod bytel 0x90 int mod byte2 0x80 INIT amp EXIT function If My INIT function b ckok ek dest DE Qe Ei e des po sx ORS U ae Toe dee static int init start void int check check re
73. f the most widespread boot loaders for Linux partitions is LILO Lilo reads the configuration file etc 1ilo conf and then stores the boot parameters in the first 512 bytes of a partition or in a hard drive s MBR Master Boot Record When a PC boots it reads off those 512 bytes and then loads the kernel image described there In order to change the configuration file we can either run liloconfig or manually edit it by typing the following command jed etc lilo conf In order to add another kernel image option for example bzImage we have to declare some variables the author places them at the end of file First of all the image s path This can be done by typing into the Lilo conf image bzImage Then the root partition should be stated with root dev hda3 Additionally we must assign a name to the new kernel image in order to recognize it at boot time in the 1i10 prompt label Slackware new 24 Finally because Linux at boot time runs some diagnostic tools on the root partition in order to prevent any damage to the file system we initially mount it as read only So we add the following command read only If we want 1ilo to be installed on the MBR the following line should exist on the top of the file boot dev hda Moreover in order to have a time period equal to 50 ms in order for the user to be able to choose which kernel to boot we must add the
74. f the projeCt EE 42 Screenshots System goes live sssssssss sees 45 CCOHGIUSI BSG sa eer ose rta eost bem Ee Ut Re deco pue ERE 46 Refereno88 eebe aper ecole quta auta be ues dote opo Lud ddr efi rue 47 Appendix 1 Project Time Plan 49 Appendix 2 User Space program source code final version 50 Appendix 3 Kernel Module source code final version 53 Appendix 4 MS DOS application source code final version 57 Appendix 5 The ISA I O card PC 59 EE 62 Familiarization with Linux uso esp eau o drea RR ter Ek ntn TENERA KNIE 62 Shell Scripting esses eee 69 ME 70 VIE runlevels sectantes Eeer 70 Recompile your Femme 71 The LILO boot E 73 Kernel programming edax a aee pue Us De ete dax M RE YO SU 75 ISA Input Output Card 76 Referenc s via sah vs Cur ta tic usa Eoo FA Rees doi dto ad s e EU E 39 PADPONGIK rises See Dead pectori n EE 80 Hello kernek Caer Ee deed ee oet s 80 APDendix Bite REX ee E EE c 8 POT NERIS 81 e 84 Device Maj r NUM GT duin rr tei a eia be E eet 85 D yiGe nales oe ette rem cv ve EN E 86 Usage Count re utt se eee Ne ae el 86 How can we create a modeisaaio cien diua etica ee qu Bul yt erasa dod eed 86 Loading modules automatically essen 87 User Space Program eere ee Sides veel e ee Set Deeg Debet oo toe rut de eoe 87 How the ISA I O card was programmed
75. ff int MajorNum 42 int base port 0x200 int port range 8 int mod bytel 0x90 int mod byte2 0x80 or devic does not exist n linux errno h less usr src linux include asm errno h dymanic name declaration module load time MODULE PARM device name s MODULE PARM MajorNum i MODULE PARM base port i MODULE PARM mod bytel i MODULE PARM mod byte2 i Declaration of functions static struct file operations FileOps static int init start void static void exit clean void static int register device char device name struct file operations FileOps static int unregister device char device name static int open card struct inode inode struct file file static int close card struct inode inode struct file file static int write card struct file filp const char ptr size t count loff t f pos static int read card struct file filp char ptr size t count loff t f pos static int allocate ports unsigned int base port unsigned int port range static void set operating mode module init start module exit clean File Operations structure X X X rz x static struct file operations FileOps release close card open open card read read card write write card D L1 lag INIT function er eec er X DE e RRS ETE eer ak Te Ge static int
76. fficient with the module Working in parallel tasks trying to develop the read and the write function problems occurred between the transaction of the variables between the module and the user space program A variable was successfully been written to the device using the module but not through the user space After several tests the user space program was sending a number to be written to the device successfully In order to check that the module received the correct variable an additional printk of that variable was added into the code Conceptually if the 42 module received the correct value someone could see it using the dmesg where all the messages from the kernel are displayed The next step was to try to read that value from the module and then from the user space program Unfortunately even after the correction of some programming mistakes the incoming value was still OxFF and it was also the value that could be seen from the user space application After contacting the supervisor for any piece of advice he could provide on the problem occurred some extended testing took place A fifty pin cable and led lights were bought in order to have a real representation of the values that exist on the card The cable was connected from the CN1 connector of the card to the CN2 The cable had a third connector that was used for connecting the led lights Actually it was like sniffing the signal passing through the two connectors in order
77. ge This project provide as with all the basic knowledge and the opportunity to learn how to use the computer in order to control any type of hardware device We could connect any type of device that sends and or receives binary signals electronic circuit in order to access it from a computer That device could be a barometer a thermometer even an electronic door lock There is a large flexibility in the areas this project could be used Except the great knowledge we obtain about the core of the Operating Systems and the ability to control hardware inside and outside a computer we are also in place to use our imagination to discover where it would be helpful to every and each one of us Acknowledgements I would like to thank some people for their help in order to make a little bit easier to me to do my research and develop my final year project My supervisor Dr Gaius Mulley for being always there in order to answer any questions I had and encouraging me about the progress of the project Mr Yiannis Koukouras for helping me take my first steps as a simple Linux user and providing my with the knowledge of how I should think when I have to deal with Linux distributions Mr Nikolaos Avourdiadis for letting me borrow his books about Linux and providing me with the Linux handouts he had from last year when he was a postgraduate student Contents Statement of Originality 1 esee eer
78. ge count If the function was executed successfully it will send a second message to the kernel In order to see these messages we must type the dmesg command which provide us with all the kernel messages For the release file operation which in our case we named exit card the only thing we must do is to decrease the usage count Failure to do that will have as result to set us unable to unload the module from the kernel In order to unload the module we would have to restart the computer 38 The write function using the base address that was set for output will send to the device a value That value is the incoming value from the user space program In order for the module to read the value that is entered in the user space program we save that value to a variable The module will read this value using the pointer of the value and finally output it to the device The read function is trying to get the data from the device reading the specific base address that is set for read It then passes it to a pointer which is returned to the user space Reference al a3 a5 a6 Writing a script for easier manipulation During the research for the first sub report the author learned how to write simple script files for Linux That came to be a very helpful when the time of compiling the modules the student wrote a script file for easier manipulation bin bash gcc D KERNEL O2 Wall I usr src linux include c labcard alt c o
79. gister device device name amp FileOps if check 0 printk lt 1l gt Can t register Device Mn return check check allocate ports base port port range if check 0 printk lt 1 gt Can t load Module Mn else printk 1 Module loaded succesfully Mn set operating mode printk 1 Module loaded succesfully INIT Mn return check ff My EXIT function 4 we ewe e eoe deo pel es uel SH e e E p oe static void exit clean void unregister device device name release region base port port range release ports Set Operation Mode function Set Orerating Mode for ISA I O card PortA will be an INPUT PortB will be an OUTPUT static void set operating mode int register addressl int register address2 base port 3 base port 7 M outb p register addressl mod bytel outb p register address2 mod byte2 118 printk 1 Module loaded succesfully SETOPMOD Mn Register amp Unregister Devic REGISTER DEVICE x x x Xx Xx x Xx X x X k k k k X static int register device char device name struct file operations FileOps int check if device name NULL device name greg register device with kernel and return gt 0 for true lt 0 for false use the current Major Number for the specific device according the Fi
80. h from the supervisor s and the developers point of view that the function read of the microcontroller on the card was faulty and was outputting always the same number 44 Screenshots System goes live The following screenshots showing the loaded module and the user space application running el tege Dos compile user sh labcard alt o user exe compile labcard sh labcard alt c user c bash 2 05a insmod labcard alt o Warning loading labcard alt o will taint the kernel no license See http www tux org lkml s1 18 for information about tainted modules Module labcard alt loaded with warnings bash 2 05at lsmod Module Size Used bu Tainted P labcard alt 1384 O unused pcmcia core 40896 Oo bash 2 05a user exe Enter device driver name path or press d for default dev isa731 d Read or Write R W w Enter a byte to write to device Dec 239 You entered 239 Enter device driver name path or press d for default dev isa 31 q Rpplication stopped bash 2 05a B First the module is linked to the kernel using the insmod command followed by the name of the compiled binary file insmod labcard alt o The lsmod command shows the loaded modules The next step is to run the user space application which will prompt us for the device name Pressing the d key for the default path dev isa731 the question if we would like to Read of Write from to the ISA card will come up
81. hat it is trying to perform an operation using the I O hardware device sends to the kernel a request in order to open the specific device The kernel is looking for the specific module that is loaded for that specific device matching the major number from the device and the module The module holds information about what the kernel should do when a request reaches it When the request arrives to the kernel it searches for the appropriate module to manipulate the request The module tells to the kernel what to do with the particular request and then the kernel tells to the device to do it Finally the kernel will return to the user space program any reply that comes from the device if any The same operation takes place when a request comes from the device in the opposite direction Reference b1 b3 b5 Devices Linux has a way to identify the type of devices that correspond to the system These devices which normally exist into the dev directory can be accessed by modules that consist of three different classes These three classes of modules are the char modules block modules and network modules Every module that is included into the kernel or a module that we might want to load manually must have this type of information A small description is provided in the following lines in order to understand how the system recognizes each of the three classes of modules A character device can be accessed as a stream of bytes This b
82. hen the program is terminated by the software interrupt Ctrl C the kernel closes all the files with file descriptors used by that program and the module executes its close card function Reference a3 a5 a6 35 How the ISA I O card was programmed The ISA I O card PCL 731 has two 50 pin IDC connectors In the following picture we can see the IDC connectors According to the user s manual of the ISA card we must register two addresses in order to use them for writing and reading from the device The student after some testing decides to use the CN1 IDC connector to write to the device and the CN2 IDC connector to read from the device The pins 49 are the pins that provide the 5 Volts All the GND are the ground pins and the rest are the pins that we can send or read bits logical 1 or 0 PCO GND PC17 GND PCOG GND PC 16 GND PCOS GND PC15 GND PCO4 GND PC 14 GND PCO3 GND PETS GND PCO2 GND PC 12 GND PCO1 GND P611 GND PDC OO GND PC10 GND PB07 GND PB17 GND PB06 GND PB16 GND PBO5 GND PB15 GND PBO4 GND PB14 GND PB03 GND PB13 GND PBO2 GND PB12 GND PBO1 GND PB11 GND PBOO GND PB 10 GND PAO GND PA 17 GND PA 06 GND PA 16 GND PA 05 GND PA 15 GND PA04 GND PA 14 GND PA 03 GND PA 13 GND PA 02 GND PA 12 GND DA GND PA 11 GND DAOU GND PA 10 GND 5 V GND 5 V GND 36 By default the ISA I O card is set for a base address of 0x300 The author of this project has changed the base address of the card and was set to be 0x20
83. hoices cs uiuc edu f kon RoundRobin nodel html 93 disable or use the S W setting of the interrupts The S W option is there to allow us to control the interrupts by other external interrupts Reference b1 b5 b8 References b1 Daniel P Bovet Marco Cesati 2000 Understanding the Linux Kernel O Reilly b2 Bill Ball 1998 Teach Yourself Linux in 24 Hours Second Edition Sams Publishing b3 Alessandro Rubini amp Jonathan Corbet 2001 Linux Device Drivers Second Edition O Reilly b4 Matt Welsh Matthias Kalle Dalheimer Lar Kaufman 1999 Running Linux OReilly b5 Richard Stones amp Neil Matthew 2001 Beginning Linux Programming Wrox b6 Jesse Liberty David Hovarth 2000 Teach Yourself C for Linux in 21 Days Sams b7 Don Anderson Tom Shanley 1995 ISA System Architecture Addison Wesley b8 Advantech User s Manual for the ISA card PCL 731 48 bit Digital I O card 94 Appendix 1 user space program de de de de de de de de incl incl incl incl incl incl incl incl SHG REPOS GG ei i define endif define endif ifndef TRUI ifndef FALSE lt stdio h gt lt stdlib h gt lt string h gt lt unistd h gt lt sys types h gt sys stat h fcntl h lt linux errno h gt ou TRUE 1 CJ ce FALSE int read int fd int write int fd int main int argc char argv char dev char device n
84. ic int init start void static void exit clean void static int register device char device name struct file operations FileOps static int unregister device char device name static int open card struct inode inode struct file file static int close card struct inode inode struct file file static int write card struct file filp const char ptr size t count loff t f pos static int read card struct file filp char ptr size t count loff t f pos static int allocate ports unsigned int base port unsigned int port range static void set operating mode module init start module exit clean fief Bide Operations str ct re X e ke Ek Oe Ws ek es Ke e ok static struct file operations FileOps release close card open open card read read card write write card D 4 Mys INIT Ereegnes E static int init start void int check check register device device name amp FileOps if check 0 122 printk 1 Can t register Device Mn return check check allocate ports base port port range if check 0 printk lt 1 gt Can t load Module Mn else printk 1 Module loaded succesfully Mn set operating mode printk 1 Module loaded succesfully INIT Mn return check Po Ae EXT E Bu nctqdoni S TS RE RDS ees GRAD AES SARS SPAR pe ce deside cec ale SRE static v
85. ific device The module doesn t know the device s representative filename in the file system but just its major number Any device with the same major number is manipulated by the same module Executable and Linking Format 30 Then they are distinguished by a second numerical prefix which is the minor number The user space program assuming that it is trying to perform an operation using the I O hardware device sends to the kernel a request in order to open the specific device The kernel is looking for the specific module that is loaded for that specific device matching the major number from the device and the module The module holds information about what the kernel should do when a request reaches it When the request arrives to the kernel it searches for the appropriate module to manipulate the request The module tells to the kernel what to do with the particular request and then the kernel tells to the device to do it Finally the kernel will return to the user space program any reply that comes from the device if any The same operation takes place when a request comes from the device in the opposite direction Reference al a3 a5 Devices Linux has a way to identify the type of devices that correspond to the system These devices which normally exist into the dev directory can be accessed by modules that consist of three different classes These three classes of modules are the char modules block
86. ig Network configuration sbin insmod module o Link a module with the Kernel sbin rmmod module Unload a module from the Kernel sbin Ismod We can see the modules that already have been loaded usr sbin cfdisk Similar to fdisk partition table manager of Windows amp less etc fstab Which devices will be mount by themselves on GNU Linux 17 boot time e g root must be automount less bin dmesg In that file we can see the outputs messages of the Kernel whatis lt command gt Displays a minimum help file for the command In the diagram below we can see the directory structure in GNU Linux The top directory as we ve already mention is called root The following diagram along with a part from the description of the directories is from Introduction to UNIX security Dr Andrew Blyth Lecture Notes 2002 MSc Information Systems and Network Security University of Glamorgan Starting from the top in the previous diagram we can see the bin and sbin directory which is the location for the binary files and or script files The dev is the device directory containing special files that allow to the operating system to reference each piece of hardware The etc directory contains system configuration files and executables A very important and useful directory is that which called lost found Disk errors or incorrect system shutdown may cause files to become lost Lost files refer to disk lo
87. is 0 4V As it was mentioned before the 48 I O pins are divided into six 1 byte ports Each one of them is assigned to a port address always in respect to the base address of the card which can be in the range from 0x0 to 0x3F8 Below a table of all the I O ports and their corresponding address is shown Address Port Base 0 AO Base 1 BO Base 2 CU Base 3 CFG REG Base 4 Al Base 5 Bl Base 6 C1 Base 7 CFG REG Reference a5 all a12 a13 a14 Light Emitting Diodes 78 References al Daniel P Bovet Marco Cesati 2000 Understanding the Linux Kernel O Reilly a2 Bill Ball 1998 Teach Yourself Linux in 24 Hours Second Edition Sams Publishing a3 Alessandro Rubini amp Jonathan Corbet 2001 Linux Device Drivers Second Edition O Reilly a4 Matt Welsh Matthias Kalle Dalheimer Lar Kaufman 1999 Running Linux OReilly a5 Richard Stones amp Neil Matthew 2001 Beginning Linux Programming Wrox a6 Jesse Liberty David Hovarth 2000 Teach Yourself C for Linux in 21 Days Sams a7 Don Anderson Tom Shanley 1995 SA System Architecture Addison Wesley a8 ntroduction to UNIX security Dr Andrew Blyth Lecture Notes 2002 MSc Information Systems and Network Security University of Glamorgan a9 Linux Journal October 2001 issue 90 How to write a Linux USB device driver p 24 a10 Linux Magazine September 2002 issue 23 lang
88. is there As soon as we verify that we have the source code we can do a make clean 22 to remove any temporary and unwanted files from that directory Then we can run one of the three config editors by typing one of the following command lines make config OR make menuconfig OR make xconfig only for X The config file is a configuration file which we can see by typing less config lining in the usr src linux directory and it stores every information the make program needs to know about which kernel modules to compile with the kernel and which to compile as loadable modules After editing this file we can save the new kernel configuration and check if there any modules included in the kernel that are depended from some others which were not included with make dep If the previous command is successful we can move into compiling a compressed image of our new kernel by typing make bzImage and then compiling and installing the modules with make modules amp amp make modules install files starting with a dot are hidden and can be seen with the Is 51 command 23 Finally if everything has gone right during the compiling and installing process we can add the new kernel image bzImage situated into usr src linuc arc i386 boot into our boot loader such as lilo or grub and then reboot into it Reference a4 a5 The LILO boot loader One o
89. ith Red Hat Linux 8 0 e Research project requirements 102 Linux Device Drivers Progress Report of Level 3 Project 8 15 November 2002 Review Tasks Set Last Week e A new version of the work plan has been created Tasks Set This Week e Familiarization with Red Had Linux environment is in progress e Research about module programming is started e Researching how PCI bus interface works e A PCII O card has been proposed to the supervisor via e mail in order to decide if it s appropriate for the project Forward look to tasks for next stage e Continuing the research about Red Hat Linux environment driver programming PCI bus interface and PCI I O card specifications 103 Linux Device Drivers Progress Report of Level 3 Project 15 22 November 2002 Review Tasks Set Last Week e Familiarization with Red Had Linux environment is in progress Tasks Set This Week e Familiarization with Red Had Linux environment is in progress e Research about module programming is in progress e Researching how PCI bus interface works e Writing some simple shell scripts to familiarize how the shells work Forward look to tasks for next stage e Continuing the research about Red Hat Linux environment driver programming e Understanding PCI I O card specifications 104 Linux Device Drivers Progress Report of Level 3 Project 22 _ 29 November 2002 Review Tasks Set Last Week e Research on How
90. itten in C Thus in case that someone needs to update the kernel or implement a device driver C language is the best approximation to do that Reference a2 a4 Familiarization with Linux Linux has many distributions the most popular are Red Hat SuSe Slackware and Debian Usually you choose the right distribution for the right job In the following pages we will explore the world of Linux through the Slackware 8 1 distribution which uses the 2 4 18 official Linux kernel The first thing we must describe is how to install Slackware into our system http mirror ati com support faq linux html 13 Considering that the most users have Windows installed into their system we must say that those two operating systems can co exist into the same operation system for that reason there is no need of uninstalling Windows in order to install any of the distributions of Linux The only thing we must be aware is that we should install Linux in a separate partition on our hard drive In that separate partition we should create two partitions the first is the root partition where all the GNU files among the Linux kernel will be installed the second is the swap partition a partition which will be used as extra memory for the operating system Some other distributions like Red Hat amp choose to use an extra partition for the boot directory where the Linux kernel and all the booting configuration files will be stored
91. le Operations check register chrdev MajorNum device name FileOps if check 0 printk 1 Can t register device Mn return check return 0 UNREGISTER DEVICE kx x Kk x k x k kk x static int unregister device char device name int check check unregister chrdev MajorNum device name if check 0 printk 1 Can t unregister device Maybe in use n return check return 0 File Operation Open amp Release static int open_card struct inode inode struct file file printk lt 1 gt Module loaded succesfully OPEN Mn if MOD_IN_USE gt 0 return EBUSY if the card is beeing accesed already return Device or Resource Busy increment usage count MOD_INC_USE_COUNT printk lt 1 gt Module loaded succesfully OPEN2 Mn return 0 static int close card struct inode inode struct file file 119 decrement usage count MOD DEC USE COUNT printk lt 1 gt CLOSE return 0 Adding a device into the dev directory mknod dev greg c 0 42 120 Linux Device Drivers Progress Report of Level 3 Project 21 28 February 2003 Review Tasks Set Last Week e Writing source code for the module Tasks Set This Week e Working on the source code that has been submitted the previous week to the supervisor in order to implement the read and write file oper
92. mbine all the work that has been done up to date in order to finish and submit the final version of the second sub report for next week 135 The program from the user s manual of the ISA card that was used in order to test the card include lt stdio h gt include lt conio h gt include lt process h gt include lt dos h gt main int base 0x200 int portA int portB int portC int 1574 corscx gotoxy 25 3 textattr 0x70 cputs PCL 731 Testing Program gotoxy 11 6 printf PortAO output value gt gotoxy 11 8 printf PortBO output value gt gotoxy 11 10 printf PortCO output value gt gotoxy 43 6 printf PortAl readback gt gotoxy 43 8 printf PortBl readback gt gotoxy 43 10 printf PortCl readback gt Fh Fh oH Ein h hK outportb base 3 0x80 for j 0 Jj lt 0x100 j outportb base j gotoxy 34 6 print f 2x Jj portA inportb base gotoxy 63 6 printf 2x portA if portA j printf 7 beep gotoxy 30 13 textattr 0x09 cprintf PortAl readback error getch exit 1 outportb basetl j gotoxy 34 8 printf 2x portB inportb base 1 gotoxy 63 8 printf 2x portB if portB j printf 7 beep 136 gotoxy 30 13 textattr 0x09 cprintf PortBl readback error getch exit 1 outportb baset 2 4 gotoxy 34 10 print
93. module h include linux fs h include linux errno h less usr src linux include asm errno h include asm uaccess h include linux param h include linux ioport h include asm io h define _ NO VERSION EXPORT NO SYMBOLS declare device nam char device name isa731 char buff int MajorNum 42 int base port 0x200 int port range 8 int mod bytel 0x90 int mod byte2 0x80 dymanic name declaration 8 module load time MODULE PARM device name s MODULE PARI MajorNum i MODULE PARM base port i MODULE PARM mod bytel i MODULE PARM mod byte2 i Declaration of functions static struct file operations FileOps static int init start void static void exit clean void 53 static int register device char device name struct file operations FileOps static int unregister device char device name static int open card struct inode inode struct file file static int close card struct inode inode struct file file static int write card struct file filp const char ptr size t count loff t f pos static int read card struct file filp char ptr size t count loff t f pos static int allocate ports unsigned int base port unsigned int port range static void set operating mode module init start module exit clean 4 File Operations structure 9 X ow We de Ck Ko E
94. modules and network modules Every module that 1s included into the kernel or a module that we might want to load manually must have this type of information A small description is provided in the following lines in order to understand how the system recognizes each of the three classes of modules A character device can be accessed as a stream of bytes This behavior is implemented by a char device This driver usually implements four types of system calls the open close read and write system calls Char devices are accesses by means of file system nodes A char device differs from a regular file just in the fact that in the regular file you are always able to move back and forth while char devices are just data channels This means that you can only access sequentially E g dev ttyl and dev lp0 31 A block device as well as char devices are accessed by file system nodes in the dev directory A block device is able to host a file system for example a disk Linux permits the application to read and write a block device like a char device and allows the transfer of any number of bytes at time Consequently block and char devices are different just in the way the kernel manages internally the data and therefore in the kernel driver software interface The differences between them are noticeable to the user A block driver gives the kernel the same interface with a char driver Furthermore it offers an additional block oriente
95. n can read values from the ISA card but the testing will be performed during the 3rd stage of the project e Combine all the work that has been done up to date in order to finish and submit the final version of the second sub report at the end of this week Forward look to tasks for next stage e The final testing of the module and the writing of the third sub report will start the next week 145 Linux Device Drivers Progress Report of Level 3 Project 28 25 April 2003 Review Tasks Set Last Week e Testing the source code of the module e Combining all the work and research has been done up to date e Started writing the third and final report Tasks Set This Week The final report of the project is in a very good level The previous work from the first and the second sub report has been attached to the final report e A few more tests were performed to the module Led lights are used to test the ISA I O card Forward look to tasks for next stage e Finishing the final report and solve the problem that the read function has reading the wrong value 146 Linux Device Drivers Progress Report of Level 3 Project 25 2 May 2003 Review Tasks Set Last Week e Testing the source code of the module e Continue on writing the final report e E mail the manufacturer of the I O card in order to check if the card has a hardware problem Tasks Set This Week e The final report of the project is constantly
96. ncrease and decrease the usage count This operation takes place when we use the file operations open and release When we open the device we must increase the usage count and when we release the device we must decrease the usage count Reference b3 b5 33 How can we create a node As we already mention a node is a file that represents a device into the dev directory In order to create a node we must use the following command before load our module to the kernel mknod DEVICE NAME c MAJOR NUM MINOR NUM E g mknod dev isa731 c 42 0 That command creates a node into the dev directory named isa731 that is a character device c with major number 42 and whose minor number is 0 The minor number can help to open the same device in a different way Furthermore if we have on our system 2 identical devices e g two ISA I O cards we can access them using the same module but the node for each card will have a different minor number Reference a3 Loading modules automatically An especially useful feature is the kernel daemon kernela The kernel can load needed device drivers and other modules automatically without manual intervention from the system administrator If the modules are not needed after a period of time 60 seconds they are automatically unloaded as well In order to take use of the kerneld we must turn it on during the kernel configuration along with the System V IPC option The
97. ned file unsigned char byte read fd amp byte 1 printf Win Readen d n byte return 0 write int fd write a byte that the user enters to the opened unsigned char byte int panos 51 printf Enter a byte to write to device Dec scanf d amp panos printf You entered d n panos byte panos write fd amp byte 1 return 0 52 Appendix 3 Kernel Module source code final version BK RK kk ok kk Sk kk kk A A K K K K E K A hh Ok Ck ke k Kok kk kk ok A kk kc hh A I AI k k K k labcard alt c Device Driver for ISA I O card PCL731 begin 01 Nov 2002 copyright C 2002 by Grigorios Fragkos email djgreg linuxmail org KK CK Ck Sk CK kk kk ck Sk KC Ck Kok kk kk kk kk CK kk Sk hh ck ke e ke Kok kk Ck ke kk koe KKK KKK KK KKK KKK ke e ex d BR KKK KK KK k kk kk Ck Ck kk kk kK k Kok kk Ck k k Sk Kok kk ko ke kk Sk kk Sk ck Sk k CK KC kk kk kk koc kk ke kk kk kk This program is free software you can redistribute it and or modify it under the terms of the GNU General Public License as published by the Free Software Foundation either version 2 of the License or at your option any later version KK CK Ck Sk CK kk kk ck kK Kk kk kk kk kk kkk Ck ck Sk hh Ck ke e ke Kk kk AA kk ke KKK AA ek e ex f ifndef _ KERNEL _ define _ KERNEL _ endif ifndef MODULE define MODULE endif include linux kernel h include linux init h include linux
98. no new line k getwchar read p Your name K gt read input and place it into K 81 2 Sub Report About the report This 1s the second sub report of the project According to the students handbook this must be a sub report detailing how the research has been evaluated and applied with respect to the design and development of the proposed system This report will not normally include details concerning any stage of the actual coding Because of the nature of the current project and the requirements of the supervisor this sub report will include the source code of the project and how that was achieved The main research that was undertaken during that stage of development was to design understand and develop the module for the I O ISA card Introduction In the following lines of the report we are going to cover the major aspects concerning a device module implementation We will start approaching the kernel structure and how it s handles the modules in order to access devices Furthermore we will see how a module links to the kernel and how it performs the various operations Additionally we will explain the role of the user space program in order to access the device In order to make use of the I O card we will analyze how the ISA card was programmed using the specification of the user s manual The issues concerning the C code that was produced and the source code structure will be covered Finally some additional informati
99. nto the dev directory In order to create a node we must use the following command before load our module to the kernel mknod DEVICE NAME c MAJOR NUM MINOR NUM E g mknod dev isa731 c 42 0 That command creates a node into the dev directory named isa731 that is a character device c with major number 42 and whose minor number is 0 The minor number can help to open the same device in a different way Furthermore if we have on our system 2 identical devices e g two ISA I O cards we can access them using the same module but the node for each card will have a different minor number Reference b3 Loading modules automatically An especially useful feature is the kernel daemon kerneld The kernel can load needed device drivers and other modules automatically without manual intervention from the system administrator If the modules are not needed after a period of time 60 seconds they are automatically unloaded as well In order to take use of the kerneld we must turn it on during the kernel configuration along with the System V IPC option The kernel configuration is the kernel recompile that we discussed in the first sub repost of this project Reference b3 b4 User Space Program In this section of the report we would like step through and explain some basic points of the user s space source code The source code of the user space is included in the appendix 1 87 The user space program in order to send
100. o sk de ioo kr Ces olo es Aes kr oko ce we kv Uk oko ks ck oko ces Se ce odes xe ke r Eo E Ge QNM MEL DS IE FILE OPERATIONS REA WRITE RELEASE kk KR KR kk kk kk kk kk zk kk zk kk X X kk X O TO ea oz Se coco cus Sce den xke we static int open_card struct inode inode struct file file printk lt 1 gt Module loaded succesfully OPEN Mn if MOD_IN_USE gt 0 return EBUSY if the card is beeing accesed already return Device or Resource Busy increment usage count MOD_INC_USE_COUNT printk 1 Module loaded succesfully OPEN2 Mn return 0 static int close card struct inode inode struct file file decrement usage count MOD DEC USE COUNT printk lt 1 gt CLOSE return 0 static int write card struct file filp const char ptr size t count loff t f pos int address base port 4 int buff ptr printk lt 1 gt WRITE d to device n n n buff outb p buff address write to device return 0 static int read card struct file filp char ptr size t count loff t f pos int address base port f EES AREER OR printk lt 1 gt READ ptr inb p address get data from device printk lt 1 gt READ2 return 0 131 The following is the source code of the User Space program that has implemented up to date de lt stdio h gt de st
101. o the jed editor less etc shadow Will open the shadow file into the less editor Is less Will send the result of the Is command into the less editor ls gt greg txt We can save to a file called greg txt in the top level directory the result of the ls command su Usually in GNU Linux we log in as simple user for security 65 purposes In order to change our login to super user root we must type su without log off and log on again and the system will prompt as for the root s password su user Change to user login usr sbin adduser In order to add a new user to our system usr bin passwd In order to change modify passwords pwd This will return the current path that we are working on ntsysv Adjustments of what is going to start at boot time RedHat ps adl This will display the processes currently running detailed export PS1 In order to change the prompt e g export PS1 Yes Master who Lists all users currently logged in to the this machine whoami Display information for the current user login id Display id number and privileges of the current user login find name Searches the file system for the requested filename Wild filename characters such as and can be used whereis command Displays in which directories included in the PATH the command is situated echo PATH Displays the path directories usr bin pine A program
102. oid exit clean void unregister device device name release region base port port range release ports Set Orerating Mode for ISA I O card PortA will be an INPUT PortB will be an OUTPUT static void set operating mode int register addressl1 base port 3 int register address2 base port 7 ER outb p register addressl mod bytel outb p register address2 mod byte2 printk 1 Module loaded succesfully SETOPMOD Mn Allocating Ports to the Registered Device x kx x X x xk x static int allocate ports unsigned int base port unsigned int port range int err 123 if err check region base port port range O0 return err device busy request region base port port range device name return 0 REGISTER DEVICE x kk Xx X x Xx X x Xx k k zk k k X static int register device char device name struct file operations FileOps int check if device name NULL device name greg register device with kernel and return gt 0 for true lt 0 for false use the current Major Number for the specific device according the File Operations check register chrdev MajorNum device name FileOps if check 0 printk 1 Can t register device n return check return 0 UNREGISTER DEVICE kx x x X X x k kk
103. on concerning an easier way to manipulate the module by writing a small script and a section about interrupts is provided 82 How a module works In the following diagram we can see how a module is linked to the kernel and how it manipulates requests from the user space program or the device through the kernel in order to perform the requested operations User space application Module 1 Official Linux Kernel Module 2 Module for T O ISA card Device Once we have developed a module we must link it to the kernel Using the insmod command and the name of the ELF file we can load our module to the Kernel e g insmod 1sa731 0 Typing the lsmod command we can see if the module was successfully loaded or we can use the dmesg command to see what messages the module sends to the kernel When a module is linked to the kernel it registers a range of ports and a major number This range of ports is going to be accessed only by our module and that s where the device is connected The major number is a unique number in order for the kernel to identify the specific device The module doesn t Executable and Linking Format 83 know the device s representative filename in the file system but just its major number Any device with the same major number is manipulated by the same module Then they are distinguished by a second numerical prefix which is the minor number The user space program assuming t
104. ong the Linux kernel will be installed the second is the swap partition a partition which will be used as extra memory for the operating system Some other distributions like Red Hat amp choose to use an extra partition for the boot directory where the Linux kernel and all the booting configuration files will be stored Except the fdisk partition table manipulator GNU Linux uses some other similar yet more user friendly programs such as the Disk Druid and cfdisk in order to help as partition our hard drive Red Hat and Slackware have a user friendly interface in order to help us install GNU Linux into our system The file systems supported by GNU Linux are plenty like reiserfs fat32 minix amp ISO9660 udf ext2 3 and MS DOS MP 8 mount Usually we use the console environment as the basic environment which is something like MS DOS command line in Windows but with furthermore capabilities and functionalities The administrator login in Linux is called root and we navigate through the directories using the following commands cd cd cd Is etc In order to find a program or command that is suitable for a specific operation we can search through the apropos MP 1 apropos database and then use it s manpage MP 1 man to understand the way it works In order to exit a manpage press the Q key on your keyboard 63 All devices in Linux have a special file to be represented This special files are into the
105. or receive data from the device must use the module as the middle man The module in order to access the device uses the special function to write to ports From the user space program we must use normal file operations on the devices representative file In order to use the file operations through the module that we developed we must open the file This will open the device in order to be used the open card function of the module will be executed or if the device does not exist it will print a message Then we have a loop in order for the program to prompt us continuously if we would like to read from or write to the device Pressing the Ctrl C the program will exit In each case read or write we call a function These functions are read and write In the write function using the file descriptor acquired from the open function we write a value to the file At that time the write card function of the module is executed The read function reads from the file the input value from the device and causes the read card function of the module to execute When the program is terminated by the software interrupt Ctrl C the kernel closes all the files with file descriptors used by that program and the module executes its close card function Reference b3 b5 b6 88 How the ISA I O card was programmed The ISA I O card PCL 731 has two 50 pin IDC connectors In the following picture we can see the IDC connectors According to
106. ortb base 2 byte3 inportb base 4 byte4 inportb base 5 byte5 inportb base 6 printf 0x200 d 0x201 x 0x202 x 0x204 d 0x205 2 x 0x206 x byte bytel byte2 byte3 byte4 byte5 getch return 0 The source code of the user space program include stdio h include stdlib h include string h include lt unistd h gt include sys types h include sys stat h include lt fcntl h gt include linux errno h ifndef TRUE define TRUE 1 endif 138 ifndef FALSE f define FALSE O0 endif int main int argc char argv char device_name int fd unsigned char byte 0xA9 device name dev greg fd open device name O RDWR if device doesn t exist print an error message if fd 1 printf Error cannot open devic maybe a wrong device name n return EBUSY write fd amp byte 1 return 0 The source code of the module ifndef KERNEL define KERNEL endif ifndef MODULE define MODULE endif include linux kernel h include lt linux init h gt include lt linux module h gt include linux fs h include include lt asm uaccess h gt include linux param h include lt linux ioport h gt include asm io h define _ NO VERSION EXPORT NO SYMBOLS declare device nam char device name greg char bu
107. rdware parts of a computer send and receive information in order to make it available to react with the outside world should be researched All the research is should be combined in order to design and develop a module for the hardware device This module will be available to be accessed by a user space program that was developed The original idea of researching the kernel of an operating system and starting writing source code for accessing a hardware device came up from the Operating Systems module we had in the second year of our degree scheme Also by the author s experience software engineers should know how to make the computer a tool to their hands in order to control hardware devices as much as inside and outside the PC case by code Abbreviations used MP 7 command for more information refer to the manpage of command in section number with the command line man command Every reference has a unique identifier At end of each paragraph there is a tag similar to this Reference a3 a4 that refers to the each relevant reference Page 156 Linux Device Drivers Final Report Introduction The following report is an introduction to Linux operating system and teaches you the concepts involved with kernel programming in order to design and implement a kernel module In this case study a device driver for an I O ISA card will be implemented We will start with a familiarization with Linux and we will end w
108. representation of the privileges of a file is showed below rwxrwxrwx r Stands for read w Stands for write x Stands for execute We can see the privileges of a file typing the command qs As we can see there are three triplets of rwx The first triplet is the owner s privileges The second triplet is the privileges of the owner s group and the third triplet is the other groups triplets Assigning a number for each letter e g r gt 4 w gt 2 x gt we can create a unique number for each privileges we want to give If we want read write and execute for the owner we add 4 2 and 1 which gives us 7 If we want read only access for the other groups triplets we use the number 4 In our case we came up with the number 764 rwxrwxrwx 42 1142114 2 1 iT uc unos yr q 6 4 20 We must type the following command to change privileges chmod 764 file in order to run the script we have just created we will type myFile or we can move it into a directory that is included in the path and just type myFile Reference a5 Kernel Init runlevels From the moment that an operating system starts booting until it reaches the run level we wish it passes through the init faces This faces are the init run levels which are 6 from init 0 to init 6 Below we can see what is happening to the operating system as it passes from the one stage run level to the other init 0 init 0 and init 6 are
109. riting the source code for the module using the C programming language Linux is an operating system that is spreading fast into the computing market We can consider of Linux as the rising star amongst operating systems A major question must be answered before we continue why people like Linux The other operating systems have a lot of limitations they are designed for low end home users and do not deliver the performance or flexibility that is expected Furthermore Linux is free stable and runs on the majority of processors like Alpha Sparc Pentium Pro Pentium Il Pentium III Pentium IV AMD Cyrix chips and even older 386 486 machines as well as some other platforms In fact Linux can be ported for almost every platform even for R T E C S At this point we should mention that Linux is a redistributable clone of the UNIX operating system which uses the X window system graphical user interface GUI and offers several different integrated desktop environments such as KDE and GNOME Despite the fact that Linux supports a great majority of devices a great part of the manufacturing companies that produce computer related hardware do not support their products with Linux device drivers yet Subsequently development of those drivers is needed to be done by the users themselves The Kernel of the operating system is a bunch of code that acts as an interface between the application layer and the hardware The application layer is the user
110. rma to be completed and submitted by end of week four Autumn Term Student s Name Grigorios Fragkos First Assessor Dr Gauis Mulley i eso ite Second Assessor Mr Keith Verheyden Project Title Linux Device Drivers Project Objectives General research on Linux Research device drivers Research on how the ISA card works Deliverables Implementation Kernel Device Driver for the ISA NO OAE esee cin eot diues User mode test program Please tick this box to indicate your awareness of the university s policy on ethical issues 150 The deliverables and objectives can often change due to unforeseen circumstances or through the student s research causing the project to follow a different path If this is the case and the project objectives change significantly then the first assessor should make a note of the date and fill in a new objectives proforma which should also be included as an appendix to the project report The project organiser is to be consulted at this stage 151 Agreed Marking Scheme Weightings Degree Scheme in Computing FINAL Project Assessment and Comment Form Student Grigorios Fragkos eeeeeees Project Title Linux Device Drivers Supervisor 1 2 Dr Gaius Mulley Mr Keith Verheyden Mark Weighting Agreed Mark
111. rn 0 113 Linux Device Drivers Progress Report of Level 3 Project 24 31 January 2003 Review Tasks Set Last Week e Review the work that has been done on kernel programming and trying to write some more source code Tasks Set This Week e Working on the source code that has been submitted to the supervisor last week for review and feedback Forward look to tasks for next stage e Further research on kernel programming 114 Linux Device Drivers Progress Report of Level 3 Project st 31 7 February 2003 Review Tasks Set Last Week e Review the work that has been done on kernel programming and trying to write some more source code Tasks Set This Week e Working on the source code that has been submitted in the previous weeks for review and feedback Forward look to tasks for next stage e Working on the structure of the module trying to identify some key points and finally to compile a simple module 115 Linux Device Drivers Progress Report of Level 3 Project 7 14 February 2003 Review Tasks Set Last Week e Review the work that has been done on kernel programming and trying to write some more source code Tasks Set This Week e Working on the source code that has been submitted in the previous weeks for review and feedback Forward look to tasks for next stage e Trying to develop and use the file operations write and read 116 Linux Device Drivers Progr
112. rnal October 2001 issue 90 How to write a Linux USB device driver p 24 10 Linux Magazine September 2002 issue 23 language of C p 66 11 Advantech User s Manual for the ISA card PCL 731 48 bit Digital I O card 154 Webliography 1 http www cse ogi edu class cse521 2002fall Lec02 ISA ppt 2 http sunsite tut fi hwb co ISA Tech html 3 http www cs arizona edu computer help policy DIGITAL unix AA QOR6C TETI html TITLE html 4 http database sarang net study linux johnsonm devices html 5 http www freeos com articles 2677 2 13 6 http www linuxplanet com linuxplanet tutorials 1019 1 7 http usb cs tum edu usbdoc 8 http class et byu edu eet343 Lecture 20Notes OS_DeviceDrivers htm 155
113. s Defining the module as NO VERSION means that the module has no problem to cooperate with any kernel version Then we can see the functions that were used and the declaration of the init and exit functions The init function is the first function that will run when the module is loaded into the kernel correspondently the exit function is the function that will run when we unload the module from the kernel These functions init exit will register and un register the device using the corresponding functions register device and unregister device The set operation mode function will write a specific byte to specific ports of the card in order to set the input and output ports of the card Those ports are called the registers Finally the most important part of the module is the actions that are being performed by the file operations In the first case we have the open card file operation which checks if the device is in use in order to print a message to the kernel if it is and then 91 increases the usage count If the function was executed successfully it will send a second message to the kernel In order to see these messages we must type the dmesg command which provide us with all the kernel messages For the release file operation which in our case we named exit card the only thing we must do is to decrease the usage count Failure to do that will have as result to set us unable to unload the module from the kernel
114. s why the programmer should make sure that every procedure included in this function must be successful As everything in the GNU Linux operating system is treated as a file by the kernel then a file operations structure should exist in order to declare which operations are permitted and how the kernel should react when each of them is called by the user space Whenever file operation is performed on a file by the application layer the kernel searches for the module that has been register with this specific file and responds as its file operations structure describes 17 American National Standard Institute 75 After the C program has been developed it must be compiled but not linked using a ANSI C compiler Using the gcc compiler this can be done with the following command line gcc e module name c This will produce our ELF binary module name o This should be linked then into the kernel by typing insmod module name o Now the module should be loaded into the kernel We can verify that by listing all the modules loaded into the kernel with Ismod and checking if it is in the listing If someone would like to unload the module he should type rmmod module name Reference al a3 a5 a6 a9 al0 ISA Input Output Card A BUS in the computer world is defined as the medium around which the data from a device to another one circulate or of a device to the memory and or the CPU A card can communicate with
115. t stage e Contacting the supervisor in order to ask what should I do with the ISA card I ve already contact the company and ask them what I should do in order to return the card Because I m a foreign student and I won t be in the UK for the Christmas period it would be impossible for the company which I ordered the card to contact in case they need any information From the other hand I cannot wait to return the card after Christmas period because they might not accept it 106 Linux Device Drivers Progress Report of Level 3 Project 6 13 December 2002 Review Tasks Set Last Week e Research on How Linux Works is in progress e Research on How to write a Kernel Driver is in progress Tasks Set This Week e Familiarization with Slackware Linux environment is in progress e Research required C source code for Linux e Learning how to recompile the Linux Kernel in order to make it work with all hardware devices and be able to see the NTFS partition of windows e Working on the specifications of the ISA I O card e Searching for some information in the Linux Journal and Linux magazine e Some part of the sub report that is to be submitted on Friday 10 12 03 has been written Forward look to tasks for next stage e Provide to the supervisor a sample of the work that has been done until now and the part of the sub report that has been implemented 107 Linux Device Drivers Progress Report of Level 3 Project
116. ter 2 lines we add the following command also Option ZAxisMapping Md ett We can press Ctrl X C inorder to exit and we press y to save the changes If we did that through the X Windows we must restart our X Server The editors that we can use in order to view or edit files in GNU Linux are plenty The most well known are jed emacs pico nano vi and vim which can be used to modify files and more or less which can be used only to view a file read only mode In the following table we will see some of the most important commands and files of GNU Linux along with a small description in order to have a generic idea of what we are going to talk about in the following pages Before we go along we must describe another helpful functionality of GNU Linux Typing the name of a folder file or command we can auto complete the rest of the word by clicking the tab button If we double click the tab button will return all the possible commands and pathnames that can be used from that user at the specific location starting with the letter s we ve typed File Command Description jed etc passwd Will open the passwords file into the jed editor less etc shadow Will open the shadow file into the less editor Is less Will send the result of the Is command into the less editor 16 ls gt greg txt We can save to a file called greg txt in the top level directory the result of the ls command
117. that will allow as to access our e mails usr bin elm A program that will allow as to access our e mails sbin ifconfig It s the same command as ipconfig for Windows ifconfig ethO up Enable the first Ethernet card The ifconfig ethO down will disable it sbin netconfig Network configuration sbin insmod module o Link a module with the Kernel sbin rmmod module Unload a module from the Kernel sbin Ismod We can see the modules that already have been loaded usr sbin cfdisk Similar to fdisk partition table manager of Windows less etc fstab Which devices will be mount by themselves on GNU Linux boot time e g root must be automount less bin dmesg In that file we can see the outputs messages of the Kernel whatis command Displays a minimum help file for the command 66 In the diagram below we can see the directory structure in GNU Linux The top directory as we ve already mention is called root The following diagram along with a part from the description of the directories is from Introduction to UNIX security Dr Andrew Blyth Lecture Notes 2002 MSc Information Systems and Network Security University of Glamorgan Starting from the top in the previous diagram we can see the bin and sbin directory which is the location for the binary files and or script files The dev is the device directory containing special files that allow to the operating system to reference each
118. the CPU using a rank of ports possible interrupts and DMA s In the following points it is possible to be seen as they are assigned or not in each type of bus architecture 18 Direct Memory Access 76 The first bus architecture that was implemented in PC architecture was ISA At first it was an 8 bit medium but it quickly evolved in 32 bits This indicates that every time in each clock cycle the bus was able to transfer one and two bytes respectively Even thought technology enables to increase the bus frequency for ISA backward compatibly issues force it to be stable at 8 33 MHz A 16 bit ISA card is able to support a maximum of 16 megabytes of transference per second for all the devices simultaneously a record which has been overcome long time ago The PCI architecture is the newest and most widespread now days bus architecture Norm PCI indicates that a 4 BUS PCI is solely of 4 slots but allows us to connect another 4 slots in groups by means of PCI bridging Also PCI is a 32 bit bus with a frequency of 33 MHz This means that the maximum transfer rate can be 133 megabytes per second This is more than double the maximum speed of an ISA bus Additionally IRQ sharing can be supported by this architecture something that PnP and Windows take a great advantage of The author initially had chosen to use a PCI card for the purposes of this project Unfortunately a posting error forced the use of an ISA Inp
119. the ancestor of the PCI bus Even thought the ISA bus architecture is still used by most manufacturers this is due to the fact that the transaction from the one architecture to the other is very expensive for the most of the industry and the managers think that is this is not a value for money solution The object of this project is to familiarize the student with the concepts of Linux Kernel programming the GNU Linux Operating System ISA bus interface and C programming in order to develop implement and test a kernel module which will provide the appropriate environment to a user space application to access the ISA I O card Reference a2 a4 61 Linux Linux is a UNIX clone written from scratch by Linus Torvalds in 1992 with assistance from a team of developers across the network It was first developed for 386 486 based PCs These days it also runs in several other machines like DEC Alphas SUN Sparcs M68000 machines like Atari and Amiga MIPS and PowerPC During this 10 years that Linux exists it has become a very wide use operating system mostly used by well experienced users and for server or embedded computers use However it still can t overcome the Windows amp monopoly on the market Therefore most device manufacturers for the above platforms do not make any drivers to support their products for the Linux environment This leaves the Linux users with two options either not buy the product or build the driv
120. the device fd0 1st floppy disk drive to the floppy directory inside the mnt directory mount dev fd0 mnt floppy To disconnect the device we must type umount dev fd0 or umount mnt floppy We could use mnt directory to mount a device If we do that the mnt directory will be our device now until we unmount it When we unmount the mnt directory we will be able again to see the contents of the original directory We can think of mount operation as a stack We can mount several devices one above the other on a single directory The last device we mounted e g CD ROM is 19 the device we are going to use from that directory Unmounting the last device automatically the directory is mounted to the previous device we had mounted e g Floppy Drive We will continue unmounting devices until we get the original directory mnt This is not useful very often and thats why we use different directories for different devices MP 8 mount Reference a2 a3 a4 a5 a8 Shell Scripting The shell scripting in Linux is similar to vbscript of windows and is very easy to use in order to implement rapid scripts At the Appendix B of the first sub report Page 81 there is a simple example that shows some useful commands in shell scripting After writing our script we must change the privileges of the file in order to make it executable and be able to run it A visual
121. the user s manual of the ISA card we must register two addresses in order to use them for writing and reading from the device The student after some testing decides to use the CN1 IDC connector to write to the device and the CN2 IDC connector to read from the device The pins 49 are the pins that provide the 5 Volts All the GND are the ground pins and the rest are the pins that we can send or read bits logical 1 or 0 PCO GND PC17 GND PCOG GND PC 16 GND PCOS GND PC15 GND PCO4 GND PC 14 GND PCO3 GND PC 13 GND PCO2 GND PC 12 GND PCO1 GND FEH GND PDC OO GND PC10 GND PB07 GND PB17 GND PBO6 GND PB16 GND PBO5 GND PB15 GND PBO4 GND PB14 GND PBO3 GND PB13 GND PBO2 GND PB12 GND PBO1 GND PB11 GND PBOO GND PB 10 GND PAO GND PA 17 GND PA 06 GND PA 16 GND PA 05 GND PA 15 GND PA04 GND PA 14 GND PA 03 GND PA 13 GND PA 02 GND PA 12 GND DA GND PA 11 GND DAOU GND PA 10 GND 5 V GND 5 V GND 89 By default the ISA I O card is set for a base address of 0x300 The author of this project has changed the base address of the card and was set to be 0x200 In order to set the CN1 IDC connector that will be used for writing to the device we send to the address 0x203 base address 3 the binary number 10000000 When we write a byte to the device we are setting the pins PA00 PAO7 to logical 1 or 0 depending to the byte we want to write For example if we write to the card the binary number 11110001 the pins PAO7 PA06 PA05 PA04
122. tree and make it visible by the menuconfig and be able to use interrupts Considering that from the beginning the nature of the project was very challenging the developer accomplished to rich the best level of development he could 46 References al Daniel P Bovet Marco Cesati 2000 Understanding the Linux Kernel O Reilly a2 Bill Ball 1998 Teach Yourself Linux in 24 Hours Second Edition Sams Publishing a3 Alessandro Rubini amp Jonathan Corbet 2001 Linux Device Drivers Second Edition O Reilly a4 Matt Welsh Matthias Kalle Dalheimer Lar Kaufman 1999 Running Linux OReilly a5 Richard Stones amp Neil Matthew 2001 Beginning Linux Programming Wrox a6 Jesse Liberty David Hovarth 2000 Teach Yourself C for Linux in 21 Days Sams a7 Don Anderson Tom Shanley 1995 SA System Architecture Addison Wesley a8 ntroduction to UNIX security Dr Andrew Blyth Lecture Notes 2002 MSc Information Systems and Network Security University of Glamorgan a9 Linux Journal October 2001 issue 90 How to write a Linux USB device driver p 24 a10 Linux Magazine September 2002 issue 23 language of C p 66 al 1 Advantech User s Manual for the ISA card PCL 731 48 bit Digital I O card al 2 http ww w cse ogi edu class cse521 2002fall Lec02 ISA ppt a13 http sunsite tut fi hwb co_ ISA Tech html 47 a14 Jhttp www cs arizona edu computer help policy DIGITAL unix AA
123. uage of C p 66 411 Advantech User s Manual for the ISA card PCL 731 48 bit Digital I O card 79 a12 http ww w cse ogi edu class cse521 2002fall Lec02 ISA ppt a13 http sunsite tut f hwb co ISA Tech html a14 Jhttp www cs arizona edu computer help policy DIGITAL unix AA QOR6C TETI html TITLE html Appendix A Hello kernel c include lt linux kernel h gt define KERNEL define that this part of the source code of the kernel define MODULE define that this is a kernel module define NO VERSION inform the compiler that the module will be kernel version independent module init hello define the init function of the module module exit bye define the exit function of the module static int init hello void printk lt 1 gt Hello from kernel n the kernel prints a message return 0 j static void exit bye void printk lt 1 gt Bye bye from Kernel n j The number lt gt into the printk statement declares the priority of this kernel message among the others 1 is the highest priority 80 Appendix B MyFile sh bin bash k 1 echo running script echo mounting mount dev hda mnt read p enter key echo unmounting umount dev hda echo k exit Explaining the previous notation n gt With whitch cell i ll run the script gt comments echo gt print echo n Your name gt n means
124. uded in this function must be successful As everything in the GNU Linux operating system is treated as a file by the kernel then a file operations structure should exist in order to declare which operations are permitted and how the kernel should react when each of them is called by the user space Whenever file operation is performed on a file by the application layer the kernel searches for the module that has been register with this specific file and responds as its file operations structure describes American National Standard Institute 26 After the C program has been developed it must be compiled but not linked using a ANSI C compiler Using the gcc compiler this can be done with the following command line gcc c module name c This will produce our ELF binary module name o This should be linked then into the kernel by typing insmod module name o Now the module should be loaded into the kernel We can verify that by listing all the modules loaded into the kernel with lsmod and checking if it is in the listing If someone would like to unload the module he should type rmmod module name Reference al a3 a5 a6 a9 a10 ISA Input Output Card A BUS in the computer world is defined as the medium around which the data from a device to another one circulate or of a device to the memory and or the CPU A card can communicate with the CPU using a rank of ports possible interrupts and DMA s In
125. ue to the card The following is the source code of the ISA I O card module that has implemented up to date ifndef KERNEL define KERNE endif Cl ifndef MODULE define MODULE endif include linux kernel h include lt linux init h gt include linux module h 127 include linux fs h include linux errno h less usr src linux include asm errno h include lt asm uaccess h gt include linux param h include lt linux ioport h gt include asm io h define _ NO VERSION EXPORT NO SYMBOLS declare device nam char device name greg char buff int MajorNum 42 int base port 0x300 int port range 8 int mod bytel 0x90 int mod byte2 0x80 dymanic name declaration module load time MODULE PARM device name s MODULE PARM MajorNum i MODULE PARM base port i MODULE PARM mod bytel i MODULE PARM mod byte2 i Declaration of functions static struct file operations FileOps static int init start void static void exit clean void static int register device char device name struct file operations FileOps static int unregister device char device name static int open card struct inode inode struct file file static int close card struct inode inode struct file file static int write card struct file filp const char ptr
126. updated and in a very good level e A few more tests were performed to the module e There was no answer from the manufacturer of the I O card and we are still trying to solve the problem Forward look to tasks for next stage e Bring the final report to the last state of writing and try to find out if there could be any solution about the I O card in order it could read correctly 147 Linux Device Drivers Progress Report of Level 3 Project 2 9 May 2003 Review Tasks Set Last Week e Testing the source code of the module e Continue on writing the final report Tasks Set This Week e The final report of the project is constantly updated and in a very good level e Anextension was given to the final submission of the final year project by the office of school of computing The new date of submission is Wednesday 21 May 2003 at 12 30pm e Approximately according the progress has been made the project will be ready for submission on Monday 19 of May Forward look to tasks for next stage e Finishing of the final report 148 Linux Device Drivers Progress Report of Level 3 Project 9 16 May 2003 Review Tasks Set Last Week e Finishing the final report Tasks Set This Week e Inform the supervisor that the final repost has finished e Submission of the final report Forward look to tasks for next stage e None 149 Objectives Settings Report FINAL YEAR DEGREE PROJECT Objective Settings Profo
127. urn 0 static int write card struct file filp const char ptr size t count loff t f pos int address base port int buff ptr outb buff address write to device return 0 static int read card struct file filp char ptr size t count loff t f pos ansigned char address base port 4 Jo eX Re KR int n count while n printk lt 1 gt READ Nn ptrt inb 0x200 get data from device printk lt 1 gt READEN d from device WMn ptr return int count 56 Appendix 4 MS DOS application source code final version OR KKK IK IK kCk kk CK kkk kk kk kk k kk k kkk kk kk kk kk ko kk ko kk kk kk This demo program shows how to use the PCL 731 s readback function to monitor the output status 1 Base address set at 0x200 kk ck ck ck ck ck ck ck ck ck ck ck ck kk kk kk Ck Ck Ck KKK KKK KKK KKK KK ko ko ko ck k ck kc ko kck kk ox ok X X X Hardware setting finclude lt stdio h gt finclude lt conio h gt finclude lt process h gt finclude dos h main int base 0x200 set base address to 200 hex int portA save readback value of port_Al int portB save readback value of port B1 int portC save readback value of port C1 DE ux handle screen clrscr gotoxy 30 3 textattr 0x70 cputs PCL 731 DEMO PROGRAM gotoxy 11 6 printf PortAO output value gt gotoxy 11
128. ut Output card whose specifications will follow The exact model code of the card is PCL 731 which is a 48 bit Digital Input Output DIO card This means that it is equipped with 48 pins which can be used as inputs or outputs to and from the card Several configurations can be made in order to set some pins as inputs and some others as outputs In addition a 8250 family microcontroller is attached to the card and is responsible for the algorithm of the digital signal processing of the data coming into and going out of the card This microcontroller is fully programmable and can change completely the functionality of Interrupt Request Line Plug and Play T the card Still the two IRQ s of the card as well the trigger level of the them can be manually selected with the use of two jumpers Furthermore the card has two 50 pin OPTO 22 compatible connectors where the 48 input outputs are situated divided into six 8 bit ports UO ports along with some registers A 50 way IDC connector may be easily connected to them and then transfer the data into an IDC cable connected to an external device Also the card is equipped with 48 LED s corresponding to the 48 I O pins showing the high or low logic for each pin The logic High voltage for each input varies between 2 0 and 5 25V and the logic Low voltage for the previous is above 0 0 and below 0 8V The logic High output signal is 2 4V and the logic Low of the same output
129. x 1 byte ports Each one of them is assigned to a port address always in respect to the base address of the card which can be in the range from 0x0 to 0x3F8 Below a table of all the I O ports and their corresponding address is shown Address Port Base 0 AO Base 1 BO Base 2 CU Base 3 CFG REG Base 4 Al Base 5 Bl Base 6 C1 Base 7 CFG REG Reference a5 all a12 a13 a14 Light Emitting Diodes 29 How a module works In the following diagram we can see how a module is linked to the kernel and how it manipulates requests from the user space program or the device through the kernel in order to perform the requested operations User space application Module 1 Module 2 Module for T O ISA card Device nen fo Module n Official Linux Kernel Once we have developed a module we must link it to the kernel Using the insmod command and the name of the ELF file we can load our module to the Kernel e g insmod isa731 0 Typing the 1smod command we can see if the module was successfully loaded or we can use the dmesg command to see what messages the module sends to the kernel When a module is linked to the kernel it registers a range of ports and a major number This range of ports is going to be accessed only by our module and that s where the device is connected The major number is a unique number in order for the kernel to identify the spec
130. ystem we must know the major number of the device assign during the load of the module In case we had used the third option let the kernel decide the major number we should look into the proc devices file in order to find the major number Instead the manual allocation of the major number was used and the only thing we should be aware of is that this number is not in the usr src linux Documentation devices txt and is not already registered to the kernel The chosen number to be used as major number for the ISA card is 42 Reference b3 b5 Device name A unique name should be invented in order to associate it with the device This would be the modules name as appeared at the output of the command Ismod and the node s name appeared under dev The author of this project chooses the name isa731 as a device name Reference b3 b5 Usage Count Usage count is a variable that we change each time we using the device This is for the reason that we don t want no other module to access the device when we are using it Using the functions MOD INC USE COUNT and MOD DEC USE COUNT we can increase and decrease the usage count This operation takes place when we use the file operations open and release When we open the device we must increase the usage count and when we release the device we must decrease the usage count Reference b3 b5 How can we create a node 86 As we already mention a node is a file that represents a device i

Download Pdf Manuals

image

Related Search

Related Contents

DESCRIPCION: USOS: MODO DE EMPLEO: TIEMPO  - Index of  SUDX manual  Bompani BO243ZL/E  全ページダウンロードはこちら  Cadenas pliable en acier Mode d`emploi  Progress Lighting P3938-104 Installation Guide  Niles Audio CABLEKIT LZR800 User's Manual  1 - Sony  K-Tech News Vol.5 No.2  

Copyright © All rights reserved.
Failed to retrieve file