xref: /openbmc/linux/drivers/usb/storage/ene_ub6250.c (revision 36f3a14d)
141e568d1Shuajun li /*
241e568d1Shuajun li  *
341e568d1Shuajun li  * This program is free software; you can redistribute it and/or modify it
441e568d1Shuajun li  * under the terms of the GNU General Public License as published by the
541e568d1Shuajun li  * Free Software Foundation; either version 2, or (at your option) any
641e568d1Shuajun li  * later version.
741e568d1Shuajun li  *
841e568d1Shuajun li  * This program is distributed in the hope that it will be useful, but
941e568d1Shuajun li  * WITHOUT ANY WARRANTY; without even the implied warranty of
1041e568d1Shuajun li  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1141e568d1Shuajun li  * General Public License for more details.
1241e568d1Shuajun li  *
1341e568d1Shuajun li  * You should have received a copy of the GNU General Public License along
1441e568d1Shuajun li  * with this program; if not, write to the Free Software Foundation, Inc.,
1541e568d1Shuajun li  * 675 Mass Ave, Cambridge, MA 02139, USA.
1641e568d1Shuajun li  */
1741e568d1Shuajun li #include <linux/jiffies.h>
1841e568d1Shuajun li #include <linux/errno.h>
1941e568d1Shuajun li #include <linux/module.h>
2041e568d1Shuajun li #include <linux/slab.h>
2141e568d1Shuajun li 
2241e568d1Shuajun li #include <scsi/scsi.h>
2341e568d1Shuajun li #include <scsi/scsi_cmnd.h>
2441e568d1Shuajun li 
2541e568d1Shuajun li #include <linux/firmware.h>
2641e568d1Shuajun li 
2741e568d1Shuajun li #include "usb.h"
2841e568d1Shuajun li #include "transport.h"
2941e568d1Shuajun li #include "protocol.h"
3041e568d1Shuajun li #include "debug.h"
3141e568d1Shuajun li 
3241e568d1Shuajun li MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
3341e568d1Shuajun li MODULE_LICENSE("GPL");
3441e568d1Shuajun li 
3541e568d1Shuajun li 
3641e568d1Shuajun li /*
3741e568d1Shuajun li  * The table of devices
3841e568d1Shuajun li  */
3941e568d1Shuajun li #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
4041e568d1Shuajun li 		    vendorName, productName, useProtocol, useTransport, \
4141e568d1Shuajun li 		    initFunction, flags) \
4241e568d1Shuajun li { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
4341e568d1Shuajun li 	.driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
4441e568d1Shuajun li 
4536f3a14dSFelipe Balbi static struct usb_device_id ene_ub6250_usb_ids[] = {
4641e568d1Shuajun li #	include "unusual_ene_ub6250.h"
4741e568d1Shuajun li 	{ }		/* Terminating entry */
4841e568d1Shuajun li };
4941e568d1Shuajun li MODULE_DEVICE_TABLE(usb, ene_ub6250_usb_ids);
5041e568d1Shuajun li 
5141e568d1Shuajun li #undef UNUSUAL_DEV
5241e568d1Shuajun li 
5341e568d1Shuajun li /*
5441e568d1Shuajun li  * The flags table
5541e568d1Shuajun li  */
5641e568d1Shuajun li #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
5741e568d1Shuajun li 		    vendor_name, product_name, use_protocol, use_transport, \
5841e568d1Shuajun li 		    init_function, Flags) \
5941e568d1Shuajun li { \
6041e568d1Shuajun li 	.vendorName = vendor_name,	\
6141e568d1Shuajun li 	.productName = product_name,	\
6241e568d1Shuajun li 	.useProtocol = use_protocol,	\
6341e568d1Shuajun li 	.useTransport = use_transport,	\
6441e568d1Shuajun li 	.initFunction = init_function,	\
6541e568d1Shuajun li }
6641e568d1Shuajun li 
6741e568d1Shuajun li static struct us_unusual_dev ene_ub6250_unusual_dev_list[] = {
6841e568d1Shuajun li #	include "unusual_ene_ub6250.h"
6941e568d1Shuajun li 	{ }		/* Terminating entry */
7041e568d1Shuajun li };
7141e568d1Shuajun li 
7241e568d1Shuajun li #undef UNUSUAL_DEV
7341e568d1Shuajun li 
7441e568d1Shuajun li 
7541e568d1Shuajun li 
7641e568d1Shuajun li /* ENE bin code len */
7741e568d1Shuajun li #define ENE_BIN_CODE_LEN    0x800
7841e568d1Shuajun li /* EnE HW Register */
7941e568d1Shuajun li #define REG_CARD_STATUS     0xFF83
8041e568d1Shuajun li #define REG_HW_TRAP1        0xFF89
8141e568d1Shuajun li 
8241e568d1Shuajun li /* SRB Status */
8341e568d1Shuajun li #define SS_SUCCESS                  0x00      /* No Sense */
8441e568d1Shuajun li #define SS_NOT_READY                0x02
8541e568d1Shuajun li #define SS_MEDIUM_ERR               0x03
8641e568d1Shuajun li #define SS_HW_ERR                   0x04
8741e568d1Shuajun li #define SS_ILLEGAL_REQUEST          0x05
8841e568d1Shuajun li #define SS_UNIT_ATTENTION           0x06
8941e568d1Shuajun li 
9041e568d1Shuajun li /* ENE Load FW Pattern */
9141e568d1Shuajun li #define SD_INIT1_PATTERN   1
9241e568d1Shuajun li #define SD_INIT2_PATTERN   2
9341e568d1Shuajun li #define SD_RW_PATTERN      3
9441e568d1Shuajun li #define MS_INIT_PATTERN    4
9541e568d1Shuajun li #define MSP_RW_PATTERN     5
9641e568d1Shuajun li #define MS_RW_PATTERN      6
9741e568d1Shuajun li #define SM_INIT_PATTERN    7
9841e568d1Shuajun li #define SM_RW_PATTERN      8
9941e568d1Shuajun li 
10041e568d1Shuajun li #define FDIR_WRITE         0
10141e568d1Shuajun li #define FDIR_READ          1
10241e568d1Shuajun li 
10333842cedSCho, Yu-Chen /* For MS Card */
10433842cedSCho, Yu-Chen 
10533842cedSCho, Yu-Chen /* Status Register 1 */
10633842cedSCho, Yu-Chen #define MS_REG_ST1_MB           0x80    /* media busy */
10733842cedSCho, Yu-Chen #define MS_REG_ST1_FB1          0x40    /* flush busy 1 */
10833842cedSCho, Yu-Chen #define MS_REG_ST1_DTER         0x20    /* error on data(corrected) */
10933842cedSCho, Yu-Chen #define MS_REG_ST1_UCDT         0x10    /* unable to correct data */
11033842cedSCho, Yu-Chen #define MS_REG_ST1_EXER         0x08    /* error on extra(corrected) */
11133842cedSCho, Yu-Chen #define MS_REG_ST1_UCEX         0x04    /* unable to correct extra */
11233842cedSCho, Yu-Chen #define MS_REG_ST1_FGER         0x02    /* error on overwrite flag(corrected) */
11333842cedSCho, Yu-Chen #define MS_REG_ST1_UCFG         0x01    /* unable to correct overwrite flag */
11433842cedSCho, Yu-Chen #define MS_REG_ST1_DEFAULT	(MS_REG_ST1_MB | MS_REG_ST1_FB1 | MS_REG_ST1_DTER | MS_REG_ST1_UCDT | MS_REG_ST1_EXER | MS_REG_ST1_UCEX | MS_REG_ST1_FGER | MS_REG_ST1_UCFG)
11533842cedSCho, Yu-Chen 
11633842cedSCho, Yu-Chen /* Overwrite Area */
11733842cedSCho, Yu-Chen #define MS_REG_OVR_BKST		0x80            /* block status */
11833842cedSCho, Yu-Chen #define MS_REG_OVR_BKST_OK	MS_REG_OVR_BKST     /* OK */
11933842cedSCho, Yu-Chen #define MS_REG_OVR_BKST_NG	0x00            /* NG */
12033842cedSCho, Yu-Chen #define MS_REG_OVR_PGST0	0x40            /* page status */
12133842cedSCho, Yu-Chen #define MS_REG_OVR_PGST1	0x20
12233842cedSCho, Yu-Chen #define MS_REG_OVR_PGST_MASK	(MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1)
12333842cedSCho, Yu-Chen #define MS_REG_OVR_PGST_OK	(MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1) /* OK */
12433842cedSCho, Yu-Chen #define MS_REG_OVR_PGST_NG	MS_REG_OVR_PGST1                      /* NG */
12533842cedSCho, Yu-Chen #define MS_REG_OVR_PGST_DATA_ERROR	0x00        /* data error */
12633842cedSCho, Yu-Chen #define MS_REG_OVR_UDST			0x10        /* update status */
12733842cedSCho, Yu-Chen #define MS_REG_OVR_UDST_UPDATING	0x00        /* updating */
12833842cedSCho, Yu-Chen #define MS_REG_OVR_UDST_NO_UPDATE	MS_REG_OVR_UDST
12933842cedSCho, Yu-Chen #define MS_REG_OVR_RESERVED	0x08
13033842cedSCho, Yu-Chen #define MS_REG_OVR_DEFAULT	(MS_REG_OVR_BKST_OK | MS_REG_OVR_PGST_OK | MS_REG_OVR_UDST_NO_UPDATE | MS_REG_OVR_RESERVED)
13133842cedSCho, Yu-Chen 
13233842cedSCho, Yu-Chen /* Management Flag */
13333842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS0	0x20    /* serial copy management system */
13433842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS1	0x10
13533842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS_MASK		(MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
13633842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS_COPY_OK		(MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
13733842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS_ONE_COPY	MS_REG_MNG_SCMS1
13833842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS_NO_COPY	0x00
13933842cedSCho, Yu-Chen #define MS_REG_MNG_ATFLG	0x08    /* address transfer table flag */
14033842cedSCho, Yu-Chen #define MS_REG_MNG_ATFLG_OTHER	MS_REG_MNG_ATFLG    /* other */
14133842cedSCho, Yu-Chen #define MS_REG_MNG_ATFLG_ATTBL	0x00	/* address transfer table */
14233842cedSCho, Yu-Chen #define MS_REG_MNG_SYSFLG	0x04	/* system flag */
14333842cedSCho, Yu-Chen #define MS_REG_MNG_SYSFLG_USER	MS_REG_MNG_SYSFLG   /* user block */
14433842cedSCho, Yu-Chen #define MS_REG_MNG_SYSFLG_BOOT	0x00	/* system block */
14533842cedSCho, Yu-Chen #define MS_REG_MNG_RESERVED	0xc3
14633842cedSCho, Yu-Chen #define MS_REG_MNG_DEFAULT	(MS_REG_MNG_SCMS_COPY_OK | MS_REG_MNG_ATFLG_OTHER | MS_REG_MNG_SYSFLG_USER | MS_REG_MNG_RESERVED)
14733842cedSCho, Yu-Chen 
14833842cedSCho, Yu-Chen 
14933842cedSCho, Yu-Chen #define MS_MAX_PAGES_PER_BLOCK		32
15033842cedSCho, Yu-Chen #define MS_MAX_INITIAL_ERROR_BLOCKS 	10
15133842cedSCho, Yu-Chen #define MS_LIB_BITS_PER_BYTE		8
15233842cedSCho, Yu-Chen 
15333842cedSCho, Yu-Chen #define MS_SYSINF_FORMAT_FAT		1
15433842cedSCho, Yu-Chen #define MS_SYSINF_USAGE_GENERAL		0
15533842cedSCho, Yu-Chen 
15633842cedSCho, Yu-Chen #define MS_SYSINF_MSCLASS_TYPE_1	1
15733842cedSCho, Yu-Chen #define MS_SYSINF_PAGE_SIZE		MS_BYTES_PER_PAGE /* fixed */
15833842cedSCho, Yu-Chen 
15933842cedSCho, Yu-Chen #define MS_SYSINF_CARDTYPE_RDONLY	1
16033842cedSCho, Yu-Chen #define MS_SYSINF_CARDTYPE_RDWR		2
16133842cedSCho, Yu-Chen #define MS_SYSINF_CARDTYPE_HYBRID	3
16233842cedSCho, Yu-Chen #define MS_SYSINF_SECURITY		0x01
16333842cedSCho, Yu-Chen #define MS_SYSINF_SECURITY_NO_SUPPORT	MS_SYSINF_SECURITY
16433842cedSCho, Yu-Chen #define MS_SYSINF_SECURITY_SUPPORT	0
16533842cedSCho, Yu-Chen 
16633842cedSCho, Yu-Chen #define MS_SYSINF_RESERVED1		1
16733842cedSCho, Yu-Chen #define MS_SYSINF_RESERVED2		1
16833842cedSCho, Yu-Chen 
16933842cedSCho, Yu-Chen #define MS_SYSENT_TYPE_INVALID_BLOCK	0x01
17033842cedSCho, Yu-Chen #define MS_SYSENT_TYPE_CIS_IDI		0x0a    /* CIS/IDI */
17133842cedSCho, Yu-Chen 
17233842cedSCho, Yu-Chen #define SIZE_OF_KIRO		1024
17333842cedSCho, Yu-Chen #define BYTE_MASK		0xff
17433842cedSCho, Yu-Chen 
17533842cedSCho, Yu-Chen /* ms error code */
17633842cedSCho, Yu-Chen #define MS_STATUS_WRITE_PROTECT	0x0106
17733842cedSCho, Yu-Chen #define MS_STATUS_SUCCESS	0x0000
17833842cedSCho, Yu-Chen #define MS_ERROR_FLASH_READ	0x8003
17933842cedSCho, Yu-Chen #define MS_ERROR_FLASH_ERASE	0x8005
18033842cedSCho, Yu-Chen #define MS_LB_ERROR		0xfff0
18133842cedSCho, Yu-Chen #define MS_LB_BOOT_BLOCK	0xfff1
18233842cedSCho, Yu-Chen #define MS_LB_INITIAL_ERROR	0xfff2
18333842cedSCho, Yu-Chen #define MS_STATUS_SUCCESS_WITH_ECC 0xfff3
18433842cedSCho, Yu-Chen #define MS_LB_ACQUIRED_ERROR	0xfff4
18533842cedSCho, Yu-Chen #define MS_LB_NOT_USED_ERASED	0xfff5
18633842cedSCho, Yu-Chen #define MS_NOCARD_ERROR		0xfff8
18733842cedSCho, Yu-Chen #define MS_NO_MEMORY_ERROR	0xfff9
18833842cedSCho, Yu-Chen #define MS_STATUS_INT_ERROR	0xfffa
18933842cedSCho, Yu-Chen #define MS_STATUS_ERROR		0xfffe
19033842cedSCho, Yu-Chen #define MS_LB_NOT_USED		0xffff
19133842cedSCho, Yu-Chen 
19233842cedSCho, Yu-Chen #define MS_REG_MNG_SYSFLG	0x04    /* system flag */
19333842cedSCho, Yu-Chen #define MS_REG_MNG_SYSFLG_USER	MS_REG_MNG_SYSFLG   /* user block */
19433842cedSCho, Yu-Chen 
19533842cedSCho, Yu-Chen #define MS_BOOT_BLOCK_ID                        0x0001
19633842cedSCho, Yu-Chen #define MS_BOOT_BLOCK_FORMAT_VERSION            0x0100
19733842cedSCho, Yu-Chen #define MS_BOOT_BLOCK_DATA_ENTRIES              2
19833842cedSCho, Yu-Chen 
19933842cedSCho, Yu-Chen #define MS_NUMBER_OF_SYSTEM_ENTRY       	4
20033842cedSCho, Yu-Chen #define MS_NUMBER_OF_BOOT_BLOCK			2
20133842cedSCho, Yu-Chen #define MS_BYTES_PER_PAGE			512
20233842cedSCho, Yu-Chen #define MS_LOGICAL_BLOCKS_PER_SEGMENT		496
20333842cedSCho, Yu-Chen #define MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT        494
20433842cedSCho, Yu-Chen 
20533842cedSCho, Yu-Chen #define MS_PHYSICAL_BLOCKS_PER_SEGMENT		0x200 /* 512 */
20633842cedSCho, Yu-Chen #define MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK     0x1ff
20733842cedSCho, Yu-Chen 
20833842cedSCho, Yu-Chen /* overwrite area */
20933842cedSCho, Yu-Chen #define MS_REG_OVR_BKST		0x80		/* block status */
21033842cedSCho, Yu-Chen #define MS_REG_OVR_BKST_OK	MS_REG_OVR_BKST	/* OK */
21133842cedSCho, Yu-Chen #define MS_REG_OVR_BKST_NG	0x00            /* NG */
21233842cedSCho, Yu-Chen 
21333842cedSCho, Yu-Chen /* Status Register 1 */
21433842cedSCho, Yu-Chen #define MS_REG_ST1_DTER		0x20	/* error on data(corrected) */
21533842cedSCho, Yu-Chen #define MS_REG_ST1_EXER		0x08	/* error on extra(corrected) */
21633842cedSCho, Yu-Chen #define MS_REG_ST1_FGER		0x02	/* error on overwrite flag(corrected) */
21733842cedSCho, Yu-Chen 
21833842cedSCho, Yu-Chen /* MemoryStick Register */
21933842cedSCho, Yu-Chen /* Status Register 0 */
22033842cedSCho, Yu-Chen #define MS_REG_ST0_WP		0x01	/* write protected */
22133842cedSCho, Yu-Chen #define MS_REG_ST0_WP_ON	MS_REG_ST0_WP
22233842cedSCho, Yu-Chen 
22333842cedSCho, Yu-Chen #define MS_LIB_CTRL_RDONLY      0
22433842cedSCho, Yu-Chen #define MS_LIB_CTRL_WRPROTECT   1
22533842cedSCho, Yu-Chen 
22633842cedSCho, Yu-Chen /*dphy->log table */
22733842cedSCho, Yu-Chen #define ms_libconv_to_logical(pdx, PhyBlock) (((PhyBlock) >= (pdx)->MS_Lib.NumberOfPhyBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Phy2LogMap[PhyBlock])
22833842cedSCho, Yu-Chen #define ms_libconv_to_physical(pdx, LogBlock) (((LogBlock) >= (pdx)->MS_Lib.NumberOfLogBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Log2PhyMap[LogBlock])
22933842cedSCho, Yu-Chen 
23033842cedSCho, Yu-Chen #define ms_lib_ctrl_set(pdx, Flag)	((pdx)->MS_Lib.flags |= (1 << (Flag)))
23133842cedSCho, Yu-Chen #define ms_lib_ctrl_reset(pdx, Flag)	((pdx)->MS_Lib.flags &= ~(1 << (Flag)))
23233842cedSCho, Yu-Chen #define ms_lib_ctrl_check(pdx, Flag)	((pdx)->MS_Lib.flags & (1 << (Flag)))
23333842cedSCho, Yu-Chen 
23433842cedSCho, Yu-Chen #define ms_lib_iswritable(pdx) ((ms_lib_ctrl_check((pdx), MS_LIB_CTRL_RDONLY) == 0) && (ms_lib_ctrl_check(pdx, MS_LIB_CTRL_WRPROTECT) == 0))
23533842cedSCho, Yu-Chen #define ms_lib_clear_pagemap(pdx) memset((pdx)->MS_Lib.pagemap, 0, sizeof((pdx)->MS_Lib.pagemap))
23633842cedSCho, Yu-Chen #define memstick_logaddr(logadr1, logadr0) ((((u16)(logadr1)) << 8) | (logadr0))
23733842cedSCho, Yu-Chen 
23841e568d1Shuajun li 
23941e568d1Shuajun li struct SD_STATUS {
24041e568d1Shuajun li 	u8    Insert:1;
24141e568d1Shuajun li 	u8    Ready:1;
24241e568d1Shuajun li 	u8    MediaChange:1;
24341e568d1Shuajun li 	u8    IsMMC:1;
24441e568d1Shuajun li 	u8    HiCapacity:1;
24541e568d1Shuajun li 	u8    HiSpeed:1;
24641e568d1Shuajun li 	u8    WtP:1;
24741e568d1Shuajun li 	u8    Reserved:1;
24841e568d1Shuajun li };
24941e568d1Shuajun li 
25041e568d1Shuajun li struct MS_STATUS {
25141e568d1Shuajun li 	u8    Insert:1;
25241e568d1Shuajun li 	u8    Ready:1;
25341e568d1Shuajun li 	u8    MediaChange:1;
25441e568d1Shuajun li 	u8    IsMSPro:1;
25541e568d1Shuajun li 	u8    IsMSPHG:1;
25641e568d1Shuajun li 	u8    Reserved1:1;
25741e568d1Shuajun li 	u8    WtP:1;
25841e568d1Shuajun li 	u8    Reserved2:1;
25941e568d1Shuajun li };
26041e568d1Shuajun li 
26141e568d1Shuajun li struct SM_STATUS {
26241e568d1Shuajun li 	u8    Insert:1;
26341e568d1Shuajun li 	u8    Ready:1;
26441e568d1Shuajun li 	u8    MediaChange:1;
26541e568d1Shuajun li 	u8    Reserved:3;
26641e568d1Shuajun li 	u8    WtP:1;
26741e568d1Shuajun li 	u8    IsMS:1;
26841e568d1Shuajun li };
26941e568d1Shuajun li 
27033842cedSCho, Yu-Chen struct ms_bootblock_cis {
27133842cedSCho, Yu-Chen 	u8 bCistplDEVICE[6];    /* 0 */
27233842cedSCho, Yu-Chen 	u8 bCistplDEVICE0C[6];  /* 6 */
27333842cedSCho, Yu-Chen 	u8 bCistplJEDECC[4];    /* 12 */
27433842cedSCho, Yu-Chen 	u8 bCistplMANFID[6];    /* 16 */
27533842cedSCho, Yu-Chen 	u8 bCistplVER1[32];     /* 22 */
27633842cedSCho, Yu-Chen 	u8 bCistplFUNCID[4];    /* 54 */
27733842cedSCho, Yu-Chen 	u8 bCistplFUNCE0[4];    /* 58 */
27833842cedSCho, Yu-Chen 	u8 bCistplFUNCE1[5];    /* 62 */
27933842cedSCho, Yu-Chen 	u8 bCistplCONF[7];      /* 67 */
28033842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT0[10];/* 74 */
28133842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT1[8]; /* 84 */
28233842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT2[12];/* 92 */
28333842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT3[8]; /* 104 */
28433842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT4[17];/* 112 */
28533842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT5[8]; /* 129 */
28633842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT6[17];/* 137 */
28733842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT7[8]; /* 154 */
28833842cedSCho, Yu-Chen 	u8 bCistplNOLINK[3];    /* 162 */
28933842cedSCho, Yu-Chen } ;
29033842cedSCho, Yu-Chen 
29133842cedSCho, Yu-Chen struct ms_bootblock_idi {
29233842cedSCho, Yu-Chen #define MS_IDI_GENERAL_CONF 0x848A
29333842cedSCho, Yu-Chen 	u16 wIDIgeneralConfiguration;	/* 0 */
29433842cedSCho, Yu-Chen 	u16 wIDInumberOfCylinder;	/* 1 */
29533842cedSCho, Yu-Chen 	u16 wIDIreserved0;		/* 2 */
29633842cedSCho, Yu-Chen 	u16 wIDInumberOfHead;		/* 3 */
29733842cedSCho, Yu-Chen 	u16 wIDIbytesPerTrack;		/* 4 */
29833842cedSCho, Yu-Chen 	u16 wIDIbytesPerSector;		/* 5 */
29933842cedSCho, Yu-Chen 	u16 wIDIsectorsPerTrack;	/* 6 */
30033842cedSCho, Yu-Chen 	u16 wIDItotalSectors[2];	/* 7-8  high,low */
30133842cedSCho, Yu-Chen 	u16 wIDIreserved1[11];		/* 9-19 */
30233842cedSCho, Yu-Chen 	u16 wIDIbufferType;		/* 20 */
30333842cedSCho, Yu-Chen 	u16 wIDIbufferSize;		/* 21 */
30433842cedSCho, Yu-Chen 	u16 wIDIlongCmdECC;		/* 22 */
30533842cedSCho, Yu-Chen 	u16 wIDIfirmVersion[4];		/* 23-26 */
30633842cedSCho, Yu-Chen 	u16 wIDImodelName[20];		/* 27-46 */
30733842cedSCho, Yu-Chen 	u16 wIDIreserved2;		/* 47 */
30833842cedSCho, Yu-Chen 	u16 wIDIlongWordSupported;	/* 48 */
30933842cedSCho, Yu-Chen 	u16 wIDIdmaSupported;		/* 49 */
31033842cedSCho, Yu-Chen 	u16 wIDIreserved3;		/* 50 */
31133842cedSCho, Yu-Chen 	u16 wIDIpioTiming;		/* 51 */
31233842cedSCho, Yu-Chen 	u16 wIDIdmaTiming;		/* 52 */
31333842cedSCho, Yu-Chen 	u16 wIDItransferParameter;	/* 53 */
31433842cedSCho, Yu-Chen 	u16 wIDIformattedCylinder;	/* 54 */
31533842cedSCho, Yu-Chen 	u16 wIDIformattedHead;		/* 55 */
31633842cedSCho, Yu-Chen 	u16 wIDIformattedSectorsPerTrack;/* 56 */
31733842cedSCho, Yu-Chen 	u16 wIDIformattedTotalSectors[2];/* 57-58 */
31833842cedSCho, Yu-Chen 	u16 wIDImultiSector;		/* 59 */
31933842cedSCho, Yu-Chen 	u16 wIDIlbaSectors[2];		/* 60-61 */
32033842cedSCho, Yu-Chen 	u16 wIDIsingleWordDMA;		/* 62 */
32133842cedSCho, Yu-Chen 	u16 wIDImultiWordDMA;		/* 63 */
32233842cedSCho, Yu-Chen 	u16 wIDIreserved4[192];		/* 64-255 */
32333842cedSCho, Yu-Chen };
32433842cedSCho, Yu-Chen 
32533842cedSCho, Yu-Chen struct ms_bootblock_sysent_rec {
32633842cedSCho, Yu-Chen 	u32 dwStart;
32733842cedSCho, Yu-Chen 	u32 dwSize;
32833842cedSCho, Yu-Chen 	u8 bType;
32933842cedSCho, Yu-Chen 	u8 bReserved[3];
33033842cedSCho, Yu-Chen };
33133842cedSCho, Yu-Chen 
33233842cedSCho, Yu-Chen struct ms_bootblock_sysent {
33333842cedSCho, Yu-Chen 	struct ms_bootblock_sysent_rec entry[MS_NUMBER_OF_SYSTEM_ENTRY];
33433842cedSCho, Yu-Chen };
33533842cedSCho, Yu-Chen 
33633842cedSCho, Yu-Chen struct ms_bootblock_sysinf {
33733842cedSCho, Yu-Chen 	u8 bMsClass;			/* must be 1 */
33833842cedSCho, Yu-Chen 	u8 bCardType;			/* see below */
33933842cedSCho, Yu-Chen 	u16 wBlockSize;			/* n KB */
34033842cedSCho, Yu-Chen 	u16 wBlockNumber;		/* number of physical block */
34133842cedSCho, Yu-Chen 	u16 wTotalBlockNumber;		/* number of logical block */
34233842cedSCho, Yu-Chen 	u16 wPageSize;			/* must be 0x200 */
34333842cedSCho, Yu-Chen 	u8 bExtraSize;			/* 0x10 */
34433842cedSCho, Yu-Chen 	u8 bSecuritySupport;
34533842cedSCho, Yu-Chen 	u8 bAssemblyDate[8];
34633842cedSCho, Yu-Chen 	u8 bFactoryArea[4];
34733842cedSCho, Yu-Chen 	u8 bAssemblyMakerCode;
34833842cedSCho, Yu-Chen 	u8 bAssemblyMachineCode[3];
34933842cedSCho, Yu-Chen 	u16 wMemoryMakerCode;
35033842cedSCho, Yu-Chen 	u16 wMemoryDeviceCode;
35133842cedSCho, Yu-Chen 	u16 wMemorySize;
35233842cedSCho, Yu-Chen 	u8 bReserved1;
35333842cedSCho, Yu-Chen 	u8 bReserved2;
35433842cedSCho, Yu-Chen 	u8 bVCC;
35533842cedSCho, Yu-Chen 	u8 bVPP;
35633842cedSCho, Yu-Chen 	u16 wControllerChipNumber;
35733842cedSCho, Yu-Chen 	u16 wControllerFunction;	/* New MS */
35833842cedSCho, Yu-Chen 	u8 bReserved3[9];		/* New MS */
35933842cedSCho, Yu-Chen 	u8 bParallelSupport;		/* New MS */
36033842cedSCho, Yu-Chen 	u16 wFormatValue;		/* New MS */
36133842cedSCho, Yu-Chen 	u8 bFormatType;
36233842cedSCho, Yu-Chen 	u8 bUsage;
36333842cedSCho, Yu-Chen 	u8 bDeviceType;
36433842cedSCho, Yu-Chen 	u8 bReserved4[22];
36533842cedSCho, Yu-Chen 	u8 bFUValue3;
36633842cedSCho, Yu-Chen 	u8 bFUValue4;
36733842cedSCho, Yu-Chen 	u8 bReserved5[15];
36833842cedSCho, Yu-Chen };
36933842cedSCho, Yu-Chen 
37033842cedSCho, Yu-Chen struct ms_bootblock_header {
37133842cedSCho, Yu-Chen 	u16 wBlockID;
37233842cedSCho, Yu-Chen 	u16 wFormatVersion;
37333842cedSCho, Yu-Chen 	u8 bReserved1[184];
37433842cedSCho, Yu-Chen 	u8 bNumberOfDataEntry;
37533842cedSCho, Yu-Chen 	u8 bReserved2[179];
37633842cedSCho, Yu-Chen };
37733842cedSCho, Yu-Chen 
37833842cedSCho, Yu-Chen struct ms_bootblock_page0 {
37933842cedSCho, Yu-Chen 	struct ms_bootblock_header header;
38033842cedSCho, Yu-Chen 	struct ms_bootblock_sysent sysent;
38133842cedSCho, Yu-Chen 	struct ms_bootblock_sysinf sysinf;
38233842cedSCho, Yu-Chen };
38333842cedSCho, Yu-Chen 
38433842cedSCho, Yu-Chen struct ms_bootblock_cis_idi {
38533842cedSCho, Yu-Chen 	union {
38633842cedSCho, Yu-Chen 		struct ms_bootblock_cis cis;
38733842cedSCho, Yu-Chen 		u8 dmy[256];
38833842cedSCho, Yu-Chen 	} cis;
38933842cedSCho, Yu-Chen 
39033842cedSCho, Yu-Chen 	union {
39133842cedSCho, Yu-Chen 		struct ms_bootblock_idi idi;
39233842cedSCho, Yu-Chen 		u8 dmy[256];
39333842cedSCho, Yu-Chen 	} idi;
39433842cedSCho, Yu-Chen 
39533842cedSCho, Yu-Chen };
39633842cedSCho, Yu-Chen 
39733842cedSCho, Yu-Chen /* ENE MS Lib struct */
39833842cedSCho, Yu-Chen struct ms_lib_type_extdat {
39933842cedSCho, Yu-Chen 	u8 reserved;
40033842cedSCho, Yu-Chen 	u8 intr;
40133842cedSCho, Yu-Chen 	u8 status0;
40233842cedSCho, Yu-Chen 	u8 status1;
40333842cedSCho, Yu-Chen 	u8 ovrflg;
40433842cedSCho, Yu-Chen 	u8 mngflg;
40533842cedSCho, Yu-Chen 	u16 logadr;
40633842cedSCho, Yu-Chen };
40733842cedSCho, Yu-Chen 
40833842cedSCho, Yu-Chen struct ms_lib_ctrl {
40933842cedSCho, Yu-Chen 	u32 flags;
41033842cedSCho, Yu-Chen 	u32 BytesPerSector;
41133842cedSCho, Yu-Chen 	u32 NumberOfCylinder;
41233842cedSCho, Yu-Chen 	u32 SectorsPerCylinder;
41333842cedSCho, Yu-Chen 	u16 cardType;			/* R/W, RO, Hybrid */
41433842cedSCho, Yu-Chen 	u16 blockSize;
41533842cedSCho, Yu-Chen 	u16 PagesPerBlock;
41633842cedSCho, Yu-Chen 	u16 NumberOfPhyBlock;
41733842cedSCho, Yu-Chen 	u16 NumberOfLogBlock;
41833842cedSCho, Yu-Chen 	u16 NumberOfSegment;
41933842cedSCho, Yu-Chen 	u16 *Phy2LogMap;		/* phy2log table */
42033842cedSCho, Yu-Chen 	u16 *Log2PhyMap;		/* log2phy table */
42133842cedSCho, Yu-Chen 	u16 wrtblk;
42233842cedSCho, Yu-Chen 	unsigned char *pagemap[(MS_MAX_PAGES_PER_BLOCK + (MS_LIB_BITS_PER_BYTE-1)) / MS_LIB_BITS_PER_BYTE];
42333842cedSCho, Yu-Chen 	unsigned char *blkpag;
42433842cedSCho, Yu-Chen 	struct ms_lib_type_extdat *blkext;
42533842cedSCho, Yu-Chen 	unsigned char copybuf[512];
42633842cedSCho, Yu-Chen };
42733842cedSCho, Yu-Chen 
42841e568d1Shuajun li 
42941e568d1Shuajun li /* SD Block Length */
43041e568d1Shuajun li /* 2^9 = 512 Bytes, The HW maximum read/write data length */
43141e568d1Shuajun li #define SD_BLOCK_LEN  9
43241e568d1Shuajun li 
43341e568d1Shuajun li struct ene_ub6250_info {
43441e568d1Shuajun li 	/* for 6250 code */
43541e568d1Shuajun li 	struct SD_STATUS	SD_Status;
43641e568d1Shuajun li 	struct MS_STATUS	MS_Status;
43741e568d1Shuajun li 	struct SM_STATUS	SM_Status;
43841e568d1Shuajun li 
43941e568d1Shuajun li 	/* ----- SD Control Data ---------------- */
44041e568d1Shuajun li 	/*SD_REGISTER SD_Regs; */
44141e568d1Shuajun li 	u16		SD_Block_Mult;
44241e568d1Shuajun li 	u8		SD_READ_BL_LEN;
44341e568d1Shuajun li 	u16		SD_C_SIZE;
44441e568d1Shuajun li 	u8		SD_C_SIZE_MULT;
44541e568d1Shuajun li 
44641e568d1Shuajun li 	/* SD/MMC New spec. */
44741e568d1Shuajun li 	u8		SD_SPEC_VER;
44841e568d1Shuajun li 	u8		SD_CSD_VER;
44941e568d1Shuajun li 	u8		SD20_HIGH_CAPACITY;
45041e568d1Shuajun li 	u32		HC_C_SIZE;
45141e568d1Shuajun li 	u8		MMC_SPEC_VER;
45241e568d1Shuajun li 	u8		MMC_BusWidth;
45341e568d1Shuajun li 	u8		MMC_HIGH_CAPACITY;
45441e568d1Shuajun li 
45541e568d1Shuajun li 	/*----- MS Control Data ---------------- */
45641e568d1Shuajun li 	bool		MS_SWWP;
45741e568d1Shuajun li 	u32		MSP_TotalBlock;
45833842cedSCho, Yu-Chen 	struct ms_lib_ctrl MS_Lib;
45941e568d1Shuajun li 	bool		MS_IsRWPage;
46041e568d1Shuajun li 	u16		MS_Model;
46141e568d1Shuajun li 
46241e568d1Shuajun li 	/*----- SM Control Data ---------------- */
46341e568d1Shuajun li 	u8		SM_DeviceID;
46441e568d1Shuajun li 	u8		SM_CardID;
46541e568d1Shuajun li 
46641e568d1Shuajun li 	unsigned char	*testbuf;
46741e568d1Shuajun li 	u8		BIN_FLAG;
46841e568d1Shuajun li 	u32		bl_num;
46941e568d1Shuajun li 	int		SrbStatus;
47041e568d1Shuajun li 
47141e568d1Shuajun li 	/*------Power Managerment ---------------*/
47241e568d1Shuajun li 	bool		Power_IsResum;
47341e568d1Shuajun li };
47441e568d1Shuajun li 
47541e568d1Shuajun li static int ene_sd_init(struct us_data *us);
47633842cedSCho, Yu-Chen static int ene_ms_init(struct us_data *us);
47741e568d1Shuajun li static int ene_load_bincode(struct us_data *us, unsigned char flag);
47841e568d1Shuajun li 
47941e568d1Shuajun li static void ene_ub6250_info_destructor(void *extra)
48041e568d1Shuajun li {
48141e568d1Shuajun li 	if (!extra)
48241e568d1Shuajun li 		return;
48341e568d1Shuajun li }
48441e568d1Shuajun li 
48541e568d1Shuajun li static int ene_send_scsi_cmd(struct us_data *us, u8 fDir, void *buf, int use_sg)
48641e568d1Shuajun li {
48741e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
48841e568d1Shuajun li 	struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
48941e568d1Shuajun li 
49041e568d1Shuajun li 	int result;
49141e568d1Shuajun li 	unsigned int residue;
49241e568d1Shuajun li 	unsigned int cswlen = 0, partial = 0;
49341e568d1Shuajun li 	unsigned int transfer_length = bcb->DataTransferLength;
49441e568d1Shuajun li 
49541e568d1Shuajun li 	/* US_DEBUGP("transport --- ene_send_scsi_cmd\n"); */
49641e568d1Shuajun li 	/* send cmd to out endpoint */
49741e568d1Shuajun li 	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
49841e568d1Shuajun li 					    bcb, US_BULK_CB_WRAP_LEN, NULL);
49941e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
50041e568d1Shuajun li 		US_DEBUGP("send cmd to out endpoint fail ---\n");
50141e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
50241e568d1Shuajun li 	}
50341e568d1Shuajun li 
50441e568d1Shuajun li 	if (buf) {
50541e568d1Shuajun li 		unsigned int pipe = fDir;
50641e568d1Shuajun li 
50741e568d1Shuajun li 		if (fDir  == FDIR_READ)
50841e568d1Shuajun li 			pipe = us->recv_bulk_pipe;
50941e568d1Shuajun li 		else
51041e568d1Shuajun li 			pipe = us->send_bulk_pipe;
51141e568d1Shuajun li 
51241e568d1Shuajun li 		/* Bulk */
51341e568d1Shuajun li 		if (use_sg) {
51441e568d1Shuajun li 			result = usb_stor_bulk_srb(us, pipe, us->srb);
51541e568d1Shuajun li 		} else {
51641e568d1Shuajun li 			result = usb_stor_bulk_transfer_sg(us, pipe, buf,
51741e568d1Shuajun li 						transfer_length, 0, &partial);
51841e568d1Shuajun li 		}
51941e568d1Shuajun li 		if (result != USB_STOR_XFER_GOOD) {
52041e568d1Shuajun li 			US_DEBUGP("data transfer fail ---\n");
52141e568d1Shuajun li 			return USB_STOR_TRANSPORT_ERROR;
52241e568d1Shuajun li 		}
52341e568d1Shuajun li 	}
52441e568d1Shuajun li 
52541e568d1Shuajun li 	/* Get CSW for device status */
52641e568d1Shuajun li 	result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
52741e568d1Shuajun li 					    US_BULK_CS_WRAP_LEN, &cswlen);
52841e568d1Shuajun li 
52941e568d1Shuajun li 	if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
53041e568d1Shuajun li 		US_DEBUGP("Received 0-length CSW; retrying...\n");
53141e568d1Shuajun li 		result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
53241e568d1Shuajun li 					    bcs, US_BULK_CS_WRAP_LEN, &cswlen);
53341e568d1Shuajun li 	}
53441e568d1Shuajun li 
53541e568d1Shuajun li 	if (result == USB_STOR_XFER_STALLED) {
53641e568d1Shuajun li 		/* get the status again */
53741e568d1Shuajun li 		US_DEBUGP("Attempting to get CSW (2nd try)...\n");
53841e568d1Shuajun li 		result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
53941e568d1Shuajun li 						bcs, US_BULK_CS_WRAP_LEN, NULL);
54041e568d1Shuajun li 	}
54141e568d1Shuajun li 
54241e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD)
54341e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
54441e568d1Shuajun li 
54541e568d1Shuajun li 	/* check bulk status */
54641e568d1Shuajun li 	residue = le32_to_cpu(bcs->Residue);
54741e568d1Shuajun li 
54841e568d1Shuajun li 	/* try to compute the actual residue, based on how much data
54941e568d1Shuajun li 	 * was really transferred and what the device tells us */
55041e568d1Shuajun li 	if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
55141e568d1Shuajun li 		residue = min(residue, transfer_length);
55241e568d1Shuajun li 		if (us->srb != NULL)
55341e568d1Shuajun li 			scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
55441e568d1Shuajun li 								(int)residue));
55541e568d1Shuajun li 	}
55641e568d1Shuajun li 
55741e568d1Shuajun li 	if (bcs->Status != US_BULK_STAT_OK)
55841e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
55941e568d1Shuajun li 
56041e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
56141e568d1Shuajun li }
56241e568d1Shuajun li 
56341e568d1Shuajun li static int sd_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
56441e568d1Shuajun li {
56541e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
56641e568d1Shuajun li 
56741e568d1Shuajun li 	if (info->SD_Status.Insert && info->SD_Status.Ready)
56841e568d1Shuajun li 		return USB_STOR_TRANSPORT_GOOD;
56941e568d1Shuajun li 	else {
57041e568d1Shuajun li 		ene_sd_init(us);
57141e568d1Shuajun li 		return USB_STOR_TRANSPORT_GOOD;
57241e568d1Shuajun li 	}
57341e568d1Shuajun li 
57441e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
57541e568d1Shuajun li }
57641e568d1Shuajun li 
57741e568d1Shuajun li static int sd_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
57841e568d1Shuajun li {
57941e568d1Shuajun li 	unsigned char data_ptr[36] = {
58041e568d1Shuajun li 		0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
58141e568d1Shuajun li 		0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
58241e568d1Shuajun li 		0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
58341e568d1Shuajun li 		0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30 };
58441e568d1Shuajun li 
58541e568d1Shuajun li 	usb_stor_set_xfer_buf(data_ptr, 36, srb);
58641e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
58741e568d1Shuajun li }
58841e568d1Shuajun li 
58941e568d1Shuajun li static int sd_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
59041e568d1Shuajun li {
59141e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
59241e568d1Shuajun li 	unsigned char mediaNoWP[12] = {
59341e568d1Shuajun li 		0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
59441e568d1Shuajun li 		0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
59541e568d1Shuajun li 	unsigned char mediaWP[12]   = {
59641e568d1Shuajun li 		0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
59741e568d1Shuajun li 		0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
59841e568d1Shuajun li 
59941e568d1Shuajun li 	if (info->SD_Status.WtP)
60041e568d1Shuajun li 		usb_stor_set_xfer_buf(mediaWP, 12, srb);
60141e568d1Shuajun li 	else
60241e568d1Shuajun li 		usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
60341e568d1Shuajun li 
60441e568d1Shuajun li 
60541e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
60641e568d1Shuajun li }
60741e568d1Shuajun li 
60841e568d1Shuajun li static int sd_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
60941e568d1Shuajun li {
61041e568d1Shuajun li 	u32	bl_num;
61136f3a14dSFelipe Balbi 	u32	bl_len;
61241e568d1Shuajun li 	unsigned int offset = 0;
61341e568d1Shuajun li 	unsigned char    buf[8];
61441e568d1Shuajun li 	struct scatterlist *sg = NULL;
61541e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
61641e568d1Shuajun li 
61741e568d1Shuajun li 	US_DEBUGP("sd_scsi_read_capacity\n");
61841e568d1Shuajun li 	if (info->SD_Status.HiCapacity) {
61941e568d1Shuajun li 		bl_len = 0x200;
62041e568d1Shuajun li 		if (info->SD_Status.IsMMC)
62141e568d1Shuajun li 			bl_num = info->HC_C_SIZE-1;
62241e568d1Shuajun li 		else
62341e568d1Shuajun li 			bl_num = (info->HC_C_SIZE + 1) * 1024 - 1;
62441e568d1Shuajun li 	} else {
62541e568d1Shuajun li 		bl_len = 1 << (info->SD_READ_BL_LEN);
62641e568d1Shuajun li 		bl_num = info->SD_Block_Mult * (info->SD_C_SIZE + 1)
62741e568d1Shuajun li 				* (1 << (info->SD_C_SIZE_MULT + 2)) - 1;
62841e568d1Shuajun li 	}
62941e568d1Shuajun li 	info->bl_num = bl_num;
63041e568d1Shuajun li 	US_DEBUGP("bl_len = %x\n", bl_len);
63141e568d1Shuajun li 	US_DEBUGP("bl_num = %x\n", bl_num);
63241e568d1Shuajun li 
63341e568d1Shuajun li 	/*srb->request_bufflen = 8; */
63441e568d1Shuajun li 	buf[0] = (bl_num >> 24) & 0xff;
63541e568d1Shuajun li 	buf[1] = (bl_num >> 16) & 0xff;
63641e568d1Shuajun li 	buf[2] = (bl_num >> 8) & 0xff;
63741e568d1Shuajun li 	buf[3] = (bl_num >> 0) & 0xff;
63841e568d1Shuajun li 	buf[4] = (bl_len >> 24) & 0xff;
63941e568d1Shuajun li 	buf[5] = (bl_len >> 16) & 0xff;
64041e568d1Shuajun li 	buf[6] = (bl_len >> 8) & 0xff;
64141e568d1Shuajun li 	buf[7] = (bl_len >> 0) & 0xff;
64241e568d1Shuajun li 
64341e568d1Shuajun li 	usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
64441e568d1Shuajun li 
64541e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
64641e568d1Shuajun li }
64741e568d1Shuajun li 
64841e568d1Shuajun li static int sd_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
64941e568d1Shuajun li {
65041e568d1Shuajun li 	int result;
65141e568d1Shuajun li 	unsigned char *cdb = srb->cmnd;
65241e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
65341e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
65441e568d1Shuajun li 
65541e568d1Shuajun li 	u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
65641e568d1Shuajun li 		 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
65741e568d1Shuajun li 	u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
65841e568d1Shuajun li 	u32 bnByte = bn * 0x200;
65941e568d1Shuajun li 	u32 blenByte = blen * 0x200;
66041e568d1Shuajun li 
66141e568d1Shuajun li 	if (bn > info->bl_num)
66241e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
66341e568d1Shuajun li 
66441e568d1Shuajun li 	result = ene_load_bincode(us, SD_RW_PATTERN);
66541e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
66641e568d1Shuajun li 		US_DEBUGP("Load SD RW pattern Fail !!\n");
66741e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
66841e568d1Shuajun li 	}
66941e568d1Shuajun li 
67041e568d1Shuajun li 	if (info->SD_Status.HiCapacity)
67141e568d1Shuajun li 		bnByte = bn;
67241e568d1Shuajun li 
67341e568d1Shuajun li 	/* set up the command wrapper */
67441e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
67541e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
67641e568d1Shuajun li 	bcb->DataTransferLength = blenByte;
67741e568d1Shuajun li 	bcb->Flags  = 0x80;
67841e568d1Shuajun li 	bcb->CDB[0] = 0xF1;
67941e568d1Shuajun li 	bcb->CDB[5] = (unsigned char)(bnByte);
68041e568d1Shuajun li 	bcb->CDB[4] = (unsigned char)(bnByte>>8);
68141e568d1Shuajun li 	bcb->CDB[3] = (unsigned char)(bnByte>>16);
68241e568d1Shuajun li 	bcb->CDB[2] = (unsigned char)(bnByte>>24);
68341e568d1Shuajun li 
68441e568d1Shuajun li 	result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
68541e568d1Shuajun li 	return result;
68641e568d1Shuajun li }
68741e568d1Shuajun li 
68841e568d1Shuajun li static int sd_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
68941e568d1Shuajun li {
69041e568d1Shuajun li 	int result;
69141e568d1Shuajun li 	unsigned char *cdb = srb->cmnd;
69241e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
69341e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
69441e568d1Shuajun li 
69541e568d1Shuajun li 	u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
69641e568d1Shuajun li 		 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
69741e568d1Shuajun li 	u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
69841e568d1Shuajun li 	u32 bnByte = bn * 0x200;
69941e568d1Shuajun li 	u32 blenByte = blen * 0x200;
70041e568d1Shuajun li 
70141e568d1Shuajun li 	if (bn > info->bl_num)
70241e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
70341e568d1Shuajun li 
70441e568d1Shuajun li 	result = ene_load_bincode(us, SD_RW_PATTERN);
70541e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
70641e568d1Shuajun li 		US_DEBUGP("Load SD RW pattern Fail !!\n");
70741e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
70841e568d1Shuajun li 	}
70941e568d1Shuajun li 
71041e568d1Shuajun li 	if (info->SD_Status.HiCapacity)
71141e568d1Shuajun li 		bnByte = bn;
71241e568d1Shuajun li 
71341e568d1Shuajun li 	/* set up the command wrapper */
71441e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
71541e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
71641e568d1Shuajun li 	bcb->DataTransferLength = blenByte;
71741e568d1Shuajun li 	bcb->Flags  = 0x00;
71841e568d1Shuajun li 	bcb->CDB[0] = 0xF0;
71941e568d1Shuajun li 	bcb->CDB[5] = (unsigned char)(bnByte);
72041e568d1Shuajun li 	bcb->CDB[4] = (unsigned char)(bnByte>>8);
72141e568d1Shuajun li 	bcb->CDB[3] = (unsigned char)(bnByte>>16);
72241e568d1Shuajun li 	bcb->CDB[2] = (unsigned char)(bnByte>>24);
72341e568d1Shuajun li 
72441e568d1Shuajun li 	result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
72541e568d1Shuajun li 	return result;
72641e568d1Shuajun li }
72741e568d1Shuajun li 
72833842cedSCho, Yu-Chen /*
72933842cedSCho, Yu-Chen  * ENE MS Card
73033842cedSCho, Yu-Chen  */
73133842cedSCho, Yu-Chen 
73233842cedSCho, Yu-Chen static int ms_lib_set_logicalpair(struct us_data *us, u16 logblk, u16 phyblk)
73333842cedSCho, Yu-Chen {
73433842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
73533842cedSCho, Yu-Chen 
73633842cedSCho, Yu-Chen 	if ((logblk >= info->MS_Lib.NumberOfLogBlock) || (phyblk >= info->MS_Lib.NumberOfPhyBlock))
73733842cedSCho, Yu-Chen 		return (u32)-1;
73833842cedSCho, Yu-Chen 
73933842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap[phyblk] = logblk;
74033842cedSCho, Yu-Chen 	info->MS_Lib.Log2PhyMap[logblk] = phyblk;
74133842cedSCho, Yu-Chen 
74233842cedSCho, Yu-Chen 	return 0;
74333842cedSCho, Yu-Chen }
74433842cedSCho, Yu-Chen 
74533842cedSCho, Yu-Chen static int ms_lib_set_logicalblockmark(struct us_data *us, u16 phyblk, u16 mark)
74633842cedSCho, Yu-Chen {
74733842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
74833842cedSCho, Yu-Chen 
74933842cedSCho, Yu-Chen 	if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
75033842cedSCho, Yu-Chen 		return (u32)-1;
75133842cedSCho, Yu-Chen 
75233842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap[phyblk] = mark;
75333842cedSCho, Yu-Chen 
75433842cedSCho, Yu-Chen 	return 0;
75533842cedSCho, Yu-Chen }
75633842cedSCho, Yu-Chen 
75733842cedSCho, Yu-Chen static int ms_lib_set_initialerrorblock(struct us_data *us, u16 phyblk)
75833842cedSCho, Yu-Chen {
75933842cedSCho, Yu-Chen 	return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_INITIAL_ERROR);
76033842cedSCho, Yu-Chen }
76133842cedSCho, Yu-Chen 
76233842cedSCho, Yu-Chen static int ms_lib_set_bootblockmark(struct us_data *us, u16 phyblk)
76333842cedSCho, Yu-Chen {
76433842cedSCho, Yu-Chen 	return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_BOOT_BLOCK);
76533842cedSCho, Yu-Chen }
76633842cedSCho, Yu-Chen 
76733842cedSCho, Yu-Chen static int ms_lib_free_logicalmap(struct us_data *us)
76833842cedSCho, Yu-Chen {
76933842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
77033842cedSCho, Yu-Chen 
77133842cedSCho, Yu-Chen 	kfree(info->MS_Lib.Phy2LogMap);
77233842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap = NULL;
77333842cedSCho, Yu-Chen 
77433842cedSCho, Yu-Chen 	kfree(info->MS_Lib.Log2PhyMap);
77533842cedSCho, Yu-Chen 	info->MS_Lib.Log2PhyMap = NULL;
77633842cedSCho, Yu-Chen 
77733842cedSCho, Yu-Chen 	return 0;
77833842cedSCho, Yu-Chen }
77933842cedSCho, Yu-Chen 
78036f3a14dSFelipe Balbi static int ms_lib_alloc_logicalmap(struct us_data *us)
78133842cedSCho, Yu-Chen {
78233842cedSCho, Yu-Chen 	u32  i;
78333842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
78433842cedSCho, Yu-Chen 
78533842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap = kmalloc(info->MS_Lib.NumberOfPhyBlock * sizeof(u16), GFP_KERNEL);
78633842cedSCho, Yu-Chen 	info->MS_Lib.Log2PhyMap = kmalloc(info->MS_Lib.NumberOfLogBlock * sizeof(u16), GFP_KERNEL);
78733842cedSCho, Yu-Chen 
78833842cedSCho, Yu-Chen 	if ((info->MS_Lib.Phy2LogMap == NULL) || (info->MS_Lib.Log2PhyMap == NULL)) {
78933842cedSCho, Yu-Chen 		ms_lib_free_logicalmap(us);
79033842cedSCho, Yu-Chen 		return (u32)-1;
79133842cedSCho, Yu-Chen 	}
79233842cedSCho, Yu-Chen 
79333842cedSCho, Yu-Chen 	for (i = 0; i < info->MS_Lib.NumberOfPhyBlock; i++)
79433842cedSCho, Yu-Chen 		info->MS_Lib.Phy2LogMap[i] = MS_LB_NOT_USED;
79533842cedSCho, Yu-Chen 
79633842cedSCho, Yu-Chen 	for (i = 0; i < info->MS_Lib.NumberOfLogBlock; i++)
79733842cedSCho, Yu-Chen 		info->MS_Lib.Log2PhyMap[i] = MS_LB_NOT_USED;
79833842cedSCho, Yu-Chen 
79933842cedSCho, Yu-Chen 	return 0;
80033842cedSCho, Yu-Chen }
80133842cedSCho, Yu-Chen 
80233842cedSCho, Yu-Chen static void ms_lib_clear_writebuf(struct us_data *us)
80333842cedSCho, Yu-Chen {
80433842cedSCho, Yu-Chen 	int i;
80533842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
80633842cedSCho, Yu-Chen 
80733842cedSCho, Yu-Chen 	info->MS_Lib.wrtblk = (u16)-1;
80833842cedSCho, Yu-Chen 	ms_lib_clear_pagemap(info);
80933842cedSCho, Yu-Chen 
81033842cedSCho, Yu-Chen 	if (info->MS_Lib.blkpag)
81133842cedSCho, Yu-Chen 		memset(info->MS_Lib.blkpag, 0xff, info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector);
81233842cedSCho, Yu-Chen 
81333842cedSCho, Yu-Chen 	if (info->MS_Lib.blkext) {
81433842cedSCho, Yu-Chen 		for (i = 0; i < info->MS_Lib.PagesPerBlock; i++) {
81533842cedSCho, Yu-Chen 			info->MS_Lib.blkext[i].status1 = MS_REG_ST1_DEFAULT;
81633842cedSCho, Yu-Chen 			info->MS_Lib.blkext[i].ovrflg = MS_REG_OVR_DEFAULT;
81733842cedSCho, Yu-Chen 			info->MS_Lib.blkext[i].mngflg = MS_REG_MNG_DEFAULT;
81833842cedSCho, Yu-Chen 			info->MS_Lib.blkext[i].logadr = MS_LB_NOT_USED;
81933842cedSCho, Yu-Chen 		}
82033842cedSCho, Yu-Chen 	}
82133842cedSCho, Yu-Chen }
82233842cedSCho, Yu-Chen 
82333842cedSCho, Yu-Chen static int ms_count_freeblock(struct us_data *us, u16 PhyBlock)
82433842cedSCho, Yu-Chen {
82533842cedSCho, Yu-Chen 	u32 Ende, Count;
82633842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
82733842cedSCho, Yu-Chen 
82833842cedSCho, Yu-Chen 	Ende = PhyBlock + MS_PHYSICAL_BLOCKS_PER_SEGMENT;
82933842cedSCho, Yu-Chen 	for (Count = 0; PhyBlock < Ende; PhyBlock++) {
83033842cedSCho, Yu-Chen 		switch (info->MS_Lib.Phy2LogMap[PhyBlock]) {
83133842cedSCho, Yu-Chen 		case MS_LB_NOT_USED:
83233842cedSCho, Yu-Chen 		case MS_LB_NOT_USED_ERASED:
83333842cedSCho, Yu-Chen 			Count++;
83433842cedSCho, Yu-Chen 		default:
83533842cedSCho, Yu-Chen 			break;
83633842cedSCho, Yu-Chen 		}
83733842cedSCho, Yu-Chen 	}
83833842cedSCho, Yu-Chen 
83933842cedSCho, Yu-Chen 	return Count;
84033842cedSCho, Yu-Chen }
84133842cedSCho, Yu-Chen 
84233842cedSCho, Yu-Chen static int ms_read_readpage(struct us_data *us, u32 PhyBlockAddr,
84333842cedSCho, Yu-Chen 		u8 PageNum, u32 *PageBuf, struct ms_lib_type_extdat *ExtraDat)
84433842cedSCho, Yu-Chen {
84533842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
84633842cedSCho, Yu-Chen 	int result;
84733842cedSCho, Yu-Chen 	u8 ExtBuf[4];
84833842cedSCho, Yu-Chen 	u32 bn = PhyBlockAddr * 0x20 + PageNum;
84933842cedSCho, Yu-Chen 
85033842cedSCho, Yu-Chen 	/* printk(KERN_INFO "MS --- MS_ReaderReadPage,
85133842cedSCho, Yu-Chen 	PhyBlockAddr = %x, PageNum = %x\n", PhyBlockAddr, PageNum); */
85233842cedSCho, Yu-Chen 
85333842cedSCho, Yu-Chen 	result = ene_load_bincode(us, MS_RW_PATTERN);
85433842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
85533842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
85633842cedSCho, Yu-Chen 
85733842cedSCho, Yu-Chen 	/* Read Page Data */
85833842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
85933842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
86033842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x200;
86133842cedSCho, Yu-Chen 	bcb->Flags      = 0x80;
86233842cedSCho, Yu-Chen 	bcb->CDB[0]     = 0xF1;
86333842cedSCho, Yu-Chen 
86433842cedSCho, Yu-Chen 	bcb->CDB[1]     = 0x02; /* in init.c ENE_MSInit() is 0x01 */
86533842cedSCho, Yu-Chen 
86633842cedSCho, Yu-Chen 	bcb->CDB[5]     = (unsigned char)(bn);
86733842cedSCho, Yu-Chen 	bcb->CDB[4]     = (unsigned char)(bn>>8);
86833842cedSCho, Yu-Chen 	bcb->CDB[3]     = (unsigned char)(bn>>16);
86933842cedSCho, Yu-Chen 	bcb->CDB[2]     = (unsigned char)(bn>>24);
87033842cedSCho, Yu-Chen 
87133842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_READ, PageBuf, 0);
87233842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
87333842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
87433842cedSCho, Yu-Chen 
87533842cedSCho, Yu-Chen 
87633842cedSCho, Yu-Chen 	/* Read Extra Data */
87733842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
87833842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
87933842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x4;
88033842cedSCho, Yu-Chen 	bcb->Flags      = 0x80;
88133842cedSCho, Yu-Chen 	bcb->CDB[0]     = 0xF1;
88233842cedSCho, Yu-Chen 	bcb->CDB[1]     = 0x03;
88333842cedSCho, Yu-Chen 
88433842cedSCho, Yu-Chen 	bcb->CDB[5]     = (unsigned char)(PageNum);
88533842cedSCho, Yu-Chen 	bcb->CDB[4]     = (unsigned char)(PhyBlockAddr);
88633842cedSCho, Yu-Chen 	bcb->CDB[3]     = (unsigned char)(PhyBlockAddr>>8);
88733842cedSCho, Yu-Chen 	bcb->CDB[2]     = (unsigned char)(PhyBlockAddr>>16);
88833842cedSCho, Yu-Chen 	bcb->CDB[6]     = 0x01;
88933842cedSCho, Yu-Chen 
89033842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_READ, &ExtBuf, 0);
89133842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
89233842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
89333842cedSCho, Yu-Chen 
89433842cedSCho, Yu-Chen 	ExtraDat->reserved = 0;
89533842cedSCho, Yu-Chen 	ExtraDat->intr     = 0x80;  /* Not yet,fireware support */
89633842cedSCho, Yu-Chen 	ExtraDat->status0  = 0x10;  /* Not yet,fireware support */
89733842cedSCho, Yu-Chen 
89833842cedSCho, Yu-Chen 	ExtraDat->status1  = 0x00;  /* Not yet,fireware support */
89933842cedSCho, Yu-Chen 	ExtraDat->ovrflg   = ExtBuf[0];
90033842cedSCho, Yu-Chen 	ExtraDat->mngflg   = ExtBuf[1];
90133842cedSCho, Yu-Chen 	ExtraDat->logadr   = memstick_logaddr(ExtBuf[2], ExtBuf[3]);
90233842cedSCho, Yu-Chen 
90333842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
90433842cedSCho, Yu-Chen }
90533842cedSCho, Yu-Chen 
90633842cedSCho, Yu-Chen static int ms_lib_process_bootblock(struct us_data *us, u16 PhyBlock, u8 *PageData)
90733842cedSCho, Yu-Chen {
90833842cedSCho, Yu-Chen 	struct ms_bootblock_sysent *SysEntry;
90933842cedSCho, Yu-Chen 	struct ms_bootblock_sysinf *SysInfo;
91033842cedSCho, Yu-Chen 	u32 i, result;
91133842cedSCho, Yu-Chen 	u8 PageNumber;
91233842cedSCho, Yu-Chen 	u8 *PageBuffer;
91333842cedSCho, Yu-Chen 	struct ms_lib_type_extdat ExtraData;
91433842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
91533842cedSCho, Yu-Chen 
91633842cedSCho, Yu-Chen 	PageBuffer = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
91733842cedSCho, Yu-Chen 	if (PageBuffer == NULL)
91833842cedSCho, Yu-Chen 		return (u32)-1;
91933842cedSCho, Yu-Chen 
92033842cedSCho, Yu-Chen 	result = (u32)-1;
92133842cedSCho, Yu-Chen 
92233842cedSCho, Yu-Chen 	SysInfo = &(((struct ms_bootblock_page0 *)PageData)->sysinf);
92333842cedSCho, Yu-Chen 
92433842cedSCho, Yu-Chen 	if ((SysInfo->bMsClass != MS_SYSINF_MSCLASS_TYPE_1) ||
92533842cedSCho, Yu-Chen 		(be16_to_cpu(SysInfo->wPageSize) != MS_SYSINF_PAGE_SIZE) ||
92633842cedSCho, Yu-Chen 		((SysInfo->bSecuritySupport & MS_SYSINF_SECURITY) == MS_SYSINF_SECURITY_SUPPORT) ||
92733842cedSCho, Yu-Chen 		(SysInfo->bReserved1 != MS_SYSINF_RESERVED1) ||
92833842cedSCho, Yu-Chen 		(SysInfo->bReserved2 != MS_SYSINF_RESERVED2) ||
92933842cedSCho, Yu-Chen 		(SysInfo->bFormatType != MS_SYSINF_FORMAT_FAT) ||
93033842cedSCho, Yu-Chen 		(SysInfo->bUsage != MS_SYSINF_USAGE_GENERAL))
93133842cedSCho, Yu-Chen 		goto exit;
93233842cedSCho, Yu-Chen 		/* */
93333842cedSCho, Yu-Chen 	switch (info->MS_Lib.cardType = SysInfo->bCardType) {
93433842cedSCho, Yu-Chen 	case MS_SYSINF_CARDTYPE_RDONLY:
93533842cedSCho, Yu-Chen 		ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY);
93633842cedSCho, Yu-Chen 		break;
93733842cedSCho, Yu-Chen 	case MS_SYSINF_CARDTYPE_RDWR:
93833842cedSCho, Yu-Chen 		ms_lib_ctrl_reset(info, MS_LIB_CTRL_RDONLY);
93933842cedSCho, Yu-Chen 		break;
94033842cedSCho, Yu-Chen 	case MS_SYSINF_CARDTYPE_HYBRID:
94133842cedSCho, Yu-Chen 	default:
94233842cedSCho, Yu-Chen 		goto exit;
94333842cedSCho, Yu-Chen 	}
94433842cedSCho, Yu-Chen 
94533842cedSCho, Yu-Chen 	info->MS_Lib.blockSize = be16_to_cpu(SysInfo->wBlockSize);
94633842cedSCho, Yu-Chen 	info->MS_Lib.NumberOfPhyBlock = be16_to_cpu(SysInfo->wBlockNumber);
94733842cedSCho, Yu-Chen 	info->MS_Lib.NumberOfLogBlock = be16_to_cpu(SysInfo->wTotalBlockNumber)-2;
94833842cedSCho, Yu-Chen 	info->MS_Lib.PagesPerBlock = info->MS_Lib.blockSize * SIZE_OF_KIRO / MS_BYTES_PER_PAGE;
94933842cedSCho, Yu-Chen 	info->MS_Lib.NumberOfSegment = info->MS_Lib.NumberOfPhyBlock / MS_PHYSICAL_BLOCKS_PER_SEGMENT;
95033842cedSCho, Yu-Chen 	info->MS_Model = be16_to_cpu(SysInfo->wMemorySize);
95133842cedSCho, Yu-Chen 
95233842cedSCho, Yu-Chen 	/*Allocate to all number of logicalblock and physicalblock */
95333842cedSCho, Yu-Chen 	if (ms_lib_alloc_logicalmap(us))
95433842cedSCho, Yu-Chen 		goto exit;
95533842cedSCho, Yu-Chen 
95633842cedSCho, Yu-Chen 	/* Mark the book block */
95733842cedSCho, Yu-Chen 	ms_lib_set_bootblockmark(us, PhyBlock);
95833842cedSCho, Yu-Chen 
95933842cedSCho, Yu-Chen 	SysEntry = &(((struct ms_bootblock_page0 *)PageData)->sysent);
96033842cedSCho, Yu-Chen 
96133842cedSCho, Yu-Chen 	for (i = 0; i < MS_NUMBER_OF_SYSTEM_ENTRY; i++) {
96233842cedSCho, Yu-Chen 		u32  EntryOffset, EntrySize;
96333842cedSCho, Yu-Chen 
96433842cedSCho, Yu-Chen 		EntryOffset = be32_to_cpu(SysEntry->entry[i].dwStart);
96533842cedSCho, Yu-Chen 
96633842cedSCho, Yu-Chen 		if (EntryOffset == 0xffffff)
96733842cedSCho, Yu-Chen 			continue;
96833842cedSCho, Yu-Chen 		EntrySize = be32_to_cpu(SysEntry->entry[i].dwSize);
96933842cedSCho, Yu-Chen 
97033842cedSCho, Yu-Chen 		if (EntrySize == 0)
97133842cedSCho, Yu-Chen 			continue;
97233842cedSCho, Yu-Chen 
97333842cedSCho, Yu-Chen 		if (EntryOffset + MS_BYTES_PER_PAGE + EntrySize > info->MS_Lib.blockSize * (u32)SIZE_OF_KIRO)
97433842cedSCho, Yu-Chen 			continue;
97533842cedSCho, Yu-Chen 
97633842cedSCho, Yu-Chen 		if (i == 0) {
97733842cedSCho, Yu-Chen 			u8 PrevPageNumber = 0;
97833842cedSCho, Yu-Chen 			u16 phyblk;
97933842cedSCho, Yu-Chen 
98033842cedSCho, Yu-Chen 			if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_INVALID_BLOCK)
98133842cedSCho, Yu-Chen 				goto exit;
98233842cedSCho, Yu-Chen 
98333842cedSCho, Yu-Chen 			while (EntrySize > 0) {
98433842cedSCho, Yu-Chen 
98533842cedSCho, Yu-Chen 				PageNumber = (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1);
98633842cedSCho, Yu-Chen 				if (PageNumber != PrevPageNumber) {
98733842cedSCho, Yu-Chen 					switch (ms_read_readpage(us, PhyBlock, PageNumber, (u32 *)PageBuffer, &ExtraData)) {
98833842cedSCho, Yu-Chen 					case MS_STATUS_SUCCESS:
98933842cedSCho, Yu-Chen 						break;
99033842cedSCho, Yu-Chen 					case MS_STATUS_WRITE_PROTECT:
99133842cedSCho, Yu-Chen 					case MS_ERROR_FLASH_READ:
99233842cedSCho, Yu-Chen 					case MS_STATUS_ERROR:
99333842cedSCho, Yu-Chen 					default:
99433842cedSCho, Yu-Chen 						goto exit;
99533842cedSCho, Yu-Chen 					}
99633842cedSCho, Yu-Chen 
99733842cedSCho, Yu-Chen 					PrevPageNumber = PageNumber;
99833842cedSCho, Yu-Chen 				}
99933842cedSCho, Yu-Chen 
100033842cedSCho, Yu-Chen 				phyblk = be16_to_cpu(*(u16 *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)));
100133842cedSCho, Yu-Chen 				if (phyblk < 0x0fff)
100233842cedSCho, Yu-Chen 					ms_lib_set_initialerrorblock(us, phyblk);
100333842cedSCho, Yu-Chen 
100433842cedSCho, Yu-Chen 				EntryOffset += 2;
100533842cedSCho, Yu-Chen 				EntrySize -= 2;
100633842cedSCho, Yu-Chen 			}
100733842cedSCho, Yu-Chen 		} else if (i == 1) {  /* CIS/IDI */
100833842cedSCho, Yu-Chen 			struct ms_bootblock_idi *idi;
100933842cedSCho, Yu-Chen 
101033842cedSCho, Yu-Chen 			if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_CIS_IDI)
101133842cedSCho, Yu-Chen 				goto exit;
101233842cedSCho, Yu-Chen 
101333842cedSCho, Yu-Chen 			switch (ms_read_readpage(us, PhyBlock, (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1), (u32 *)PageBuffer, &ExtraData)) {
101433842cedSCho, Yu-Chen 			case MS_STATUS_SUCCESS:
101533842cedSCho, Yu-Chen 				break;
101633842cedSCho, Yu-Chen 			case MS_STATUS_WRITE_PROTECT:
101733842cedSCho, Yu-Chen 			case MS_ERROR_FLASH_READ:
101833842cedSCho, Yu-Chen 			case MS_STATUS_ERROR:
101933842cedSCho, Yu-Chen 			default:
102033842cedSCho, Yu-Chen 				goto exit;
102133842cedSCho, Yu-Chen 			}
102233842cedSCho, Yu-Chen 
102333842cedSCho, Yu-Chen 			idi = &((struct ms_bootblock_cis_idi *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)))->idi.idi;
102433842cedSCho, Yu-Chen 			if (le16_to_cpu(idi->wIDIgeneralConfiguration) != MS_IDI_GENERAL_CONF)
102533842cedSCho, Yu-Chen 				goto exit;
102633842cedSCho, Yu-Chen 
102733842cedSCho, Yu-Chen 			info->MS_Lib.BytesPerSector = le16_to_cpu(idi->wIDIbytesPerSector);
102833842cedSCho, Yu-Chen 			if (info->MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE)
102933842cedSCho, Yu-Chen 				goto exit;
103033842cedSCho, Yu-Chen 		}
103133842cedSCho, Yu-Chen 	} /* End for .. */
103233842cedSCho, Yu-Chen 
103333842cedSCho, Yu-Chen 	result = 0;
103433842cedSCho, Yu-Chen 
103533842cedSCho, Yu-Chen exit:
103633842cedSCho, Yu-Chen 	if (result)
103733842cedSCho, Yu-Chen 		ms_lib_free_logicalmap(us);
103833842cedSCho, Yu-Chen 
103933842cedSCho, Yu-Chen 	kfree(PageBuffer);
104033842cedSCho, Yu-Chen 
104133842cedSCho, Yu-Chen 	result = 0;
104233842cedSCho, Yu-Chen 	return result;
104333842cedSCho, Yu-Chen }
104433842cedSCho, Yu-Chen 
104533842cedSCho, Yu-Chen static void ms_lib_free_writebuf(struct us_data *us)
104633842cedSCho, Yu-Chen {
104733842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
104833842cedSCho, Yu-Chen 	info->MS_Lib.wrtblk = (u16)-1; /* set to -1 */
104933842cedSCho, Yu-Chen 
105033842cedSCho, Yu-Chen 	/* memset((fdoExt)->MS_Lib.pagemap, 0, sizeof((fdoExt)->MS_Lib.pagemap)) */
105133842cedSCho, Yu-Chen 
105233842cedSCho, Yu-Chen 	ms_lib_clear_pagemap(info); /* (pdx)->MS_Lib.pagemap memset 0 in ms.h */
105333842cedSCho, Yu-Chen 
105433842cedSCho, Yu-Chen 	if (info->MS_Lib.blkpag) {
105533842cedSCho, Yu-Chen 		kfree((u8 *)(info->MS_Lib.blkpag));  /* Arnold test ... */
105633842cedSCho, Yu-Chen 		info->MS_Lib.blkpag = NULL;
105733842cedSCho, Yu-Chen 	}
105833842cedSCho, Yu-Chen 
105933842cedSCho, Yu-Chen 	if (info->MS_Lib.blkext) {
106033842cedSCho, Yu-Chen 		kfree((u8 *)(info->MS_Lib.blkext));  /* Arnold test ... */
106133842cedSCho, Yu-Chen 		info->MS_Lib.blkext = NULL;
106233842cedSCho, Yu-Chen 	}
106333842cedSCho, Yu-Chen }
106433842cedSCho, Yu-Chen 
106533842cedSCho, Yu-Chen 
106633842cedSCho, Yu-Chen static void ms_lib_free_allocatedarea(struct us_data *us)
106733842cedSCho, Yu-Chen {
106833842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
106933842cedSCho, Yu-Chen 
107033842cedSCho, Yu-Chen 	ms_lib_free_writebuf(us); /* Free MS_Lib.pagemap */
107133842cedSCho, Yu-Chen 	ms_lib_free_logicalmap(us); /* kfree MS_Lib.Phy2LogMap and MS_Lib.Log2PhyMap */
107233842cedSCho, Yu-Chen 
107333842cedSCho, Yu-Chen 	/* set struct us point flag to 0 */
107433842cedSCho, Yu-Chen 	info->MS_Lib.flags = 0;
107533842cedSCho, Yu-Chen 	info->MS_Lib.BytesPerSector = 0;
107633842cedSCho, Yu-Chen 	info->MS_Lib.SectorsPerCylinder = 0;
107733842cedSCho, Yu-Chen 
107833842cedSCho, Yu-Chen 	info->MS_Lib.cardType = 0;
107933842cedSCho, Yu-Chen 	info->MS_Lib.blockSize = 0;
108033842cedSCho, Yu-Chen 	info->MS_Lib.PagesPerBlock = 0;
108133842cedSCho, Yu-Chen 
108233842cedSCho, Yu-Chen 	info->MS_Lib.NumberOfPhyBlock = 0;
108333842cedSCho, Yu-Chen 	info->MS_Lib.NumberOfLogBlock = 0;
108433842cedSCho, Yu-Chen }
108533842cedSCho, Yu-Chen 
108633842cedSCho, Yu-Chen 
108733842cedSCho, Yu-Chen static int ms_lib_alloc_writebuf(struct us_data *us)
108833842cedSCho, Yu-Chen {
108933842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
109033842cedSCho, Yu-Chen 
109133842cedSCho, Yu-Chen 	info->MS_Lib.wrtblk = (u16)-1;
109233842cedSCho, Yu-Chen 
109333842cedSCho, Yu-Chen 	info->MS_Lib.blkpag = kmalloc(info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector, GFP_KERNEL);
109433842cedSCho, Yu-Chen 	info->MS_Lib.blkext = kmalloc(info->MS_Lib.PagesPerBlock * sizeof(struct ms_lib_type_extdat), GFP_KERNEL);
109533842cedSCho, Yu-Chen 
109633842cedSCho, Yu-Chen 	if ((info->MS_Lib.blkpag == NULL) || (info->MS_Lib.blkext == NULL)) {
109733842cedSCho, Yu-Chen 		ms_lib_free_writebuf(us);
109833842cedSCho, Yu-Chen 		return (u32)-1;
109933842cedSCho, Yu-Chen 	}
110033842cedSCho, Yu-Chen 
110133842cedSCho, Yu-Chen 	ms_lib_clear_writebuf(us);
110233842cedSCho, Yu-Chen 
110333842cedSCho, Yu-Chen return 0;
110433842cedSCho, Yu-Chen }
110533842cedSCho, Yu-Chen 
110633842cedSCho, Yu-Chen static int ms_lib_force_setlogical_pair(struct us_data *us, u16 logblk, u16 phyblk)
110733842cedSCho, Yu-Chen {
110833842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
110933842cedSCho, Yu-Chen 
111033842cedSCho, Yu-Chen 	if (logblk == MS_LB_NOT_USED)
111133842cedSCho, Yu-Chen 		return 0;
111233842cedSCho, Yu-Chen 
111333842cedSCho, Yu-Chen 	if ((logblk >= info->MS_Lib.NumberOfLogBlock) ||
111433842cedSCho, Yu-Chen 		(phyblk >= info->MS_Lib.NumberOfPhyBlock))
111533842cedSCho, Yu-Chen 		return (u32)-1;
111633842cedSCho, Yu-Chen 
111733842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap[phyblk] = logblk;
111833842cedSCho, Yu-Chen 	info->MS_Lib.Log2PhyMap[logblk] = phyblk;
111933842cedSCho, Yu-Chen 
112033842cedSCho, Yu-Chen 	return 0;
112133842cedSCho, Yu-Chen }
112233842cedSCho, Yu-Chen 
112333842cedSCho, Yu-Chen static int ms_read_copyblock(struct us_data *us, u16 oldphy, u16 newphy,
112433842cedSCho, Yu-Chen 			u16 PhyBlockAddr, u8 PageNum, unsigned char *buf, u16 len)
112533842cedSCho, Yu-Chen {
112633842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
112733842cedSCho, Yu-Chen 	int result;
112833842cedSCho, Yu-Chen 
112933842cedSCho, Yu-Chen 	/* printk(KERN_INFO "MS_ReaderCopyBlock --- PhyBlockAddr = %x,
113033842cedSCho, Yu-Chen 		PageNum = %x\n", PhyBlockAddr, PageNum); */
113133842cedSCho, Yu-Chen 	result = ene_load_bincode(us, MS_RW_PATTERN);
113233842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
113333842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
113433842cedSCho, Yu-Chen 
113533842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
113633842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
113733842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x200*len;
113833842cedSCho, Yu-Chen 	bcb->Flags = 0x00;
113933842cedSCho, Yu-Chen 	bcb->CDB[0] = 0xF0;
114033842cedSCho, Yu-Chen 	bcb->CDB[1] = 0x08;
114133842cedSCho, Yu-Chen 	bcb->CDB[4] = (unsigned char)(oldphy);
114233842cedSCho, Yu-Chen 	bcb->CDB[3] = (unsigned char)(oldphy>>8);
114333842cedSCho, Yu-Chen 	bcb->CDB[2] = 0; /* (BYTE)(oldphy>>16) */
114433842cedSCho, Yu-Chen 	bcb->CDB[7] = (unsigned char)(newphy);
114533842cedSCho, Yu-Chen 	bcb->CDB[6] = (unsigned char)(newphy>>8);
114633842cedSCho, Yu-Chen 	bcb->CDB[5] = 0; /* (BYTE)(newphy>>16) */
114733842cedSCho, Yu-Chen 	bcb->CDB[9] = (unsigned char)(PhyBlockAddr);
114833842cedSCho, Yu-Chen 	bcb->CDB[8] = (unsigned char)(PhyBlockAddr>>8);
114933842cedSCho, Yu-Chen 	bcb->CDB[10] = PageNum;
115033842cedSCho, Yu-Chen 
115133842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
115233842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
115333842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
115433842cedSCho, Yu-Chen 
115533842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
115633842cedSCho, Yu-Chen }
115733842cedSCho, Yu-Chen 
115833842cedSCho, Yu-Chen static int ms_read_eraseblock(struct us_data *us, u32 PhyBlockAddr)
115933842cedSCho, Yu-Chen {
116033842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
116133842cedSCho, Yu-Chen 	int result;
116233842cedSCho, Yu-Chen 	u32 bn = PhyBlockAddr;
116333842cedSCho, Yu-Chen 
116433842cedSCho, Yu-Chen 	/* printk(KERN_INFO "MS --- ms_read_eraseblock,
116533842cedSCho, Yu-Chen 			PhyBlockAddr = %x\n", PhyBlockAddr); */
116633842cedSCho, Yu-Chen 	result = ene_load_bincode(us, MS_RW_PATTERN);
116733842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
116833842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
116933842cedSCho, Yu-Chen 
117033842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
117133842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
117233842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x200;
117333842cedSCho, Yu-Chen 	bcb->Flags = 0x80;
117433842cedSCho, Yu-Chen 	bcb->CDB[0] = 0xF2;
117533842cedSCho, Yu-Chen 	bcb->CDB[1] = 0x06;
117633842cedSCho, Yu-Chen 	bcb->CDB[4] = (unsigned char)(bn);
117733842cedSCho, Yu-Chen 	bcb->CDB[3] = (unsigned char)(bn>>8);
117833842cedSCho, Yu-Chen 	bcb->CDB[2] = (unsigned char)(bn>>16);
117933842cedSCho, Yu-Chen 
118033842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
118133842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
118233842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
118333842cedSCho, Yu-Chen 
118433842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
118533842cedSCho, Yu-Chen }
118633842cedSCho, Yu-Chen 
118733842cedSCho, Yu-Chen static int ms_lib_check_disableblock(struct us_data *us, u16 PhyBlock)
118833842cedSCho, Yu-Chen {
118933842cedSCho, Yu-Chen 	unsigned char *PageBuf = NULL;
119033842cedSCho, Yu-Chen 	u16 result = MS_STATUS_SUCCESS;
119133842cedSCho, Yu-Chen 	u16 blk, index = 0;
119233842cedSCho, Yu-Chen 	struct ms_lib_type_extdat extdat;
119333842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
119433842cedSCho, Yu-Chen 
119533842cedSCho, Yu-Chen 	PageBuf = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
119633842cedSCho, Yu-Chen 	if (PageBuf == NULL) {
119733842cedSCho, Yu-Chen 		result = MS_NO_MEMORY_ERROR;
119833842cedSCho, Yu-Chen 		goto exit;
119933842cedSCho, Yu-Chen 	}
120033842cedSCho, Yu-Chen 
120133842cedSCho, Yu-Chen 	ms_read_readpage(us, PhyBlock, 1, (u32 *)PageBuf, &extdat);
120233842cedSCho, Yu-Chen 	do {
120333842cedSCho, Yu-Chen 		blk = be16_to_cpu(PageBuf[index]);
120433842cedSCho, Yu-Chen 		if (blk == MS_LB_NOT_USED)
120533842cedSCho, Yu-Chen 			break;
120633842cedSCho, Yu-Chen 		if (blk == info->MS_Lib.Log2PhyMap[0]) {
120733842cedSCho, Yu-Chen 			result = MS_ERROR_FLASH_READ;
120833842cedSCho, Yu-Chen 			break;
120933842cedSCho, Yu-Chen 		}
121033842cedSCho, Yu-Chen 		index++;
121133842cedSCho, Yu-Chen 	} while (1);
121233842cedSCho, Yu-Chen 
121333842cedSCho, Yu-Chen exit:
121433842cedSCho, Yu-Chen 	kfree(PageBuf);
121533842cedSCho, Yu-Chen 	return result;
121633842cedSCho, Yu-Chen }
121733842cedSCho, Yu-Chen 
121833842cedSCho, Yu-Chen static int ms_lib_setacquired_errorblock(struct us_data *us, u16 phyblk)
121933842cedSCho, Yu-Chen {
122033842cedSCho, Yu-Chen 	u16 log;
122133842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
122233842cedSCho, Yu-Chen 
122333842cedSCho, Yu-Chen 	if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
122433842cedSCho, Yu-Chen 		return (u32)-1;
122533842cedSCho, Yu-Chen 
122633842cedSCho, Yu-Chen 	log = info->MS_Lib.Phy2LogMap[phyblk];
122733842cedSCho, Yu-Chen 
122833842cedSCho, Yu-Chen 	if (log < info->MS_Lib.NumberOfLogBlock)
122933842cedSCho, Yu-Chen 		info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
123033842cedSCho, Yu-Chen 
123133842cedSCho, Yu-Chen 	if (info->MS_Lib.Phy2LogMap[phyblk] != MS_LB_INITIAL_ERROR)
123233842cedSCho, Yu-Chen 		info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_ACQUIRED_ERROR;
123333842cedSCho, Yu-Chen 
123433842cedSCho, Yu-Chen 	return 0;
123533842cedSCho, Yu-Chen }
123633842cedSCho, Yu-Chen 
123733842cedSCho, Yu-Chen static int ms_lib_overwrite_extra(struct us_data *us, u32 PhyBlockAddr,
123833842cedSCho, Yu-Chen 				u8 PageNum, u8 OverwriteFlag)
123933842cedSCho, Yu-Chen {
124033842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
124133842cedSCho, Yu-Chen 	int result;
124233842cedSCho, Yu-Chen 
124333842cedSCho, Yu-Chen 	/* printk("MS --- MS_LibOverwriteExtra,
124433842cedSCho, Yu-Chen 		PhyBlockAddr = %x, PageNum = %x\n", PhyBlockAddr, PageNum); */
124533842cedSCho, Yu-Chen 	result = ene_load_bincode(us, MS_RW_PATTERN);
124633842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
124733842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
124833842cedSCho, Yu-Chen 
124933842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
125033842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
125133842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x4;
125233842cedSCho, Yu-Chen 	bcb->Flags = 0x80;
125333842cedSCho, Yu-Chen 	bcb->CDB[0] = 0xF2;
125433842cedSCho, Yu-Chen 	bcb->CDB[1] = 0x05;
125533842cedSCho, Yu-Chen 	bcb->CDB[5] = (unsigned char)(PageNum);
125633842cedSCho, Yu-Chen 	bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
125733842cedSCho, Yu-Chen 	bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
125833842cedSCho, Yu-Chen 	bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
125933842cedSCho, Yu-Chen 	bcb->CDB[6] = OverwriteFlag;
126033842cedSCho, Yu-Chen 	bcb->CDB[7] = 0xFF;
126133842cedSCho, Yu-Chen 	bcb->CDB[8] = 0xFF;
126233842cedSCho, Yu-Chen 	bcb->CDB[9] = 0xFF;
126333842cedSCho, Yu-Chen 
126433842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
126533842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
126633842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
126733842cedSCho, Yu-Chen 
126833842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
126933842cedSCho, Yu-Chen }
127033842cedSCho, Yu-Chen 
127133842cedSCho, Yu-Chen static int ms_lib_error_phyblock(struct us_data *us, u16 phyblk)
127233842cedSCho, Yu-Chen {
127333842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
127433842cedSCho, Yu-Chen 
127533842cedSCho, Yu-Chen 	if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
127633842cedSCho, Yu-Chen 		return MS_STATUS_ERROR;
127733842cedSCho, Yu-Chen 
127833842cedSCho, Yu-Chen 	ms_lib_setacquired_errorblock(us, phyblk);
127933842cedSCho, Yu-Chen 
128033842cedSCho, Yu-Chen 	if (ms_lib_iswritable(info))
128133842cedSCho, Yu-Chen 		return ms_lib_overwrite_extra(us, phyblk, 0, (u8)(~MS_REG_OVR_BKST & BYTE_MASK));
128233842cedSCho, Yu-Chen 
128333842cedSCho, Yu-Chen 	return MS_STATUS_SUCCESS;
128433842cedSCho, Yu-Chen }
128533842cedSCho, Yu-Chen 
128633842cedSCho, Yu-Chen static int ms_lib_erase_phyblock(struct us_data *us, u16 phyblk)
128733842cedSCho, Yu-Chen {
128833842cedSCho, Yu-Chen 	u16 log;
128933842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
129033842cedSCho, Yu-Chen 
129133842cedSCho, Yu-Chen 	if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
129233842cedSCho, Yu-Chen 		return MS_STATUS_ERROR;
129333842cedSCho, Yu-Chen 
129433842cedSCho, Yu-Chen 	log = info->MS_Lib.Phy2LogMap[phyblk];
129533842cedSCho, Yu-Chen 
129633842cedSCho, Yu-Chen 	if (log < info->MS_Lib.NumberOfLogBlock)
129733842cedSCho, Yu-Chen 		info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
129833842cedSCho, Yu-Chen 
129933842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED;
130033842cedSCho, Yu-Chen 
130133842cedSCho, Yu-Chen 	if (ms_lib_iswritable(info)) {
130233842cedSCho, Yu-Chen 		switch (ms_read_eraseblock(us, phyblk)) {
130333842cedSCho, Yu-Chen 		case MS_STATUS_SUCCESS:
130433842cedSCho, Yu-Chen 			info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED_ERASED;
130533842cedSCho, Yu-Chen 			return MS_STATUS_SUCCESS;
130633842cedSCho, Yu-Chen 		case MS_ERROR_FLASH_ERASE:
130733842cedSCho, Yu-Chen 		case MS_STATUS_INT_ERROR:
130833842cedSCho, Yu-Chen 			ms_lib_error_phyblock(us, phyblk);
130933842cedSCho, Yu-Chen 			return MS_ERROR_FLASH_ERASE;
131033842cedSCho, Yu-Chen 		case MS_STATUS_ERROR:
131133842cedSCho, Yu-Chen 		default:
131233842cedSCho, Yu-Chen 			ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY); /* MS_LibCtrlSet will used by ENE_MSInit ,need check, and why us to info*/
131333842cedSCho, Yu-Chen 			ms_lib_setacquired_errorblock(us, phyblk);
131433842cedSCho, Yu-Chen 			return MS_STATUS_ERROR;
131533842cedSCho, Yu-Chen 		}
131633842cedSCho, Yu-Chen 	}
131733842cedSCho, Yu-Chen 
131833842cedSCho, Yu-Chen 	ms_lib_setacquired_errorblock(us, phyblk);
131933842cedSCho, Yu-Chen 
132033842cedSCho, Yu-Chen 	return MS_STATUS_SUCCESS;
132133842cedSCho, Yu-Chen }
132233842cedSCho, Yu-Chen 
132333842cedSCho, Yu-Chen static int ms_lib_read_extra(struct us_data *us, u32 PhyBlock,
132433842cedSCho, Yu-Chen 				u8 PageNum, struct ms_lib_type_extdat *ExtraDat)
132533842cedSCho, Yu-Chen {
132633842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
132733842cedSCho, Yu-Chen 	int result;
132833842cedSCho, Yu-Chen 	u8 ExtBuf[4];
132933842cedSCho, Yu-Chen 
133033842cedSCho, Yu-Chen 	/* printk("MS_LibReadExtra --- PhyBlock = %x, PageNum = %x\n", PhyBlock, PageNum); */
133133842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
133233842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
133333842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x4;
133433842cedSCho, Yu-Chen 	bcb->Flags      = 0x80;
133533842cedSCho, Yu-Chen 	bcb->CDB[0]     = 0xF1;
133633842cedSCho, Yu-Chen 	bcb->CDB[1]     = 0x03;
133733842cedSCho, Yu-Chen 	bcb->CDB[5]     = (unsigned char)(PageNum);
133833842cedSCho, Yu-Chen 	bcb->CDB[4]     = (unsigned char)(PhyBlock);
133933842cedSCho, Yu-Chen 	bcb->CDB[3]     = (unsigned char)(PhyBlock>>8);
134033842cedSCho, Yu-Chen 	bcb->CDB[2]     = (unsigned char)(PhyBlock>>16);
134133842cedSCho, Yu-Chen 	bcb->CDB[6]     = 0x01;
134233842cedSCho, Yu-Chen 
134333842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_READ, &ExtBuf, 0);
134433842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
134533842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
134633842cedSCho, Yu-Chen 
134733842cedSCho, Yu-Chen 	ExtraDat->reserved = 0;
134833842cedSCho, Yu-Chen 	ExtraDat->intr     = 0x80;  /* Not yet, waiting for fireware support */
134933842cedSCho, Yu-Chen 	ExtraDat->status0  = 0x10;  /* Not yet, waiting for fireware support */
135033842cedSCho, Yu-Chen 	ExtraDat->status1  = 0x00;  /* Not yet, waiting for fireware support */
135133842cedSCho, Yu-Chen 	ExtraDat->ovrflg   = ExtBuf[0];
135233842cedSCho, Yu-Chen 	ExtraDat->mngflg   = ExtBuf[1];
135333842cedSCho, Yu-Chen 	ExtraDat->logadr   = memstick_logaddr(ExtBuf[2], ExtBuf[3]);
135433842cedSCho, Yu-Chen 
135533842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
135633842cedSCho, Yu-Chen }
135733842cedSCho, Yu-Chen 
135833842cedSCho, Yu-Chen static int ms_libsearch_block_from_physical(struct us_data *us, u16 phyblk)
135933842cedSCho, Yu-Chen {
136033842cedSCho, Yu-Chen 	u16 Newblk;
136133842cedSCho, Yu-Chen 	u16 blk;
136233842cedSCho, Yu-Chen 	struct ms_lib_type_extdat extdat; /* need check */
136333842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
136433842cedSCho, Yu-Chen 
136533842cedSCho, Yu-Chen 
136633842cedSCho, Yu-Chen 	if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
136733842cedSCho, Yu-Chen 		return MS_LB_ERROR;
136833842cedSCho, Yu-Chen 
136933842cedSCho, Yu-Chen 	for (blk = phyblk + 1; blk != phyblk; blk++) {
137033842cedSCho, Yu-Chen 		if ((blk & MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK) == 0)
137133842cedSCho, Yu-Chen 			blk -= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
137233842cedSCho, Yu-Chen 
137333842cedSCho, Yu-Chen 		Newblk = info->MS_Lib.Phy2LogMap[blk];
137433842cedSCho, Yu-Chen 		if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED_ERASED) {
137533842cedSCho, Yu-Chen 			return blk;
137633842cedSCho, Yu-Chen 		} else if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED) {
137733842cedSCho, Yu-Chen 			switch (ms_lib_read_extra(us, blk, 0, &extdat)) {
137833842cedSCho, Yu-Chen 			case MS_STATUS_SUCCESS:
137933842cedSCho, Yu-Chen 			case MS_STATUS_SUCCESS_WITH_ECC:
138033842cedSCho, Yu-Chen 				break;
138133842cedSCho, Yu-Chen 			case MS_NOCARD_ERROR:
138233842cedSCho, Yu-Chen 				return MS_NOCARD_ERROR;
138333842cedSCho, Yu-Chen 			case MS_STATUS_INT_ERROR:
138433842cedSCho, Yu-Chen 				return MS_LB_ERROR;
138533842cedSCho, Yu-Chen 			case MS_ERROR_FLASH_READ:
138633842cedSCho, Yu-Chen 			default:
138733842cedSCho, Yu-Chen 				ms_lib_setacquired_errorblock(us, blk);
138833842cedSCho, Yu-Chen 				continue;
138933842cedSCho, Yu-Chen 			} /* End switch */
139033842cedSCho, Yu-Chen 
139133842cedSCho, Yu-Chen 			if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
139233842cedSCho, Yu-Chen 				ms_lib_setacquired_errorblock(us, blk);
139333842cedSCho, Yu-Chen 				continue;
139433842cedSCho, Yu-Chen 			}
139533842cedSCho, Yu-Chen 
139633842cedSCho, Yu-Chen 			switch (ms_lib_erase_phyblock(us, blk)) {
139733842cedSCho, Yu-Chen 			case MS_STATUS_SUCCESS:
139833842cedSCho, Yu-Chen 				return blk;
139933842cedSCho, Yu-Chen 			case MS_STATUS_ERROR:
140033842cedSCho, Yu-Chen 				return MS_LB_ERROR;
140133842cedSCho, Yu-Chen 			case MS_ERROR_FLASH_ERASE:
140233842cedSCho, Yu-Chen 			default:
140333842cedSCho, Yu-Chen 				ms_lib_error_phyblock(us, blk);
140433842cedSCho, Yu-Chen 				break;
140533842cedSCho, Yu-Chen 			}
140633842cedSCho, Yu-Chen 		}
140733842cedSCho, Yu-Chen 	} /* End for */
140833842cedSCho, Yu-Chen 
140933842cedSCho, Yu-Chen 	return MS_LB_ERROR;
141033842cedSCho, Yu-Chen }
141133842cedSCho, Yu-Chen static int ms_libsearch_block_from_logical(struct us_data *us, u16 logblk)
141233842cedSCho, Yu-Chen {
141333842cedSCho, Yu-Chen 	u16 phyblk;
141433842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
141533842cedSCho, Yu-Chen 
141633842cedSCho, Yu-Chen 	phyblk = ms_libconv_to_physical(info, logblk);
141733842cedSCho, Yu-Chen 	if (phyblk >= MS_LB_ERROR) {
141833842cedSCho, Yu-Chen 		if (logblk >= info->MS_Lib.NumberOfLogBlock)
141933842cedSCho, Yu-Chen 			return MS_LB_ERROR;
142033842cedSCho, Yu-Chen 
142133842cedSCho, Yu-Chen 		phyblk = (logblk + MS_NUMBER_OF_BOOT_BLOCK) / MS_LOGICAL_BLOCKS_PER_SEGMENT;
142233842cedSCho, Yu-Chen 		phyblk *= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
142333842cedSCho, Yu-Chen 		phyblk += MS_PHYSICAL_BLOCKS_PER_SEGMENT - 1;
142433842cedSCho, Yu-Chen 	}
142533842cedSCho, Yu-Chen 
142633842cedSCho, Yu-Chen 	return ms_libsearch_block_from_physical(us, phyblk);
142733842cedSCho, Yu-Chen }
142833842cedSCho, Yu-Chen 
142933842cedSCho, Yu-Chen static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
143033842cedSCho, Yu-Chen {
143133842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
143233842cedSCho, Yu-Chen 
143333842cedSCho, Yu-Chen 	/* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
143433842cedSCho, Yu-Chen 	if (info->MS_Status.Insert && info->MS_Status.Ready) {
143533842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_GOOD;
143633842cedSCho, Yu-Chen 	} else {
143733842cedSCho, Yu-Chen 		ene_ms_init(us);
143833842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_GOOD;
143933842cedSCho, Yu-Chen 	}
144033842cedSCho, Yu-Chen 
144133842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
144233842cedSCho, Yu-Chen }
144333842cedSCho, Yu-Chen 
144433842cedSCho, Yu-Chen static int ms_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
144533842cedSCho, Yu-Chen {
144633842cedSCho, Yu-Chen 	/* pr_info("MS_SCSI_Inquiry\n"); */
144733842cedSCho, Yu-Chen 	unsigned char data_ptr[36] = {
144833842cedSCho, Yu-Chen 		0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
144933842cedSCho, Yu-Chen 		0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
145033842cedSCho, Yu-Chen 		0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
145133842cedSCho, Yu-Chen 		0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30};
145233842cedSCho, Yu-Chen 
145333842cedSCho, Yu-Chen 	usb_stor_set_xfer_buf(data_ptr, 36, srb);
145433842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
145533842cedSCho, Yu-Chen }
145633842cedSCho, Yu-Chen 
145733842cedSCho, Yu-Chen static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
145833842cedSCho, Yu-Chen {
145933842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
146033842cedSCho, Yu-Chen 	unsigned char mediaNoWP[12] = {
146133842cedSCho, Yu-Chen 		0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
146233842cedSCho, Yu-Chen 		0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
146333842cedSCho, Yu-Chen 	unsigned char mediaWP[12]   = {
146433842cedSCho, Yu-Chen 		0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
146533842cedSCho, Yu-Chen 		0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
146633842cedSCho, Yu-Chen 
146733842cedSCho, Yu-Chen 	if (info->MS_Status.WtP)
146833842cedSCho, Yu-Chen 		usb_stor_set_xfer_buf(mediaWP, 12, srb);
146933842cedSCho, Yu-Chen 	else
147033842cedSCho, Yu-Chen 		usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
147133842cedSCho, Yu-Chen 
147233842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
147333842cedSCho, Yu-Chen }
147433842cedSCho, Yu-Chen 
147533842cedSCho, Yu-Chen static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
147633842cedSCho, Yu-Chen {
147733842cedSCho, Yu-Chen 	u32   bl_num;
147833842cedSCho, Yu-Chen 	u16    bl_len;
147933842cedSCho, Yu-Chen 	unsigned int offset = 0;
148033842cedSCho, Yu-Chen 	unsigned char    buf[8];
148133842cedSCho, Yu-Chen 	struct scatterlist *sg = NULL;
148233842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
148333842cedSCho, Yu-Chen 
148433842cedSCho, Yu-Chen 	US_DEBUGP("ms_scsi_read_capacity\n");
148533842cedSCho, Yu-Chen 	bl_len = 0x200;
148633842cedSCho, Yu-Chen 	if (info->MS_Status.IsMSPro)
148733842cedSCho, Yu-Chen 		bl_num = info->MSP_TotalBlock - 1;
148833842cedSCho, Yu-Chen 	else
148933842cedSCho, Yu-Chen 		bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1;
149033842cedSCho, Yu-Chen 
149133842cedSCho, Yu-Chen 	info->bl_num = bl_num;
149233842cedSCho, Yu-Chen 	US_DEBUGP("bl_len = %x\n", bl_len);
149333842cedSCho, Yu-Chen 	US_DEBUGP("bl_num = %x\n", bl_num);
149433842cedSCho, Yu-Chen 
149533842cedSCho, Yu-Chen 	/*srb->request_bufflen = 8; */
149633842cedSCho, Yu-Chen 	buf[0] = (bl_num >> 24) & 0xff;
149733842cedSCho, Yu-Chen 	buf[1] = (bl_num >> 16) & 0xff;
149833842cedSCho, Yu-Chen 	buf[2] = (bl_num >> 8) & 0xff;
149933842cedSCho, Yu-Chen 	buf[3] = (bl_num >> 0) & 0xff;
150033842cedSCho, Yu-Chen 	buf[4] = (bl_len >> 24) & 0xff;
150133842cedSCho, Yu-Chen 	buf[5] = (bl_len >> 16) & 0xff;
150233842cedSCho, Yu-Chen 	buf[6] = (bl_len >> 8) & 0xff;
150333842cedSCho, Yu-Chen 	buf[7] = (bl_len >> 0) & 0xff;
150433842cedSCho, Yu-Chen 
150533842cedSCho, Yu-Chen 	usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
150633842cedSCho, Yu-Chen 
150733842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
150833842cedSCho, Yu-Chen }
150933842cedSCho, Yu-Chen 
151033842cedSCho, Yu-Chen static void ms_lib_phy_to_log_range(u16 PhyBlock, u16 *LogStart, u16 *LogEnde)
151133842cedSCho, Yu-Chen {
151233842cedSCho, Yu-Chen 	PhyBlock /= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
151333842cedSCho, Yu-Chen 
151433842cedSCho, Yu-Chen 	if (PhyBlock) {
151533842cedSCho, Yu-Chen 		*LogStart = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT + (PhyBlock - 1) * MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
151633842cedSCho, Yu-Chen 		*LogEnde = *LogStart + MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
151733842cedSCho, Yu-Chen 	} else {
151833842cedSCho, Yu-Chen 		*LogStart = 0;
151933842cedSCho, Yu-Chen 		*LogEnde = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT;/*494*/
152033842cedSCho, Yu-Chen 	}
152133842cedSCho, Yu-Chen }
152233842cedSCho, Yu-Chen 
152333842cedSCho, Yu-Chen static int ms_lib_read_extrablock(struct us_data *us, u32 PhyBlock,
152433842cedSCho, Yu-Chen 	u8 PageNum, u8 blen, void *buf)
152533842cedSCho, Yu-Chen {
152633842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
152733842cedSCho, Yu-Chen 	int     result;
152833842cedSCho, Yu-Chen 
152933842cedSCho, Yu-Chen 	/* printk("MS_LibReadExtraBlock --- PhyBlock = %x,
153033842cedSCho, Yu-Chen 		PageNum = %x, blen = %x\n", PhyBlock, PageNum, blen); */
153133842cedSCho, Yu-Chen 
153233842cedSCho, Yu-Chen 	/* Read Extra Data */
153333842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
153433842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
153533842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x4 * blen;
153633842cedSCho, Yu-Chen 	bcb->Flags      = 0x80;
153733842cedSCho, Yu-Chen 	bcb->CDB[0]     = 0xF1;
153833842cedSCho, Yu-Chen 	bcb->CDB[1]     = 0x03;
153933842cedSCho, Yu-Chen 	bcb->CDB[5]     = (unsigned char)(PageNum);
154033842cedSCho, Yu-Chen 	bcb->CDB[4]     = (unsigned char)(PhyBlock);
154133842cedSCho, Yu-Chen 	bcb->CDB[3]     = (unsigned char)(PhyBlock>>8);
154233842cedSCho, Yu-Chen 	bcb->CDB[2]     = (unsigned char)(PhyBlock>>16);
154333842cedSCho, Yu-Chen 	bcb->CDB[6]     = blen;
154433842cedSCho, Yu-Chen 
154533842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
154633842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
154733842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
154833842cedSCho, Yu-Chen 
154933842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
155033842cedSCho, Yu-Chen }
155133842cedSCho, Yu-Chen 
155233842cedSCho, Yu-Chen static int ms_lib_scan_logicalblocknumber(struct us_data *us, u16 btBlk1st)
155333842cedSCho, Yu-Chen {
155433842cedSCho, Yu-Chen 	u16 PhyBlock, newblk, i;
155533842cedSCho, Yu-Chen 	u16 LogStart, LogEnde;
155633842cedSCho, Yu-Chen 	struct ms_lib_type_extdat extdat;
155733842cedSCho, Yu-Chen 	u8 buf[0x200];
155833842cedSCho, Yu-Chen 	u32 count = 0, index = 0;
155933842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
156033842cedSCho, Yu-Chen 
156133842cedSCho, Yu-Chen 	for (PhyBlock = 0; PhyBlock < info->MS_Lib.NumberOfPhyBlock;) {
156233842cedSCho, Yu-Chen 		ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde);
156333842cedSCho, Yu-Chen 
156433842cedSCho, Yu-Chen 		for (i = 0; i < MS_PHYSICAL_BLOCKS_PER_SEGMENT; i++, PhyBlock++) {
156533842cedSCho, Yu-Chen 			switch (ms_libconv_to_logical(info, PhyBlock)) {
156633842cedSCho, Yu-Chen 			case MS_STATUS_ERROR:
156733842cedSCho, Yu-Chen 				continue;
156833842cedSCho, Yu-Chen 			default:
156933842cedSCho, Yu-Chen 				break;
157033842cedSCho, Yu-Chen 			}
157133842cedSCho, Yu-Chen 
157233842cedSCho, Yu-Chen 			if (count == PhyBlock) {
157333842cedSCho, Yu-Chen 				ms_lib_read_extrablock(us, PhyBlock, 0, 0x80, &buf);
157433842cedSCho, Yu-Chen 				count += 0x80;
157533842cedSCho, Yu-Chen 			}
157633842cedSCho, Yu-Chen 			index = (PhyBlock % 0x80) * 4;
157733842cedSCho, Yu-Chen 
157833842cedSCho, Yu-Chen 			extdat.ovrflg = buf[index];
157933842cedSCho, Yu-Chen 			extdat.mngflg = buf[index+1];
158033842cedSCho, Yu-Chen 			extdat.logadr = memstick_logaddr(buf[index+2], buf[index+3]);
158133842cedSCho, Yu-Chen 
158233842cedSCho, Yu-Chen 			if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
158333842cedSCho, Yu-Chen 				ms_lib_setacquired_errorblock(us, PhyBlock);
158433842cedSCho, Yu-Chen 				continue;
158533842cedSCho, Yu-Chen 			}
158633842cedSCho, Yu-Chen 
158733842cedSCho, Yu-Chen 			if ((extdat.mngflg & MS_REG_MNG_ATFLG) == MS_REG_MNG_ATFLG_ATTBL) {
158833842cedSCho, Yu-Chen 				ms_lib_erase_phyblock(us, PhyBlock);
158933842cedSCho, Yu-Chen 				continue;
159033842cedSCho, Yu-Chen 			}
159133842cedSCho, Yu-Chen 
159233842cedSCho, Yu-Chen 			if (extdat.logadr != MS_LB_NOT_USED) {
159333842cedSCho, Yu-Chen 				if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) {
159433842cedSCho, Yu-Chen 					ms_lib_erase_phyblock(us, PhyBlock);
159533842cedSCho, Yu-Chen 					continue;
159633842cedSCho, Yu-Chen 				}
159733842cedSCho, Yu-Chen 
159833842cedSCho, Yu-Chen 				newblk = ms_libconv_to_physical(info, extdat.logadr);
159933842cedSCho, Yu-Chen 
160033842cedSCho, Yu-Chen 				if (newblk != MS_LB_NOT_USED) {
160133842cedSCho, Yu-Chen 					if (extdat.logadr == 0) {
160233842cedSCho, Yu-Chen 						ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
160333842cedSCho, Yu-Chen 						if (ms_lib_check_disableblock(us, btBlk1st)) {
160433842cedSCho, Yu-Chen 							ms_lib_set_logicalpair(us, extdat.logadr, newblk);
160533842cedSCho, Yu-Chen 							continue;
160633842cedSCho, Yu-Chen 						}
160733842cedSCho, Yu-Chen 					}
160833842cedSCho, Yu-Chen 
160933842cedSCho, Yu-Chen 					ms_lib_read_extra(us, newblk, 0, &extdat);
161033842cedSCho, Yu-Chen 					if ((extdat.ovrflg & MS_REG_OVR_UDST) == MS_REG_OVR_UDST_UPDATING) {
161133842cedSCho, Yu-Chen 						ms_lib_erase_phyblock(us, PhyBlock);
161233842cedSCho, Yu-Chen 						continue;
161333842cedSCho, Yu-Chen 					} else {
161433842cedSCho, Yu-Chen 						ms_lib_erase_phyblock(us, newblk);
161533842cedSCho, Yu-Chen 					}
161633842cedSCho, Yu-Chen 				}
161733842cedSCho, Yu-Chen 
161833842cedSCho, Yu-Chen 				ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
161933842cedSCho, Yu-Chen 			}
162033842cedSCho, Yu-Chen 		}
162133842cedSCho, Yu-Chen 	} /* End for ... */
162233842cedSCho, Yu-Chen 
162333842cedSCho, Yu-Chen 	return MS_STATUS_SUCCESS;
162433842cedSCho, Yu-Chen }
162533842cedSCho, Yu-Chen 
162633842cedSCho, Yu-Chen 
162733842cedSCho, Yu-Chen static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
162833842cedSCho, Yu-Chen {
162933842cedSCho, Yu-Chen 	int result;
163033842cedSCho, Yu-Chen 	unsigned char *cdb = srb->cmnd;
163133842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
163233842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
163333842cedSCho, Yu-Chen 
163433842cedSCho, Yu-Chen 	u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
163533842cedSCho, Yu-Chen 		((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
163633842cedSCho, Yu-Chen 	u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
163733842cedSCho, Yu-Chen 	u32 blenByte = blen * 0x200;
163833842cedSCho, Yu-Chen 
163933842cedSCho, Yu-Chen 	if (bn > info->bl_num)
164033842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
164133842cedSCho, Yu-Chen 
164233842cedSCho, Yu-Chen 	if (info->MS_Status.IsMSPro) {
164333842cedSCho, Yu-Chen 		result = ene_load_bincode(us, MSP_RW_PATTERN);
164433842cedSCho, Yu-Chen 		if (result != USB_STOR_XFER_GOOD) {
164533842cedSCho, Yu-Chen 			US_DEBUGP("Load MPS RW pattern Fail !!\n");
164633842cedSCho, Yu-Chen 			return USB_STOR_TRANSPORT_ERROR;
164733842cedSCho, Yu-Chen 		}
164833842cedSCho, Yu-Chen 
164933842cedSCho, Yu-Chen 		/* set up the command wrapper */
165033842cedSCho, Yu-Chen 		memset(bcb, 0, sizeof(struct bulk_cb_wrap));
165133842cedSCho, Yu-Chen 		bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
165233842cedSCho, Yu-Chen 		bcb->DataTransferLength = blenByte;
165333842cedSCho, Yu-Chen 		bcb->Flags  = 0x80;
165433842cedSCho, Yu-Chen 		bcb->CDB[0] = 0xF1;
165533842cedSCho, Yu-Chen 		bcb->CDB[1] = 0x02;
165633842cedSCho, Yu-Chen 		bcb->CDB[5] = (unsigned char)(bn);
165733842cedSCho, Yu-Chen 		bcb->CDB[4] = (unsigned char)(bn>>8);
165833842cedSCho, Yu-Chen 		bcb->CDB[3] = (unsigned char)(bn>>16);
165933842cedSCho, Yu-Chen 		bcb->CDB[2] = (unsigned char)(bn>>24);
166033842cedSCho, Yu-Chen 
166133842cedSCho, Yu-Chen 		result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
166233842cedSCho, Yu-Chen 	} else {
166333842cedSCho, Yu-Chen 		void *buf;
166433842cedSCho, Yu-Chen 		int offset = 0;
166533842cedSCho, Yu-Chen 		u16 phyblk, logblk;
166633842cedSCho, Yu-Chen 		u8 PageNum;
166733842cedSCho, Yu-Chen 		u16 len;
166833842cedSCho, Yu-Chen 		u32 blkno;
166933842cedSCho, Yu-Chen 
167033842cedSCho, Yu-Chen 		buf = kmalloc(blenByte, GFP_KERNEL);
167133842cedSCho, Yu-Chen 		if (buf == NULL)
167233842cedSCho, Yu-Chen 			return USB_STOR_TRANSPORT_ERROR;
167333842cedSCho, Yu-Chen 
167433842cedSCho, Yu-Chen 		result = ene_load_bincode(us, MS_RW_PATTERN);
167533842cedSCho, Yu-Chen 		if (result != USB_STOR_XFER_GOOD) {
167633842cedSCho, Yu-Chen 			pr_info("Load MS RW pattern Fail !!\n");
167733842cedSCho, Yu-Chen 			result = USB_STOR_TRANSPORT_ERROR;
167833842cedSCho, Yu-Chen 			goto exit;
167933842cedSCho, Yu-Chen 		}
168033842cedSCho, Yu-Chen 
168133842cedSCho, Yu-Chen 		logblk  = (u16)(bn / info->MS_Lib.PagesPerBlock);
168233842cedSCho, Yu-Chen 		PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
168333842cedSCho, Yu-Chen 
168433842cedSCho, Yu-Chen 		while (1) {
168533842cedSCho, Yu-Chen 			if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
168633842cedSCho, Yu-Chen 				len = info->MS_Lib.PagesPerBlock-PageNum;
168733842cedSCho, Yu-Chen 			else
168833842cedSCho, Yu-Chen 				len = blen;
168933842cedSCho, Yu-Chen 
169033842cedSCho, Yu-Chen 			phyblk = ms_libconv_to_physical(info, logblk);
169133842cedSCho, Yu-Chen 			blkno  = phyblk * 0x20 + PageNum;
169233842cedSCho, Yu-Chen 
169333842cedSCho, Yu-Chen 			/* set up the command wrapper */
169433842cedSCho, Yu-Chen 			memset(bcb, 0, sizeof(struct bulk_cb_wrap));
169533842cedSCho, Yu-Chen 			bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
169633842cedSCho, Yu-Chen 			bcb->DataTransferLength = 0x200 * len;
169733842cedSCho, Yu-Chen 			bcb->Flags  = 0x80;
169833842cedSCho, Yu-Chen 			bcb->CDB[0] = 0xF1;
169933842cedSCho, Yu-Chen 			bcb->CDB[1] = 0x02;
170033842cedSCho, Yu-Chen 			bcb->CDB[5] = (unsigned char)(blkno);
170133842cedSCho, Yu-Chen 			bcb->CDB[4] = (unsigned char)(blkno>>8);
170233842cedSCho, Yu-Chen 			bcb->CDB[3] = (unsigned char)(blkno>>16);
170333842cedSCho, Yu-Chen 			bcb->CDB[2] = (unsigned char)(blkno>>24);
170433842cedSCho, Yu-Chen 
170533842cedSCho, Yu-Chen 			result = ene_send_scsi_cmd(us, FDIR_READ, buf+offset, 0);
170633842cedSCho, Yu-Chen 			if (result != USB_STOR_XFER_GOOD) {
170733842cedSCho, Yu-Chen 				pr_info("MS_SCSI_Read --- result = %x\n", result);
170833842cedSCho, Yu-Chen 				result = USB_STOR_TRANSPORT_ERROR;
170933842cedSCho, Yu-Chen 				goto exit;
171033842cedSCho, Yu-Chen 			}
171133842cedSCho, Yu-Chen 
171233842cedSCho, Yu-Chen 			blen -= len;
171333842cedSCho, Yu-Chen 			if (blen <= 0)
171433842cedSCho, Yu-Chen 				break;
171533842cedSCho, Yu-Chen 			logblk++;
171633842cedSCho, Yu-Chen 			PageNum = 0;
171733842cedSCho, Yu-Chen 			offset += MS_BYTES_PER_PAGE*len;
171833842cedSCho, Yu-Chen 		}
171933842cedSCho, Yu-Chen 		usb_stor_set_xfer_buf(buf, blenByte, srb);
172033842cedSCho, Yu-Chen exit:
172133842cedSCho, Yu-Chen 		kfree(buf);
172233842cedSCho, Yu-Chen 	}
172333842cedSCho, Yu-Chen 	return result;
172433842cedSCho, Yu-Chen }
172533842cedSCho, Yu-Chen 
172633842cedSCho, Yu-Chen static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
172733842cedSCho, Yu-Chen {
172833842cedSCho, Yu-Chen 	int result;
172933842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
173033842cedSCho, Yu-Chen 	unsigned char *cdb = srb->cmnd;
173133842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
173233842cedSCho, Yu-Chen 
173333842cedSCho, Yu-Chen 	u32 bn = ((cdb[2] << 24) & 0xff000000) |
173433842cedSCho, Yu-Chen 			((cdb[3] << 16) & 0x00ff0000) |
173533842cedSCho, Yu-Chen 			((cdb[4] << 8) & 0x0000ff00) |
173633842cedSCho, Yu-Chen 			((cdb[5] << 0) & 0x000000ff);
173733842cedSCho, Yu-Chen 	u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
173833842cedSCho, Yu-Chen 	u32 blenByte = blen * 0x200;
173933842cedSCho, Yu-Chen 
174033842cedSCho, Yu-Chen 	if (bn > info->bl_num)
174133842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
174233842cedSCho, Yu-Chen 
174333842cedSCho, Yu-Chen 	if (info->MS_Status.IsMSPro) {
174433842cedSCho, Yu-Chen 		result = ene_load_bincode(us, MSP_RW_PATTERN);
174533842cedSCho, Yu-Chen 		if (result != USB_STOR_XFER_GOOD) {
174633842cedSCho, Yu-Chen 			pr_info("Load MSP RW pattern Fail !!\n");
174733842cedSCho, Yu-Chen 			return USB_STOR_TRANSPORT_ERROR;
174833842cedSCho, Yu-Chen 		}
174933842cedSCho, Yu-Chen 
175033842cedSCho, Yu-Chen 		/* set up the command wrapper */
175133842cedSCho, Yu-Chen 		memset(bcb, 0, sizeof(struct bulk_cb_wrap));
175233842cedSCho, Yu-Chen 		bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
175333842cedSCho, Yu-Chen 		bcb->DataTransferLength = blenByte;
175433842cedSCho, Yu-Chen 		bcb->Flags  = 0x00;
175533842cedSCho, Yu-Chen 		bcb->CDB[0] = 0xF0;
175633842cedSCho, Yu-Chen 		bcb->CDB[1] = 0x04;
175733842cedSCho, Yu-Chen 		bcb->CDB[5] = (unsigned char)(bn);
175833842cedSCho, Yu-Chen 		bcb->CDB[4] = (unsigned char)(bn>>8);
175933842cedSCho, Yu-Chen 		bcb->CDB[3] = (unsigned char)(bn>>16);
176033842cedSCho, Yu-Chen 		bcb->CDB[2] = (unsigned char)(bn>>24);
176133842cedSCho, Yu-Chen 
176233842cedSCho, Yu-Chen 		result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
176333842cedSCho, Yu-Chen 	} else {
176433842cedSCho, Yu-Chen 		void *buf;
176533842cedSCho, Yu-Chen 		int offset;
176633842cedSCho, Yu-Chen 		u16 PhyBlockAddr;
176733842cedSCho, Yu-Chen 		u8 PageNum;
176833842cedSCho, Yu-Chen 		u32 result;
176933842cedSCho, Yu-Chen 		u16 len, oldphy, newphy;
177033842cedSCho, Yu-Chen 
177133842cedSCho, Yu-Chen 		buf = kmalloc(blenByte, GFP_KERNEL);
177233842cedSCho, Yu-Chen 		if (buf == NULL)
177333842cedSCho, Yu-Chen 			return USB_STOR_TRANSPORT_ERROR;
177433842cedSCho, Yu-Chen 		usb_stor_set_xfer_buf(buf, blenByte, srb);
177533842cedSCho, Yu-Chen 
177633842cedSCho, Yu-Chen 		result = ene_load_bincode(us, MS_RW_PATTERN);
177733842cedSCho, Yu-Chen 		if (result != USB_STOR_XFER_GOOD) {
177833842cedSCho, Yu-Chen 			pr_info("Load MS RW pattern Fail !!\n");
177933842cedSCho, Yu-Chen 			result = USB_STOR_TRANSPORT_ERROR;
178033842cedSCho, Yu-Chen 			goto exit;
178133842cedSCho, Yu-Chen 		}
178233842cedSCho, Yu-Chen 
178333842cedSCho, Yu-Chen 		PhyBlockAddr = (u16)(bn / info->MS_Lib.PagesPerBlock);
178433842cedSCho, Yu-Chen 		PageNum      = (u8)(bn % info->MS_Lib.PagesPerBlock);
178533842cedSCho, Yu-Chen 
178633842cedSCho, Yu-Chen 		while (1) {
178733842cedSCho, Yu-Chen 			if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
178833842cedSCho, Yu-Chen 				len = info->MS_Lib.PagesPerBlock-PageNum;
178933842cedSCho, Yu-Chen 			else
179033842cedSCho, Yu-Chen 				len = blen;
179133842cedSCho, Yu-Chen 
179233842cedSCho, Yu-Chen 			oldphy = ms_libconv_to_physical(info, PhyBlockAddr); /* need check us <-> info */
179333842cedSCho, Yu-Chen 			newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr);
179433842cedSCho, Yu-Chen 
179533842cedSCho, Yu-Chen 			result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len);
179633842cedSCho, Yu-Chen 
179733842cedSCho, Yu-Chen 			if (result != USB_STOR_XFER_GOOD) {
179833842cedSCho, Yu-Chen 				pr_info("MS_SCSI_Write --- result = %x\n", result);
179933842cedSCho, Yu-Chen 				result =  USB_STOR_TRANSPORT_ERROR;
180033842cedSCho, Yu-Chen 				goto exit;
180133842cedSCho, Yu-Chen 			}
180233842cedSCho, Yu-Chen 
180333842cedSCho, Yu-Chen 			info->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED;
180433842cedSCho, Yu-Chen 			ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy);
180533842cedSCho, Yu-Chen 
180633842cedSCho, Yu-Chen 			blen -= len;
180733842cedSCho, Yu-Chen 			if (blen <= 0)
180833842cedSCho, Yu-Chen 				break;
180933842cedSCho, Yu-Chen 			PhyBlockAddr++;
181033842cedSCho, Yu-Chen 			PageNum = 0;
181133842cedSCho, Yu-Chen 			offset += MS_BYTES_PER_PAGE*len;
181233842cedSCho, Yu-Chen 		}
181333842cedSCho, Yu-Chen exit:
181433842cedSCho, Yu-Chen 		kfree(buf);
181533842cedSCho, Yu-Chen 	}
181633842cedSCho, Yu-Chen 	return result;
181733842cedSCho, Yu-Chen }
181833842cedSCho, Yu-Chen 
181933842cedSCho, Yu-Chen /*
182033842cedSCho, Yu-Chen  * ENE MS Card
182133842cedSCho, Yu-Chen  */
182233842cedSCho, Yu-Chen 
182341e568d1Shuajun li static int ene_get_card_type(struct us_data *us, u16 index, void *buf)
182441e568d1Shuajun li {
182541e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
182641e568d1Shuajun li 	int result;
182741e568d1Shuajun li 
182841e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
182941e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
183041e568d1Shuajun li 	bcb->DataTransferLength	= 0x01;
183141e568d1Shuajun li 	bcb->Flags			= 0x80;
183241e568d1Shuajun li 	bcb->CDB[0]			= 0xED;
183341e568d1Shuajun li 	bcb->CDB[2]			= (unsigned char)(index>>8);
183441e568d1Shuajun li 	bcb->CDB[3]			= (unsigned char)index;
183541e568d1Shuajun li 
183641e568d1Shuajun li 	result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
183741e568d1Shuajun li 	return result;
183841e568d1Shuajun li }
183941e568d1Shuajun li 
184041e568d1Shuajun li static int ene_get_card_status(struct us_data *us, u8 *buf)
184141e568d1Shuajun li {
184241e568d1Shuajun li 	u16 tmpreg;
184341e568d1Shuajun li 	u32 reg4b;
184441e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
184541e568d1Shuajun li 
184641e568d1Shuajun li 	/*US_DEBUGP("transport --- ENE_ReadSDReg\n");*/
184741e568d1Shuajun li 	reg4b = *(u32 *)&buf[0x18];
184841e568d1Shuajun li 	info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f);
184941e568d1Shuajun li 
185041e568d1Shuajun li 	tmpreg = (u16) reg4b;
185141e568d1Shuajun li 	reg4b = *(u32 *)(&buf[0x14]);
185241e568d1Shuajun li 	if (info->SD_Status.HiCapacity && !info->SD_Status.IsMMC)
185341e568d1Shuajun li 		info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
185441e568d1Shuajun li 
185541e568d1Shuajun li 	info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
185641e568d1Shuajun li 	info->SD_C_SIZE_MULT = (u8)(reg4b >> 7)  & 0x07;
185741e568d1Shuajun li 	if (info->SD_Status.HiCapacity && info->SD_Status.IsMMC)
185841e568d1Shuajun li 		info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
185941e568d1Shuajun li 
186041e568d1Shuajun li 	if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
186141e568d1Shuajun li 		info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN);
186241e568d1Shuajun li 		info->SD_READ_BL_LEN = SD_BLOCK_LEN;
186341e568d1Shuajun li 	} else {
186441e568d1Shuajun li 		info->SD_Block_Mult = 1;
186541e568d1Shuajun li 	}
186641e568d1Shuajun li 
186741e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
186841e568d1Shuajun li }
186941e568d1Shuajun li 
187041e568d1Shuajun li static int ene_load_bincode(struct us_data *us, unsigned char flag)
187141e568d1Shuajun li {
187241e568d1Shuajun li 	int err;
187341e568d1Shuajun li 	char *fw_name = NULL;
187441e568d1Shuajun li 	unsigned char *buf = NULL;
187541e568d1Shuajun li 	const struct firmware *sd_fw = NULL;
187641e568d1Shuajun li 	int result = USB_STOR_TRANSPORT_ERROR;
187741e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
187841e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
187941e568d1Shuajun li 
188041e568d1Shuajun li 	if (info->BIN_FLAG == flag)
188141e568d1Shuajun li 		return USB_STOR_TRANSPORT_GOOD;
188241e568d1Shuajun li 
188341e568d1Shuajun li 	switch (flag) {
188441e568d1Shuajun li 	/* For SD */
188541e568d1Shuajun li 	case SD_INIT1_PATTERN:
188641e568d1Shuajun li 		US_DEBUGP("SD_INIT1_PATTERN\n");
188741e568d1Shuajun li 		fw_name = "ene-ub6250/sd_init1.bin";
188841e568d1Shuajun li 		break;
188941e568d1Shuajun li 	case SD_INIT2_PATTERN:
189041e568d1Shuajun li 		US_DEBUGP("SD_INIT2_PATTERN\n");
189141e568d1Shuajun li 		fw_name = "ene-ub6250/sd_init2.bin";
189241e568d1Shuajun li 		break;
189341e568d1Shuajun li 	case SD_RW_PATTERN:
189441e568d1Shuajun li 		US_DEBUGP("SD_RDWR_PATTERN\n");
189541e568d1Shuajun li 		fw_name = "ene-ub6250/sd_rdwr.bin";
189641e568d1Shuajun li 		break;
189733842cedSCho, Yu-Chen 	/* For MS */
189833842cedSCho, Yu-Chen 	case MS_INIT_PATTERN:
189933842cedSCho, Yu-Chen 		US_DEBUGP("MS_INIT_PATTERN\n");
190033842cedSCho, Yu-Chen 		fw_name = "ene-ub6250/ms_init.bin";
190133842cedSCho, Yu-Chen 		break;
190233842cedSCho, Yu-Chen 	case MSP_RW_PATTERN:
190333842cedSCho, Yu-Chen 		US_DEBUGP("MSP_RW_PATTERN\n");
190433842cedSCho, Yu-Chen 		fw_name = "ene-ub6250/msp_rdwr.bin";
190533842cedSCho, Yu-Chen 		break;
190633842cedSCho, Yu-Chen 	case MS_RW_PATTERN:
190733842cedSCho, Yu-Chen 		US_DEBUGP("MS_RW_PATTERN\n");
190833842cedSCho, Yu-Chen 		fw_name = "ene-ub6250/ms_rdwr.bin";
190933842cedSCho, Yu-Chen 		break;
191041e568d1Shuajun li 	default:
191141e568d1Shuajun li 		US_DEBUGP("----------- Unknown PATTERN ----------\n");
191241e568d1Shuajun li 		goto nofw;
191341e568d1Shuajun li 	}
191441e568d1Shuajun li 
191541e568d1Shuajun li 	err = request_firmware(&sd_fw, fw_name, &us->pusb_dev->dev);
191641e568d1Shuajun li 	if (err) {
191741e568d1Shuajun li 		US_DEBUGP("load firmware %s failed\n", fw_name);
191841e568d1Shuajun li 		goto nofw;
191941e568d1Shuajun li 	}
192041e568d1Shuajun li 	buf = kmalloc(sd_fw->size, GFP_KERNEL);
192141e568d1Shuajun li 	if (buf == NULL) {
192241e568d1Shuajun li 		US_DEBUGP("Malloc memory for fireware failed!\n");
192341e568d1Shuajun li 		goto nofw;
192441e568d1Shuajun li 	}
192541e568d1Shuajun li 	memcpy(buf, sd_fw->data, sd_fw->size);
192641e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
192741e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
192841e568d1Shuajun li 	bcb->DataTransferLength = sd_fw->size;
192941e568d1Shuajun li 	bcb->Flags = 0x00;
193041e568d1Shuajun li 	bcb->CDB[0] = 0xEF;
193141e568d1Shuajun li 
193241e568d1Shuajun li 	result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
193341e568d1Shuajun li 	info->BIN_FLAG = flag;
193441e568d1Shuajun li 	kfree(buf);
193541e568d1Shuajun li 
193641e568d1Shuajun li nofw:
193741e568d1Shuajun li 	if (sd_fw != NULL) {
193841e568d1Shuajun li 		release_firmware(sd_fw);
193941e568d1Shuajun li 		sd_fw = NULL;
194041e568d1Shuajun li 	}
194141e568d1Shuajun li 
194241e568d1Shuajun li 	return result;
194341e568d1Shuajun li }
194441e568d1Shuajun li 
194533842cedSCho, Yu-Chen static int ms_card_init(struct us_data *us)
194633842cedSCho, Yu-Chen {
194733842cedSCho, Yu-Chen 	u32 result;
194833842cedSCho, Yu-Chen 	u16 TmpBlock;
194933842cedSCho, Yu-Chen 	unsigned char *PageBuffer0 = NULL, *PageBuffer1 = NULL;
195033842cedSCho, Yu-Chen 	struct ms_lib_type_extdat extdat;
195133842cedSCho, Yu-Chen 	u16 btBlk1st, btBlk2nd;
195233842cedSCho, Yu-Chen 	u32 btBlk1stErred;
195333842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
195433842cedSCho, Yu-Chen 
195533842cedSCho, Yu-Chen 	printk(KERN_INFO "MS_CardInit start\n");
195633842cedSCho, Yu-Chen 
195733842cedSCho, Yu-Chen 	ms_lib_free_allocatedarea(us); /* Clean buffer and set struct us_data flag to 0 */
195833842cedSCho, Yu-Chen 
195933842cedSCho, Yu-Chen 	/* get two PageBuffer */
196033842cedSCho, Yu-Chen 	PageBuffer0 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
196133842cedSCho, Yu-Chen 	PageBuffer1 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
196233842cedSCho, Yu-Chen 	if ((PageBuffer0 == NULL) || (PageBuffer1 == NULL)) {
196333842cedSCho, Yu-Chen 		result = MS_NO_MEMORY_ERROR;
196433842cedSCho, Yu-Chen 		goto exit;
196533842cedSCho, Yu-Chen 	}
196633842cedSCho, Yu-Chen 
196733842cedSCho, Yu-Chen 	btBlk1st = btBlk2nd = MS_LB_NOT_USED;
196833842cedSCho, Yu-Chen 	btBlk1stErred = 0;
196933842cedSCho, Yu-Chen 
197033842cedSCho, Yu-Chen 	for (TmpBlock = 0; TmpBlock < MS_MAX_INITIAL_ERROR_BLOCKS+2; TmpBlock++) {
197133842cedSCho, Yu-Chen 
197233842cedSCho, Yu-Chen 		switch (ms_read_readpage(us, TmpBlock, 0, (u32 *)PageBuffer0, &extdat)) {
197333842cedSCho, Yu-Chen 		case MS_STATUS_SUCCESS:
197433842cedSCho, Yu-Chen 			break;
197533842cedSCho, Yu-Chen 		case MS_STATUS_INT_ERROR:
197633842cedSCho, Yu-Chen 			break;
197733842cedSCho, Yu-Chen 		case MS_STATUS_ERROR:
197833842cedSCho, Yu-Chen 		default:
197933842cedSCho, Yu-Chen 			continue;
198033842cedSCho, Yu-Chen 		}
198133842cedSCho, Yu-Chen 
198233842cedSCho, Yu-Chen 		if ((extdat.ovrflg & MS_REG_OVR_BKST) == MS_REG_OVR_BKST_NG)
198333842cedSCho, Yu-Chen 			continue;
198433842cedSCho, Yu-Chen 
198533842cedSCho, Yu-Chen 		if (((extdat.mngflg & MS_REG_MNG_SYSFLG) == MS_REG_MNG_SYSFLG_USER) ||
198633842cedSCho, Yu-Chen 			(be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wBlockID) != MS_BOOT_BLOCK_ID) ||
198733842cedSCho, Yu-Chen 			(be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wFormatVersion) != MS_BOOT_BLOCK_FORMAT_VERSION) ||
198833842cedSCho, Yu-Chen 			(((struct ms_bootblock_page0 *)PageBuffer0)->header.bNumberOfDataEntry != MS_BOOT_BLOCK_DATA_ENTRIES))
198933842cedSCho, Yu-Chen 				continue;
199033842cedSCho, Yu-Chen 
199133842cedSCho, Yu-Chen 		if (btBlk1st != MS_LB_NOT_USED) {
199233842cedSCho, Yu-Chen 			btBlk2nd = TmpBlock;
199333842cedSCho, Yu-Chen 			break;
199433842cedSCho, Yu-Chen 		}
199533842cedSCho, Yu-Chen 
199633842cedSCho, Yu-Chen 		btBlk1st = TmpBlock;
199733842cedSCho, Yu-Chen 		memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE);
199833842cedSCho, Yu-Chen 		if (extdat.status1 & (MS_REG_ST1_DTER | MS_REG_ST1_EXER | MS_REG_ST1_FGER))
199933842cedSCho, Yu-Chen 			btBlk1stErred = 1;
200033842cedSCho, Yu-Chen 	}
200133842cedSCho, Yu-Chen 
200233842cedSCho, Yu-Chen 	if (btBlk1st == MS_LB_NOT_USED) {
200333842cedSCho, Yu-Chen 		result = MS_STATUS_ERROR;
200433842cedSCho, Yu-Chen 		goto exit;
200533842cedSCho, Yu-Chen 	}
200633842cedSCho, Yu-Chen 
200733842cedSCho, Yu-Chen 	/* write protect */
200833842cedSCho, Yu-Chen 	if ((extdat.status0 & MS_REG_ST0_WP) == MS_REG_ST0_WP_ON)
200933842cedSCho, Yu-Chen 		ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
201033842cedSCho, Yu-Chen 
201133842cedSCho, Yu-Chen 	result = MS_STATUS_ERROR;
201233842cedSCho, Yu-Chen 	/* 1st Boot Block */
201333842cedSCho, Yu-Chen 	if (btBlk1stErred == 0)
201433842cedSCho, Yu-Chen 		result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1);
201533842cedSCho, Yu-Chen 		/* 1st */
201633842cedSCho, Yu-Chen 	/* 2nd Boot Block */
201733842cedSCho, Yu-Chen 	if (result && (btBlk2nd != MS_LB_NOT_USED))
201833842cedSCho, Yu-Chen 		result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0);
201933842cedSCho, Yu-Chen 
202033842cedSCho, Yu-Chen 	if (result) {
202133842cedSCho, Yu-Chen 		result = MS_STATUS_ERROR;
202233842cedSCho, Yu-Chen 		goto exit;
202333842cedSCho, Yu-Chen 	}
202433842cedSCho, Yu-Chen 
202533842cedSCho, Yu-Chen 	for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++)
202633842cedSCho, Yu-Chen 		info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
202733842cedSCho, Yu-Chen 
202833842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap[btBlk1st] = MS_LB_BOOT_BLOCK;
202933842cedSCho, Yu-Chen 
203033842cedSCho, Yu-Chen 	if (btBlk2nd != MS_LB_NOT_USED) {
203133842cedSCho, Yu-Chen 		for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++)
203233842cedSCho, Yu-Chen 			info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
203333842cedSCho, Yu-Chen 
203433842cedSCho, Yu-Chen 		info->MS_Lib.Phy2LogMap[btBlk2nd] = MS_LB_BOOT_BLOCK;
203533842cedSCho, Yu-Chen 	}
203633842cedSCho, Yu-Chen 
203733842cedSCho, Yu-Chen 	result = ms_lib_scan_logicalblocknumber(us, btBlk1st);
203833842cedSCho, Yu-Chen 	if (result)
203933842cedSCho, Yu-Chen 		goto exit;
204033842cedSCho, Yu-Chen 
204133842cedSCho, Yu-Chen 	for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT;
204233842cedSCho, Yu-Chen 		TmpBlock < info->MS_Lib.NumberOfPhyBlock;
204333842cedSCho, Yu-Chen 		TmpBlock += MS_PHYSICAL_BLOCKS_PER_SEGMENT) {
204433842cedSCho, Yu-Chen 		if (ms_count_freeblock(us, TmpBlock) == 0) {
204533842cedSCho, Yu-Chen 			ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
204633842cedSCho, Yu-Chen 			break;
204733842cedSCho, Yu-Chen 		}
204833842cedSCho, Yu-Chen 	}
204933842cedSCho, Yu-Chen 
205033842cedSCho, Yu-Chen 	/* write */
205133842cedSCho, Yu-Chen 	if (ms_lib_alloc_writebuf(us)) {
205233842cedSCho, Yu-Chen 		result = MS_NO_MEMORY_ERROR;
205333842cedSCho, Yu-Chen 		goto exit;
205433842cedSCho, Yu-Chen 	}
205533842cedSCho, Yu-Chen 
205633842cedSCho, Yu-Chen 	result = MS_STATUS_SUCCESS;
205733842cedSCho, Yu-Chen 
205833842cedSCho, Yu-Chen exit:
205933842cedSCho, Yu-Chen 	kfree(PageBuffer1);
206033842cedSCho, Yu-Chen 	kfree(PageBuffer0);
206133842cedSCho, Yu-Chen 
206233842cedSCho, Yu-Chen 	printk(KERN_INFO "MS_CardInit end\n");
206333842cedSCho, Yu-Chen 	return result;
206433842cedSCho, Yu-Chen }
206533842cedSCho, Yu-Chen 
206633842cedSCho, Yu-Chen static int ene_ms_init(struct us_data *us)
206733842cedSCho, Yu-Chen {
206833842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
206933842cedSCho, Yu-Chen 	int result;
207033842cedSCho, Yu-Chen 	u8 buf[0x200];
207133842cedSCho, Yu-Chen 	u16 MSP_BlockSize, MSP_UserAreaBlocks;
207233842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
207333842cedSCho, Yu-Chen 
207433842cedSCho, Yu-Chen 	printk(KERN_INFO "transport --- ENE_MSInit\n");
207533842cedSCho, Yu-Chen 
207633842cedSCho, Yu-Chen 	/* the same part to test ENE */
207733842cedSCho, Yu-Chen 
207833842cedSCho, Yu-Chen 	result = ene_load_bincode(us, MS_INIT_PATTERN);
207933842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD) {
208033842cedSCho, Yu-Chen 		printk(KERN_ERR "Load MS Init Code Fail !!\n");
208133842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
208233842cedSCho, Yu-Chen 	}
208333842cedSCho, Yu-Chen 
208433842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
208533842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
208633842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x200;
208733842cedSCho, Yu-Chen 	bcb->Flags      = 0x80;
208833842cedSCho, Yu-Chen 	bcb->CDB[0]     = 0xF1;
208933842cedSCho, Yu-Chen 	bcb->CDB[1]     = 0x01;
209033842cedSCho, Yu-Chen 
209133842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_READ, &buf, 0);
209233842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD) {
209333842cedSCho, Yu-Chen 		printk(KERN_ERR "Execution MS Init Code Fail !!\n");
209433842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
209533842cedSCho, Yu-Chen 	}
209633842cedSCho, Yu-Chen 	/* the same part to test ENE */
209733842cedSCho, Yu-Chen 	info->MS_Status = *(struct MS_STATUS *)&buf[0];
209833842cedSCho, Yu-Chen 
209933842cedSCho, Yu-Chen 	if (info->MS_Status.Insert && info->MS_Status.Ready) {
210033842cedSCho, Yu-Chen 		printk(KERN_INFO "Insert     = %x\n", info->MS_Status.Insert);
210133842cedSCho, Yu-Chen 		printk(KERN_INFO "Ready      = %x\n", info->MS_Status.Ready);
210233842cedSCho, Yu-Chen 		printk(KERN_INFO "IsMSPro    = %x\n", info->MS_Status.IsMSPro);
210333842cedSCho, Yu-Chen 		printk(KERN_INFO "IsMSPHG    = %x\n", info->MS_Status.IsMSPHG);
210433842cedSCho, Yu-Chen 		printk(KERN_INFO "WtP= %x\n", info->MS_Status.WtP);
210533842cedSCho, Yu-Chen 		if (info->MS_Status.IsMSPro) {
210633842cedSCho, Yu-Chen 			MSP_BlockSize      = (buf[6] << 8) | buf[7];
210733842cedSCho, Yu-Chen 			MSP_UserAreaBlocks = (buf[10] << 8) | buf[11];
210833842cedSCho, Yu-Chen 			info->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
210933842cedSCho, Yu-Chen 		} else {
211033842cedSCho, Yu-Chen 			ms_card_init(us); /* Card is MS (to ms.c)*/
211133842cedSCho, Yu-Chen 		}
211233842cedSCho, Yu-Chen 		US_DEBUGP("MS Init Code OK !!\n");
211333842cedSCho, Yu-Chen 	} else {
211433842cedSCho, Yu-Chen 		US_DEBUGP("MS Card Not Ready --- %x\n", buf[0]);
211533842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
211633842cedSCho, Yu-Chen 	}
211733842cedSCho, Yu-Chen 
211833842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
211933842cedSCho, Yu-Chen }
212033842cedSCho, Yu-Chen 
212141e568d1Shuajun li static int ene_sd_init(struct us_data *us)
212241e568d1Shuajun li {
212341e568d1Shuajun li 	int result;
212441e568d1Shuajun li 	u8  buf[0x200];
212541e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
212641e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
212741e568d1Shuajun li 
212841e568d1Shuajun li 	US_DEBUGP("transport --- ENE_SDInit\n");
212941e568d1Shuajun li 	/* SD Init Part-1 */
213041e568d1Shuajun li 	result = ene_load_bincode(us, SD_INIT1_PATTERN);
213141e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
213241e568d1Shuajun li 		US_DEBUGP("Load SD Init Code Part-1 Fail !!\n");
213341e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
213441e568d1Shuajun li 	}
213541e568d1Shuajun li 
213641e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
213741e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
213841e568d1Shuajun li 	bcb->Flags = 0x80;
213941e568d1Shuajun li 	bcb->CDB[0] = 0xF2;
214041e568d1Shuajun li 
214141e568d1Shuajun li 	result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
214241e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
214325985edcSLucas De Marchi 		US_DEBUGP("Execution SD Init Code Fail !!\n");
214441e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
214541e568d1Shuajun li 	}
214641e568d1Shuajun li 
214741e568d1Shuajun li 	/* SD Init Part-2 */
214841e568d1Shuajun li 	result = ene_load_bincode(us, SD_INIT2_PATTERN);
214941e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
215041e568d1Shuajun li 		US_DEBUGP("Load SD Init Code Part-2 Fail !!\n");
215141e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
215241e568d1Shuajun li 	}
215341e568d1Shuajun li 
215441e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
215541e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
215641e568d1Shuajun li 	bcb->DataTransferLength = 0x200;
215741e568d1Shuajun li 	bcb->Flags              = 0x80;
215841e568d1Shuajun li 	bcb->CDB[0]             = 0xF1;
215941e568d1Shuajun li 
216041e568d1Shuajun li 	result = ene_send_scsi_cmd(us, FDIR_READ, &buf, 0);
216141e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
216225985edcSLucas De Marchi 		US_DEBUGP("Execution SD Init Code Fail !!\n");
216341e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
216441e568d1Shuajun li 	}
216541e568d1Shuajun li 
216641e568d1Shuajun li 	info->SD_Status =  *(struct SD_STATUS *)&buf[0];
216741e568d1Shuajun li 	if (info->SD_Status.Insert && info->SD_Status.Ready) {
216841e568d1Shuajun li 		ene_get_card_status(us, (unsigned char *)&buf);
216941e568d1Shuajun li 		US_DEBUGP("Insert     = %x\n", info->SD_Status.Insert);
217041e568d1Shuajun li 		US_DEBUGP("Ready      = %x\n", info->SD_Status.Ready);
217141e568d1Shuajun li 		US_DEBUGP("IsMMC      = %x\n", info->SD_Status.IsMMC);
217241e568d1Shuajun li 		US_DEBUGP("HiCapacity = %x\n", info->SD_Status.HiCapacity);
217341e568d1Shuajun li 		US_DEBUGP("HiSpeed    = %x\n", info->SD_Status.HiSpeed);
217441e568d1Shuajun li 		US_DEBUGP("WtP        = %x\n", info->SD_Status.WtP);
217541e568d1Shuajun li 	} else {
217641e568d1Shuajun li 		US_DEBUGP("SD Card Not Ready --- %x\n", buf[0]);
217741e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
217841e568d1Shuajun li 	}
217941e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
218041e568d1Shuajun li }
218141e568d1Shuajun li 
218241e568d1Shuajun li 
218341e568d1Shuajun li static int ene_init(struct us_data *us)
218441e568d1Shuajun li {
218541e568d1Shuajun li 	int result;
218641e568d1Shuajun li 	u8  misc_reg03 = 0;
218741e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
218841e568d1Shuajun li 
218941e568d1Shuajun li 	result = ene_get_card_type(us, REG_CARD_STATUS, &misc_reg03);
219041e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD)
219141e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
219241e568d1Shuajun li 
219341e568d1Shuajun li 	if (misc_reg03 & 0x01) {
219441e568d1Shuajun li 		if (!info->SD_Status.Ready) {
219541e568d1Shuajun li 			result = ene_sd_init(us);
219641e568d1Shuajun li 			if (result != USB_STOR_XFER_GOOD)
219741e568d1Shuajun li 				return USB_STOR_TRANSPORT_ERROR;
219841e568d1Shuajun li 		}
219941e568d1Shuajun li 	}
220033842cedSCho, Yu-Chen 	if (misc_reg03 & 0x02) {
220133842cedSCho, Yu-Chen 		if (!info->MS_Status.Ready) {
220233842cedSCho, Yu-Chen 			result = ene_ms_init(us);
220333842cedSCho, Yu-Chen 			if (result != USB_STOR_XFER_GOOD)
220433842cedSCho, Yu-Chen 				return USB_STOR_TRANSPORT_ERROR;
220533842cedSCho, Yu-Chen 		}
220633842cedSCho, Yu-Chen 	}
220741e568d1Shuajun li 	return result;
220841e568d1Shuajun li }
220941e568d1Shuajun li 
221041e568d1Shuajun li /*----- sd_scsi_irp() ---------*/
221141e568d1Shuajun li static int sd_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
221241e568d1Shuajun li {
221341e568d1Shuajun li 	int    result;
221441e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
221541e568d1Shuajun li 
221641e568d1Shuajun li 	info->SrbStatus = SS_SUCCESS;
221741e568d1Shuajun li 	switch (srb->cmnd[0]) {
221841e568d1Shuajun li 	case TEST_UNIT_READY:
221941e568d1Shuajun li 		result = sd_scsi_test_unit_ready(us, srb);
222041e568d1Shuajun li 		break; /* 0x00 */
222141e568d1Shuajun li 	case INQUIRY:
222241e568d1Shuajun li 		result = sd_scsi_inquiry(us, srb);
222341e568d1Shuajun li 		break; /* 0x12 */
222441e568d1Shuajun li 	case MODE_SENSE:
222541e568d1Shuajun li 		result = sd_scsi_mode_sense(us, srb);
222641e568d1Shuajun li 		break; /* 0x1A */
222741e568d1Shuajun li 	/*
222841e568d1Shuajun li 	case START_STOP:
222941e568d1Shuajun li 		result = SD_SCSI_Start_Stop(us, srb);
223041e568d1Shuajun li 		break; //0x1B
223141e568d1Shuajun li 	*/
223241e568d1Shuajun li 	case READ_CAPACITY:
223341e568d1Shuajun li 		result = sd_scsi_read_capacity(us, srb);
223441e568d1Shuajun li 		break; /* 0x25 */
223541e568d1Shuajun li 	case READ_10:
223641e568d1Shuajun li 		result = sd_scsi_read(us, srb);
223741e568d1Shuajun li 		break; /* 0x28 */
223841e568d1Shuajun li 	case WRITE_10:
223941e568d1Shuajun li 		result = sd_scsi_write(us, srb);
224041e568d1Shuajun li 		break; /* 0x2A */
224141e568d1Shuajun li 	default:
224241e568d1Shuajun li 		info->SrbStatus = SS_ILLEGAL_REQUEST;
224341e568d1Shuajun li 		result = USB_STOR_TRANSPORT_FAILED;
224441e568d1Shuajun li 		break;
224541e568d1Shuajun li 	}
224641e568d1Shuajun li 	return result;
224741e568d1Shuajun li }
224841e568d1Shuajun li 
224933842cedSCho, Yu-Chen /*
225033842cedSCho, Yu-Chen  * ms_scsi_irp()
225133842cedSCho, Yu-Chen  */
225236f3a14dSFelipe Balbi static int ms_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
225333842cedSCho, Yu-Chen {
225433842cedSCho, Yu-Chen 	int result;
225533842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
225633842cedSCho, Yu-Chen 	info->SrbStatus = SS_SUCCESS;
225733842cedSCho, Yu-Chen 	switch (srb->cmnd[0]) {
225833842cedSCho, Yu-Chen 	case TEST_UNIT_READY:
225933842cedSCho, Yu-Chen 		result = ms_scsi_test_unit_ready(us, srb);
226033842cedSCho, Yu-Chen 		break; /* 0x00 */
226133842cedSCho, Yu-Chen 	case INQUIRY:
226233842cedSCho, Yu-Chen 		result = ms_scsi_inquiry(us, srb);
226333842cedSCho, Yu-Chen 		break; /* 0x12 */
226433842cedSCho, Yu-Chen 	case MODE_SENSE:
226533842cedSCho, Yu-Chen 		result = ms_scsi_mode_sense(us, srb);
226633842cedSCho, Yu-Chen 		break; /* 0x1A */
226733842cedSCho, Yu-Chen 	case READ_CAPACITY:
226833842cedSCho, Yu-Chen 		result = ms_scsi_read_capacity(us, srb);
226933842cedSCho, Yu-Chen 		break; /* 0x25 */
227033842cedSCho, Yu-Chen 	case READ_10:
227133842cedSCho, Yu-Chen 		result = ms_scsi_read(us, srb);
227233842cedSCho, Yu-Chen 		break; /* 0x28 */
227333842cedSCho, Yu-Chen 	case WRITE_10:
227433842cedSCho, Yu-Chen 		result = ms_scsi_write(us, srb);
227533842cedSCho, Yu-Chen 		break;  /* 0x2A */
227633842cedSCho, Yu-Chen 	default:
227733842cedSCho, Yu-Chen 		info->SrbStatus = SS_ILLEGAL_REQUEST;
227833842cedSCho, Yu-Chen 		result = USB_STOR_TRANSPORT_FAILED;
227933842cedSCho, Yu-Chen 		break;
228033842cedSCho, Yu-Chen 	}
228133842cedSCho, Yu-Chen 	return result;
228233842cedSCho, Yu-Chen }
228333842cedSCho, Yu-Chen 
228441e568d1Shuajun li static int ene_transport(struct scsi_cmnd *srb, struct us_data *us)
228541e568d1Shuajun li {
228641e568d1Shuajun li 	int result = 0;
228741e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
228841e568d1Shuajun li 
228941e568d1Shuajun li 	/*US_DEBUG(usb_stor_show_command(srb)); */
229041e568d1Shuajun li 	scsi_set_resid(srb, 0);
229133842cedSCho, Yu-Chen 	if (unlikely(!(info->SD_Status.Ready || info->MS_Status.Ready))) {
229241e568d1Shuajun li 		result = ene_init(us);
229333842cedSCho, Yu-Chen 	} else {
229433842cedSCho, Yu-Chen 		if (info->SD_Status.Ready)
229541e568d1Shuajun li 			result = sd_scsi_irp(us, srb);
229641e568d1Shuajun li 
229733842cedSCho, Yu-Chen 		if (info->MS_Status.Ready)
229833842cedSCho, Yu-Chen 			result = ms_scsi_irp(us, srb);
229933842cedSCho, Yu-Chen 	}
230041e568d1Shuajun li 	return 0;
230141e568d1Shuajun li }
230241e568d1Shuajun li 
230341e568d1Shuajun li 
230441e568d1Shuajun li static int ene_ub6250_probe(struct usb_interface *intf,
230541e568d1Shuajun li 			 const struct usb_device_id *id)
230641e568d1Shuajun li {
230741e568d1Shuajun li 	int result;
230841e568d1Shuajun li 	u8  misc_reg03 = 0;
230941e568d1Shuajun li 	struct us_data *us;
231041e568d1Shuajun li 
231141e568d1Shuajun li 	result = usb_stor_probe1(&us, intf, id,
231241e568d1Shuajun li 		   (id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list);
231341e568d1Shuajun li 	if (result)
231441e568d1Shuajun li 		return result;
231541e568d1Shuajun li 
231641e568d1Shuajun li 	/* FIXME: where should the code alloc extra buf ? */
231741e568d1Shuajun li 	if (!us->extra) {
231841e568d1Shuajun li 		us->extra = kzalloc(sizeof(struct ene_ub6250_info), GFP_KERNEL);
231941e568d1Shuajun li 		if (!us->extra)
232041e568d1Shuajun li 			return -ENOMEM;
232141e568d1Shuajun li 		us->extra_destructor = ene_ub6250_info_destructor;
232241e568d1Shuajun li 	}
232341e568d1Shuajun li 
232441e568d1Shuajun li 	us->transport_name = "ene_ub6250";
232541e568d1Shuajun li 	us->transport = ene_transport;
232641e568d1Shuajun li 	us->max_lun = 0;
232741e568d1Shuajun li 
232841e568d1Shuajun li 	result = usb_stor_probe2(us);
232941e568d1Shuajun li 	if (result)
233041e568d1Shuajun li 		return result;
233141e568d1Shuajun li 
233241e568d1Shuajun li 	/* probe card type */
233341e568d1Shuajun li 	result = ene_get_card_type(us, REG_CARD_STATUS, &misc_reg03);
233441e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
233541e568d1Shuajun li 		usb_stor_disconnect(intf);
233641e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
233741e568d1Shuajun li 	}
233841e568d1Shuajun li 
233941e568d1Shuajun li 	if (!(misc_reg03 & 0x01)) {
234033842cedSCho, Yu-Chen 		pr_info("ums_eneub6250: The driver only supports SD/MS card. "
234133842cedSCho, Yu-Chen 			"To use SM card, please build driver/staging/keucr\n");
234241e568d1Shuajun li 	}
234341e568d1Shuajun li 
234441e568d1Shuajun li 	return result;
234541e568d1Shuajun li }
234641e568d1Shuajun li 
234741e568d1Shuajun li 
234841e568d1Shuajun li #ifdef CONFIG_PM
234941e568d1Shuajun li 
235041e568d1Shuajun li static int ene_ub6250_resume(struct usb_interface *iface)
235141e568d1Shuajun li {
235241e568d1Shuajun li 	u8 tmp = 0;
235341e568d1Shuajun li 	struct us_data *us = usb_get_intfdata(iface);
235441e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
235541e568d1Shuajun li 
235641e568d1Shuajun li 	mutex_lock(&us->dev_mutex);
235741e568d1Shuajun li 
235841e568d1Shuajun li 	US_DEBUGP("%s\n", __func__);
235941e568d1Shuajun li 	if (us->suspend_resume_hook)
236041e568d1Shuajun li 		(us->suspend_resume_hook)(us, US_RESUME);
236141e568d1Shuajun li 
236241e568d1Shuajun li 	mutex_unlock(&us->dev_mutex);
236341e568d1Shuajun li 
236441e568d1Shuajun li 	info->Power_IsResum = true;
236541e568d1Shuajun li 	/*info->SD_Status.Ready = 0; */
236641e568d1Shuajun li 	info->SD_Status = *(struct SD_STATUS *)&tmp;
236741e568d1Shuajun li 	info->MS_Status = *(struct MS_STATUS *)&tmp;
236841e568d1Shuajun li 	info->SM_Status = *(struct SM_STATUS *)&tmp;
236941e568d1Shuajun li 
237041e568d1Shuajun li 	return 0;
237141e568d1Shuajun li }
237241e568d1Shuajun li 
237341e568d1Shuajun li static int ene_ub6250_reset_resume(struct usb_interface *iface)
237441e568d1Shuajun li {
237541e568d1Shuajun li 	u8 tmp = 0;
237641e568d1Shuajun li 	struct us_data *us = usb_get_intfdata(iface);
237741e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
237841e568d1Shuajun li 	US_DEBUGP("%s\n", __func__);
237941e568d1Shuajun li 	/* Report the reset to the SCSI core */
238041e568d1Shuajun li 	usb_stor_reset_resume(iface);
238141e568d1Shuajun li 
238241e568d1Shuajun li 	/* FIXME: Notify the subdrivers that they need to reinitialize
238341e568d1Shuajun li 	 * the device */
238441e568d1Shuajun li 	info->Power_IsResum = true;
238541e568d1Shuajun li 	/*info->SD_Status.Ready = 0; */
238641e568d1Shuajun li 	info->SD_Status = *(struct SD_STATUS *)&tmp;
238741e568d1Shuajun li 	info->MS_Status = *(struct MS_STATUS *)&tmp;
238841e568d1Shuajun li 	info->SM_Status = *(struct SM_STATUS *)&tmp;
238941e568d1Shuajun li 
239041e568d1Shuajun li 	return 0;
239141e568d1Shuajun li }
239241e568d1Shuajun li 
239341e568d1Shuajun li #else
239441e568d1Shuajun li 
239541e568d1Shuajun li #define ene_ub6250_resume		NULL
239641e568d1Shuajun li #define ene_ub6250_reset_resume		NULL
239741e568d1Shuajun li 
239841e568d1Shuajun li #endif
239941e568d1Shuajun li 
240041e568d1Shuajun li static struct usb_driver ene_ub6250_driver = {
240141e568d1Shuajun li 	.name =		"ums_eneub6250",
240241e568d1Shuajun li 	.probe =	ene_ub6250_probe,
240341e568d1Shuajun li 	.disconnect =	usb_stor_disconnect,
240441e568d1Shuajun li 	.suspend =	usb_stor_suspend,
240541e568d1Shuajun li 	.resume =	ene_ub6250_resume,
240641e568d1Shuajun li 	.reset_resume =	ene_ub6250_reset_resume,
240741e568d1Shuajun li 	.pre_reset =	usb_stor_pre_reset,
240841e568d1Shuajun li 	.post_reset =	usb_stor_post_reset,
240941e568d1Shuajun li 	.id_table =	ene_ub6250_usb_ids,
241041e568d1Shuajun li 	.soft_unbind =	1,
241141e568d1Shuajun li };
241241e568d1Shuajun li 
241341e568d1Shuajun li static int __init ene_ub6250_init(void)
241441e568d1Shuajun li {
241541e568d1Shuajun li 	return usb_register(&ene_ub6250_driver);
241641e568d1Shuajun li }
241741e568d1Shuajun li 
241841e568d1Shuajun li static void __exit ene_ub6250_exit(void)
241941e568d1Shuajun li {
242041e568d1Shuajun li 	usb_deregister(&ene_ub6250_driver);
242141e568d1Shuajun li }
242241e568d1Shuajun li 
242341e568d1Shuajun li module_init(ene_ub6250_init);
242441e568d1Shuajun li module_exit(ene_ub6250_exit);
2425