From: Adrian M. <zx8...@us...> - 2002-09-11 20:09:10
|
Update of /cvsroot/linuxdc/linux-sh-dc/drivers/mtd/maps In directory usw-pr-cvs1:/tmp/cvs-serv23652/drivers/mtd/maps Modified Files: vmu-flash.c Log Message: Code cleaned up Index: vmu-flash.c =================================================================== RCS file: /cvsroot/linuxdc/linux-sh-dc/drivers/mtd/maps/vmu-flash.c,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- vmu-flash.c 10 Sep 2002 22:55:51 -0000 1.10 +++ vmu-flash.c 11 Sep 2002 20:09:07 -0000 1.11 @@ -23,7 +23,6 @@ #include <asm/io.h> -static int VMU_NUM_BLOCKS; /* This is a dynamic property */ static int VMU_BLOCK_SIZE; #define _DEBUG_ @@ -54,6 +53,7 @@ long writecnt; long readcnt; long removable; + long numblocks; struct mtd_info *mtd; } memcard_t; @@ -91,8 +91,8 @@ /* Sanity check */ if (!mtd) { - DEBGM(KERN_WARNING - "VMU FLASH: Attempting to read data without having set up mtd.\n"); + DEBGM + ("VMU FLASH: Attempting to read data without having set up mtd.\n"); return -1; } @@ -111,7 +111,7 @@ ((unsigned long *) (mqu->recvbuf))[1] = num << 24; mqu->sendbuf = mqu->recvbuf; if (maple_add_packet(mqu) != 0) { - DEBGM(KERN_WARNING "VMU FLASH: Could not add packet\n"); + DEBGM("VMU FLASH: Could not add packet\n"); return -1; } @@ -130,46 +130,46 @@ return 0; } - DEBGM(KERN_WARNING - "VMU FLASH: Read has failed - return is 0x%X\n", + DEBGM("VMU FLASH: Read has failed - return is 0x%X\n", lastmq->recvbuf[0]); - DEBGM(KERN_WARNING "ERROR code is 0x%X\n", lastmq->recvbuf[1]); + DEBGM("ERROR code is 0x%X\n", lastmq->recvbuf[1]); return -1; } -static int maple_vmu_write_block(unsigned int num, u_char * buf, struct mtd_info *mtd) +static int maple_vmu_write_block(unsigned int num, u_char * buf, + struct mtd_info *mtd) { - - /* Sanity check */ if (!mtd) { printk(KERN_WARNING "VMU FLASH: Attempting to write data without having set up mtd.\n"); return -1; } + struct maple_driver_data *d = + (struct maple_driver_data *) (mtd->priv); + memcard_t *memcard = (memcard_t *) (d->private_data); + vmu_cache->valid = 0; /* Writing so any cache may be invalid */ - struct maple_driver_data *mdd = - (struct maple_driver_data *) (mtd->priv); - struct mapleq *mqu = (struct mapleq *) &(mdd->mq); - memcard_t *memcard = (memcard_t *) mdd->private_data; + + struct mapleq *mqu = (struct mapleq *) &(d->mq); /* Writing is phased */ int x; for (x = 0; x < memcard->writecnt; x++) { mqu->command = MAPLE_COMMAND_BWRITE; mqu->length = - (VMU_BLOCK_SIZE / (memcard->writecnt * 4)) + 2; + (memcard->blocklen / (memcard->writecnt * 4)) + 2; ((unsigned long *) (mqu->recvbuf))[0] = cpu_to_be32(MAPLE_FUNC_MEMCARD); ((unsigned long *) (mqu->recvbuf))[1] = num << 24 | x << 8; memcpy(mqu->recvbuf + 8, - buf + (VMU_BLOCK_SIZE / memcard->writecnt) * x, - VMU_BLOCK_SIZE / memcard->writecnt); + buf + (memcard->blocklen / memcard->writecnt) * x, + memcard->blocklen / memcard->writecnt); mqu->sendbuf = mqu->recvbuf; if (maple_add_packet(mqu) != 0) { @@ -201,15 +201,19 @@ * offset within the block. * */ -static block_t *__ofs_to_block(unsigned long src_ofs) +static block_t *__ofs_to_block(unsigned long src_ofs, struct mtd_info *mtd) { block_t *block; + struct maple_driver_data *d = + (struct maple_driver_data *) (mtd->priv); + memcard_t *card = (memcard_t *) (d->private_data); + block = kmalloc(sizeof(struct block_s), GFP_KERNEL); /* Validate we've got allocated memory */ if (!block) { - DEBGM(KERN_WARNING "Can't allocate block\n"); + DEBGM("Can't allocate block\n"); return NULL; } @@ -217,25 +221,24 @@ memset(block, 0, sizeof(struct block_s)); /* Make sure we don't overstep our boundaries */ - if (src_ofs >= VMU_NUM_BLOCKS * VMU_BLOCK_SIZE) { - DEBGM(KERN_WARNING "Source offset exceeds total offset\n"); + if (src_ofs >= card->numblocks * card->blocklen) { + DEBGM("Source offset exceeds total offset\n"); kfree(block); return NULL; } /* Find the block number */ - block->num = (unsigned int) (src_ofs / VMU_BLOCK_SIZE); + block->num = (unsigned int) (src_ofs / card->blocklen); /* Validate we've got a valid block */ - if (block->num > VMU_NUM_BLOCKS) { - DEBGM(KERN_WARNING - "Block number exceeds number of blocks\n"); + if (block->num > card->numblocks) { + DEBGM("Block number exceeds number of blocks\n"); kfree(block); return NULL; } /* Calculate remaining offset in block */ - block->ofs = (unsigned int) (src_ofs % VMU_BLOCK_SIZE); + block->ofs = (unsigned int) (src_ofs % card->blocklen); return block; } @@ -256,13 +259,16 @@ { block_t *block; *retval = 0; - u_char *buf = kmalloc(VMU_BLOCK_SIZE, GFP_KERNEL); + struct maple_driver_data *d = + (struct maple_driver_data *) (mtd->priv); + memcard_t *card = (memcard_t *) (d->private_data); + u_char *buf = kmalloc(card->blocklen, GFP_KERNEL); /* Find the block */ - block = __ofs_to_block(ofs); + block = __ofs_to_block(ofs, mtd); /* Validate it was found */ if (!block) { - DEBGM(KERN_WARNING "Got an invalid block\n"); + DEBGM("Got an invalid block\n"); *retval = 1; return -1; } @@ -272,7 +278,7 @@ if (vmu_cache->block == block->num) { if ((vmu_cache->jiffies_atc + 10) >= jiffies) { /* short lifespan */ memcpy(buf, vmu_cache->buffer, - VMU_BLOCK_SIZE); + card->blocklen); u_char *buf2 = buf; buf2 += block->ofs; @@ -290,7 +296,7 @@ /* Read the block */ if (maple_vmu_read_block(block->num, buf, mtd) == -1) { - DEBGM(KERN_WARNING "Can't read block: %d\n", block->num); + DEBGM("Can't read block: %d\n", block->num); kfree(block); *retval = 2; return -1; @@ -320,21 +326,24 @@ struct mtd_info *mtd) { block_t *block; - u_char *buf = kmalloc(VMU_BLOCK_SIZE, GFP_KERNEL); + struct maple_driver_data *dx = + (struct maple_driver_data *) (mtd->priv); + memcard_t *card = (memcard_t *) (dx->private_data); + u_char *buf = kmalloc(card->blocklen, GFP_KERNEL); /* Find the block */ - block = __ofs_to_block(ofs); + block = __ofs_to_block(ofs, mtd); /* Validate it was found */ if (!block) { - DEBGM(KERN_WARNING "Got an invalid block\n"); + DEBGM("Got an invalid block\n"); return; } /* Read the block */ if (maple_vmu_read_block(block->num, buf, mtd)) { - DEBGM(KERN_WARNING "Can't read block: %d\n", block->num); + DEBGM("Can't read block: %d\n", block->num); kfree(block); return; } @@ -361,11 +370,14 @@ { /* printk("Reading from %llx with length %llx\n", from, len); */ + struct maple_driver_data *d = + (struct maple_driver_data *) (mtd->priv); + memcard_t *card = (memcard_t *) (d->private_data); if (len < 1) return -1; - if (len > (VMU_NUM_BLOCKS * VMU_BLOCK_SIZE)) - len = VMU_NUM_BLOCKS * VMU_BLOCK_SIZE; + if (len > (card->numblocks * card->blocklen)) + len = card->numblocks * card->blocklen; int start_here = (int) (from & 0xffffffff); long retval = 0; int index = 0; @@ -383,7 +395,6 @@ } - static int vmu_flash_write(struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, const u_char * buf) { @@ -395,13 +406,17 @@ *retlen = 1; return 0; } + struct maple_driver_data *d = + (struct maple_driver_data *) (mtd->priv); + memcard_t *card = (memcard_t *) (d->private_data); + /* Cached write */ int index = 0; block_t *block; /* Find the block */ - block = __ofs_to_block(from); + block = __ofs_to_block(from, mtd); /* Validate it was found */ if (!block) { @@ -412,12 +427,11 @@ do { - u_char *buffer = kmalloc(VMU_BLOCK_SIZE, GFP_KERNEL); + u_char *buffer = kmalloc(card->blocklen, GFP_KERNEL); /* Read the block */ if (maple_vmu_read_block(block->num, buffer, mtd)) { - DEBGM(KERN_WARNING "Can't read block: %d\n", - block->num); + DEBGM("Can't read block: %d\n", block->num); kfree(block); *retlen = 0; return -1; @@ -429,7 +443,7 @@ index++; if (index >= len) break; - } while (block->ofs < VMU_BLOCK_SIZE); + } while (block->ofs < card->blocklen); maple_vmu_write_block(block->num, buffer, mtd); kfree(buffer); (block->num)++; @@ -439,8 +453,6 @@ kfree(block); return 0; - - } static int vmu_flash_erase(struct mtd_info *mtd, struct erase_info *erase) @@ -474,7 +486,6 @@ } - /** * vmu_flash_map_exit - Cleanup * @@ -487,7 +498,7 @@ maple_unregister_driver(&dc_flashmap_driver); /* See if there's anything to unregister */ if (!vmu_flash_mtd) { - DEBGM(KERN_WARNING "Nothing to unregister\n"); + DEBGM("Nothing to unregister\n"); return; } @@ -498,7 +509,6 @@ /***********Maple functions***************/ - void dc_flashmap_callback(struct maple_driver_data *data) { struct mapleq *mq = &data->mq; @@ -517,6 +527,9 @@ } if (mq->recvbuf[0] == MAPLE_RESPONSE_DATATRF) { + + + memcard_t *card = (memcard_t *) (data->private_data); /* int res = mq->recvbuf[0]; printk @@ -529,11 +542,11 @@ */ block_buffer = kmalloc(VMU_BLOCK_SIZE, GFP_KERNEL); /* Copy over */ - memcpy(block_buffer, mq->recvbuf + 12, VMU_BLOCK_SIZE); + memcpy(block_buffer, mq->recvbuf + 12, card->blocklen); lastmq = mq; wake_up_interruptible(&wq_mq); /* Wake sleeping code */ - memcpy(vmu_cache->buffer, block_buffer, VMU_BLOCK_SIZE); + memcpy(vmu_cache->buffer, block_buffer, card->blocklen); vmu_cache->block = mq->recvbuf[11] & 0xff; vmu_cache->jiffies_atc = jiffies; /* Mark the creation time */ vmu_cache->valid = 1; /* Block is now valid */ @@ -542,8 +555,6 @@ lastmq = mq; } - - static int dc_flashmap_connect(struct maple_driver_data *d) { DEBGM @@ -559,8 +570,7 @@ lastmq = NULL; mqu->sendbuf = mqu->recvbuf; if (maple_add_packet(mqu) != 0) { - DEBGM(KERN_WARNING - "VMU FLASH: Could not add packet to query device\n"); + DEBGM("VMU FLASH: Could not add packet to query device\n"); return -1; } @@ -586,10 +596,8 @@ be32_to_cpu(((unsigned long *) (lastmq->recvbuf))[1 + bit_cnt - controller_here]); - - /* Create a permanent record */ - - /* With thanks to Marcus Comstedt */ + /* Create a permanent record + * With thanks to Marcus Comstedt */ memcard_t *card = kmalloc(sizeof(memcard_t), GFP_KERNEL); card->partitions = ((basic_flash_data >> 24) & 0xff) + 1; @@ -619,8 +627,7 @@ lastmq = NULL; mqu->sendbuf = mqu->recvbuf; if (maple_add_packet(mqu) != 0) { - DEBGM(KERN_WARNING - "VMU FLASH: Could not add packet to query device\n"); + DEBGM("VMU FLASH: Could not add packet to query device\n"); return -1; } @@ -634,10 +641,7 @@ DEBGM(" Flash device has %d blocks available to user\n", user_blocks); DEBGM(" Flash root block is at block %d\n", root_block); - VMU_NUM_BLOCKS = root_block + 1; - - - + card->numblocks = root_block + 1; /* Register the flash with mtd subsystem */ @@ -651,8 +655,8 @@ vmu_flash_mtd->flags = MTD_CLEAR_BITS | MTD_SET_BITS | MTD_ERASEABLE | MTD_WRITEB_WRITEABLE; - vmu_flash_mtd->size = VMU_NUM_BLOCKS * VMU_BLOCK_SIZE; - vmu_flash_mtd->erasesize = VMU_BLOCK_SIZE; + vmu_flash_mtd->size = card->numblocks * card->blocklen; + vmu_flash_mtd->erasesize = card->blocklen; /* Mandatory functions */ vmu_flash_mtd->write = vmu_flash_write; @@ -660,9 +664,7 @@ vmu_flash_mtd->erase = vmu_flash_erase; vmu_flash_mtd->sync = vmu_flash_sync; /* Use private data to point to d */ - vmu_flash_mtd->priv = d; /* TO DO: Fix this */ - - + vmu_flash_mtd->priv = d; vmu_flash_mtd->module = THIS_MODULE; @@ -671,14 +673,14 @@ /* Lock the device in */ vmu_flash_mtd = get_mtd_device(vmu_flash_mtd, -1); if (!vmu_flash_mtd) { - DEBGM(KERN_ERR "VMU Flash driver initialisation failed\n"); + DEBGM("VMU Flash driver initialisation failed\n"); return -1; } card->mtd = vmu_flash_mtd; /* Create the cache */ vmu_cache = kmalloc(64, GFP_KERNEL); - vmu_cache->buffer = kmalloc(VMU_BLOCK_SIZE, GFP_KERNEL); + vmu_cache->buffer = kmalloc(card->blocklen, GFP_KERNEL); vmu_cache->valid = 0; return 0; @@ -693,7 +695,7 @@ /* See if there's anything to unregister */ if (!(card->mtd)) { - DEBGM(KERN_WARNING "Nothing to unregister\n"); + DEBGM("Nothing to unregister\n"); return; } @@ -716,7 +718,6 @@ } - /***********Maple device*****************/ static struct maple_driver dc_flashmap_driver = { function:MAPLE_FUNC_MEMCARD, @@ -727,10 +728,6 @@ }; - - - - module_init(vmu_flash_map_init); |