Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Linux driver for SSFDC Flash Translation Layer (Read only) |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 3 | * © 2005 Eptar srl |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 4 | * Author: Claudio Lanconelli <lanconelli.claudio@eptar.com> |
| 5 | * |
| 6 | * Based on NTFL and MTDBLOCK_RO drivers |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/hdreg.h> |
| 18 | #include <linux/mtd/mtd.h> |
| 19 | #include <linux/mtd/nand.h> |
| 20 | #include <linux/mtd/blktrans.h> |
| 21 | |
| 22 | struct ssfdcr_record { |
| 23 | struct mtd_blktrans_dev mbd; |
| 24 | int usecount; |
| 25 | unsigned char heads; |
| 26 | unsigned char sectors; |
| 27 | unsigned short cylinders; |
| 28 | int cis_block; /* block n. containing CIS/IDI */ |
| 29 | int erase_size; /* phys_block_size */ |
| 30 | unsigned short *logic_block_map; /* all zones (max 8192 phys blocks on |
David Woodhouse | 9a05ede | 2006-09-23 10:56:24 +0100 | [diff] [blame] | 31 | the 128MiB) */ |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 32 | int map_len; /* n. phys_blocks on the card */ |
| 33 | }; |
| 34 | |
| 35 | #define SSFDCR_MAJOR 257 |
| 36 | #define SSFDCR_PARTN_BITS 3 |
| 37 | |
| 38 | #define SECTOR_SIZE 512 |
| 39 | #define SECTOR_SHIFT 9 |
| 40 | #define OOB_SIZE 16 |
| 41 | |
| 42 | #define MAX_LOGIC_BLK_PER_ZONE 1000 |
| 43 | #define MAX_PHYS_BLK_PER_ZONE 1024 |
| 44 | |
David Woodhouse | 9a05ede | 2006-09-23 10:56:24 +0100 | [diff] [blame] | 45 | #define KiB(x) ( (x) * 1024L ) |
| 46 | #define MiB(x) ( KiB(x) * 1024L ) |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 47 | |
| 48 | /** CHS Table |
David Woodhouse | 9a05ede | 2006-09-23 10:56:24 +0100 | [diff] [blame] | 49 | 1MiB 2MiB 4MiB 8MiB 16MiB 32MiB 64MiB 128MiB |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 50 | NCylinder 125 125 250 250 500 500 500 500 |
| 51 | NHead 4 4 4 4 4 8 8 16 |
| 52 | NSector 4 8 8 16 16 16 32 32 |
| 53 | SumSector 2,000 4,000 8,000 16,000 32,000 64,000 128,000 256,000 |
| 54 | SectorSize 512 512 512 512 512 512 512 512 |
| 55 | **/ |
| 56 | |
| 57 | typedef struct { |
| 58 | unsigned long size; |
| 59 | unsigned short cyl; |
| 60 | unsigned char head; |
| 61 | unsigned char sec; |
| 62 | } chs_entry_t; |
| 63 | |
| 64 | /* Must be ordered by size */ |
| 65 | static const chs_entry_t chs_table[] = { |
David Woodhouse | 9a05ede | 2006-09-23 10:56:24 +0100 | [diff] [blame] | 66 | { MiB( 1), 125, 4, 4 }, |
| 67 | { MiB( 2), 125, 4, 8 }, |
| 68 | { MiB( 4), 250, 4, 8 }, |
| 69 | { MiB( 8), 250, 4, 16 }, |
| 70 | { MiB( 16), 500, 4, 16 }, |
| 71 | { MiB( 32), 500, 8, 16 }, |
| 72 | { MiB( 64), 500, 8, 32 }, |
| 73 | { MiB(128), 500, 16, 32 }, |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 74 | { 0 }, |
| 75 | }; |
| 76 | |
| 77 | static int get_chs(unsigned long size, unsigned short *cyl, unsigned char *head, |
| 78 | unsigned char *sec) |
| 79 | { |
| 80 | int k; |
| 81 | int found = 0; |
| 82 | |
| 83 | k = 0; |
| 84 | while (chs_table[k].size > 0 && size > chs_table[k].size) |
| 85 | k++; |
| 86 | |
| 87 | if (chs_table[k].size > 0) { |
| 88 | if (cyl) |
| 89 | *cyl = chs_table[k].cyl; |
| 90 | if (head) |
| 91 | *head = chs_table[k].head; |
| 92 | if (sec) |
| 93 | *sec = chs_table[k].sec; |
| 94 | found = 1; |
| 95 | } |
| 96 | |
| 97 | return found; |
| 98 | } |
| 99 | |
| 100 | /* These bytes are the signature for the CIS/IDI sector */ |
| 101 | static const uint8_t cis_numbers[] = { |
| 102 | 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20 |
| 103 | }; |
| 104 | |
| 105 | /* Read and check for a valid CIS sector */ |
| 106 | static int get_valid_cis_sector(struct mtd_info *mtd) |
| 107 | { |
| 108 | int ret, k, cis_sector; |
| 109 | size_t retlen; |
| 110 | loff_t offset; |
David Woodhouse | 9a05ede | 2006-09-23 10:56:24 +0100 | [diff] [blame] | 111 | uint8_t *sect_buf; |
| 112 | |
| 113 | cis_sector = -1; |
| 114 | |
| 115 | sect_buf = kmalloc(SECTOR_SIZE, GFP_KERNEL); |
| 116 | if (!sect_buf) |
| 117 | goto out; |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * Look for CIS/IDI sector on the first GOOD block (give up after 4 bad |
| 121 | * blocks). If the first good block doesn't contain CIS number the flash |
| 122 | * is not SSFDC formatted |
| 123 | */ |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 124 | for (k = 0, offset = 0; k < 4; k++, offset += mtd->erasesize) { |
Artem Bityutskiy | 7086c19 | 2011-12-23 19:35:30 +0200 | [diff] [blame] | 125 | if (mtd_block_isbad(mtd, offset)) { |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 126 | ret = mtd_read(mtd, offset, SECTOR_SIZE, &retlen, |
| 127 | sect_buf); |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 128 | |
| 129 | /* CIS pattern match on the sector buffer */ |
David Woodhouse | 08d3ad6 | 2006-09-23 16:20:48 +0100 | [diff] [blame] | 130 | if (ret < 0 || retlen != SECTOR_SIZE) { |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 131 | printk(KERN_WARNING |
| 132 | "SSFDC_RO:can't read CIS/IDI sector\n"); |
David Woodhouse | 08d3ad6 | 2006-09-23 16:20:48 +0100 | [diff] [blame] | 133 | } else if (!memcmp(sect_buf, cis_numbers, |
| 134 | sizeof(cis_numbers))) { |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 135 | /* Found */ |
| 136 | cis_sector = (int)(offset >> SECTOR_SHIFT); |
| 137 | } else { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 138 | pr_debug("SSFDC_RO: CIS/IDI sector not found" |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 139 | " on %s (mtd%d)\n", mtd->name, |
| 140 | mtd->index); |
| 141 | } |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
David Woodhouse | 9a05ede | 2006-09-23 10:56:24 +0100 | [diff] [blame] | 146 | kfree(sect_buf); |
| 147 | out: |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 148 | return cis_sector; |
| 149 | } |
| 150 | |
| 151 | /* Read physical sector (wrapper to MTD_READ) */ |
| 152 | static int read_physical_sector(struct mtd_info *mtd, uint8_t *sect_buf, |
| 153 | int sect_no) |
| 154 | { |
| 155 | int ret; |
| 156 | size_t retlen; |
| 157 | loff_t offset = (loff_t)sect_no << SECTOR_SHIFT; |
| 158 | |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 159 | ret = mtd_read(mtd, offset, SECTOR_SIZE, &retlen, sect_buf); |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 160 | if (ret < 0 || retlen != SECTOR_SIZE) |
| 161 | return -1; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* Read redundancy area (wrapper to MTD_READ_OOB */ |
| 167 | static int read_raw_oob(struct mtd_info *mtd, loff_t offs, uint8_t *buf) |
| 168 | { |
| 169 | struct mtd_oob_ops ops; |
| 170 | int ret; |
| 171 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 172 | ops.mode = MTD_OPS_RAW; |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 173 | ops.ooboffs = 0; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 174 | ops.ooblen = OOB_SIZE; |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 175 | ops.oobbuf = buf; |
| 176 | ops.datbuf = NULL; |
| 177 | |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 178 | ret = mtd_read_oob(mtd, offs, &ops); |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 179 | if (ret < 0 || ops.oobretlen != OOB_SIZE) |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 180 | return -1; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /* Parity calculator on a word of n bit size */ |
| 186 | static int get_parity(int number, int size) |
| 187 | { |
| 188 | int k; |
| 189 | int parity; |
| 190 | |
| 191 | parity = 1; |
| 192 | for (k = 0; k < size; k++) { |
| 193 | parity += (number >> k); |
| 194 | parity &= 1; |
| 195 | } |
| 196 | return parity; |
| 197 | } |
| 198 | |
| 199 | /* Read and validate the logical block address field stored in the OOB */ |
| 200 | static int get_logical_address(uint8_t *oob_buf) |
| 201 | { |
| 202 | int block_address, parity; |
| 203 | int offset[2] = {6, 11}; /* offset of the 2 address fields within OOB */ |
| 204 | int j; |
| 205 | int ok = 0; |
| 206 | |
| 207 | /* |
| 208 | * Look for the first valid logical address |
| 209 | * Valid address has fixed pattern on most significant bits and |
| 210 | * parity check |
| 211 | */ |
| 212 | for (j = 0; j < ARRAY_SIZE(offset); j++) { |
| 213 | block_address = ((int)oob_buf[offset[j]] << 8) | |
| 214 | oob_buf[offset[j]+1]; |
| 215 | |
| 216 | /* Check for the signature bits in the address field (MSBits) */ |
| 217 | if ((block_address & ~0x7FF) == 0x1000) { |
| 218 | parity = block_address & 0x01; |
| 219 | block_address &= 0x7FF; |
| 220 | block_address >>= 1; |
| 221 | |
| 222 | if (get_parity(block_address, 10) != parity) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 223 | pr_debug("SSFDC_RO: logical address field%d" |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 224 | "parity error(0x%04X)\n", j+1, |
| 225 | block_address); |
| 226 | } else { |
| 227 | ok = 1; |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
David Woodhouse | 08d3ad6 | 2006-09-23 16:20:48 +0100 | [diff] [blame] | 233 | if (!ok) |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 234 | block_address = -2; |
| 235 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 236 | pr_debug("SSFDC_RO: get_logical_address() %d\n", |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 237 | block_address); |
| 238 | |
| 239 | return block_address; |
| 240 | } |
| 241 | |
| 242 | /* Build the logic block map */ |
| 243 | static int build_logical_block_map(struct ssfdcr_record *ssfdc) |
| 244 | { |
| 245 | unsigned long offset; |
| 246 | uint8_t oob_buf[OOB_SIZE]; |
| 247 | int ret, block_address, phys_block; |
| 248 | struct mtd_info *mtd = ssfdc->mbd.mtd; |
| 249 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 250 | pr_debug("SSFDC_RO: build_block_map() nblks=%d (%luK)\n", |
David Woodhouse | 08d3ad6 | 2006-09-23 16:20:48 +0100 | [diff] [blame] | 251 | ssfdc->map_len, |
| 252 | (unsigned long)ssfdc->map_len * ssfdc->erase_size / 1024); |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 253 | |
| 254 | /* Scan every physical block, skip CIS block */ |
| 255 | for (phys_block = ssfdc->cis_block + 1; phys_block < ssfdc->map_len; |
| 256 | phys_block++) { |
| 257 | offset = (unsigned long)phys_block * ssfdc->erase_size; |
Artem Bityutskiy | 7086c19 | 2011-12-23 19:35:30 +0200 | [diff] [blame] | 258 | if (mtd_block_isbad(mtd, offset)) |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 259 | continue; /* skip bad blocks */ |
| 260 | |
| 261 | ret = read_raw_oob(mtd, offset, oob_buf); |
| 262 | if (ret < 0) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 263 | pr_debug("SSFDC_RO: mtd read_oob() failed at %lu\n", |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 264 | offset); |
| 265 | return -1; |
| 266 | } |
| 267 | block_address = get_logical_address(oob_buf); |
| 268 | |
| 269 | /* Skip invalid addresses */ |
| 270 | if (block_address >= 0 && |
| 271 | block_address < MAX_LOGIC_BLK_PER_ZONE) { |
| 272 | int zone_index; |
| 273 | |
| 274 | zone_index = phys_block / MAX_PHYS_BLK_PER_ZONE; |
| 275 | block_address += zone_index * MAX_LOGIC_BLK_PER_ZONE; |
| 276 | ssfdc->logic_block_map[block_address] = |
| 277 | (unsigned short)phys_block; |
| 278 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 279 | pr_debug("SSFDC_RO: build_block_map() phys_block=%d," |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 280 | "logic_block_addr=%d, zone=%d\n", |
| 281 | phys_block, block_address, zone_index); |
| 282 | } |
| 283 | } |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static void ssfdcr_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) |
| 288 | { |
| 289 | struct ssfdcr_record *ssfdc; |
| 290 | int cis_sector; |
| 291 | |
| 292 | /* Check for small page NAND flash */ |
Huang Shijie | 818b973 | 2013-09-25 14:58:17 +0800 | [diff] [blame] | 293 | if (!mtd_type_is_nand(mtd) || mtd->oobsize != OOB_SIZE || |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 294 | mtd->size > UINT_MAX) |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 295 | return; |
| 296 | |
| 297 | /* Check for SSDFC format by reading CIS/IDI sector */ |
| 298 | cis_sector = get_valid_cis_sector(mtd); |
| 299 | if (cis_sector == -1) |
| 300 | return; |
| 301 | |
| 302 | ssfdc = kzalloc(sizeof(struct ssfdcr_record), GFP_KERNEL); |
Brian Norris | 0870066 | 2011-06-07 16:01:54 -0700 | [diff] [blame] | 303 | if (!ssfdc) |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 304 | return; |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 305 | |
| 306 | ssfdc->mbd.mtd = mtd; |
| 307 | ssfdc->mbd.devnum = -1; |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 308 | ssfdc->mbd.tr = tr; |
| 309 | ssfdc->mbd.readonly = 1; |
| 310 | |
| 311 | ssfdc->cis_block = cis_sector / (mtd->erasesize >> SECTOR_SHIFT); |
| 312 | ssfdc->erase_size = mtd->erasesize; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 313 | ssfdc->map_len = (u32)mtd->size / mtd->erasesize; |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 314 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 315 | pr_debug("SSFDC_RO: cis_block=%d,erase_size=%d,map_len=%d,n_zones=%d\n", |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 316 | ssfdc->cis_block, ssfdc->erase_size, ssfdc->map_len, |
Julia Lawall | c8872b0 | 2008-08-02 17:14:21 +0200 | [diff] [blame] | 317 | DIV_ROUND_UP(ssfdc->map_len, MAX_PHYS_BLK_PER_ZONE)); |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 318 | |
| 319 | /* Set geometry */ |
| 320 | ssfdc->heads = 16; |
| 321 | ssfdc->sectors = 32; |
David Woodhouse | 08d3ad6 | 2006-09-23 16:20:48 +0100 | [diff] [blame] | 322 | get_chs(mtd->size, NULL, &ssfdc->heads, &ssfdc->sectors); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 323 | ssfdc->cylinders = (unsigned short)(((u32)mtd->size >> SECTOR_SHIFT) / |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 324 | ((long)ssfdc->sectors * (long)ssfdc->heads)); |
| 325 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 326 | pr_debug("SSFDC_RO: using C:%d H:%d S:%d == %ld sects\n", |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 327 | ssfdc->cylinders, ssfdc->heads , ssfdc->sectors, |
| 328 | (long)ssfdc->cylinders * (long)ssfdc->heads * |
David Woodhouse | 08d3ad6 | 2006-09-23 16:20:48 +0100 | [diff] [blame] | 329 | (long)ssfdc->sectors); |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 330 | |
| 331 | ssfdc->mbd.size = (long)ssfdc->heads * (long)ssfdc->cylinders * |
| 332 | (long)ssfdc->sectors; |
| 333 | |
| 334 | /* Allocate logical block map */ |
David Woodhouse | 08d3ad6 | 2006-09-23 16:20:48 +0100 | [diff] [blame] | 335 | ssfdc->logic_block_map = kmalloc(sizeof(ssfdc->logic_block_map[0]) * |
| 336 | ssfdc->map_len, GFP_KERNEL); |
Brian Norris | 0870066 | 2011-06-07 16:01:54 -0700 | [diff] [blame] | 337 | if (!ssfdc->logic_block_map) |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 338 | goto out_err; |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 339 | memset(ssfdc->logic_block_map, 0xff, sizeof(ssfdc->logic_block_map[0]) * |
| 340 | ssfdc->map_len); |
| 341 | |
| 342 | /* Build logical block map */ |
| 343 | if (build_logical_block_map(ssfdc) < 0) |
| 344 | goto out_err; |
| 345 | |
| 346 | /* Register device + partitions */ |
| 347 | if (add_mtd_blktrans_dev(&ssfdc->mbd)) |
| 348 | goto out_err; |
| 349 | |
| 350 | printk(KERN_INFO "SSFDC_RO: Found ssfdc%c on mtd%d (%s)\n", |
| 351 | ssfdc->mbd.devnum + 'a', mtd->index, mtd->name); |
| 352 | return; |
| 353 | |
| 354 | out_err: |
| 355 | kfree(ssfdc->logic_block_map); |
| 356 | kfree(ssfdc); |
| 357 | } |
| 358 | |
| 359 | static void ssfdcr_remove_dev(struct mtd_blktrans_dev *dev) |
| 360 | { |
| 361 | struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev; |
| 362 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 363 | pr_debug("SSFDC_RO: remove_dev (i=%d)\n", dev->devnum); |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 364 | |
| 365 | del_mtd_blktrans_dev(dev); |
| 366 | kfree(ssfdc->logic_block_map); |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static int ssfdcr_readsect(struct mtd_blktrans_dev *dev, |
| 370 | unsigned long logic_sect_no, char *buf) |
| 371 | { |
| 372 | struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev; |
| 373 | int sectors_per_block, offset, block_address; |
| 374 | |
| 375 | sectors_per_block = ssfdc->erase_size >> SECTOR_SHIFT; |
| 376 | offset = (int)(logic_sect_no % sectors_per_block); |
| 377 | block_address = (int)(logic_sect_no / sectors_per_block); |
| 378 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 379 | pr_debug("SSFDC_RO: ssfdcr_readsect(%lu) sec_per_blk=%d, ofst=%d," |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 380 | " block_addr=%d\n", logic_sect_no, sectors_per_block, offset, |
| 381 | block_address); |
| 382 | |
| 383 | if (block_address >= ssfdc->map_len) |
| 384 | BUG(); |
| 385 | |
| 386 | block_address = ssfdc->logic_block_map[block_address]; |
| 387 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 388 | pr_debug("SSFDC_RO: ssfdcr_readsect() phys_block_addr=%d\n", |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 389 | block_address); |
| 390 | |
| 391 | if (block_address < 0xffff) { |
| 392 | unsigned long sect_no; |
| 393 | |
| 394 | sect_no = (unsigned long)block_address * sectors_per_block + |
| 395 | offset; |
| 396 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 397 | pr_debug("SSFDC_RO: ssfdcr_readsect() phys_sect_no=%lu\n", |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 398 | sect_no); |
| 399 | |
David Woodhouse | 08d3ad6 | 2006-09-23 16:20:48 +0100 | [diff] [blame] | 400 | if (read_physical_sector(ssfdc->mbd.mtd, buf, sect_no) < 0) |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 401 | return -EIO; |
| 402 | } else { |
| 403 | memset(buf, 0xff, SECTOR_SIZE); |
| 404 | } |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | static int ssfdcr_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo) |
| 410 | { |
| 411 | struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev; |
| 412 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 413 | pr_debug("SSFDC_RO: ssfdcr_getgeo() C=%d, H=%d, S=%d\n", |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 414 | ssfdc->cylinders, ssfdc->heads, ssfdc->sectors); |
| 415 | |
| 416 | geo->heads = ssfdc->heads; |
| 417 | geo->sectors = ssfdc->sectors; |
| 418 | geo->cylinders = ssfdc->cylinders; |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | /**************************************************************************** |
| 424 | * |
| 425 | * Module stuff |
| 426 | * |
| 427 | ****************************************************************************/ |
| 428 | |
| 429 | static struct mtd_blktrans_ops ssfdcr_tr = { |
| 430 | .name = "ssfdc", |
| 431 | .major = SSFDCR_MAJOR, |
| 432 | .part_bits = SSFDCR_PARTN_BITS, |
David Woodhouse | 76ab40e | 2006-12-11 09:43:38 +0000 | [diff] [blame] | 433 | .blksize = SECTOR_SIZE, |
Claudio Lanconelli | 51197ab | 2006-09-22 11:01:37 +0100 | [diff] [blame] | 434 | .getgeo = ssfdcr_getgeo, |
| 435 | .readsect = ssfdcr_readsect, |
| 436 | .add_mtd = ssfdcr_add_mtd, |
| 437 | .remove_dev = ssfdcr_remove_dev, |
| 438 | .owner = THIS_MODULE, |
| 439 | }; |
| 440 | |
| 441 | static int __init init_ssfdcr(void) |
| 442 | { |
| 443 | printk(KERN_INFO "SSFDC read-only Flash Translation layer\n"); |
| 444 | |
| 445 | return register_mtd_blktrans(&ssfdcr_tr); |
| 446 | } |
| 447 | |
| 448 | static void __exit cleanup_ssfdcr(void) |
| 449 | { |
| 450 | deregister_mtd_blktrans(&ssfdcr_tr); |
| 451 | } |
| 452 | |
| 453 | module_init(init_ssfdcr); |
| 454 | module_exit(cleanup_ssfdcr); |
| 455 | |
| 456 | MODULE_LICENSE("GPL"); |
| 457 | MODULE_AUTHOR("Claudio Lanconelli <lanconelli.claudio@eptar.com>"); |
| 458 | MODULE_DESCRIPTION("Flash Translation Layer for read-only SSFDC SmartMedia card"); |