linux驅動學習之字元裝置驅動模板

來源:互聯網
上載者:User

今天整理了一下字元裝置驅動的模板,工程分開3個檔案.

main.c        驅動載入初始化工作。

loadmod.c  提供安裝和卸載驅動的函數。

fileops.c     檔案操作函數。

Makefile

#KERNELDIR = /home/fontlose/board/tx2416/kernelsom2416KERNELDIR = /usr/src/kernels/2.6.35.13-92.fc14.i686/PWD := $(shell pwd)#CC    = arm-linux-gccmoddev-objs=main.o fileops.o loadmod.oobj-m+=moddev.o modules:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesclean:rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions.PHONY: modules clean 

main.c

#include <linux/module.h>#include <linux/kernel.h>#include <linux/device.h>#include <linux/init.h>#include "loadmod.h"#include "fileops.h"MODULE_AUTHOR("my name");MODULE_LICENSE("Dual BSD/GPL");#define MAJORNUM  0#define MINJORNUM 60 struct file_operations ops={   .owner=THIS_MODULE,   .open=fileops_open,   .write=fileops_write,   .read=fileops_read,   .release=fileops_release};static int template_init(void){ if ( install_chardev(MAJORNUM,MINJORNUM,4,"moduledev","modclass",&ops))         return -1;  if(fileops_init())  {    uninstall_chardev();      return -1;  }  return 0;}static void template_exit(void){  uninstall_chardev();  fileops_destroy();  printk(KERN_ALERT "goodbye\n");}module_init(template_init);module_exit(template_exit);

loadmod.h

#ifndef   __LOADMOD_H__#define  __LOADMOD_H__#include <linux/fs.h>  extern unsigned char install_chardev(int major_i,int minor_i,unsigned char count,char*devname,char*classname,struct file_operations *ops);  extern int uninstall_chardev(void);#endif

loadmod.c

#include <linux/module.h>#include <linux/errno.h>#include <linux/kernel.h>#include <linux/device.h>#include <linux/kdev_t.h>#include <linux/err.h>#include <linux/cdev.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/string.h>static struct cdev  cdev_install;         //static struct class *class_install;       //static int  major_install,minor_install;  //static unsigned char count_install;       //  /** * install_chardev  字元裝置註冊函數 * @major_i 主裝置號 * @minor_i 次裝置號 * @count   裝置個數 * @devname 裝置名稱+註冊的裝置名稱自動在/dev下產生devname+次裝置號的檔案 * @classname 裝置類名 * @ops     檔案操作資料結構 */unsigned char install_chardev(int major_i,int minor_i,unsigned char count,char*devname,char*classname,struct file_operations *ops){  //step 1:裝置號的註冊  int i; dev_t dev;   if(major_i==0) //動態申請一個主裝置號  {      int res=alloc_chrdev_region(&dev,minor_i,count,devname);  if(res){    printk(KERN_ALERT "alloc chrdev region fail!\n");return -1;      }  major_i=MAJOR(dev);  }  else  {      int res;  dev=MKDEV(major_i,minor_i);  res=register_chrdev_region(dev,count,devname);  if(res){    printk(KERN_ALERT "register chrdev region fail!\n");return -1;      }    }  major_install=major_i;  minor_install=minor_i;  count_install=count;  //step 2:註冊裝置  cdev_install.count=0;  if(ops!=0)  {    cdev_init(&cdev_install,ops);    cdev_install.owner=THIS_MODULE;    cdev_install.ops  =ops;    if( cdev_add(&cdev_install,dev,count_install) )    {  unregister_chrdev_region(dev,count_install);   printk(KERN_ALERT "cdev add fail!\n");  return -1;    }    }  //step 3:建立裝置節點 注意在linux 2.6  //低版本使用class_device_create和class_device_destroy,高版本使用device_create和device_destroy  class_install=class_create(THIS_MODULE,classname);  if(IS_ERR(class_install))  {if(cdev_install.count>0) cdev_del(&cdev_install);  unregister_chrdev_region(dev,count_install); printk(KERN_ALERT "class creat fail!\n");      return -1;  }  for(i=0;i<count;i++)  {    device_create(class_install,NULL,MKDEV(major_install,minor_install+i),NULL,"%s%d",devname,minor_install+i);  }  printk(KERN_ALERT "device initial success major:%d minor:%d!\n",major_install,minor_install);    return 0;}/** * uninstall_chardev 卸載字元裝置 */int uninstall_chardev(void){int i;    if(cdev_install.count >0)   cdev_del(&cdev_install);  unregister_chrdev_region(MKDEV(major_install,minor_install),count_install);for(i=0;i<count_install;i++){  device_destroy(class_install,MKDEV(major_install,minor_install+i)); }class_destroy(class_install);return 0;}

