Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2 | #include <linux/jiffies.h> |
| 3 | #include <linux/errno.h> |
| 4 | #include <linux/module.h> |
| 5 | #include <linux/slab.h> |
| 6 | |
| 7 | #include <scsi/scsi.h> |
| 8 | #include <scsi/scsi_cmnd.h> |
| 9 | |
| 10 | #include <linux/firmware.h> |
| 11 | |
| 12 | #include "usb.h" |
| 13 | #include "transport.h" |
| 14 | #include "protocol.h" |
| 15 | #include "debug.h" |
Akinobu Mita | aa519be | 2015-05-06 18:24:21 +0900 | [diff] [blame] | 16 | #include "scsiglue.h" |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 17 | |
Tim Gardner | 595c897 | 2012-07-27 10:53:21 -0600 | [diff] [blame] | 18 | #define SD_INIT1_FIRMWARE "ene-ub6250/sd_init1.bin" |
| 19 | #define SD_INIT2_FIRMWARE "ene-ub6250/sd_init2.bin" |
| 20 | #define SD_RW_FIRMWARE "ene-ub6250/sd_rdwr.bin" |
| 21 | #define MS_INIT_FIRMWARE "ene-ub6250/ms_init.bin" |
| 22 | #define MSP_RW_FIRMWARE "ene-ub6250/msp_rdwr.bin" |
| 23 | #define MS_RW_FIRMWARE "ene-ub6250/ms_rdwr.bin" |
| 24 | |
Akinobu Mita | aa519be | 2015-05-06 18:24:21 +0900 | [diff] [blame] | 25 | #define DRV_NAME "ums_eneub6250" |
| 26 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 27 | MODULE_DESCRIPTION("Driver for ENE UB6250 reader"); |
| 28 | MODULE_LICENSE("GPL"); |
Tim Gardner | 595c897 | 2012-07-27 10:53:21 -0600 | [diff] [blame] | 29 | MODULE_FIRMWARE(SD_INIT1_FIRMWARE); |
| 30 | MODULE_FIRMWARE(SD_INIT2_FIRMWARE); |
| 31 | MODULE_FIRMWARE(SD_RW_FIRMWARE); |
| 32 | MODULE_FIRMWARE(MS_INIT_FIRMWARE); |
| 33 | MODULE_FIRMWARE(MSP_RW_FIRMWARE); |
| 34 | MODULE_FIRMWARE(MS_RW_FIRMWARE); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * The table of devices |
| 38 | */ |
| 39 | #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ |
| 40 | vendorName, productName, useProtocol, useTransport, \ |
| 41 | initFunction, flags) \ |
| 42 | { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ |
Sebastian Andrzej Siewior | f61870e | 2012-08-28 22:37:13 +0200 | [diff] [blame] | 43 | .driver_info = (flags)} |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 44 | |
Felipe Balbi | 36f3a14d | 2011-11-15 09:53:31 +0200 | [diff] [blame] | 45 | static struct usb_device_id ene_ub6250_usb_ids[] = { |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 46 | # include "unusual_ene_ub6250.h" |
| 47 | { } /* Terminating entry */ |
| 48 | }; |
| 49 | MODULE_DEVICE_TABLE(usb, ene_ub6250_usb_ids); |
| 50 | |
| 51 | #undef UNUSUAL_DEV |
| 52 | |
| 53 | /* |
| 54 | * The flags table |
| 55 | */ |
| 56 | #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \ |
| 57 | vendor_name, product_name, use_protocol, use_transport, \ |
| 58 | init_function, Flags) \ |
| 59 | { \ |
| 60 | .vendorName = vendor_name, \ |
| 61 | .productName = product_name, \ |
| 62 | .useProtocol = use_protocol, \ |
| 63 | .useTransport = use_transport, \ |
| 64 | .initFunction = init_function, \ |
| 65 | } |
| 66 | |
| 67 | static struct us_unusual_dev ene_ub6250_unusual_dev_list[] = { |
| 68 | # include "unusual_ene_ub6250.h" |
| 69 | { } /* Terminating entry */ |
| 70 | }; |
| 71 | |
| 72 | #undef UNUSUAL_DEV |
| 73 | |
| 74 | |
| 75 | |
| 76 | /* ENE bin code len */ |
| 77 | #define ENE_BIN_CODE_LEN 0x800 |
| 78 | /* EnE HW Register */ |
| 79 | #define REG_CARD_STATUS 0xFF83 |
| 80 | #define REG_HW_TRAP1 0xFF89 |
| 81 | |
| 82 | /* SRB Status */ |
Alan Stern | ce553bd | 2017-05-16 11:48:01 -0400 | [diff] [blame] | 83 | #define SS_SUCCESS 0x000000 /* No Sense */ |
| 84 | #define SS_NOT_READY 0x023A00 /* Medium not present */ |
| 85 | #define SS_MEDIUM_ERR 0x031100 /* Unrecovered read error */ |
| 86 | #define SS_HW_ERR 0x040800 /* Communication failure */ |
| 87 | #define SS_ILLEGAL_REQUEST 0x052000 /* Invalid command */ |
| 88 | #define SS_UNIT_ATTENTION 0x062900 /* Reset occurred */ |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 89 | |
| 90 | /* ENE Load FW Pattern */ |
| 91 | #define SD_INIT1_PATTERN 1 |
| 92 | #define SD_INIT2_PATTERN 2 |
| 93 | #define SD_RW_PATTERN 3 |
| 94 | #define MS_INIT_PATTERN 4 |
| 95 | #define MSP_RW_PATTERN 5 |
| 96 | #define MS_RW_PATTERN 6 |
| 97 | #define SM_INIT_PATTERN 7 |
| 98 | #define SM_RW_PATTERN 8 |
| 99 | |
| 100 | #define FDIR_WRITE 0 |
| 101 | #define FDIR_READ 1 |
| 102 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 103 | /* For MS Card */ |
| 104 | |
| 105 | /* Status Register 1 */ |
| 106 | #define MS_REG_ST1_MB 0x80 /* media busy */ |
| 107 | #define MS_REG_ST1_FB1 0x40 /* flush busy 1 */ |
| 108 | #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */ |
| 109 | #define MS_REG_ST1_UCDT 0x10 /* unable to correct data */ |
| 110 | #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */ |
| 111 | #define MS_REG_ST1_UCEX 0x04 /* unable to correct extra */ |
| 112 | #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */ |
| 113 | #define MS_REG_ST1_UCFG 0x01 /* unable to correct overwrite flag */ |
| 114 | #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) |
| 115 | |
| 116 | /* Overwrite Area */ |
| 117 | #define MS_REG_OVR_BKST 0x80 /* block status */ |
| 118 | #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */ |
| 119 | #define MS_REG_OVR_BKST_NG 0x00 /* NG */ |
| 120 | #define MS_REG_OVR_PGST0 0x40 /* page status */ |
| 121 | #define MS_REG_OVR_PGST1 0x20 |
| 122 | #define MS_REG_OVR_PGST_MASK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1) |
| 123 | #define MS_REG_OVR_PGST_OK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1) /* OK */ |
| 124 | #define MS_REG_OVR_PGST_NG MS_REG_OVR_PGST1 /* NG */ |
| 125 | #define MS_REG_OVR_PGST_DATA_ERROR 0x00 /* data error */ |
| 126 | #define MS_REG_OVR_UDST 0x10 /* update status */ |
| 127 | #define MS_REG_OVR_UDST_UPDATING 0x00 /* updating */ |
| 128 | #define MS_REG_OVR_UDST_NO_UPDATE MS_REG_OVR_UDST |
| 129 | #define MS_REG_OVR_RESERVED 0x08 |
| 130 | #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) |
| 131 | |
| 132 | /* Management Flag */ |
| 133 | #define MS_REG_MNG_SCMS0 0x20 /* serial copy management system */ |
| 134 | #define MS_REG_MNG_SCMS1 0x10 |
| 135 | #define MS_REG_MNG_SCMS_MASK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1) |
| 136 | #define MS_REG_MNG_SCMS_COPY_OK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1) |
| 137 | #define MS_REG_MNG_SCMS_ONE_COPY MS_REG_MNG_SCMS1 |
| 138 | #define MS_REG_MNG_SCMS_NO_COPY 0x00 |
| 139 | #define MS_REG_MNG_ATFLG 0x08 /* address transfer table flag */ |
| 140 | #define MS_REG_MNG_ATFLG_OTHER MS_REG_MNG_ATFLG /* other */ |
| 141 | #define MS_REG_MNG_ATFLG_ATTBL 0x00 /* address transfer table */ |
| 142 | #define MS_REG_MNG_SYSFLG 0x04 /* system flag */ |
| 143 | #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */ |
| 144 | #define MS_REG_MNG_SYSFLG_BOOT 0x00 /* system block */ |
| 145 | #define MS_REG_MNG_RESERVED 0xc3 |
| 146 | #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) |
| 147 | |
| 148 | |
| 149 | #define MS_MAX_PAGES_PER_BLOCK 32 |
| 150 | #define MS_MAX_INITIAL_ERROR_BLOCKS 10 |
| 151 | #define MS_LIB_BITS_PER_BYTE 8 |
| 152 | |
| 153 | #define MS_SYSINF_FORMAT_FAT 1 |
| 154 | #define MS_SYSINF_USAGE_GENERAL 0 |
| 155 | |
| 156 | #define MS_SYSINF_MSCLASS_TYPE_1 1 |
| 157 | #define MS_SYSINF_PAGE_SIZE MS_BYTES_PER_PAGE /* fixed */ |
| 158 | |
| 159 | #define MS_SYSINF_CARDTYPE_RDONLY 1 |
| 160 | #define MS_SYSINF_CARDTYPE_RDWR 2 |
| 161 | #define MS_SYSINF_CARDTYPE_HYBRID 3 |
| 162 | #define MS_SYSINF_SECURITY 0x01 |
| 163 | #define MS_SYSINF_SECURITY_NO_SUPPORT MS_SYSINF_SECURITY |
| 164 | #define MS_SYSINF_SECURITY_SUPPORT 0 |
| 165 | |
| 166 | #define MS_SYSINF_RESERVED1 1 |
| 167 | #define MS_SYSINF_RESERVED2 1 |
| 168 | |
| 169 | #define MS_SYSENT_TYPE_INVALID_BLOCK 0x01 |
| 170 | #define MS_SYSENT_TYPE_CIS_IDI 0x0a /* CIS/IDI */ |
| 171 | |
| 172 | #define SIZE_OF_KIRO 1024 |
| 173 | #define BYTE_MASK 0xff |
| 174 | |
| 175 | /* ms error code */ |
| 176 | #define MS_STATUS_WRITE_PROTECT 0x0106 |
| 177 | #define MS_STATUS_SUCCESS 0x0000 |
| 178 | #define MS_ERROR_FLASH_READ 0x8003 |
| 179 | #define MS_ERROR_FLASH_ERASE 0x8005 |
| 180 | #define MS_LB_ERROR 0xfff0 |
| 181 | #define MS_LB_BOOT_BLOCK 0xfff1 |
| 182 | #define MS_LB_INITIAL_ERROR 0xfff2 |
| 183 | #define MS_STATUS_SUCCESS_WITH_ECC 0xfff3 |
| 184 | #define MS_LB_ACQUIRED_ERROR 0xfff4 |
| 185 | #define MS_LB_NOT_USED_ERASED 0xfff5 |
| 186 | #define MS_NOCARD_ERROR 0xfff8 |
| 187 | #define MS_NO_MEMORY_ERROR 0xfff9 |
| 188 | #define MS_STATUS_INT_ERROR 0xfffa |
| 189 | #define MS_STATUS_ERROR 0xfffe |
| 190 | #define MS_LB_NOT_USED 0xffff |
| 191 | |
| 192 | #define MS_REG_MNG_SYSFLG 0x04 /* system flag */ |
| 193 | #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */ |
| 194 | |
| 195 | #define MS_BOOT_BLOCK_ID 0x0001 |
| 196 | #define MS_BOOT_BLOCK_FORMAT_VERSION 0x0100 |
| 197 | #define MS_BOOT_BLOCK_DATA_ENTRIES 2 |
| 198 | |
| 199 | #define MS_NUMBER_OF_SYSTEM_ENTRY 4 |
| 200 | #define MS_NUMBER_OF_BOOT_BLOCK 2 |
| 201 | #define MS_BYTES_PER_PAGE 512 |
| 202 | #define MS_LOGICAL_BLOCKS_PER_SEGMENT 496 |
| 203 | #define MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT 494 |
| 204 | |
| 205 | #define MS_PHYSICAL_BLOCKS_PER_SEGMENT 0x200 /* 512 */ |
| 206 | #define MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK 0x1ff |
| 207 | |
| 208 | /* overwrite area */ |
| 209 | #define MS_REG_OVR_BKST 0x80 /* block status */ |
| 210 | #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */ |
| 211 | #define MS_REG_OVR_BKST_NG 0x00 /* NG */ |
| 212 | |
| 213 | /* Status Register 1 */ |
| 214 | #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */ |
| 215 | #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */ |
| 216 | #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */ |
| 217 | |
| 218 | /* MemoryStick Register */ |
| 219 | /* Status Register 0 */ |
| 220 | #define MS_REG_ST0_WP 0x01 /* write protected */ |
| 221 | #define MS_REG_ST0_WP_ON MS_REG_ST0_WP |
| 222 | |
| 223 | #define MS_LIB_CTRL_RDONLY 0 |
| 224 | #define MS_LIB_CTRL_WRPROTECT 1 |
| 225 | |
| 226 | /*dphy->log table */ |
| 227 | #define ms_libconv_to_logical(pdx, PhyBlock) (((PhyBlock) >= (pdx)->MS_Lib.NumberOfPhyBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Phy2LogMap[PhyBlock]) |
| 228 | #define ms_libconv_to_physical(pdx, LogBlock) (((LogBlock) >= (pdx)->MS_Lib.NumberOfLogBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Log2PhyMap[LogBlock]) |
| 229 | |
| 230 | #define ms_lib_ctrl_set(pdx, Flag) ((pdx)->MS_Lib.flags |= (1 << (Flag))) |
| 231 | #define ms_lib_ctrl_reset(pdx, Flag) ((pdx)->MS_Lib.flags &= ~(1 << (Flag))) |
| 232 | #define ms_lib_ctrl_check(pdx, Flag) ((pdx)->MS_Lib.flags & (1 << (Flag))) |
| 233 | |
| 234 | #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)) |
| 235 | #define ms_lib_clear_pagemap(pdx) memset((pdx)->MS_Lib.pagemap, 0, sizeof((pdx)->MS_Lib.pagemap)) |
| 236 | #define memstick_logaddr(logadr1, logadr0) ((((u16)(logadr1)) << 8) | (logadr0)) |
| 237 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 238 | |
| 239 | struct SD_STATUS { |
| 240 | u8 Insert:1; |
| 241 | u8 Ready:1; |
| 242 | u8 MediaChange:1; |
| 243 | u8 IsMMC:1; |
| 244 | u8 HiCapacity:1; |
| 245 | u8 HiSpeed:1; |
| 246 | u8 WtP:1; |
| 247 | u8 Reserved:1; |
| 248 | }; |
| 249 | |
| 250 | struct MS_STATUS { |
| 251 | u8 Insert:1; |
| 252 | u8 Ready:1; |
| 253 | u8 MediaChange:1; |
| 254 | u8 IsMSPro:1; |
| 255 | u8 IsMSPHG:1; |
| 256 | u8 Reserved1:1; |
| 257 | u8 WtP:1; |
| 258 | u8 Reserved2:1; |
| 259 | }; |
| 260 | |
| 261 | struct SM_STATUS { |
| 262 | u8 Insert:1; |
| 263 | u8 Ready:1; |
| 264 | u8 MediaChange:1; |
| 265 | u8 Reserved:3; |
| 266 | u8 WtP:1; |
| 267 | u8 IsMS:1; |
| 268 | }; |
| 269 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 270 | struct ms_bootblock_cis { |
| 271 | u8 bCistplDEVICE[6]; /* 0 */ |
| 272 | u8 bCistplDEVICE0C[6]; /* 6 */ |
| 273 | u8 bCistplJEDECC[4]; /* 12 */ |
| 274 | u8 bCistplMANFID[6]; /* 16 */ |
| 275 | u8 bCistplVER1[32]; /* 22 */ |
| 276 | u8 bCistplFUNCID[4]; /* 54 */ |
| 277 | u8 bCistplFUNCE0[4]; /* 58 */ |
| 278 | u8 bCistplFUNCE1[5]; /* 62 */ |
| 279 | u8 bCistplCONF[7]; /* 67 */ |
| 280 | u8 bCistplCFTBLENT0[10];/* 74 */ |
| 281 | u8 bCistplCFTBLENT1[8]; /* 84 */ |
| 282 | u8 bCistplCFTBLENT2[12];/* 92 */ |
| 283 | u8 bCistplCFTBLENT3[8]; /* 104 */ |
| 284 | u8 bCistplCFTBLENT4[17];/* 112 */ |
| 285 | u8 bCistplCFTBLENT5[8]; /* 129 */ |
| 286 | u8 bCistplCFTBLENT6[17];/* 137 */ |
| 287 | u8 bCistplCFTBLENT7[8]; /* 154 */ |
| 288 | u8 bCistplNOLINK[3]; /* 162 */ |
| 289 | } ; |
| 290 | |
| 291 | struct ms_bootblock_idi { |
| 292 | #define MS_IDI_GENERAL_CONF 0x848A |
| 293 | u16 wIDIgeneralConfiguration; /* 0 */ |
| 294 | u16 wIDInumberOfCylinder; /* 1 */ |
| 295 | u16 wIDIreserved0; /* 2 */ |
| 296 | u16 wIDInumberOfHead; /* 3 */ |
| 297 | u16 wIDIbytesPerTrack; /* 4 */ |
| 298 | u16 wIDIbytesPerSector; /* 5 */ |
| 299 | u16 wIDIsectorsPerTrack; /* 6 */ |
| 300 | u16 wIDItotalSectors[2]; /* 7-8 high,low */ |
| 301 | u16 wIDIreserved1[11]; /* 9-19 */ |
| 302 | u16 wIDIbufferType; /* 20 */ |
| 303 | u16 wIDIbufferSize; /* 21 */ |
| 304 | u16 wIDIlongCmdECC; /* 22 */ |
| 305 | u16 wIDIfirmVersion[4]; /* 23-26 */ |
| 306 | u16 wIDImodelName[20]; /* 27-46 */ |
| 307 | u16 wIDIreserved2; /* 47 */ |
| 308 | u16 wIDIlongWordSupported; /* 48 */ |
| 309 | u16 wIDIdmaSupported; /* 49 */ |
| 310 | u16 wIDIreserved3; /* 50 */ |
| 311 | u16 wIDIpioTiming; /* 51 */ |
| 312 | u16 wIDIdmaTiming; /* 52 */ |
| 313 | u16 wIDItransferParameter; /* 53 */ |
| 314 | u16 wIDIformattedCylinder; /* 54 */ |
| 315 | u16 wIDIformattedHead; /* 55 */ |
| 316 | u16 wIDIformattedSectorsPerTrack;/* 56 */ |
| 317 | u16 wIDIformattedTotalSectors[2];/* 57-58 */ |
| 318 | u16 wIDImultiSector; /* 59 */ |
| 319 | u16 wIDIlbaSectors[2]; /* 60-61 */ |
| 320 | u16 wIDIsingleWordDMA; /* 62 */ |
| 321 | u16 wIDImultiWordDMA; /* 63 */ |
| 322 | u16 wIDIreserved4[192]; /* 64-255 */ |
| 323 | }; |
| 324 | |
| 325 | struct ms_bootblock_sysent_rec { |
| 326 | u32 dwStart; |
| 327 | u32 dwSize; |
| 328 | u8 bType; |
| 329 | u8 bReserved[3]; |
| 330 | }; |
| 331 | |
| 332 | struct ms_bootblock_sysent { |
| 333 | struct ms_bootblock_sysent_rec entry[MS_NUMBER_OF_SYSTEM_ENTRY]; |
| 334 | }; |
| 335 | |
| 336 | struct ms_bootblock_sysinf { |
| 337 | u8 bMsClass; /* must be 1 */ |
| 338 | u8 bCardType; /* see below */ |
| 339 | u16 wBlockSize; /* n KB */ |
| 340 | u16 wBlockNumber; /* number of physical block */ |
| 341 | u16 wTotalBlockNumber; /* number of logical block */ |
| 342 | u16 wPageSize; /* must be 0x200 */ |
| 343 | u8 bExtraSize; /* 0x10 */ |
| 344 | u8 bSecuritySupport; |
| 345 | u8 bAssemblyDate[8]; |
| 346 | u8 bFactoryArea[4]; |
| 347 | u8 bAssemblyMakerCode; |
| 348 | u8 bAssemblyMachineCode[3]; |
| 349 | u16 wMemoryMakerCode; |
| 350 | u16 wMemoryDeviceCode; |
| 351 | u16 wMemorySize; |
| 352 | u8 bReserved1; |
| 353 | u8 bReserved2; |
| 354 | u8 bVCC; |
| 355 | u8 bVPP; |
| 356 | u16 wControllerChipNumber; |
| 357 | u16 wControllerFunction; /* New MS */ |
| 358 | u8 bReserved3[9]; /* New MS */ |
| 359 | u8 bParallelSupport; /* New MS */ |
| 360 | u16 wFormatValue; /* New MS */ |
| 361 | u8 bFormatType; |
| 362 | u8 bUsage; |
| 363 | u8 bDeviceType; |
| 364 | u8 bReserved4[22]; |
| 365 | u8 bFUValue3; |
| 366 | u8 bFUValue4; |
| 367 | u8 bReserved5[15]; |
| 368 | }; |
| 369 | |
| 370 | struct ms_bootblock_header { |
| 371 | u16 wBlockID; |
| 372 | u16 wFormatVersion; |
| 373 | u8 bReserved1[184]; |
| 374 | u8 bNumberOfDataEntry; |
| 375 | u8 bReserved2[179]; |
| 376 | }; |
| 377 | |
| 378 | struct ms_bootblock_page0 { |
| 379 | struct ms_bootblock_header header; |
| 380 | struct ms_bootblock_sysent sysent; |
| 381 | struct ms_bootblock_sysinf sysinf; |
| 382 | }; |
| 383 | |
| 384 | struct ms_bootblock_cis_idi { |
| 385 | union { |
| 386 | struct ms_bootblock_cis cis; |
| 387 | u8 dmy[256]; |
| 388 | } cis; |
| 389 | |
| 390 | union { |
| 391 | struct ms_bootblock_idi idi; |
| 392 | u8 dmy[256]; |
| 393 | } idi; |
| 394 | |
| 395 | }; |
| 396 | |
| 397 | /* ENE MS Lib struct */ |
| 398 | struct ms_lib_type_extdat { |
| 399 | u8 reserved; |
| 400 | u8 intr; |
| 401 | u8 status0; |
| 402 | u8 status1; |
| 403 | u8 ovrflg; |
| 404 | u8 mngflg; |
| 405 | u16 logadr; |
| 406 | }; |
| 407 | |
| 408 | struct ms_lib_ctrl { |
| 409 | u32 flags; |
| 410 | u32 BytesPerSector; |
| 411 | u32 NumberOfCylinder; |
| 412 | u32 SectorsPerCylinder; |
| 413 | u16 cardType; /* R/W, RO, Hybrid */ |
| 414 | u16 blockSize; |
| 415 | u16 PagesPerBlock; |
| 416 | u16 NumberOfPhyBlock; |
| 417 | u16 NumberOfLogBlock; |
| 418 | u16 NumberOfSegment; |
| 419 | u16 *Phy2LogMap; /* phy2log table */ |
| 420 | u16 *Log2PhyMap; /* log2phy table */ |
| 421 | u16 wrtblk; |
| 422 | unsigned char *pagemap[(MS_MAX_PAGES_PER_BLOCK + (MS_LIB_BITS_PER_BYTE-1)) / MS_LIB_BITS_PER_BYTE]; |
| 423 | unsigned char *blkpag; |
| 424 | struct ms_lib_type_extdat *blkext; |
| 425 | unsigned char copybuf[512]; |
| 426 | }; |
| 427 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 428 | |
| 429 | /* SD Block Length */ |
| 430 | /* 2^9 = 512 Bytes, The HW maximum read/write data length */ |
| 431 | #define SD_BLOCK_LEN 9 |
| 432 | |
| 433 | struct ene_ub6250_info { |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 434 | |
| 435 | /* I/O bounce buffer */ |
| 436 | u8 *bbuf; |
| 437 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 438 | /* for 6250 code */ |
| 439 | struct SD_STATUS SD_Status; |
| 440 | struct MS_STATUS MS_Status; |
| 441 | struct SM_STATUS SM_Status; |
| 442 | |
| 443 | /* ----- SD Control Data ---------------- */ |
| 444 | /*SD_REGISTER SD_Regs; */ |
| 445 | u16 SD_Block_Mult; |
| 446 | u8 SD_READ_BL_LEN; |
| 447 | u16 SD_C_SIZE; |
| 448 | u8 SD_C_SIZE_MULT; |
| 449 | |
| 450 | /* SD/MMC New spec. */ |
| 451 | u8 SD_SPEC_VER; |
| 452 | u8 SD_CSD_VER; |
| 453 | u8 SD20_HIGH_CAPACITY; |
| 454 | u32 HC_C_SIZE; |
| 455 | u8 MMC_SPEC_VER; |
| 456 | u8 MMC_BusWidth; |
| 457 | u8 MMC_HIGH_CAPACITY; |
| 458 | |
| 459 | /*----- MS Control Data ---------------- */ |
| 460 | bool MS_SWWP; |
| 461 | u32 MSP_TotalBlock; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 462 | struct ms_lib_ctrl MS_Lib; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 463 | bool MS_IsRWPage; |
| 464 | u16 MS_Model; |
| 465 | |
| 466 | /*----- SM Control Data ---------------- */ |
| 467 | u8 SM_DeviceID; |
| 468 | u8 SM_CardID; |
| 469 | |
| 470 | unsigned char *testbuf; |
| 471 | u8 BIN_FLAG; |
| 472 | u32 bl_num; |
| 473 | int SrbStatus; |
| 474 | |
| 475 | /*------Power Managerment ---------------*/ |
| 476 | bool Power_IsResum; |
| 477 | }; |
| 478 | |
| 479 | static int ene_sd_init(struct us_data *us); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 480 | static int ene_ms_init(struct us_data *us); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 481 | static int ene_load_bincode(struct us_data *us, unsigned char flag); |
| 482 | |
| 483 | static void ene_ub6250_info_destructor(void *extra) |
| 484 | { |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 485 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) extra; |
| 486 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 487 | if (!extra) |
| 488 | return; |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 489 | kfree(info->bbuf); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static int ene_send_scsi_cmd(struct us_data *us, u8 fDir, void *buf, int use_sg) |
| 493 | { |
| 494 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 495 | struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf; |
| 496 | |
| 497 | int result; |
| 498 | unsigned int residue; |
| 499 | unsigned int cswlen = 0, partial = 0; |
| 500 | unsigned int transfer_length = bcb->DataTransferLength; |
| 501 | |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 502 | /* usb_stor_dbg(us, "transport --- ene_send_scsi_cmd\n"); */ |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 503 | /* send cmd to out endpoint */ |
| 504 | result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, |
| 505 | bcb, US_BULK_CB_WRAP_LEN, NULL); |
| 506 | if (result != USB_STOR_XFER_GOOD) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 507 | usb_stor_dbg(us, "send cmd to out endpoint fail ---\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 508 | return USB_STOR_TRANSPORT_ERROR; |
| 509 | } |
| 510 | |
| 511 | if (buf) { |
| 512 | unsigned int pipe = fDir; |
| 513 | |
| 514 | if (fDir == FDIR_READ) |
| 515 | pipe = us->recv_bulk_pipe; |
| 516 | else |
| 517 | pipe = us->send_bulk_pipe; |
| 518 | |
| 519 | /* Bulk */ |
| 520 | if (use_sg) { |
| 521 | result = usb_stor_bulk_srb(us, pipe, us->srb); |
| 522 | } else { |
| 523 | result = usb_stor_bulk_transfer_sg(us, pipe, buf, |
| 524 | transfer_length, 0, &partial); |
| 525 | } |
| 526 | if (result != USB_STOR_XFER_GOOD) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 527 | usb_stor_dbg(us, "data transfer fail ---\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 528 | return USB_STOR_TRANSPORT_ERROR; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /* Get CSW for device status */ |
| 533 | result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs, |
| 534 | US_BULK_CS_WRAP_LEN, &cswlen); |
| 535 | |
| 536 | if (result == USB_STOR_XFER_SHORT && cswlen == 0) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 537 | usb_stor_dbg(us, "Received 0-length CSW; retrying...\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 538 | result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, |
| 539 | bcs, US_BULK_CS_WRAP_LEN, &cswlen); |
| 540 | } |
| 541 | |
| 542 | if (result == USB_STOR_XFER_STALLED) { |
| 543 | /* get the status again */ |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 544 | usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 545 | result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, |
| 546 | bcs, US_BULK_CS_WRAP_LEN, NULL); |
| 547 | } |
| 548 | |
| 549 | if (result != USB_STOR_XFER_GOOD) |
| 550 | return USB_STOR_TRANSPORT_ERROR; |
| 551 | |
| 552 | /* check bulk status */ |
| 553 | residue = le32_to_cpu(bcs->Residue); |
| 554 | |
Felipe Balbi | f0183a3 | 2016-04-18 13:09:11 +0300 | [diff] [blame] | 555 | /* |
| 556 | * try to compute the actual residue, based on how much data |
| 557 | * was really transferred and what the device tells us |
| 558 | */ |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 559 | if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) { |
| 560 | residue = min(residue, transfer_length); |
| 561 | if (us->srb != NULL) |
| 562 | scsi_set_resid(us->srb, max(scsi_get_resid(us->srb), |
| 563 | (int)residue)); |
| 564 | } |
| 565 | |
| 566 | if (bcs->Status != US_BULK_STAT_OK) |
| 567 | return USB_STOR_TRANSPORT_ERROR; |
| 568 | |
| 569 | return USB_STOR_TRANSPORT_GOOD; |
| 570 | } |
| 571 | |
Alan Stern | ce553bd | 2017-05-16 11:48:01 -0400 | [diff] [blame] | 572 | static int do_scsi_request_sense(struct us_data *us, struct scsi_cmnd *srb) |
| 573 | { |
| 574 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 575 | unsigned char buf[18]; |
| 576 | |
| 577 | memset(buf, 0, 18); |
| 578 | buf[0] = 0x70; /* Current error */ |
| 579 | buf[2] = info->SrbStatus >> 16; /* Sense key */ |
| 580 | buf[7] = 10; /* Additional length */ |
| 581 | buf[12] = info->SrbStatus >> 8; /* ASC */ |
| 582 | buf[13] = info->SrbStatus; /* ASCQ */ |
| 583 | |
| 584 | usb_stor_set_xfer_buf(buf, sizeof(buf), srb); |
| 585 | return USB_STOR_TRANSPORT_GOOD; |
| 586 | } |
| 587 | |
Alan Stern | f8efdab | 2017-05-16 11:48:10 -0400 | [diff] [blame] | 588 | static int do_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb) |
| 589 | { |
| 590 | unsigned char data_ptr[36] = { |
Alan Stern | 5fcf937 | 2017-05-16 11:48:20 -0400 | [diff] [blame] | 591 | 0x00, 0x00, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55, |
Alan Stern | f8efdab | 2017-05-16 11:48:10 -0400 | [diff] [blame] | 592 | 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61, |
| 593 | 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20, |
| 594 | 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30 }; |
| 595 | |
| 596 | usb_stor_set_xfer_buf(data_ptr, 36, srb); |
| 597 | return USB_STOR_TRANSPORT_GOOD; |
| 598 | } |
| 599 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 600 | static int sd_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb) |
| 601 | { |
| 602 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 603 | |
| 604 | if (info->SD_Status.Insert && info->SD_Status.Ready) |
| 605 | return USB_STOR_TRANSPORT_GOOD; |
| 606 | else { |
| 607 | ene_sd_init(us); |
| 608 | return USB_STOR_TRANSPORT_GOOD; |
| 609 | } |
| 610 | |
| 611 | return USB_STOR_TRANSPORT_GOOD; |
| 612 | } |
| 613 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 614 | static int sd_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb) |
| 615 | { |
| 616 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 617 | unsigned char mediaNoWP[12] = { |
| 618 | 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00, |
| 619 | 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 }; |
| 620 | unsigned char mediaWP[12] = { |
| 621 | 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00, |
| 622 | 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 }; |
| 623 | |
| 624 | if (info->SD_Status.WtP) |
| 625 | usb_stor_set_xfer_buf(mediaWP, 12, srb); |
| 626 | else |
| 627 | usb_stor_set_xfer_buf(mediaNoWP, 12, srb); |
| 628 | |
| 629 | |
| 630 | return USB_STOR_TRANSPORT_GOOD; |
| 631 | } |
| 632 | |
| 633 | static int sd_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb) |
| 634 | { |
Felipe Balbi | 36f3a14d | 2011-11-15 09:53:31 +0200 | [diff] [blame] | 635 | u32 bl_num; |
| 636 | u32 bl_len; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 637 | unsigned int offset = 0; |
| 638 | unsigned char buf[8]; |
| 639 | struct scatterlist *sg = NULL; |
| 640 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 641 | |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 642 | usb_stor_dbg(us, "sd_scsi_read_capacity\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 643 | if (info->SD_Status.HiCapacity) { |
| 644 | bl_len = 0x200; |
| 645 | if (info->SD_Status.IsMMC) |
| 646 | bl_num = info->HC_C_SIZE-1; |
| 647 | else |
| 648 | bl_num = (info->HC_C_SIZE + 1) * 1024 - 1; |
| 649 | } else { |
Felipe Balbi | 36f3a14d | 2011-11-15 09:53:31 +0200 | [diff] [blame] | 650 | bl_len = 1 << (info->SD_READ_BL_LEN); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 651 | bl_num = info->SD_Block_Mult * (info->SD_C_SIZE + 1) |
| 652 | * (1 << (info->SD_C_SIZE_MULT + 2)) - 1; |
| 653 | } |
| 654 | info->bl_num = bl_num; |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 655 | usb_stor_dbg(us, "bl_len = %x\n", bl_len); |
| 656 | usb_stor_dbg(us, "bl_num = %x\n", bl_num); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 657 | |
| 658 | /*srb->request_bufflen = 8; */ |
| 659 | buf[0] = (bl_num >> 24) & 0xff; |
| 660 | buf[1] = (bl_num >> 16) & 0xff; |
| 661 | buf[2] = (bl_num >> 8) & 0xff; |
| 662 | buf[3] = (bl_num >> 0) & 0xff; |
| 663 | buf[4] = (bl_len >> 24) & 0xff; |
| 664 | buf[5] = (bl_len >> 16) & 0xff; |
| 665 | buf[6] = (bl_len >> 8) & 0xff; |
| 666 | buf[7] = (bl_len >> 0) & 0xff; |
| 667 | |
| 668 | usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF); |
| 669 | |
| 670 | return USB_STOR_TRANSPORT_GOOD; |
| 671 | } |
| 672 | |
| 673 | static int sd_scsi_read(struct us_data *us, struct scsi_cmnd *srb) |
| 674 | { |
| 675 | int result; |
| 676 | unsigned char *cdb = srb->cmnd; |
| 677 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 678 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 679 | |
| 680 | u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) | |
| 681 | ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff); |
| 682 | u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff); |
| 683 | u32 bnByte = bn * 0x200; |
| 684 | u32 blenByte = blen * 0x200; |
| 685 | |
| 686 | if (bn > info->bl_num) |
| 687 | return USB_STOR_TRANSPORT_ERROR; |
| 688 | |
| 689 | result = ene_load_bincode(us, SD_RW_PATTERN); |
| 690 | if (result != USB_STOR_XFER_GOOD) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 691 | usb_stor_dbg(us, "Load SD RW pattern Fail !!\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 692 | return USB_STOR_TRANSPORT_ERROR; |
| 693 | } |
| 694 | |
| 695 | if (info->SD_Status.HiCapacity) |
| 696 | bnByte = bn; |
| 697 | |
| 698 | /* set up the command wrapper */ |
| 699 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 700 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 701 | bcb->DataTransferLength = blenByte; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 702 | bcb->Flags = US_BULK_FLAG_IN; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 703 | bcb->CDB[0] = 0xF1; |
| 704 | bcb->CDB[5] = (unsigned char)(bnByte); |
| 705 | bcb->CDB[4] = (unsigned char)(bnByte>>8); |
| 706 | bcb->CDB[3] = (unsigned char)(bnByte>>16); |
| 707 | bcb->CDB[2] = (unsigned char)(bnByte>>24); |
| 708 | |
| 709 | result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1); |
| 710 | return result; |
| 711 | } |
| 712 | |
| 713 | static int sd_scsi_write(struct us_data *us, struct scsi_cmnd *srb) |
| 714 | { |
| 715 | int result; |
| 716 | unsigned char *cdb = srb->cmnd; |
| 717 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 718 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 719 | |
| 720 | u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) | |
| 721 | ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff); |
| 722 | u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff); |
| 723 | u32 bnByte = bn * 0x200; |
| 724 | u32 blenByte = blen * 0x200; |
| 725 | |
| 726 | if (bn > info->bl_num) |
| 727 | return USB_STOR_TRANSPORT_ERROR; |
| 728 | |
| 729 | result = ene_load_bincode(us, SD_RW_PATTERN); |
| 730 | if (result != USB_STOR_XFER_GOOD) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 731 | usb_stor_dbg(us, "Load SD RW pattern Fail !!\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 732 | return USB_STOR_TRANSPORT_ERROR; |
| 733 | } |
| 734 | |
| 735 | if (info->SD_Status.HiCapacity) |
| 736 | bnByte = bn; |
| 737 | |
| 738 | /* set up the command wrapper */ |
| 739 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 740 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 741 | bcb->DataTransferLength = blenByte; |
| 742 | bcb->Flags = 0x00; |
| 743 | bcb->CDB[0] = 0xF0; |
| 744 | bcb->CDB[5] = (unsigned char)(bnByte); |
| 745 | bcb->CDB[4] = (unsigned char)(bnByte>>8); |
| 746 | bcb->CDB[3] = (unsigned char)(bnByte>>16); |
| 747 | bcb->CDB[2] = (unsigned char)(bnByte>>24); |
| 748 | |
| 749 | result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1); |
| 750 | return result; |
| 751 | } |
| 752 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 753 | /* |
| 754 | * ENE MS Card |
| 755 | */ |
| 756 | |
| 757 | static int ms_lib_set_logicalpair(struct us_data *us, u16 logblk, u16 phyblk) |
| 758 | { |
| 759 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 760 | |
| 761 | if ((logblk >= info->MS_Lib.NumberOfLogBlock) || (phyblk >= info->MS_Lib.NumberOfPhyBlock)) |
| 762 | return (u32)-1; |
| 763 | |
| 764 | info->MS_Lib.Phy2LogMap[phyblk] = logblk; |
| 765 | info->MS_Lib.Log2PhyMap[logblk] = phyblk; |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | static int ms_lib_set_logicalblockmark(struct us_data *us, u16 phyblk, u16 mark) |
| 771 | { |
| 772 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 773 | |
| 774 | if (phyblk >= info->MS_Lib.NumberOfPhyBlock) |
| 775 | return (u32)-1; |
| 776 | |
| 777 | info->MS_Lib.Phy2LogMap[phyblk] = mark; |
| 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | static int ms_lib_set_initialerrorblock(struct us_data *us, u16 phyblk) |
| 783 | { |
| 784 | return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_INITIAL_ERROR); |
| 785 | } |
| 786 | |
| 787 | static int ms_lib_set_bootblockmark(struct us_data *us, u16 phyblk) |
| 788 | { |
| 789 | return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_BOOT_BLOCK); |
| 790 | } |
| 791 | |
| 792 | static int ms_lib_free_logicalmap(struct us_data *us) |
| 793 | { |
| 794 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 795 | |
| 796 | kfree(info->MS_Lib.Phy2LogMap); |
| 797 | info->MS_Lib.Phy2LogMap = NULL; |
| 798 | |
| 799 | kfree(info->MS_Lib.Log2PhyMap); |
| 800 | info->MS_Lib.Log2PhyMap = NULL; |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
Felipe Balbi | 36f3a14d | 2011-11-15 09:53:31 +0200 | [diff] [blame] | 805 | static int ms_lib_alloc_logicalmap(struct us_data *us) |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 806 | { |
| 807 | u32 i; |
| 808 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 809 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 810 | info->MS_Lib.Phy2LogMap = kmalloc_array(info->MS_Lib.NumberOfPhyBlock, |
| 811 | sizeof(u16), |
| 812 | GFP_KERNEL); |
| 813 | info->MS_Lib.Log2PhyMap = kmalloc_array(info->MS_Lib.NumberOfLogBlock, |
| 814 | sizeof(u16), |
| 815 | GFP_KERNEL); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 816 | |
| 817 | if ((info->MS_Lib.Phy2LogMap == NULL) || (info->MS_Lib.Log2PhyMap == NULL)) { |
| 818 | ms_lib_free_logicalmap(us); |
| 819 | return (u32)-1; |
| 820 | } |
| 821 | |
| 822 | for (i = 0; i < info->MS_Lib.NumberOfPhyBlock; i++) |
| 823 | info->MS_Lib.Phy2LogMap[i] = MS_LB_NOT_USED; |
| 824 | |
| 825 | for (i = 0; i < info->MS_Lib.NumberOfLogBlock; i++) |
| 826 | info->MS_Lib.Log2PhyMap[i] = MS_LB_NOT_USED; |
| 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | static void ms_lib_clear_writebuf(struct us_data *us) |
| 832 | { |
| 833 | int i; |
| 834 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 835 | |
| 836 | info->MS_Lib.wrtblk = (u16)-1; |
| 837 | ms_lib_clear_pagemap(info); |
| 838 | |
| 839 | if (info->MS_Lib.blkpag) |
| 840 | memset(info->MS_Lib.blkpag, 0xff, info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector); |
| 841 | |
| 842 | if (info->MS_Lib.blkext) { |
| 843 | for (i = 0; i < info->MS_Lib.PagesPerBlock; i++) { |
| 844 | info->MS_Lib.blkext[i].status1 = MS_REG_ST1_DEFAULT; |
| 845 | info->MS_Lib.blkext[i].ovrflg = MS_REG_OVR_DEFAULT; |
| 846 | info->MS_Lib.blkext[i].mngflg = MS_REG_MNG_DEFAULT; |
| 847 | info->MS_Lib.blkext[i].logadr = MS_LB_NOT_USED; |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | static int ms_count_freeblock(struct us_data *us, u16 PhyBlock) |
| 853 | { |
| 854 | u32 Ende, Count; |
| 855 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 856 | |
| 857 | Ende = PhyBlock + MS_PHYSICAL_BLOCKS_PER_SEGMENT; |
| 858 | for (Count = 0; PhyBlock < Ende; PhyBlock++) { |
| 859 | switch (info->MS_Lib.Phy2LogMap[PhyBlock]) { |
| 860 | case MS_LB_NOT_USED: |
| 861 | case MS_LB_NOT_USED_ERASED: |
| 862 | Count++; |
| 863 | default: |
| 864 | break; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | return Count; |
| 869 | } |
| 870 | |
| 871 | static int ms_read_readpage(struct us_data *us, u32 PhyBlockAddr, |
| 872 | u8 PageNum, u32 *PageBuf, struct ms_lib_type_extdat *ExtraDat) |
| 873 | { |
| 874 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 875 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 876 | u8 *bbuf = info->bbuf; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 877 | int result; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 878 | u32 bn = PhyBlockAddr * 0x20 + PageNum; |
| 879 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 880 | result = ene_load_bincode(us, MS_RW_PATTERN); |
| 881 | if (result != USB_STOR_XFER_GOOD) |
| 882 | return USB_STOR_TRANSPORT_ERROR; |
| 883 | |
| 884 | /* Read Page Data */ |
| 885 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 886 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 887 | bcb->DataTransferLength = 0x200; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 888 | bcb->Flags = US_BULK_FLAG_IN; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 889 | bcb->CDB[0] = 0xF1; |
| 890 | |
| 891 | bcb->CDB[1] = 0x02; /* in init.c ENE_MSInit() is 0x01 */ |
| 892 | |
| 893 | bcb->CDB[5] = (unsigned char)(bn); |
| 894 | bcb->CDB[4] = (unsigned char)(bn>>8); |
| 895 | bcb->CDB[3] = (unsigned char)(bn>>16); |
| 896 | bcb->CDB[2] = (unsigned char)(bn>>24); |
| 897 | |
| 898 | result = ene_send_scsi_cmd(us, FDIR_READ, PageBuf, 0); |
| 899 | if (result != USB_STOR_XFER_GOOD) |
| 900 | return USB_STOR_TRANSPORT_ERROR; |
| 901 | |
| 902 | |
| 903 | /* Read Extra Data */ |
| 904 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 905 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 906 | bcb->DataTransferLength = 0x4; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 907 | bcb->Flags = US_BULK_FLAG_IN; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 908 | bcb->CDB[0] = 0xF1; |
| 909 | bcb->CDB[1] = 0x03; |
| 910 | |
| 911 | bcb->CDB[5] = (unsigned char)(PageNum); |
| 912 | bcb->CDB[4] = (unsigned char)(PhyBlockAddr); |
| 913 | bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8); |
| 914 | bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16); |
| 915 | bcb->CDB[6] = 0x01; |
| 916 | |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 917 | result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 918 | if (result != USB_STOR_XFER_GOOD) |
| 919 | return USB_STOR_TRANSPORT_ERROR; |
| 920 | |
| 921 | ExtraDat->reserved = 0; |
| 922 | ExtraDat->intr = 0x80; /* Not yet,fireware support */ |
| 923 | ExtraDat->status0 = 0x10; /* Not yet,fireware support */ |
| 924 | |
| 925 | ExtraDat->status1 = 0x00; /* Not yet,fireware support */ |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 926 | ExtraDat->ovrflg = bbuf[0]; |
| 927 | ExtraDat->mngflg = bbuf[1]; |
| 928 | ExtraDat->logadr = memstick_logaddr(bbuf[2], bbuf[3]); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 929 | |
| 930 | return USB_STOR_TRANSPORT_GOOD; |
| 931 | } |
| 932 | |
| 933 | static int ms_lib_process_bootblock(struct us_data *us, u16 PhyBlock, u8 *PageData) |
| 934 | { |
| 935 | struct ms_bootblock_sysent *SysEntry; |
| 936 | struct ms_bootblock_sysinf *SysInfo; |
| 937 | u32 i, result; |
| 938 | u8 PageNumber; |
| 939 | u8 *PageBuffer; |
| 940 | struct ms_lib_type_extdat ExtraData; |
| 941 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 942 | |
| 943 | PageBuffer = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL); |
| 944 | if (PageBuffer == NULL) |
| 945 | return (u32)-1; |
| 946 | |
| 947 | result = (u32)-1; |
| 948 | |
| 949 | SysInfo = &(((struct ms_bootblock_page0 *)PageData)->sysinf); |
| 950 | |
| 951 | if ((SysInfo->bMsClass != MS_SYSINF_MSCLASS_TYPE_1) || |
| 952 | (be16_to_cpu(SysInfo->wPageSize) != MS_SYSINF_PAGE_SIZE) || |
| 953 | ((SysInfo->bSecuritySupport & MS_SYSINF_SECURITY) == MS_SYSINF_SECURITY_SUPPORT) || |
| 954 | (SysInfo->bReserved1 != MS_SYSINF_RESERVED1) || |
| 955 | (SysInfo->bReserved2 != MS_SYSINF_RESERVED2) || |
| 956 | (SysInfo->bFormatType != MS_SYSINF_FORMAT_FAT) || |
| 957 | (SysInfo->bUsage != MS_SYSINF_USAGE_GENERAL)) |
| 958 | goto exit; |
| 959 | /* */ |
| 960 | switch (info->MS_Lib.cardType = SysInfo->bCardType) { |
| 961 | case MS_SYSINF_CARDTYPE_RDONLY: |
| 962 | ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY); |
| 963 | break; |
| 964 | case MS_SYSINF_CARDTYPE_RDWR: |
| 965 | ms_lib_ctrl_reset(info, MS_LIB_CTRL_RDONLY); |
| 966 | break; |
| 967 | case MS_SYSINF_CARDTYPE_HYBRID: |
| 968 | default: |
| 969 | goto exit; |
| 970 | } |
| 971 | |
| 972 | info->MS_Lib.blockSize = be16_to_cpu(SysInfo->wBlockSize); |
| 973 | info->MS_Lib.NumberOfPhyBlock = be16_to_cpu(SysInfo->wBlockNumber); |
| 974 | info->MS_Lib.NumberOfLogBlock = be16_to_cpu(SysInfo->wTotalBlockNumber)-2; |
| 975 | info->MS_Lib.PagesPerBlock = info->MS_Lib.blockSize * SIZE_OF_KIRO / MS_BYTES_PER_PAGE; |
| 976 | info->MS_Lib.NumberOfSegment = info->MS_Lib.NumberOfPhyBlock / MS_PHYSICAL_BLOCKS_PER_SEGMENT; |
| 977 | info->MS_Model = be16_to_cpu(SysInfo->wMemorySize); |
| 978 | |
| 979 | /*Allocate to all number of logicalblock and physicalblock */ |
| 980 | if (ms_lib_alloc_logicalmap(us)) |
| 981 | goto exit; |
| 982 | |
| 983 | /* Mark the book block */ |
| 984 | ms_lib_set_bootblockmark(us, PhyBlock); |
| 985 | |
| 986 | SysEntry = &(((struct ms_bootblock_page0 *)PageData)->sysent); |
| 987 | |
| 988 | for (i = 0; i < MS_NUMBER_OF_SYSTEM_ENTRY; i++) { |
| 989 | u32 EntryOffset, EntrySize; |
| 990 | |
| 991 | EntryOffset = be32_to_cpu(SysEntry->entry[i].dwStart); |
| 992 | |
| 993 | if (EntryOffset == 0xffffff) |
| 994 | continue; |
| 995 | EntrySize = be32_to_cpu(SysEntry->entry[i].dwSize); |
| 996 | |
| 997 | if (EntrySize == 0) |
| 998 | continue; |
| 999 | |
| 1000 | if (EntryOffset + MS_BYTES_PER_PAGE + EntrySize > info->MS_Lib.blockSize * (u32)SIZE_OF_KIRO) |
| 1001 | continue; |
| 1002 | |
| 1003 | if (i == 0) { |
| 1004 | u8 PrevPageNumber = 0; |
| 1005 | u16 phyblk; |
| 1006 | |
| 1007 | if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_INVALID_BLOCK) |
| 1008 | goto exit; |
| 1009 | |
| 1010 | while (EntrySize > 0) { |
| 1011 | |
| 1012 | PageNumber = (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1); |
| 1013 | if (PageNumber != PrevPageNumber) { |
| 1014 | switch (ms_read_readpage(us, PhyBlock, PageNumber, (u32 *)PageBuffer, &ExtraData)) { |
| 1015 | case MS_STATUS_SUCCESS: |
| 1016 | break; |
| 1017 | case MS_STATUS_WRITE_PROTECT: |
| 1018 | case MS_ERROR_FLASH_READ: |
| 1019 | case MS_STATUS_ERROR: |
| 1020 | default: |
| 1021 | goto exit; |
| 1022 | } |
| 1023 | |
| 1024 | PrevPageNumber = PageNumber; |
| 1025 | } |
| 1026 | |
| 1027 | phyblk = be16_to_cpu(*(u16 *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE))); |
| 1028 | if (phyblk < 0x0fff) |
| 1029 | ms_lib_set_initialerrorblock(us, phyblk); |
| 1030 | |
| 1031 | EntryOffset += 2; |
| 1032 | EntrySize -= 2; |
| 1033 | } |
| 1034 | } else if (i == 1) { /* CIS/IDI */ |
| 1035 | struct ms_bootblock_idi *idi; |
| 1036 | |
| 1037 | if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_CIS_IDI) |
| 1038 | goto exit; |
| 1039 | |
| 1040 | switch (ms_read_readpage(us, PhyBlock, (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1), (u32 *)PageBuffer, &ExtraData)) { |
| 1041 | case MS_STATUS_SUCCESS: |
| 1042 | break; |
| 1043 | case MS_STATUS_WRITE_PROTECT: |
| 1044 | case MS_ERROR_FLASH_READ: |
| 1045 | case MS_STATUS_ERROR: |
| 1046 | default: |
| 1047 | goto exit; |
| 1048 | } |
| 1049 | |
| 1050 | idi = &((struct ms_bootblock_cis_idi *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)))->idi.idi; |
| 1051 | if (le16_to_cpu(idi->wIDIgeneralConfiguration) != MS_IDI_GENERAL_CONF) |
| 1052 | goto exit; |
| 1053 | |
| 1054 | info->MS_Lib.BytesPerSector = le16_to_cpu(idi->wIDIbytesPerSector); |
| 1055 | if (info->MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE) |
| 1056 | goto exit; |
| 1057 | } |
| 1058 | } /* End for .. */ |
| 1059 | |
| 1060 | result = 0; |
| 1061 | |
| 1062 | exit: |
| 1063 | if (result) |
| 1064 | ms_lib_free_logicalmap(us); |
| 1065 | |
| 1066 | kfree(PageBuffer); |
| 1067 | |
| 1068 | result = 0; |
| 1069 | return result; |
| 1070 | } |
| 1071 | |
| 1072 | static void ms_lib_free_writebuf(struct us_data *us) |
| 1073 | { |
| 1074 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1075 | info->MS_Lib.wrtblk = (u16)-1; /* set to -1 */ |
| 1076 | |
| 1077 | /* memset((fdoExt)->MS_Lib.pagemap, 0, sizeof((fdoExt)->MS_Lib.pagemap)) */ |
| 1078 | |
| 1079 | ms_lib_clear_pagemap(info); /* (pdx)->MS_Lib.pagemap memset 0 in ms.h */ |
| 1080 | |
| 1081 | if (info->MS_Lib.blkpag) { |
Amitoj Kaur Chawla | e6533e8 | 2016-01-27 00:12:23 +0530 | [diff] [blame] | 1082 | kfree(info->MS_Lib.blkpag); /* Arnold test ... */ |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1083 | info->MS_Lib.blkpag = NULL; |
| 1084 | } |
| 1085 | |
| 1086 | if (info->MS_Lib.blkext) { |
Amitoj Kaur Chawla | e6533e8 | 2016-01-27 00:12:23 +0530 | [diff] [blame] | 1087 | kfree(info->MS_Lib.blkext); /* Arnold test ... */ |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1088 | info->MS_Lib.blkext = NULL; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | static void ms_lib_free_allocatedarea(struct us_data *us) |
| 1094 | { |
| 1095 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1096 | |
| 1097 | ms_lib_free_writebuf(us); /* Free MS_Lib.pagemap */ |
| 1098 | ms_lib_free_logicalmap(us); /* kfree MS_Lib.Phy2LogMap and MS_Lib.Log2PhyMap */ |
| 1099 | |
| 1100 | /* set struct us point flag to 0 */ |
| 1101 | info->MS_Lib.flags = 0; |
| 1102 | info->MS_Lib.BytesPerSector = 0; |
| 1103 | info->MS_Lib.SectorsPerCylinder = 0; |
| 1104 | |
| 1105 | info->MS_Lib.cardType = 0; |
| 1106 | info->MS_Lib.blockSize = 0; |
| 1107 | info->MS_Lib.PagesPerBlock = 0; |
| 1108 | |
| 1109 | info->MS_Lib.NumberOfPhyBlock = 0; |
| 1110 | info->MS_Lib.NumberOfLogBlock = 0; |
| 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | static int ms_lib_alloc_writebuf(struct us_data *us) |
| 1115 | { |
| 1116 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1117 | |
| 1118 | info->MS_Lib.wrtblk = (u16)-1; |
| 1119 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1120 | info->MS_Lib.blkpag = kmalloc_array(info->MS_Lib.PagesPerBlock, |
| 1121 | info->MS_Lib.BytesPerSector, |
| 1122 | GFP_KERNEL); |
| 1123 | info->MS_Lib.blkext = kmalloc_array(info->MS_Lib.PagesPerBlock, |
| 1124 | sizeof(struct ms_lib_type_extdat), |
| 1125 | GFP_KERNEL); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1126 | |
| 1127 | if ((info->MS_Lib.blkpag == NULL) || (info->MS_Lib.blkext == NULL)) { |
| 1128 | ms_lib_free_writebuf(us); |
| 1129 | return (u32)-1; |
| 1130 | } |
| 1131 | |
| 1132 | ms_lib_clear_writebuf(us); |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | static int ms_lib_force_setlogical_pair(struct us_data *us, u16 logblk, u16 phyblk) |
| 1138 | { |
| 1139 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1140 | |
| 1141 | if (logblk == MS_LB_NOT_USED) |
| 1142 | return 0; |
| 1143 | |
| 1144 | if ((logblk >= info->MS_Lib.NumberOfLogBlock) || |
| 1145 | (phyblk >= info->MS_Lib.NumberOfPhyBlock)) |
| 1146 | return (u32)-1; |
| 1147 | |
| 1148 | info->MS_Lib.Phy2LogMap[phyblk] = logblk; |
| 1149 | info->MS_Lib.Log2PhyMap[logblk] = phyblk; |
| 1150 | |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | static int ms_read_copyblock(struct us_data *us, u16 oldphy, u16 newphy, |
| 1155 | u16 PhyBlockAddr, u8 PageNum, unsigned char *buf, u16 len) |
| 1156 | { |
| 1157 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 1158 | int result; |
| 1159 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1160 | result = ene_load_bincode(us, MS_RW_PATTERN); |
| 1161 | if (result != USB_STOR_XFER_GOOD) |
| 1162 | return USB_STOR_TRANSPORT_ERROR; |
| 1163 | |
| 1164 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 1165 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 1166 | bcb->DataTransferLength = 0x200*len; |
| 1167 | bcb->Flags = 0x00; |
| 1168 | bcb->CDB[0] = 0xF0; |
| 1169 | bcb->CDB[1] = 0x08; |
| 1170 | bcb->CDB[4] = (unsigned char)(oldphy); |
| 1171 | bcb->CDB[3] = (unsigned char)(oldphy>>8); |
| 1172 | bcb->CDB[2] = 0; /* (BYTE)(oldphy>>16) */ |
| 1173 | bcb->CDB[7] = (unsigned char)(newphy); |
| 1174 | bcb->CDB[6] = (unsigned char)(newphy>>8); |
| 1175 | bcb->CDB[5] = 0; /* (BYTE)(newphy>>16) */ |
| 1176 | bcb->CDB[9] = (unsigned char)(PhyBlockAddr); |
| 1177 | bcb->CDB[8] = (unsigned char)(PhyBlockAddr>>8); |
| 1178 | bcb->CDB[10] = PageNum; |
| 1179 | |
| 1180 | result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0); |
| 1181 | if (result != USB_STOR_XFER_GOOD) |
| 1182 | return USB_STOR_TRANSPORT_ERROR; |
| 1183 | |
| 1184 | return USB_STOR_TRANSPORT_GOOD; |
| 1185 | } |
| 1186 | |
| 1187 | static int ms_read_eraseblock(struct us_data *us, u32 PhyBlockAddr) |
| 1188 | { |
| 1189 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 1190 | int result; |
| 1191 | u32 bn = PhyBlockAddr; |
| 1192 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1193 | result = ene_load_bincode(us, MS_RW_PATTERN); |
| 1194 | if (result != USB_STOR_XFER_GOOD) |
| 1195 | return USB_STOR_TRANSPORT_ERROR; |
| 1196 | |
| 1197 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 1198 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 1199 | bcb->DataTransferLength = 0x200; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 1200 | bcb->Flags = US_BULK_FLAG_IN; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1201 | bcb->CDB[0] = 0xF2; |
| 1202 | bcb->CDB[1] = 0x06; |
| 1203 | bcb->CDB[4] = (unsigned char)(bn); |
| 1204 | bcb->CDB[3] = (unsigned char)(bn>>8); |
| 1205 | bcb->CDB[2] = (unsigned char)(bn>>16); |
| 1206 | |
| 1207 | result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0); |
| 1208 | if (result != USB_STOR_XFER_GOOD) |
| 1209 | return USB_STOR_TRANSPORT_ERROR; |
| 1210 | |
| 1211 | return USB_STOR_TRANSPORT_GOOD; |
| 1212 | } |
| 1213 | |
| 1214 | static int ms_lib_check_disableblock(struct us_data *us, u16 PhyBlock) |
| 1215 | { |
| 1216 | unsigned char *PageBuf = NULL; |
| 1217 | u16 result = MS_STATUS_SUCCESS; |
| 1218 | u16 blk, index = 0; |
| 1219 | struct ms_lib_type_extdat extdat; |
| 1220 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1221 | |
| 1222 | PageBuf = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL); |
| 1223 | if (PageBuf == NULL) { |
| 1224 | result = MS_NO_MEMORY_ERROR; |
| 1225 | goto exit; |
| 1226 | } |
| 1227 | |
| 1228 | ms_read_readpage(us, PhyBlock, 1, (u32 *)PageBuf, &extdat); |
| 1229 | do { |
| 1230 | blk = be16_to_cpu(PageBuf[index]); |
| 1231 | if (blk == MS_LB_NOT_USED) |
| 1232 | break; |
| 1233 | if (blk == info->MS_Lib.Log2PhyMap[0]) { |
| 1234 | result = MS_ERROR_FLASH_READ; |
| 1235 | break; |
| 1236 | } |
| 1237 | index++; |
| 1238 | } while (1); |
| 1239 | |
| 1240 | exit: |
| 1241 | kfree(PageBuf); |
| 1242 | return result; |
| 1243 | } |
| 1244 | |
| 1245 | static int ms_lib_setacquired_errorblock(struct us_data *us, u16 phyblk) |
| 1246 | { |
| 1247 | u16 log; |
| 1248 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1249 | |
| 1250 | if (phyblk >= info->MS_Lib.NumberOfPhyBlock) |
| 1251 | return (u32)-1; |
| 1252 | |
| 1253 | log = info->MS_Lib.Phy2LogMap[phyblk]; |
| 1254 | |
| 1255 | if (log < info->MS_Lib.NumberOfLogBlock) |
| 1256 | info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED; |
| 1257 | |
| 1258 | if (info->MS_Lib.Phy2LogMap[phyblk] != MS_LB_INITIAL_ERROR) |
| 1259 | info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_ACQUIRED_ERROR; |
| 1260 | |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | static int ms_lib_overwrite_extra(struct us_data *us, u32 PhyBlockAddr, |
| 1265 | u8 PageNum, u8 OverwriteFlag) |
| 1266 | { |
| 1267 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 1268 | int result; |
| 1269 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1270 | result = ene_load_bincode(us, MS_RW_PATTERN); |
| 1271 | if (result != USB_STOR_XFER_GOOD) |
| 1272 | return USB_STOR_TRANSPORT_ERROR; |
| 1273 | |
| 1274 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 1275 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 1276 | bcb->DataTransferLength = 0x4; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 1277 | bcb->Flags = US_BULK_FLAG_IN; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1278 | bcb->CDB[0] = 0xF2; |
| 1279 | bcb->CDB[1] = 0x05; |
| 1280 | bcb->CDB[5] = (unsigned char)(PageNum); |
| 1281 | bcb->CDB[4] = (unsigned char)(PhyBlockAddr); |
| 1282 | bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8); |
| 1283 | bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16); |
| 1284 | bcb->CDB[6] = OverwriteFlag; |
| 1285 | bcb->CDB[7] = 0xFF; |
| 1286 | bcb->CDB[8] = 0xFF; |
| 1287 | bcb->CDB[9] = 0xFF; |
| 1288 | |
| 1289 | result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0); |
| 1290 | if (result != USB_STOR_XFER_GOOD) |
| 1291 | return USB_STOR_TRANSPORT_ERROR; |
| 1292 | |
| 1293 | return USB_STOR_TRANSPORT_GOOD; |
| 1294 | } |
| 1295 | |
| 1296 | static int ms_lib_error_phyblock(struct us_data *us, u16 phyblk) |
| 1297 | { |
| 1298 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1299 | |
| 1300 | if (phyblk >= info->MS_Lib.NumberOfPhyBlock) |
| 1301 | return MS_STATUS_ERROR; |
| 1302 | |
| 1303 | ms_lib_setacquired_errorblock(us, phyblk); |
| 1304 | |
| 1305 | if (ms_lib_iswritable(info)) |
| 1306 | return ms_lib_overwrite_extra(us, phyblk, 0, (u8)(~MS_REG_OVR_BKST & BYTE_MASK)); |
| 1307 | |
| 1308 | return MS_STATUS_SUCCESS; |
| 1309 | } |
| 1310 | |
| 1311 | static int ms_lib_erase_phyblock(struct us_data *us, u16 phyblk) |
| 1312 | { |
| 1313 | u16 log; |
| 1314 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1315 | |
| 1316 | if (phyblk >= info->MS_Lib.NumberOfPhyBlock) |
| 1317 | return MS_STATUS_ERROR; |
| 1318 | |
| 1319 | log = info->MS_Lib.Phy2LogMap[phyblk]; |
| 1320 | |
| 1321 | if (log < info->MS_Lib.NumberOfLogBlock) |
| 1322 | info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED; |
| 1323 | |
| 1324 | info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED; |
| 1325 | |
| 1326 | if (ms_lib_iswritable(info)) { |
| 1327 | switch (ms_read_eraseblock(us, phyblk)) { |
| 1328 | case MS_STATUS_SUCCESS: |
| 1329 | info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED_ERASED; |
| 1330 | return MS_STATUS_SUCCESS; |
| 1331 | case MS_ERROR_FLASH_ERASE: |
| 1332 | case MS_STATUS_INT_ERROR: |
| 1333 | ms_lib_error_phyblock(us, phyblk); |
| 1334 | return MS_ERROR_FLASH_ERASE; |
| 1335 | case MS_STATUS_ERROR: |
| 1336 | default: |
| 1337 | ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY); /* MS_LibCtrlSet will used by ENE_MSInit ,need check, and why us to info*/ |
| 1338 | ms_lib_setacquired_errorblock(us, phyblk); |
| 1339 | return MS_STATUS_ERROR; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | ms_lib_setacquired_errorblock(us, phyblk); |
| 1344 | |
| 1345 | return MS_STATUS_SUCCESS; |
| 1346 | } |
| 1347 | |
| 1348 | static int ms_lib_read_extra(struct us_data *us, u32 PhyBlock, |
| 1349 | u8 PageNum, struct ms_lib_type_extdat *ExtraDat) |
| 1350 | { |
| 1351 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 1352 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1353 | u8 *bbuf = info->bbuf; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1354 | int result; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1355 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1356 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 1357 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 1358 | bcb->DataTransferLength = 0x4; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 1359 | bcb->Flags = US_BULK_FLAG_IN; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1360 | bcb->CDB[0] = 0xF1; |
| 1361 | bcb->CDB[1] = 0x03; |
| 1362 | bcb->CDB[5] = (unsigned char)(PageNum); |
| 1363 | bcb->CDB[4] = (unsigned char)(PhyBlock); |
| 1364 | bcb->CDB[3] = (unsigned char)(PhyBlock>>8); |
| 1365 | bcb->CDB[2] = (unsigned char)(PhyBlock>>16); |
| 1366 | bcb->CDB[6] = 0x01; |
| 1367 | |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 1368 | result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1369 | if (result != USB_STOR_XFER_GOOD) |
| 1370 | return USB_STOR_TRANSPORT_ERROR; |
| 1371 | |
| 1372 | ExtraDat->reserved = 0; |
| 1373 | ExtraDat->intr = 0x80; /* Not yet, waiting for fireware support */ |
| 1374 | ExtraDat->status0 = 0x10; /* Not yet, waiting for fireware support */ |
| 1375 | ExtraDat->status1 = 0x00; /* Not yet, waiting for fireware support */ |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 1376 | ExtraDat->ovrflg = bbuf[0]; |
| 1377 | ExtraDat->mngflg = bbuf[1]; |
| 1378 | ExtraDat->logadr = memstick_logaddr(bbuf[2], bbuf[3]); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1379 | |
| 1380 | return USB_STOR_TRANSPORT_GOOD; |
| 1381 | } |
| 1382 | |
| 1383 | static int ms_libsearch_block_from_physical(struct us_data *us, u16 phyblk) |
| 1384 | { |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1385 | u16 blk; |
| 1386 | struct ms_lib_type_extdat extdat; /* need check */ |
| 1387 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1388 | |
| 1389 | |
| 1390 | if (phyblk >= info->MS_Lib.NumberOfPhyBlock) |
| 1391 | return MS_LB_ERROR; |
| 1392 | |
| 1393 | for (blk = phyblk + 1; blk != phyblk; blk++) { |
| 1394 | if ((blk & MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK) == 0) |
| 1395 | blk -= MS_PHYSICAL_BLOCKS_PER_SEGMENT; |
| 1396 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1397 | if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED_ERASED) { |
| 1398 | return blk; |
| 1399 | } else if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED) { |
| 1400 | switch (ms_lib_read_extra(us, blk, 0, &extdat)) { |
| 1401 | case MS_STATUS_SUCCESS: |
| 1402 | case MS_STATUS_SUCCESS_WITH_ECC: |
| 1403 | break; |
| 1404 | case MS_NOCARD_ERROR: |
| 1405 | return MS_NOCARD_ERROR; |
| 1406 | case MS_STATUS_INT_ERROR: |
| 1407 | return MS_LB_ERROR; |
| 1408 | case MS_ERROR_FLASH_READ: |
| 1409 | default: |
| 1410 | ms_lib_setacquired_errorblock(us, blk); |
| 1411 | continue; |
| 1412 | } /* End switch */ |
| 1413 | |
| 1414 | if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) { |
| 1415 | ms_lib_setacquired_errorblock(us, blk); |
| 1416 | continue; |
| 1417 | } |
| 1418 | |
| 1419 | switch (ms_lib_erase_phyblock(us, blk)) { |
| 1420 | case MS_STATUS_SUCCESS: |
| 1421 | return blk; |
| 1422 | case MS_STATUS_ERROR: |
| 1423 | return MS_LB_ERROR; |
| 1424 | case MS_ERROR_FLASH_ERASE: |
| 1425 | default: |
| 1426 | ms_lib_error_phyblock(us, blk); |
| 1427 | break; |
| 1428 | } |
| 1429 | } |
| 1430 | } /* End for */ |
| 1431 | |
| 1432 | return MS_LB_ERROR; |
| 1433 | } |
| 1434 | static int ms_libsearch_block_from_logical(struct us_data *us, u16 logblk) |
| 1435 | { |
| 1436 | u16 phyblk; |
| 1437 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1438 | |
| 1439 | phyblk = ms_libconv_to_physical(info, logblk); |
| 1440 | if (phyblk >= MS_LB_ERROR) { |
| 1441 | if (logblk >= info->MS_Lib.NumberOfLogBlock) |
| 1442 | return MS_LB_ERROR; |
| 1443 | |
| 1444 | phyblk = (logblk + MS_NUMBER_OF_BOOT_BLOCK) / MS_LOGICAL_BLOCKS_PER_SEGMENT; |
| 1445 | phyblk *= MS_PHYSICAL_BLOCKS_PER_SEGMENT; |
| 1446 | phyblk += MS_PHYSICAL_BLOCKS_PER_SEGMENT - 1; |
| 1447 | } |
| 1448 | |
| 1449 | return ms_libsearch_block_from_physical(us, phyblk); |
| 1450 | } |
| 1451 | |
| 1452 | static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb) |
| 1453 | { |
| 1454 | struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra); |
| 1455 | |
| 1456 | /* pr_info("MS_SCSI_Test_Unit_Ready\n"); */ |
| 1457 | if (info->MS_Status.Insert && info->MS_Status.Ready) { |
| 1458 | return USB_STOR_TRANSPORT_GOOD; |
| 1459 | } else { |
| 1460 | ene_ms_init(us); |
| 1461 | return USB_STOR_TRANSPORT_GOOD; |
| 1462 | } |
| 1463 | |
| 1464 | return USB_STOR_TRANSPORT_GOOD; |
| 1465 | } |
| 1466 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1467 | static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb) |
| 1468 | { |
| 1469 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1470 | unsigned char mediaNoWP[12] = { |
| 1471 | 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00, |
| 1472 | 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 }; |
| 1473 | unsigned char mediaWP[12] = { |
| 1474 | 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00, |
| 1475 | 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 }; |
| 1476 | |
| 1477 | if (info->MS_Status.WtP) |
| 1478 | usb_stor_set_xfer_buf(mediaWP, 12, srb); |
| 1479 | else |
| 1480 | usb_stor_set_xfer_buf(mediaNoWP, 12, srb); |
| 1481 | |
| 1482 | return USB_STOR_TRANSPORT_GOOD; |
| 1483 | } |
| 1484 | |
| 1485 | static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb) |
| 1486 | { |
| 1487 | u32 bl_num; |
| 1488 | u16 bl_len; |
| 1489 | unsigned int offset = 0; |
| 1490 | unsigned char buf[8]; |
| 1491 | struct scatterlist *sg = NULL; |
| 1492 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1493 | |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1494 | usb_stor_dbg(us, "ms_scsi_read_capacity\n"); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1495 | bl_len = 0x200; |
| 1496 | if (info->MS_Status.IsMSPro) |
| 1497 | bl_num = info->MSP_TotalBlock - 1; |
| 1498 | else |
| 1499 | bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1; |
| 1500 | |
| 1501 | info->bl_num = bl_num; |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1502 | usb_stor_dbg(us, "bl_len = %x\n", bl_len); |
| 1503 | usb_stor_dbg(us, "bl_num = %x\n", bl_num); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1504 | |
| 1505 | /*srb->request_bufflen = 8; */ |
| 1506 | buf[0] = (bl_num >> 24) & 0xff; |
| 1507 | buf[1] = (bl_num >> 16) & 0xff; |
| 1508 | buf[2] = (bl_num >> 8) & 0xff; |
| 1509 | buf[3] = (bl_num >> 0) & 0xff; |
| 1510 | buf[4] = (bl_len >> 24) & 0xff; |
| 1511 | buf[5] = (bl_len >> 16) & 0xff; |
| 1512 | buf[6] = (bl_len >> 8) & 0xff; |
| 1513 | buf[7] = (bl_len >> 0) & 0xff; |
| 1514 | |
| 1515 | usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF); |
| 1516 | |
| 1517 | return USB_STOR_TRANSPORT_GOOD; |
| 1518 | } |
| 1519 | |
| 1520 | static void ms_lib_phy_to_log_range(u16 PhyBlock, u16 *LogStart, u16 *LogEnde) |
| 1521 | { |
| 1522 | PhyBlock /= MS_PHYSICAL_BLOCKS_PER_SEGMENT; |
| 1523 | |
| 1524 | if (PhyBlock) { |
| 1525 | *LogStart = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT + (PhyBlock - 1) * MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/ |
| 1526 | *LogEnde = *LogStart + MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/ |
| 1527 | } else { |
| 1528 | *LogStart = 0; |
| 1529 | *LogEnde = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT;/*494*/ |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | static int ms_lib_read_extrablock(struct us_data *us, u32 PhyBlock, |
| 1534 | u8 PageNum, u8 blen, void *buf) |
| 1535 | { |
| 1536 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 1537 | int result; |
| 1538 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1539 | /* Read Extra Data */ |
| 1540 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 1541 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 1542 | bcb->DataTransferLength = 0x4 * blen; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 1543 | bcb->Flags = US_BULK_FLAG_IN; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1544 | bcb->CDB[0] = 0xF1; |
| 1545 | bcb->CDB[1] = 0x03; |
| 1546 | bcb->CDB[5] = (unsigned char)(PageNum); |
| 1547 | bcb->CDB[4] = (unsigned char)(PhyBlock); |
| 1548 | bcb->CDB[3] = (unsigned char)(PhyBlock>>8); |
| 1549 | bcb->CDB[2] = (unsigned char)(PhyBlock>>16); |
| 1550 | bcb->CDB[6] = blen; |
| 1551 | |
| 1552 | result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0); |
| 1553 | if (result != USB_STOR_XFER_GOOD) |
| 1554 | return USB_STOR_TRANSPORT_ERROR; |
| 1555 | |
| 1556 | return USB_STOR_TRANSPORT_GOOD; |
| 1557 | } |
| 1558 | |
| 1559 | static int ms_lib_scan_logicalblocknumber(struct us_data *us, u16 btBlk1st) |
| 1560 | { |
| 1561 | u16 PhyBlock, newblk, i; |
| 1562 | u16 LogStart, LogEnde; |
| 1563 | struct ms_lib_type_extdat extdat; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1564 | u32 count = 0, index = 0; |
| 1565 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 1566 | u8 *bbuf = info->bbuf; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1567 | |
| 1568 | for (PhyBlock = 0; PhyBlock < info->MS_Lib.NumberOfPhyBlock;) { |
| 1569 | ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde); |
| 1570 | |
| 1571 | for (i = 0; i < MS_PHYSICAL_BLOCKS_PER_SEGMENT; i++, PhyBlock++) { |
| 1572 | switch (ms_libconv_to_logical(info, PhyBlock)) { |
| 1573 | case MS_STATUS_ERROR: |
| 1574 | continue; |
| 1575 | default: |
| 1576 | break; |
| 1577 | } |
| 1578 | |
| 1579 | if (count == PhyBlock) { |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 1580 | ms_lib_read_extrablock(us, PhyBlock, 0, 0x80, |
| 1581 | bbuf); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1582 | count += 0x80; |
| 1583 | } |
| 1584 | index = (PhyBlock % 0x80) * 4; |
| 1585 | |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 1586 | extdat.ovrflg = bbuf[index]; |
| 1587 | extdat.mngflg = bbuf[index+1]; |
| 1588 | extdat.logadr = memstick_logaddr(bbuf[index+2], |
| 1589 | bbuf[index+3]); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1590 | |
| 1591 | if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) { |
| 1592 | ms_lib_setacquired_errorblock(us, PhyBlock); |
| 1593 | continue; |
| 1594 | } |
| 1595 | |
| 1596 | if ((extdat.mngflg & MS_REG_MNG_ATFLG) == MS_REG_MNG_ATFLG_ATTBL) { |
| 1597 | ms_lib_erase_phyblock(us, PhyBlock); |
| 1598 | continue; |
| 1599 | } |
| 1600 | |
| 1601 | if (extdat.logadr != MS_LB_NOT_USED) { |
| 1602 | if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) { |
| 1603 | ms_lib_erase_phyblock(us, PhyBlock); |
| 1604 | continue; |
| 1605 | } |
| 1606 | |
| 1607 | newblk = ms_libconv_to_physical(info, extdat.logadr); |
| 1608 | |
| 1609 | if (newblk != MS_LB_NOT_USED) { |
| 1610 | if (extdat.logadr == 0) { |
| 1611 | ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock); |
| 1612 | if (ms_lib_check_disableblock(us, btBlk1st)) { |
| 1613 | ms_lib_set_logicalpair(us, extdat.logadr, newblk); |
| 1614 | continue; |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | ms_lib_read_extra(us, newblk, 0, &extdat); |
| 1619 | if ((extdat.ovrflg & MS_REG_OVR_UDST) == MS_REG_OVR_UDST_UPDATING) { |
| 1620 | ms_lib_erase_phyblock(us, PhyBlock); |
| 1621 | continue; |
| 1622 | } else { |
| 1623 | ms_lib_erase_phyblock(us, newblk); |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock); |
| 1628 | } |
| 1629 | } |
| 1630 | } /* End for ... */ |
| 1631 | |
| 1632 | return MS_STATUS_SUCCESS; |
| 1633 | } |
| 1634 | |
| 1635 | |
| 1636 | static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb) |
| 1637 | { |
| 1638 | int result; |
| 1639 | unsigned char *cdb = srb->cmnd; |
| 1640 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 1641 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1642 | |
| 1643 | u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) | |
| 1644 | ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff); |
| 1645 | u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff); |
| 1646 | u32 blenByte = blen * 0x200; |
| 1647 | |
| 1648 | if (bn > info->bl_num) |
| 1649 | return USB_STOR_TRANSPORT_ERROR; |
| 1650 | |
| 1651 | if (info->MS_Status.IsMSPro) { |
| 1652 | result = ene_load_bincode(us, MSP_RW_PATTERN); |
| 1653 | if (result != USB_STOR_XFER_GOOD) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1654 | usb_stor_dbg(us, "Load MPS RW pattern Fail !!\n"); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1655 | return USB_STOR_TRANSPORT_ERROR; |
| 1656 | } |
| 1657 | |
| 1658 | /* set up the command wrapper */ |
| 1659 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 1660 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 1661 | bcb->DataTransferLength = blenByte; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 1662 | bcb->Flags = US_BULK_FLAG_IN; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1663 | bcb->CDB[0] = 0xF1; |
| 1664 | bcb->CDB[1] = 0x02; |
| 1665 | bcb->CDB[5] = (unsigned char)(bn); |
| 1666 | bcb->CDB[4] = (unsigned char)(bn>>8); |
| 1667 | bcb->CDB[3] = (unsigned char)(bn>>16); |
| 1668 | bcb->CDB[2] = (unsigned char)(bn>>24); |
| 1669 | |
| 1670 | result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1); |
| 1671 | } else { |
| 1672 | void *buf; |
| 1673 | int offset = 0; |
| 1674 | u16 phyblk, logblk; |
| 1675 | u8 PageNum; |
| 1676 | u16 len; |
| 1677 | u32 blkno; |
| 1678 | |
| 1679 | buf = kmalloc(blenByte, GFP_KERNEL); |
| 1680 | if (buf == NULL) |
| 1681 | return USB_STOR_TRANSPORT_ERROR; |
| 1682 | |
| 1683 | result = ene_load_bincode(us, MS_RW_PATTERN); |
| 1684 | if (result != USB_STOR_XFER_GOOD) { |
| 1685 | pr_info("Load MS RW pattern Fail !!\n"); |
| 1686 | result = USB_STOR_TRANSPORT_ERROR; |
| 1687 | goto exit; |
| 1688 | } |
| 1689 | |
| 1690 | logblk = (u16)(bn / info->MS_Lib.PagesPerBlock); |
| 1691 | PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock); |
| 1692 | |
| 1693 | while (1) { |
| 1694 | if (blen > (info->MS_Lib.PagesPerBlock-PageNum)) |
| 1695 | len = info->MS_Lib.PagesPerBlock-PageNum; |
| 1696 | else |
| 1697 | len = blen; |
| 1698 | |
| 1699 | phyblk = ms_libconv_to_physical(info, logblk); |
| 1700 | blkno = phyblk * 0x20 + PageNum; |
| 1701 | |
| 1702 | /* set up the command wrapper */ |
| 1703 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 1704 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 1705 | bcb->DataTransferLength = 0x200 * len; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 1706 | bcb->Flags = US_BULK_FLAG_IN; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1707 | bcb->CDB[0] = 0xF1; |
| 1708 | bcb->CDB[1] = 0x02; |
| 1709 | bcb->CDB[5] = (unsigned char)(blkno); |
| 1710 | bcb->CDB[4] = (unsigned char)(blkno>>8); |
| 1711 | bcb->CDB[3] = (unsigned char)(blkno>>16); |
| 1712 | bcb->CDB[2] = (unsigned char)(blkno>>24); |
| 1713 | |
| 1714 | result = ene_send_scsi_cmd(us, FDIR_READ, buf+offset, 0); |
| 1715 | if (result != USB_STOR_XFER_GOOD) { |
| 1716 | pr_info("MS_SCSI_Read --- result = %x\n", result); |
| 1717 | result = USB_STOR_TRANSPORT_ERROR; |
| 1718 | goto exit; |
| 1719 | } |
| 1720 | |
| 1721 | blen -= len; |
| 1722 | if (blen <= 0) |
| 1723 | break; |
| 1724 | logblk++; |
| 1725 | PageNum = 0; |
| 1726 | offset += MS_BYTES_PER_PAGE*len; |
| 1727 | } |
| 1728 | usb_stor_set_xfer_buf(buf, blenByte, srb); |
| 1729 | exit: |
| 1730 | kfree(buf); |
| 1731 | } |
| 1732 | return result; |
| 1733 | } |
| 1734 | |
| 1735 | static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb) |
| 1736 | { |
| 1737 | int result; |
| 1738 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 1739 | unsigned char *cdb = srb->cmnd; |
| 1740 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1741 | |
| 1742 | u32 bn = ((cdb[2] << 24) & 0xff000000) | |
| 1743 | ((cdb[3] << 16) & 0x00ff0000) | |
| 1744 | ((cdb[4] << 8) & 0x0000ff00) | |
| 1745 | ((cdb[5] << 0) & 0x000000ff); |
| 1746 | u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff); |
| 1747 | u32 blenByte = blen * 0x200; |
| 1748 | |
| 1749 | if (bn > info->bl_num) |
| 1750 | return USB_STOR_TRANSPORT_ERROR; |
| 1751 | |
| 1752 | if (info->MS_Status.IsMSPro) { |
| 1753 | result = ene_load_bincode(us, MSP_RW_PATTERN); |
| 1754 | if (result != USB_STOR_XFER_GOOD) { |
| 1755 | pr_info("Load MSP RW pattern Fail !!\n"); |
| 1756 | return USB_STOR_TRANSPORT_ERROR; |
| 1757 | } |
| 1758 | |
| 1759 | /* set up the command wrapper */ |
| 1760 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 1761 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 1762 | bcb->DataTransferLength = blenByte; |
| 1763 | bcb->Flags = 0x00; |
| 1764 | bcb->CDB[0] = 0xF0; |
| 1765 | bcb->CDB[1] = 0x04; |
| 1766 | bcb->CDB[5] = (unsigned char)(bn); |
| 1767 | bcb->CDB[4] = (unsigned char)(bn>>8); |
| 1768 | bcb->CDB[3] = (unsigned char)(bn>>16); |
| 1769 | bcb->CDB[2] = (unsigned char)(bn>>24); |
| 1770 | |
| 1771 | result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1); |
| 1772 | } else { |
| 1773 | void *buf; |
Felipe Balbi | bc985c1 | 2011-11-15 09:23:26 +0200 | [diff] [blame] | 1774 | int offset = 0; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1775 | u16 PhyBlockAddr; |
| 1776 | u8 PageNum; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1777 | u16 len, oldphy, newphy; |
| 1778 | |
| 1779 | buf = kmalloc(blenByte, GFP_KERNEL); |
| 1780 | if (buf == NULL) |
| 1781 | return USB_STOR_TRANSPORT_ERROR; |
| 1782 | usb_stor_set_xfer_buf(buf, blenByte, srb); |
| 1783 | |
| 1784 | result = ene_load_bincode(us, MS_RW_PATTERN); |
| 1785 | if (result != USB_STOR_XFER_GOOD) { |
| 1786 | pr_info("Load MS RW pattern Fail !!\n"); |
| 1787 | result = USB_STOR_TRANSPORT_ERROR; |
| 1788 | goto exit; |
| 1789 | } |
| 1790 | |
| 1791 | PhyBlockAddr = (u16)(bn / info->MS_Lib.PagesPerBlock); |
| 1792 | PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock); |
| 1793 | |
| 1794 | while (1) { |
| 1795 | if (blen > (info->MS_Lib.PagesPerBlock-PageNum)) |
| 1796 | len = info->MS_Lib.PagesPerBlock-PageNum; |
| 1797 | else |
| 1798 | len = blen; |
| 1799 | |
| 1800 | oldphy = ms_libconv_to_physical(info, PhyBlockAddr); /* need check us <-> info */ |
| 1801 | newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr); |
| 1802 | |
| 1803 | result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len); |
| 1804 | |
| 1805 | if (result != USB_STOR_XFER_GOOD) { |
| 1806 | pr_info("MS_SCSI_Write --- result = %x\n", result); |
| 1807 | result = USB_STOR_TRANSPORT_ERROR; |
| 1808 | goto exit; |
| 1809 | } |
| 1810 | |
| 1811 | info->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED; |
| 1812 | ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy); |
| 1813 | |
| 1814 | blen -= len; |
| 1815 | if (blen <= 0) |
| 1816 | break; |
| 1817 | PhyBlockAddr++; |
| 1818 | PageNum = 0; |
| 1819 | offset += MS_BYTES_PER_PAGE*len; |
| 1820 | } |
| 1821 | exit: |
| 1822 | kfree(buf); |
| 1823 | } |
| 1824 | return result; |
| 1825 | } |
| 1826 | |
| 1827 | /* |
| 1828 | * ENE MS Card |
| 1829 | */ |
| 1830 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1831 | static int ene_get_card_type(struct us_data *us, u16 index, void *buf) |
| 1832 | { |
| 1833 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 1834 | int result; |
| 1835 | |
| 1836 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 1837 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 1838 | bcb->DataTransferLength = 0x01; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 1839 | bcb->Flags = US_BULK_FLAG_IN; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1840 | bcb->CDB[0] = 0xED; |
| 1841 | bcb->CDB[2] = (unsigned char)(index>>8); |
| 1842 | bcb->CDB[3] = (unsigned char)index; |
| 1843 | |
| 1844 | result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0); |
| 1845 | return result; |
| 1846 | } |
| 1847 | |
| 1848 | static int ene_get_card_status(struct us_data *us, u8 *buf) |
| 1849 | { |
| 1850 | u16 tmpreg; |
| 1851 | u32 reg4b; |
| 1852 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1853 | |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1854 | /*usb_stor_dbg(us, "transport --- ENE_ReadSDReg\n");*/ |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1855 | reg4b = *(u32 *)&buf[0x18]; |
| 1856 | info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f); |
| 1857 | |
| 1858 | tmpreg = (u16) reg4b; |
| 1859 | reg4b = *(u32 *)(&buf[0x14]); |
| 1860 | if (info->SD_Status.HiCapacity && !info->SD_Status.IsMMC) |
| 1861 | info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff; |
| 1862 | |
| 1863 | info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22); |
| 1864 | info->SD_C_SIZE_MULT = (u8)(reg4b >> 7) & 0x07; |
| 1865 | if (info->SD_Status.HiCapacity && info->SD_Status.IsMMC) |
| 1866 | info->HC_C_SIZE = *(u32 *)(&buf[0x100]); |
| 1867 | |
| 1868 | if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) { |
| 1869 | info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN); |
| 1870 | info->SD_READ_BL_LEN = SD_BLOCK_LEN; |
| 1871 | } else { |
| 1872 | info->SD_Block_Mult = 1; |
| 1873 | } |
| 1874 | |
| 1875 | return USB_STOR_TRANSPORT_GOOD; |
| 1876 | } |
| 1877 | |
| 1878 | static int ene_load_bincode(struct us_data *us, unsigned char flag) |
| 1879 | { |
| 1880 | int err; |
| 1881 | char *fw_name = NULL; |
| 1882 | unsigned char *buf = NULL; |
| 1883 | const struct firmware *sd_fw = NULL; |
| 1884 | int result = USB_STOR_TRANSPORT_ERROR; |
| 1885 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 1886 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1887 | |
| 1888 | if (info->BIN_FLAG == flag) |
| 1889 | return USB_STOR_TRANSPORT_GOOD; |
| 1890 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1891 | switch (flag) { |
| 1892 | /* For SD */ |
| 1893 | case SD_INIT1_PATTERN: |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1894 | usb_stor_dbg(us, "SD_INIT1_PATTERN\n"); |
Tim Gardner | 595c897 | 2012-07-27 10:53:21 -0600 | [diff] [blame] | 1895 | fw_name = SD_INIT1_FIRMWARE; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1896 | break; |
| 1897 | case SD_INIT2_PATTERN: |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1898 | usb_stor_dbg(us, "SD_INIT2_PATTERN\n"); |
Tim Gardner | 595c897 | 2012-07-27 10:53:21 -0600 | [diff] [blame] | 1899 | fw_name = SD_INIT2_FIRMWARE; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1900 | break; |
| 1901 | case SD_RW_PATTERN: |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1902 | usb_stor_dbg(us, "SD_RW_PATTERN\n"); |
Tim Gardner | 595c897 | 2012-07-27 10:53:21 -0600 | [diff] [blame] | 1903 | fw_name = SD_RW_FIRMWARE; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1904 | break; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1905 | /* For MS */ |
| 1906 | case MS_INIT_PATTERN: |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1907 | usb_stor_dbg(us, "MS_INIT_PATTERN\n"); |
Tim Gardner | 595c897 | 2012-07-27 10:53:21 -0600 | [diff] [blame] | 1908 | fw_name = MS_INIT_FIRMWARE; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1909 | break; |
| 1910 | case MSP_RW_PATTERN: |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1911 | usb_stor_dbg(us, "MSP_RW_PATTERN\n"); |
Tim Gardner | 595c897 | 2012-07-27 10:53:21 -0600 | [diff] [blame] | 1912 | fw_name = MSP_RW_FIRMWARE; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1913 | break; |
| 1914 | case MS_RW_PATTERN: |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1915 | usb_stor_dbg(us, "MS_RW_PATTERN\n"); |
Tim Gardner | 595c897 | 2012-07-27 10:53:21 -0600 | [diff] [blame] | 1916 | fw_name = MS_RW_FIRMWARE; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1917 | break; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1918 | default: |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1919 | usb_stor_dbg(us, "----------- Unknown PATTERN ----------\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1920 | goto nofw; |
| 1921 | } |
| 1922 | |
| 1923 | err = request_firmware(&sd_fw, fw_name, &us->pusb_dev->dev); |
| 1924 | if (err) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1925 | usb_stor_dbg(us, "load firmware %s failed\n", fw_name); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1926 | goto nofw; |
| 1927 | } |
Benoit Taine | a328512 | 2014-05-26 17:21:10 +0200 | [diff] [blame] | 1928 | buf = kmemdup(sd_fw->data, sd_fw->size, GFP_KERNEL); |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1929 | if (buf == NULL) |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1930 | goto nofw; |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 1931 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1932 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 1933 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 1934 | bcb->DataTransferLength = sd_fw->size; |
| 1935 | bcb->Flags = 0x00; |
| 1936 | bcb->CDB[0] = 0xEF; |
| 1937 | |
| 1938 | result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0); |
Alan Stern | aa18c4b6 | 2017-05-16 11:47:52 -0400 | [diff] [blame] | 1939 | if (us->srb != NULL) |
| 1940 | scsi_set_resid(us->srb, 0); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1941 | info->BIN_FLAG = flag; |
| 1942 | kfree(buf); |
| 1943 | |
| 1944 | nofw: |
Jesper Juhl | e44fabb | 2012-04-09 22:52:04 +0200 | [diff] [blame] | 1945 | release_firmware(sd_fw); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 1946 | return result; |
| 1947 | } |
| 1948 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 1949 | static int ms_card_init(struct us_data *us) |
| 1950 | { |
| 1951 | u32 result; |
| 1952 | u16 TmpBlock; |
| 1953 | unsigned char *PageBuffer0 = NULL, *PageBuffer1 = NULL; |
| 1954 | struct ms_lib_type_extdat extdat; |
| 1955 | u16 btBlk1st, btBlk2nd; |
| 1956 | u32 btBlk1stErred; |
| 1957 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
| 1958 | |
| 1959 | printk(KERN_INFO "MS_CardInit start\n"); |
| 1960 | |
| 1961 | ms_lib_free_allocatedarea(us); /* Clean buffer and set struct us_data flag to 0 */ |
| 1962 | |
| 1963 | /* get two PageBuffer */ |
| 1964 | PageBuffer0 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL); |
| 1965 | PageBuffer1 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL); |
| 1966 | if ((PageBuffer0 == NULL) || (PageBuffer1 == NULL)) { |
| 1967 | result = MS_NO_MEMORY_ERROR; |
| 1968 | goto exit; |
| 1969 | } |
| 1970 | |
| 1971 | btBlk1st = btBlk2nd = MS_LB_NOT_USED; |
| 1972 | btBlk1stErred = 0; |
| 1973 | |
| 1974 | for (TmpBlock = 0; TmpBlock < MS_MAX_INITIAL_ERROR_BLOCKS+2; TmpBlock++) { |
| 1975 | |
| 1976 | switch (ms_read_readpage(us, TmpBlock, 0, (u32 *)PageBuffer0, &extdat)) { |
| 1977 | case MS_STATUS_SUCCESS: |
| 1978 | break; |
| 1979 | case MS_STATUS_INT_ERROR: |
| 1980 | break; |
| 1981 | case MS_STATUS_ERROR: |
| 1982 | default: |
| 1983 | continue; |
| 1984 | } |
| 1985 | |
| 1986 | if ((extdat.ovrflg & MS_REG_OVR_BKST) == MS_REG_OVR_BKST_NG) |
| 1987 | continue; |
| 1988 | |
| 1989 | if (((extdat.mngflg & MS_REG_MNG_SYSFLG) == MS_REG_MNG_SYSFLG_USER) || |
| 1990 | (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wBlockID) != MS_BOOT_BLOCK_ID) || |
| 1991 | (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wFormatVersion) != MS_BOOT_BLOCK_FORMAT_VERSION) || |
| 1992 | (((struct ms_bootblock_page0 *)PageBuffer0)->header.bNumberOfDataEntry != MS_BOOT_BLOCK_DATA_ENTRIES)) |
| 1993 | continue; |
| 1994 | |
| 1995 | if (btBlk1st != MS_LB_NOT_USED) { |
| 1996 | btBlk2nd = TmpBlock; |
| 1997 | break; |
| 1998 | } |
| 1999 | |
| 2000 | btBlk1st = TmpBlock; |
| 2001 | memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE); |
| 2002 | if (extdat.status1 & (MS_REG_ST1_DTER | MS_REG_ST1_EXER | MS_REG_ST1_FGER)) |
| 2003 | btBlk1stErred = 1; |
| 2004 | } |
| 2005 | |
| 2006 | if (btBlk1st == MS_LB_NOT_USED) { |
| 2007 | result = MS_STATUS_ERROR; |
| 2008 | goto exit; |
| 2009 | } |
| 2010 | |
| 2011 | /* write protect */ |
| 2012 | if ((extdat.status0 & MS_REG_ST0_WP) == MS_REG_ST0_WP_ON) |
| 2013 | ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT); |
| 2014 | |
| 2015 | result = MS_STATUS_ERROR; |
| 2016 | /* 1st Boot Block */ |
| 2017 | if (btBlk1stErred == 0) |
| 2018 | result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1); |
| 2019 | /* 1st */ |
| 2020 | /* 2nd Boot Block */ |
| 2021 | if (result && (btBlk2nd != MS_LB_NOT_USED)) |
| 2022 | result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0); |
| 2023 | |
| 2024 | if (result) { |
| 2025 | result = MS_STATUS_ERROR; |
| 2026 | goto exit; |
| 2027 | } |
| 2028 | |
| 2029 | for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++) |
| 2030 | info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR; |
| 2031 | |
| 2032 | info->MS_Lib.Phy2LogMap[btBlk1st] = MS_LB_BOOT_BLOCK; |
| 2033 | |
| 2034 | if (btBlk2nd != MS_LB_NOT_USED) { |
| 2035 | for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++) |
| 2036 | info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR; |
| 2037 | |
| 2038 | info->MS_Lib.Phy2LogMap[btBlk2nd] = MS_LB_BOOT_BLOCK; |
| 2039 | } |
| 2040 | |
| 2041 | result = ms_lib_scan_logicalblocknumber(us, btBlk1st); |
| 2042 | if (result) |
| 2043 | goto exit; |
| 2044 | |
| 2045 | for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT; |
| 2046 | TmpBlock < info->MS_Lib.NumberOfPhyBlock; |
| 2047 | TmpBlock += MS_PHYSICAL_BLOCKS_PER_SEGMENT) { |
| 2048 | if (ms_count_freeblock(us, TmpBlock) == 0) { |
| 2049 | ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT); |
| 2050 | break; |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | /* write */ |
| 2055 | if (ms_lib_alloc_writebuf(us)) { |
| 2056 | result = MS_NO_MEMORY_ERROR; |
| 2057 | goto exit; |
| 2058 | } |
| 2059 | |
| 2060 | result = MS_STATUS_SUCCESS; |
| 2061 | |
| 2062 | exit: |
| 2063 | kfree(PageBuffer1); |
| 2064 | kfree(PageBuffer0); |
| 2065 | |
| 2066 | printk(KERN_INFO "MS_CardInit end\n"); |
| 2067 | return result; |
| 2068 | } |
| 2069 | |
| 2070 | static int ene_ms_init(struct us_data *us) |
| 2071 | { |
| 2072 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 2073 | int result; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2074 | u16 MSP_BlockSize, MSP_UserAreaBlocks; |
| 2075 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2076 | u8 *bbuf = info->bbuf; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2077 | |
| 2078 | printk(KERN_INFO "transport --- ENE_MSInit\n"); |
| 2079 | |
| 2080 | /* the same part to test ENE */ |
| 2081 | |
| 2082 | result = ene_load_bincode(us, MS_INIT_PATTERN); |
| 2083 | if (result != USB_STOR_XFER_GOOD) { |
| 2084 | printk(KERN_ERR "Load MS Init Code Fail !!\n"); |
| 2085 | return USB_STOR_TRANSPORT_ERROR; |
| 2086 | } |
| 2087 | |
| 2088 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 2089 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 2090 | bcb->DataTransferLength = 0x200; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 2091 | bcb->Flags = US_BULK_FLAG_IN; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2092 | bcb->CDB[0] = 0xF1; |
| 2093 | bcb->CDB[1] = 0x01; |
| 2094 | |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2095 | result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2096 | if (result != USB_STOR_XFER_GOOD) { |
| 2097 | printk(KERN_ERR "Execution MS Init Code Fail !!\n"); |
| 2098 | return USB_STOR_TRANSPORT_ERROR; |
| 2099 | } |
| 2100 | /* the same part to test ENE */ |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2101 | info->MS_Status = *(struct MS_STATUS *) bbuf; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2102 | |
| 2103 | if (info->MS_Status.Insert && info->MS_Status.Ready) { |
| 2104 | printk(KERN_INFO "Insert = %x\n", info->MS_Status.Insert); |
| 2105 | printk(KERN_INFO "Ready = %x\n", info->MS_Status.Ready); |
| 2106 | printk(KERN_INFO "IsMSPro = %x\n", info->MS_Status.IsMSPro); |
| 2107 | printk(KERN_INFO "IsMSPHG = %x\n", info->MS_Status.IsMSPHG); |
| 2108 | printk(KERN_INFO "WtP= %x\n", info->MS_Status.WtP); |
| 2109 | if (info->MS_Status.IsMSPro) { |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2110 | MSP_BlockSize = (bbuf[6] << 8) | bbuf[7]; |
| 2111 | MSP_UserAreaBlocks = (bbuf[10] << 8) | bbuf[11]; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2112 | info->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks; |
| 2113 | } else { |
| 2114 | ms_card_init(us); /* Card is MS (to ms.c)*/ |
| 2115 | } |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 2116 | usb_stor_dbg(us, "MS Init Code OK !!\n"); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2117 | } else { |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2118 | usb_stor_dbg(us, "MS Card Not Ready --- %x\n", bbuf[0]); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2119 | return USB_STOR_TRANSPORT_ERROR; |
| 2120 | } |
| 2121 | |
| 2122 | return USB_STOR_TRANSPORT_GOOD; |
| 2123 | } |
| 2124 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2125 | static int ene_sd_init(struct us_data *us) |
| 2126 | { |
| 2127 | int result; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2128 | struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; |
| 2129 | struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra; |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2130 | u8 *bbuf = info->bbuf; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2131 | |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 2132 | usb_stor_dbg(us, "transport --- ENE_SDInit\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2133 | /* SD Init Part-1 */ |
| 2134 | result = ene_load_bincode(us, SD_INIT1_PATTERN); |
| 2135 | if (result != USB_STOR_XFER_GOOD) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 2136 | usb_stor_dbg(us, "Load SD Init Code Part-1 Fail !!\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2137 | return USB_STOR_TRANSPORT_ERROR; |
| 2138 | } |
| 2139 | |
| 2140 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 2141 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 2142 | bcb->Flags = US_BULK_FLAG_IN; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2143 | bcb->CDB[0] = 0xF2; |
| 2144 | |
| 2145 | result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0); |
| 2146 | if (result != USB_STOR_XFER_GOOD) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 2147 | usb_stor_dbg(us, "Execution SD Init Code Fail !!\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2148 | return USB_STOR_TRANSPORT_ERROR; |
| 2149 | } |
| 2150 | |
| 2151 | /* SD Init Part-2 */ |
| 2152 | result = ene_load_bincode(us, SD_INIT2_PATTERN); |
| 2153 | if (result != USB_STOR_XFER_GOOD) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 2154 | usb_stor_dbg(us, "Load SD Init Code Part-2 Fail !!\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2155 | return USB_STOR_TRANSPORT_ERROR; |
| 2156 | } |
| 2157 | |
| 2158 | memset(bcb, 0, sizeof(struct bulk_cb_wrap)); |
| 2159 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 2160 | bcb->DataTransferLength = 0x200; |
Sebastian Andrzej Siewior | b8db6d6 | 2012-02-25 18:28:10 +0100 | [diff] [blame] | 2161 | bcb->Flags = US_BULK_FLAG_IN; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2162 | bcb->CDB[0] = 0xF1; |
| 2163 | |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2164 | result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2165 | if (result != USB_STOR_XFER_GOOD) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 2166 | usb_stor_dbg(us, "Execution SD Init Code Fail !!\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2167 | return USB_STOR_TRANSPORT_ERROR; |
| 2168 | } |
| 2169 | |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2170 | info->SD_Status = *(struct SD_STATUS *) bbuf; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2171 | if (info->SD_Status.Insert && info->SD_Status.Ready) { |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 2172 | struct SD_STATUS *s = &info->SD_Status; |
| 2173 | |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2174 | ene_get_card_status(us, bbuf); |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 2175 | usb_stor_dbg(us, "Insert = %x\n", s->Insert); |
| 2176 | usb_stor_dbg(us, "Ready = %x\n", s->Ready); |
| 2177 | usb_stor_dbg(us, "IsMMC = %x\n", s->IsMMC); |
| 2178 | usb_stor_dbg(us, "HiCapacity = %x\n", s->HiCapacity); |
| 2179 | usb_stor_dbg(us, "HiSpeed = %x\n", s->HiSpeed); |
| 2180 | usb_stor_dbg(us, "WtP = %x\n", s->WtP); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2181 | } else { |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2182 | usb_stor_dbg(us, "SD Card Not Ready --- %x\n", bbuf[0]); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2183 | return USB_STOR_TRANSPORT_ERROR; |
| 2184 | } |
| 2185 | return USB_STOR_TRANSPORT_GOOD; |
| 2186 | } |
| 2187 | |
| 2188 | |
| 2189 | static int ene_init(struct us_data *us) |
| 2190 | { |
| 2191 | int result; |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2192 | u8 misc_reg03; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2193 | struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra); |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2194 | u8 *bbuf = info->bbuf; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2195 | |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2196 | result = ene_get_card_type(us, REG_CARD_STATUS, bbuf); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2197 | if (result != USB_STOR_XFER_GOOD) |
| 2198 | return USB_STOR_TRANSPORT_ERROR; |
| 2199 | |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2200 | misc_reg03 = bbuf[0]; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2201 | if (misc_reg03 & 0x01) { |
| 2202 | if (!info->SD_Status.Ready) { |
| 2203 | result = ene_sd_init(us); |
| 2204 | if (result != USB_STOR_XFER_GOOD) |
| 2205 | return USB_STOR_TRANSPORT_ERROR; |
| 2206 | } |
| 2207 | } |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2208 | if (misc_reg03 & 0x02) { |
| 2209 | if (!info->MS_Status.Ready) { |
| 2210 | result = ene_ms_init(us); |
| 2211 | if (result != USB_STOR_XFER_GOOD) |
| 2212 | return USB_STOR_TRANSPORT_ERROR; |
| 2213 | } |
| 2214 | } |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2215 | return result; |
| 2216 | } |
| 2217 | |
| 2218 | /*----- sd_scsi_irp() ---------*/ |
| 2219 | static int sd_scsi_irp(struct us_data *us, struct scsi_cmnd *srb) |
| 2220 | { |
| 2221 | int result; |
| 2222 | struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra; |
| 2223 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2224 | switch (srb->cmnd[0]) { |
| 2225 | case TEST_UNIT_READY: |
| 2226 | result = sd_scsi_test_unit_ready(us, srb); |
| 2227 | break; /* 0x00 */ |
Alan Stern | ce553bd | 2017-05-16 11:48:01 -0400 | [diff] [blame] | 2228 | case REQUEST_SENSE: |
| 2229 | result = do_scsi_request_sense(us, srb); |
| 2230 | break; /* 0x03 */ |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2231 | case INQUIRY: |
Alan Stern | f8efdab | 2017-05-16 11:48:10 -0400 | [diff] [blame] | 2232 | result = do_scsi_inquiry(us, srb); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2233 | break; /* 0x12 */ |
| 2234 | case MODE_SENSE: |
| 2235 | result = sd_scsi_mode_sense(us, srb); |
| 2236 | break; /* 0x1A */ |
| 2237 | /* |
| 2238 | case START_STOP: |
| 2239 | result = SD_SCSI_Start_Stop(us, srb); |
| 2240 | break; //0x1B |
| 2241 | */ |
| 2242 | case READ_CAPACITY: |
| 2243 | result = sd_scsi_read_capacity(us, srb); |
| 2244 | break; /* 0x25 */ |
| 2245 | case READ_10: |
| 2246 | result = sd_scsi_read(us, srb); |
| 2247 | break; /* 0x28 */ |
| 2248 | case WRITE_10: |
| 2249 | result = sd_scsi_write(us, srb); |
| 2250 | break; /* 0x2A */ |
| 2251 | default: |
| 2252 | info->SrbStatus = SS_ILLEGAL_REQUEST; |
| 2253 | result = USB_STOR_TRANSPORT_FAILED; |
| 2254 | break; |
| 2255 | } |
Alan Stern | ce553bd | 2017-05-16 11:48:01 -0400 | [diff] [blame] | 2256 | if (result == USB_STOR_TRANSPORT_GOOD) |
| 2257 | info->SrbStatus = SS_SUCCESS; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2258 | return result; |
| 2259 | } |
| 2260 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2261 | /* |
| 2262 | * ms_scsi_irp() |
| 2263 | */ |
Felipe Balbi | 36f3a14d | 2011-11-15 09:53:31 +0200 | [diff] [blame] | 2264 | static int ms_scsi_irp(struct us_data *us, struct scsi_cmnd *srb) |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2265 | { |
| 2266 | int result; |
| 2267 | struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra; |
Alan Stern | ce553bd | 2017-05-16 11:48:01 -0400 | [diff] [blame] | 2268 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2269 | switch (srb->cmnd[0]) { |
| 2270 | case TEST_UNIT_READY: |
| 2271 | result = ms_scsi_test_unit_ready(us, srb); |
| 2272 | break; /* 0x00 */ |
Alan Stern | ce553bd | 2017-05-16 11:48:01 -0400 | [diff] [blame] | 2273 | case REQUEST_SENSE: |
| 2274 | result = do_scsi_request_sense(us, srb); |
| 2275 | break; /* 0x03 */ |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2276 | case INQUIRY: |
Alan Stern | f8efdab | 2017-05-16 11:48:10 -0400 | [diff] [blame] | 2277 | result = do_scsi_inquiry(us, srb); |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2278 | break; /* 0x12 */ |
| 2279 | case MODE_SENSE: |
| 2280 | result = ms_scsi_mode_sense(us, srb); |
| 2281 | break; /* 0x1A */ |
| 2282 | case READ_CAPACITY: |
| 2283 | result = ms_scsi_read_capacity(us, srb); |
| 2284 | break; /* 0x25 */ |
| 2285 | case READ_10: |
| 2286 | result = ms_scsi_read(us, srb); |
| 2287 | break; /* 0x28 */ |
| 2288 | case WRITE_10: |
| 2289 | result = ms_scsi_write(us, srb); |
| 2290 | break; /* 0x2A */ |
| 2291 | default: |
| 2292 | info->SrbStatus = SS_ILLEGAL_REQUEST; |
| 2293 | result = USB_STOR_TRANSPORT_FAILED; |
| 2294 | break; |
| 2295 | } |
Alan Stern | ce553bd | 2017-05-16 11:48:01 -0400 | [diff] [blame] | 2296 | if (result == USB_STOR_TRANSPORT_GOOD) |
| 2297 | info->SrbStatus = SS_SUCCESS; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2298 | return result; |
| 2299 | } |
| 2300 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2301 | static int ene_transport(struct scsi_cmnd *srb, struct us_data *us) |
| 2302 | { |
Alan Stern | 4b309f1 | 2017-05-16 11:47:42 -0400 | [diff] [blame] | 2303 | int result = USB_STOR_XFER_GOOD; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2304 | struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra); |
| 2305 | |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 2306 | /*US_DEBUG(usb_stor_show_command(us, srb)); */ |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2307 | scsi_set_resid(srb, 0); |
Alan Stern | 4b309f1 | 2017-05-16 11:47:42 -0400 | [diff] [blame] | 2308 | if (unlikely(!(info->SD_Status.Ready || info->MS_Status.Ready))) |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2309 | result = ene_init(us); |
Alan Stern | 4b309f1 | 2017-05-16 11:47:42 -0400 | [diff] [blame] | 2310 | if (result == USB_STOR_XFER_GOOD) { |
| 2311 | result = USB_STOR_TRANSPORT_ERROR; |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2312 | if (info->SD_Status.Ready) |
| 2313 | result = sd_scsi_irp(us, srb); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2314 | |
Cho, Yu-Chen | 33842ce | 2011-07-07 11:27:13 +0800 | [diff] [blame] | 2315 | if (info->MS_Status.Ready) |
| 2316 | result = ms_scsi_irp(us, srb); |
| 2317 | } |
Alan Stern | 4b309f1 | 2017-05-16 11:47:42 -0400 | [diff] [blame] | 2318 | return result; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2319 | } |
| 2320 | |
Akinobu Mita | aa519be | 2015-05-06 18:24:21 +0900 | [diff] [blame] | 2321 | static struct scsi_host_template ene_ub6250_host_template; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2322 | |
| 2323 | static int ene_ub6250_probe(struct usb_interface *intf, |
| 2324 | const struct usb_device_id *id) |
| 2325 | { |
| 2326 | int result; |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2327 | u8 misc_reg03; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2328 | struct us_data *us; |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2329 | struct ene_ub6250_info *info; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2330 | |
| 2331 | result = usb_stor_probe1(&us, intf, id, |
Akinobu Mita | aa519be | 2015-05-06 18:24:21 +0900 | [diff] [blame] | 2332 | (id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list, |
| 2333 | &ene_ub6250_host_template); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2334 | if (result) |
| 2335 | return result; |
| 2336 | |
| 2337 | /* FIXME: where should the code alloc extra buf ? */ |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2338 | us->extra = kzalloc(sizeof(struct ene_ub6250_info), GFP_KERNEL); |
| 2339 | if (!us->extra) |
| 2340 | return -ENOMEM; |
| 2341 | us->extra_destructor = ene_ub6250_info_destructor; |
| 2342 | |
| 2343 | info = (struct ene_ub6250_info *)(us->extra); |
| 2344 | info->bbuf = kmalloc(512, GFP_KERNEL); |
| 2345 | if (!info->bbuf) { |
| 2346 | kfree(us->extra); |
| 2347 | return -ENOMEM; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | us->transport_name = "ene_ub6250"; |
| 2351 | us->transport = ene_transport; |
| 2352 | us->max_lun = 0; |
| 2353 | |
| 2354 | result = usb_stor_probe2(us); |
| 2355 | if (result) |
| 2356 | return result; |
| 2357 | |
| 2358 | /* probe card type */ |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2359 | result = ene_get_card_type(us, REG_CARD_STATUS, info->bbuf); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2360 | if (result != USB_STOR_XFER_GOOD) { |
| 2361 | usb_stor_disconnect(intf); |
| 2362 | return USB_STOR_TRANSPORT_ERROR; |
| 2363 | } |
| 2364 | |
Alan Stern | 628c289 | 2017-05-16 11:47:29 -0400 | [diff] [blame] | 2365 | misc_reg03 = info->bbuf[0]; |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2366 | if (!(misc_reg03 & 0x01)) { |
Kristina Martšenko | 16fae05 | 2014-07-24 04:34:38 +0300 | [diff] [blame] | 2367 | pr_info("ums_eneub6250: This driver only supports SD/MS cards. " |
| 2368 | "It does not support SM cards.\n"); |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | return result; |
| 2372 | } |
| 2373 | |
| 2374 | |
| 2375 | #ifdef CONFIG_PM |
| 2376 | |
| 2377 | static int ene_ub6250_resume(struct usb_interface *iface) |
| 2378 | { |
| 2379 | u8 tmp = 0; |
| 2380 | struct us_data *us = usb_get_intfdata(iface); |
| 2381 | struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra); |
| 2382 | |
| 2383 | mutex_lock(&us->dev_mutex); |
| 2384 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2385 | if (us->suspend_resume_hook) |
| 2386 | (us->suspend_resume_hook)(us, US_RESUME); |
| 2387 | |
| 2388 | mutex_unlock(&us->dev_mutex); |
| 2389 | |
| 2390 | info->Power_IsResum = true; |
| 2391 | /*info->SD_Status.Ready = 0; */ |
| 2392 | info->SD_Status = *(struct SD_STATUS *)&tmp; |
| 2393 | info->MS_Status = *(struct MS_STATUS *)&tmp; |
| 2394 | info->SM_Status = *(struct SM_STATUS *)&tmp; |
| 2395 | |
| 2396 | return 0; |
| 2397 | } |
| 2398 | |
| 2399 | static int ene_ub6250_reset_resume(struct usb_interface *iface) |
| 2400 | { |
| 2401 | u8 tmp = 0; |
| 2402 | struct us_data *us = usb_get_intfdata(iface); |
| 2403 | struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra); |
Joe Perches | 191648d | 2013-04-19 11:44:00 -0700 | [diff] [blame] | 2404 | |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2405 | /* Report the reset to the SCSI core */ |
| 2406 | usb_stor_reset_resume(iface); |
| 2407 | |
Felipe Balbi | f0183a3 | 2016-04-18 13:09:11 +0300 | [diff] [blame] | 2408 | /* |
| 2409 | * FIXME: Notify the subdrivers that they need to reinitialize |
| 2410 | * the device |
| 2411 | */ |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2412 | info->Power_IsResum = true; |
| 2413 | /*info->SD_Status.Ready = 0; */ |
| 2414 | info->SD_Status = *(struct SD_STATUS *)&tmp; |
| 2415 | info->MS_Status = *(struct MS_STATUS *)&tmp; |
| 2416 | info->SM_Status = *(struct SM_STATUS *)&tmp; |
| 2417 | |
| 2418 | return 0; |
| 2419 | } |
| 2420 | |
| 2421 | #else |
| 2422 | |
| 2423 | #define ene_ub6250_resume NULL |
| 2424 | #define ene_ub6250_reset_resume NULL |
| 2425 | |
| 2426 | #endif |
| 2427 | |
| 2428 | static struct usb_driver ene_ub6250_driver = { |
Akinobu Mita | aa519be | 2015-05-06 18:24:21 +0900 | [diff] [blame] | 2429 | .name = DRV_NAME, |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2430 | .probe = ene_ub6250_probe, |
| 2431 | .disconnect = usb_stor_disconnect, |
| 2432 | .suspend = usb_stor_suspend, |
| 2433 | .resume = ene_ub6250_resume, |
| 2434 | .reset_resume = ene_ub6250_reset_resume, |
| 2435 | .pre_reset = usb_stor_pre_reset, |
| 2436 | .post_reset = usb_stor_post_reset, |
| 2437 | .id_table = ene_ub6250_usb_ids, |
| 2438 | .soft_unbind = 1, |
Huajun Li | e73b2db | 2012-01-14 10:15:21 +0800 | [diff] [blame] | 2439 | .no_dynamic_id = 1, |
huajun li | 41e568d | 2011-03-04 10:56:18 +0800 | [diff] [blame] | 2440 | }; |
| 2441 | |
Akinobu Mita | aa519be | 2015-05-06 18:24:21 +0900 | [diff] [blame] | 2442 | module_usb_stor_driver(ene_ub6250_driver, ene_ub6250_host_template, DRV_NAME); |