⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directio.c

📁 ARM开发实例emed linuxexamples.tar.gz
💻 C
字号:
/*  leddrv.c - Create an input/output character device */#include <linux/kernel.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/errno.h> 	/* for -EBUSY */#include <linux/ioport.h>	/* for verify_area */#include <linux/init.h>		/* for module_init */#include <asm/uaccess.h>  	/* for get_user and put_user */#include <asm-arm/arch-s3c2410/S3C2410.h>#include <sys/io.h>#include <asm/unistd.h>#include <sys/syscall.h>#include <linux/slab.h>#include <asm/segment.h>#include <linux/compatmac.h>#include "../include/directio.h"#define 	DEBUG#ifdef  	DEBUG#define 	DbgPrintk(S)	printk (S)#else#define 	DbgPrintk(S)		#endif	#define 	BUF_LEN 		4#define 	DEVICE_NAME 	"char_dev"static int 	Device_Open = 0;static char Message[BUF_LEN];static char *Message_Ptr;void 	Led_Port_init(void);void 	Led_Put(int data);int 	Led_Get(void);int 	device_ioctl(struct inode *,struct file *,unsigned int ,unsigned long );int     request_port(unsigned int , int);int     release_port(void);/* This function is called whenever a process attempts  * to open the device file */static int device_open(struct inode *inode, struct file *file){  /* We don't want to talk to two processes at the    * same time */  if (Device_Open) return -EBUSY;  Device_Open++;    MOD_INC_USE_COUNT;  /* Initialize the message */  Message_Ptr = Message;  return 0;}/* This function is called when a process closes the  * device file. It doesn't have a return value because  * it cannot fail. Regardless of what else happens, you  * should always be able to close a device (in 2.0, a 2.2 * device file could be impossible to close). */static int device_release(struct inode *inode, struct file *file){  /* We're now ready for our next caller */  Device_Open --;  MOD_DEC_USE_COUNT;  return 0;}/* This function is called whenever a process which  * has already opened the device file attempts to  * read from it. */static ssize_t device_read(    struct file *file,    char *buffer, 		/* The buffer to fill with the data */       size_t length,     	/* The length of the buffer */    loff_t *offset) 	/* offset to the file */{   /* Read functions are supposed to return the number     * of bytes actually inserted into the buffer */  return 0;}/* This function is called when somebody tries to  * write into our device file. */ static ssize_t device_write(struct file *file, const char *buffer, size_t length, loff_t *offset){  /* Again, return the number of input characters used */  return 0;};typedef struct port_stru{    unsigned int startaddr;    unsigned int endaddr;    struct port_stru * next;}port_stru;struct port_stru *port_head = NULL;struct port_stru *port_tail = NULL;/* * request the port * parameters: *   port : port started *   nums : port nums(bytes) * return: *   -1 : parameter error *   -2 : port busy *    0 : OK *    1 : port already request by this module */int request_port(unsigned int port, int nums){    struct port_stru *temp = port_head;    if(nums <= 0)    {        printk("invalid ports\n");        return -1;    }    /* find the port from the existed lists */    while(temp)    {        if((port >= temp->startaddr && port <= temp->endaddr) && \           (port + nums - 1 >= temp->startaddr && port + nums -1 <= temp->endaddr))        {            /* already request by this module */            return 1;        }        temp = temp->next;    }    /*     * 申请端口     */    if(check_region(port, nums))    {        printk("%x is used by another module\n", port);        return -2;    };      request_region(port, nums, DEVICE_NAME);    /* add to this list */    temp = (struct port_stru *)kmalloc(sizeof(struct port_stru), GFP_KERNEL);    temp->next = NULL;    temp->startaddr = port;    temp->endaddr = port + nums -1;    if(port_head == NULL)        port_head = port_tail = temp;    else        port_tail = port_tail->next = temp;    return 0;}int release_port(){    struct port_stru *temp = port_head;    while(port_head)    {        temp = port_head->next;        release_region(port_head->startaddr, port_head->endaddr - port_head->startaddr + 1);        kfree(port_head);    }    return 0;}/* This function is called whenever a process tries to  * do an ioctl on our device file. We get two extra  * parameters (additional to the inode and file  * structures, which all device functions get): the number * of the ioctl called and the parameter given to the  * ioctl function. * * If the ioctl is write or read/write (meaning output  * is returned to the calling process), the ioctl call  * returns the output of this function. */int device_ioctl(    struct inode *inode,    struct file *file,    unsigned int ioctl_num,/* The number of the ioctl */    unsigned long ioctl_param) /* The parameter to it */{  port_data     temp;  port_data* 	data;  /* Switch according to the ioctl called */  switch (ioctl_num)   {    case IOCTL_SET_MSG:      DbgPrintk("device_ioctl IOCTL_SET_MSG\n");      /* Receive a pointer to a message (in user space)        * and set that to be the device's message. */       /* Get the parameter given to ioctl by the process */      data = (struct port_data*) ioctl_param;            copy_from_user(&temp, data, sizeof(struct port_data));      if(temp.cmd == CMD_IRQ)      {      }else      {          /* port write access */          if(request_port(temp.startaddr, temp.nums) >= 0)          {              switch(temp.opts)              {              case PORT_BYTE:              (*(unsigned char *)temp.startaddr) = temp.value;              break;              case PORT_HWORD:              (*(unsigned short *)temp.startaddr)=temp.value;              break;              case PORT_WORD:              (*(unsigned int *)temp.startaddr)=temp.value;              break;              }          }else          {              printk("\nrequest port error\n");              return -1;          }      }      break;    case IOCTL_GET_MSG:      /* Get the parameter given to ioctl by the process */      data = (struct port_data*) ioctl_param;      copy_from_user(&temp, data, sizeof(struct port_data));      /* port read access */      if(request_port(temp.startaddr, temp.nums) >= 0)      {          switch(temp.opts)          {          case PORT_BYTE:               temp.value = (*(unsigned char *)temp.startaddr);          break;          case PORT_HWORD:              temp.value = (*(unsigned short *)temp.startaddr);          break;          case PORT_WORD:              temp.value = (*(unsigned int *)temp.startaddr);          break;          }      }else      {          printk("\nrequest port error\n");          return -1;      }      copy_to_user((struct port_data *) ioctl_param, &temp, sizeof(struct port_data));      break;  }  return 0;};/* Module Declarations *************************** *//* This structure will hold the functions to be called  * when a process does something to the device we  * created. Since a pointer to this structure is kept in  * the devices table, it can't be local to * init_module. NULL is for unimplemented functions. */struct file_operations Fops = {	.read 		= device_read, 	.write 		= device_write,	.open 		= device_open,	.release 	= device_release,	.ioctl		= device_ioctl	};/* Initialize the module - Register the character device */static int __init directio_module_init( void ){    	int ret_val;          /* Register the character device (atleast try) */        ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &Fops);        /* Negative values signify an error */        if (ret_val < 0) {            printk ("%s failed with %d\n",                "Sorry, registering the character device ",                ret_val);            return ret_val;        }    printk ("Directio: let user program to access io port using outx & inx\n");  return 0;};/* Cleanup - unregister the appropriate file from /proc */static void __exit directio_module_cleanup(void){  int ret;    /* Unregister the device */  ret = unregister_chrdev(MAJOR_NUM, DEVICE_NAME);    /* If there's an error, report it */   if (ret < 0)    printk("Error in module_unregister_chrdev: %d\n", ret);  release_port();};module_init(directio_module_init);module_exit(directio_module_cleanup);MODULE_LICENSE("GPL");MODULE_AUTHOR("Cvtech Co., Ltd <http://www.cvtech.com.cn>");MODULE_DESCRIPTION("directio char driver");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -