_ Code sample platform bus

 

 

 


 

 

 

  1 #include <linux/init.h>
  2 #include <linux/module.h>
  3 #include <linux/platform_device.h>
  4 #include <linux/fs.h>
  5 #include <linux/device.h>
  6 #include <linux/miscdevice.h>
  7 
  8 #include <asm/io.h>
  9 #include <asm/uaccess.h>
 10 
 11 
 12 static void *led_reg_base;
 13 static struct miscdevice  led_misc;
 14 
 15 //实现设备操作接口
 16 int led_plat_drv_open(struct inode *inode, struct File * the filp)
 . 17  {
 18 is  
. 19      the printk ( " -------- ^ _ ^ S ------------% \ n- " , __ FUNCTION__);
 20 is      // register is initialization 
21 is      unsigned Long value = __raw_readl (led_reg_base);
 22 is      value & = ~ ( 0xFF << 12 is );
 23 is      value | = ( 0x11 << 12 is ); // configured to output 
24      __raw_writel (value, led_reg_base);
 25  
26 is      return  0 ;
 27  }
28 an ssize_t led_plat_drv_write ( struct File the filp *, const  char __user * buf, size_t size, loff_t * the flags)
 29  {
 30      int RET;
 31 is      the printk ( " -------- ----- ^ _ ^ S% ------- \ n- " , __ FUNCTION__);
 32  
33 is      unsigned int   value;
 34 is      // application space data into kernel space data 
35      RET = copy_from_user (& value, buf, size);
 36      IF (RET =! 0 ) {
 37 [          the printk ( " copy_from_user error! \ n-");
 38         return -EFAULT;
 39     }
 40 
 41     
 42 
 43     if(value)
 44     {
 45         // 将led亮起来
 46         __raw_writel(__raw_readl(led_reg_base + 4) | (0x3<<3), led_reg_base + 4);
 47     }else
 48     {
 49         __raw_writel(__raw_readl(led_reg_base + 4) &~(0x3<<3), led_reg_base + 4);
 50     }
 51 
 52 
 53     return size;
 54 }
 55 
 56 int led_plat_drv_close(struct inode *inode, struct file *filp)
 57 {
 58     printk("--------^_^ %s------------\n",__FUNCTION__);
 59 
 60     __raw_writel(__raw_readl(led_reg_base + 4) &~(0x3<<3), led_reg_base + 4);
 61     
 62     return 0;
 63 }
 64 
 65 
 66 
 67 const struct file_operations plat_led_fops = {
 68     .open = led_plat_drv_open,
 69     .write = led_plat_drv_write,
 70     .release = led_plat_drv_close,
 71 
 72 };
 73 
 74 int led_pdrv_probe(struct platform_device *pdev)
 75 {
 76     printk("-----------%s-----------\n", __FUNCTION__);
 77 
 78 
 79     //任务1---与应用交互
 80     //the register_chrdev (), class_create, device_create, FOPS
 81      // or one with a function written --- get all of the above functions 
82      led_misc.name = " plat_led " ;   // / dev / plat_led 
83      led_misc.minor = 120 ;   // default major number 10, minor freely defined --MISC_DYNAMIC_MINOR (255) assigned by the system 
84      led_misc.fops = & plat_led_fops;
 85      misc_register (& led_misc);
 86      
87  
88      // tasks interact with the hardware 2 --- - - must first obtain resources, ioremap / request_irq ()
 89      // parameters 1-- obtained from which pdev in
 90      // parameter 2-- type of resource
 91     // Parameter 3-- resources of several isoforms 
92      struct Resource * mem_res = platform_get_resource (PDEV, IORESOURCE_MEM, 0 );
 93  
94      // get the address of the resource on the need for address mapping 
95      led_reg_base = ioremap (mem_res-> Start, resource_size (mem_res));
 96  
97  
98  
99      return  0 ;
 100  }
 101  int led_pdrv_remove ( struct platform_device * PDEV)
 102  {
 103      the printk ( " ----------- ------- S% ---- \ n- " , __FUNCTION__);
 104      
105     iounmap(led_reg_base);
106     misc_deregister(&led_misc);
107     
108     return 0;
109 }
110 
111 
112 const struct platform_device_id led_id_table[] = {
113         {"s5pv210_led", 0x123}, //第二个值随便写
114         {"exynos4_led", 0x124},
115         {"s5p6818_led", 0x444},
116 };
 117  
1 18  
119  struct platform_driver led_pdrv = {
 120      .PROBE = led_pdrv_probe,
 121      .remove = led_pdrv_remove,
 122      .driver = { // parent class must be initialized 
123          .name = " samsung_led_drv " , // must initialize self - definitions 
124      },
 125      .id_table = led_id_table,
 126  
127  };
 128  
129  static  int the __init plat_led_drv_init ( void )
 130.  {
131 
132     //注册一个pdrv
133     return platform_driver_register(&led_pdrv);
134 
135 }
136 
137 static void __exit plat_led_drv_exit(void)
138 {
139 
140     platform_driver_unregister(&led_pdrv);
141 }
142 
143 
144 module_init(plat_led_drv_init);
145 module_exit(plat_led_drv_exit);
146 MODULE_LICENSE("GPL");

 

 


 

 

 1 #include <linux/init.h>
 2 #include <linux/module.h>
 3 #include <linux/platform_device.h>
 4 
 5 
 6 #define GPC0_REG_BASE 0xE0200060
 7 #define GPC0_SIZE 8
 8 
 9 struct resource     led_res[] = {
10     [0] = {
11         .start = GPC0_REG_BASE,
12         .end = GPC0_REG_BASE + GPC0_SIZE - 1,
13         .flags = IORESOURCE_MEM,
14     },
15     //The following code shows how to define a plurality of hardware resources are not actually --led 
16      [ . 1 ] = {
 . 17          .start = 0x666 ,
 18 is          .end = 0x666 ,
 . 19          .name = " fake_irq " ,
 20 is          .flags = IORESOURCE_IRQ,
 21 is      },
 22 is      [ 2 ] = {
 23 is          .start = 0x12345678 ,
 24          .end = 0x12345678 + 0x8 - . 1 ,
 25          .name =" Fake_mem " ,
 26 is          .flags = IORESOURCE_MEM,
 27      },
 28  };
 29  
30  
31 is  // mainly used to solve the warning rmmod 
32  void     led_pdev_release ( struct Device * dev) // PDEV called automatically removed from the bus system 
33  {
 34 is  
35  
36  }
 37 [  
38 is  
39  struct platform_device led_pdev = {
 40      .name = " s5pv210_led " ,
 41 is      .id = - . 1 ,
 42 is     .dev = {
43         .release = led_pdev_release,
44     },
45     .num_resources = ARRAY_SIZE(led_res),
46     .resource = led_res,
47 };
48 
49 
50 static int __init plat_led_dev_init(void)
51 {
52 
53     //注册一个pdev
54     return platform_device_register(&led_pdev);
55 
56 }
57 
58 static void __exit plat_led_dev_exit(void)
59 {
60 
61     platform_device_unregister(&led_pdev);
62 }
63 
64 
65 module_init(plat_led_dev_init);
66 module_exit(plat_led_dev_exit);
67 MODULE_LICENSE("GPL");

 

 


 

 

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <sys/types.h>
 5 #include <sys/stat.h>
 6 #include <fcntl.h>
 7 
 8 int main(void)
 9 {
10 
11     int fd;
12     int on;
13 
14     fd = open("/dev/plat_led",O_RDWR);
15     if(fd < 0)
16     {
17         perror("open");
18         exit(1);
19     }
20 
21     
22     while(1)
23     {
24         
25         on = 1;
26         write(fd,&on,sizeof(on));
27         sleep(1);
28 
29         
30         on = 0;
31         write(fd,&on,sizeof(on));
32         sleep(1);
33     }
34 
35     close(fd);
36     return 0;
37 }

 

 


 

 

 

 

. 1 the CROSS_COMPILE = none-ARM-Linux-gnueabi-
 2 the CC = $ (the CROSS_COMPILE) GCC
 . 3  
. 4  # Specify the kernel source path
 . 5 KERNEL_DIR should = / Home / Farsight / S5PV210 / Kernel / Driver / Linux- 3.0 . . 8 
. 6 CUR_DIR = $ ( pwd the shell)
 . 7  
. 8 MYAPP = plat_led_app
 . 9  
10 the mODULE = plat_led_dev
 . 11 MODULE2 = plat_led_drv
 12 is  
13 is  All:
 14      # let make compiled into the kernel source, while c procedures in the current directory as the compiled kernel module with
 15      make -C $ (KERNEL_DIR should ) M = $ (CUR_DIR) modules
 16 ifneq ($ (MYAPP),)    
 . 17      $ (the CC) - O $ (MYAPP) $ (MYAPP) .c
 18 is  endif
 . 19  
20 is  Clean:
 21 is      # Delete above compiled files
 22 is      the make -C $ (KERNEL_DIR should) M = $ (CUR_DIR) Clean
 23 is      RM - RF $ (MYAPP)
 24  
25  the install:
 26 is      CP * .ko $ (MYAPP) / opt / the rootfs / drv_module
 27  
28  # which specifies the current directory as the file compiled kernel module
 29 obj-m + = $ (the MODULE) .o
 30 obj-m + = $ (MODULE2) .o

 

Guess you like

Origin www.cnblogs.com/panda-w/p/10991957.html