untitled

Similar documents
Embeddedsystem(8).PDF

untitled

슬라이드 1

K&R2 Reference Manual 번역본

PowerPoint 프레젠테이션

KEY 디바이스 드라이버

교육지원 IT시스템 선진화

Microsoft PowerPoint - lab14.pptx

PowerPoint 프레젠테이션

Chapter #01 Subject

휠세미나3 ver0.4

망고100 보드로 놀아보자 -13


PowerPoint 프레젠테이션

(Microsoft PowerPoint - Device Driver [\310\243\310\257 \270\360\265\345])

(Microsoft PowerPoint - Device Driver [\310\243\310\257 \270\360\265\345])

PowerPoint 프레젠테이션

Sena Technologies, Inc. HelloDevice Super 1.1.0

슬라이드 제목 없음

프로그램을 학교 등지에서 조금이라도 배운 사람들을 위한 프로그래밍 노트 입니다. 저 역시 그 사람들 중 하나 입니다. 중고등학교 시절 학교 도서관, 새로 생긴 시립 도서관 등을 다니며 책을 보 고 정리하며 어느정도 독학으르 공부하긴 했지만, 자주 안하다 보면 금방 잊어

10.

PowerPoint 프레젠테이션

슬라이드 1

Microsoft Word doc

06Àå

Adding a New Dev file

교육지원 IT시스템 선진화

PowerPoint 프레젠테이션

<4D F736F F F696E74202D205BBAB0C3B75D20B8AEB4AABDBA20B5F0B9D9C0CCBDBA20B5E5B6F3C0CCB9F620B8F0B5A82E >

1217 WebTrafMon II

(72) 발명자 이동희 서울 동작구 여의대방로44길 10, 101동 802호 (대 방동, 대림아파트) 노삼혁 서울 중구 정동길 21-31, B동 404호 (정동, 정동상 림원) 이 발명을 지원한 국가연구개발사업 과제고유번호 부처명 교육과학기술부

Microsoft PowerPoint - lab15.pptx

PowerPoint 프레젠테이션

¼ÒÇÁÆ®-12È£-ÃÖÁ¾¿Ï¼º

Microsoft Word doc

PowerPoint 프레젠테이션

K7VT2_QIG_v3

Microsoft Word - MPC850 SPI Driver.doc

PowerPoint 프레젠테이션

Microsoft PowerPoint - Chapter_04.pptx

Chap 6 모듈프로그래밍및 디바이스드라이버구현

PowerPoint 프레젠테이션

PCServerMgmt7

Chap06(Interprocess Communication).PDF

chap7.key

본 강의에 들어가기 전

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

13주-14주proc.PDF

ECE30076 Embedded System Programming - LED Device Driver

Microsoft PowerPoint - 알고리즘_1주차_2차시.pptx

03장.스택.key

Mango-AM335x LCD Type 커널 Module Parameter에서 변경하기

강의10

(Asynchronous Mode) ( 1, 5~8, 1~2) & (Parity) 1 ; * S erial Port (BIOS INT 14H) - 1 -

<C0CCBCBCBFB52DC1A4B4EBBFF82DBCAEBBE7B3EDB9AE2D D382E687770>

chap10.PDF

목차 1. 제품 소개 특징 개요 Function table 기능 소개 Copy Compare Copy & Compare Erase

untitled

Mango220 Android How to compile and Transfer image to Target

임베디드시스템설계강의자료 6 system call 2/2 (2014 년도 1 학기 ) 김영진 아주대학교전자공학과

디바이스드라이버 (Device Driver) Driver is literally a subject which drive a object. 응용프로그램에서하드웨어장치를이용해서데이터를직접읽고쓰거나제어해야하는경우에디바이스드라이버를이용 하드웨어를제어하는프로그램과애플리케이션에서

Assign an IP Address and Access the Video Stream - Installation Guide

PowerPoint 프레젠테이션

[8051] 강의자료.PDF


DE1-SoC Board

6주차.key

UI TASK & KEY EVENT

GNU/Linux 1, GNU/Linux MS-DOS LOADLIN DOS-MBR LILO DOS-MBR LILO... 6

