mydev.ko --- mymod_single.c

it's a single ringbuffer hardware module driver, based on mymod_single.c and ringbuffer.*.

1. Makefile

obj-m := mydev.o
#mydev-objs := mymod_basic.o
mydev-objs := mymod_single.o ringbuffer.o

# With this simplified Makefile, type 'make' to build your module
# Type 'make install' to build and install the module to the target file system

KERNELDIR=$(shell ls -d ~/linux-2.6.[0-9]*)
TARGETDIR=/targetfs

PWD := $(shell pwd)

default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

install:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules modules_install INSTALL_MOD_PATH=$(TARGETDIR)

2. mymod_single.c

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/uaccess.h>	/* for accessing user-space */
#include "ringbuffer.h"

static unsigned major = 0;				/* save major number assigned to driver */
static struct cdev mydev;				/* to be registered with kernel */
static struct class *mydev_class = NULL;		/* device class to register with sysfs */
static struct device *mydev_device;			/* device registered with sysfs */

#define MYDEV_RING_SIZE 2048					/* accomodate 2K per ring */
static struct OutRingBuffer *mydev_ring;			/* ring buffer handle */
static char rbuf[MYDEV_RING_SIZE], wbuf[MYDEV_RING_SIZE];	/* temporary buffers for moving data
								   between user-space and device */

static ssize_t
mydev_read(struct file *filp, char __user *buf, size_t nbuf, loff_t *offs)
{
	/* cap nbuf to MYDEV_RING_SIZE; otherwise we risk overflowing our tmp buffer */
	if (nbuf > MYDEV_RING_SIZE)
		nbuf = MYDEV_RING_SIZE;

	/* read data from device into the tmp buffer, since device cannot write directly
	   to user-space memory */
	nbuf = rb_read(mydev_ring, rbuf, nbuf);

	/* copy the data from the user-supplied buffer to the tmp buffer, since device cannot
	   directly read from user-space memory */
	copy_to_user(buf, rbuf, nbuf);

	/* adjust file offset */
	*offs += nbuf;

	/* return number of bytes read */
	return nbuf;
}

static ssize_t
mydev_write(struct file *filp, const char __user *buf, size_t nbuf, loff_t *offs)
{
	/* cap nbuf to MYDEV_RING_SIZE; otherwise we risk overflowing our tmp buffer */
	if (nbuf > MYDEV_RING_SIZE)
		nbuf = MYDEV_RING_SIZE;

	/* copy the data from the user-supplied buffer to the tmp buffer, since device cannot
	   directly read from user-space memory */
	copy_from_user(wbuf, buf, nbuf);

	/* write data out to device */
	nbuf = rb_write(mydev_ring, wbuf, nbuf);

	/* adjust file offset */
	*offs += nbuf;

	/* return number of bytes written */
	return nbuf;
}

static const struct file_operations mydev_fops = {
	.owner = THIS_MODULE,
	.read = mydev_read,
	.write = mydev_write
};

module_param(major, uint, 0);		/* allow manual specification of major # */

static int __init
mymod_init (void)
{
	int rc;
	dev_t devid;

	/* Handle static allocation of major number (specified as a module parameter) or
	   otherwise dynamically allocate the major number */

	if (major) {
		/* Use the major number specified */
		devid = MKDEV(major, 0);
		rc = register_chrdev_region(devid, 1, "mydev");
	} else {
		/* Dynamically allocate a major/minor number */
		rc = alloc_chrdev_region(&devid, 0, 1, "mydev");

		/* save major number component of devid */
		major = MAJOR(devid);
	}

	if (rc) {
		/* inability to register major number is fatal */
		printk("Trouble registering major number!\n");
		return rc;
	}

	/* Time to initialize hardware, for this must be done prior to calling
	   cdev_add(), after which time requests can start coming in */

	rc = rb_init(&mydev_ring, MYDEV_RING_SIZE);

	if (rc) {
		printk("rb_init() returned error %d\n", rc);
		unregister_chrdev_region(devid, 1);
		return rc;
	}
	
	/* Initalize the struct cdev structure, and set the table of file
	   operation functions (this function cannot fail) */

	cdev_init(&mydev, &mydev_fops);

	/* Register the char device with the kernel. At this point, driver
	   should be prepared to handle requests (particularly if the device
	   nodes were already created) */

	if ((rc = cdev_add(&mydev, devid, 1))) {
		/* failure at this point is fatal */
		printk("Trouble registering with kernel!\n");
		rb_destroy(mydev_ring);
		unregister_chrdev_region(devid, 1);
		return rc;
	}

	/* Establish a class under which we will register our driver with sysfs.
	   Registration with sysfs is nice, but not necessary for the driver to
	   work. Therefore, do not consider failure at this point to be fatal. */
		   
	mydev_class = class_create(THIS_MODULE, "mydev");

	if (IS_ERR(mydev_class)) {
		printk("class_create() returned error %ld\n", PTR_ERR(mydev_class));
		mydev_class = NULL;
	} else {
		/* Register device with sysfs. Among other things, this
		   will trigger creation of the device node in /dev */

		mydev_device = device_create(mydev_class, NULL, devid, NULL, "mydev0");
		if (IS_ERR(mydev_device)) {
			printk("device_create() returned error %ld\n", PTR_ERR(mydev_device));
			mydev_device = NULL;
		}
	}

	printk("Hello from mydev!\n");
	return 0;
}  

