18 #include <linux/errno.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
22 #include <scsi/scsi.h>
32 #define SD_INIT1_FIRMWARE "ene-ub6250/sd_init1.bin"
33 #define SD_INIT2_FIRMWARE "ene-ub6250/sd_init2.bin"
34 #define SD_RW_FIRMWARE "ene-ub6250/sd_rdwr.bin"
35 #define MS_INIT_FIRMWARE "ene-ub6250/ms_init.bin"
36 #define MSP_RW_FIRMWARE "ene-ub6250/msp_rdwr.bin"
37 #define MS_RW_FIRMWARE "ene-ub6250/ms_rdwr.bin"
51 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
52 vendorName, productName, useProtocol, useTransport, \
53 initFunction, flags) \
54 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
55 .driver_info = (flags)}
68 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
69 vendor_name, product_name, use_protocol, use_transport, \
70 init_function, Flags) \
72 .vendorName = vendor_name, \
73 .productName = product_name, \
74 .useProtocol = use_protocol, \
75 .useTransport = use_transport, \
76 .initFunction = init_function, \
89 #define ENE_BIN_CODE_LEN 0x800
91 #define REG_CARD_STATUS 0xFF83
92 #define REG_HW_TRAP1 0xFF89
95 #define SS_SUCCESS 0x00
96 #define SS_NOT_READY 0x02
97 #define SS_MEDIUM_ERR 0x03
98 #define SS_HW_ERR 0x04
99 #define SS_ILLEGAL_REQUEST 0x05
100 #define SS_UNIT_ATTENTION 0x06
103 #define SD_INIT1_PATTERN 1
104 #define SD_INIT2_PATTERN 2
105 #define SD_RW_PATTERN 3
106 #define MS_INIT_PATTERN 4
107 #define MSP_RW_PATTERN 5
108 #define MS_RW_PATTERN 6
109 #define SM_INIT_PATTERN 7
110 #define SM_RW_PATTERN 8
118 #define MS_REG_ST1_MB 0x80
119 #define MS_REG_ST1_FB1 0x40
120 #define MS_REG_ST1_DTER 0x20
121 #define MS_REG_ST1_UCDT 0x10
122 #define MS_REG_ST1_EXER 0x08
123 #define MS_REG_ST1_UCEX 0x04
124 #define MS_REG_ST1_FGER 0x02
125 #define MS_REG_ST1_UCFG 0x01
126 #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)
129 #define MS_REG_OVR_BKST 0x80
130 #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST
131 #define MS_REG_OVR_BKST_NG 0x00
132 #define MS_REG_OVR_PGST0 0x40
133 #define MS_REG_OVR_PGST1 0x20
134 #define MS_REG_OVR_PGST_MASK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1)
135 #define MS_REG_OVR_PGST_OK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1)
136 #define MS_REG_OVR_PGST_NG MS_REG_OVR_PGST1
137 #define MS_REG_OVR_PGST_DATA_ERROR 0x00
138 #define MS_REG_OVR_UDST 0x10
139 #define MS_REG_OVR_UDST_UPDATING 0x00
140 #define MS_REG_OVR_UDST_NO_UPDATE MS_REG_OVR_UDST
141 #define MS_REG_OVR_RESERVED 0x08
142 #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)
145 #define MS_REG_MNG_SCMS0 0x20
146 #define MS_REG_MNG_SCMS1 0x10
147 #define MS_REG_MNG_SCMS_MASK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
148 #define MS_REG_MNG_SCMS_COPY_OK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
149 #define MS_REG_MNG_SCMS_ONE_COPY MS_REG_MNG_SCMS1
150 #define MS_REG_MNG_SCMS_NO_COPY 0x00
151 #define MS_REG_MNG_ATFLG 0x08
152 #define MS_REG_MNG_ATFLG_OTHER MS_REG_MNG_ATFLG
153 #define MS_REG_MNG_ATFLG_ATTBL 0x00
154 #define MS_REG_MNG_SYSFLG 0x04
155 #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG
156 #define MS_REG_MNG_SYSFLG_BOOT 0x00
157 #define MS_REG_MNG_RESERVED 0xc3
158 #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)
161 #define MS_MAX_PAGES_PER_BLOCK 32
162 #define MS_MAX_INITIAL_ERROR_BLOCKS 10
163 #define MS_LIB_BITS_PER_BYTE 8
165 #define MS_SYSINF_FORMAT_FAT 1
166 #define MS_SYSINF_USAGE_GENERAL 0
168 #define MS_SYSINF_MSCLASS_TYPE_1 1
169 #define MS_SYSINF_PAGE_SIZE MS_BYTES_PER_PAGE
171 #define MS_SYSINF_CARDTYPE_RDONLY 1
172 #define MS_SYSINF_CARDTYPE_RDWR 2
173 #define MS_SYSINF_CARDTYPE_HYBRID 3
174 #define MS_SYSINF_SECURITY 0x01
175 #define MS_SYSINF_SECURITY_NO_SUPPORT MS_SYSINF_SECURITY
176 #define MS_SYSINF_SECURITY_SUPPORT 0
178 #define MS_SYSINF_RESERVED1 1
179 #define MS_SYSINF_RESERVED2 1
181 #define MS_SYSENT_TYPE_INVALID_BLOCK 0x01
182 #define MS_SYSENT_TYPE_CIS_IDI 0x0a
184 #define SIZE_OF_KIRO 1024
185 #define BYTE_MASK 0xff
188 #define MS_STATUS_WRITE_PROTECT 0x0106
189 #define MS_STATUS_SUCCESS 0x0000
190 #define MS_ERROR_FLASH_READ 0x8003
191 #define MS_ERROR_FLASH_ERASE 0x8005
192 #define MS_LB_ERROR 0xfff0
193 #define MS_LB_BOOT_BLOCK 0xfff1
194 #define MS_LB_INITIAL_ERROR 0xfff2
195 #define MS_STATUS_SUCCESS_WITH_ECC 0xfff3
196 #define MS_LB_ACQUIRED_ERROR 0xfff4
197 #define MS_LB_NOT_USED_ERASED 0xfff5
198 #define MS_NOCARD_ERROR 0xfff8
199 #define MS_NO_MEMORY_ERROR 0xfff9
200 #define MS_STATUS_INT_ERROR 0xfffa
201 #define MS_STATUS_ERROR 0xfffe
202 #define MS_LB_NOT_USED 0xffff
204 #define MS_REG_MNG_SYSFLG 0x04
205 #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG
207 #define MS_BOOT_BLOCK_ID 0x0001
208 #define MS_BOOT_BLOCK_FORMAT_VERSION 0x0100
209 #define MS_BOOT_BLOCK_DATA_ENTRIES 2
211 #define MS_NUMBER_OF_SYSTEM_ENTRY 4
212 #define MS_NUMBER_OF_BOOT_BLOCK 2
213 #define MS_BYTES_PER_PAGE 512
214 #define MS_LOGICAL_BLOCKS_PER_SEGMENT 496
215 #define MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT 494
217 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT 0x200
218 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK 0x1ff
221 #define MS_REG_OVR_BKST 0x80
222 #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST
223 #define MS_REG_OVR_BKST_NG 0x00
226 #define MS_REG_ST1_DTER 0x20
227 #define MS_REG_ST1_EXER 0x08
228 #define MS_REG_ST1_FGER 0x02
232 #define MS_REG_ST0_WP 0x01
233 #define MS_REG_ST0_WP_ON MS_REG_ST0_WP
235 #define MS_LIB_CTRL_RDONLY 0
236 #define MS_LIB_CTRL_WRPROTECT 1
239 #define ms_libconv_to_logical(pdx, PhyBlock) (((PhyBlock) >= (pdx)->MS_Lib.NumberOfPhyBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Phy2LogMap[PhyBlock])
240 #define ms_libconv_to_physical(pdx, LogBlock) (((LogBlock) >= (pdx)->MS_Lib.NumberOfLogBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Log2PhyMap[LogBlock])
242 #define ms_lib_ctrl_set(pdx, Flag) ((pdx)->MS_Lib.flags |= (1 << (Flag)))
243 #define ms_lib_ctrl_reset(pdx, Flag) ((pdx)->MS_Lib.flags &= ~(1 << (Flag)))
244 #define ms_lib_ctrl_check(pdx, Flag) ((pdx)->MS_Lib.flags & (1 << (Flag)))
246 #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))
247 #define ms_lib_clear_pagemap(pdx) memset((pdx)->MS_Lib.pagemap, 0, sizeof((pdx)->MS_Lib.pagemap))
248 #define memstick_logaddr(logadr1, logadr0) ((((u16)(logadr1)) << 8) | (logadr0))
304 #define MS_IDI_GENERAL_CONF 0x848A
443 #define SD_BLOCK_LEN 9
487 static int ene_sd_init(
struct us_data *us);
488 static int ene_ms_init(
struct us_data *us);
489 static int ene_load_bincode(
struct us_data *us,
unsigned char flag);
491 static void ene_ub6250_info_destructor(
void *
extra)
497 static int ene_send_scsi_cmd(
struct us_data *us,
u8 fDir,
void *
buf,
int use_sg)
503 unsigned int residue;
504 unsigned int cswlen = 0, partial = 0;
512 US_DEBUGP(
"send cmd to out endpoint fail ---\n");
517 unsigned int pipe = fDir;
529 transfer_length, 0, &partial);
542 US_DEBUGP(
"Received 0-length CSW; retrying...\n");
549 US_DEBUGP(
"Attempting to get CSW (2nd try)...\n");
562 if (residue && !(us->
fflags & US_FL_IGNORE_RESIDUE)) {
563 residue =
min(residue, transfer_length);
565 scsi_set_resid(us->
srb,
max(scsi_get_resid(us->
srb),
591 unsigned char data_ptr[36] = {
592 0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
593 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
594 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
595 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30 };
604 unsigned char mediaNoWP[12] = {
605 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
606 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
607 unsigned char mediaWP[12] = {
608 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
609 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
625 unsigned char buf[8];
635 bl_num = (info->
HC_C_SIZE + 1) * 1024 - 1;
646 buf[0] = (bl_num >> 24) & 0xff;
647 buf[1] = (bl_num >> 16) & 0xff;
648 buf[2] = (bl_num >> 8) & 0xff;
649 buf[3] = (bl_num >> 0) & 0xff;
650 buf[4] = (bl_len >> 24) & 0xff;
651 buf[5] = (bl_len >> 16) & 0xff;
652 buf[6] = (bl_len >> 8) & 0xff;
653 buf[7] = (bl_len >> 0) & 0xff;
663 unsigned char *
cdb = srb->
cmnd;
667 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
668 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
669 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
670 u32 bnByte = bn * 0x200;
671 u32 blenByte = blen * 0x200;
678 US_DEBUGP(
"Load SD RW pattern Fail !!\n");
691 bcb->
CDB[5] = (
unsigned char)(bnByte);
692 bcb->
CDB[4] = (
unsigned char)(bnByte>>8);
693 bcb->
CDB[3] = (
unsigned char)(bnByte>>16);
694 bcb->
CDB[2] = (
unsigned char)(bnByte>>24);
696 result = ene_send_scsi_cmd(us,
FDIR_READ, scsi_sglist(srb), 1);
703 unsigned char *cdb = srb->
cmnd;
707 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
708 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
709 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
710 u32 bnByte = bn * 0x200;
711 u32 blenByte = blen * 0x200;
718 US_DEBUGP(
"Load SD RW pattern Fail !!\n");
731 bcb->
CDB[5] = (
unsigned char)(bnByte);
732 bcb->
CDB[4] = (
unsigned char)(bnByte>>8);
733 bcb->
CDB[3] = (
unsigned char)(bnByte>>16);
734 bcb->
CDB[2] = (
unsigned char)(bnByte>>24);
736 result = ene_send_scsi_cmd(us,
FDIR_WRITE, scsi_sglist(srb), 1);
744 static int ms_lib_set_logicalpair(
struct us_data *us,
u16 logblk,
u16 phyblk)
748 if ((logblk >= info->
MS_Lib.NumberOfLogBlock) || (phyblk >= info->
MS_Lib.NumberOfPhyBlock))
751 info->
MS_Lib.Phy2LogMap[phyblk] = logblk;
752 info->
MS_Lib.Log2PhyMap[logblk] = phyblk;
761 if (phyblk >= info->
MS_Lib.NumberOfPhyBlock)
769 static int ms_lib_set_initialerrorblock(
struct us_data *us,
u16 phyblk)
774 static int ms_lib_set_bootblockmark(
struct us_data *us,
u16 phyblk)
779 static int ms_lib_free_logicalmap(
struct us_data *us)
792 static int ms_lib_alloc_logicalmap(
struct us_data *us)
801 ms_lib_free_logicalmap(us);
805 for (i = 0; i < info->
MS_Lib.NumberOfPhyBlock; i++)
808 for (i = 0; i < info->
MS_Lib.NumberOfLogBlock; i++)
814 static void ms_lib_clear_writebuf(
struct us_data *us)
825 if (info->
MS_Lib.blkext) {
826 for (i = 0; i < info->
MS_Lib.PagesPerBlock; i++) {
835 static int ms_count_freeblock(
struct us_data *us,
u16 PhyBlock)
841 for (Count = 0; PhyBlock < Ende; PhyBlock++) {
842 switch (info->
MS_Lib.Phy2LogMap[PhyBlock]) {
854 static int ms_read_readpage(
struct us_data *us,
u32 PhyBlockAddr,
860 u32 bn = PhyBlockAddr * 0x20 + PageNum;
878 bcb->
CDB[5] = (
unsigned char)(bn);
879 bcb->
CDB[4] = (
unsigned char)(bn>>8);
880 bcb->
CDB[3] = (
unsigned char)(bn>>16);
881 bcb->
CDB[2] = (
unsigned char)(bn>>24);
883 result = ene_send_scsi_cmd(us,
FDIR_READ, PageBuf, 0);
896 bcb->
CDB[5] = (
unsigned char)(PageNum);
897 bcb->
CDB[4] = (
unsigned char)(PhyBlockAddr);
898 bcb->
CDB[3] = (
unsigned char)(PhyBlockAddr>>8);
899 bcb->
CDB[2] = (
unsigned char)(PhyBlockAddr>>16);
902 result = ene_send_scsi_cmd(us,
FDIR_READ, &ExtBuf, 0);
907 ExtraDat->
intr = 0x80;
911 ExtraDat->
ovrflg = ExtBuf[0];
912 ExtraDat->
mngflg = ExtBuf[1];
918 static int ms_lib_process_bootblock(
struct us_data *us,
u16 PhyBlock,
u8 *PageData)
929 if (PageBuffer ==
NULL)
965 if (ms_lib_alloc_logicalmap(us))
969 ms_lib_set_bootblockmark(us, PhyBlock);
974 u32 EntryOffset, EntrySize;
978 if (EntryOffset == 0xffffff)
989 u8 PrevPageNumber = 0;
995 while (EntrySize > 0) {
998 if (PageNumber != PrevPageNumber) {
999 switch (ms_read_readpage(us, PhyBlock, PageNumber, (
u32 *)PageBuffer, &ExtraData)) {
1009 PrevPageNumber = PageNumber;
1013 if (phyblk < 0x0fff)
1014 ms_lib_set_initialerrorblock(us, phyblk);
1019 }
else if (i == 1) {
1025 switch (ms_read_readpage(us, PhyBlock, (
u8)(EntryOffset /
MS_BYTES_PER_PAGE + 1), (
u32 *)PageBuffer, &ExtraData)) {
1040 if (info->
MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE)
1049 ms_lib_free_logicalmap(us);
1057 static void ms_lib_free_writebuf(
struct us_data *us)
1066 if (info->
MS_Lib.blkpag) {
1071 if (info->
MS_Lib.blkext) {
1078 static void ms_lib_free_allocatedarea(
struct us_data *us)
1082 ms_lib_free_writebuf(us);
1083 ms_lib_free_logicalmap(us);
1087 info->
MS_Lib.BytesPerSector = 0;
1088 info->
MS_Lib.SectorsPerCylinder = 0;
1090 info->
MS_Lib.cardType = 0;
1091 info->
MS_Lib.blockSize = 0;
1092 info->
MS_Lib.PagesPerBlock = 0;
1094 info->
MS_Lib.NumberOfPhyBlock = 0;
1095 info->
MS_Lib.NumberOfLogBlock = 0;
1099 static int ms_lib_alloc_writebuf(
struct us_data *us)
1109 ms_lib_free_writebuf(us);
1113 ms_lib_clear_writebuf(us);
1118 static int ms_lib_force_setlogical_pair(
struct us_data *us,
u16 logblk,
u16 phyblk)
1125 if ((logblk >= info->
MS_Lib.NumberOfLogBlock) ||
1126 (phyblk >= info->
MS_Lib.NumberOfPhyBlock))
1129 info->
MS_Lib.Phy2LogMap[phyblk] = logblk;
1130 info->
MS_Lib.Log2PhyMap[logblk] = phyblk;
1135 static int ms_read_copyblock(
struct us_data *us,
u16 oldphy,
u16 newphy,
1136 u16 PhyBlockAddr,
u8 PageNum,
unsigned char *buf,
u16 len)
1153 bcb->
CDB[4] = (
unsigned char)(oldphy);
1154 bcb->
CDB[3] = (
unsigned char)(oldphy>>8);
1156 bcb->
CDB[7] = (
unsigned char)(newphy);
1157 bcb->
CDB[6] = (
unsigned char)(newphy>>8);
1159 bcb->
CDB[9] = (
unsigned char)(PhyBlockAddr);
1160 bcb->
CDB[8] = (
unsigned char)(PhyBlockAddr>>8);
1161 bcb->
CDB[10] = PageNum;
1163 result = ene_send_scsi_cmd(us,
FDIR_WRITE, buf, 0);
1170 static int ms_read_eraseblock(
struct us_data *us,
u32 PhyBlockAddr)
1174 u32 bn = PhyBlockAddr;
1188 bcb->
CDB[4] = (
unsigned char)(bn);
1189 bcb->
CDB[3] = (
unsigned char)(bn>>8);
1190 bcb->
CDB[2] = (
unsigned char)(bn>>16);
1199 static int ms_lib_check_disableblock(
struct us_data *us,
u16 PhyBlock)
1201 unsigned char *PageBuf =
NULL;
1208 if (PageBuf ==
NULL) {
1213 ms_read_readpage(us, PhyBlock, 1, (
u32 *)PageBuf, &extdat);
1218 if (blk == info->
MS_Lib.Log2PhyMap[0]) {
1230 static int ms_lib_setacquired_errorblock(
struct us_data *us,
u16 phyblk)
1235 if (phyblk >= info->
MS_Lib.NumberOfPhyBlock)
1238 log = info->
MS_Lib.Phy2LogMap[phyblk];
1249 static int ms_lib_overwrite_extra(
struct us_data *us,
u32 PhyBlockAddr,
1267 bcb->
CDB[5] = (
unsigned char)(PageNum);
1268 bcb->
CDB[4] = (
unsigned char)(PhyBlockAddr);
1269 bcb->
CDB[3] = (
unsigned char)(PhyBlockAddr>>8);
1270 bcb->
CDB[2] = (
unsigned char)(PhyBlockAddr>>16);
1283 static int ms_lib_error_phyblock(
struct us_data *us,
u16 phyblk)
1287 if (phyblk >= info->
MS_Lib.NumberOfPhyBlock)
1290 ms_lib_setacquired_errorblock(us, phyblk);
1298 static int ms_lib_erase_phyblock(
struct us_data *us,
u16 phyblk)
1303 if (phyblk >= info->
MS_Lib.NumberOfPhyBlock)
1306 log = info->
MS_Lib.Phy2LogMap[phyblk];
1314 switch (ms_read_eraseblock(us, phyblk)) {
1320 ms_lib_error_phyblock(us, phyblk);
1325 ms_lib_setacquired_errorblock(us, phyblk);
1330 ms_lib_setacquired_errorblock(us, phyblk);
1335 static int ms_lib_read_extra(
struct us_data *us,
u32 PhyBlock,
1349 bcb->
CDB[5] = (
unsigned char)(PageNum);
1350 bcb->
CDB[4] = (
unsigned char)(PhyBlock);
1351 bcb->
CDB[3] = (
unsigned char)(PhyBlock>>8);
1352 bcb->
CDB[2] = (
unsigned char)(PhyBlock>>16);
1355 result = ene_send_scsi_cmd(us,
FDIR_READ, &ExtBuf, 0);
1360 ExtraDat->
intr = 0x80;
1363 ExtraDat->
ovrflg = ExtBuf[0];
1364 ExtraDat->
mngflg = ExtBuf[1];
1370 static int ms_libsearch_block_from_physical(
struct us_data *us,
u16 phyblk)
1378 if (phyblk >= info->
MS_Lib.NumberOfPhyBlock)
1381 for (blk = phyblk + 1; blk != phyblk; blk++) {
1389 switch (ms_lib_read_extra(us, blk, 0, &extdat)) {
1399 ms_lib_setacquired_errorblock(us, blk);
1404 ms_lib_setacquired_errorblock(us, blk);
1408 switch (ms_lib_erase_phyblock(us, blk)) {
1415 ms_lib_error_phyblock(us, blk);
1423 static int ms_libsearch_block_from_logical(
struct us_data *us,
u16 logblk)
1430 if (logblk >= info->
MS_Lib.NumberOfLogBlock)
1438 return ms_libsearch_block_from_physical(us, phyblk);
1441 static int ms_scsi_test_unit_ready(
struct us_data *us,
struct scsi_cmnd *srb)
1459 unsigned char data_ptr[36] = {
1460 0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
1461 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
1462 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
1463 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30};
1472 unsigned char mediaNoWP[12] = {
1473 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
1474 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1475 unsigned char mediaWP[12] = {
1476 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
1477 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1487 static int ms_scsi_read_capacity(
struct us_data *us,
struct scsi_cmnd *srb)
1491 unsigned int offset = 0;
1492 unsigned char buf[8];
1501 bl_num = info->
MS_Lib.NumberOfLogBlock * info->
MS_Lib.blockSize * 2 - 1;
1508 buf[0] = (bl_num >> 24) & 0xff;
1509 buf[1] = (bl_num >> 16) & 0xff;
1510 buf[2] = (bl_num >> 8) & 0xff;
1511 buf[3] = (bl_num >> 0) & 0xff;
1512 buf[4] = (bl_len >> 24) & 0xff;
1513 buf[5] = (bl_len >> 16) & 0xff;
1514 buf[6] = (bl_len >> 8) & 0xff;
1515 buf[7] = (bl_len >> 0) & 0xff;
1522 static void ms_lib_phy_to_log_range(
u16 PhyBlock,
u16 *LogStart,
u16 *LogEnde)
1535 static int ms_lib_read_extrablock(
struct us_data *us,
u32 PhyBlock,
1536 u8 PageNum,
u8 blen,
void *buf)
1551 bcb->
CDB[5] = (
unsigned char)(PageNum);
1552 bcb->
CDB[4] = (
unsigned char)(PhyBlock);
1553 bcb->
CDB[3] = (
unsigned char)(PhyBlock>>8);
1554 bcb->
CDB[2] = (
unsigned char)(PhyBlock>>16);
1557 result = ene_send_scsi_cmd(us,
FDIR_READ, buf, 0);
1564 static int ms_lib_scan_logicalblocknumber(
struct us_data *us,
u16 btBlk1st)
1566 u16 PhyBlock, newblk,
i;
1567 u16 LogStart, LogEnde;
1573 for (PhyBlock = 0; PhyBlock < info->
MS_Lib.NumberOfPhyBlock;) {
1574 ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde);
1584 if (count == PhyBlock) {
1585 ms_lib_read_extrablock(us, PhyBlock, 0, 0x80, &buf);
1588 index = (PhyBlock % 0x80) * 4;
1590 extdat.ovrflg = buf[
index];
1591 extdat.mngflg = buf[index+1];
1595 ms_lib_setacquired_errorblock(us, PhyBlock);
1600 ms_lib_erase_phyblock(us, PhyBlock);
1605 if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) {
1606 ms_lib_erase_phyblock(us, PhyBlock);
1613 if (extdat.logadr == 0) {
1614 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1615 if (ms_lib_check_disableblock(us, btBlk1st)) {
1616 ms_lib_set_logicalpair(us, extdat.logadr, newblk);
1621 ms_lib_read_extra(us, newblk, 0, &extdat);
1623 ms_lib_erase_phyblock(us, PhyBlock);
1626 ms_lib_erase_phyblock(us, newblk);
1630 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1642 unsigned char *cdb = srb->
cmnd;
1646 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
1647 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
1648 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1649 u32 blenByte = blen * 0x200;
1657 US_DEBUGP(
"Load MPS RW pattern Fail !!\n");
1668 bcb->
CDB[5] = (
unsigned char)(bn);
1669 bcb->
CDB[4] = (
unsigned char)(bn>>8);
1670 bcb->
CDB[3] = (
unsigned char)(bn>>16);
1671 bcb->
CDB[2] = (
unsigned char)(bn>>24);
1673 result = ene_send_scsi_cmd(us,
FDIR_READ, scsi_sglist(srb), 1);
1688 pr_info(
"Load MS RW pattern Fail !!\n");
1693 logblk = (
u16)(bn / info->
MS_Lib.PagesPerBlock);
1694 PageNum = (
u8)(bn % info->
MS_Lib.PagesPerBlock);
1697 if (blen > (info->
MS_Lib.PagesPerBlock-PageNum))
1698 len = info->
MS_Lib.PagesPerBlock-PageNum;
1703 blkno = phyblk * 0x20 + PageNum;
1712 bcb->
CDB[5] = (
unsigned char)(blkno);
1713 bcb->
CDB[4] = (
unsigned char)(blkno>>8);
1714 bcb->
CDB[3] = (
unsigned char)(blkno>>16);
1715 bcb->
CDB[2] = (
unsigned char)(blkno>>24);
1717 result = ene_send_scsi_cmd(us,
FDIR_READ, buf+offset, 0);
1719 pr_info(
"MS_SCSI_Read --- result = %x\n", result);
1729 offset += MS_BYTES_PER_PAGE*len;
1742 unsigned char *cdb = srb->
cmnd;
1745 u32 bn = ((cdb[2] << 24) & 0xff000000) |
1746 ((cdb[3] << 16) & 0x00ff0000) |
1747 ((cdb[4] << 8) & 0x0000ff00) |
1748 ((cdb[5] << 0) & 0x000000ff);
1749 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1750 u32 blenByte = blen * 0x200;
1758 pr_info(
"Load MSP RW pattern Fail !!\n");
1769 bcb->
CDB[5] = (
unsigned char)(bn);
1770 bcb->
CDB[4] = (
unsigned char)(bn>>8);
1771 bcb->
CDB[3] = (
unsigned char)(bn>>16);
1772 bcb->
CDB[2] = (
unsigned char)(bn>>24);
1774 result = ene_send_scsi_cmd(us,
FDIR_WRITE, scsi_sglist(srb), 1);
1780 u16 len, oldphy, newphy;
1789 pr_info(
"Load MS RW pattern Fail !!\n");
1794 PhyBlockAddr = (
u16)(bn / info->
MS_Lib.PagesPerBlock);
1795 PageNum = (
u8)(bn % info->
MS_Lib.PagesPerBlock);
1798 if (blen > (info->
MS_Lib.PagesPerBlock-PageNum))
1799 len = info->
MS_Lib.PagesPerBlock-PageNum;
1804 newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr);
1806 result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len);
1809 pr_info(
"MS_SCSI_Write --- result = %x\n", result);
1815 ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy);
1822 offset += MS_BYTES_PER_PAGE*len;
1834 static int ene_get_card_type(
struct us_data *us,
u16 index,
void *buf)
1844 bcb->
CDB[2] = (
unsigned char)(index>>8);
1845 bcb->
CDB[3] = (
unsigned char)index;
1847 result = ene_send_scsi_cmd(us,
FDIR_READ, buf, 0);
1851 static int ene_get_card_status(
struct us_data *us,
u8 *buf)
1858 reg4b = *(
u32 *)&buf[0x18];
1861 tmpreg = (
u16) reg4b;
1862 reg4b = *(
u32 *)(&buf[0x14]);
1864 info->
HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
1866 info->
SD_C_SIZE = ((tmpreg & 0x03) << 10) | (
u16)(reg4b >> 22);
1881 static int ene_load_bincode(
struct us_data *us,
unsigned char flag)
1885 unsigned char *buf =
NULL;
1922 US_DEBUGP(
"----------- Unknown PATTERN ----------\n");
1928 US_DEBUGP(
"load firmware %s failed\n", fw_name);
1933 US_DEBUGP(
"Malloc memory for fireware failed!\n");
1943 result = ene_send_scsi_cmd(us,
FDIR_WRITE, buf, 0);
1952 static int ms_card_init(
struct us_data *us)
1956 unsigned char *PageBuffer0 =
NULL, *PageBuffer1 =
NULL;
1958 u16 btBlk1st, btBlk2nd;
1964 ms_lib_free_allocatedarea(us);
1969 if ((PageBuffer0 ==
NULL) || (PageBuffer1 ==
NULL)) {
1979 switch (ms_read_readpage(us, TmpBlock, 0, (
u32 *)PageBuffer0, &extdat)) {
1999 btBlk2nd = TmpBlock;
2003 btBlk1st = TmpBlock;
2004 memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE);
2020 if (btBlk1stErred == 0)
2021 result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1);
2025 result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0);
2032 for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++)
2038 for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++)
2044 result = ms_lib_scan_logicalblocknumber(us, btBlk1st);
2048 for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT;
2049 TmpBlock < info->
MS_Lib.NumberOfPhyBlock;
2051 if (ms_count_freeblock(us, TmpBlock) == 0) {
2058 if (ms_lib_alloc_writebuf(us)) {
2073 static int ene_ms_init(
struct us_data *us)
2078 u16 MSP_BlockSize, MSP_UserAreaBlocks;
2098 result = ene_send_scsi_cmd(us,
FDIR_READ, &buf, 0);
2113 MSP_BlockSize = (buf[6] << 8) | buf[7];
2114 MSP_UserAreaBlocks = (buf[10] << 8) | buf[11];
2121 US_DEBUGP(
"MS Card Not Ready --- %x\n", buf[0]);
2128 static int ene_sd_init(
struct us_data *us)
2135 US_DEBUGP(
"transport --- ENE_SDInit\n");
2139 US_DEBUGP(
"Load SD Init Code Part-1 Fail !!\n");
2150 US_DEBUGP(
"Execution SD Init Code Fail !!\n");
2157 US_DEBUGP(
"Load SD Init Code Part-2 Fail !!\n");
2167 result = ene_send_scsi_cmd(us,
FDIR_READ, &buf, 0);
2169 US_DEBUGP(
"Execution SD Init Code Fail !!\n");
2175 ene_get_card_status(us, (
unsigned char *)&buf);
2183 US_DEBUGP(
"SD Card Not Ready --- %x\n", buf[0]);
2190 static int ene_init(
struct us_data *us)
2200 if (misc_reg03 & 0x01) {
2202 result = ene_sd_init(us);
2207 if (misc_reg03 & 0x02) {
2209 result = ene_ms_init(us);
2224 switch (srb->
cmnd[0]) {
2226 result = sd_scsi_test_unit_ready(us, srb);
2229 result = sd_scsi_inquiry(us, srb);
2232 result = sd_scsi_mode_sense(us, srb);
2240 result = sd_scsi_read_capacity(us, srb);
2243 result = sd_scsi_read(us, srb);
2246 result = sd_scsi_write(us, srb);
2264 switch (srb->
cmnd[0]) {
2266 result = ms_scsi_test_unit_ready(us, srb);
2269 result = ms_scsi_inquiry(us, srb);
2272 result = ms_scsi_mode_sense(us, srb);
2275 result = ms_scsi_read_capacity(us, srb);
2278 result = ms_scsi_read(us, srb);
2281 result = ms_scsi_write(us, srb);
2297 scsi_set_resid(srb, 0);
2299 result = ene_init(us);
2302 result = sd_scsi_irp(us, srb);
2305 result = ms_scsi_irp(us, srb);
2319 (
id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list);
2346 if (!(misc_reg03 & 0x01)) {
2347 pr_info(
"ums_eneub6250: The driver only supports SD/MS card. "
2348 "To use SM card, please build driver/staging/keucr\n");
2360 struct us_data *us = usb_get_intfdata(iface);
2366 if (us->suspend_resume_hook)
2367 (us->suspend_resume_hook)(us,
US_RESUME);
2383 struct us_data *us = usb_get_intfdata(iface);
2402 #define ene_ub6250_resume NULL
2403 #define ene_ub6250_reset_resume NULL
2407 static struct usb_driver ene_ub6250_driver = {
2408 .name =
"ums_eneub6250",
2409 .probe = ene_ub6250_probe,
2416 .id_table = ene_ub6250_usb_ids,