반응형

 







 

실행결과

 

응용 프로그램 simpleApp.c

디바이스 드라이버 simple_dev.c

device file open

[13895.005913] open

App : write something

life is good 12bytes

[13895.009107] simple_write

[13895.013561] DEV : read something

[13895.018762] life is good 12bytes

App : read something

life is good 12bytes

[13895.023354] simple_read

[13895.027854] DEV : write something

[13895.033086] life is good 12bytes

ioctl function call

ret = 0

[13895.037110] ioctl

device file close

ret = 0

[13895.039809] close

 

 

소스코드

simple_dev.c

  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/cdev.h>  
  4. #include <linux/device.h>  
  5. #include <linux/fs.h>            
  6. #include <linux/slab.h>  
  7. #include <asm/uaccess.h>  
  8.     
  9. dev_t id;  
  10. struct cdev cdev;  
  11. struct class *class;  
  12. struct device *dev;  
  13.     
  14. char temp[100];  
  15.     
  16.     
  17. #define DEVICE_NAME "simple"  
  18.     
  19.     
  20. int simple_open (struct inode *inode, struct file *filp)  
  21. {  
  22.     printk( "open\n" );  
  23.     memset( temp, 0, 0 );  
  24.     
  25.     return 0;  
  26. }  
  27.     
  28. int simple_close (struct inode *inode, struct file *filp)  
  29. {  
  30.     printk( "close\n" );  
  31.     return 0;  
  32. }  
  33.     
  34. ssize_t simple_read(struct file *filp, char *buf, size_t size, loff_t *offset)  
  35. {  
  36.     printk( "simple_read\n" );  
  37.     printk( "DEV : write something\n" );  
  38.     printk( "%s %dbytes\n", temp, strlen(temp) );  
  39.     int ret = copy_to_user( buf, temp, strlen(temp)+1 );  
  40.     
  41.     return strlen(temp);  
  42. }  
  43.     
  44. ssize_t simple_write (struct file *filp, const char *buf, size_t size, loff_t *offset)  
  45. {  
  46.     printk( "simple_write\n" );  
  47.     printk( "DEV : read something\n");  
  48.     
  49.     int ret = copy_from_user( temp, buf, size );  
  50.     printk( "%s %dbytes\n", temp, size );  
  51.     
  52.     return size;  
  53. }  
  54.     
  55. long simple_ioctl ( struct file *filp, unsigned int cmd, unsigned long arg)  
  56. {  
  57.     
  58.     printk( "ioctl\n" );  
  59.     return 0;  
  60. }  
  61.     
  62.     
  63. struct file_operations simple_fops =  
  64. {  
  65.     .owner           = THIS_MODULE,  
  66.     .read            = simple_read,       
  67.     .write           = simple_write,      
  68.     .unlocked_ioctl  = simple_ioctl,      
  69.     .open            = simple_open,       
  70.     .release         = simple_close,    
  71. };  
  72.     
  73. int simple_init(void)  
  74. {  
  75.     int ret;  
  76.     
  77.     ret = alloc_chrdev_region( &id, 0, 1, DEVICE_NAME );  
  78.     if ( ret ){  
  79.         printk( "alloc_chrdev_region error %d\n", ret );  
  80.         return ret;  
  81.     }  
  82.     
  83.     cdev_init( &cdev, &simple_fops );  
  84.     cdev.owner = THIS_MODULE;  
  85.     
  86.     ret = cdev_add( &cdev, id, 1 );  
  87.     if (ret){  
  88.         printk( "cdev_add error %d\n", ret );  
  89.         unregister_chrdev_region( id, 1 );  
  90.         return ret;  
  91.     }  
  92.     
  93.     class = class_create( THIS_MODULE, DEVICE_NAME );  
  94.     if ( IS_ERR(class)){  
  95.         ret = PTR_ERR( class );  
  96.         printk( "class_create error %d\n", ret );  
  97.     
  98.         cdev_del( &cdev );  
  99.         unregister_chrdev_region( id, 1 );  
  100.         return ret;  
  101.     }  
  102.     
  103.     dev = device_create( class, NULL, id, NULL, DEVICE_NAME );  
  104.     if ( IS_ERR(dev) ){  
  105.         ret = PTR_ERR(dev);  
  106.         printk( "device_create error %d\n", ret );  
  107.     
  108.         class_destroy(class);  
  109.         cdev_del( &cdev );  
  110.         unregister_chrdev_region( id, 1 );  
  111.         return ret;  
  112.     }  
  113.     
  114.     
  115.     return 0;  
  116. }  
  117.     
  118. void simple_exit(void)  
  119. {  
  120.     device_destroy(class, id );  
  121.     class_destroy(class);  
  122.     cdev_del( &cdev );  
  123.     unregister_chrdev_region( id, 1 );  
  124. }  
  125.     
  126. module_init(simple_init);  
  127. module_exit(simple_exit);  
  128.     
  129. MODULE_LICENSE("Dual BSD/GPL");  

 

 

Makefile

  1. obj-m   := simple_dev.o  
  2.     
  3. KDIR    := /mnt/nfs/temp/linux-sunxi  
  4. ARCH :=arm  
  5. PWD     := $(shell pwd)  
  6.     
  7.     
  8. all:  
  9.     $(MAKE) -C $(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=arm-linux-gnueabi-  
  10. clean:  
  11.     $(MAKE) -C $(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=arm-linux-gnueabi- clean  

 

 

simple_App.c

  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <sys/stat.h>  
  4. #include <sys/ioctl.h>  
  5. #include <fcntl.h>  
  6. #include <unistd.h>  
  7.     
  8. #define DEVICE_FILENAME  "/dev/simple"  
  9.     
  10. int main()  
  11. {  
  12.     int dev, ret;  
  13.     char buf[100], buf2[100];  
  14.     
  15.     memset( buf2, 0, 0 );  
  16.     strcpy(buf, "life is good" );  
  17.     int len = strlen( buf );  
  18.     
  19.     
  20.     printf( "device file open\n");   
  21.     dev = open( DEVICE_FILENAME, O_RDWR|O_NDELAY );  
  22.     
  23.     if( dev >= 0 )  
  24.     {  
  25.         printf( "App : write something\n");  
  26.         ret = write(dev, buf, len );  
  27.         printf( "%s %dbytes\n", buf, ret );  
  28.     
  29.         printf( "App : read something\n");  
  30.         ret = read(dev, buf2, 100 );  
  31.         printf( "%s %dbytes\n", buf2, ret );  
  32.     
  33.         printf( "ioctl function call\n");  
  34.         ret = ioctl(dev, 0x100, 30 );  
  35.         printf( "ret = %d\n", ret );  
  36.     
  37.         printf( "device file close\n");  
  38.         ret = close(dev);  
  39.         printf( "ret = %d\n", ret );  
  40.     }  
  41.     
  42.     return 0;  
  43. }  


반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts