The sample code of Song Baohua's detailed explanation of Linux device driver development: Basic Character Device Driver

Source: Internet
Author: User

Simplest character device driver code

Scull. c

#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>#defineSCULL_MAJOR252#define SCULL_NAME"scull"#define MAX_DATA0x1000staticint scull_major = SCULL_MAJOR;struct scull_dev {struct cdev cdev;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){struct scull_dev *dev = container_of(inode->i_cdev, struct scull_dev, cdev);filp->private_data = dev;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){struct scull_dev *dev = filp->private_data;unsigned longp = *f_ops;unsigned intcnt = count;int ret = 0;if(down_interruptible(&dev->sem))return -ERESTARTSYS;if(copy_to_user(buf, dev->data,cnt)){ret = -EFAULT;} else {*f_ops += cnt;ret = cnt;printk(KERN_ALERT "read %u bytes from %lu\n",cnt, p);}up(&dev->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){struct scull_dev *dev = filp->private_data;unsigned long p = *f_ops;unsigned int cnt = count;int ret = 0;if(down_interruptible(&dev->sem))return -ERESTARTSYS;if(copy_from_user(dev->data,buf, cnt))ret = -EFAULT;else {*f_ops += cnt;ret = cnt;printk(KERN_ALERT "written %u bytes from %lu\n",cnt, p);}up(&dev->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,};staticvoidscull_setup_cdev(struct scull_dev *dev, int index){int err, devno = MKDEV(scull_major, index);cdev_init(&dev->cdev, &scull_fops);dev->cdev.owner = THIS_MODULE;err = cdev_add(&dev->cdev, devno, 1);if(err)printk(KERN_NOTICE "ERROR %d adding scull_dev %d", err, index);}intscull_init(void){int result;dev_tdevno = MKDEV(scull_major,0);if(scull_major)result = register_chrdev_region(devno, 1, "scull");else {result = alloc_chrdev_region(&devno, 0, 1, "scull");scull_major = MAJOR(devno);}if(result < 0)return result;scull_devp = kmalloc(sizeof(struct scull_dev), GFP_KERNEL);if(!scull_devp){result = -ENOMEM;goto fail_malloc;}memset(scull_devp, 0 , sizeof(struct scull_dev));scull_setup_cdev(scull_devp, 0);init_MUTEX(&scull_devp->sem);printk(KERN_ALERT "init scull device\n");return 0;fail_malloc:unregister_chrdev_region(devno, 1);return result;}voidscull_cleanup(void){cdev_del(&scull_devp->cdev);kfree(scull_devp);unregister_chrdev_region(MKDEV(scull_major, 0), 1);printk(KERN_ALERT "clean scull device\n");}module_init(scull_init);module_exit(scull_cleanup);

Test code

/************************************************************************* *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.komknod /dev/scull c 252 0

2 unload_scull.sh

#!/bin/sh/sbin/rmmod scull.korm /dev/scull -f

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.