Microsoft PowerPoint - IOControl [호환 모드]

Microsoft PowerPoint APUE(Intro).ppt

Copyright 2004 Sun Microsystems, Inc Network Circle, Santa Clara, CA U.S.A..,,. Sun. Sun. Berkeley BSD. UNIX X/Open Company, Ltd.. Sun, Su

untitled

<4D F736F F F696E74202D20B8AEB4AABDBA20BFC0B7F920C3B3B8AEC7CFB1E22E BC8A3C8AF20B8F0B5E55D>

vi 사용법

PowerPoint 프레젠테이션

MySQL-Ch10

LXR 설치 및 사용법.doc

다음 사항을 꼭 확인하세요! 도움말 안내 - 본 도움말에는 iodd2511 조작방법 및 활용법이 적혀 있습니다. - 본 제품 사용 전에 안전을 위한 주의사항 을 반드시 숙지하십시오. - 문제가 발생하면 문제해결 을 참조하십시오. 중요한 Data 는 항상 백업 하십시오.

CD-RW_Advanced.PDF

Microsoft PowerPoint - 10-EmbedSW-11-모듈

untitled

메시지큐를이용한 IPC 프로그램구현과제보고서 1. 과제의목적 1 리눅스가지원하는프로세스간통신방식중다수의프로세스사이에구조화된데이터블럭, 즉메시지를전달하는데주로사용되는메시지큐방식에대하여무엇인지, 어떻게사용하는지공부한다. 2 공부한내용을점검하기위해기작성된 epda 프로세스관

<4D F736F F F696E74202D20B8B6C0CCC5A9B7CEC7C1B7CEBCBCBCAD202839C1D6C2F7207E203135C1D6C2F >

04디바이스드라이버

리눅스커널-06

Microsoft Word doc

[ 융합과학 ] 과학고 R&E 결과보고서 뇌파를이용한곤충제어 연구기간 : ~ 연구책임자 : 최홍수 ( 대구경북과학기술원 ) 지도교사 : 박경희 ( 부산일과학고 ) 참여학생 : 김남호 ( 부산일과학고 ) 안진웅 ( 부산일과학고 )

C++-¿Ïº®Çؼ³10Àå

T100MD+

untitled

Microsoft PowerPoint - polling.pptx

리뉴얼 xtremI 최종 softcopy

Microsoft PowerPoint - chap6 [호환 모드]

SRC PLUS 제어기 MANUAL

5.스택(강의자료).key

10주차.key

Microsoft PowerPoint - 알고리즘_2주차_1차시.pptx

Chapter 4. LISTS

Transcription:

Block Device Driver in Linux Embedded System Lab. II

Embedded System Lab. II 2

Define objective of applications using device Study Hardware manual (Registers) (vector number) Understand interface to related utilities Device setup Device ioctl command Design to adapt to future changes Module Embedded System Lab. II 3

Linux Device, access. File operation Major number Minor number Device Driver Embedded System Lab. II 4

Application area Application System Call Interface VFS Kernel area Buffer Cache Network Subsystem Char Device Driver Block D/D Network D/D Device Interface Hardware Hardware Embedded System Lab. II 5

Block Device( ) Block device random access mount, Ram Disk, CD-ROM Block device fd* : Floppy disk Hd* : Hard disk sda : SCSI disk brw------- brw-rw---- brw-rw---- brw-rw---- brw-rw---- 1 root floppy 2, 0 May 6 1998 fd0 1 root disk 3, 0 May 6 1998 hda 1 root disk 3, 1 May 6 1998 hda1 1 root disk 8, 0 May 6 1998 sda 1 root disk 8, 1 May 6 1998 sda1 Embedded System Lab. II 6

Block device Block Device Driver Major number - include/linux/major.h 255 : MAX_BLKDEV blkdevs[ ] fs/block_dev.c index block device driver major number driver ~/include/linux/fs.h static struct { const char *name; struct block_device_operations *bdops; } blkdevs[max_blkdev]; /*MAX_BLKDEV = 255*/ Embedded System Lab. II 7