static void __exit
mymod_exit(void) 
{
	/* deregister from kernel */
	cdev_del(&mydev);

	/* release major/minor numbers allocated to the driver */
	unregister_chrdev_region(MKDEV(major, 0), 1);

	/* Destroy ring buffers only after cdev interfaces is brought down */
	rb_destroy(mydev_ring);

	if (mydev_class != NULL) {
		/* deregister device from sysfs */
		device_destroy(mydev_class, MKDEV(major, 0));

		class_destroy(mydev_class);
	}

	printk("Goodbye from mydev.\n"); 
}  

module_init(mymod_init);
module_exit(mymod_exit);

MODULE_AUTHOR("Me"); 
MODULE_LICENSE("GPL"); 
MODULE_DESCRIPTION("Character driver skeleton.");

3. ringbuffer.c

/*
   BeatForce
   ringbuffer.c  - ring buffer's
   
   Copyright (c) 2001, Patrick Prasse (patrick.prasse@gmx.net)

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public Licensse as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* This source has been modified from its original form by David LeBlanc
   (leblancdw@yahoo.com) for the following reasons:
   - Converted to use Linux kernel APIs and headers
   - Added error checking if buffer allocation fails
   - A function called rb_destroy() has also been provided to free a ring
     buffer structure previously allocated with rb_init()
   - Introduced a data_size member to fix a corner case or two
 */

#include <linux/slab.h>
#include "ringbuffer.h"


/* call before output thread is active !!!!! */
int
rb_init (struct OutRingBuffer **rb, int size)
{
    struct OutRingBuffer *ring;

    if(rb==NULL || size < 1024)
    {
        return -EINVAL;
    }
    
    ring = kzalloc (sizeof (struct OutRingBuffer), GFP_KERNEL); //allocate memory, and set the memory to zero
    if(ring == NULL)
    {
        return -ENOMEM;
    }

    ring->size = 1;
    while(ring->size < size)
        ring->size <<= 1;


    ring->buffer=kmalloc(sizeof(char)*(ring->size), GFP_KERNEL); //only just allocate memory 
 
    if(ring->buffer == NULL)
    {
        kfree(ring);
        return -ENOMEM;
    }
   
    *rb = ring;

    return 0;
}

void
rb_destroy (struct OutRingBuffer *rb)
{
    kfree(rb->buffer);
    kfree(rb);
}

int
rb_write (struct OutRingBuffer *rb, unsigned char * buf, int len)
{
    int total;
    int i;

    /* total = len = min(space, len) */
    total = rb_free(rb);
    if(len > total)
        len = total;
    else
        total = len;

    i = rb->wr_pointer;
    if(i + len > rb->size)
    {
        memcpy(rb->buffer + i, buf, rb->size - i);
        buf += rb->size - i;
        len -= rb->size - i;
        rb->data_size += rb->size - i;
        i = 0;
    }
    memcpy(rb->buffer + i, buf, len);
    rb->wr_pointer = i + len;
    rb->data_size += len;
    return total;

        
}

int
rb_free (struct OutRingBuffer *rb)
{
    return (rb->size - rb->data_size);
}


int
rb_read (struct OutRingBuffer *rb, unsigned char * buf, int max)
{
    int total;
    int i;
    /* total = len = min(used, len) */
    total = rb_data_size(rb);

    if(max > total)
        max = total;
    else
        total = max;

    i = rb->rd_pointer;
    if(i + max > rb->size)
    {
        memcpy(buf, rb->buffer + i, rb->size - i);
        buf += rb->size - i;
        max -= rb->size - i;
        rb->data_size -= rb->size - i;
        i = 0;
    }
    memcpy(buf, rb->buffer + i, max);
    rb->rd_pointer = i + max;
    rb->data_size -= max;
    return total;

}

int
rb_data_size (struct OutRingBuffer *rb)
{
    return rb->data_size;
}


int
rb_clear (struct OutRingBuffer *rb)
{
    memset(rb->buffer,0,rb->size);
    rb->rd_pointer=0;
    rb->wr_pointer=0;
    rb->data_size=0;
    return 0;
}

4. ringbuffer.h

/*
   BeatForce
   ringbuffer.h  - ring buffer (header)
   
   Copyright (c) 2001, Patrick Prasse (patrick.prasse@gmx.net)

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public Licensse as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

 */

#ifndef __RINGBUFFER_H__
#define __RINGBUFFER_H__

struct OutRingBuffer
{
    char *buffer;
    int wr_pointer;
    int rd_pointer;
    int data_size;
    int size;
};


/* ring buffer functions */
int rb_init (struct OutRingBuffer **, int);
void rb_destroy (struct OutRingBuffer *);
int rb_write (struct OutRingBuffer *, unsigned char *, int);
int rb_free (struct OutRingBuffer *);
int rb_read (struct OutRingBuffer *, unsigned char *, int);

int rb_data_size (struct OutRingBuffer *);
int rb_clear (struct OutRingBuffer *);


#endif




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值