block driver

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,
   I am trying to implement a minimal block driver similar to sbull
driver that is implemented in LDD3 by rubini.But When i call insmod that
console is hanging.(i.e) the control is not returning from init
function.
I am listing the code below.

Will you please tell me wht is wrong with it.

Thanks in advance,
raja











#ifndef __KERNEL__
	#define __KERNEL__
#endif
 
#ifndef MODULE
	#define MODULE
#endif

#include <linux/config.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/genhd.h>
#include <linux/errno.h>	/* error codes */
#include <linux/timer.h>
#include <linux/types.h>	/* size_t */
#include <linux/blkdev.h>
#include <linux/kdev_t.h>
#include <linux/vmalloc.h>


MODULE_AUTHOR("RAJA");
MODULE_DESCRIPTION("BLOCK DRIVER");
MODULE_LICENSE("GPL");


#define DEV_NAME "ramfs_drv"
#define SBULL_MINORS	16
#define KERNEL_SECTOR_SIZE	512

#define DEBUG(fmt, args...) printk(KERN_DEBUG "%d:%s(): " fmt "\n",  \
 __LINE__, __FUNCTION__, ## args);


struct block_device_operations sbull_ops = {
	.owner = THIS_MODULE
};

typedef struct sbull_dev {
	int size;	//device size in sectors
	u8*data;	//data Array
	short users;	// no of users
	short media_changed; // flag media changed
	spinlock_t lock;	//for Mutual Exclusion
	struct request_queue *queue;	// The Device Request Queue
	struct gendisk *gd;	// gendisk structure
	struct timer_list timer;	// For Simulated Media Changes
}sbull_dev_t;

static int __init init_blkdev(void);
static void __exit exit_blkdev(void);



dev_t devNo;

static int hardsect_size = 512;
module_param(hardsect_size, int, 0);
static int nsectors = 1024;	/* How big the drive is */
module_param(nsectors, int, 0);
static int noOfDevices = 4;
module_param(noOfDevices , int, 0);

sbull_dev_t *devices;

static void request_fun(request_queue_t *q)
{
	DEBUG("Entered");
	DEBUG("Returned");
}


void setup_device(sbull_dev_t *device,int which)
{
	memset(device,0,sizeof(sbull_dev_t));
	device->size = nsectors*hardsect_size;
	device->data = vmalloc(device->size);
	if(!device->data)
		printk("%d : vmalloc Fail",__LINE__);
	else
		printk("Memory Allocated For device : %d\n",which);
	spin_lock_init(&device->lock);
	device->queue = blk_init_queue(request_fun,&device->lock);
	if (device->queue == NULL)
		printk("Unable To Initialize Request Queue\n");
	else
		printk("Request Queue Initialized\n");
	blk_queue_hardsect_size(device->queue, hardsect_size);
	device->queue->queuedata = device;
	
	device->gd = alloc_disk(SBULL_MINORS);
	if(!device->gd)
		printk("Unable To Allocate GenDisk Structure\n");
	else
		printk("GenDisk Structure Allocated \n");
	
	device->gd->major = devNo;
	device->gd->first_minor = which*SBULL_MINORS;
	device->gd->fops = &sbull_ops;
	device->gd->queue = device->queue;
	device->gd->private_data = device;
	snprintf(device->gd->disk_name,32,"sbull%c",which+'a');
	set_capacity(device->gd, nsectors*(hardsect_size/KERNEL_SECTOR_SIZE));
	add_disk(device->gd);
	return;
}

static int __init init_blkdev()
{
	DEBUG("Entered");
	int i;
	devNo = register_blkdev(0,DEV_NAME);
	if(devNo < 0)
	{
		printk("Unable To Create Block Device");
		return -EBUSY;
	}
	else
		DEBUG("Block Device Created");
	devices = kmalloc(noOfDevices*sizeof(sbull_dev_t),GFP_KERNEL);
	for(i=0;i<noOfDevices;i++)
		setup_device(devices+i,i);
	DEBUG("Returned");
	return 0;
}
static void __exit exit_blkdev()
{
	DEBUG("Entered");
	int i;
	for(i=0;i<noOfDevices;i++)
	{		
		struct sbull_dev *device = devices + i;
		if(device->gd)
		{	
			del_gendisk(device->gd);
			put_disk(device->gd);
		}
		if (device->queue) 
		{
			blk_cleanup_queue(device->queue);
		}
		if (device->data)
			vfree(device->data);
	}
	unregister_blkdev(devNo,DEV_NAME);
	kfree(devices);
	DEBUG("Returned");
}
module_init(init_blkdev);
module_exit(exit_blkdev);







--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux