13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/slab.h>
35 #define SSFDCR_MAJOR 257
36 #define SSFDCR_PARTN_BITS 3
38 #define SECTOR_SIZE 512
39 #define SECTOR_SHIFT 9
42 #define MAX_LOGIC_BLK_PER_ZONE 1000
43 #define MAX_PHYS_BLK_PER_ZONE 1024
45 #define KiB(x) ( (x) * 1024L )
46 #define MiB(x) ( KiB(x) * 1024L )
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 },
77 static int get_chs(
unsigned long size,
unsigned short *
cyl,
unsigned char *
head,
84 while (chs_table[k].size > 0 && size > chs_table[k].size)
87 if (chs_table[k].size > 0) {
89 *cyl = chs_table[
k].
cyl;
91 *head = chs_table[
k].
head;
93 *sec = chs_table[
k].
sec;
101 static const uint8_t cis_numbers[] = {
102 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
106 static int get_valid_cis_sector(
struct mtd_info *mtd)
108 int ret,
k, cis_sector;
124 for (k = 0, offset = 0; k < 4; k++, offset += mtd->
erasesize) {
132 "SSFDC_RO:can't read CIS/IDI sector\n");
133 }
else if (!
memcmp(sect_buf, cis_numbers,
134 sizeof(cis_numbers))) {
138 pr_debug(
"SSFDC_RO: CIS/IDI sector not found"
139 " on %s (mtd%d)\n", mtd->
name,
152 static int read_physical_sector(
struct mtd_info *mtd,
uint8_t *sect_buf,
186 static int get_parity(
int number,
int size)
192 for (k = 0; k <
size; k++) {
193 parity += (number >>
k);
200 static int get_logical_address(
uint8_t *oob_buf)
203 int offset[2] = {6, 11};
213 block_address = ((
int)oob_buf[offset[j]] << 8) |
214 oob_buf[offset[
j]+1];
217 if ((block_address & ~0x7FF) == 0x1000) {
218 parity = block_address & 0x01;
219 block_address &= 0x7FF;
222 if (get_parity(block_address, 10) != parity) {
223 pr_debug(
"SSFDC_RO: logical address field%d"
224 "parity error(0x%04X)\n", j+1,
236 pr_debug(
"SSFDC_RO: get_logical_address() %d\n",
243 static int build_logical_block_map(
struct ssfdcr_record *ssfdc)
250 pr_debug(
"SSFDC_RO: build_block_map() nblks=%d (%luK)\n",
255 for (phys_block = ssfdc->
cis_block + 1; phys_block < ssfdc->map_len;
261 ret = read_raw_oob(mtd, offset, oob_buf);
263 pr_debug(
"SSFDC_RO: mtd read_oob() failed at %lu\n",
267 block_address = get_logical_address(oob_buf);
270 if (block_address >= 0 &&
277 (
unsigned short)phys_block;
279 pr_debug(
"SSFDC_RO: build_block_map() phys_block=%d,"
280 "logic_block_addr=%d, zone=%d\n",
281 phys_block, block_address, zone_index);
298 cis_sector = get_valid_cis_sector(mtd);
299 if (cis_sector == -1)
306 ssfdc->
mbd.mtd = mtd;
307 ssfdc->
mbd.devnum = -1;
309 ssfdc->
mbd.readonly = 1;
315 pr_debug(
"SSFDC_RO: cis_block=%d,erase_size=%d,map_len=%d,n_zones=%d\n",
326 pr_debug(
"SSFDC_RO: using C:%d H:%d S:%d == %ld sects\n",
343 if (build_logical_block_map(ssfdc) < 0)
370 unsigned long logic_sect_no,
char *buf)
376 offset = (
int)(logic_sect_no % sectors_per_block);
377 block_address = (
int)(logic_sect_no / sectors_per_block);
379 pr_debug(
"SSFDC_RO: ssfdcr_readsect(%lu) sec_per_blk=%d, ofst=%d,"
380 " block_addr=%d\n", logic_sect_no, sectors_per_block, offset,
383 if (block_address >= ssfdc->
map_len)
388 pr_debug(
"SSFDC_RO: ssfdcr_readsect() phys_block_addr=%d\n",
391 if (block_address < 0xffff) {
392 unsigned long sect_no;
394 sect_no = (
unsigned long)block_address * sectors_per_block +
397 pr_debug(
"SSFDC_RO: ssfdcr_readsect() phys_sect_no=%lu\n",
400 if (read_physical_sector(ssfdc->
mbd.mtd, buf, sect_no) < 0)
413 pr_debug(
"SSFDC_RO: ssfdcr_getgeo() C=%d, H=%d, S=%d\n",
434 .getgeo = ssfdcr_getgeo,
435 .readsect = ssfdcr_readsect,
436 .add_mtd = ssfdcr_add_mtd,
437 .remove_dev = ssfdcr_remove_dev,
441 static int __init init_ssfdcr(
void)
448 static void __exit cleanup_ssfdcr(
void)