xref: /openbmc/linux/drivers/usb/storage/ene_ub6250.c (revision 5fd54ace)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
241e568d1Shuajun li /*
341e568d1Shuajun li  *
441e568d1Shuajun li  * This program is free software; you can redistribute it and/or modify it
541e568d1Shuajun li  * under the terms of the GNU General Public License as published by the
641e568d1Shuajun li  * Free Software Foundation; either version 2, or (at your option) any
741e568d1Shuajun li  * later version.
841e568d1Shuajun li  *
941e568d1Shuajun li  * This program is distributed in the hope that it will be useful, but
1041e568d1Shuajun li  * WITHOUT ANY WARRANTY; without even the implied warranty of
1141e568d1Shuajun li  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1241e568d1Shuajun li  * General Public License for more details.
1341e568d1Shuajun li  *
1441e568d1Shuajun li  * You should have received a copy of the GNU General Public License along
1541e568d1Shuajun li  * with this program; if not, write to the Free Software Foundation, Inc.,
1641e568d1Shuajun li  * 675 Mass Ave, Cambridge, MA 02139, USA.
1741e568d1Shuajun li  */
1841e568d1Shuajun li #include <linux/jiffies.h>
1941e568d1Shuajun li #include <linux/errno.h>
2041e568d1Shuajun li #include <linux/module.h>
2141e568d1Shuajun li #include <linux/slab.h>
2241e568d1Shuajun li 
2341e568d1Shuajun li #include <scsi/scsi.h>
2441e568d1Shuajun li #include <scsi/scsi_cmnd.h>
2541e568d1Shuajun li 
2641e568d1Shuajun li #include <linux/firmware.h>
2741e568d1Shuajun li 
2841e568d1Shuajun li #include "usb.h"
2941e568d1Shuajun li #include "transport.h"
3041e568d1Shuajun li #include "protocol.h"
3141e568d1Shuajun li #include "debug.h"
32aa519be3SAkinobu Mita #include "scsiglue.h"
3341e568d1Shuajun li 
34595c8970STim Gardner #define SD_INIT1_FIRMWARE "ene-ub6250/sd_init1.bin"
35595c8970STim Gardner #define SD_INIT2_FIRMWARE "ene-ub6250/sd_init2.bin"
36595c8970STim Gardner #define SD_RW_FIRMWARE "ene-ub6250/sd_rdwr.bin"
37595c8970STim Gardner #define MS_INIT_FIRMWARE "ene-ub6250/ms_init.bin"
38595c8970STim Gardner #define MSP_RW_FIRMWARE "ene-ub6250/msp_rdwr.bin"
39595c8970STim Gardner #define MS_RW_FIRMWARE "ene-ub6250/ms_rdwr.bin"
40595c8970STim Gardner 
41aa519be3SAkinobu Mita #define DRV_NAME "ums_eneub6250"
42aa519be3SAkinobu Mita 
4341e568d1Shuajun li MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
4441e568d1Shuajun li MODULE_LICENSE("GPL");
45595c8970STim Gardner MODULE_FIRMWARE(SD_INIT1_FIRMWARE);
46595c8970STim Gardner MODULE_FIRMWARE(SD_INIT2_FIRMWARE);
47595c8970STim Gardner MODULE_FIRMWARE(SD_RW_FIRMWARE);
48595c8970STim Gardner MODULE_FIRMWARE(MS_INIT_FIRMWARE);
49595c8970STim Gardner MODULE_FIRMWARE(MSP_RW_FIRMWARE);
50595c8970STim Gardner MODULE_FIRMWARE(MS_RW_FIRMWARE);
5141e568d1Shuajun li 
5241e568d1Shuajun li /*
5341e568d1Shuajun li  * The table of devices
5441e568d1Shuajun li  */
5541e568d1Shuajun li #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
5641e568d1Shuajun li 		    vendorName, productName, useProtocol, useTransport, \
5741e568d1Shuajun li 		    initFunction, flags) \
5841e568d1Shuajun li { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
59f61870eeSSebastian Andrzej Siewior 	.driver_info = (flags)}
6041e568d1Shuajun li 
6136f3a14dSFelipe Balbi static struct usb_device_id ene_ub6250_usb_ids[] = {
6241e568d1Shuajun li #	include "unusual_ene_ub6250.h"
6341e568d1Shuajun li 	{ }		/* Terminating entry */
6441e568d1Shuajun li };
6541e568d1Shuajun li MODULE_DEVICE_TABLE(usb, ene_ub6250_usb_ids);
6641e568d1Shuajun li 
6741e568d1Shuajun li #undef UNUSUAL_DEV
6841e568d1Shuajun li 
6941e568d1Shuajun li /*
7041e568d1Shuajun li  * The flags table
7141e568d1Shuajun li  */
7241e568d1Shuajun li #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
7341e568d1Shuajun li 		    vendor_name, product_name, use_protocol, use_transport, \
7441e568d1Shuajun li 		    init_function, Flags) \
7541e568d1Shuajun li { \
7641e568d1Shuajun li 	.vendorName = vendor_name,	\
7741e568d1Shuajun li 	.productName = product_name,	\
7841e568d1Shuajun li 	.useProtocol = use_protocol,	\
7941e568d1Shuajun li 	.useTransport = use_transport,	\
8041e568d1Shuajun li 	.initFunction = init_function,	\
8141e568d1Shuajun li }
8241e568d1Shuajun li 
8341e568d1Shuajun li static struct us_unusual_dev ene_ub6250_unusual_dev_list[] = {
8441e568d1Shuajun li #	include "unusual_ene_ub6250.h"
8541e568d1Shuajun li 	{ }		/* Terminating entry */
8641e568d1Shuajun li };
8741e568d1Shuajun li 
8841e568d1Shuajun li #undef UNUSUAL_DEV
8941e568d1Shuajun li 
9041e568d1Shuajun li 
9141e568d1Shuajun li 
9241e568d1Shuajun li /* ENE bin code len */
9341e568d1Shuajun li #define ENE_BIN_CODE_LEN    0x800
9441e568d1Shuajun li /* EnE HW Register */
9541e568d1Shuajun li #define REG_CARD_STATUS     0xFF83
9641e568d1Shuajun li #define REG_HW_TRAP1        0xFF89
9741e568d1Shuajun li 
9841e568d1Shuajun li /* SRB Status */
99ce553bd1SAlan Stern #define SS_SUCCESS		0x000000	/* No Sense */
100ce553bd1SAlan Stern #define SS_NOT_READY		0x023A00	/* Medium not present */
101ce553bd1SAlan Stern #define SS_MEDIUM_ERR		0x031100	/* Unrecovered read error */
102ce553bd1SAlan Stern #define SS_HW_ERR		0x040800	/* Communication failure */
103ce553bd1SAlan Stern #define SS_ILLEGAL_REQUEST	0x052000	/* Invalid command */
104ce553bd1SAlan Stern #define SS_UNIT_ATTENTION	0x062900	/* Reset occurred */
10541e568d1Shuajun li 
10641e568d1Shuajun li /* ENE Load FW Pattern */
10741e568d1Shuajun li #define SD_INIT1_PATTERN   1
10841e568d1Shuajun li #define SD_INIT2_PATTERN   2
10941e568d1Shuajun li #define SD_RW_PATTERN      3
11041e568d1Shuajun li #define MS_INIT_PATTERN    4
11141e568d1Shuajun li #define MSP_RW_PATTERN     5
11241e568d1Shuajun li #define MS_RW_PATTERN      6
11341e568d1Shuajun li #define SM_INIT_PATTERN    7
11441e568d1Shuajun li #define SM_RW_PATTERN      8
11541e568d1Shuajun li 
11641e568d1Shuajun li #define FDIR_WRITE         0
11741e568d1Shuajun li #define FDIR_READ          1
11841e568d1Shuajun li 
11933842cedSCho, Yu-Chen /* For MS Card */
12033842cedSCho, Yu-Chen 
12133842cedSCho, Yu-Chen /* Status Register 1 */
12233842cedSCho, Yu-Chen #define MS_REG_ST1_MB           0x80    /* media busy */
12333842cedSCho, Yu-Chen #define MS_REG_ST1_FB1          0x40    /* flush busy 1 */
12433842cedSCho, Yu-Chen #define MS_REG_ST1_DTER         0x20    /* error on data(corrected) */
12533842cedSCho, Yu-Chen #define MS_REG_ST1_UCDT         0x10    /* unable to correct data */
12633842cedSCho, Yu-Chen #define MS_REG_ST1_EXER         0x08    /* error on extra(corrected) */
12733842cedSCho, Yu-Chen #define MS_REG_ST1_UCEX         0x04    /* unable to correct extra */
12833842cedSCho, Yu-Chen #define MS_REG_ST1_FGER         0x02    /* error on overwrite flag(corrected) */
12933842cedSCho, Yu-Chen #define MS_REG_ST1_UCFG         0x01    /* unable to correct overwrite flag */
13033842cedSCho, 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)
13133842cedSCho, Yu-Chen 
13233842cedSCho, Yu-Chen /* Overwrite Area */
13333842cedSCho, Yu-Chen #define MS_REG_OVR_BKST		0x80            /* block status */
13433842cedSCho, Yu-Chen #define MS_REG_OVR_BKST_OK	MS_REG_OVR_BKST     /* OK */
13533842cedSCho, Yu-Chen #define MS_REG_OVR_BKST_NG	0x00            /* NG */
13633842cedSCho, Yu-Chen #define MS_REG_OVR_PGST0	0x40            /* page status */
13733842cedSCho, Yu-Chen #define MS_REG_OVR_PGST1	0x20
13833842cedSCho, Yu-Chen #define MS_REG_OVR_PGST_MASK	(MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1)
13933842cedSCho, Yu-Chen #define MS_REG_OVR_PGST_OK	(MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1) /* OK */
14033842cedSCho, Yu-Chen #define MS_REG_OVR_PGST_NG	MS_REG_OVR_PGST1                      /* NG */
14133842cedSCho, Yu-Chen #define MS_REG_OVR_PGST_DATA_ERROR	0x00        /* data error */
14233842cedSCho, Yu-Chen #define MS_REG_OVR_UDST			0x10        /* update status */
14333842cedSCho, Yu-Chen #define MS_REG_OVR_UDST_UPDATING	0x00        /* updating */
14433842cedSCho, Yu-Chen #define MS_REG_OVR_UDST_NO_UPDATE	MS_REG_OVR_UDST
14533842cedSCho, Yu-Chen #define MS_REG_OVR_RESERVED	0x08
14633842cedSCho, 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)
14733842cedSCho, Yu-Chen 
14833842cedSCho, Yu-Chen /* Management Flag */
14933842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS0	0x20    /* serial copy management system */
15033842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS1	0x10
15133842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS_MASK		(MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
15233842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS_COPY_OK		(MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
15333842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS_ONE_COPY	MS_REG_MNG_SCMS1
15433842cedSCho, Yu-Chen #define MS_REG_MNG_SCMS_NO_COPY	0x00
15533842cedSCho, Yu-Chen #define MS_REG_MNG_ATFLG	0x08    /* address transfer table flag */
15633842cedSCho, Yu-Chen #define MS_REG_MNG_ATFLG_OTHER	MS_REG_MNG_ATFLG    /* other */
15733842cedSCho, Yu-Chen #define MS_REG_MNG_ATFLG_ATTBL	0x00	/* address transfer table */
15833842cedSCho, Yu-Chen #define MS_REG_MNG_SYSFLG	0x04	/* system flag */
15933842cedSCho, Yu-Chen #define MS_REG_MNG_SYSFLG_USER	MS_REG_MNG_SYSFLG   /* user block */
16033842cedSCho, Yu-Chen #define MS_REG_MNG_SYSFLG_BOOT	0x00	/* system block */
16133842cedSCho, Yu-Chen #define MS_REG_MNG_RESERVED	0xc3
16233842cedSCho, 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)
16333842cedSCho, Yu-Chen 
16433842cedSCho, Yu-Chen 
16533842cedSCho, Yu-Chen #define MS_MAX_PAGES_PER_BLOCK		32
16633842cedSCho, Yu-Chen #define MS_MAX_INITIAL_ERROR_BLOCKS 	10
16733842cedSCho, Yu-Chen #define MS_LIB_BITS_PER_BYTE		8
16833842cedSCho, Yu-Chen 
16933842cedSCho, Yu-Chen #define MS_SYSINF_FORMAT_FAT		1
17033842cedSCho, Yu-Chen #define MS_SYSINF_USAGE_GENERAL		0
17133842cedSCho, Yu-Chen 
17233842cedSCho, Yu-Chen #define MS_SYSINF_MSCLASS_TYPE_1	1
17333842cedSCho, Yu-Chen #define MS_SYSINF_PAGE_SIZE		MS_BYTES_PER_PAGE /* fixed */
17433842cedSCho, Yu-Chen 
17533842cedSCho, Yu-Chen #define MS_SYSINF_CARDTYPE_RDONLY	1
17633842cedSCho, Yu-Chen #define MS_SYSINF_CARDTYPE_RDWR		2
17733842cedSCho, Yu-Chen #define MS_SYSINF_CARDTYPE_HYBRID	3
17833842cedSCho, Yu-Chen #define MS_SYSINF_SECURITY		0x01
17933842cedSCho, Yu-Chen #define MS_SYSINF_SECURITY_NO_SUPPORT	MS_SYSINF_SECURITY
18033842cedSCho, Yu-Chen #define MS_SYSINF_SECURITY_SUPPORT	0
18133842cedSCho, Yu-Chen 
18233842cedSCho, Yu-Chen #define MS_SYSINF_RESERVED1		1
18333842cedSCho, Yu-Chen #define MS_SYSINF_RESERVED2		1
18433842cedSCho, Yu-Chen 
18533842cedSCho, Yu-Chen #define MS_SYSENT_TYPE_INVALID_BLOCK	0x01
18633842cedSCho, Yu-Chen #define MS_SYSENT_TYPE_CIS_IDI		0x0a    /* CIS/IDI */
18733842cedSCho, Yu-Chen 
18833842cedSCho, Yu-Chen #define SIZE_OF_KIRO		1024
18933842cedSCho, Yu-Chen #define BYTE_MASK		0xff
19033842cedSCho, Yu-Chen 
19133842cedSCho, Yu-Chen /* ms error code */
19233842cedSCho, Yu-Chen #define MS_STATUS_WRITE_PROTECT	0x0106
19333842cedSCho, Yu-Chen #define MS_STATUS_SUCCESS	0x0000
19433842cedSCho, Yu-Chen #define MS_ERROR_FLASH_READ	0x8003
19533842cedSCho, Yu-Chen #define MS_ERROR_FLASH_ERASE	0x8005
19633842cedSCho, Yu-Chen #define MS_LB_ERROR		0xfff0
19733842cedSCho, Yu-Chen #define MS_LB_BOOT_BLOCK	0xfff1
19833842cedSCho, Yu-Chen #define MS_LB_INITIAL_ERROR	0xfff2
19933842cedSCho, Yu-Chen #define MS_STATUS_SUCCESS_WITH_ECC 0xfff3
20033842cedSCho, Yu-Chen #define MS_LB_ACQUIRED_ERROR	0xfff4
20133842cedSCho, Yu-Chen #define MS_LB_NOT_USED_ERASED	0xfff5
20233842cedSCho, Yu-Chen #define MS_NOCARD_ERROR		0xfff8
20333842cedSCho, Yu-Chen #define MS_NO_MEMORY_ERROR	0xfff9
20433842cedSCho, Yu-Chen #define MS_STATUS_INT_ERROR	0xfffa
20533842cedSCho, Yu-Chen #define MS_STATUS_ERROR		0xfffe
20633842cedSCho, Yu-Chen #define MS_LB_NOT_USED		0xffff
20733842cedSCho, Yu-Chen 
20833842cedSCho, Yu-Chen #define MS_REG_MNG_SYSFLG	0x04    /* system flag */
20933842cedSCho, Yu-Chen #define MS_REG_MNG_SYSFLG_USER	MS_REG_MNG_SYSFLG   /* user block */
21033842cedSCho, Yu-Chen 
21133842cedSCho, Yu-Chen #define MS_BOOT_BLOCK_ID                        0x0001
21233842cedSCho, Yu-Chen #define MS_BOOT_BLOCK_FORMAT_VERSION            0x0100
21333842cedSCho, Yu-Chen #define MS_BOOT_BLOCK_DATA_ENTRIES              2
21433842cedSCho, Yu-Chen 
21533842cedSCho, Yu-Chen #define MS_NUMBER_OF_SYSTEM_ENTRY       	4
21633842cedSCho, Yu-Chen #define MS_NUMBER_OF_BOOT_BLOCK			2
21733842cedSCho, Yu-Chen #define MS_BYTES_PER_PAGE			512
21833842cedSCho, Yu-Chen #define MS_LOGICAL_BLOCKS_PER_SEGMENT		496
21933842cedSCho, Yu-Chen #define MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT        494
22033842cedSCho, Yu-Chen 
22133842cedSCho, Yu-Chen #define MS_PHYSICAL_BLOCKS_PER_SEGMENT		0x200 /* 512 */
22233842cedSCho, Yu-Chen #define MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK     0x1ff
22333842cedSCho, Yu-Chen 
22433842cedSCho, Yu-Chen /* overwrite area */
22533842cedSCho, Yu-Chen #define MS_REG_OVR_BKST		0x80		/* block status */
22633842cedSCho, Yu-Chen #define MS_REG_OVR_BKST_OK	MS_REG_OVR_BKST	/* OK */
22733842cedSCho, Yu-Chen #define MS_REG_OVR_BKST_NG	0x00            /* NG */
22833842cedSCho, Yu-Chen 
22933842cedSCho, Yu-Chen /* Status Register 1 */
23033842cedSCho, Yu-Chen #define MS_REG_ST1_DTER		0x20	/* error on data(corrected) */
23133842cedSCho, Yu-Chen #define MS_REG_ST1_EXER		0x08	/* error on extra(corrected) */
23233842cedSCho, Yu-Chen #define MS_REG_ST1_FGER		0x02	/* error on overwrite flag(corrected) */
23333842cedSCho, Yu-Chen 
23433842cedSCho, Yu-Chen /* MemoryStick Register */
23533842cedSCho, Yu-Chen /* Status Register 0 */
23633842cedSCho, Yu-Chen #define MS_REG_ST0_WP		0x01	/* write protected */
23733842cedSCho, Yu-Chen #define MS_REG_ST0_WP_ON	MS_REG_ST0_WP
23833842cedSCho, Yu-Chen 
23933842cedSCho, Yu-Chen #define MS_LIB_CTRL_RDONLY      0
24033842cedSCho, Yu-Chen #define MS_LIB_CTRL_WRPROTECT   1
24133842cedSCho, Yu-Chen 
24233842cedSCho, Yu-Chen /*dphy->log table */
24333842cedSCho, Yu-Chen #define ms_libconv_to_logical(pdx, PhyBlock) (((PhyBlock) >= (pdx)->MS_Lib.NumberOfPhyBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Phy2LogMap[PhyBlock])
24433842cedSCho, Yu-Chen #define ms_libconv_to_physical(pdx, LogBlock) (((LogBlock) >= (pdx)->MS_Lib.NumberOfLogBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Log2PhyMap[LogBlock])
24533842cedSCho, Yu-Chen 
24633842cedSCho, Yu-Chen #define ms_lib_ctrl_set(pdx, Flag)	((pdx)->MS_Lib.flags |= (1 << (Flag)))
24733842cedSCho, Yu-Chen #define ms_lib_ctrl_reset(pdx, Flag)	((pdx)->MS_Lib.flags &= ~(1 << (Flag)))
24833842cedSCho, Yu-Chen #define ms_lib_ctrl_check(pdx, Flag)	((pdx)->MS_Lib.flags & (1 << (Flag)))
24933842cedSCho, Yu-Chen 
25033842cedSCho, 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))
25133842cedSCho, Yu-Chen #define ms_lib_clear_pagemap(pdx) memset((pdx)->MS_Lib.pagemap, 0, sizeof((pdx)->MS_Lib.pagemap))
25233842cedSCho, Yu-Chen #define memstick_logaddr(logadr1, logadr0) ((((u16)(logadr1)) << 8) | (logadr0))
25333842cedSCho, Yu-Chen 
25441e568d1Shuajun li 
25541e568d1Shuajun li struct SD_STATUS {
25641e568d1Shuajun li 	u8    Insert:1;
25741e568d1Shuajun li 	u8    Ready:1;
25841e568d1Shuajun li 	u8    MediaChange:1;
25941e568d1Shuajun li 	u8    IsMMC:1;
26041e568d1Shuajun li 	u8    HiCapacity:1;
26141e568d1Shuajun li 	u8    HiSpeed:1;
26241e568d1Shuajun li 	u8    WtP:1;
26341e568d1Shuajun li 	u8    Reserved:1;
26441e568d1Shuajun li };
26541e568d1Shuajun li 
26641e568d1Shuajun li struct MS_STATUS {
26741e568d1Shuajun li 	u8    Insert:1;
26841e568d1Shuajun li 	u8    Ready:1;
26941e568d1Shuajun li 	u8    MediaChange:1;
27041e568d1Shuajun li 	u8    IsMSPro:1;
27141e568d1Shuajun li 	u8    IsMSPHG:1;
27241e568d1Shuajun li 	u8    Reserved1:1;
27341e568d1Shuajun li 	u8    WtP:1;
27441e568d1Shuajun li 	u8    Reserved2:1;
27541e568d1Shuajun li };
27641e568d1Shuajun li 
27741e568d1Shuajun li struct SM_STATUS {
27841e568d1Shuajun li 	u8    Insert:1;
27941e568d1Shuajun li 	u8    Ready:1;
28041e568d1Shuajun li 	u8    MediaChange:1;
28141e568d1Shuajun li 	u8    Reserved:3;
28241e568d1Shuajun li 	u8    WtP:1;
28341e568d1Shuajun li 	u8    IsMS:1;
28441e568d1Shuajun li };
28541e568d1Shuajun li 
28633842cedSCho, Yu-Chen struct ms_bootblock_cis {
28733842cedSCho, Yu-Chen 	u8 bCistplDEVICE[6];    /* 0 */
28833842cedSCho, Yu-Chen 	u8 bCistplDEVICE0C[6];  /* 6 */
28933842cedSCho, Yu-Chen 	u8 bCistplJEDECC[4];    /* 12 */
29033842cedSCho, Yu-Chen 	u8 bCistplMANFID[6];    /* 16 */
29133842cedSCho, Yu-Chen 	u8 bCistplVER1[32];     /* 22 */
29233842cedSCho, Yu-Chen 	u8 bCistplFUNCID[4];    /* 54 */
29333842cedSCho, Yu-Chen 	u8 bCistplFUNCE0[4];    /* 58 */
29433842cedSCho, Yu-Chen 	u8 bCistplFUNCE1[5];    /* 62 */
29533842cedSCho, Yu-Chen 	u8 bCistplCONF[7];      /* 67 */
29633842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT0[10];/* 74 */
29733842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT1[8]; /* 84 */
29833842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT2[12];/* 92 */
29933842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT3[8]; /* 104 */
30033842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT4[17];/* 112 */
30133842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT5[8]; /* 129 */
30233842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT6[17];/* 137 */
30333842cedSCho, Yu-Chen 	u8 bCistplCFTBLENT7[8]; /* 154 */
30433842cedSCho, Yu-Chen 	u8 bCistplNOLINK[3];    /* 162 */
30533842cedSCho, Yu-Chen } ;
30633842cedSCho, Yu-Chen 
30733842cedSCho, Yu-Chen struct ms_bootblock_idi {
30833842cedSCho, Yu-Chen #define MS_IDI_GENERAL_CONF 0x848A
30933842cedSCho, Yu-Chen 	u16 wIDIgeneralConfiguration;	/* 0 */
31033842cedSCho, Yu-Chen 	u16 wIDInumberOfCylinder;	/* 1 */
31133842cedSCho, Yu-Chen 	u16 wIDIreserved0;		/* 2 */
31233842cedSCho, Yu-Chen 	u16 wIDInumberOfHead;		/* 3 */
31333842cedSCho, Yu-Chen 	u16 wIDIbytesPerTrack;		/* 4 */
31433842cedSCho, Yu-Chen 	u16 wIDIbytesPerSector;		/* 5 */
31533842cedSCho, Yu-Chen 	u16 wIDIsectorsPerTrack;	/* 6 */
31633842cedSCho, Yu-Chen 	u16 wIDItotalSectors[2];	/* 7-8  high,low */
31733842cedSCho, Yu-Chen 	u16 wIDIreserved1[11];		/* 9-19 */
31833842cedSCho, Yu-Chen 	u16 wIDIbufferType;		/* 20 */
31933842cedSCho, Yu-Chen 	u16 wIDIbufferSize;		/* 21 */
32033842cedSCho, Yu-Chen 	u16 wIDIlongCmdECC;		/* 22 */
32133842cedSCho, Yu-Chen 	u16 wIDIfirmVersion[4];		/* 23-26 */
32233842cedSCho, Yu-Chen 	u16 wIDImodelName[20];		/* 27-46 */
32333842cedSCho, Yu-Chen 	u16 wIDIreserved2;		/* 47 */
32433842cedSCho, Yu-Chen 	u16 wIDIlongWordSupported;	/* 48 */
32533842cedSCho, Yu-Chen 	u16 wIDIdmaSupported;		/* 49 */
32633842cedSCho, Yu-Chen 	u16 wIDIreserved3;		/* 50 */
32733842cedSCho, Yu-Chen 	u16 wIDIpioTiming;		/* 51 */
32833842cedSCho, Yu-Chen 	u16 wIDIdmaTiming;		/* 52 */
32933842cedSCho, Yu-Chen 	u16 wIDItransferParameter;	/* 53 */
33033842cedSCho, Yu-Chen 	u16 wIDIformattedCylinder;	/* 54 */
33133842cedSCho, Yu-Chen 	u16 wIDIformattedHead;		/* 55 */
33233842cedSCho, Yu-Chen 	u16 wIDIformattedSectorsPerTrack;/* 56 */
33333842cedSCho, Yu-Chen 	u16 wIDIformattedTotalSectors[2];/* 57-58 */
33433842cedSCho, Yu-Chen 	u16 wIDImultiSector;		/* 59 */
33533842cedSCho, Yu-Chen 	u16 wIDIlbaSectors[2];		/* 60-61 */
33633842cedSCho, Yu-Chen 	u16 wIDIsingleWordDMA;		/* 62 */
33733842cedSCho, Yu-Chen 	u16 wIDImultiWordDMA;		/* 63 */
33833842cedSCho, Yu-Chen 	u16 wIDIreserved4[192];		/* 64-255 */
33933842cedSCho, Yu-Chen };
34033842cedSCho, Yu-Chen 
34133842cedSCho, Yu-Chen struct ms_bootblock_sysent_rec {
34233842cedSCho, Yu-Chen 	u32 dwStart;
34333842cedSCho, Yu-Chen 	u32 dwSize;
34433842cedSCho, Yu-Chen 	u8 bType;
34533842cedSCho, Yu-Chen 	u8 bReserved[3];
34633842cedSCho, Yu-Chen };
34733842cedSCho, Yu-Chen 
34833842cedSCho, Yu-Chen struct ms_bootblock_sysent {
34933842cedSCho, Yu-Chen 	struct ms_bootblock_sysent_rec entry[MS_NUMBER_OF_SYSTEM_ENTRY];
35033842cedSCho, Yu-Chen };
35133842cedSCho, Yu-Chen 
35233842cedSCho, Yu-Chen struct ms_bootblock_sysinf {
35333842cedSCho, Yu-Chen 	u8 bMsClass;			/* must be 1 */
35433842cedSCho, Yu-Chen 	u8 bCardType;			/* see below */
35533842cedSCho, Yu-Chen 	u16 wBlockSize;			/* n KB */
35633842cedSCho, Yu-Chen 	u16 wBlockNumber;		/* number of physical block */
35733842cedSCho, Yu-Chen 	u16 wTotalBlockNumber;		/* number of logical block */
35833842cedSCho, Yu-Chen 	u16 wPageSize;			/* must be 0x200 */
35933842cedSCho, Yu-Chen 	u8 bExtraSize;			/* 0x10 */
36033842cedSCho, Yu-Chen 	u8 bSecuritySupport;
36133842cedSCho, Yu-Chen 	u8 bAssemblyDate[8];
36233842cedSCho, Yu-Chen 	u8 bFactoryArea[4];
36333842cedSCho, Yu-Chen 	u8 bAssemblyMakerCode;
36433842cedSCho, Yu-Chen 	u8 bAssemblyMachineCode[3];
36533842cedSCho, Yu-Chen 	u16 wMemoryMakerCode;
36633842cedSCho, Yu-Chen 	u16 wMemoryDeviceCode;
36733842cedSCho, Yu-Chen 	u16 wMemorySize;
36833842cedSCho, Yu-Chen 	u8 bReserved1;
36933842cedSCho, Yu-Chen 	u8 bReserved2;
37033842cedSCho, Yu-Chen 	u8 bVCC;
37133842cedSCho, Yu-Chen 	u8 bVPP;
37233842cedSCho, Yu-Chen 	u16 wControllerChipNumber;
37333842cedSCho, Yu-Chen 	u16 wControllerFunction;	/* New MS */
37433842cedSCho, Yu-Chen 	u8 bReserved3[9];		/* New MS */
37533842cedSCho, Yu-Chen 	u8 bParallelSupport;		/* New MS */
37633842cedSCho, Yu-Chen 	u16 wFormatValue;		/* New MS */
37733842cedSCho, Yu-Chen 	u8 bFormatType;
37833842cedSCho, Yu-Chen 	u8 bUsage;
37933842cedSCho, Yu-Chen 	u8 bDeviceType;
38033842cedSCho, Yu-Chen 	u8 bReserved4[22];
38133842cedSCho, Yu-Chen 	u8 bFUValue3;
38233842cedSCho, Yu-Chen 	u8 bFUValue4;
38333842cedSCho, Yu-Chen 	u8 bReserved5[15];
38433842cedSCho, Yu-Chen };
38533842cedSCho, Yu-Chen 
38633842cedSCho, Yu-Chen struct ms_bootblock_header {
38733842cedSCho, Yu-Chen 	u16 wBlockID;
38833842cedSCho, Yu-Chen 	u16 wFormatVersion;
38933842cedSCho, Yu-Chen 	u8 bReserved1[184];
39033842cedSCho, Yu-Chen 	u8 bNumberOfDataEntry;
39133842cedSCho, Yu-Chen 	u8 bReserved2[179];
39233842cedSCho, Yu-Chen };
39333842cedSCho, Yu-Chen 
39433842cedSCho, Yu-Chen struct ms_bootblock_page0 {
39533842cedSCho, Yu-Chen 	struct ms_bootblock_header header;
39633842cedSCho, Yu-Chen 	struct ms_bootblock_sysent sysent;
39733842cedSCho, Yu-Chen 	struct ms_bootblock_sysinf sysinf;
39833842cedSCho, Yu-Chen };
39933842cedSCho, Yu-Chen 
40033842cedSCho, Yu-Chen struct ms_bootblock_cis_idi {
40133842cedSCho, Yu-Chen 	union {
40233842cedSCho, Yu-Chen 		struct ms_bootblock_cis cis;
40333842cedSCho, Yu-Chen 		u8 dmy[256];
40433842cedSCho, Yu-Chen 	} cis;
40533842cedSCho, Yu-Chen 
40633842cedSCho, Yu-Chen 	union {
40733842cedSCho, Yu-Chen 		struct ms_bootblock_idi idi;
40833842cedSCho, Yu-Chen 		u8 dmy[256];
40933842cedSCho, Yu-Chen 	} idi;
41033842cedSCho, Yu-Chen 
41133842cedSCho, Yu-Chen };
41233842cedSCho, Yu-Chen 
41333842cedSCho, Yu-Chen /* ENE MS Lib struct */
41433842cedSCho, Yu-Chen struct ms_lib_type_extdat {
41533842cedSCho, Yu-Chen 	u8 reserved;
41633842cedSCho, Yu-Chen 	u8 intr;
41733842cedSCho, Yu-Chen 	u8 status0;
41833842cedSCho, Yu-Chen 	u8 status1;
41933842cedSCho, Yu-Chen 	u8 ovrflg;
42033842cedSCho, Yu-Chen 	u8 mngflg;
42133842cedSCho, Yu-Chen 	u16 logadr;
42233842cedSCho, Yu-Chen };
42333842cedSCho, Yu-Chen 
42433842cedSCho, Yu-Chen struct ms_lib_ctrl {
42533842cedSCho, Yu-Chen 	u32 flags;
42633842cedSCho, Yu-Chen 	u32 BytesPerSector;
42733842cedSCho, Yu-Chen 	u32 NumberOfCylinder;
42833842cedSCho, Yu-Chen 	u32 SectorsPerCylinder;
42933842cedSCho, Yu-Chen 	u16 cardType;			/* R/W, RO, Hybrid */
43033842cedSCho, Yu-Chen 	u16 blockSize;
43133842cedSCho, Yu-Chen 	u16 PagesPerBlock;
43233842cedSCho, Yu-Chen 	u16 NumberOfPhyBlock;
43333842cedSCho, Yu-Chen 	u16 NumberOfLogBlock;
43433842cedSCho, Yu-Chen 	u16 NumberOfSegment;
43533842cedSCho, Yu-Chen 	u16 *Phy2LogMap;		/* phy2log table */
43633842cedSCho, Yu-Chen 	u16 *Log2PhyMap;		/* log2phy table */
43733842cedSCho, Yu-Chen 	u16 wrtblk;
43833842cedSCho, Yu-Chen 	unsigned char *pagemap[(MS_MAX_PAGES_PER_BLOCK + (MS_LIB_BITS_PER_BYTE-1)) / MS_LIB_BITS_PER_BYTE];
43933842cedSCho, Yu-Chen 	unsigned char *blkpag;
44033842cedSCho, Yu-Chen 	struct ms_lib_type_extdat *blkext;
44133842cedSCho, Yu-Chen 	unsigned char copybuf[512];
44233842cedSCho, Yu-Chen };
44333842cedSCho, Yu-Chen 
44441e568d1Shuajun li 
44541e568d1Shuajun li /* SD Block Length */
44641e568d1Shuajun li /* 2^9 = 512 Bytes, The HW maximum read/write data length */
44741e568d1Shuajun li #define SD_BLOCK_LEN  9
44841e568d1Shuajun li 
44941e568d1Shuajun li struct ene_ub6250_info {
450628c2893SAlan Stern 
451628c2893SAlan Stern 	/* I/O bounce buffer */
452628c2893SAlan Stern 	u8		*bbuf;
453628c2893SAlan Stern 
45441e568d1Shuajun li 	/* for 6250 code */
45541e568d1Shuajun li 	struct SD_STATUS	SD_Status;
45641e568d1Shuajun li 	struct MS_STATUS	MS_Status;
45741e568d1Shuajun li 	struct SM_STATUS	SM_Status;
45841e568d1Shuajun li 
45941e568d1Shuajun li 	/* ----- SD Control Data ---------------- */
46041e568d1Shuajun li 	/*SD_REGISTER SD_Regs; */
46141e568d1Shuajun li 	u16		SD_Block_Mult;
46241e568d1Shuajun li 	u8		SD_READ_BL_LEN;
46341e568d1Shuajun li 	u16		SD_C_SIZE;
46441e568d1Shuajun li 	u8		SD_C_SIZE_MULT;
46541e568d1Shuajun li 
46641e568d1Shuajun li 	/* SD/MMC New spec. */
46741e568d1Shuajun li 	u8		SD_SPEC_VER;
46841e568d1Shuajun li 	u8		SD_CSD_VER;
46941e568d1Shuajun li 	u8		SD20_HIGH_CAPACITY;
47041e568d1Shuajun li 	u32		HC_C_SIZE;
47141e568d1Shuajun li 	u8		MMC_SPEC_VER;
47241e568d1Shuajun li 	u8		MMC_BusWidth;
47341e568d1Shuajun li 	u8		MMC_HIGH_CAPACITY;
47441e568d1Shuajun li 
47541e568d1Shuajun li 	/*----- MS Control Data ---------------- */
47641e568d1Shuajun li 	bool		MS_SWWP;
47741e568d1Shuajun li 	u32		MSP_TotalBlock;
47833842cedSCho, Yu-Chen 	struct ms_lib_ctrl MS_Lib;
47941e568d1Shuajun li 	bool		MS_IsRWPage;
48041e568d1Shuajun li 	u16		MS_Model;
48141e568d1Shuajun li 
48241e568d1Shuajun li 	/*----- SM Control Data ---------------- */
48341e568d1Shuajun li 	u8		SM_DeviceID;
48441e568d1Shuajun li 	u8		SM_CardID;
48541e568d1Shuajun li 
48641e568d1Shuajun li 	unsigned char	*testbuf;
48741e568d1Shuajun li 	u8		BIN_FLAG;
48841e568d1Shuajun li 	u32		bl_num;
48941e568d1Shuajun li 	int		SrbStatus;
49041e568d1Shuajun li 
49141e568d1Shuajun li 	/*------Power Managerment ---------------*/
49241e568d1Shuajun li 	bool		Power_IsResum;
49341e568d1Shuajun li };
49441e568d1Shuajun li 
49541e568d1Shuajun li static int ene_sd_init(struct us_data *us);
49633842cedSCho, Yu-Chen static int ene_ms_init(struct us_data *us);
49741e568d1Shuajun li static int ene_load_bincode(struct us_data *us, unsigned char flag);
49841e568d1Shuajun li 
49941e568d1Shuajun li static void ene_ub6250_info_destructor(void *extra)
50041e568d1Shuajun li {
501628c2893SAlan Stern 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) extra;
502628c2893SAlan Stern 
50341e568d1Shuajun li 	if (!extra)
50441e568d1Shuajun li 		return;
505628c2893SAlan Stern 	kfree(info->bbuf);
50641e568d1Shuajun li }
50741e568d1Shuajun li 
50841e568d1Shuajun li static int ene_send_scsi_cmd(struct us_data *us, u8 fDir, void *buf, int use_sg)
50941e568d1Shuajun li {
51041e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
51141e568d1Shuajun li 	struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
51241e568d1Shuajun li 
51341e568d1Shuajun li 	int result;
51441e568d1Shuajun li 	unsigned int residue;
51541e568d1Shuajun li 	unsigned int cswlen = 0, partial = 0;
51641e568d1Shuajun li 	unsigned int transfer_length = bcb->DataTransferLength;
51741e568d1Shuajun li 
518191648d0SJoe Perches 	/* usb_stor_dbg(us, "transport --- ene_send_scsi_cmd\n"); */
51941e568d1Shuajun li 	/* send cmd to out endpoint */
52041e568d1Shuajun li 	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
52141e568d1Shuajun li 					    bcb, US_BULK_CB_WRAP_LEN, NULL);
52241e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
523191648d0SJoe Perches 		usb_stor_dbg(us, "send cmd to out endpoint fail ---\n");
52441e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
52541e568d1Shuajun li 	}
52641e568d1Shuajun li 
52741e568d1Shuajun li 	if (buf) {
52841e568d1Shuajun li 		unsigned int pipe = fDir;
52941e568d1Shuajun li 
53041e568d1Shuajun li 		if (fDir  == FDIR_READ)
53141e568d1Shuajun li 			pipe = us->recv_bulk_pipe;
53241e568d1Shuajun li 		else
53341e568d1Shuajun li 			pipe = us->send_bulk_pipe;
53441e568d1Shuajun li 
53541e568d1Shuajun li 		/* Bulk */
53641e568d1Shuajun li 		if (use_sg) {
53741e568d1Shuajun li 			result = usb_stor_bulk_srb(us, pipe, us->srb);
53841e568d1Shuajun li 		} else {
53941e568d1Shuajun li 			result = usb_stor_bulk_transfer_sg(us, pipe, buf,
54041e568d1Shuajun li 						transfer_length, 0, &partial);
54141e568d1Shuajun li 		}
54241e568d1Shuajun li 		if (result != USB_STOR_XFER_GOOD) {
543191648d0SJoe Perches 			usb_stor_dbg(us, "data transfer fail ---\n");
54441e568d1Shuajun li 			return USB_STOR_TRANSPORT_ERROR;
54541e568d1Shuajun li 		}
54641e568d1Shuajun li 	}
54741e568d1Shuajun li 
54841e568d1Shuajun li 	/* Get CSW for device status */
54941e568d1Shuajun li 	result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
55041e568d1Shuajun li 					    US_BULK_CS_WRAP_LEN, &cswlen);
55141e568d1Shuajun li 
55241e568d1Shuajun li 	if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
553191648d0SJoe Perches 		usb_stor_dbg(us, "Received 0-length CSW; retrying...\n");
55441e568d1Shuajun li 		result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
55541e568d1Shuajun li 					    bcs, US_BULK_CS_WRAP_LEN, &cswlen);
55641e568d1Shuajun li 	}
55741e568d1Shuajun li 
55841e568d1Shuajun li 	if (result == USB_STOR_XFER_STALLED) {
55941e568d1Shuajun li 		/* get the status again */
560191648d0SJoe Perches 		usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n");
56141e568d1Shuajun li 		result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
56241e568d1Shuajun li 						bcs, US_BULK_CS_WRAP_LEN, NULL);
56341e568d1Shuajun li 	}
56441e568d1Shuajun li 
56541e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD)
56641e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
56741e568d1Shuajun li 
56841e568d1Shuajun li 	/* check bulk status */
56941e568d1Shuajun li 	residue = le32_to_cpu(bcs->Residue);
57041e568d1Shuajun li 
571f0183a33SFelipe Balbi 	/*
572f0183a33SFelipe Balbi 	 * try to compute the actual residue, based on how much data
573f0183a33SFelipe Balbi 	 * was really transferred and what the device tells us
574f0183a33SFelipe Balbi 	 */
57541e568d1Shuajun li 	if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
57641e568d1Shuajun li 		residue = min(residue, transfer_length);
57741e568d1Shuajun li 		if (us->srb != NULL)
57841e568d1Shuajun li 			scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
57941e568d1Shuajun li 								(int)residue));
58041e568d1Shuajun li 	}
58141e568d1Shuajun li 
58241e568d1Shuajun li 	if (bcs->Status != US_BULK_STAT_OK)
58341e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
58441e568d1Shuajun li 
58541e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
58641e568d1Shuajun li }
58741e568d1Shuajun li 
588ce553bd1SAlan Stern static int do_scsi_request_sense(struct us_data *us, struct scsi_cmnd *srb)
589ce553bd1SAlan Stern {
590ce553bd1SAlan Stern 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
591ce553bd1SAlan Stern 	unsigned char buf[18];
592ce553bd1SAlan Stern 
593ce553bd1SAlan Stern 	memset(buf, 0, 18);
594ce553bd1SAlan Stern 	buf[0] = 0x70;				/* Current error */
595ce553bd1SAlan Stern 	buf[2] = info->SrbStatus >> 16;		/* Sense key */
596ce553bd1SAlan Stern 	buf[7] = 10;				/* Additional length */
597ce553bd1SAlan Stern 	buf[12] = info->SrbStatus >> 8;		/* ASC */
598ce553bd1SAlan Stern 	buf[13] = info->SrbStatus;		/* ASCQ */
599ce553bd1SAlan Stern 
600ce553bd1SAlan Stern 	usb_stor_set_xfer_buf(buf, sizeof(buf), srb);
601ce553bd1SAlan Stern 	return USB_STOR_TRANSPORT_GOOD;
602ce553bd1SAlan Stern }
603ce553bd1SAlan Stern 
604f8efdabdSAlan Stern static int do_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
605f8efdabdSAlan Stern {
606f8efdabdSAlan Stern 	unsigned char data_ptr[36] = {
6075fcf9379SAlan Stern 		0x00, 0x00, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
608f8efdabdSAlan Stern 		0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
609f8efdabdSAlan Stern 		0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
610f8efdabdSAlan Stern 		0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30 };
611f8efdabdSAlan Stern 
612f8efdabdSAlan Stern 	usb_stor_set_xfer_buf(data_ptr, 36, srb);
613f8efdabdSAlan Stern 	return USB_STOR_TRANSPORT_GOOD;
614f8efdabdSAlan Stern }
615f8efdabdSAlan Stern 
61641e568d1Shuajun li static int sd_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
61741e568d1Shuajun li {
61841e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
61941e568d1Shuajun li 
62041e568d1Shuajun li 	if (info->SD_Status.Insert && info->SD_Status.Ready)
62141e568d1Shuajun li 		return USB_STOR_TRANSPORT_GOOD;
62241e568d1Shuajun li 	else {
62341e568d1Shuajun li 		ene_sd_init(us);
62441e568d1Shuajun li 		return USB_STOR_TRANSPORT_GOOD;
62541e568d1Shuajun li 	}
62641e568d1Shuajun li 
62741e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
62841e568d1Shuajun li }
62941e568d1Shuajun li 
63041e568d1Shuajun li static int sd_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
63141e568d1Shuajun li {
63241e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
63341e568d1Shuajun li 	unsigned char mediaNoWP[12] = {
63441e568d1Shuajun li 		0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
63541e568d1Shuajun li 		0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
63641e568d1Shuajun li 	unsigned char mediaWP[12]   = {
63741e568d1Shuajun li 		0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
63841e568d1Shuajun li 		0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
63941e568d1Shuajun li 
64041e568d1Shuajun li 	if (info->SD_Status.WtP)
64141e568d1Shuajun li 		usb_stor_set_xfer_buf(mediaWP, 12, srb);
64241e568d1Shuajun li 	else
64341e568d1Shuajun li 		usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
64441e568d1Shuajun li 
64541e568d1Shuajun li 
64641e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
64741e568d1Shuajun li }
64841e568d1Shuajun li 
64941e568d1Shuajun li static int sd_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
65041e568d1Shuajun li {
65141e568d1Shuajun li 	u32	bl_num;
65236f3a14dSFelipe Balbi 	u32	bl_len;
65341e568d1Shuajun li 	unsigned int offset = 0;
65441e568d1Shuajun li 	unsigned char    buf[8];
65541e568d1Shuajun li 	struct scatterlist *sg = NULL;
65641e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
65741e568d1Shuajun li 
658191648d0SJoe Perches 	usb_stor_dbg(us, "sd_scsi_read_capacity\n");
65941e568d1Shuajun li 	if (info->SD_Status.HiCapacity) {
66041e568d1Shuajun li 		bl_len = 0x200;
66141e568d1Shuajun li 		if (info->SD_Status.IsMMC)
66241e568d1Shuajun li 			bl_num = info->HC_C_SIZE-1;
66341e568d1Shuajun li 		else
66441e568d1Shuajun li 			bl_num = (info->HC_C_SIZE + 1) * 1024 - 1;
66541e568d1Shuajun li 	} else {
66641e568d1Shuajun li 		bl_len = 1 << (info->SD_READ_BL_LEN);
66741e568d1Shuajun li 		bl_num = info->SD_Block_Mult * (info->SD_C_SIZE + 1)
66841e568d1Shuajun li 				* (1 << (info->SD_C_SIZE_MULT + 2)) - 1;
66941e568d1Shuajun li 	}
67041e568d1Shuajun li 	info->bl_num = bl_num;
671191648d0SJoe Perches 	usb_stor_dbg(us, "bl_len = %x\n", bl_len);
672191648d0SJoe Perches 	usb_stor_dbg(us, "bl_num = %x\n", bl_num);
67341e568d1Shuajun li 
67441e568d1Shuajun li 	/*srb->request_bufflen = 8; */
67541e568d1Shuajun li 	buf[0] = (bl_num >> 24) & 0xff;
67641e568d1Shuajun li 	buf[1] = (bl_num >> 16) & 0xff;
67741e568d1Shuajun li 	buf[2] = (bl_num >> 8) & 0xff;
67841e568d1Shuajun li 	buf[3] = (bl_num >> 0) & 0xff;
67941e568d1Shuajun li 	buf[4] = (bl_len >> 24) & 0xff;
68041e568d1Shuajun li 	buf[5] = (bl_len >> 16) & 0xff;
68141e568d1Shuajun li 	buf[6] = (bl_len >> 8) & 0xff;
68241e568d1Shuajun li 	buf[7] = (bl_len >> 0) & 0xff;
68341e568d1Shuajun li 
68441e568d1Shuajun li 	usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
68541e568d1Shuajun li 
68641e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
68741e568d1Shuajun li }
68841e568d1Shuajun li 
68941e568d1Shuajun li static int sd_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
69041e568d1Shuajun li {
69141e568d1Shuajun li 	int result;
69241e568d1Shuajun li 	unsigned char *cdb = srb->cmnd;
69341e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
69441e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
69541e568d1Shuajun li 
69641e568d1Shuajun li 	u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
69741e568d1Shuajun li 		 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
69841e568d1Shuajun li 	u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
69941e568d1Shuajun li 	u32 bnByte = bn * 0x200;
70041e568d1Shuajun li 	u32 blenByte = blen * 0x200;
70141e568d1Shuajun li 
70241e568d1Shuajun li 	if (bn > info->bl_num)
70341e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
70441e568d1Shuajun li 
70541e568d1Shuajun li 	result = ene_load_bincode(us, SD_RW_PATTERN);
70641e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
707191648d0SJoe Perches 		usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
70841e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
70941e568d1Shuajun li 	}
71041e568d1Shuajun li 
71141e568d1Shuajun li 	if (info->SD_Status.HiCapacity)
71241e568d1Shuajun li 		bnByte = bn;
71341e568d1Shuajun li 
71441e568d1Shuajun li 	/* set up the command wrapper */
71541e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
71641e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
71741e568d1Shuajun li 	bcb->DataTransferLength = blenByte;
718b8db6d64SSebastian Andrzej Siewior 	bcb->Flags  = US_BULK_FLAG_IN;
71941e568d1Shuajun li 	bcb->CDB[0] = 0xF1;
72041e568d1Shuajun li 	bcb->CDB[5] = (unsigned char)(bnByte);
72141e568d1Shuajun li 	bcb->CDB[4] = (unsigned char)(bnByte>>8);
72241e568d1Shuajun li 	bcb->CDB[3] = (unsigned char)(bnByte>>16);
72341e568d1Shuajun li 	bcb->CDB[2] = (unsigned char)(bnByte>>24);
72441e568d1Shuajun li 
72541e568d1Shuajun li 	result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
72641e568d1Shuajun li 	return result;
72741e568d1Shuajun li }
72841e568d1Shuajun li 
72941e568d1Shuajun li static int sd_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
73041e568d1Shuajun li {
73141e568d1Shuajun li 	int result;
73241e568d1Shuajun li 	unsigned char *cdb = srb->cmnd;
73341e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
73441e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
73541e568d1Shuajun li 
73641e568d1Shuajun li 	u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
73741e568d1Shuajun li 		 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
73841e568d1Shuajun li 	u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
73941e568d1Shuajun li 	u32 bnByte = bn * 0x200;
74041e568d1Shuajun li 	u32 blenByte = blen * 0x200;
74141e568d1Shuajun li 
74241e568d1Shuajun li 	if (bn > info->bl_num)
74341e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
74441e568d1Shuajun li 
74541e568d1Shuajun li 	result = ene_load_bincode(us, SD_RW_PATTERN);
74641e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
747191648d0SJoe Perches 		usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
74841e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
74941e568d1Shuajun li 	}
75041e568d1Shuajun li 
75141e568d1Shuajun li 	if (info->SD_Status.HiCapacity)
75241e568d1Shuajun li 		bnByte = bn;
75341e568d1Shuajun li 
75441e568d1Shuajun li 	/* set up the command wrapper */
75541e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
75641e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
75741e568d1Shuajun li 	bcb->DataTransferLength = blenByte;
75841e568d1Shuajun li 	bcb->Flags  = 0x00;
75941e568d1Shuajun li 	bcb->CDB[0] = 0xF0;
76041e568d1Shuajun li 	bcb->CDB[5] = (unsigned char)(bnByte);
76141e568d1Shuajun li 	bcb->CDB[4] = (unsigned char)(bnByte>>8);
76241e568d1Shuajun li 	bcb->CDB[3] = (unsigned char)(bnByte>>16);
76341e568d1Shuajun li 	bcb->CDB[2] = (unsigned char)(bnByte>>24);
76441e568d1Shuajun li 
76541e568d1Shuajun li 	result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
76641e568d1Shuajun li 	return result;
76741e568d1Shuajun li }
76841e568d1Shuajun li 
76933842cedSCho, Yu-Chen /*
77033842cedSCho, Yu-Chen  * ENE MS Card
77133842cedSCho, Yu-Chen  */
77233842cedSCho, Yu-Chen 
77333842cedSCho, Yu-Chen static int ms_lib_set_logicalpair(struct us_data *us, u16 logblk, u16 phyblk)
77433842cedSCho, Yu-Chen {
77533842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
77633842cedSCho, Yu-Chen 
77733842cedSCho, Yu-Chen 	if ((logblk >= info->MS_Lib.NumberOfLogBlock) || (phyblk >= info->MS_Lib.NumberOfPhyBlock))
77833842cedSCho, Yu-Chen 		return (u32)-1;
77933842cedSCho, Yu-Chen 
78033842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap[phyblk] = logblk;
78133842cedSCho, Yu-Chen 	info->MS_Lib.Log2PhyMap[logblk] = phyblk;
78233842cedSCho, Yu-Chen 
78333842cedSCho, Yu-Chen 	return 0;
78433842cedSCho, Yu-Chen }
78533842cedSCho, Yu-Chen 
78633842cedSCho, Yu-Chen static int ms_lib_set_logicalblockmark(struct us_data *us, u16 phyblk, u16 mark)
78733842cedSCho, Yu-Chen {
78833842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
78933842cedSCho, Yu-Chen 
79033842cedSCho, Yu-Chen 	if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
79133842cedSCho, Yu-Chen 		return (u32)-1;
79233842cedSCho, Yu-Chen 
79333842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap[phyblk] = mark;
79433842cedSCho, Yu-Chen 
79533842cedSCho, Yu-Chen 	return 0;
79633842cedSCho, Yu-Chen }
79733842cedSCho, Yu-Chen 
79833842cedSCho, Yu-Chen static int ms_lib_set_initialerrorblock(struct us_data *us, u16 phyblk)
79933842cedSCho, Yu-Chen {
80033842cedSCho, Yu-Chen 	return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_INITIAL_ERROR);
80133842cedSCho, Yu-Chen }
80233842cedSCho, Yu-Chen 
80333842cedSCho, Yu-Chen static int ms_lib_set_bootblockmark(struct us_data *us, u16 phyblk)
80433842cedSCho, Yu-Chen {
80533842cedSCho, Yu-Chen 	return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_BOOT_BLOCK);
80633842cedSCho, Yu-Chen }
80733842cedSCho, Yu-Chen 
80833842cedSCho, Yu-Chen static int ms_lib_free_logicalmap(struct us_data *us)
80933842cedSCho, Yu-Chen {
81033842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
81133842cedSCho, Yu-Chen 
81233842cedSCho, Yu-Chen 	kfree(info->MS_Lib.Phy2LogMap);
81333842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap = NULL;
81433842cedSCho, Yu-Chen 
81533842cedSCho, Yu-Chen 	kfree(info->MS_Lib.Log2PhyMap);
81633842cedSCho, Yu-Chen 	info->MS_Lib.Log2PhyMap = NULL;
81733842cedSCho, Yu-Chen 
81833842cedSCho, Yu-Chen 	return 0;
81933842cedSCho, Yu-Chen }
82033842cedSCho, Yu-Chen 
82136f3a14dSFelipe Balbi static int ms_lib_alloc_logicalmap(struct us_data *us)
82233842cedSCho, Yu-Chen {
82333842cedSCho, Yu-Chen 	u32  i;
82433842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
82533842cedSCho, Yu-Chen 
82633842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap = kmalloc(info->MS_Lib.NumberOfPhyBlock * sizeof(u16), GFP_KERNEL);
82733842cedSCho, Yu-Chen 	info->MS_Lib.Log2PhyMap = kmalloc(info->MS_Lib.NumberOfLogBlock * sizeof(u16), GFP_KERNEL);
82833842cedSCho, Yu-Chen 
82933842cedSCho, Yu-Chen 	if ((info->MS_Lib.Phy2LogMap == NULL) || (info->MS_Lib.Log2PhyMap == NULL)) {
83033842cedSCho, Yu-Chen 		ms_lib_free_logicalmap(us);
83133842cedSCho, Yu-Chen 		return (u32)-1;
83233842cedSCho, Yu-Chen 	}
83333842cedSCho, Yu-Chen 
83433842cedSCho, Yu-Chen 	for (i = 0; i < info->MS_Lib.NumberOfPhyBlock; i++)
83533842cedSCho, Yu-Chen 		info->MS_Lib.Phy2LogMap[i] = MS_LB_NOT_USED;
83633842cedSCho, Yu-Chen 
83733842cedSCho, Yu-Chen 	for (i = 0; i < info->MS_Lib.NumberOfLogBlock; i++)
83833842cedSCho, Yu-Chen 		info->MS_Lib.Log2PhyMap[i] = MS_LB_NOT_USED;
83933842cedSCho, Yu-Chen 
84033842cedSCho, Yu-Chen 	return 0;
84133842cedSCho, Yu-Chen }
84233842cedSCho, Yu-Chen 
84333842cedSCho, Yu-Chen static void ms_lib_clear_writebuf(struct us_data *us)
84433842cedSCho, Yu-Chen {
84533842cedSCho, Yu-Chen 	int i;
84633842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
84733842cedSCho, Yu-Chen 
84833842cedSCho, Yu-Chen 	info->MS_Lib.wrtblk = (u16)-1;
84933842cedSCho, Yu-Chen 	ms_lib_clear_pagemap(info);
85033842cedSCho, Yu-Chen 
85133842cedSCho, Yu-Chen 	if (info->MS_Lib.blkpag)
85233842cedSCho, Yu-Chen 		memset(info->MS_Lib.blkpag, 0xff, info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector);
85333842cedSCho, Yu-Chen 
85433842cedSCho, Yu-Chen 	if (info->MS_Lib.blkext) {
85533842cedSCho, Yu-Chen 		for (i = 0; i < info->MS_Lib.PagesPerBlock; i++) {
85633842cedSCho, Yu-Chen 			info->MS_Lib.blkext[i].status1 = MS_REG_ST1_DEFAULT;
85733842cedSCho, Yu-Chen 			info->MS_Lib.blkext[i].ovrflg = MS_REG_OVR_DEFAULT;
85833842cedSCho, Yu-Chen 			info->MS_Lib.blkext[i].mngflg = MS_REG_MNG_DEFAULT;
85933842cedSCho, Yu-Chen 			info->MS_Lib.blkext[i].logadr = MS_LB_NOT_USED;
86033842cedSCho, Yu-Chen 		}
86133842cedSCho, Yu-Chen 	}
86233842cedSCho, Yu-Chen }
86333842cedSCho, Yu-Chen 
86433842cedSCho, Yu-Chen static int ms_count_freeblock(struct us_data *us, u16 PhyBlock)
86533842cedSCho, Yu-Chen {
86633842cedSCho, Yu-Chen 	u32 Ende, Count;
86733842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
86833842cedSCho, Yu-Chen 
86933842cedSCho, Yu-Chen 	Ende = PhyBlock + MS_PHYSICAL_BLOCKS_PER_SEGMENT;
87033842cedSCho, Yu-Chen 	for (Count = 0; PhyBlock < Ende; PhyBlock++) {
87133842cedSCho, Yu-Chen 		switch (info->MS_Lib.Phy2LogMap[PhyBlock]) {
87233842cedSCho, Yu-Chen 		case MS_LB_NOT_USED:
87333842cedSCho, Yu-Chen 		case MS_LB_NOT_USED_ERASED:
87433842cedSCho, Yu-Chen 			Count++;
87533842cedSCho, Yu-Chen 		default:
87633842cedSCho, Yu-Chen 			break;
87733842cedSCho, Yu-Chen 		}
87833842cedSCho, Yu-Chen 	}
87933842cedSCho, Yu-Chen 
88033842cedSCho, Yu-Chen 	return Count;
88133842cedSCho, Yu-Chen }
88233842cedSCho, Yu-Chen 
88333842cedSCho, Yu-Chen static int ms_read_readpage(struct us_data *us, u32 PhyBlockAddr,
88433842cedSCho, Yu-Chen 		u8 PageNum, u32 *PageBuf, struct ms_lib_type_extdat *ExtraDat)
88533842cedSCho, Yu-Chen {
88633842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
887628c2893SAlan Stern 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
888628c2893SAlan Stern 	u8 *bbuf = info->bbuf;
88933842cedSCho, Yu-Chen 	int result;
89033842cedSCho, Yu-Chen 	u32 bn = PhyBlockAddr * 0x20 + PageNum;
89133842cedSCho, Yu-Chen 
89233842cedSCho, Yu-Chen 	result = ene_load_bincode(us, MS_RW_PATTERN);
89333842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
89433842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
89533842cedSCho, Yu-Chen 
89633842cedSCho, Yu-Chen 	/* Read Page Data */
89733842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
89833842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
89933842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x200;
900b8db6d64SSebastian Andrzej Siewior 	bcb->Flags      = US_BULK_FLAG_IN;
90133842cedSCho, Yu-Chen 	bcb->CDB[0]     = 0xF1;
90233842cedSCho, Yu-Chen 
90333842cedSCho, Yu-Chen 	bcb->CDB[1]     = 0x02; /* in init.c ENE_MSInit() is 0x01 */
90433842cedSCho, Yu-Chen 
90533842cedSCho, Yu-Chen 	bcb->CDB[5]     = (unsigned char)(bn);
90633842cedSCho, Yu-Chen 	bcb->CDB[4]     = (unsigned char)(bn>>8);
90733842cedSCho, Yu-Chen 	bcb->CDB[3]     = (unsigned char)(bn>>16);
90833842cedSCho, Yu-Chen 	bcb->CDB[2]     = (unsigned char)(bn>>24);
90933842cedSCho, Yu-Chen 
91033842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_READ, PageBuf, 0);
91133842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
91233842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
91333842cedSCho, Yu-Chen 
91433842cedSCho, Yu-Chen 
91533842cedSCho, Yu-Chen 	/* Read Extra Data */
91633842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
91733842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
91833842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x4;
919b8db6d64SSebastian Andrzej Siewior 	bcb->Flags      = US_BULK_FLAG_IN;
92033842cedSCho, Yu-Chen 	bcb->CDB[0]     = 0xF1;
92133842cedSCho, Yu-Chen 	bcb->CDB[1]     = 0x03;
92233842cedSCho, Yu-Chen 
92333842cedSCho, Yu-Chen 	bcb->CDB[5]     = (unsigned char)(PageNum);
92433842cedSCho, Yu-Chen 	bcb->CDB[4]     = (unsigned char)(PhyBlockAddr);
92533842cedSCho, Yu-Chen 	bcb->CDB[3]     = (unsigned char)(PhyBlockAddr>>8);
92633842cedSCho, Yu-Chen 	bcb->CDB[2]     = (unsigned char)(PhyBlockAddr>>16);
92733842cedSCho, Yu-Chen 	bcb->CDB[6]     = 0x01;
92833842cedSCho, Yu-Chen 
929628c2893SAlan Stern 	result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
93033842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
93133842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
93233842cedSCho, Yu-Chen 
93333842cedSCho, Yu-Chen 	ExtraDat->reserved = 0;
93433842cedSCho, Yu-Chen 	ExtraDat->intr     = 0x80;  /* Not yet,fireware support */
93533842cedSCho, Yu-Chen 	ExtraDat->status0  = 0x10;  /* Not yet,fireware support */
93633842cedSCho, Yu-Chen 
93733842cedSCho, Yu-Chen 	ExtraDat->status1  = 0x00;  /* Not yet,fireware support */
938628c2893SAlan Stern 	ExtraDat->ovrflg   = bbuf[0];
939628c2893SAlan Stern 	ExtraDat->mngflg   = bbuf[1];
940628c2893SAlan Stern 	ExtraDat->logadr   = memstick_logaddr(bbuf[2], bbuf[3]);
94133842cedSCho, Yu-Chen 
94233842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
94333842cedSCho, Yu-Chen }
94433842cedSCho, Yu-Chen 
94533842cedSCho, Yu-Chen static int ms_lib_process_bootblock(struct us_data *us, u16 PhyBlock, u8 *PageData)
94633842cedSCho, Yu-Chen {
94733842cedSCho, Yu-Chen 	struct ms_bootblock_sysent *SysEntry;
94833842cedSCho, Yu-Chen 	struct ms_bootblock_sysinf *SysInfo;
94933842cedSCho, Yu-Chen 	u32 i, result;
95033842cedSCho, Yu-Chen 	u8 PageNumber;
95133842cedSCho, Yu-Chen 	u8 *PageBuffer;
95233842cedSCho, Yu-Chen 	struct ms_lib_type_extdat ExtraData;
95333842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
95433842cedSCho, Yu-Chen 
95533842cedSCho, Yu-Chen 	PageBuffer = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
95633842cedSCho, Yu-Chen 	if (PageBuffer == NULL)
95733842cedSCho, Yu-Chen 		return (u32)-1;
95833842cedSCho, Yu-Chen 
95933842cedSCho, Yu-Chen 	result = (u32)-1;
96033842cedSCho, Yu-Chen 
96133842cedSCho, Yu-Chen 	SysInfo = &(((struct ms_bootblock_page0 *)PageData)->sysinf);
96233842cedSCho, Yu-Chen 
96333842cedSCho, Yu-Chen 	if ((SysInfo->bMsClass != MS_SYSINF_MSCLASS_TYPE_1) ||
96433842cedSCho, Yu-Chen 		(be16_to_cpu(SysInfo->wPageSize) != MS_SYSINF_PAGE_SIZE) ||
96533842cedSCho, Yu-Chen 		((SysInfo->bSecuritySupport & MS_SYSINF_SECURITY) == MS_SYSINF_SECURITY_SUPPORT) ||
96633842cedSCho, Yu-Chen 		(SysInfo->bReserved1 != MS_SYSINF_RESERVED1) ||
96733842cedSCho, Yu-Chen 		(SysInfo->bReserved2 != MS_SYSINF_RESERVED2) ||
96833842cedSCho, Yu-Chen 		(SysInfo->bFormatType != MS_SYSINF_FORMAT_FAT) ||
96933842cedSCho, Yu-Chen 		(SysInfo->bUsage != MS_SYSINF_USAGE_GENERAL))
97033842cedSCho, Yu-Chen 		goto exit;
97133842cedSCho, Yu-Chen 		/* */
97233842cedSCho, Yu-Chen 	switch (info->MS_Lib.cardType = SysInfo->bCardType) {
97333842cedSCho, Yu-Chen 	case MS_SYSINF_CARDTYPE_RDONLY:
97433842cedSCho, Yu-Chen 		ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY);
97533842cedSCho, Yu-Chen 		break;
97633842cedSCho, Yu-Chen 	case MS_SYSINF_CARDTYPE_RDWR:
97733842cedSCho, Yu-Chen 		ms_lib_ctrl_reset(info, MS_LIB_CTRL_RDONLY);
97833842cedSCho, Yu-Chen 		break;
97933842cedSCho, Yu-Chen 	case MS_SYSINF_CARDTYPE_HYBRID:
98033842cedSCho, Yu-Chen 	default:
98133842cedSCho, Yu-Chen 		goto exit;
98233842cedSCho, Yu-Chen 	}
98333842cedSCho, Yu-Chen 
98433842cedSCho, Yu-Chen 	info->MS_Lib.blockSize = be16_to_cpu(SysInfo->wBlockSize);
98533842cedSCho, Yu-Chen 	info->MS_Lib.NumberOfPhyBlock = be16_to_cpu(SysInfo->wBlockNumber);
98633842cedSCho, Yu-Chen 	info->MS_Lib.NumberOfLogBlock = be16_to_cpu(SysInfo->wTotalBlockNumber)-2;
98733842cedSCho, Yu-Chen 	info->MS_Lib.PagesPerBlock = info->MS_Lib.blockSize * SIZE_OF_KIRO / MS_BYTES_PER_PAGE;
98833842cedSCho, Yu-Chen 	info->MS_Lib.NumberOfSegment = info->MS_Lib.NumberOfPhyBlock / MS_PHYSICAL_BLOCKS_PER_SEGMENT;
98933842cedSCho, Yu-Chen 	info->MS_Model = be16_to_cpu(SysInfo->wMemorySize);
99033842cedSCho, Yu-Chen 
99133842cedSCho, Yu-Chen 	/*Allocate to all number of logicalblock and physicalblock */
99233842cedSCho, Yu-Chen 	if (ms_lib_alloc_logicalmap(us))
99333842cedSCho, Yu-Chen 		goto exit;
99433842cedSCho, Yu-Chen 
99533842cedSCho, Yu-Chen 	/* Mark the book block */
99633842cedSCho, Yu-Chen 	ms_lib_set_bootblockmark(us, PhyBlock);
99733842cedSCho, Yu-Chen 
99833842cedSCho, Yu-Chen 	SysEntry = &(((struct ms_bootblock_page0 *)PageData)->sysent);
99933842cedSCho, Yu-Chen 
100033842cedSCho, Yu-Chen 	for (i = 0; i < MS_NUMBER_OF_SYSTEM_ENTRY; i++) {
100133842cedSCho, Yu-Chen 		u32  EntryOffset, EntrySize;
100233842cedSCho, Yu-Chen 
100333842cedSCho, Yu-Chen 		EntryOffset = be32_to_cpu(SysEntry->entry[i].dwStart);
100433842cedSCho, Yu-Chen 
100533842cedSCho, Yu-Chen 		if (EntryOffset == 0xffffff)
100633842cedSCho, Yu-Chen 			continue;
100733842cedSCho, Yu-Chen 		EntrySize = be32_to_cpu(SysEntry->entry[i].dwSize);
100833842cedSCho, Yu-Chen 
100933842cedSCho, Yu-Chen 		if (EntrySize == 0)
101033842cedSCho, Yu-Chen 			continue;
101133842cedSCho, Yu-Chen 
101233842cedSCho, Yu-Chen 		if (EntryOffset + MS_BYTES_PER_PAGE + EntrySize > info->MS_Lib.blockSize * (u32)SIZE_OF_KIRO)
101333842cedSCho, Yu-Chen 			continue;
101433842cedSCho, Yu-Chen 
101533842cedSCho, Yu-Chen 		if (i == 0) {
101633842cedSCho, Yu-Chen 			u8 PrevPageNumber = 0;
101733842cedSCho, Yu-Chen 			u16 phyblk;
101833842cedSCho, Yu-Chen 
101933842cedSCho, Yu-Chen 			if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_INVALID_BLOCK)
102033842cedSCho, Yu-Chen 				goto exit;
102133842cedSCho, Yu-Chen 
102233842cedSCho, Yu-Chen 			while (EntrySize > 0) {
102333842cedSCho, Yu-Chen 
102433842cedSCho, Yu-Chen 				PageNumber = (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1);
102533842cedSCho, Yu-Chen 				if (PageNumber != PrevPageNumber) {
102633842cedSCho, Yu-Chen 					switch (ms_read_readpage(us, PhyBlock, PageNumber, (u32 *)PageBuffer, &ExtraData)) {
102733842cedSCho, Yu-Chen 					case MS_STATUS_SUCCESS:
102833842cedSCho, Yu-Chen 						break;
102933842cedSCho, Yu-Chen 					case MS_STATUS_WRITE_PROTECT:
103033842cedSCho, Yu-Chen 					case MS_ERROR_FLASH_READ:
103133842cedSCho, Yu-Chen 					case MS_STATUS_ERROR:
103233842cedSCho, Yu-Chen 					default:
103333842cedSCho, Yu-Chen 						goto exit;
103433842cedSCho, Yu-Chen 					}
103533842cedSCho, Yu-Chen 
103633842cedSCho, Yu-Chen 					PrevPageNumber = PageNumber;
103733842cedSCho, Yu-Chen 				}
103833842cedSCho, Yu-Chen 
103933842cedSCho, Yu-Chen 				phyblk = be16_to_cpu(*(u16 *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)));
104033842cedSCho, Yu-Chen 				if (phyblk < 0x0fff)
104133842cedSCho, Yu-Chen 					ms_lib_set_initialerrorblock(us, phyblk);
104233842cedSCho, Yu-Chen 
104333842cedSCho, Yu-Chen 				EntryOffset += 2;
104433842cedSCho, Yu-Chen 				EntrySize -= 2;
104533842cedSCho, Yu-Chen 			}
104633842cedSCho, Yu-Chen 		} else if (i == 1) {  /* CIS/IDI */
104733842cedSCho, Yu-Chen 			struct ms_bootblock_idi *idi;
104833842cedSCho, Yu-Chen 
104933842cedSCho, Yu-Chen 			if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_CIS_IDI)
105033842cedSCho, Yu-Chen 				goto exit;
105133842cedSCho, Yu-Chen 
105233842cedSCho, Yu-Chen 			switch (ms_read_readpage(us, PhyBlock, (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1), (u32 *)PageBuffer, &ExtraData)) {
105333842cedSCho, Yu-Chen 			case MS_STATUS_SUCCESS:
105433842cedSCho, Yu-Chen 				break;
105533842cedSCho, Yu-Chen 			case MS_STATUS_WRITE_PROTECT:
105633842cedSCho, Yu-Chen 			case MS_ERROR_FLASH_READ:
105733842cedSCho, Yu-Chen 			case MS_STATUS_ERROR:
105833842cedSCho, Yu-Chen 			default:
105933842cedSCho, Yu-Chen 				goto exit;
106033842cedSCho, Yu-Chen 			}
106133842cedSCho, Yu-Chen 
106233842cedSCho, Yu-Chen 			idi = &((struct ms_bootblock_cis_idi *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)))->idi.idi;
106333842cedSCho, Yu-Chen 			if (le16_to_cpu(idi->wIDIgeneralConfiguration) != MS_IDI_GENERAL_CONF)
106433842cedSCho, Yu-Chen 				goto exit;
106533842cedSCho, Yu-Chen 
106633842cedSCho, Yu-Chen 			info->MS_Lib.BytesPerSector = le16_to_cpu(idi->wIDIbytesPerSector);
106733842cedSCho, Yu-Chen 			if (info->MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE)
106833842cedSCho, Yu-Chen 				goto exit;
106933842cedSCho, Yu-Chen 		}
107033842cedSCho, Yu-Chen 	} /* End for .. */
107133842cedSCho, Yu-Chen 
107233842cedSCho, Yu-Chen 	result = 0;
107333842cedSCho, Yu-Chen 
107433842cedSCho, Yu-Chen exit:
107533842cedSCho, Yu-Chen 	if (result)
107633842cedSCho, Yu-Chen 		ms_lib_free_logicalmap(us);
107733842cedSCho, Yu-Chen 
107833842cedSCho, Yu-Chen 	kfree(PageBuffer);
107933842cedSCho, Yu-Chen 
108033842cedSCho, Yu-Chen 	result = 0;
108133842cedSCho, Yu-Chen 	return result;
108233842cedSCho, Yu-Chen }
108333842cedSCho, Yu-Chen 
108433842cedSCho, Yu-Chen static void ms_lib_free_writebuf(struct us_data *us)
108533842cedSCho, Yu-Chen {
108633842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
108733842cedSCho, Yu-Chen 	info->MS_Lib.wrtblk = (u16)-1; /* set to -1 */
108833842cedSCho, Yu-Chen 
108933842cedSCho, Yu-Chen 	/* memset((fdoExt)->MS_Lib.pagemap, 0, sizeof((fdoExt)->MS_Lib.pagemap)) */
109033842cedSCho, Yu-Chen 
109133842cedSCho, Yu-Chen 	ms_lib_clear_pagemap(info); /* (pdx)->MS_Lib.pagemap memset 0 in ms.h */
109233842cedSCho, Yu-Chen 
109333842cedSCho, Yu-Chen 	if (info->MS_Lib.blkpag) {
1094e6533e87SAmitoj Kaur Chawla 		kfree(info->MS_Lib.blkpag);  /* Arnold test ... */
109533842cedSCho, Yu-Chen 		info->MS_Lib.blkpag = NULL;
109633842cedSCho, Yu-Chen 	}
109733842cedSCho, Yu-Chen 
109833842cedSCho, Yu-Chen 	if (info->MS_Lib.blkext) {
1099e6533e87SAmitoj Kaur Chawla 		kfree(info->MS_Lib.blkext);  /* Arnold test ... */
110033842cedSCho, Yu-Chen 		info->MS_Lib.blkext = NULL;
110133842cedSCho, Yu-Chen 	}
110233842cedSCho, Yu-Chen }
110333842cedSCho, Yu-Chen 
110433842cedSCho, Yu-Chen 
110533842cedSCho, Yu-Chen static void ms_lib_free_allocatedarea(struct us_data *us)
110633842cedSCho, Yu-Chen {
110733842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
110833842cedSCho, Yu-Chen 
110933842cedSCho, Yu-Chen 	ms_lib_free_writebuf(us); /* Free MS_Lib.pagemap */
111033842cedSCho, Yu-Chen 	ms_lib_free_logicalmap(us); /* kfree MS_Lib.Phy2LogMap and MS_Lib.Log2PhyMap */
111133842cedSCho, Yu-Chen 
111233842cedSCho, Yu-Chen 	/* set struct us point flag to 0 */
111333842cedSCho, Yu-Chen 	info->MS_Lib.flags = 0;
111433842cedSCho, Yu-Chen 	info->MS_Lib.BytesPerSector = 0;
111533842cedSCho, Yu-Chen 	info->MS_Lib.SectorsPerCylinder = 0;
111633842cedSCho, Yu-Chen 
111733842cedSCho, Yu-Chen 	info->MS_Lib.cardType = 0;
111833842cedSCho, Yu-Chen 	info->MS_Lib.blockSize = 0;
111933842cedSCho, Yu-Chen 	info->MS_Lib.PagesPerBlock = 0;
112033842cedSCho, Yu-Chen 
112133842cedSCho, Yu-Chen 	info->MS_Lib.NumberOfPhyBlock = 0;
112233842cedSCho, Yu-Chen 	info->MS_Lib.NumberOfLogBlock = 0;
112333842cedSCho, Yu-Chen }
112433842cedSCho, Yu-Chen 
112533842cedSCho, Yu-Chen 
112633842cedSCho, Yu-Chen static int ms_lib_alloc_writebuf(struct us_data *us)
112733842cedSCho, Yu-Chen {
112833842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
112933842cedSCho, Yu-Chen 
113033842cedSCho, Yu-Chen 	info->MS_Lib.wrtblk = (u16)-1;
113133842cedSCho, Yu-Chen 
113233842cedSCho, Yu-Chen 	info->MS_Lib.blkpag = kmalloc(info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector, GFP_KERNEL);
113333842cedSCho, Yu-Chen 	info->MS_Lib.blkext = kmalloc(info->MS_Lib.PagesPerBlock * sizeof(struct ms_lib_type_extdat), GFP_KERNEL);
113433842cedSCho, Yu-Chen 
113533842cedSCho, Yu-Chen 	if ((info->MS_Lib.blkpag == NULL) || (info->MS_Lib.blkext == NULL)) {
113633842cedSCho, Yu-Chen 		ms_lib_free_writebuf(us);
113733842cedSCho, Yu-Chen 		return (u32)-1;
113833842cedSCho, Yu-Chen 	}
113933842cedSCho, Yu-Chen 
114033842cedSCho, Yu-Chen 	ms_lib_clear_writebuf(us);
114133842cedSCho, Yu-Chen 
114233842cedSCho, Yu-Chen return 0;
114333842cedSCho, Yu-Chen }
114433842cedSCho, Yu-Chen 
114533842cedSCho, Yu-Chen static int ms_lib_force_setlogical_pair(struct us_data *us, u16 logblk, u16 phyblk)
114633842cedSCho, Yu-Chen {
114733842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
114833842cedSCho, Yu-Chen 
114933842cedSCho, Yu-Chen 	if (logblk == MS_LB_NOT_USED)
115033842cedSCho, Yu-Chen 		return 0;
115133842cedSCho, Yu-Chen 
115233842cedSCho, Yu-Chen 	if ((logblk >= info->MS_Lib.NumberOfLogBlock) ||
115333842cedSCho, Yu-Chen 		(phyblk >= info->MS_Lib.NumberOfPhyBlock))
115433842cedSCho, Yu-Chen 		return (u32)-1;
115533842cedSCho, Yu-Chen 
115633842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap[phyblk] = logblk;
115733842cedSCho, Yu-Chen 	info->MS_Lib.Log2PhyMap[logblk] = phyblk;
115833842cedSCho, Yu-Chen 
115933842cedSCho, Yu-Chen 	return 0;
116033842cedSCho, Yu-Chen }
116133842cedSCho, Yu-Chen 
116233842cedSCho, Yu-Chen static int ms_read_copyblock(struct us_data *us, u16 oldphy, u16 newphy,
116333842cedSCho, Yu-Chen 			u16 PhyBlockAddr, u8 PageNum, unsigned char *buf, u16 len)
116433842cedSCho, Yu-Chen {
116533842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
116633842cedSCho, Yu-Chen 	int result;
116733842cedSCho, Yu-Chen 
116833842cedSCho, Yu-Chen 	result = ene_load_bincode(us, MS_RW_PATTERN);
116933842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
117033842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
117133842cedSCho, Yu-Chen 
117233842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
117333842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
117433842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x200*len;
117533842cedSCho, Yu-Chen 	bcb->Flags = 0x00;
117633842cedSCho, Yu-Chen 	bcb->CDB[0] = 0xF0;
117733842cedSCho, Yu-Chen 	bcb->CDB[1] = 0x08;
117833842cedSCho, Yu-Chen 	bcb->CDB[4] = (unsigned char)(oldphy);
117933842cedSCho, Yu-Chen 	bcb->CDB[3] = (unsigned char)(oldphy>>8);
118033842cedSCho, Yu-Chen 	bcb->CDB[2] = 0; /* (BYTE)(oldphy>>16) */
118133842cedSCho, Yu-Chen 	bcb->CDB[7] = (unsigned char)(newphy);
118233842cedSCho, Yu-Chen 	bcb->CDB[6] = (unsigned char)(newphy>>8);
118333842cedSCho, Yu-Chen 	bcb->CDB[5] = 0; /* (BYTE)(newphy>>16) */
118433842cedSCho, Yu-Chen 	bcb->CDB[9] = (unsigned char)(PhyBlockAddr);
118533842cedSCho, Yu-Chen 	bcb->CDB[8] = (unsigned char)(PhyBlockAddr>>8);
118633842cedSCho, Yu-Chen 	bcb->CDB[10] = PageNum;
118733842cedSCho, Yu-Chen 
118833842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
118933842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
119033842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
119133842cedSCho, Yu-Chen 
119233842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
119333842cedSCho, Yu-Chen }
119433842cedSCho, Yu-Chen 
119533842cedSCho, Yu-Chen static int ms_read_eraseblock(struct us_data *us, u32 PhyBlockAddr)
119633842cedSCho, Yu-Chen {
119733842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
119833842cedSCho, Yu-Chen 	int result;
119933842cedSCho, Yu-Chen 	u32 bn = PhyBlockAddr;
120033842cedSCho, Yu-Chen 
120133842cedSCho, Yu-Chen 	result = ene_load_bincode(us, MS_RW_PATTERN);
120233842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
120333842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
120433842cedSCho, Yu-Chen 
120533842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
120633842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
120733842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x200;
1208b8db6d64SSebastian Andrzej Siewior 	bcb->Flags = US_BULK_FLAG_IN;
120933842cedSCho, Yu-Chen 	bcb->CDB[0] = 0xF2;
121033842cedSCho, Yu-Chen 	bcb->CDB[1] = 0x06;
121133842cedSCho, Yu-Chen 	bcb->CDB[4] = (unsigned char)(bn);
121233842cedSCho, Yu-Chen 	bcb->CDB[3] = (unsigned char)(bn>>8);
121333842cedSCho, Yu-Chen 	bcb->CDB[2] = (unsigned char)(bn>>16);
121433842cedSCho, Yu-Chen 
121533842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
121633842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
121733842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
121833842cedSCho, Yu-Chen 
121933842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
122033842cedSCho, Yu-Chen }
122133842cedSCho, Yu-Chen 
122233842cedSCho, Yu-Chen static int ms_lib_check_disableblock(struct us_data *us, u16 PhyBlock)
122333842cedSCho, Yu-Chen {
122433842cedSCho, Yu-Chen 	unsigned char *PageBuf = NULL;
122533842cedSCho, Yu-Chen 	u16 result = MS_STATUS_SUCCESS;
122633842cedSCho, Yu-Chen 	u16 blk, index = 0;
122733842cedSCho, Yu-Chen 	struct ms_lib_type_extdat extdat;
122833842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
122933842cedSCho, Yu-Chen 
123033842cedSCho, Yu-Chen 	PageBuf = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
123133842cedSCho, Yu-Chen 	if (PageBuf == NULL) {
123233842cedSCho, Yu-Chen 		result = MS_NO_MEMORY_ERROR;
123333842cedSCho, Yu-Chen 		goto exit;
123433842cedSCho, Yu-Chen 	}
123533842cedSCho, Yu-Chen 
123633842cedSCho, Yu-Chen 	ms_read_readpage(us, PhyBlock, 1, (u32 *)PageBuf, &extdat);
123733842cedSCho, Yu-Chen 	do {
123833842cedSCho, Yu-Chen 		blk = be16_to_cpu(PageBuf[index]);
123933842cedSCho, Yu-Chen 		if (blk == MS_LB_NOT_USED)
124033842cedSCho, Yu-Chen 			break;
124133842cedSCho, Yu-Chen 		if (blk == info->MS_Lib.Log2PhyMap[0]) {
124233842cedSCho, Yu-Chen 			result = MS_ERROR_FLASH_READ;
124333842cedSCho, Yu-Chen 			break;
124433842cedSCho, Yu-Chen 		}
124533842cedSCho, Yu-Chen 		index++;
124633842cedSCho, Yu-Chen 	} while (1);
124733842cedSCho, Yu-Chen 
124833842cedSCho, Yu-Chen exit:
124933842cedSCho, Yu-Chen 	kfree(PageBuf);
125033842cedSCho, Yu-Chen 	return result;
125133842cedSCho, Yu-Chen }
125233842cedSCho, Yu-Chen 
125333842cedSCho, Yu-Chen static int ms_lib_setacquired_errorblock(struct us_data *us, u16 phyblk)
125433842cedSCho, Yu-Chen {
125533842cedSCho, Yu-Chen 	u16 log;
125633842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
125733842cedSCho, Yu-Chen 
125833842cedSCho, Yu-Chen 	if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
125933842cedSCho, Yu-Chen 		return (u32)-1;
126033842cedSCho, Yu-Chen 
126133842cedSCho, Yu-Chen 	log = info->MS_Lib.Phy2LogMap[phyblk];
126233842cedSCho, Yu-Chen 
126333842cedSCho, Yu-Chen 	if (log < info->MS_Lib.NumberOfLogBlock)
126433842cedSCho, Yu-Chen 		info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
126533842cedSCho, Yu-Chen 
126633842cedSCho, Yu-Chen 	if (info->MS_Lib.Phy2LogMap[phyblk] != MS_LB_INITIAL_ERROR)
126733842cedSCho, Yu-Chen 		info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_ACQUIRED_ERROR;
126833842cedSCho, Yu-Chen 
126933842cedSCho, Yu-Chen 	return 0;
127033842cedSCho, Yu-Chen }
127133842cedSCho, Yu-Chen 
127233842cedSCho, Yu-Chen static int ms_lib_overwrite_extra(struct us_data *us, u32 PhyBlockAddr,
127333842cedSCho, Yu-Chen 				u8 PageNum, u8 OverwriteFlag)
127433842cedSCho, Yu-Chen {
127533842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
127633842cedSCho, Yu-Chen 	int result;
127733842cedSCho, Yu-Chen 
127833842cedSCho, Yu-Chen 	result = ene_load_bincode(us, MS_RW_PATTERN);
127933842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
128033842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
128133842cedSCho, Yu-Chen 
128233842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
128333842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
128433842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x4;
1285b8db6d64SSebastian Andrzej Siewior 	bcb->Flags = US_BULK_FLAG_IN;
128633842cedSCho, Yu-Chen 	bcb->CDB[0] = 0xF2;
128733842cedSCho, Yu-Chen 	bcb->CDB[1] = 0x05;
128833842cedSCho, Yu-Chen 	bcb->CDB[5] = (unsigned char)(PageNum);
128933842cedSCho, Yu-Chen 	bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
129033842cedSCho, Yu-Chen 	bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
129133842cedSCho, Yu-Chen 	bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
129233842cedSCho, Yu-Chen 	bcb->CDB[6] = OverwriteFlag;
129333842cedSCho, Yu-Chen 	bcb->CDB[7] = 0xFF;
129433842cedSCho, Yu-Chen 	bcb->CDB[8] = 0xFF;
129533842cedSCho, Yu-Chen 	bcb->CDB[9] = 0xFF;
129633842cedSCho, Yu-Chen 
129733842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
129833842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
129933842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
130033842cedSCho, Yu-Chen 
130133842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
130233842cedSCho, Yu-Chen }
130333842cedSCho, Yu-Chen 
130433842cedSCho, Yu-Chen static int ms_lib_error_phyblock(struct us_data *us, u16 phyblk)
130533842cedSCho, Yu-Chen {
130633842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
130733842cedSCho, Yu-Chen 
130833842cedSCho, Yu-Chen 	if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
130933842cedSCho, Yu-Chen 		return MS_STATUS_ERROR;
131033842cedSCho, Yu-Chen 
131133842cedSCho, Yu-Chen 	ms_lib_setacquired_errorblock(us, phyblk);
131233842cedSCho, Yu-Chen 
131333842cedSCho, Yu-Chen 	if (ms_lib_iswritable(info))
131433842cedSCho, Yu-Chen 		return ms_lib_overwrite_extra(us, phyblk, 0, (u8)(~MS_REG_OVR_BKST & BYTE_MASK));
131533842cedSCho, Yu-Chen 
131633842cedSCho, Yu-Chen 	return MS_STATUS_SUCCESS;
131733842cedSCho, Yu-Chen }
131833842cedSCho, Yu-Chen 
131933842cedSCho, Yu-Chen static int ms_lib_erase_phyblock(struct us_data *us, u16 phyblk)
132033842cedSCho, Yu-Chen {
132133842cedSCho, Yu-Chen 	u16 log;
132233842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
132333842cedSCho, Yu-Chen 
132433842cedSCho, Yu-Chen 	if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
132533842cedSCho, Yu-Chen 		return MS_STATUS_ERROR;
132633842cedSCho, Yu-Chen 
132733842cedSCho, Yu-Chen 	log = info->MS_Lib.Phy2LogMap[phyblk];
132833842cedSCho, Yu-Chen 
132933842cedSCho, Yu-Chen 	if (log < info->MS_Lib.NumberOfLogBlock)
133033842cedSCho, Yu-Chen 		info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
133133842cedSCho, Yu-Chen 
133233842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED;
133333842cedSCho, Yu-Chen 
133433842cedSCho, Yu-Chen 	if (ms_lib_iswritable(info)) {
133533842cedSCho, Yu-Chen 		switch (ms_read_eraseblock(us, phyblk)) {
133633842cedSCho, Yu-Chen 		case MS_STATUS_SUCCESS:
133733842cedSCho, Yu-Chen 			info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED_ERASED;
133833842cedSCho, Yu-Chen 			return MS_STATUS_SUCCESS;
133933842cedSCho, Yu-Chen 		case MS_ERROR_FLASH_ERASE:
134033842cedSCho, Yu-Chen 		case MS_STATUS_INT_ERROR:
134133842cedSCho, Yu-Chen 			ms_lib_error_phyblock(us, phyblk);
134233842cedSCho, Yu-Chen 			return MS_ERROR_FLASH_ERASE;
134333842cedSCho, Yu-Chen 		case MS_STATUS_ERROR:
134433842cedSCho, Yu-Chen 		default:
134533842cedSCho, 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*/
134633842cedSCho, Yu-Chen 			ms_lib_setacquired_errorblock(us, phyblk);
134733842cedSCho, Yu-Chen 			return MS_STATUS_ERROR;
134833842cedSCho, Yu-Chen 		}
134933842cedSCho, Yu-Chen 	}
135033842cedSCho, Yu-Chen 
135133842cedSCho, Yu-Chen 	ms_lib_setacquired_errorblock(us, phyblk);
135233842cedSCho, Yu-Chen 
135333842cedSCho, Yu-Chen 	return MS_STATUS_SUCCESS;
135433842cedSCho, Yu-Chen }
135533842cedSCho, Yu-Chen 
135633842cedSCho, Yu-Chen static int ms_lib_read_extra(struct us_data *us, u32 PhyBlock,
135733842cedSCho, Yu-Chen 				u8 PageNum, struct ms_lib_type_extdat *ExtraDat)
135833842cedSCho, Yu-Chen {
135933842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1360628c2893SAlan Stern 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1361628c2893SAlan Stern 	u8 *bbuf = info->bbuf;
136233842cedSCho, Yu-Chen 	int result;
136333842cedSCho, Yu-Chen 
136433842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
136533842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
136633842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x4;
1367b8db6d64SSebastian Andrzej Siewior 	bcb->Flags      = US_BULK_FLAG_IN;
136833842cedSCho, Yu-Chen 	bcb->CDB[0]     = 0xF1;
136933842cedSCho, Yu-Chen 	bcb->CDB[1]     = 0x03;
137033842cedSCho, Yu-Chen 	bcb->CDB[5]     = (unsigned char)(PageNum);
137133842cedSCho, Yu-Chen 	bcb->CDB[4]     = (unsigned char)(PhyBlock);
137233842cedSCho, Yu-Chen 	bcb->CDB[3]     = (unsigned char)(PhyBlock>>8);
137333842cedSCho, Yu-Chen 	bcb->CDB[2]     = (unsigned char)(PhyBlock>>16);
137433842cedSCho, Yu-Chen 	bcb->CDB[6]     = 0x01;
137533842cedSCho, Yu-Chen 
1376628c2893SAlan Stern 	result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
137733842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
137833842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
137933842cedSCho, Yu-Chen 
138033842cedSCho, Yu-Chen 	ExtraDat->reserved = 0;
138133842cedSCho, Yu-Chen 	ExtraDat->intr     = 0x80;  /* Not yet, waiting for fireware support */
138233842cedSCho, Yu-Chen 	ExtraDat->status0  = 0x10;  /* Not yet, waiting for fireware support */
138333842cedSCho, Yu-Chen 	ExtraDat->status1  = 0x00;  /* Not yet, waiting for fireware support */
1384628c2893SAlan Stern 	ExtraDat->ovrflg   = bbuf[0];
1385628c2893SAlan Stern 	ExtraDat->mngflg   = bbuf[1];
1386628c2893SAlan Stern 	ExtraDat->logadr   = memstick_logaddr(bbuf[2], bbuf[3]);
138733842cedSCho, Yu-Chen 
138833842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
138933842cedSCho, Yu-Chen }
139033842cedSCho, Yu-Chen 
139133842cedSCho, Yu-Chen static int ms_libsearch_block_from_physical(struct us_data *us, u16 phyblk)
139233842cedSCho, Yu-Chen {
139333842cedSCho, Yu-Chen 	u16 blk;
139433842cedSCho, Yu-Chen 	struct ms_lib_type_extdat extdat; /* need check */
139533842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
139633842cedSCho, Yu-Chen 
139733842cedSCho, Yu-Chen 
139833842cedSCho, Yu-Chen 	if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
139933842cedSCho, Yu-Chen 		return MS_LB_ERROR;
140033842cedSCho, Yu-Chen 
140133842cedSCho, Yu-Chen 	for (blk = phyblk + 1; blk != phyblk; blk++) {
140233842cedSCho, Yu-Chen 		if ((blk & MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK) == 0)
140333842cedSCho, Yu-Chen 			blk -= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
140433842cedSCho, Yu-Chen 
140533842cedSCho, Yu-Chen 		if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED_ERASED) {
140633842cedSCho, Yu-Chen 			return blk;
140733842cedSCho, Yu-Chen 		} else if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED) {
140833842cedSCho, Yu-Chen 			switch (ms_lib_read_extra(us, blk, 0, &extdat)) {
140933842cedSCho, Yu-Chen 			case MS_STATUS_SUCCESS:
141033842cedSCho, Yu-Chen 			case MS_STATUS_SUCCESS_WITH_ECC:
141133842cedSCho, Yu-Chen 				break;
141233842cedSCho, Yu-Chen 			case MS_NOCARD_ERROR:
141333842cedSCho, Yu-Chen 				return MS_NOCARD_ERROR;
141433842cedSCho, Yu-Chen 			case MS_STATUS_INT_ERROR:
141533842cedSCho, Yu-Chen 				return MS_LB_ERROR;
141633842cedSCho, Yu-Chen 			case MS_ERROR_FLASH_READ:
141733842cedSCho, Yu-Chen 			default:
141833842cedSCho, Yu-Chen 				ms_lib_setacquired_errorblock(us, blk);
141933842cedSCho, Yu-Chen 				continue;
142033842cedSCho, Yu-Chen 			} /* End switch */
142133842cedSCho, Yu-Chen 
142233842cedSCho, Yu-Chen 			if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
142333842cedSCho, Yu-Chen 				ms_lib_setacquired_errorblock(us, blk);
142433842cedSCho, Yu-Chen 				continue;
142533842cedSCho, Yu-Chen 			}
142633842cedSCho, Yu-Chen 
142733842cedSCho, Yu-Chen 			switch (ms_lib_erase_phyblock(us, blk)) {
142833842cedSCho, Yu-Chen 			case MS_STATUS_SUCCESS:
142933842cedSCho, Yu-Chen 				return blk;
143033842cedSCho, Yu-Chen 			case MS_STATUS_ERROR:
143133842cedSCho, Yu-Chen 				return MS_LB_ERROR;
143233842cedSCho, Yu-Chen 			case MS_ERROR_FLASH_ERASE:
143333842cedSCho, Yu-Chen 			default:
143433842cedSCho, Yu-Chen 				ms_lib_error_phyblock(us, blk);
143533842cedSCho, Yu-Chen 				break;
143633842cedSCho, Yu-Chen 			}
143733842cedSCho, Yu-Chen 		}
143833842cedSCho, Yu-Chen 	} /* End for */
143933842cedSCho, Yu-Chen 
144033842cedSCho, Yu-Chen 	return MS_LB_ERROR;
144133842cedSCho, Yu-Chen }
144233842cedSCho, Yu-Chen static int ms_libsearch_block_from_logical(struct us_data *us, u16 logblk)
144333842cedSCho, Yu-Chen {
144433842cedSCho, Yu-Chen 	u16 phyblk;
144533842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
144633842cedSCho, Yu-Chen 
144733842cedSCho, Yu-Chen 	phyblk = ms_libconv_to_physical(info, logblk);
144833842cedSCho, Yu-Chen 	if (phyblk >= MS_LB_ERROR) {
144933842cedSCho, Yu-Chen 		if (logblk >= info->MS_Lib.NumberOfLogBlock)
145033842cedSCho, Yu-Chen 			return MS_LB_ERROR;
145133842cedSCho, Yu-Chen 
145233842cedSCho, Yu-Chen 		phyblk = (logblk + MS_NUMBER_OF_BOOT_BLOCK) / MS_LOGICAL_BLOCKS_PER_SEGMENT;
145333842cedSCho, Yu-Chen 		phyblk *= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
145433842cedSCho, Yu-Chen 		phyblk += MS_PHYSICAL_BLOCKS_PER_SEGMENT - 1;
145533842cedSCho, Yu-Chen 	}
145633842cedSCho, Yu-Chen 
145733842cedSCho, Yu-Chen 	return ms_libsearch_block_from_physical(us, phyblk);
145833842cedSCho, Yu-Chen }
145933842cedSCho, Yu-Chen 
146033842cedSCho, Yu-Chen static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
146133842cedSCho, Yu-Chen {
146233842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
146333842cedSCho, Yu-Chen 
146433842cedSCho, Yu-Chen 	/* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
146533842cedSCho, Yu-Chen 	if (info->MS_Status.Insert && info->MS_Status.Ready) {
146633842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_GOOD;
146733842cedSCho, Yu-Chen 	} else {
146833842cedSCho, Yu-Chen 		ene_ms_init(us);
146933842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_GOOD;
147033842cedSCho, Yu-Chen 	}
147133842cedSCho, Yu-Chen 
147233842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
147333842cedSCho, Yu-Chen }
147433842cedSCho, Yu-Chen 
147533842cedSCho, Yu-Chen static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
147633842cedSCho, Yu-Chen {
147733842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
147833842cedSCho, Yu-Chen 	unsigned char mediaNoWP[12] = {
147933842cedSCho, Yu-Chen 		0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
148033842cedSCho, Yu-Chen 		0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
148133842cedSCho, Yu-Chen 	unsigned char mediaWP[12]   = {
148233842cedSCho, Yu-Chen 		0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
148333842cedSCho, Yu-Chen 		0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
148433842cedSCho, Yu-Chen 
148533842cedSCho, Yu-Chen 	if (info->MS_Status.WtP)
148633842cedSCho, Yu-Chen 		usb_stor_set_xfer_buf(mediaWP, 12, srb);
148733842cedSCho, Yu-Chen 	else
148833842cedSCho, Yu-Chen 		usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
148933842cedSCho, Yu-Chen 
149033842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
149133842cedSCho, Yu-Chen }
149233842cedSCho, Yu-Chen 
149333842cedSCho, Yu-Chen static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
149433842cedSCho, Yu-Chen {
149533842cedSCho, Yu-Chen 	u32   bl_num;
149633842cedSCho, Yu-Chen 	u16    bl_len;
149733842cedSCho, Yu-Chen 	unsigned int offset = 0;
149833842cedSCho, Yu-Chen 	unsigned char    buf[8];
149933842cedSCho, Yu-Chen 	struct scatterlist *sg = NULL;
150033842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
150133842cedSCho, Yu-Chen 
1502191648d0SJoe Perches 	usb_stor_dbg(us, "ms_scsi_read_capacity\n");
150333842cedSCho, Yu-Chen 	bl_len = 0x200;
150433842cedSCho, Yu-Chen 	if (info->MS_Status.IsMSPro)
150533842cedSCho, Yu-Chen 		bl_num = info->MSP_TotalBlock - 1;
150633842cedSCho, Yu-Chen 	else
150733842cedSCho, Yu-Chen 		bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1;
150833842cedSCho, Yu-Chen 
150933842cedSCho, Yu-Chen 	info->bl_num = bl_num;
1510191648d0SJoe Perches 	usb_stor_dbg(us, "bl_len = %x\n", bl_len);
1511191648d0SJoe Perches 	usb_stor_dbg(us, "bl_num = %x\n", bl_num);
151233842cedSCho, Yu-Chen 
151333842cedSCho, Yu-Chen 	/*srb->request_bufflen = 8; */
151433842cedSCho, Yu-Chen 	buf[0] = (bl_num >> 24) & 0xff;
151533842cedSCho, Yu-Chen 	buf[1] = (bl_num >> 16) & 0xff;
151633842cedSCho, Yu-Chen 	buf[2] = (bl_num >> 8) & 0xff;
151733842cedSCho, Yu-Chen 	buf[3] = (bl_num >> 0) & 0xff;
151833842cedSCho, Yu-Chen 	buf[4] = (bl_len >> 24) & 0xff;
151933842cedSCho, Yu-Chen 	buf[5] = (bl_len >> 16) & 0xff;
152033842cedSCho, Yu-Chen 	buf[6] = (bl_len >> 8) & 0xff;
152133842cedSCho, Yu-Chen 	buf[7] = (bl_len >> 0) & 0xff;
152233842cedSCho, Yu-Chen 
152333842cedSCho, Yu-Chen 	usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
152433842cedSCho, Yu-Chen 
152533842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
152633842cedSCho, Yu-Chen }
152733842cedSCho, Yu-Chen 
152833842cedSCho, Yu-Chen static void ms_lib_phy_to_log_range(u16 PhyBlock, u16 *LogStart, u16 *LogEnde)
152933842cedSCho, Yu-Chen {
153033842cedSCho, Yu-Chen 	PhyBlock /= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
153133842cedSCho, Yu-Chen 
153233842cedSCho, Yu-Chen 	if (PhyBlock) {
153333842cedSCho, Yu-Chen 		*LogStart = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT + (PhyBlock - 1) * MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
153433842cedSCho, Yu-Chen 		*LogEnde = *LogStart + MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
153533842cedSCho, Yu-Chen 	} else {
153633842cedSCho, Yu-Chen 		*LogStart = 0;
153733842cedSCho, Yu-Chen 		*LogEnde = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT;/*494*/
153833842cedSCho, Yu-Chen 	}
153933842cedSCho, Yu-Chen }
154033842cedSCho, Yu-Chen 
154133842cedSCho, Yu-Chen static int ms_lib_read_extrablock(struct us_data *us, u32 PhyBlock,
154233842cedSCho, Yu-Chen 	u8 PageNum, u8 blen, void *buf)
154333842cedSCho, Yu-Chen {
154433842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
154533842cedSCho, Yu-Chen 	int     result;
154633842cedSCho, Yu-Chen 
154733842cedSCho, Yu-Chen 	/* Read Extra Data */
154833842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
154933842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
155033842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x4 * blen;
1551b8db6d64SSebastian Andrzej Siewior 	bcb->Flags      = US_BULK_FLAG_IN;
155233842cedSCho, Yu-Chen 	bcb->CDB[0]     = 0xF1;
155333842cedSCho, Yu-Chen 	bcb->CDB[1]     = 0x03;
155433842cedSCho, Yu-Chen 	bcb->CDB[5]     = (unsigned char)(PageNum);
155533842cedSCho, Yu-Chen 	bcb->CDB[4]     = (unsigned char)(PhyBlock);
155633842cedSCho, Yu-Chen 	bcb->CDB[3]     = (unsigned char)(PhyBlock>>8);
155733842cedSCho, Yu-Chen 	bcb->CDB[2]     = (unsigned char)(PhyBlock>>16);
155833842cedSCho, Yu-Chen 	bcb->CDB[6]     = blen;
155933842cedSCho, Yu-Chen 
156033842cedSCho, Yu-Chen 	result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
156133842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD)
156233842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
156333842cedSCho, Yu-Chen 
156433842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
156533842cedSCho, Yu-Chen }
156633842cedSCho, Yu-Chen 
156733842cedSCho, Yu-Chen static int ms_lib_scan_logicalblocknumber(struct us_data *us, u16 btBlk1st)
156833842cedSCho, Yu-Chen {
156933842cedSCho, Yu-Chen 	u16 PhyBlock, newblk, i;
157033842cedSCho, Yu-Chen 	u16 LogStart, LogEnde;
157133842cedSCho, Yu-Chen 	struct ms_lib_type_extdat extdat;
157233842cedSCho, Yu-Chen 	u32 count = 0, index = 0;
157333842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1574628c2893SAlan Stern 	u8 *bbuf = info->bbuf;
157533842cedSCho, Yu-Chen 
157633842cedSCho, Yu-Chen 	for (PhyBlock = 0; PhyBlock < info->MS_Lib.NumberOfPhyBlock;) {
157733842cedSCho, Yu-Chen 		ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde);
157833842cedSCho, Yu-Chen 
157933842cedSCho, Yu-Chen 		for (i = 0; i < MS_PHYSICAL_BLOCKS_PER_SEGMENT; i++, PhyBlock++) {
158033842cedSCho, Yu-Chen 			switch (ms_libconv_to_logical(info, PhyBlock)) {
158133842cedSCho, Yu-Chen 			case MS_STATUS_ERROR:
158233842cedSCho, Yu-Chen 				continue;
158333842cedSCho, Yu-Chen 			default:
158433842cedSCho, Yu-Chen 				break;
158533842cedSCho, Yu-Chen 			}
158633842cedSCho, Yu-Chen 
158733842cedSCho, Yu-Chen 			if (count == PhyBlock) {
1588628c2893SAlan Stern 				ms_lib_read_extrablock(us, PhyBlock, 0, 0x80,
1589628c2893SAlan Stern 						bbuf);
159033842cedSCho, Yu-Chen 				count += 0x80;
159133842cedSCho, Yu-Chen 			}
159233842cedSCho, Yu-Chen 			index = (PhyBlock % 0x80) * 4;
159333842cedSCho, Yu-Chen 
1594628c2893SAlan Stern 			extdat.ovrflg = bbuf[index];
1595628c2893SAlan Stern 			extdat.mngflg = bbuf[index+1];
1596628c2893SAlan Stern 			extdat.logadr = memstick_logaddr(bbuf[index+2],
1597628c2893SAlan Stern 					bbuf[index+3]);
159833842cedSCho, Yu-Chen 
159933842cedSCho, Yu-Chen 			if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
160033842cedSCho, Yu-Chen 				ms_lib_setacquired_errorblock(us, PhyBlock);
160133842cedSCho, Yu-Chen 				continue;
160233842cedSCho, Yu-Chen 			}
160333842cedSCho, Yu-Chen 
160433842cedSCho, Yu-Chen 			if ((extdat.mngflg & MS_REG_MNG_ATFLG) == MS_REG_MNG_ATFLG_ATTBL) {
160533842cedSCho, Yu-Chen 				ms_lib_erase_phyblock(us, PhyBlock);
160633842cedSCho, Yu-Chen 				continue;
160733842cedSCho, Yu-Chen 			}
160833842cedSCho, Yu-Chen 
160933842cedSCho, Yu-Chen 			if (extdat.logadr != MS_LB_NOT_USED) {
161033842cedSCho, Yu-Chen 				if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) {
161133842cedSCho, Yu-Chen 					ms_lib_erase_phyblock(us, PhyBlock);
161233842cedSCho, Yu-Chen 					continue;
161333842cedSCho, Yu-Chen 				}
161433842cedSCho, Yu-Chen 
161533842cedSCho, Yu-Chen 				newblk = ms_libconv_to_physical(info, extdat.logadr);
161633842cedSCho, Yu-Chen 
161733842cedSCho, Yu-Chen 				if (newblk != MS_LB_NOT_USED) {
161833842cedSCho, Yu-Chen 					if (extdat.logadr == 0) {
161933842cedSCho, Yu-Chen 						ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
162033842cedSCho, Yu-Chen 						if (ms_lib_check_disableblock(us, btBlk1st)) {
162133842cedSCho, Yu-Chen 							ms_lib_set_logicalpair(us, extdat.logadr, newblk);
162233842cedSCho, Yu-Chen 							continue;
162333842cedSCho, Yu-Chen 						}
162433842cedSCho, Yu-Chen 					}
162533842cedSCho, Yu-Chen 
162633842cedSCho, Yu-Chen 					ms_lib_read_extra(us, newblk, 0, &extdat);
162733842cedSCho, Yu-Chen 					if ((extdat.ovrflg & MS_REG_OVR_UDST) == MS_REG_OVR_UDST_UPDATING) {
162833842cedSCho, Yu-Chen 						ms_lib_erase_phyblock(us, PhyBlock);
162933842cedSCho, Yu-Chen 						continue;
163033842cedSCho, Yu-Chen 					} else {
163133842cedSCho, Yu-Chen 						ms_lib_erase_phyblock(us, newblk);
163233842cedSCho, Yu-Chen 					}
163333842cedSCho, Yu-Chen 				}
163433842cedSCho, Yu-Chen 
163533842cedSCho, Yu-Chen 				ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
163633842cedSCho, Yu-Chen 			}
163733842cedSCho, Yu-Chen 		}
163833842cedSCho, Yu-Chen 	} /* End for ... */
163933842cedSCho, Yu-Chen 
164033842cedSCho, Yu-Chen 	return MS_STATUS_SUCCESS;
164133842cedSCho, Yu-Chen }
164233842cedSCho, Yu-Chen 
164333842cedSCho, Yu-Chen 
164433842cedSCho, Yu-Chen static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
164533842cedSCho, Yu-Chen {
164633842cedSCho, Yu-Chen 	int result;
164733842cedSCho, Yu-Chen 	unsigned char *cdb = srb->cmnd;
164833842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
164933842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
165033842cedSCho, Yu-Chen 
165133842cedSCho, Yu-Chen 	u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
165233842cedSCho, Yu-Chen 		((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
165333842cedSCho, Yu-Chen 	u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
165433842cedSCho, Yu-Chen 	u32 blenByte = blen * 0x200;
165533842cedSCho, Yu-Chen 
165633842cedSCho, Yu-Chen 	if (bn > info->bl_num)
165733842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
165833842cedSCho, Yu-Chen 
165933842cedSCho, Yu-Chen 	if (info->MS_Status.IsMSPro) {
166033842cedSCho, Yu-Chen 		result = ene_load_bincode(us, MSP_RW_PATTERN);
166133842cedSCho, Yu-Chen 		if (result != USB_STOR_XFER_GOOD) {
1662191648d0SJoe Perches 			usb_stor_dbg(us, "Load MPS RW pattern Fail !!\n");
166333842cedSCho, Yu-Chen 			return USB_STOR_TRANSPORT_ERROR;
166433842cedSCho, Yu-Chen 		}
166533842cedSCho, Yu-Chen 
166633842cedSCho, Yu-Chen 		/* set up the command wrapper */
166733842cedSCho, Yu-Chen 		memset(bcb, 0, sizeof(struct bulk_cb_wrap));
166833842cedSCho, Yu-Chen 		bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
166933842cedSCho, Yu-Chen 		bcb->DataTransferLength = blenByte;
1670b8db6d64SSebastian Andrzej Siewior 		bcb->Flags  = US_BULK_FLAG_IN;
167133842cedSCho, Yu-Chen 		bcb->CDB[0] = 0xF1;
167233842cedSCho, Yu-Chen 		bcb->CDB[1] = 0x02;
167333842cedSCho, Yu-Chen 		bcb->CDB[5] = (unsigned char)(bn);
167433842cedSCho, Yu-Chen 		bcb->CDB[4] = (unsigned char)(bn>>8);
167533842cedSCho, Yu-Chen 		bcb->CDB[3] = (unsigned char)(bn>>16);
167633842cedSCho, Yu-Chen 		bcb->CDB[2] = (unsigned char)(bn>>24);
167733842cedSCho, Yu-Chen 
167833842cedSCho, Yu-Chen 		result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
167933842cedSCho, Yu-Chen 	} else {
168033842cedSCho, Yu-Chen 		void *buf;
168133842cedSCho, Yu-Chen 		int offset = 0;
168233842cedSCho, Yu-Chen 		u16 phyblk, logblk;
168333842cedSCho, Yu-Chen 		u8 PageNum;
168433842cedSCho, Yu-Chen 		u16 len;
168533842cedSCho, Yu-Chen 		u32 blkno;
168633842cedSCho, Yu-Chen 
168733842cedSCho, Yu-Chen 		buf = kmalloc(blenByte, GFP_KERNEL);
168833842cedSCho, Yu-Chen 		if (buf == NULL)
168933842cedSCho, Yu-Chen 			return USB_STOR_TRANSPORT_ERROR;
169033842cedSCho, Yu-Chen 
169133842cedSCho, Yu-Chen 		result = ene_load_bincode(us, MS_RW_PATTERN);
169233842cedSCho, Yu-Chen 		if (result != USB_STOR_XFER_GOOD) {
169333842cedSCho, Yu-Chen 			pr_info("Load MS RW pattern Fail !!\n");
169433842cedSCho, Yu-Chen 			result = USB_STOR_TRANSPORT_ERROR;
169533842cedSCho, Yu-Chen 			goto exit;
169633842cedSCho, Yu-Chen 		}
169733842cedSCho, Yu-Chen 
169833842cedSCho, Yu-Chen 		logblk  = (u16)(bn / info->MS_Lib.PagesPerBlock);
169933842cedSCho, Yu-Chen 		PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
170033842cedSCho, Yu-Chen 
170133842cedSCho, Yu-Chen 		while (1) {
170233842cedSCho, Yu-Chen 			if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
170333842cedSCho, Yu-Chen 				len = info->MS_Lib.PagesPerBlock-PageNum;
170433842cedSCho, Yu-Chen 			else
170533842cedSCho, Yu-Chen 				len = blen;
170633842cedSCho, Yu-Chen 
170733842cedSCho, Yu-Chen 			phyblk = ms_libconv_to_physical(info, logblk);
170833842cedSCho, Yu-Chen 			blkno  = phyblk * 0x20 + PageNum;
170933842cedSCho, Yu-Chen 
171033842cedSCho, Yu-Chen 			/* set up the command wrapper */
171133842cedSCho, Yu-Chen 			memset(bcb, 0, sizeof(struct bulk_cb_wrap));
171233842cedSCho, Yu-Chen 			bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
171333842cedSCho, Yu-Chen 			bcb->DataTransferLength = 0x200 * len;
1714b8db6d64SSebastian Andrzej Siewior 			bcb->Flags  = US_BULK_FLAG_IN;
171533842cedSCho, Yu-Chen 			bcb->CDB[0] = 0xF1;
171633842cedSCho, Yu-Chen 			bcb->CDB[1] = 0x02;
171733842cedSCho, Yu-Chen 			bcb->CDB[5] = (unsigned char)(blkno);
171833842cedSCho, Yu-Chen 			bcb->CDB[4] = (unsigned char)(blkno>>8);
171933842cedSCho, Yu-Chen 			bcb->CDB[3] = (unsigned char)(blkno>>16);
172033842cedSCho, Yu-Chen 			bcb->CDB[2] = (unsigned char)(blkno>>24);
172133842cedSCho, Yu-Chen 
172233842cedSCho, Yu-Chen 			result = ene_send_scsi_cmd(us, FDIR_READ, buf+offset, 0);
172333842cedSCho, Yu-Chen 			if (result != USB_STOR_XFER_GOOD) {
172433842cedSCho, Yu-Chen 				pr_info("MS_SCSI_Read --- result = %x\n", result);
172533842cedSCho, Yu-Chen 				result = USB_STOR_TRANSPORT_ERROR;
172633842cedSCho, Yu-Chen 				goto exit;
172733842cedSCho, Yu-Chen 			}
172833842cedSCho, Yu-Chen 
172933842cedSCho, Yu-Chen 			blen -= len;
173033842cedSCho, Yu-Chen 			if (blen <= 0)
173133842cedSCho, Yu-Chen 				break;
173233842cedSCho, Yu-Chen 			logblk++;
173333842cedSCho, Yu-Chen 			PageNum = 0;
173433842cedSCho, Yu-Chen 			offset += MS_BYTES_PER_PAGE*len;
173533842cedSCho, Yu-Chen 		}
173633842cedSCho, Yu-Chen 		usb_stor_set_xfer_buf(buf, blenByte, srb);
173733842cedSCho, Yu-Chen exit:
173833842cedSCho, Yu-Chen 		kfree(buf);
173933842cedSCho, Yu-Chen 	}
174033842cedSCho, Yu-Chen 	return result;
174133842cedSCho, Yu-Chen }
174233842cedSCho, Yu-Chen 
174333842cedSCho, Yu-Chen static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
174433842cedSCho, Yu-Chen {
174533842cedSCho, Yu-Chen 	int result;
174633842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
174733842cedSCho, Yu-Chen 	unsigned char *cdb = srb->cmnd;
174833842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
174933842cedSCho, Yu-Chen 
175033842cedSCho, Yu-Chen 	u32 bn = ((cdb[2] << 24) & 0xff000000) |
175133842cedSCho, Yu-Chen 			((cdb[3] << 16) & 0x00ff0000) |
175233842cedSCho, Yu-Chen 			((cdb[4] << 8) & 0x0000ff00) |
175333842cedSCho, Yu-Chen 			((cdb[5] << 0) & 0x000000ff);
175433842cedSCho, Yu-Chen 	u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
175533842cedSCho, Yu-Chen 	u32 blenByte = blen * 0x200;
175633842cedSCho, Yu-Chen 
175733842cedSCho, Yu-Chen 	if (bn > info->bl_num)
175833842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
175933842cedSCho, Yu-Chen 
176033842cedSCho, Yu-Chen 	if (info->MS_Status.IsMSPro) {
176133842cedSCho, Yu-Chen 		result = ene_load_bincode(us, MSP_RW_PATTERN);
176233842cedSCho, Yu-Chen 		if (result != USB_STOR_XFER_GOOD) {
176333842cedSCho, Yu-Chen 			pr_info("Load MSP RW pattern Fail !!\n");
176433842cedSCho, Yu-Chen 			return USB_STOR_TRANSPORT_ERROR;
176533842cedSCho, Yu-Chen 		}
176633842cedSCho, Yu-Chen 
176733842cedSCho, Yu-Chen 		/* set up the command wrapper */
176833842cedSCho, Yu-Chen 		memset(bcb, 0, sizeof(struct bulk_cb_wrap));
176933842cedSCho, Yu-Chen 		bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
177033842cedSCho, Yu-Chen 		bcb->DataTransferLength = blenByte;
177133842cedSCho, Yu-Chen 		bcb->Flags  = 0x00;
177233842cedSCho, Yu-Chen 		bcb->CDB[0] = 0xF0;
177333842cedSCho, Yu-Chen 		bcb->CDB[1] = 0x04;
177433842cedSCho, Yu-Chen 		bcb->CDB[5] = (unsigned char)(bn);
177533842cedSCho, Yu-Chen 		bcb->CDB[4] = (unsigned char)(bn>>8);
177633842cedSCho, Yu-Chen 		bcb->CDB[3] = (unsigned char)(bn>>16);
177733842cedSCho, Yu-Chen 		bcb->CDB[2] = (unsigned char)(bn>>24);
177833842cedSCho, Yu-Chen 
177933842cedSCho, Yu-Chen 		result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
178033842cedSCho, Yu-Chen 	} else {
178133842cedSCho, Yu-Chen 		void *buf;
1782bc985c10SFelipe Balbi 		int offset = 0;
178333842cedSCho, Yu-Chen 		u16 PhyBlockAddr;
178433842cedSCho, Yu-Chen 		u8 PageNum;
178533842cedSCho, Yu-Chen 		u16 len, oldphy, newphy;
178633842cedSCho, Yu-Chen 
178733842cedSCho, Yu-Chen 		buf = kmalloc(blenByte, GFP_KERNEL);
178833842cedSCho, Yu-Chen 		if (buf == NULL)
178933842cedSCho, Yu-Chen 			return USB_STOR_TRANSPORT_ERROR;
179033842cedSCho, Yu-Chen 		usb_stor_set_xfer_buf(buf, blenByte, srb);
179133842cedSCho, Yu-Chen 
179233842cedSCho, Yu-Chen 		result = ene_load_bincode(us, MS_RW_PATTERN);
179333842cedSCho, Yu-Chen 		if (result != USB_STOR_XFER_GOOD) {
179433842cedSCho, Yu-Chen 			pr_info("Load MS RW pattern Fail !!\n");
179533842cedSCho, Yu-Chen 			result = USB_STOR_TRANSPORT_ERROR;
179633842cedSCho, Yu-Chen 			goto exit;
179733842cedSCho, Yu-Chen 		}
179833842cedSCho, Yu-Chen 
179933842cedSCho, Yu-Chen 		PhyBlockAddr = (u16)(bn / info->MS_Lib.PagesPerBlock);
180033842cedSCho, Yu-Chen 		PageNum      = (u8)(bn % info->MS_Lib.PagesPerBlock);
180133842cedSCho, Yu-Chen 
180233842cedSCho, Yu-Chen 		while (1) {
180333842cedSCho, Yu-Chen 			if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
180433842cedSCho, Yu-Chen 				len = info->MS_Lib.PagesPerBlock-PageNum;
180533842cedSCho, Yu-Chen 			else
180633842cedSCho, Yu-Chen 				len = blen;
180733842cedSCho, Yu-Chen 
180833842cedSCho, Yu-Chen 			oldphy = ms_libconv_to_physical(info, PhyBlockAddr); /* need check us <-> info */
180933842cedSCho, Yu-Chen 			newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr);
181033842cedSCho, Yu-Chen 
181133842cedSCho, Yu-Chen 			result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len);
181233842cedSCho, Yu-Chen 
181333842cedSCho, Yu-Chen 			if (result != USB_STOR_XFER_GOOD) {
181433842cedSCho, Yu-Chen 				pr_info("MS_SCSI_Write --- result = %x\n", result);
181533842cedSCho, Yu-Chen 				result =  USB_STOR_TRANSPORT_ERROR;
181633842cedSCho, Yu-Chen 				goto exit;
181733842cedSCho, Yu-Chen 			}
181833842cedSCho, Yu-Chen 
181933842cedSCho, Yu-Chen 			info->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED;
182033842cedSCho, Yu-Chen 			ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy);
182133842cedSCho, Yu-Chen 
182233842cedSCho, Yu-Chen 			blen -= len;
182333842cedSCho, Yu-Chen 			if (blen <= 0)
182433842cedSCho, Yu-Chen 				break;
182533842cedSCho, Yu-Chen 			PhyBlockAddr++;
182633842cedSCho, Yu-Chen 			PageNum = 0;
182733842cedSCho, Yu-Chen 			offset += MS_BYTES_PER_PAGE*len;
182833842cedSCho, Yu-Chen 		}
182933842cedSCho, Yu-Chen exit:
183033842cedSCho, Yu-Chen 		kfree(buf);
183133842cedSCho, Yu-Chen 	}
183233842cedSCho, Yu-Chen 	return result;
183333842cedSCho, Yu-Chen }
183433842cedSCho, Yu-Chen 
183533842cedSCho, Yu-Chen /*
183633842cedSCho, Yu-Chen  * ENE MS Card
183733842cedSCho, Yu-Chen  */
183833842cedSCho, Yu-Chen 
183941e568d1Shuajun li static int ene_get_card_type(struct us_data *us, u16 index, void *buf)
184041e568d1Shuajun li {
184141e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
184241e568d1Shuajun li 	int result;
184341e568d1Shuajun li 
184441e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
184541e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
184641e568d1Shuajun li 	bcb->DataTransferLength	= 0x01;
1847b8db6d64SSebastian Andrzej Siewior 	bcb->Flags			= US_BULK_FLAG_IN;
184841e568d1Shuajun li 	bcb->CDB[0]			= 0xED;
184941e568d1Shuajun li 	bcb->CDB[2]			= (unsigned char)(index>>8);
185041e568d1Shuajun li 	bcb->CDB[3]			= (unsigned char)index;
185141e568d1Shuajun li 
185241e568d1Shuajun li 	result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
185341e568d1Shuajun li 	return result;
185441e568d1Shuajun li }
185541e568d1Shuajun li 
185641e568d1Shuajun li static int ene_get_card_status(struct us_data *us, u8 *buf)
185741e568d1Shuajun li {
185841e568d1Shuajun li 	u16 tmpreg;
185941e568d1Shuajun li 	u32 reg4b;
186041e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
186141e568d1Shuajun li 
1862191648d0SJoe Perches 	/*usb_stor_dbg(us, "transport --- ENE_ReadSDReg\n");*/
186341e568d1Shuajun li 	reg4b = *(u32 *)&buf[0x18];
186441e568d1Shuajun li 	info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f);
186541e568d1Shuajun li 
186641e568d1Shuajun li 	tmpreg = (u16) reg4b;
186741e568d1Shuajun li 	reg4b = *(u32 *)(&buf[0x14]);
186841e568d1Shuajun li 	if (info->SD_Status.HiCapacity && !info->SD_Status.IsMMC)
186941e568d1Shuajun li 		info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
187041e568d1Shuajun li 
187141e568d1Shuajun li 	info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
187241e568d1Shuajun li 	info->SD_C_SIZE_MULT = (u8)(reg4b >> 7)  & 0x07;
187341e568d1Shuajun li 	if (info->SD_Status.HiCapacity && info->SD_Status.IsMMC)
187441e568d1Shuajun li 		info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
187541e568d1Shuajun li 
187641e568d1Shuajun li 	if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
187741e568d1Shuajun li 		info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN);
187841e568d1Shuajun li 		info->SD_READ_BL_LEN = SD_BLOCK_LEN;
187941e568d1Shuajun li 	} else {
188041e568d1Shuajun li 		info->SD_Block_Mult = 1;
188141e568d1Shuajun li 	}
188241e568d1Shuajun li 
188341e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
188441e568d1Shuajun li }
188541e568d1Shuajun li 
188641e568d1Shuajun li static int ene_load_bincode(struct us_data *us, unsigned char flag)
188741e568d1Shuajun li {
188841e568d1Shuajun li 	int err;
188941e568d1Shuajun li 	char *fw_name = NULL;
189041e568d1Shuajun li 	unsigned char *buf = NULL;
189141e568d1Shuajun li 	const struct firmware *sd_fw = NULL;
189241e568d1Shuajun li 	int result = USB_STOR_TRANSPORT_ERROR;
189341e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
189441e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
189541e568d1Shuajun li 
189641e568d1Shuajun li 	if (info->BIN_FLAG == flag)
189741e568d1Shuajun li 		return USB_STOR_TRANSPORT_GOOD;
189841e568d1Shuajun li 
189941e568d1Shuajun li 	switch (flag) {
190041e568d1Shuajun li 	/* For SD */
190141e568d1Shuajun li 	case SD_INIT1_PATTERN:
1902191648d0SJoe Perches 		usb_stor_dbg(us, "SD_INIT1_PATTERN\n");
1903595c8970STim Gardner 		fw_name = SD_INIT1_FIRMWARE;
190441e568d1Shuajun li 		break;
190541e568d1Shuajun li 	case SD_INIT2_PATTERN:
1906191648d0SJoe Perches 		usb_stor_dbg(us, "SD_INIT2_PATTERN\n");
1907595c8970STim Gardner 		fw_name = SD_INIT2_FIRMWARE;
190841e568d1Shuajun li 		break;
190941e568d1Shuajun li 	case SD_RW_PATTERN:
1910191648d0SJoe Perches 		usb_stor_dbg(us, "SD_RW_PATTERN\n");
1911595c8970STim Gardner 		fw_name = SD_RW_FIRMWARE;
191241e568d1Shuajun li 		break;
191333842cedSCho, Yu-Chen 	/* For MS */
191433842cedSCho, Yu-Chen 	case MS_INIT_PATTERN:
1915191648d0SJoe Perches 		usb_stor_dbg(us, "MS_INIT_PATTERN\n");
1916595c8970STim Gardner 		fw_name = MS_INIT_FIRMWARE;
191733842cedSCho, Yu-Chen 		break;
191833842cedSCho, Yu-Chen 	case MSP_RW_PATTERN:
1919191648d0SJoe Perches 		usb_stor_dbg(us, "MSP_RW_PATTERN\n");
1920595c8970STim Gardner 		fw_name = MSP_RW_FIRMWARE;
192133842cedSCho, Yu-Chen 		break;
192233842cedSCho, Yu-Chen 	case MS_RW_PATTERN:
1923191648d0SJoe Perches 		usb_stor_dbg(us, "MS_RW_PATTERN\n");
1924595c8970STim Gardner 		fw_name = MS_RW_FIRMWARE;
192533842cedSCho, Yu-Chen 		break;
192641e568d1Shuajun li 	default:
1927191648d0SJoe Perches 		usb_stor_dbg(us, "----------- Unknown PATTERN ----------\n");
192841e568d1Shuajun li 		goto nofw;
192941e568d1Shuajun li 	}
193041e568d1Shuajun li 
193141e568d1Shuajun li 	err = request_firmware(&sd_fw, fw_name, &us->pusb_dev->dev);
193241e568d1Shuajun li 	if (err) {
1933191648d0SJoe Perches 		usb_stor_dbg(us, "load firmware %s failed\n", fw_name);
193441e568d1Shuajun li 		goto nofw;
193541e568d1Shuajun li 	}
1936a328512dSBenoit Taine 	buf = kmemdup(sd_fw->data, sd_fw->size, GFP_KERNEL);
1937191648d0SJoe Perches 	if (buf == NULL)
193841e568d1Shuajun li 		goto nofw;
1939191648d0SJoe Perches 
194041e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
194141e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
194241e568d1Shuajun li 	bcb->DataTransferLength = sd_fw->size;
194341e568d1Shuajun li 	bcb->Flags = 0x00;
194441e568d1Shuajun li 	bcb->CDB[0] = 0xEF;
194541e568d1Shuajun li 
194641e568d1Shuajun li 	result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1947aa18c4b6SAlan Stern 	if (us->srb != NULL)
1948aa18c4b6SAlan Stern 		scsi_set_resid(us->srb, 0);
194941e568d1Shuajun li 	info->BIN_FLAG = flag;
195041e568d1Shuajun li 	kfree(buf);
195141e568d1Shuajun li 
195241e568d1Shuajun li nofw:
195341e568d1Shuajun li 	release_firmware(sd_fw);
195441e568d1Shuajun li 	return result;
195541e568d1Shuajun li }
195641e568d1Shuajun li 
195733842cedSCho, Yu-Chen static int ms_card_init(struct us_data *us)
195833842cedSCho, Yu-Chen {
195933842cedSCho, Yu-Chen 	u32 result;
196033842cedSCho, Yu-Chen 	u16 TmpBlock;
196133842cedSCho, Yu-Chen 	unsigned char *PageBuffer0 = NULL, *PageBuffer1 = NULL;
196233842cedSCho, Yu-Chen 	struct ms_lib_type_extdat extdat;
196333842cedSCho, Yu-Chen 	u16 btBlk1st, btBlk2nd;
196433842cedSCho, Yu-Chen 	u32 btBlk1stErred;
196533842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
196633842cedSCho, Yu-Chen 
196733842cedSCho, Yu-Chen 	printk(KERN_INFO "MS_CardInit start\n");
196833842cedSCho, Yu-Chen 
196933842cedSCho, Yu-Chen 	ms_lib_free_allocatedarea(us); /* Clean buffer and set struct us_data flag to 0 */
197033842cedSCho, Yu-Chen 
197133842cedSCho, Yu-Chen 	/* get two PageBuffer */
197233842cedSCho, Yu-Chen 	PageBuffer0 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
197333842cedSCho, Yu-Chen 	PageBuffer1 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
197433842cedSCho, Yu-Chen 	if ((PageBuffer0 == NULL) || (PageBuffer1 == NULL)) {
197533842cedSCho, Yu-Chen 		result = MS_NO_MEMORY_ERROR;
197633842cedSCho, Yu-Chen 		goto exit;
197733842cedSCho, Yu-Chen 	}
197833842cedSCho, Yu-Chen 
197933842cedSCho, Yu-Chen 	btBlk1st = btBlk2nd = MS_LB_NOT_USED;
198033842cedSCho, Yu-Chen 	btBlk1stErred = 0;
198133842cedSCho, Yu-Chen 
198233842cedSCho, Yu-Chen 	for (TmpBlock = 0; TmpBlock < MS_MAX_INITIAL_ERROR_BLOCKS+2; TmpBlock++) {
198333842cedSCho, Yu-Chen 
198433842cedSCho, Yu-Chen 		switch (ms_read_readpage(us, TmpBlock, 0, (u32 *)PageBuffer0, &extdat)) {
198533842cedSCho, Yu-Chen 		case MS_STATUS_SUCCESS:
198633842cedSCho, Yu-Chen 			break;
198733842cedSCho, Yu-Chen 		case MS_STATUS_INT_ERROR:
198833842cedSCho, Yu-Chen 			break;
198933842cedSCho, Yu-Chen 		case MS_STATUS_ERROR:
199033842cedSCho, Yu-Chen 		default:
199133842cedSCho, Yu-Chen 			continue;
199233842cedSCho, Yu-Chen 		}
199333842cedSCho, Yu-Chen 
199433842cedSCho, Yu-Chen 		if ((extdat.ovrflg & MS_REG_OVR_BKST) == MS_REG_OVR_BKST_NG)
199533842cedSCho, Yu-Chen 			continue;
199633842cedSCho, Yu-Chen 
199733842cedSCho, Yu-Chen 		if (((extdat.mngflg & MS_REG_MNG_SYSFLG) == MS_REG_MNG_SYSFLG_USER) ||
199833842cedSCho, Yu-Chen 			(be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wBlockID) != MS_BOOT_BLOCK_ID) ||
199933842cedSCho, Yu-Chen 			(be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wFormatVersion) != MS_BOOT_BLOCK_FORMAT_VERSION) ||
200033842cedSCho, Yu-Chen 			(((struct ms_bootblock_page0 *)PageBuffer0)->header.bNumberOfDataEntry != MS_BOOT_BLOCK_DATA_ENTRIES))
200133842cedSCho, Yu-Chen 				continue;
200233842cedSCho, Yu-Chen 
200333842cedSCho, Yu-Chen 		if (btBlk1st != MS_LB_NOT_USED) {
200433842cedSCho, Yu-Chen 			btBlk2nd = TmpBlock;
200533842cedSCho, Yu-Chen 			break;
200633842cedSCho, Yu-Chen 		}
200733842cedSCho, Yu-Chen 
200833842cedSCho, Yu-Chen 		btBlk1st = TmpBlock;
200933842cedSCho, Yu-Chen 		memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE);
201033842cedSCho, Yu-Chen 		if (extdat.status1 & (MS_REG_ST1_DTER | MS_REG_ST1_EXER | MS_REG_ST1_FGER))
201133842cedSCho, Yu-Chen 			btBlk1stErred = 1;
201233842cedSCho, Yu-Chen 	}
201333842cedSCho, Yu-Chen 
201433842cedSCho, Yu-Chen 	if (btBlk1st == MS_LB_NOT_USED) {
201533842cedSCho, Yu-Chen 		result = MS_STATUS_ERROR;
201633842cedSCho, Yu-Chen 		goto exit;
201733842cedSCho, Yu-Chen 	}
201833842cedSCho, Yu-Chen 
201933842cedSCho, Yu-Chen 	/* write protect */
202033842cedSCho, Yu-Chen 	if ((extdat.status0 & MS_REG_ST0_WP) == MS_REG_ST0_WP_ON)
202133842cedSCho, Yu-Chen 		ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
202233842cedSCho, Yu-Chen 
202333842cedSCho, Yu-Chen 	result = MS_STATUS_ERROR;
202433842cedSCho, Yu-Chen 	/* 1st Boot Block */
202533842cedSCho, Yu-Chen 	if (btBlk1stErred == 0)
202633842cedSCho, Yu-Chen 		result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1);
202733842cedSCho, Yu-Chen 		/* 1st */
202833842cedSCho, Yu-Chen 	/* 2nd Boot Block */
202933842cedSCho, Yu-Chen 	if (result && (btBlk2nd != MS_LB_NOT_USED))
203033842cedSCho, Yu-Chen 		result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0);
203133842cedSCho, Yu-Chen 
203233842cedSCho, Yu-Chen 	if (result) {
203333842cedSCho, Yu-Chen 		result = MS_STATUS_ERROR;
203433842cedSCho, Yu-Chen 		goto exit;
203533842cedSCho, Yu-Chen 	}
203633842cedSCho, Yu-Chen 
203733842cedSCho, Yu-Chen 	for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++)
203833842cedSCho, Yu-Chen 		info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
203933842cedSCho, Yu-Chen 
204033842cedSCho, Yu-Chen 	info->MS_Lib.Phy2LogMap[btBlk1st] = MS_LB_BOOT_BLOCK;
204133842cedSCho, Yu-Chen 
204233842cedSCho, Yu-Chen 	if (btBlk2nd != MS_LB_NOT_USED) {
204333842cedSCho, Yu-Chen 		for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++)
204433842cedSCho, Yu-Chen 			info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
204533842cedSCho, Yu-Chen 
204633842cedSCho, Yu-Chen 		info->MS_Lib.Phy2LogMap[btBlk2nd] = MS_LB_BOOT_BLOCK;
204733842cedSCho, Yu-Chen 	}
204833842cedSCho, Yu-Chen 
204933842cedSCho, Yu-Chen 	result = ms_lib_scan_logicalblocknumber(us, btBlk1st);
205033842cedSCho, Yu-Chen 	if (result)
205133842cedSCho, Yu-Chen 		goto exit;
205233842cedSCho, Yu-Chen 
205333842cedSCho, Yu-Chen 	for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT;
205433842cedSCho, Yu-Chen 		TmpBlock < info->MS_Lib.NumberOfPhyBlock;
205533842cedSCho, Yu-Chen 		TmpBlock += MS_PHYSICAL_BLOCKS_PER_SEGMENT) {
205633842cedSCho, Yu-Chen 		if (ms_count_freeblock(us, TmpBlock) == 0) {
205733842cedSCho, Yu-Chen 			ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
205833842cedSCho, Yu-Chen 			break;
205933842cedSCho, Yu-Chen 		}
206033842cedSCho, Yu-Chen 	}
206133842cedSCho, Yu-Chen 
206233842cedSCho, Yu-Chen 	/* write */
206333842cedSCho, Yu-Chen 	if (ms_lib_alloc_writebuf(us)) {
206433842cedSCho, Yu-Chen 		result = MS_NO_MEMORY_ERROR;
206533842cedSCho, Yu-Chen 		goto exit;
206633842cedSCho, Yu-Chen 	}
206733842cedSCho, Yu-Chen 
206833842cedSCho, Yu-Chen 	result = MS_STATUS_SUCCESS;
206933842cedSCho, Yu-Chen 
207033842cedSCho, Yu-Chen exit:
207133842cedSCho, Yu-Chen 	kfree(PageBuffer1);
207233842cedSCho, Yu-Chen 	kfree(PageBuffer0);
207333842cedSCho, Yu-Chen 
207433842cedSCho, Yu-Chen 	printk(KERN_INFO "MS_CardInit end\n");
207533842cedSCho, Yu-Chen 	return result;
207633842cedSCho, Yu-Chen }
207733842cedSCho, Yu-Chen 
207833842cedSCho, Yu-Chen static int ene_ms_init(struct us_data *us)
207933842cedSCho, Yu-Chen {
208033842cedSCho, Yu-Chen 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
208133842cedSCho, Yu-Chen 	int result;
208233842cedSCho, Yu-Chen 	u16 MSP_BlockSize, MSP_UserAreaBlocks;
208333842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2084628c2893SAlan Stern 	u8 *bbuf = info->bbuf;
208533842cedSCho, Yu-Chen 
208633842cedSCho, Yu-Chen 	printk(KERN_INFO "transport --- ENE_MSInit\n");
208733842cedSCho, Yu-Chen 
208833842cedSCho, Yu-Chen 	/* the same part to test ENE */
208933842cedSCho, Yu-Chen 
209033842cedSCho, Yu-Chen 	result = ene_load_bincode(us, MS_INIT_PATTERN);
209133842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD) {
209233842cedSCho, Yu-Chen 		printk(KERN_ERR "Load MS Init Code Fail !!\n");
209333842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
209433842cedSCho, Yu-Chen 	}
209533842cedSCho, Yu-Chen 
209633842cedSCho, Yu-Chen 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
209733842cedSCho, Yu-Chen 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
209833842cedSCho, Yu-Chen 	bcb->DataTransferLength = 0x200;
2099b8db6d64SSebastian Andrzej Siewior 	bcb->Flags      = US_BULK_FLAG_IN;
210033842cedSCho, Yu-Chen 	bcb->CDB[0]     = 0xF1;
210133842cedSCho, Yu-Chen 	bcb->CDB[1]     = 0x01;
210233842cedSCho, Yu-Chen 
2103628c2893SAlan Stern 	result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
210433842cedSCho, Yu-Chen 	if (result != USB_STOR_XFER_GOOD) {
210533842cedSCho, Yu-Chen 		printk(KERN_ERR "Execution MS Init Code Fail !!\n");
210633842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
210733842cedSCho, Yu-Chen 	}
210833842cedSCho, Yu-Chen 	/* the same part to test ENE */
2109628c2893SAlan Stern 	info->MS_Status = *(struct MS_STATUS *) bbuf;
211033842cedSCho, Yu-Chen 
211133842cedSCho, Yu-Chen 	if (info->MS_Status.Insert && info->MS_Status.Ready) {
211233842cedSCho, Yu-Chen 		printk(KERN_INFO "Insert     = %x\n", info->MS_Status.Insert);
211333842cedSCho, Yu-Chen 		printk(KERN_INFO "Ready      = %x\n", info->MS_Status.Ready);
211433842cedSCho, Yu-Chen 		printk(KERN_INFO "IsMSPro    = %x\n", info->MS_Status.IsMSPro);
211533842cedSCho, Yu-Chen 		printk(KERN_INFO "IsMSPHG    = %x\n", info->MS_Status.IsMSPHG);
211633842cedSCho, Yu-Chen 		printk(KERN_INFO "WtP= %x\n", info->MS_Status.WtP);
211733842cedSCho, Yu-Chen 		if (info->MS_Status.IsMSPro) {
2118628c2893SAlan Stern 			MSP_BlockSize      = (bbuf[6] << 8) | bbuf[7];
2119628c2893SAlan Stern 			MSP_UserAreaBlocks = (bbuf[10] << 8) | bbuf[11];
212033842cedSCho, Yu-Chen 			info->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
212133842cedSCho, Yu-Chen 		} else {
212233842cedSCho, Yu-Chen 			ms_card_init(us); /* Card is MS (to ms.c)*/
212333842cedSCho, Yu-Chen 		}
2124191648d0SJoe Perches 		usb_stor_dbg(us, "MS Init Code OK !!\n");
212533842cedSCho, Yu-Chen 	} else {
2126628c2893SAlan Stern 		usb_stor_dbg(us, "MS Card Not Ready --- %x\n", bbuf[0]);
212733842cedSCho, Yu-Chen 		return USB_STOR_TRANSPORT_ERROR;
212833842cedSCho, Yu-Chen 	}
212933842cedSCho, Yu-Chen 
213033842cedSCho, Yu-Chen 	return USB_STOR_TRANSPORT_GOOD;
213133842cedSCho, Yu-Chen }
213233842cedSCho, Yu-Chen 
213341e568d1Shuajun li static int ene_sd_init(struct us_data *us)
213441e568d1Shuajun li {
213541e568d1Shuajun li 	int result;
213641e568d1Shuajun li 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
213741e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2138628c2893SAlan Stern 	u8 *bbuf = info->bbuf;
213941e568d1Shuajun li 
2140191648d0SJoe Perches 	usb_stor_dbg(us, "transport --- ENE_SDInit\n");
214141e568d1Shuajun li 	/* SD Init Part-1 */
214241e568d1Shuajun li 	result = ene_load_bincode(us, SD_INIT1_PATTERN);
214341e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
2144191648d0SJoe Perches 		usb_stor_dbg(us, "Load SD Init Code Part-1 Fail !!\n");
214541e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
214641e568d1Shuajun li 	}
214741e568d1Shuajun li 
214841e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
214941e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2150b8db6d64SSebastian Andrzej Siewior 	bcb->Flags = US_BULK_FLAG_IN;
215141e568d1Shuajun li 	bcb->CDB[0] = 0xF2;
215241e568d1Shuajun li 
215341e568d1Shuajun li 	result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
215441e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
2155191648d0SJoe Perches 		usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
215641e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
215741e568d1Shuajun li 	}
215841e568d1Shuajun li 
215941e568d1Shuajun li 	/* SD Init Part-2 */
216041e568d1Shuajun li 	result = ene_load_bincode(us, SD_INIT2_PATTERN);
216141e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
2162191648d0SJoe Perches 		usb_stor_dbg(us, "Load SD Init Code Part-2 Fail !!\n");
216341e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
216441e568d1Shuajun li 	}
216541e568d1Shuajun li 
216641e568d1Shuajun li 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
216741e568d1Shuajun li 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
216841e568d1Shuajun li 	bcb->DataTransferLength = 0x200;
2169b8db6d64SSebastian Andrzej Siewior 	bcb->Flags              = US_BULK_FLAG_IN;
217041e568d1Shuajun li 	bcb->CDB[0]             = 0xF1;
217141e568d1Shuajun li 
2172628c2893SAlan Stern 	result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
217341e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
2174191648d0SJoe Perches 		usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
217541e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
217641e568d1Shuajun li 	}
217741e568d1Shuajun li 
2178628c2893SAlan Stern 	info->SD_Status =  *(struct SD_STATUS *) bbuf;
217941e568d1Shuajun li 	if (info->SD_Status.Insert && info->SD_Status.Ready) {
2180191648d0SJoe Perches 		struct SD_STATUS *s = &info->SD_Status;
2181191648d0SJoe Perches 
2182628c2893SAlan Stern 		ene_get_card_status(us, bbuf);
2183191648d0SJoe Perches 		usb_stor_dbg(us, "Insert     = %x\n", s->Insert);
2184191648d0SJoe Perches 		usb_stor_dbg(us, "Ready      = %x\n", s->Ready);
2185191648d0SJoe Perches 		usb_stor_dbg(us, "IsMMC      = %x\n", s->IsMMC);
2186191648d0SJoe Perches 		usb_stor_dbg(us, "HiCapacity = %x\n", s->HiCapacity);
2187191648d0SJoe Perches 		usb_stor_dbg(us, "HiSpeed    = %x\n", s->HiSpeed);
2188191648d0SJoe Perches 		usb_stor_dbg(us, "WtP        = %x\n", s->WtP);
218941e568d1Shuajun li 	} else {
2190628c2893SAlan Stern 		usb_stor_dbg(us, "SD Card Not Ready --- %x\n", bbuf[0]);
219141e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
219241e568d1Shuajun li 	}
219341e568d1Shuajun li 	return USB_STOR_TRANSPORT_GOOD;
219441e568d1Shuajun li }
219541e568d1Shuajun li 
219641e568d1Shuajun li 
219741e568d1Shuajun li static int ene_init(struct us_data *us)
219841e568d1Shuajun li {
219941e568d1Shuajun li 	int result;
2200628c2893SAlan Stern 	u8  misc_reg03;
220141e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2202628c2893SAlan Stern 	u8 *bbuf = info->bbuf;
220341e568d1Shuajun li 
2204628c2893SAlan Stern 	result = ene_get_card_type(us, REG_CARD_STATUS, bbuf);
220541e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD)
220641e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
220741e568d1Shuajun li 
2208628c2893SAlan Stern 	misc_reg03 = bbuf[0];
220941e568d1Shuajun li 	if (misc_reg03 & 0x01) {
221041e568d1Shuajun li 		if (!info->SD_Status.Ready) {
221141e568d1Shuajun li 			result = ene_sd_init(us);
221241e568d1Shuajun li 			if (result != USB_STOR_XFER_GOOD)
221341e568d1Shuajun li 				return USB_STOR_TRANSPORT_ERROR;
221441e568d1Shuajun li 		}
221541e568d1Shuajun li 	}
221633842cedSCho, Yu-Chen 	if (misc_reg03 & 0x02) {
221733842cedSCho, Yu-Chen 		if (!info->MS_Status.Ready) {
221833842cedSCho, Yu-Chen 			result = ene_ms_init(us);
221933842cedSCho, Yu-Chen 			if (result != USB_STOR_XFER_GOOD)
222033842cedSCho, Yu-Chen 				return USB_STOR_TRANSPORT_ERROR;
222133842cedSCho, Yu-Chen 		}
222233842cedSCho, Yu-Chen 	}
222341e568d1Shuajun li 	return result;
222441e568d1Shuajun li }
222541e568d1Shuajun li 
222641e568d1Shuajun li /*----- sd_scsi_irp() ---------*/
222741e568d1Shuajun li static int sd_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
222841e568d1Shuajun li {
222941e568d1Shuajun li 	int    result;
223041e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
223141e568d1Shuajun li 
223241e568d1Shuajun li 	switch (srb->cmnd[0]) {
223341e568d1Shuajun li 	case TEST_UNIT_READY:
223441e568d1Shuajun li 		result = sd_scsi_test_unit_ready(us, srb);
223541e568d1Shuajun li 		break; /* 0x00 */
2236ce553bd1SAlan Stern 	case REQUEST_SENSE:
2237ce553bd1SAlan Stern 		result = do_scsi_request_sense(us, srb);
2238ce553bd1SAlan Stern 		break; /* 0x03 */
223941e568d1Shuajun li 	case INQUIRY:
2240f8efdabdSAlan Stern 		result = do_scsi_inquiry(us, srb);
224141e568d1Shuajun li 		break; /* 0x12 */
224241e568d1Shuajun li 	case MODE_SENSE:
224341e568d1Shuajun li 		result = sd_scsi_mode_sense(us, srb);
224441e568d1Shuajun li 		break; /* 0x1A */
224541e568d1Shuajun li 	/*
224641e568d1Shuajun li 	case START_STOP:
224741e568d1Shuajun li 		result = SD_SCSI_Start_Stop(us, srb);
224841e568d1Shuajun li 		break; //0x1B
224941e568d1Shuajun li 	*/
225041e568d1Shuajun li 	case READ_CAPACITY:
225141e568d1Shuajun li 		result = sd_scsi_read_capacity(us, srb);
225241e568d1Shuajun li 		break; /* 0x25 */
225341e568d1Shuajun li 	case READ_10:
225441e568d1Shuajun li 		result = sd_scsi_read(us, srb);
225541e568d1Shuajun li 		break; /* 0x28 */
225641e568d1Shuajun li 	case WRITE_10:
225741e568d1Shuajun li 		result = sd_scsi_write(us, srb);
225841e568d1Shuajun li 		break; /* 0x2A */
225941e568d1Shuajun li 	default:
226041e568d1Shuajun li 		info->SrbStatus = SS_ILLEGAL_REQUEST;
226141e568d1Shuajun li 		result = USB_STOR_TRANSPORT_FAILED;
226241e568d1Shuajun li 		break;
226341e568d1Shuajun li 	}
2264ce553bd1SAlan Stern 	if (result == USB_STOR_TRANSPORT_GOOD)
2265ce553bd1SAlan Stern 		info->SrbStatus = SS_SUCCESS;
226641e568d1Shuajun li 	return result;
226741e568d1Shuajun li }
226841e568d1Shuajun li 
226933842cedSCho, Yu-Chen /*
227033842cedSCho, Yu-Chen  * ms_scsi_irp()
227133842cedSCho, Yu-Chen  */
227236f3a14dSFelipe Balbi static int ms_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
227333842cedSCho, Yu-Chen {
227433842cedSCho, Yu-Chen 	int result;
227533842cedSCho, Yu-Chen 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2276ce553bd1SAlan Stern 
227733842cedSCho, Yu-Chen 	switch (srb->cmnd[0]) {
227833842cedSCho, Yu-Chen 	case TEST_UNIT_READY:
227933842cedSCho, Yu-Chen 		result = ms_scsi_test_unit_ready(us, srb);
228033842cedSCho, Yu-Chen 		break; /* 0x00 */
2281ce553bd1SAlan Stern 	case REQUEST_SENSE:
2282ce553bd1SAlan Stern 		result = do_scsi_request_sense(us, srb);
2283ce553bd1SAlan Stern 		break; /* 0x03 */
228433842cedSCho, Yu-Chen 	case INQUIRY:
2285f8efdabdSAlan Stern 		result = do_scsi_inquiry(us, srb);
228633842cedSCho, Yu-Chen 		break; /* 0x12 */
228733842cedSCho, Yu-Chen 	case MODE_SENSE:
228833842cedSCho, Yu-Chen 		result = ms_scsi_mode_sense(us, srb);
228933842cedSCho, Yu-Chen 		break; /* 0x1A */
229033842cedSCho, Yu-Chen 	case READ_CAPACITY:
229133842cedSCho, Yu-Chen 		result = ms_scsi_read_capacity(us, srb);
229233842cedSCho, Yu-Chen 		break; /* 0x25 */
229333842cedSCho, Yu-Chen 	case READ_10:
229433842cedSCho, Yu-Chen 		result = ms_scsi_read(us, srb);
229533842cedSCho, Yu-Chen 		break; /* 0x28 */
229633842cedSCho, Yu-Chen 	case WRITE_10:
229733842cedSCho, Yu-Chen 		result = ms_scsi_write(us, srb);
229833842cedSCho, Yu-Chen 		break;  /* 0x2A */
229933842cedSCho, Yu-Chen 	default:
230033842cedSCho, Yu-Chen 		info->SrbStatus = SS_ILLEGAL_REQUEST;
230133842cedSCho, Yu-Chen 		result = USB_STOR_TRANSPORT_FAILED;
230233842cedSCho, Yu-Chen 		break;
230333842cedSCho, Yu-Chen 	}
2304ce553bd1SAlan Stern 	if (result == USB_STOR_TRANSPORT_GOOD)
2305ce553bd1SAlan Stern 		info->SrbStatus = SS_SUCCESS;
230633842cedSCho, Yu-Chen 	return result;
230733842cedSCho, Yu-Chen }
230833842cedSCho, Yu-Chen 
230941e568d1Shuajun li static int ene_transport(struct scsi_cmnd *srb, struct us_data *us)
231041e568d1Shuajun li {
23114b309f1cSAlan Stern 	int result = USB_STOR_XFER_GOOD;
231241e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
231341e568d1Shuajun li 
2314191648d0SJoe Perches 	/*US_DEBUG(usb_stor_show_command(us, srb)); */
231541e568d1Shuajun li 	scsi_set_resid(srb, 0);
23164b309f1cSAlan Stern 	if (unlikely(!(info->SD_Status.Ready || info->MS_Status.Ready)))
231741e568d1Shuajun li 		result = ene_init(us);
23184b309f1cSAlan Stern 	if (result == USB_STOR_XFER_GOOD) {
23194b309f1cSAlan Stern 		result = USB_STOR_TRANSPORT_ERROR;
232033842cedSCho, Yu-Chen 		if (info->SD_Status.Ready)
232141e568d1Shuajun li 			result = sd_scsi_irp(us, srb);
232241e568d1Shuajun li 
232333842cedSCho, Yu-Chen 		if (info->MS_Status.Ready)
232433842cedSCho, Yu-Chen 			result = ms_scsi_irp(us, srb);
232533842cedSCho, Yu-Chen 	}
23264b309f1cSAlan Stern 	return result;
232741e568d1Shuajun li }
232841e568d1Shuajun li 
2329aa519be3SAkinobu Mita static struct scsi_host_template ene_ub6250_host_template;
233041e568d1Shuajun li 
233141e568d1Shuajun li static int ene_ub6250_probe(struct usb_interface *intf,
233241e568d1Shuajun li 			 const struct usb_device_id *id)
233341e568d1Shuajun li {
233441e568d1Shuajun li 	int result;
2335628c2893SAlan Stern 	u8  misc_reg03;
233641e568d1Shuajun li 	struct us_data *us;
2337628c2893SAlan Stern 	struct ene_ub6250_info *info;
233841e568d1Shuajun li 
233941e568d1Shuajun li 	result = usb_stor_probe1(&us, intf, id,
2340aa519be3SAkinobu Mita 		   (id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list,
2341aa519be3SAkinobu Mita 		   &ene_ub6250_host_template);
234241e568d1Shuajun li 	if (result)
234341e568d1Shuajun li 		return result;
234441e568d1Shuajun li 
234541e568d1Shuajun li 	/* FIXME: where should the code alloc extra buf ? */
234641e568d1Shuajun li 	us->extra = kzalloc(sizeof(struct ene_ub6250_info), GFP_KERNEL);
234741e568d1Shuajun li 	if (!us->extra)
234841e568d1Shuajun li 		return -ENOMEM;
234941e568d1Shuajun li 	us->extra_destructor = ene_ub6250_info_destructor;
2350628c2893SAlan Stern 
2351628c2893SAlan Stern 	info = (struct ene_ub6250_info *)(us->extra);
2352628c2893SAlan Stern 	info->bbuf = kmalloc(512, GFP_KERNEL);
2353628c2893SAlan Stern 	if (!info->bbuf) {
2354628c2893SAlan Stern 		kfree(us->extra);
2355628c2893SAlan Stern 		return -ENOMEM;
235641e568d1Shuajun li 	}
235741e568d1Shuajun li 
235841e568d1Shuajun li 	us->transport_name = "ene_ub6250";
235941e568d1Shuajun li 	us->transport = ene_transport;
236041e568d1Shuajun li 	us->max_lun = 0;
236141e568d1Shuajun li 
236241e568d1Shuajun li 	result = usb_stor_probe2(us);
236341e568d1Shuajun li 	if (result)
236441e568d1Shuajun li 		return result;
236541e568d1Shuajun li 
236641e568d1Shuajun li 	/* probe card type */
2367628c2893SAlan Stern 	result = ene_get_card_type(us, REG_CARD_STATUS, info->bbuf);
236841e568d1Shuajun li 	if (result != USB_STOR_XFER_GOOD) {
236941e568d1Shuajun li 		usb_stor_disconnect(intf);
237041e568d1Shuajun li 		return USB_STOR_TRANSPORT_ERROR;
237141e568d1Shuajun li 	}
237241e568d1Shuajun li 
2373628c2893SAlan Stern 	misc_reg03 = info->bbuf[0];
237441e568d1Shuajun li 	if (!(misc_reg03 & 0x01)) {
237516fae052SKristina Martšenko 		pr_info("ums_eneub6250: This driver only supports SD/MS cards. "
237616fae052SKristina Martšenko 			"It does not support SM cards.\n");
237741e568d1Shuajun li 	}
237841e568d1Shuajun li 
237941e568d1Shuajun li 	return result;
238041e568d1Shuajun li }
238141e568d1Shuajun li 
238241e568d1Shuajun li 
238341e568d1Shuajun li #ifdef CONFIG_PM
238441e568d1Shuajun li 
238541e568d1Shuajun li static int ene_ub6250_resume(struct usb_interface *iface)
238641e568d1Shuajun li {
238741e568d1Shuajun li 	u8 tmp = 0;
238841e568d1Shuajun li 	struct us_data *us = usb_get_intfdata(iface);
238941e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
239041e568d1Shuajun li 
239141e568d1Shuajun li 	mutex_lock(&us->dev_mutex);
239241e568d1Shuajun li 
239341e568d1Shuajun li 	if (us->suspend_resume_hook)
239441e568d1Shuajun li 		(us->suspend_resume_hook)(us, US_RESUME);
239541e568d1Shuajun li 
239641e568d1Shuajun li 	mutex_unlock(&us->dev_mutex);
239741e568d1Shuajun li 
239841e568d1Shuajun li 	info->Power_IsResum = true;
239941e568d1Shuajun li 	/*info->SD_Status.Ready = 0; */
240041e568d1Shuajun li 	info->SD_Status = *(struct SD_STATUS *)&tmp;
240141e568d1Shuajun li 	info->MS_Status = *(struct MS_STATUS *)&tmp;
240241e568d1Shuajun li 	info->SM_Status = *(struct SM_STATUS *)&tmp;
240341e568d1Shuajun li 
240441e568d1Shuajun li 	return 0;
240541e568d1Shuajun li }
240641e568d1Shuajun li 
240741e568d1Shuajun li static int ene_ub6250_reset_resume(struct usb_interface *iface)
240841e568d1Shuajun li {
240941e568d1Shuajun li 	u8 tmp = 0;
241041e568d1Shuajun li 	struct us_data *us = usb_get_intfdata(iface);
241141e568d1Shuajun li 	struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2412191648d0SJoe Perches 
241341e568d1Shuajun li 	/* Report the reset to the SCSI core */
241441e568d1Shuajun li 	usb_stor_reset_resume(iface);
241541e568d1Shuajun li 
2416f0183a33SFelipe Balbi 	/*
2417f0183a33SFelipe Balbi 	 * FIXME: Notify the subdrivers that they need to reinitialize
2418f0183a33SFelipe Balbi 	 * the device
2419f0183a33SFelipe Balbi 	 */
242041e568d1Shuajun li 	info->Power_IsResum = true;
242141e568d1Shuajun li 	/*info->SD_Status.Ready = 0; */
242241e568d1Shuajun li 	info->SD_Status = *(struct SD_STATUS *)&tmp;
242341e568d1Shuajun li 	info->MS_Status = *(struct MS_STATUS *)&tmp;
242441e568d1Shuajun li 	info->SM_Status = *(struct SM_STATUS *)&tmp;
242541e568d1Shuajun li 
242641e568d1Shuajun li 	return 0;
242741e568d1Shuajun li }
242841e568d1Shuajun li 
242941e568d1Shuajun li #else
243041e568d1Shuajun li 
243141e568d1Shuajun li #define ene_ub6250_resume		NULL
243241e568d1Shuajun li #define ene_ub6250_reset_resume		NULL
243341e568d1Shuajun li 
243441e568d1Shuajun li #endif
243541e568d1Shuajun li 
243641e568d1Shuajun li static struct usb_driver ene_ub6250_driver = {
2437aa519be3SAkinobu Mita 	.name =		DRV_NAME,
243841e568d1Shuajun li 	.probe =	ene_ub6250_probe,
243941e568d1Shuajun li 	.disconnect =	usb_stor_disconnect,
244041e568d1Shuajun li 	.suspend =	usb_stor_suspend,
244141e568d1Shuajun li 	.resume =	ene_ub6250_resume,
244241e568d1Shuajun li 	.reset_resume =	ene_ub6250_reset_resume,
244341e568d1Shuajun li 	.pre_reset =	usb_stor_pre_reset,
244441e568d1Shuajun li 	.post_reset =	usb_stor_post_reset,
244541e568d1Shuajun li 	.id_table =	ene_ub6250_usb_ids,
244641e568d1Shuajun li 	.soft_unbind =	1,
2447e73b2db6SHuajun Li 	.no_dynamic_id = 1,
244841e568d1Shuajun li };
244941e568d1Shuajun li 
2450aa519be3SAkinobu Mita module_usb_stor_driver(ene_ub6250_driver, ene_ub6250_host_template, DRV_NAME);
2451