網上有很多關于手持pos機驅動,字符設備驅動開發的知識,也有很多人為大家解答關于手持pos機驅動的問題,今天pos機之家(www.tonybus.com)為大家整理了關于這方面的知識,讓我們一起來看下吧!
本文目錄一覽:
1、手持pos機驅動
手持pos機驅動
一、字符設備驅動我們之前學習過驅動程序的開發,接下來我們接著深入學習字符設備驅動程序的開發。字符設備驅動是比較簡單的一種驅動。主要在于實現init、exit、open、read、write等函數。
二、字符設備驅動實例1、在package/Kernel/文件夾下新建一個chardrv文件夾 2、在chardrv文件夾下新建一個Makefile文件,內容如下:
## Copyright (C) 2008 OpenWrt.org## This is free software, licensed under the GNU General Public License v2.# See /LICENSE for more information.#include $(TOPDIR)/rules.mkinclude $(INCLUDE_DIR)/kernel.mkPKG_NAME:=chardrvPKG_RELEASE:=2include $(INCLUDE_DIR)/package.mkdefine KernelPackage/chardrv SUBMENU:=Other modules TITLE:=CharDrv FILES:=$(PKG_BUILD_DIR)/chardrv.ko KCONFIG:=endefdefine KernelPackage/chardrv/descriptionKernel module for register chardrv.endefEXTRA_KCONFIG:= \\ CONFIG_CHARDRV=mEXTRA_CFLAGS:= \\ $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG)))) \\ $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) \\MAKE_OPTS:= \\ ARCH="$(LINUX_KARCH)" \\ CROSS_COMPILE="$(TARGET_CROSS)" \\ SUBDIRS="$(PKG_BUILD_DIR)" \\ EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \\ $(EXTRA_KCONFIG)define Build/Prepare mkdir -p $(PKG_BUILD_DIR) $(CP) ./src/* $(PKG_BUILD_DIR)/endefdefine Build/Compile $(MAKE) -C "$(LINUX_DIR)" \\ $(MAKE_OPTS) \\ modulesendef$(eval $(call KernelPackage,chardrv))
3、在chardrv文件夾下新建一個src文件夾,在src下新建一個Makefile文件,內容為:
obj-${CONFIG_CHARDRV} += chardrv.o
4、新建一個chardrv.c的驅動文件,文件內容為:
/***************************** ** 字符設備驅動程序模板********************************/#include <linux/mm.h>#include <linux/miscdevice.h>#include <linux/slab.h>#include <linux/vmalloc.h>#include <linux/mman.h>#include <linux/random.h>#include <linux/init.h>#include <linux/raw.h>#include <linux/tty.h>#include <linux/capability.h>#include <linux/ptrace.h>#include <linux/device.h>#include <linux/highmem.h>#include <linux/crash_dump.h>#include <linux/backing-dev.h>#include <linux/bootmem.h>#include <linux/splice.h>#include <linux/pfn.h>#include <linux/export.h>#include <linux/IO.h>#include <linux/aio.h>#include <linux/kernel.h>#include <linux/module.h>#include <asm/uaccess.h>#include <linux/ioctl.h>/**************** 基本定義 **********************///加密函數參數內容: _IOW(IOW_CHAR , IOW_NUMn , IOW_TYPE)//加密函數用于chardrv_ioctl函數中//使用舉例:ioctl(fd , _IOW('L',0x80,long) , 0x1);//#define NUMn chardrv#define IOW_CHAR 'L'#define IOW_TYPE long#define IOW_NUM1 0x80//初始化函數必要資源定義//用于初始化函數當中//device number; //設備號 dev_t dev_num; //struct dev //字符設備 struct cdev chardrv_cdev;//auto "mknode /dev/chardrv c dev_num minor_num"//自動創建設備對象struct class *chardrv_class = NULL;struct device *chardrv_device = NULL;/**************** 結構體 file_operations 成員函數 *****************///openstatic int chardrv_open(struct inode *inode, struct file *file){ printk("chardrv drive open...\"); return 0;}//closestatic int chardrv_close(struct inode *inode , struct file *file){ printk("chardrv drive close...\"); return 0;}//readstatic ssize_t chardrv_read(struct file *file, char __user *buffer, size_t len, loff_t *pos){ int ret_v = 0; printk("chardrv drive read...\"); return ret_v;}//writestatic ssize_t chardrv_write( struct file *file , const char __user *buffer, size_t len , loff_t *offset ){ int ret_v = 0; printk("chardrv drive write...\"); return ret_v;}//unlocked_ioctlstatic int chardrv_ioctl (struct file *filp , unsigned int cmd , unsigned long arg){ int ret_v = 0; printk("chardrv drive ioctl...\"); switch(cmd) { //常規: //cmd值自行進行修改 case 0x1: { if(arg == 0x1) //第二條件; { } } break; //帶密碼保護: //請在"基本定義"進行必要的定義 case _IOW(IOW_CHAR,IOW_NUM1,IOW_TYPE): { if(arg == 0x1) //第二條件 { } } break; default: break; } return ret_v;}/***************** 結構體: file_operations ,該結構體將驅動中的函數和應用層函數關聯(例如當調用應用層調用open函數時就會調用驅動中的open函數)************************/static const struct file_operations chardrv_fops = { .owner = THIS_MODULE, .open = chardrv_open, .release = chardrv_close, .read = chardrv_read, .write = chardrv_write, .unlocked_ioctl = chardrv_ioctl,};//使用insmod掛載驅動時回調static __init int chardrv_init(void){ int ret_v = 0; printk("mydrv drive init...\"); /* 函數alloc_chrdev_region主要參數說明: 參數1: 自動分配的設備號 參數2: 次設備號 參數3: 創建多少個設備 */ if( ( ret_v = alloc_chrdev_region(&dev_num,0,1,"chardrv") ) < 0 ) //為chardrv動態分配設備號 { goto dev_reg_error; } //打印主設備號和次設備號 printk("The drive info of chardrv:\major: %d\minor: %d\", MAJOR(dev_num),MINOR(dev_num)); //關聯設備和操作函數 cdev_init(&chardrv_cdev,&chardrv_fops); //注冊設備 if( (ret_v = cdev_add(&chardrv_cdev,dev_num,1)) != 0 ) { goto cdev_add_error; } //創建設備類,用于自動創建設備 chardrv_class = class_create(THIS_MODULE,"chardrv"); if( IS_ERR(chardrv_class) ) { goto class_c_error; } //通過設備類創建設備 chardrv_device = device_create(chardrv_class,NULL,dev_num,NULL,"chardrv"); if( IS_ERR(chardrv_device) ) { goto device_c_error; } printk("auto mknod success!\"); //------------ 請在此添加您的初始化程序 --------------// //如果需要做錯誤處理,請:goto mydrv_error; //---------------------- END ---------------------------// goto init_success;dev_reg_error: printk("alloc_chrdev_region failed\"); return ret_v;cdev_add_error: printk("cdev_add failed\"); unregister_chrdev_region(dev_num, 1); return ret_v;class_c_error: printk("class_create failed\"); cdev_del(&chardrv_cdev); unregister_chrdev_region(dev_num, 1); return PTR_ERR(chardrv_class);device_c_error: printk("device_create failed\"); cdev_del(&chardrv_cdev); unregister_chrdev_region(dev_num, 1); class_destroy(chardrv_class); return PTR_ERR(chardrv_device);//------------------ 請在此添加您的錯誤處理內容 ----------------//chardrv_error: return -1;//-------------------- END -------------------//init_success: printk("chardrv init success!\"); return 0;}//使用rmmod卸載驅動是回調該函數static __exit void chardrv_exit(void){ printk("chardrv drive exit...\"); //釋放初始化使用到的資源; cdev_del(&chardrv_cdev); unregister_chrdev_region(dev_num, 1); device_unregister(chardrv_device); class_destroy(chardrv_class);}/**************** module operations**********************///聲明加載函數和卸載函數module_init(chardrv_init);module_exit(chardrv_exit);//some infomationMODULE_LICENSE("GPL v2");MODULE_AUTHOR("YANG");/********************* The End ***************************/
5、最后就是編譯了,和上一節一樣進行編譯就OK了!
喜歡這篇文章或者對你有幫助,歡迎點贊,分享,關注??!
更多精彩文章,歡迎關注微信公眾號"嵌入式軟件開發交流" !
以上就是關于手持pos機驅動,字符設備驅動開發的知識,后面我們會繼續為大家整理關于手持pos機驅動的知識,希望能夠幫助到大家!
