Description of Linux device driver development by Song Baohua

Source: Internet
Author: User
The drive of a general character device is transformed into a misc device. The MISC device is not as troublesome as a character device. In the future, try to replace the scull. c driver with a misc device.
 
#include <linux/fs.h>
#include <linux/kernel.h>#include <linux/cdev.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/gfp.h>#include <asm/uaccess.h>#include <linux/miscdevice.h>#define SCULL_NAME"scull"#define MAX_DATA0x1000struct scull_dev {struct miscdevice misc_scull;char data[MAX_DATA];struct semaphore sem;};MODULE_LICENSE("Dual BSD/GPL");MODULE_AUTHOR("BG2BKK");structscull_dev *scull_devp;intscull_open(struct inode *inode, struct file *filp){printk(KERN_ALERT "open the scull device\n");return 0;}intscull_release(struct inode *inode, struct file *filp){printk(KERN_ALERT "close the scull device\n");return 0;}ssize_tscull_read(struct file *filp, char __user *buf, size_t count, loff_t *f_ops){unsigned longp = *f_ops;unsigned intcnt = count;int ret = 0;if(down_interruptible(&scull_devp->sem))return -ERESTARTSYS;if(copy_to_user(buf, scull_devp->data,cnt)){ret = -EFAULT;} else {*f_ops += cnt;ret = cnt;printk(KERN_ALERT "read %u bytes from %lu\n",cnt, p);}up(&scull_devp->sem);printk(KERN_ALERT "read %d bytes\n",count);return ret;}ssize_t scull_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_ops){unsigned long p = *f_ops;unsigned int cnt = count;int ret = 0;if(down_interruptible(&scull_devp->sem))return -ERESTARTSYS;if(copy_from_user(scull_devp->data,buf, cnt))ret = -EFAULT;else {*f_ops += cnt;ret = cnt;printk(KERN_ALERT "written %u bytes from %lu\n",cnt, p);}up(&scull_devp->sem);printk(KERN_ALERT "write %d bytes\n",count);return ret;}structfile_operations scull_fops = {.owner=THIS_MODULE,.open=scull_open,.release=scull_release,.write=scull_write,.read=scull_read,};intscull_init(void){int result;scull_devp = kmalloc(sizeof(struct scull_dev), GFP_KERNEL);if(!scull_devp){printk(KERN_ALERT "fialed to malloc a device\n");result = -ENOMEM;goto fail_malloc;}memset(scull_devp, 0 , sizeof(struct scull_dev));scull_devp->misc_scull.name=SCULL_NAME;scull_devp->misc_scull.minor=MISC_DYNAMIC_MINOR;scull_devp->misc_scull.fops=&scull_fops;result = misc_register(&scull_devp->misc_scull);if(result){printk(KERN_ALERT "failed to register a misc device\n");result = -1;goto fail_register;}init_MUTEX(&scull_devp->sem);printk(KERN_ALERT "init scull device\n");return 0;fail_register:kfree(scull_devp);scull_devp = NULL;fail_malloc:return result;}voidscull_cleanup(void){misc_deregister(&scull_devp->misc_scull);kfree(scull_devp);scull_devp = NULL;printk(KERN_ALERT "clean scull device\n");}module_init(scull_init);module_exit(scull_cleanup);

Test code

Test. c

/************************************************************************* *fileName:    test.c *description: test the myscull.c *author:      Hzc *create time: 2007-04-20 *modify info: -*************************************************************************/#include <stdio.h>#include <fcntl.h>#include <sys/ioctl.h>#include <sys/types.h>/* device path */char path[] = "/dev/scull";char buf[10];char rbuf[10];int i;int t = 1;intmain (){  int f = open (path, O_RDWR);   for (i = 0; i < 10; i++)    {      buf[i] = i;      printf("buf[%d] = %d\n",i,buf[i]);    }printf("write %d bytes\n",write (f, buf, 10));//  close (f);//////  f = open (path, O_RDONLY);// if (f == -1)//    {//      printf ("device open error 2!\n");//      return 1;//    }  printf ("Read the string from device...\n");printf("read %d bytes\n",read (f, rbuf, 10));  for (i = 0; i < 10; i++)    {      printf ("%d\n", rbuf[i]);    }  close (f);}

Makefile

#KERNEL_DIR := /home/huang/linux-2.6.18/KERNEL_DIR := /lib/modules/$(shell uname -r)/buildPWD:= $(shell pwd)obj-m := scull.odefault:$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modulestest: test.cgcc $< -o $@.o  -gclean:rm -rf *.o *.ko *~ *.order *.symvers *.markers *.mod.c

Test script

1 load_scull.sh

#!/bin/sh/sbin/insmod scull.ko

2 unload_scull.sh

#!/bin/sh/sbin/rmmod scull.ko

3 test. Sh

#!/bin/shmake cleanmake make testsudo ./unload_scullsudo ./load_scullsudo ./test.o

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.