반응형

 

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

 

 

Makefile

  1. obj-m+=simple_dev.o  
  2.     
  3. ARCH :=arm  
  4. PWD := $(shell pwd)  
  5.      
  6. all:  
  7.     make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) ARCH=$(ARCH) modules  
  8.     
  9. clean:  
  10.     make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) ARCH=$(ARCH) clean  

 

 

simpleApp.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. }    

 

 

 

 

반응형

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


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

+ Recent posts