📄 walei.c
字号:
#include <linux/unistd.h>#include <linux/module.h>#if defined(CONFIG_SMP)#define __SMP__#endif#if defined(CONFIG_MODVERSIONS)#define MODVERSIONS#endif#include <linux/sched.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/errno.h>#include <linux/malloc.h>#include <linux/ioport.h>#include <linux/mm.h>#include <linux/interrupt.h>#include <linux/tqueue.h>#include <linux/timer.h>#include <linux/delay.h>#include <linux/types.h>#include <asm/io.h>#include <asm/segment.h>#include <asm/irq.h>#include <asm/signal.h>#include <asm/siginfo.h>#include <asm/uaccess.h>#include <asm/fcntl.h>#include <asm/cache.h>#include <asm/atomic.h>#include <sys/syscall.h>#include "sjacan.h"/*****************************Variable define*******************************/unsigned int SJA_major = SJA_MAJOR;struct SJA_device *Device[SJA_NUM];unsigned int SJA_irq[SJA_NUM] = {6,6};unsigned int SJA_baudrate[SJA_NUM] = {40,40};static char *SJA_name[SJA_NUM] = {"sjacan0","sjacan1"};/*****************************Function define*******************************/void SJA_interrupt(int ,void *,struct pt_regs *);void recv_bottom_half(void *);void timer_function(unsigned long);/*****************************SJA device initialize*************************/static int SJA_init(void){ int res,i; res = check_region(SJA_CMD_PORT,3); if(res < 0) { printk(KERN_ALERT"check SJA port failed.\n"); return res; } request_region(SJA_CMD_PORT,3,"SJACAN"); for(i=0;i<SJA_NUM;i++) { Device[i] = kmalloc(sizeof(struct SJA_device),GFP_KERNEL); if(Device[i] == NULL) { printk(KERN_ALERT"allocate device memory failed.\n"); return(-ENOMEM); } memset(Device[i],0,sizeof(struct SJA_device)); Device[i]->RecvBuf = (unsigned char *)get_free_page(GFP_KERNEL); if(Device[i]->RecvBuf == NULL) { printk(KERN_ALERT"allocate Recv memory failed.\n"); return(-ENOMEM); } memset(Device[i]->RecvBuf,0,BUFSIZ); Device[i]->RecvHead = 0; Device[i]->RecvTail = 0; Device[i]->RecvNum = 0; Device[i]->RecvQueue = NULL; Device[i]->SendBuf = (unsigned char *)get_free_page(GFP_KERNEL); if(Device[i]->SendBuf == NULL) { printk(KERN_ALERT"allocate Send memory failed.\n"); return(-ENOMEM); } memset(Device[i]->SendBuf,0,BUFSIZ); Device[i]->recv_bottom_task = (struct tq_struct *)kmalloc(sizeof(struct tq_struct),GFP_KERNEL); if(Device[i]->recv_bottom_task == NULL) { printk(KERN_ALERT"allocate bottom memory failed.\n"); return(-ENOMEM); } memset(Device[i]->recv_bottom_task,0,sizeof(struct tq_struct)); Device[i]->recv_bottom_task->routine = recv_bottom_half; Device[i]->recv_bottom_task->data = (void *)Device[i]; Device[i]->timer = (struct timer_list *)kmalloc(sizeof(struct timer_list),GFP_KERNEL); if(Device[i]->timer == NULL) { printk(KERN_ALERT"allocate timer memory failed.\n"); return(-ENOMEM); } Device[i]->timer->data = 0; Device[i]->timer->function = timer_function; Device[i]->timer->expires = 0; Device[i]->TimerCount = 0; init_timer(Device[i]->timer); Device[i]->PortNum = i; Device[i]->FrameMode = 1; Device[i]->IrqNum = SJA_irq[i]; Device[i]->BaudRate = SJA_baudrate[i]; } return 0;}/*****************************Function bottom interrupt********************/void recv_bottom_half(void *device){}/*****************************Function interrupt****************************/void SJA_interrupt(int irq,void *dev_id,struct pt_regs *regs){}/*****************************SJACAN open**********************************/int SJA_open(struct inode *inode,struct file *filp){ int res; int PortNum; struct SJA_device *dev; PortNum = MINOR(inode->i_rdev); if(PortNum > SJA_NUM) return(-ENODEV); dev = Device[PortNum]; if((filp->f_flags & O_ACCMODE) == O_WRONLY) { } else { if(dev->ref_count == 0) { res = request_irq(dev->IrqNum, SJA_interrupt, SA_SHIRQ | SA_INTERRUPT,SJA_name[dev->PortNum],dev); if(res < 0) { printk(KERN_ALERT"can't request irq %i.\n",dev->IrqNum); return res; } printk(KERN_ALERT"request irq %i success.\n",dev->IrqNum); /*****************Initialize SJA1000*********************/ if(dev->FrameMode) SJA_WRITE(0x01,SJA_MODE,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0x00,SJA_MODE,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0x01,SJA_ACR0,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0xff,SJA_ACR1,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0x02,SJA_ACR2,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0xff,SJA_ACR3,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0xff,SJA_AMR0,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0xff,SJA_AMR1,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0xff,SJA_AMR2,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0xff,SJA_AMR3,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0x07,SJA_BTR0,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0xff,SJA_BTR1,(SJA1_DAT_PORT+PortNum));/*Set baudrate = 40k*/ SJA_WRITE(0x1a,SJA_OCR,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0x01,SJA_IE,(SJA1_DAT_PORT+PortNum)); SJA_WRITE(0x00,SJA_MODE,(SJA1_DAT_PORT+PortNum)); } dev->ref_count++; } filp->private_data = dev; MOD_INC_USE_COUNT; return 0;}/*****************************Functin SJA_release**************************/int SJA_release(struct inode *inode,struct file *filp){ struct SJA_device *dev = filp->private_data; if((filp->f_flags & O_ACCMODE) != O_WRONLY) { if(--dev->ref_count == 0) { free_irq(dev->IrqNum,dev); SJA_WRITE(0x00,SJA_IE,(SJA1_DAT_PORT+dev->PortNum)); } } MOD_DEC_USE_COUNT; return 0;}/*****************************Function timer*******************************/void timer_function(unsigned long ptr){}/*****************************File operations define************************/struct file_operations SJA_fops = { NULL,
NULL, //read NULL, //write NULL,
NULL, //EMM_poll, NULL, //ioctl NULL,
SJA_open, //open NULL,
SJA_release, //release NULL,
NULL, NULL,
NULL,
NULL
};
/****************************Module initialize******************************/int init_module(void){ int res; EXPORT_NO_SYMBOLS; res = register_chrdev(SJA_major,"SJACAN",&SJA_fops); if(res < 0) { printk(KERN_ALERT"device register failed.\n"); return res; } if(SJA_major == 0) SJA_major = res; if(SJA_init() < 0) printk(KERN_ALERT"SJA device initializition failed.\n"); else printk(KERN_ALERT"SJACAN initializition success.\n"); return 0;}/****************************Module release********************************/void cleanup_module(void){ unregister_chrdev(SJA_major,"SJACAN"); release_region(SJA_CMD_PORT,3); printk(KERN_ALERT"SJACAN release success.\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -