linux driver overview

Upload: nguyenvan-thanh

Post on 30-May-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Linux Driver Overview

    1/22

    Linux Driver OverviewLinux Driver Overview

    ::

    http://www.ntut.edu.tw/~s4599001http://www.ntut.edu.tw/~s4599001

  • 8/9/2019 Linux Driver Overview

    2/22

    Operating System LinuxOperating System Linux Operating systemOperating system

    Kernel space/User spaceKernel space/User space MMUMMU mutex/semaphore/interruptmutex/semaphore/interrupt

    Task structureTask structure ptrace/mmu/signal/task relation /process idptrace/mmu/signal/task relation /process id file/timer/semaphores/miscfile/timer/semaphores/misc

    Main algorithmMain algorithm Signal handingSignal handing InterruptInterrupt

    TaskletsTasklets Bottom havesBottom haves

    Software interruptSoftware interruptmax 32max 32 Void open_sorftirq(int nr, void (*action) (structure softirq_action *, void *data)Void open_sorftirq(int nr, void (*action) (structure softirq_action *, void *data) Raise_softirq()Raise_softirq()

    SchedulerScheduler SCHED_FIFO/SCHED_RR/SCHED_OTHERSCHED_FIFO/SCHED_RR/SCHED_OTHER

  • 8/9/2019 Linux Driver Overview

    3/22

    MutexMutex

    DefinitionDefinition In computer programming, a mutex is aIn computer programming, a mutex is a

    program object that allows multiple programprogram object that allows multiple programthreads to share the same resource, such asthreads to share the same resource, such asfile access, but not simultaneously.file access, but not simultaneously.

    Andrea Steinbach and Angelika Schofer,Andrea Steinbach and Angelika Schofer,as a master's thesis at Rheinischeas a master's thesis at RheinischeFriedrichFriedrich--Wilhelms University.Wilhelms University.

  • 8/9/2019 Linux Driver Overview

    4/22

    SemaphoreSemaphore

    DefinitionDefinition A semaphore is a protected variable (or abstract dataA semaphore is a protected variable (or abstract data

    type) and constitutes the classic method fortype) and constitutes the classic method forrestricting access to shared resources (e.g. storage)restricting access to shared resources (e.g. storage)in a multiprogramming environment.in a multiprogramming environment.

    SampleSample Dining philosophers problemDining philosophers problem

    do not prevent all deadlocksdo not prevent all deadlocks

    invented by Edsger Dijkstra and first used in theinvented by Edsger Dijkstra and first used in theTHE operating systemTHE operating system

  • 8/9/2019 Linux Driver Overview

    5/22

    Semaphore ProgrammingSemaphore Programming

    P(Semaphore s)P(Semaphore s){{

    await s > 0 then s = sawait s > 0 then s = s--1; /* must be atomic once s > 0 is detected */1; /* must be atomic once s > 0 is detected */}}

    V(Semaphore s)V(Semaphore s){{

    s = s+1; /* must be atomic */s = s+1; /* must be atomic */}}Init(Semaphore s, Integer v)Init(Semaphore s, Integer v){{

    s = v;s = v;}}

  • 8/9/2019 Linux Driver Overview

    6/22

  • 8/9/2019 Linux Driver Overview

    7/22

    Driver implementation in LinuxDriver implementation in Linux

    Driver specificationsDriver specifications Char driversChar drivers

    ConsoleConsole Serial portSerial port

    Block driversBlock drivers

    DMA transferDMA transfer

    Network interfacesNetwork interfaces

    IO port / IO memoryIO port / IO memory

  • 8/9/2019 Linux Driver Overview

    8/22

    Driver implementation in LinuxDriver implementation in Linux

    InterruptInterrupt int request_irq(unsigned int irq,voidint request_irq(unsigned int irq,void

    (*handler)(int, void *, struct pt_regs(*handler)(int, void *, struct pt_regs*),unsigned long flags,const char*),unsigned long flags,const char*dev_name,void *dev_id);*dev_name,void *dev_id);

    void free_irq(unsigned int irq, void *dev_id);void free_irq(unsigned int irq, void *dev_id);

  • 8/9/2019 Linux Driver Overview

    9/22

    Writing Linux Device DriverWriting Linux Device Driver

    Header filesHeader files

    #include #include

    #include #include

    #include #include

    #include #include

    #include #include

    #include #include

    #include #include

  • 8/9/2019 Linux Driver Overview

    10/22

    Writing Linux Device DriverWriting Linux Device Driver

    Initialize register (clear)Initialize register (clear)static int test_open(struct inode *inodePtr, struct file *fp)static int test_open(struct inode *inodePtr, struct file *fp)

    {{

    printk( "Enter open routine.printk( "Enter open routine.\\n" );n" );

    return 0;return 0;}}

    static int test_release(struct inode *inodePtr, struct file *fp)static int test_release(struct inode *inodePtr, struct file *fp)

    {{

    MOD_DEC_USE_COUNT;MOD_DEC_USE_COUNT;printk( "Enter test_releaseprintk( "Enter test_release\\n" );n" );

    return 0;return 0;

    }}

  • 8/9/2019 Linux Driver Overview

    11/22

    Writing Linux Device DriverWriting Linux Device Driver

    OperationOperationstatic ssize_t test_read(struct file *fp, char *buf, size_t len, loff_tstatic ssize_t test_read(struct file *fp, char *buf, size_t len, loff_t

    *offset)*offset){{

    printk( "Enter test_readprintk( "Enter test_read\\n" );n" );return len;return len;

    }}

    static ssize_t test_write(struct file *fp, char *buf, size_t len, loff_tstatic ssize_t test_write(struct file *fp, char *buf, size_t len, loff_t*offset)*offset)

    {{ printk( "Enter test_writeprintk( "Enter test_write\\n" );n" );return len;return len;

    }}

  • 8/9/2019 Linux Driver Overview

    12/22

    Writing Linux Device DriverWriting Linux Device DriverMappingMapping

    structstructfile_operationsfile_operations test_fops =test_fops =

    {{

    read:read: test_read,test_read,

    write:write: test_write,test_write,

    open:open: test_open,test_open,

    release:release: test_release,test_release,ioctl:ioctl: test_ioctl,test_ioctl,

    };};

  • 8/9/2019 Linux Driver Overview

    13/22

    Writing Linux Device DriverWriting Linux Device Driver

    static int test_ioctl(struct inode *inodePtr, structstatic int test_ioctl(struct inode *inodePtr, structfile *fp, unsignedfile *fp, unsigned int cmd, unsigned long argint cmd, unsigned long arg))

    {{switch( cmd ){switch( cmd ){

    default:default:break;break;

    }}return 0;return 0;

    }}

  • 8/9/2019 Linux Driver Overview

    14/22

    Writing Linux Device DriverWriting Linux Device Driver

    Entry point V.S. Exit pointEntry point V.S. Exit pointintinttest_init_moduletest_init_module(void)(void){{

    register_chrdevregister_chrdev(( 100100, testName, &test_fops );, testName, &test_fops );

    printk( "Init test moduleprintk( "Init test module\\n" );n" );

    return 0;return 0;}}

    voidvoid test_cleanup_moduletest_cleanup_module(void)(void){{

    unregister_chrdevunregister_chrdev(( 100100, testName );, testName );

    printk( "UnInit testprintk( "UnInit test\\n" );n" );

    }}

    module_initmodule_init( test_init_module );( test_init_module );module_exitmodule_exit( test_cleanup_module );( test_cleanup_module );

    Entry point

    Exit point

  • 8/9/2019 Linux Driver Overview

    15/22

    Create EnvironmentCreate Environment

    CompilerCompiler gccgcc --D__KERNEL__D__KERNEL__ --DMODULEDMODULE --I/usr/src/linuxI/usr/src/linux--2.4.22/include2.4.22/include --cc --OO --Wall drv.cWall drv.c --o drv.oo drv.o

    Make nodeMake node

    mknod /dev/test cmknod /dev/test c 100100 00

    Check nodeCheck nodecrwcrw--rwrw--rwrw-- 1 root dialout 4, 64 Jun 30 11:19 test1 root dialout 4, 64 Jun 30 11:19 test

    crwcrw--rwrw--rwrw-- 1 root dialout 4, 65 Aug 16 00:00 ttyS11 root dialout 4, 65 Aug 16 00:00 ttyS1

  • 8/9/2019 Linux Driver Overview

    16/22

    Writing ApplicationWriting Application

    int main(int argc, char *argv[])int main(int argc, char *argv[]){{

    int fd;int fd;char buf[256] = "Hello World!!";char buf[256] = "Hello World!!";

    fd = open(fd = open( "/dev/test"/dev/test, O_RDWR );, O_RDWR );if ( fd ==if ( fd == --1 )1 ){{

    printf( "Open drv driver fail.[Application]printf( "Open drv driver fail.[Application]\\nn ););returnreturn --1;1;

    }}write( fd, buf, 6 );write( fd, buf, 6 );close( fd );close( fd );return 0;return 0;

    }}

  • 8/9/2019 Linux Driver Overview

    17/22

    Implementation in Embedded LinuxImplementation in Embedded Linux

    NOMMU (arm as example)NOMMU (arm as example) Uclinux(ww.uclinux.org)Uclinux(ww.uclinux.org)

    MMUMMU http://www.arm.linux.org.uk/http://www.arm.linux.org.uk/

    Other versionOther version http://www.linux.org/http://www.linux.org/

  • 8/9/2019 Linux Driver Overview

    18/22

    Implementation in Embedded LinuxImplementation in Embedded Linux

    The driver in embedded linuxThe driver in embedded linux Most implement in setting registersMost implement in setting registers

    The different in mmu/nommuThe different in mmu/nommu mmummu

    The same except less supportThe same except less support

    nommunommu Almost no difference about address in user space /kernelAlmost no difference about address in user space /kernel

    sapcesapce

  • 8/9/2019 Linux Driver Overview

    19/22

    Recommended Books && web sitesRecommended Books && web sites

    Linux Document ProjectLinux Document Project http://www.tldp.orghttp://www.tldp.org

    AdvacendAdvacend linuxlinux programmingprogramming http://www.advancedlinuxprogramming.com/alphttp://www.advancedlinuxprogramming.com/alp--folderfolder

    Linux device driversLinux device drivers http://www.xml.com/ldd/chapter/book/http://www.xml.com/ldd/chapter/book/

    LINUXLINUX

    Linux kernel programming M BeckLinux kernel programming M Beck Understanding the LINUX KERNELUnderstanding the LINUX KERNEL BovetBovet && CesatiCesati

    Linux server hacksLinux server hacks OthersOthersIn the diskIn the disk

  • 8/9/2019 Linux Driver Overview

    20/22

    Introduce to source navigatorIntroduce to source navigator

    http://sourcenav.sourceforge.net/http://sourcenav.sourceforge.net/

  • 8/9/2019 Linux Driver Overview

    21/22

    Q&A

  • 8/9/2019 Linux Driver Overview

    22/22

    Define in Define in

    struct file_operations {struct file_operations {

    struct module *owner;struct module *owner;

    ssize_t (*read) (struct file *, char *, size_t, loff_t *);ssize_t (*read) (struct file *, char *, size_t, loff_t *);

    ssize_t (*write) (struct file *, const char *, size_t, loff_t *);ssize_t (*write) (struct file *, const char *, size_t, loff_t *);

    int (*ioctl) (struct inode *, struct file *, unsigned int,int (*ioctl) (struct inode *, struct file *, unsigned int,unsigned long);unsigned long);

    int (*open) (struct inode *, struct file *);int (*open) (struct inode *, struct file *);

    int (*release) (struct inode *, struct file *);int (*release) (struct inode *, struct file *);};};