Block device ~/include/linux/fs.h int register_blkdev(unsigned int major, const char *name, struct block_device_operations *bdops); { if (major == 0){ for (major = MAX_BLKDEV-1; major > 0; major--) { if (blkdevs[major].bdops == NULL) { blkdevs[major].name = name; blkdevs[major].bdops = bdops; return major; } } return EBUSY; } if (major >= MAX_BLKDEV) return EINVAL; if (blkdevs[major].bdops && blkdevs[major].bdops!= bdops) return EBUSY; blkdevs[major].name = name; blkdevs[major].bdops = bdops; return 0; } Embedded System Lab. II 8

Block device int unregister_blkdev(unsigned int major, const char *name); { if (major >= MAX_BLKDEV) return EINVAL; if (!blkdevs[major].bdops) return EINVAL; if ( strcmp(blkdevs[major].name, name)) return EINVAL; blkdevs[major].name = NULL; blkdevs[major].bdops = NULL; return 0; } Embedded System Lab. II 9

(1) Block device struct block_device_operations = { int (*open) (struct inode*, struct file*);/*open*/ int (*release) (struct inode*, struct file*, unsigned, unsigned long);/*close*/ int (*ioctl) (struct inode*, struct file*, unsigned, unsigned long);/*i/o control*/ int (*check_media_change) (kdev_t);/* */ int (*revalidate) (kdev_t); /* */ }; Embedded System Lab. II 10

(2) struct file_operations def_blk_fops= { open : blkdev_open, /* */ release : blkdev_close, /* */ llseek : block_llseek, /* */ read : block_read, /* */ write : block_write, /* */ fsync : block_fsync, /* */ ioctl : blkdev_ioctl, /* */ }; Embedded System Lab. II 11

/ blk_dev[] fops blkdevs[] Embedded System Lab. II 12

IDE (1) / (,, ) ( ) ( ) Embedded System Lab. II 13

IDE (2) IDE hd_init() Buffer Cache hd_open( ) hd_release( ) hd_request( ) hd_ioctl( ) hd_interrupt( ) hd_out( ) check_status( ) NULL, block_read, block_write NULL, NULL, hd_ioctl, NULL, hd_open, NULL hd_release, block_fsync struct file_operations hd_ops /* driver/block/hd.c */ Embedded System Lab. II 14

IDE (3) init_process init_module /* driver/block/hd.c */ hd_init() struct file_operations hd_ops /* driver/block/hd.c */ NULL, block_read, block_write NULL, NULL, hd_ioctl, NULL, hd_open, NULL hd_release, block_fsync - register_blkdev(hd_major, hd, &hd_fops); /* fs/devices.c */ register_blkdev() /* include/linux/major.h */ - blkdevs[major].name = device name - blkdevs[major].fops = fops Embedded System Lab. II 15

IDE (4) open /* fs/open.c */ /* fs/namei.c */ open_namei( ) sys_open( ) /* fs/open.c */ filp_open( ) - get_unused_fd( ) - fd_install(fd, f) - struct file initialize - f->f_op->open( ) blkdev_open( ) /* fs/device.c */ - filp->f_op = get_blkfops(major(inode->i_rdev)); /* filp->f_op = blkdevs[major].fops */ - filp->f_op->open; /* hd_open */ Embedded System Lab. II 16

IDE (5) register_chrdev() register_blkdev() blkdevs, / hd_request() (buffer cache) / block_read(), block_write(), Linux. Embedded System Lab. II 17

IDE (6) read /* fs/read_write.c */ sys_read() System call handling layer sock_read() - f->f_op->read hd_read() VFS layer pipe_read() /* mm/filemap.c */ tty_read() Specific File layer generic_file_read() - try to find page in cache, if (hit) OK. - inode->i_op->readpage() Specific FS layer Embedded System Lab. II 18

IDE (7) /* include/linux/blkdev.h */ bread block_read struct blk_dev_struct { request_fn; queue; request;... } blk_dev[]; request_fn struct request { rq_status rq_dev cmd /* R/W */ error sector, nr_sector buffer, bh sem next... } buffer cache ll_rw_block make_request req queue req req request hd_request block device driver hd_out Embedded System Lab. II 19

