Linux character device driver development (1) - using character device driver

1. Compile / install the driver

  • In the Linux system, the driver usually kernel module program structure to be encoded. Therefore, compile / install a driver, its essence is to compile / install a kernel module. The following sample code is copied to a Linux system:
  • memdev.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>


int dev1_registers[5];
int dev2_registers[5];

struct cdev cdev; 
dev_t devno;

/*文件打开函数*/
int mem_open(struct inode *inode, struct file *filp)
{
    
    /*获取次设备号*/
    int num = MINOR(inode->i_rdev);
    
    if (num==0)
        filp->private_data = dev1_registers;
    else if(num == 1)
        filp->private_data = dev2_registers;
    else
        return -ENODEV;  //无效的次设备号
    
    return 0; 
}

/*文件释放函数*/
int mem_release(struct inode *inode, struct file *filp)
{
  return 0;
}

/*读函数*/
static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
  unsigned long p =  *ppos;
  unsigned int count = size;
  int ret = 0;
  int *register_addr = filp->private_data; /*获取设备的寄存器基地址*/

  /*判断读位置是否有效*/
  if (p >= 5*sizeof(int))
    return 0;
  if (count > 5*sizeof(int) - p)
    count = 5*sizeof(int) - p;

  /*读数据到用户空间*/
  if (copy_to_user(buf, register_addr+p, count))
  {
    ret = -EFAULT;
  }
  else
  {
    *ppos += count;
    ret = count;
  }

  return ret;
}

/*写函数*/
static ssize_t mem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
  unsigned long p =  *ppos;
  unsigned int count = size;
  int ret = 0;
  int *register_addr = filp->private_data; /*获取设备的寄存器地址*/
  
  /*分析和获取有效的写长度*/
  if (p >= 5*sizeof(int))
    return 0;
  if (count > 5*sizeof(int) - p)
    count = 5*sizeof(int) - p;
    
  /*从用户空间写入数据*/
  if (copy_from_user(register_addr + p, buf, count))
    ret = -EFAULT;
  else
  {
    *ppos += count;
    ret = count;
  }

  return ret;
}

/* seek文件定位函数 */
static loff_t mem_llseek(struct file *filp, loff_t offset, int whence)
{ 
    loff_t newpos;

    switch(whence) {
      case SEEK_SET: 
        newpos = offset;
        break;

      case SEEK_CUR: 
        newpos = filp->f_pos + offset;
        break;

      case SEEK_END: 
        newpos = 5*sizeof(int)-1 + offset;
        break;

      default: 
        return -EINVAL;
    }
    if ((newpos<0) || (newpos>5*sizeof(int)))
    	return -EINVAL;
    	
    filp->f_pos = newpos;
    return newpos;

}

/*文件操作结构体*/
static const struct file_operations mem_fops =
{
  .llseek = mem_llseek,
  .read = mem_read,
  .write = mem_write,
  .open = mem_open,
  .release = mem_release,
};

/*设备驱动模块加载函数*/
static int memdev_init(void)
{
  /*初始化cdev结构*/
  cdev_init(&cdev, &mem_fops);
  
  /* 注册字符设备 */
  alloc_chrdev_region(&devno, 0, 2, "memdev");
  cdev_add(&cdev, devno, 2);
}

/*模块卸载函数*/
static void memdev_exit(void)
{
  cdev_del(&cdev);   /*注销设备*/
  unregister_chrdev_region(devno, 2); /*释放设备号*/
}

MODULE_LICENSE("GPL");

module_init(memdev_init);
module_exit(memdev_exit);
  • Makefile (note the kernel source code should be changed to your own path):
obj-m := memdev.o

KDIR := /home/S4_a/part3/lesson3/lesson-2440/linux-mini2440

all :
	make -C $(KDIR) M=$(PWD) CROSS_COMPILE=arm-linux- ARCH=arm

clean:
	rm -f *.o *.ko *.order *.symvers *.mod.c
  • Compile:
    • #make
  • Memdev.ko copied to the development board and then installs:
    • #insmod memdev.ko

2. Create a device file

  • This figure may be known by the application of the device driver is accessed through the device file to carry out, the device as a media file as the applications and device drivers linked together.
  • By character device file, the application can use the corresponding character device driver to control the character device .
  • Create a character device file, there are two general ways:
    • 1. Use the mknod: mknod / dev / filename c major number minor device numbers
      • It has four parameters, the first is the name of a character device file needs to be created, small c for character device file, the major number to establish a connection and device drivers, they need to use the same major number, so how View device number mendev driver uses just installed it? Development board input: #cat proc / Devices
      • 2 shows the information, the first column is the major number, the second column is the name of the device driver. It should be 253, the minor number of non-negative number can be taken, it is generally between 0 and 255.
      • Now we create a file in the device development board (device file name and existing conflicts can not):
        • #mknod /dev/memdev0 c 253 1
        • And then view the dev: #ls / dev
    • 2. Create function (subsequent description) in the driver

3. Access Device

  • The device file is actually a memory operation because the actual hardware device access, and ultimately all access to device registers, the memory access in a few units to achieve the same effect.
  • Write an application to access the device files:
  • Write device: write_mem.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int fd = 0;
    int src = 2013;
   
    fd = open("/dev/memdev0", O_RDWR);

    write(fd, &src, sizeof(int));

    close(fd);

    return 0;
}
  • Reading equipment: read_mem.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int fd = 0;
    int dst = 0;
    
    fd = open("/dev/memdev0", O_RDWR);

    read(fd, &dst, sizeof(int));

    printf("dst is %d\n",dst);
 
    return 0;
}
  • Using the arm-linux-gcc compiler generated files are copied to the development board, and then executed, if an error is found:
    • ~bin/sh: ./write_mem not found
    • In fact, this is not write_mem can not find the file, but it relies on the library can not be found, you can use:
    • #arm-readelf  -d  write_mem
    • -d indicates that the query dynamic link library to view it depends on the library
    • This method is time 2 minutes, 1 is a copy of the libraries it depends lib directory development board, the connector 2 with static at compile time:
    • #arm-linux-gcc  -static  write_mem.c  -o write_mem
  • Reading equipment: read_mem.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int fd = 0;
    int dst = 0;
    
    fd = open("/dev/memdev0", O_RDWR);

    read(fd, &dst, sizeof(int));

    printf("dst is %d\n",dst);
 
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_22847457/article/details/91125994