From 02981ccf1ab7040162fee0baf54c85dcab035a0c Mon Sep 17 00:00:00 2001 From: =?utf8?q?T=C3=BAlio=20Magno=20Quites=20Machado=20Filho?= Date: Wed, 11 Jun 2008 23:54:37 -0300 Subject: [PATCH] Initial code for the module. --- .gitignore | 6 +++++ src/driver/Makefile | 9 +++++++ src/driver/vdi.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/driver/vdi.h | 33 +++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 src/driver/Makefile create mode 100644 src/driver/vdi.c create mode 100644 src/driver/vdi.h diff --git a/.gitignore b/.gitignore index 9298f33..a32f44a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,10 @@ *.vdi tstVDI obj/* +*.mod +*.cmd +*.ko +*.mod.c +*.mod.o +*.symvers diff --git a/src/driver/Makefile b/src/driver/Makefile new file mode 100644 index 0000000..ea0ec83 --- /dev/null +++ b/src/driver/Makefile @@ -0,0 +1,9 @@ +ifneq ($(KERNELRELEASE),) + obj-m := vdi.o +else + KERNELDIR ?= /lib/modules/$(shell uname -r)/build + PWD := $(shell pwd) + +default: + $(MAKE) -C $(KERNELDIR) M=$(PWD) modules +endif diff --git a/src/driver/vdi.c b/src/driver/vdi.c new file mode 100644 index 0000000..d16411d --- /dev/null +++ b/src/driver/vdi.c @@ -0,0 +1,78 @@ +/* + * vdi.h - The driver for virtual disks of VirtualBox + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 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, see . + */ + +#ifndef __KERNEL__ +# define __KERNEL__ +#endif +#ifndef MODULE +# define MODULE +#endif + +//#include +#include +#include + +static int vdi_major; + +#include "vdi.h" + + +/* + * + */ +static int major = VDI_MAJOR; +static int devs = 0; +static int rahead = 0; +static int size = 0; +static int blksize = 1024; + +int vdi_devs, vdi_ra; + +static int vdi_init(void) +{ + int result; +// printk(KERN_ALERT "Starting VDI driver.\n"); + + // Register the VDI + result = register_blkdev(major, "vdi"); + if (result < 0) + { + printk(KERN_WARNING "vdi: cannot get major %d\n", vdi_major); + return result; + } + // Set dynamic number + if (vdi_major == 0) + { + vdi_major = result; + } + major = vdi_major; + + + + return 0; +} + +static void vdi_exit(void) +{ + //printk(KERN_ALERT "Exiting VDI driver.\n"); + + // Unregister device + unregister_blkdev(major, "vdi"); +} + +module_init(vdi_init); +module_exit(vdi_exit); diff --git a/src/driver/vdi.h b/src/driver/vdi.h new file mode 100644 index 0000000..6edcc56 --- /dev/null +++ b/src/driver/vdi.h @@ -0,0 +1,33 @@ +/* + * vdi.h - The driver for virtual disks of VirtualBox + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 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, see . + */ + +#ifndef ___vdi_h +#define ___vdi_h + +#define VDI_MAJOR 0 + +struct vdi { + int size; // Device size + u8 *data; // Pointer to the data array + short users; // Number of users + short media_change // Flag for media change + spinlock_t lock; // Mutual exclusion + struct request_queue *queue; // Device request queue + struct gendisk *gd; // gendisk structure +} + +#endif -- 2.11.4.GIT