,, major number read/write read/write read/write (request) Request, read/write.,, Embedded System Lab. II 20

(2) extern int devfs_register_blkdev(unsigned int major, const char name, struct block device operation *bdops) major 0 extern int devfs_unregister_blkdev(unsigned int major, const char *name) read/write/fsync, block_read(), block_write(), block_fsync() open, release ioctl ioctl, Embedded System Lab. II 21

Mini RAMDISK RAM Read/write read/write request,, load RAK Disk Request read/write. - Site : http://www.linuxkernel.net/moduleprog/lkp/ Embedded System Lab. II 22

(1) RAM (minird.c) #include <linux/kernel.h> #include <linux/module.h> #ifdef CONFIG_MODVERSIONS #define MODVERSIONS #include <linux/modversions.h> #endif #include <linux/fs.h> #include <linux/vmalloc.h> #include <asm/uaccess.h> #include <linux/devfs_fs_kernel.h> #define MINIRD_MAJOR 0 /* major number : dynamic allocation */ #define DEVICE_NAME "MiniRD /* device name */ #define DEVICE_FILENAME "minird" #define DEVICE_NUM 2 /* minor device number */ #define MINIRD_DEF_SIZE 1024 /* default device size in KB */ #define MINIRD_BLOCKSIZE 1024 /* sizeof block */ #define MINIRD_SECTSIZE 512 /* sizeof sector */ Embedded System Lab. II 23

(2) extern int s_nmajor; #define MAJOR_NR (s_nmajor) #define DEVICE_NR(device) (MINOR(device)) #define DEVICE_ON(device) /* do nothing */ #define DEVICE_OFF(device) /* do nothing */ #define DEVICE_NO_RANDOM #include <linux/blk.h> #include <linux/blkpg.h> /* Globak Variables */ static int s_nmajor = 0; static int minird_size = MINIRD_DEF_SIZE; static int s_kbsize[device_num]; /* size in blocks of 1024 bytes */ static int s_blocksize[device_num]; /* size of 1024 byte block */ static int s_hardsect[device_num]; /* sizeof real block in bytes */ static int s_length[device_num]; /* size of disks in bytes */ static char *s_data[device_num]; static devfs_handle_t devfs_handle; MODULE_PARM(minird_size, "i"); MODULE_PARM_DESC(minird_size, "RAM Disk size in KB"); Embedded System Lab. II 24

(3) /* Function Prototypes */ static int minird_open(struct inode *inodep, struct file *filp); static int minird_release(struct inode *inodep, struct file *filp); static int minird_ioctl(struct inode *inodep, struct file *filp, unsigned int cmd, unsigned long arg); static int minird_make_request(request_queue_t *q, int rw, struct buffer_head *sbh); /* Device Operations */ static struct block_device_operations minird_fops = { open : minird_open, release : minird_release, ioctl : minird_ioctl, }; /* Module startup/cleanup */ int init_module(void) { int i, j; printk("loading Mini RAM Disk Module\n"); for (i = 0; i < DEVICE_NUM; ++i) { s_kbsize[i] = minird_size; Embedded System Lab. II 25

(4) s_blocksize[i] = MINIRD_BLOCKSIZE; s_hardsect[i] = MINIRD_SECTSIZE; s_length[i] = (minird_size << BLOCK_SIZE_BITS); } if ((s_data[i] = vmalloc(s_length[i])) == NULL) { for (j = 0; j < i; ++j) vfree(s_data[j]); return -ENOMEM; } if ((s_nmajor = devfs_register_blkdev(minird_major, DEVICE_NAME, &minird_fops)) < 0) { printk(device_name " : Device registration failed (%d)\n", s_nmajor); return s_nmajor; } printk(device_name " : Device registered with Major Number = %d\n", MAJOR_NR); devfs_handle = devfs_mk_dir(null, DEVICE_FILENAME, NULL); devfs_register_series(devfs_handle, "%u", DEVICE_NUM, DEVFS_FL_DEFAULT, s_nmajor, 0, S_IFBLK S_IRUGO S_IWUGO, &minird_fops, NULL); blk_queue_make_request(blk_default_queue(major_nr), &minird_make_request); Embedded System Lab. II 26

(5) blk_size[major_nr] = s_kbsize; blksize_size[major_nr] = s_blocksize; hardsect_size[major_nr] = s_hardsect; } return 0; void cleanup_module(void) { int i; printk("unloading Mini RAM Disk Module\n"); for (i = 0; i < DEVICE_NUM; ++i) destroy_buffers(mkdev(major_nr, i)); /* flush the devices */ for (i = 0; i < DEVICE_NUM; ++i) vfree(s_data[i]); devfs_unregister(devfs_handle); devfs_unregister_blkdev(major_nr, DEVICE_NAME); } blk_size[major_nr] = NULL; blksize_size[major_nr] = NULL; hardsect_size[major_nr] = NULL; Embedded System Lab. II 27

