Merge branch 'for-linus' of git://git390.osdl.marist.edu/pub/scm/linux-2.6
[cascardo/linux.git] / drivers / usb / storage / sddr09.c
index 760fe93..b12202c 100644 (file)
@@ -41,7 +41,6 @@
  * EF: compute checksum (?)
  */
 
-#include <linux/sched.h>
 #include <linux/errno.h>
 #include <linux/slab.h>
 
@@ -133,13 +132,11 @@ static struct nand_flash_dev nand_flash_ids[] = {
        { 0,}
 };
 
-#define SIZE(a)        (sizeof(a)/sizeof((a)[0]))
-
 static struct nand_flash_dev *
 nand_find_id(unsigned char id) {
        int i;
 
-       for (i = 0; i < SIZE(nand_flash_ids); i++)
+       for (i = 0; i < ARRAY_SIZE(nand_flash_ids); i++)
                if (nand_flash_ids[i].model_id == id)
                        return &(nand_flash_ids[i]);
        return NULL;
@@ -648,7 +645,7 @@ sddr09_read_sg_test_only(struct us_data *us) {
                return result;
        }
 
-       buf = (unsigned char *) kmalloc(bulklen, GFP_NOIO);
+       buf = kmalloc(bulklen, GFP_NOIO);
        if (!buf)
                return -ENOMEM;
 
@@ -708,9 +705,17 @@ sddr09_read_data(struct us_data *us,
        unsigned char *buffer;
        unsigned int lba, maxlba, pba;
        unsigned int page, pages;
-       unsigned int len, index, offset;
+       unsigned int len, offset;
+       struct scatterlist *sg;
        int result;
 
+       // Figure out the initial LBA and page
+       lba = address >> info->blockshift;
+       page = (address & info->blockmask);
+       maxlba = info->capacity >> (info->pageshift + info->blockshift);
+       if (lba >= maxlba)
+               return -EIO;
+
        // Since we only read in one block at a time, we have to create
        // a bounce buffer and move the data a piece at a time between the
        // bounce buffer and the actual transfer buffer.
@@ -722,16 +727,12 @@ sddr09_read_data(struct us_data *us,
                return -ENOMEM;
        }
 
-       // Figure out the initial LBA and page
-       lba = address >> info->blockshift;
-       page = (address & info->blockmask);
-       maxlba = info->capacity >> (info->pageshift + info->blockshift);
-
        // This could be made much more efficient by checking for
        // contiguous LBA's. Another exercise left to the student.
 
        result = 0;
-       index = offset = 0;
+       offset = 0;
+       sg = NULL;
 
        while (sectors > 0) {
 
@@ -778,7 +779,7 @@ sddr09_read_data(struct us_data *us,
 
                // Store the data in the transfer buffer
                usb_stor_access_xfer_buf(buffer, len, us->srb,
-                               &index, &offset, TO_XFER_BUF);
+                               &sg, &offset, TO_XFER_BUF);
 
                page = 0;
                lba++;
@@ -928,13 +929,21 @@ sddr09_write_data(struct us_data *us,
                  unsigned int sectors) {
 
        struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
-       unsigned int lba, page, pages;
+       unsigned int lba, maxlba, page, pages;
        unsigned int pagelen, blocklen;
        unsigned char *blockbuffer;
        unsigned char *buffer;
-       unsigned int len, index, offset;
+       unsigned int len, offset;
+       struct scatterlist *sg;
        int result;
 
+       // Figure out the initial LBA and page
+       lba = address >> info->blockshift;
+       page = (address & info->blockmask);
+       maxlba = info->capacity >> (info->pageshift + info->blockshift);
+       if (lba >= maxlba)
+               return -EIO;
+
        // blockbuffer is used for reading in the old data, overwriting
        // with the new data, and performing ECC calculations
 
@@ -961,12 +970,9 @@ sddr09_write_data(struct us_data *us,
                return -ENOMEM;
        }
 
-       // Figure out the initial LBA and page
-       lba = address >> info->blockshift;
-       page = (address & info->blockmask);
-
        result = 0;
-       index = offset = 0;
+       offset = 0;
+       sg = NULL;
 
        while (sectors > 0) {
 
@@ -975,9 +981,17 @@ sddr09_write_data(struct us_data *us,
                pages = min(sectors, info->blocksize - page);
                len = (pages << info->pageshift);
 
+               /* Not overflowing capacity? */
+               if (lba >= maxlba) {
+                       US_DEBUGP("Error: Requested lba %u exceeds "
+                                 "maximum %u\n", lba, maxlba);
+                       result = -EIO;
+                       break;
+               }
+
                // Get the data from the transfer buffer
                usb_stor_access_xfer_buf(buffer, len, us->srb,
-                               &index, &offset, FROM_XFER_BUF);
+                               &sg, &offset, FROM_XFER_BUF);
 
                result = sddr09_write_lba(us, lba, page, pages,
                                buffer, blockbuffer);