Всем привет :)
Я пишу драйвер блочного устройства. Но он упорно отказывается работать. Вот код:
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/spinlock.h>
#include <linux/genhd.h>
#include <linux/string.h>
#include <linux/hdreg.h>
#include <linux/slab.h>
#define USB_TMP_MJ 239
DEFINE_SPINLOCK(usb_tmp_queue_lock);
#define usb_tmp_capacity 4*32*64
char *usb_tmp_buf;
static int usb_tmp_read(unsigned sector, unsigned len, char *buffer)
{
// memset(usb_tmp_buf, 1, count);
/*
* *****ОШИБКА ПО МОИМ ПОДСЧЕТАМ В ЭТОЙ СТРОКЕ**************************
* вроде определил что ошибка возникает из за переполнения "buffer" (могу ошибаться)
*/
memcpy(buffer, usb_tmp_buf+sector*512, len);
printk("%x %x %x %x %x\n", buffer, usb_tmp_buf,usb_tmp_buf+sector*512, sector, len);
return 0;
}
static int usb_tmp_write(unsigned sector, unsigned len, char *buffer)
{
memcpy(usb_tmp_buf+sector*512, buffer, len);
// printk("%x %x %x %x\n", buffer, usb_tmp_buf, usb_tmp_buf+sector*512, count);
return 0;
}
static int usb_tmp_open(struct block_device *bd, fmode_t fm)
{
printk("Open!!!\n";
return 0;
}
static int usb_tmp_getgeo(struct block_device *bd,struct hd_geometry *hg)
{
hg->heads = 4;
hg->cylinders = 64;
hg->sectors = 32;
return 0;
}
struct request_queue *usb_tmp_rq;
struct gendisk *usb_tmp_gd;
struct block_device_operations usb_tmp_ops = {
.owner = THIS_MODULE,
// .open = &usb_tmp_open,
.getgeo = &usb_tmp_getgeo,
};
void usb_tmp_request(struct request_queue *rq)
{
struct request *req;
unsigned sector, count, len;
while ((req = blk_fetch_request(rq)) != NULL) {
sector = blk_rq_pos(req);
count = blk_rq_sectors(req);
len = blk_rq_bytes(req);
if (sector + count >= usb_tmp_capacity) goto end_req;
if (req->cmd_type == REQ_TYPE_FS) {
switch (rq_data_dir(req)) {
case READ:
printk("READ: sect=%d, count=%d\n", sector, len);
usb_tmp_read(sector, len, req->buffer);
break;
case WRITE:
printk("WRITE: sect=%d, count=%d\n", sector, len);
usb_tmp_write(sector, len, req->buffer);
break;
}
}
end_req:
blk_end_request_all(req, 0);
}
}
static int __init usb_tmp_init(void)
{
int res;
if ((usb_tmp_buf = vmalloc(usb_tmp_capacity*512)) == NULL) {
printk("error vmalloc()\n";
return -1;
}
if (res = register_blkdev(USB_TMP_MJ, "usb_tmp") {
printk("Error: res=%d\n", (int) res);
return -1;
}
if (!(usb_tmp_rq = blk_init_queue(&usb_tmp_request, &usb_tmp_queue_lock))) {
printk("Error: rq=%d\n", (int) usb_tmp_rq);
return -1;
}
usb_tmp_gd = alloc_disk(1);
usb_tmp_gd->major = USB_TMP_MJ;
usb_tmp_gd->first_minor = 0;
usb_tmp_gd->minors = 4;
strcpy(&usb_tmp_gd->disk_name,"usb_tmp";
usb_tmp_gd->fops = &usb_tmp_ops;
usb_tmp_gd->queue = usb_tmp_rq;
set_capacity(usb_tmp_gd, usb_tmp_capacity);
add_disk(usb_tmp_gd);
return 0;
}
module_init(usb_tmp_init);
static void __exit usb_tmp_exit(void)
{
del_gendisk(usb_tmp_gd);
blk_cleanup_queue(usb_tmp_rq);
unregister_blkdev(USB_TMP_MJ, "usb_tmp";
printk("exit\n";
}
module_exit(usb_tmp_exit);
Драйвер - так называемый ram-device driver
Где же здесь может быть ошибка:
memcpy(buffer, usb_tmp_buf+sector*512, len);Вроде передаю правильный размер и правильные адреса в функцию...
Помогите плз кто может...
kernel-2.6.39.2