driver private data should be retained in inode, not file

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@215 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2007-05-12 20:09:37 +00:00
parent a0d83d8604
commit 9900280cbd

View File

@ -94,8 +94,7 @@ static const struct block_operations g_bops =
static int up_open(FAR struct file *filp) static int up_open(FAR struct file *filp)
{ {
filp->f_priv = (void*)up_deviceimage(); return OK;
return 0;
} }
/**************************************************************************** /****************************************************************************
@ -107,12 +106,7 @@ static int up_open(FAR struct file *filp)
static int up_close(FAR struct file *filp) static int up_close(FAR struct file *filp)
{ {
if (filp->f_priv) return OK;
{
free(filp->f_priv);
filp->f_priv = NULL;
}
return 0;
} }
/**************************************************************************** /****************************************************************************
@ -125,18 +119,21 @@ static int up_close(FAR struct file *filp)
static ssize_t up_read(FAR struct file *filp, char *buffer, static ssize_t up_read(FAR struct file *filp, char *buffer,
size_t start_sector, size_t nsectors) size_t start_sector, size_t nsectors)
{ {
char *src = filp->f_priv; struct inode *inode = filp->f_inode;
if (src && if (inode)
start_sector < NSECTORS &&
start_sector + nsectors < NSECTORS)
{ {
memcpy(buffer, &src[start_sector*LOGICAL_SECTOR_SIZE], nsectors*LOGICAL_SECTOR_SIZE); char *src = inode->i_private;
return OK; if (src &&
} start_sector < NSECTORS &&
else start_sector + nsectors < NSECTORS)
{ {
return -EINVAL; memcpy(buffer,
&src[start_sector*LOGICAL_SECTOR_SIZE],
nsectors*LOGICAL_SECTOR_SIZE);
return OK;
}
} }
return -EINVAL;
} }
/**************************************************************************** /****************************************************************************
@ -149,18 +146,21 @@ static ssize_t up_read(FAR struct file *filp, char *buffer,
static ssize_t up_write(FAR struct file *filp, const char *buffer, static ssize_t up_write(FAR struct file *filp, const char *buffer,
size_t start_sector, size_t nsectors) size_t start_sector, size_t nsectors)
{ {
char *dest = filp->f_priv; struct inode *inode = filp->f_inode;
if (dest && if (inode)
start_sector < NSECTORS &&
start_sector + nsectors < NSECTORS)
{ {
memcpy(&dest[start_sector*LOGICAL_SECTOR_SIZE], buffer, nsectors*LOGICAL_SECTOR_SIZE); char *dest = inode->i_private;
return OK; if (dest &&
} start_sector < NSECTORS &&
else start_sector + nsectors < NSECTORS)
{ {
return -EINVAL; memcpy(&dest[start_sector*LOGICAL_SECTOR_SIZE],
buffer,
nsectors*LOGICAL_SECTOR_SIZE);
return OK;
}
} }
return -EINVAL;
} }
/**************************************************************************** /****************************************************************************
@ -172,9 +172,10 @@ static ssize_t up_write(FAR struct file *filp, const char *buffer,
static size_t up_geometry(FAR struct file *filp, struct geometry *geometry) static size_t up_geometry(FAR struct file *filp, struct geometry *geometry)
{ {
struct inode *inode = filp->f_inode;
if (geometry) if (geometry)
{ {
geometry->geo_available = (filp->f_priv != NULL); geometry->geo_available = (inode->i_private != NULL);
geometry->geo_nsectors = NSECTORS; geometry->geo_nsectors = NSECTORS;
geometry->geo_sectorsize = LOGICAL_SECTOR_SIZE; geometry->geo_sectorsize = LOGICAL_SECTOR_SIZE;
return OK; return OK;
@ -195,7 +196,7 @@ static size_t up_geometry(FAR struct file *filp, struct geometry *geometry)
void up_registerblockdevice(void) void up_registerblockdevice(void)
{ {
(void)register_blockdriver("/dev/blkdev", &g_bops, 0, NULL); /* Inode private data is a filesystem image */
void *priv = (void*)up_deviceimage();
(void)register_blockdriver("/dev/blkdev", &g_bops, 0, priv);
} }