fileops.h

#ifndef __FILEOPS_H__#define __FILEOPS_H__#include <linux/fs.h>extern int  fileops_init(void);extern void fileops_destroy(void);extern int fileops_open(struct inode *inode, struct file *filp);extern int fileops_release(struct inode *inode,struct file *filp);extern ssize_t fileops_read(struct file *filp, char __user *buff, size_t count, loff_t *offp);extern ssize_t fileops_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp);extern int fileops_ioctl(struct inode *, struct file *, unsigned int, unsigned long);#endif


fileops.c

#include <linux/init.h>#include <linux/kernel.h>#include <linux/kdev_t.h>#include <linux/err.h>#include <linux/cdev.h>#include <linux/fs.h>#include "fileops.h"#include <asm/uaccess.h>/** * 初始化 在載入時候調用 */int fileops_init(void){  return 0;}/** *釋放函數 在模組卸載時候調用 */void fileops_destroy(void){}/** * 檔案開啟 驅動初始化為後續操作做準備,如果不實現預設開啟成功  * 初始化裝置,如有需要則初始化filp->private_data * * @inode 檔案的索引節點,它用來存放檔案及目錄的基本資料,可取inode結構內 *        的次編號來確定具體哪個裝置 * @filp   */ int fileops_open(struct inode *inode, struct file *filp){  printk(KERN_ALERT "fileops_open\n");  return 0;   }/** * 檔案關閉 釋放open申請的記憶體 關閉裝置 */int fileops_release(struct inode *inode,struct file *filp){  printk(KERN_ALERT "fileops_release\n");  return 0;}/** *讀裝置 *@filp  一般操作private_data資料 *@buff  使用者空間的緩衝,用於存放讀取的資料 *@count 使用者想要讀取的資料大小 *@offp  使用者正在存取的檔案位置 *返回實際讀出大小 負數表示出錯   */ssize_t fileops_read(struct file *filp, char __user *buff, size_t count, loff_t *offp){  /*每次讀完更新offp*/  printk(KERN_ALERT "fileops_read\n");  return 0;}/** *寫裝置 *@filp  一般操作private_data資料 *@buff  使用者空間的緩衝,用於存放要寫的資料 *@count 使用者想要寫入的資料大小 *@offp  使用者正在存取的檔案位置 *返回實際寫入大小 負數表示出錯 */ssize_t fileops_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp){  /*這裡系統調用會一直寫 直到寫夠count,如返回小於count的數,會再次調用此方法直到返回錯誤或寫夠count*/  printk(KERN_ALERT "fileops_write\n");  return count;}/** *fileops_ioctl ioctl 系統調用提供了發出裝置特定命令的方法 *@inode *@filp *@cmd *@arg */int fileops_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg){  printk(KERN_ALERT "fileops_ioctl\n");  return 0;}

測試結果

[root@localhost module]# makemake -C /usr/src/kernels/2.6.35.13-92.fc14.i686/ M=/home/fontlose/board/tx2416/rootfs/usr/ldds/module modulesmake[1]: Entering directory `/usr/src/kernels/2.6.35.13-92.fc14.i686'  CC [M]  /home/fontlose/board/tx2416/rootfs/usr/ldds/module/main.o  CC [M]  /home/fontlose/board/tx2416/rootfs/usr/ldds/module/fileops.o  CC [M]  /home/fontlose/board/tx2416/rootfs/usr/ldds/module/loadmod.o  LD [M]  /home/fontlose/board/tx2416/rootfs/usr/ldds/module/moddev.o  Building modules, stage 2.  MODPOST 1 modules  CC      /home/fontlose/board/tx2416/rootfs/usr/ldds/module/moddev.mod.o  LD [M]  /home/fontlose/board/tx2416/rootfs/usr/ldds/module/moddev.komake[1]: Leaving directory `/usr/src/kernels/2.6.35.13-92.fc14.i686'[root@localhost module]# dmesg|tail -2[31864.045477] device initial success major:247 minor:60![31872.877461] goodbye[root@localhost module]# ls /dev/moduledev6*/dev/moduledev60  /dev/moduledev61  /dev/moduledev62  /dev/moduledev63[root@localhost module]# ls /sys/class/modclass/moduledev60  moduledev61  moduledev62  moduledev63[root@localhost module]# echo 1234 > /dev/moduledev60 [root@localhost module]# dmesg|tail -3[32022.480159] fileops_open[32022.480171] fileops_write[32022.480174] fileops_release

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.