(6) /* Device Operations */ int minird_open(struct inode *inodep, struct file *filp) { if (DEVICE_NR(inodep->i_rdev) >= DEVICE_NUM) return -ENXIO; MOD_INC_USE_COUNT; } return 0; int minird_release(struct inode *inodep, struct file *filp) { MOD_DEC_USE_COUNT; return 0; } int minird_ioctl(struct inode *inodep, struct file *filp, unsigned int cmd, unsigned long arg) { int minor = DEVICE_NR(inodep->i_rdev); long devsize; switch (cmd) { case BLKGETSIZE : /* return device size */ devsize = s_length[minor] / s_hardsect[minor]; return put_user(devsize, (long *) arg); Embedded System Lab. II 28

(7) case BLKSSZGET : /* block size of media */ return put_user(s_blocksize[minor], (int *) arg); case BLKFLSBUF : /* flush */ if (!capable(cap_sys_admin)) return -EACCES; destroy_buffers(inodep->i_rdev); break; case BLKROSET : case BLKROGET : return blk_ioctl(inodep->i_rdev, cmd, arg); } default : return -EINVAL; } return 0; /* Request Processing */ int minird_make_request(request_queue_t *q, int rw, struct buffer_head *sbh) { char *ptr, *bdata; int size, minor; Embedded System Lab. II 29

(8) if ((minor = DEVICE_NR(sbh->b_rdev)) > DEVICE_NUM) { printk(device_name " : Unknown Minor Device\n"); goto fail; } ptr = s_data[minor] + sbh->b_rsector * s_hardsect[minor]; size = sbh->b_size; if (ptr + size > s_data[minor] + s_length[minor]) { printk(device_name " : Request past end of device\n"); goto fail; } bdata = bh_kmap(sbh); switch (rw) { case READ : case READA : memcpy(bdata, ptr, size); break; case WRITE : memcpy(ptr, bdata, size); break; default : goto fail; } Embedded System Lab. II 30

(9) sbh->b_end_io(sbh, 1); return 0; fail: } sbh->b_end_io(sbh, 0); return 0; Embedded System Lab. II 31

Makefile /* Makefile */ CC = arm-linux-gcc LD = arm-linux-ld KERNELDIR = /usr/src/linux-2.4.19 INCLUDEDIR = -I$(KERNELDIR)/include CFLAGS = -D KERNEL -DMODULE -O2 -Wall $(INCLUDEDIR) OBJS = minird.o SRCS = minird.c $(OBJS): $(SRCS) $(CC) $(CFLAGS) -c $(SRCS) clean : rm -f *.o *~ Embedded System Lab. II 32

$ make LED minird.o $ insmod minird.o $ mknod /dev/minird0 b [major] 0 $ dd if=/dev/zero of=/dev/minird0 bs=1024 count=1024 $ mke2fs /dev/minird0 1024 $ mkdir /mnt/other $ mount /dev/minird0 /mnt/other $ cp /bin/ls /mnt/other $ ls /mnt/other $ umount /mnt/other $ ls /mnt/other $ mount /dev/minird0 /mnt/other $ ls /mnt/other Embedded System Lab. II 33

Embedded System Lab. II 34

(2) Embedded System Lab. II 35

(3) Embedded System Lab. II 36