驅(qū)動(dòng)學(xué)習(xí)1
/*清除空間*/
memset(mem_devp,0,sizeof(struct mem_dev));
for(i = 0; i < MEMDEV_NR_DEVS; ++i)
{
mem_devp[i].size = MEMDEV_SIZE;
mem_devp[i].data = kmalloc(MEMDEV_SIZE,GFP_KERNEL);
/*問(wèn)題,沒(méi)有進(jìn)行錯(cuò)誤的控制*/
memset(mem_devp[i].data,0,MEMDEV_SIZE);
}
return 0;
fail_malloc:
unregister_chrdev_region(devno,1);
return result;
}
/*模塊清除函數(shù)*/
static void memdev_exit(void)
{
cdev_del(&cdev);/*注銷字符設(shè)備*/
/*釋放兩個(gè)物理內(nèi)存*/
kfree(mem_devp[0].data);
kfree(mem_devp[1].data);
kfree(mem_devp);/*釋放設(shè)備結(jié)構(gòu)體內(nèi)存*/
unregister_chrdev_region(MKDEV(mem_major,0),2);
}
/*定義相關(guān)的操作函數(shù)*/
/*mem_open*/
int mem_open(struct inode *inode,struct file *filp)
{
struct mem_dev *dev;
/*判斷設(shè)備文件的次設(shè)備號(hào)*/
int num = MINOR(inode->i_rdev);
if(num >= MEMDEV_NR_DEVS)
return -ENODEV;
dev = &mem_devp[num];
/*將數(shù)據(jù)指向兩個(gè)內(nèi)存空間*/
filp->private_data = dev;
return 0;
}
/*release函數(shù)的實(shí)現(xiàn)*/
int mem_release(struct inode *inode, struct file *flip)
{
return 0;
}
/*read函數(shù)的實(shí)現(xiàn)*/
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;
/*獲得file結(jié)構(gòu)體上的指針*/
struct mem_dev *dev = filp->private_data;
/*參數(shù)的檢查*/
if(p >= MEMDEV_SIZE)
return 0;
if(count > MEMDEV_SIZE - p)
count = MEMDEV_SIZE - p;
/*從內(nèi)核讀數(shù)據(jù)到用戶空間*/
if(copy_to_user(buf,(void *)(dev->data + p),count))
{
/*出錯(cuò)誤*/
ret = -EFAULT;
}
else
{
/*移動(dòng)當(dāng)前文件光標(biāo)的位置*/
*ppos += count;
ret = count;
printk(KERN_INFO "read %d bytes(s) from %d",count,p);
}
return ret;
}
/*write函數(shù)的實(shí)現(xiàn)*/
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;
/*獲得設(shè)備結(jié)構(gòu)體的指針*/
struct mem_dev *dev = filp->private_data;
/*檢查參數(shù)的長(zhǎng)度*/
if(p >= MEMDEV_SIZE)
return 0;
if(count > MEMDEV_SIZE - p)
count = MEMDEV_SIZE - p;
if(copy_from_user(dev->data + p,buf,count))
ret = -EFAULT;
else
{
/*改變文件位置*/
*ppos += count;
ret = count;
printk(KERN_INFO "writted %d bytes(s) from %d",count,p);
}
return ret;
}
/*lseek的實(shí)現(xiàn)*/
static loff_t mem_llseek(struct file *filp,loff_t offset, int whence)
{
loff_t newpos;
switch(whence)
{
case 0:/*SEEK_SET*/
newpos = offset;
break;
case 1:/*SEEK_CUR*/
newpos = filp->f_pos + offset;
break;
case 2:/*SEEK_END*/
newpos = MEMDEV_SIZE - 1 + offset;
break;
default:
return -EINVAL;
}
filp->f_pos = newpos;
return newpos;
}
/*作者以及權(quán)限的聲明*/
MODULE_AUTHOR("GongPing");
MODULE_LICENSE("GPL");
/*通過(guò)宏module_init和module_exit實(shí)現(xiàn)模塊添加*/
module_init(memdev_init);
module_exit(memdev_exit);
第三個(gè)是Makefile:
ifneq ($(KERNELRELEASE),)
obj-m := memdev.o
else
#KDIR :=/usr/src/kernels/2.6.35.14-96.fc14.i686
#KDIR :=/lib/modules/2.6.35.14-96.fc14.i686/build
#KDIR ?=/opt/LinuxKernel/linux-2.6.38.1
KDIR :=/opt/LinuxKernel/linux-2.6.38.1
PWD :=$(shell pwd)
default:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif
第4個(gè)是應(yīng)用程序:
#include
#include
#include
int main()
{
FILE *fp0 = NULL;
char Buf[4096];
/*復(fù)制數(shù)據(jù)到buf中*/
strcpy(Buf,"Mem is char devices!");
printf("Buf: %s",Buf);
/*打開(kāi)設(shè)備文件*/
fp0 = fopen("/dev/memdev0","rw");
/*錯(cuò)誤處理*/
if(fp0 == NULL)
{
printf("Open Memdev0 Error!");
return -1;
}
/*寫數(shù)據(jù)到設(shè)備中*/
//fread(Buf,sizeof(Buf),1,fp0);
fwrite(Buf,sizeof(Buf),1,fp0);
/*定位*/
fseek(fp0,0,SEEK_SET);
/*復(fù)制數(shù)據(jù)到Buf*/
strcpy(Buf,"Buf is NULL!");
/*打印*/
printf("Buf: %s",Buf);
/*讀數(shù)據(jù)*/
fread(Buf,sizeof(Buf),1,fp0);
printf("BUF: %s",Buf);
fclose(fp0);
return 0;
}
源碼是別人的,我只是做了一下注釋,然后做了適當(dāng)?shù)男薷?,?qū)動(dòng)程序與應(yīng)用程序以及硬件代碼存在較大的差別,以后要多寫,多理解,多總結(jié)。
關(guān)鍵詞:
驅(qū)動(dòng)學(xué)
相關(guān)推薦
技術(shù)專區(qū)
- FPGA
- DSP
- MCU
- 示波器
- 步進(jìn)電機(jī)
- Zigbee
- LabVIEW
- Arduino
- RFID
- NFC
- STM32
- Protel
- GPS
- MSP430
- Multisim
- 濾波器
- CAN總線
- 開(kāi)關(guān)電源
- 單片機(jī)
- PCB
- USB
- ARM
- CPLD
- 連接器
- MEMS
- CMOS
- MIPS
- EMC
- EDA
- ROM
- 陀螺儀
- VHDL
- 比較器
- Verilog
- 穩(wěn)壓電源
- RAM
- AVR
- 傳感器
- 可控硅
- IGBT
- 嵌入式開(kāi)發(fā)
- 逆變器
- Quartus
- RS-232
- Cyclone
- 電位器
- 電機(jī)控制
- 藍(lán)牙
- PLC
- PWM
- 汽車電子
- 轉(zhuǎn)換器
- 電源管理
- 信號(hào)放大器
評(píng)論