1 / 22

Linux Driver Overview

Linux Driver Overview. 報告人 : 陸述人 http://www.ntut.edu.tw/~s4599001. Operating System Linux. Operating system Kernel space/User space MMU mutex/semaphore/interrupt Task structure ptrace/mmu/signal/task relation /process id file/timer/semaphores/misc Main algorithm Signal handing

janna
Download Presentation

Linux Driver Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux Driver Overview 報告人: 陸述人 http://www.ntut.edu.tw/~s4599001

  2. Operating System Linux • Operating system • Kernel space/User space • MMU • mutex/semaphore/interrupt • Task structure • ptrace/mmu/signal/task relation /process id • file/timer/semaphores/misc • Main algorithm • Signal handing • Interrupt • Tasklets • Bottom haves • Software interrupt –max 32 • Void open_sorftirq(int nr, void (*action) (structure softirq_action *, void *data) • Raise_softirq() • Scheduler • SCHED_FIFO/SCHED_RR/SCHED_OTHER

  3. Mutex • Definition • In computer programming, a mutex is a program object that allows multiple program threads to share the same resource, such as file access, but not simultaneously. • Andrea Steinbach and Angelika Schofer, as a master's thesis at Rheinische Friedrich-Wilhelms University.

  4. Semaphore • Definition • A semaphore is a protected variable (or abstract data type) and constitutes the classic method for restricting access to shared resources (e.g. storage) in a multiprogramming environment. • Sample • Dining philosophers problem • do not prevent all deadlocks • invented by Edsger Dijkstra and first used in the THE operating system

  5. Semaphore Programming P(Semaphore s) { await s > 0 then s = s-1; /* must be atomic once s > 0 is detected */ } V(Semaphore s) { s = s+1; /* must be atomic */ } Init(Semaphore s, Integer v) { s = v; }

  6. Operating System in Linux -3 • Driver: • Modprobe/Insmod/rmmod/lsmod • Built in kernel

  7. Driver implementation in Linux • Driver specifications • Char drivers • Console • Serial port • Block drivers • DMA transfer • Network interfaces • IO port / IO memory

  8. Driver implementation in Linux • Interrupt • int request_irq(unsigned int irq,void (*handler)(int, void *, struct pt_regs *),unsigned long flags,const char *dev_name,void *dev_id); • void free_irq(unsigned int irq, void *dev_id);

  9. Writing Linux Device DriverHeader files #include <linux/kernel.h> #include <linux/module.h> #include <linux/version.h> #include <linux/config.h> #include <linux/init.h> #include <linux/types.h> #include <linux/fs.h>

  10. Writing Linux Device DriverInitialize register (clear) static int test_open(struct inode *inodePtr, struct file *fp) { printk( "Enter open routine.\n" ); return 0; } static int test_release(struct inode *inodePtr, struct file *fp) { MOD_DEC_USE_COUNT; printk( "Enter test_release\n" ); return 0; }

  11. Writing Linux Device DriverOperation static ssize_t test_read(struct file *fp, char *buf, size_t len, loff_t *offset) { printk( "Enter test_read\n" ); return len; } static ssize_t test_write(struct file *fp, char *buf, size_t len, loff_t *offset) { printk( "Enter test_write\n" ); return len; }

  12. Writing Linux Device DriverMapping struct file_operations test_fops = { read: test_read, write: test_write, open: test_open, release: test_release, ioctl: test_ioctl, };

  13. Writing Linux Device Driver static int test_ioctl(struct inode *inodePtr, struct file *fp, unsigned int cmd, unsigned long arg) { switch( cmd ){ default: break; } return 0; }

  14. Entry point Exit point Writing Linux Device DriverEntry point V.S. Exit point int test_init_module(void) { register_chrdev( 100, testName, &test_fops ); printk( "Init test module\n" ); return 0; } void test_cleanup_module(void) { unregister_chrdev( 100, testName ); printk( "UnInit test\n" ); } module_init ( test_init_module ); module_exit ( test_cleanup_module );

  15. Create Environment • Compiler • gcc -D__KERNEL__ -DMODULE -I/usr/src/linux-2.4.22/include -c -O -Wall drv.c -o drv.o • Make node • mknod /dev/test c 100 0 • Check node crw-rw-rw- 1 root dialout 4, 64 Jun 30 11:19 test crw-rw-rw- 1 root dialout 4, 65 Aug 16 00:00 ttyS1

  16. Writing Application int main(int argc, char *argv[]) { int fd; char buf[256] = "Hello World!!"; fd = open( "/dev/test”, O_RDWR ); if ( fd == -1 ) { printf( "Open drv driver fail.[Application]\n“ ); return -1; } write( fd, buf, 6 ); close( fd ); return 0; }

  17. Implementation in Embedded Linux • NOMMU (arm as example) • Uclinux(ww.uclinux.org) • MMU • http://www.arm.linux.org.uk/ • Other version • http://www.linux.org/

  18. Implementation in Embedded Linux • The driver in embedded linux • Most implement in setting registers • The different in mmu/nommu • mmu • The same except less support • nommu • Almost no difference about address in user space /kernel sapce

  19. Recommended Books && web sites • Linux Document Project • http://www.tldp.org • Advacend linux programming • http://www.advancedlinuxprogramming.com/alp-folder • Linux device drivers • http://www.xml.com/ldd/chapter/book/ • LINUX 核心程式設計 • Linux kernel programming M Beck • Understanding the LINUX KERNEL • Bovet & Cesati • Linux server hacks 駭客一百招 • Others—In the disk

  20. Introduce to source navigator • http://sourcenav.sourceforge.net/

  21. Q&A

  22. Define in <linux/kernel.h> struct file_operations { struct module *owner; ssize_t (*read) (struct file *, char *, size_t, loff_t *); ssize_t (*write) (struct file *, const char *, size_t, loff_t *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*open) (struct inode *, struct file *); int (*release) (struct inode *, struct file *); };

More Related