Character device driver---lighting on led---based on jz2440 development board

The contents of First_drv.c are as follows:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

static struct class *firstdrv_class;
static struct class_device *firstdrv_class_devs;

volatile unsigned long *GPFCON = NULL;
volatile unsigned long *GPFDAT = NULL;

static int first_drv_open(struct inode *inode, struct file *file)
{
//printk("first_drv_open\n");
/*配置引脚GPF 4 5 6为输出*/
*GPFCON &= ~((3<< (4*2)) |(3<<(5*2)) |(3<< (6*2)));
*GPFCON |= ((1<< (4*2)) |(1<< (5*2)) |(1<< (6*2)));
return 0;
}

static ssize_t first_drv_write(struct file * file, const char __user *buf, size_t count, loff_t * ppos)
{
int val;
//printk("first_drv_write\n");
/*Data transfer between user space and kernel space cope_to_user*/
copy_from_user(&val, buf , count);
if(val == 1)
{
                  //light on

                *GPFDAT &= ~((1<<5) |(1<<6)|(1<<4));

      }
else
{
//Off
*GPFDAT |= ((1<<5) |(1<<6)|(1<<4));
}
return 0;
}
/* This structure is the core of the character device driver
 * The open, read, write and other functions called when the application operates the device file,
 * will eventually call the corresponding function specified in this structure
 */
static struct file_operations first_drv_fops = {
    .owner = THIS_MODULE, /* This is a macro, Push to the __this_module variable automatically created when the module is compiled*/
    .open = first_drv_open,     .write = first_drv_write, };  int major;  /*  * This function is called when the insmod command is executed   */ int first_drv_init(void) { major = register_chrdev(0,"first_drv",&first_drv_fops); //Register, tell the kernel   
  









firstdrv_class = class_create(THIS_MODULE, "firstdrv"); //Create a class //if (IS_ERR(firstdrv_class)) //return PTR_ERR(firstdrv_class); firstdrv_class_devs = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */   //Create a device under the class // if (unlikely(IS_ERR(firstdrv_class_devs))) // return PTR_ERR(firstdrv_class_devs); GPFCON = (volatile unsigned long *)ioremap (0x56000050,16); GPFDAT = GPFCON+1; return 0; }  /*  * This function is called when the rmmod command is executed   */ static void first_drv_exit(void) { unregister_chrdev(major,"first_drv"); //Uninstall the driver


















class_device_unregister(firstdrv_class_devs);
class_destroy(firstdrv_class);
iounmap(GPFCON);
}
module_init(first_drv_init);
module_exit(first_drv_exit);

MODULE_LICENSE("GPL");

The test file firstdrvtest.c is as follows:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/*
firstdrvtest on
firstdrvtest off
*/
int main(int argc , char **argv)
{
int fd;
int val = 1;
fd = open("/dev/xyz",O_RDWR);
if (fd < 0)
printf("cannt open!\n");
if(argc != 2)
{
printf("Usage :\n");
printf("%s <on/off>\n",argv[0]);
return 0;
}
if(strcmp(argv[1], "on") == 0)
{
val=1;
}

else

{

val=0;
}
       write(fd,&val,4);
return 0;

}

Note:

When compiling the led driver file, the directory of the header file must be specified. In the video of Wei Dongshan, the compiled kernel directory is specified in the makefile.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325476288&siteId=291194637