1 // SPDX-License-Identifier: GPL-2.0+
2 #include <linux/jiffies.h>
3 #include <linux/errno.h>
4 #include <linux/module.h>
5 #include <linux/slab.h>
8 #include <scsi/scsi_cmnd.h>
10 #include <linux/firmware.h>
13 #include "transport.h"
18 #define SD_INIT1_FIRMWARE "/*(DEBLOBBED)*/"
19 #define SD_INIT2_FIRMWARE "/*(DEBLOBBED)*/"
20 #define SD_RW_FIRMWARE "/*(DEBLOBBED)*/"
21 #define MS_INIT_FIRMWARE "/*(DEBLOBBED)*/"
22 #define MSP_RW_FIRMWARE "/*(DEBLOBBED)*/"
23 #define MS_RW_FIRMWARE "/*(DEBLOBBED)*/"
25 #define DRV_NAME "ums_eneub6250"
27 MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
28 MODULE_LICENSE("GPL");
29 MODULE_IMPORT_NS(USB_STORAGE);
33 * The table of devices
35 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
36 vendorName, productName, useProtocol, useTransport, \
37 initFunction, flags) \
38 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
39 .driver_info = (flags)}
41 static struct usb_device_id ene_ub6250_usb_ids[] = {
42 # include "unusual_ene_ub6250.h"
43 { } /* Terminating entry */
45 MODULE_DEVICE_TABLE(usb, ene_ub6250_usb_ids);
52 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
53 vendor_name, product_name, use_protocol, use_transport, \
54 init_function, Flags) \
56 .vendorName = vendor_name, \
57 .productName = product_name, \
58 .useProtocol = use_protocol, \
59 .useTransport = use_transport, \
60 .initFunction = init_function, \
63 static struct us_unusual_dev ene_ub6250_unusual_dev_list[] = {
64 # include "unusual_ene_ub6250.h"
65 { } /* Terminating entry */
72 /* ENE bin code len */
73 #define ENE_BIN_CODE_LEN 0x800
75 #define REG_CARD_STATUS 0xFF83
76 #define REG_HW_TRAP1 0xFF89
79 #define SS_SUCCESS 0x000000 /* No Sense */
80 #define SS_NOT_READY 0x023A00 /* Medium not present */
81 #define SS_MEDIUM_ERR 0x031100 /* Unrecovered read error */
82 #define SS_HW_ERR 0x040800 /* Communication failure */
83 #define SS_ILLEGAL_REQUEST 0x052000 /* Invalid command */
84 #define SS_UNIT_ATTENTION 0x062900 /* Reset occurred */
86 /* ENE Load FW Pattern */
87 #define SD_INIT1_PATTERN 1
88 #define SD_INIT2_PATTERN 2
89 #define SD_RW_PATTERN 3
90 #define MS_INIT_PATTERN 4
91 #define MSP_RW_PATTERN 5
92 #define MS_RW_PATTERN 6
93 #define SM_INIT_PATTERN 7
94 #define SM_RW_PATTERN 8
101 /* Status Register 1 */
102 #define MS_REG_ST1_MB 0x80 /* media busy */
103 #define MS_REG_ST1_FB1 0x40 /* flush busy 1 */
104 #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */
105 #define MS_REG_ST1_UCDT 0x10 /* unable to correct data */
106 #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */
107 #define MS_REG_ST1_UCEX 0x04 /* unable to correct extra */
108 #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */
109 #define MS_REG_ST1_UCFG 0x01 /* unable to correct overwrite flag */
110 #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)
113 #define MS_REG_OVR_BKST 0x80 /* block status */
114 #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */
115 #define MS_REG_OVR_BKST_NG 0x00 /* NG */
116 #define MS_REG_OVR_PGST0 0x40 /* page status */
117 #define MS_REG_OVR_PGST1 0x20
118 #define MS_REG_OVR_PGST_MASK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1)
119 #define MS_REG_OVR_PGST_OK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1) /* OK */
120 #define MS_REG_OVR_PGST_NG MS_REG_OVR_PGST1 /* NG */
121 #define MS_REG_OVR_PGST_DATA_ERROR 0x00 /* data error */
122 #define MS_REG_OVR_UDST 0x10 /* update status */
123 #define MS_REG_OVR_UDST_UPDATING 0x00 /* updating */
124 #define MS_REG_OVR_UDST_NO_UPDATE MS_REG_OVR_UDST
125 #define MS_REG_OVR_RESERVED 0x08
126 #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)
128 /* Management Flag */
129 #define MS_REG_MNG_SCMS0 0x20 /* serial copy management system */
130 #define MS_REG_MNG_SCMS1 0x10
131 #define MS_REG_MNG_SCMS_MASK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
132 #define MS_REG_MNG_SCMS_COPY_OK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
133 #define MS_REG_MNG_SCMS_ONE_COPY MS_REG_MNG_SCMS1
134 #define MS_REG_MNG_SCMS_NO_COPY 0x00
135 #define MS_REG_MNG_ATFLG 0x08 /* address transfer table flag */
136 #define MS_REG_MNG_ATFLG_OTHER MS_REG_MNG_ATFLG /* other */
137 #define MS_REG_MNG_ATFLG_ATTBL 0x00 /* address transfer table */
138 #define MS_REG_MNG_SYSFLG 0x04 /* system flag */
139 #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */
140 #define MS_REG_MNG_SYSFLG_BOOT 0x00 /* system block */
141 #define MS_REG_MNG_RESERVED 0xc3
142 #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)
145 #define MS_MAX_PAGES_PER_BLOCK 32
146 #define MS_MAX_INITIAL_ERROR_BLOCKS 10
147 #define MS_LIB_BITS_PER_BYTE 8
149 #define MS_SYSINF_FORMAT_FAT 1
150 #define MS_SYSINF_USAGE_GENERAL 0
152 #define MS_SYSINF_MSCLASS_TYPE_1 1
153 #define MS_SYSINF_PAGE_SIZE MS_BYTES_PER_PAGE /* fixed */
155 #define MS_SYSINF_CARDTYPE_RDONLY 1
156 #define MS_SYSINF_CARDTYPE_RDWR 2
157 #define MS_SYSINF_CARDTYPE_HYBRID 3
158 #define MS_SYSINF_SECURITY 0x01
159 #define MS_SYSINF_SECURITY_NO_SUPPORT MS_SYSINF_SECURITY
160 #define MS_SYSINF_SECURITY_SUPPORT 0
162 #define MS_SYSINF_RESERVED1 1
163 #define MS_SYSINF_RESERVED2 1
165 #define MS_SYSENT_TYPE_INVALID_BLOCK 0x01
166 #define MS_SYSENT_TYPE_CIS_IDI 0x0a /* CIS/IDI */
168 #define SIZE_OF_KIRO 1024
169 #define BYTE_MASK 0xff
172 #define MS_STATUS_WRITE_PROTECT 0x0106
173 #define MS_STATUS_SUCCESS 0x0000
174 #define MS_ERROR_FLASH_READ 0x8003
175 #define MS_ERROR_FLASH_ERASE 0x8005
176 #define MS_LB_ERROR 0xfff0
177 #define MS_LB_BOOT_BLOCK 0xfff1
178 #define MS_LB_INITIAL_ERROR 0xfff2
179 #define MS_STATUS_SUCCESS_WITH_ECC 0xfff3
180 #define MS_LB_ACQUIRED_ERROR 0xfff4
181 #define MS_LB_NOT_USED_ERASED 0xfff5
182 #define MS_NOCARD_ERROR 0xfff8
183 #define MS_NO_MEMORY_ERROR 0xfff9
184 #define MS_STATUS_INT_ERROR 0xfffa
185 #define MS_STATUS_ERROR 0xfffe
186 #define MS_LB_NOT_USED 0xffff
188 #define MS_REG_MNG_SYSFLG 0x04 /* system flag */
189 #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */
191 #define MS_BOOT_BLOCK_ID 0x0001
192 #define MS_BOOT_BLOCK_FORMAT_VERSION 0x0100
193 #define MS_BOOT_BLOCK_DATA_ENTRIES 2
195 #define MS_NUMBER_OF_SYSTEM_ENTRY 4
196 #define MS_NUMBER_OF_BOOT_BLOCK 2
197 #define MS_BYTES_PER_PAGE 512
198 #define MS_LOGICAL_BLOCKS_PER_SEGMENT 496
199 #define MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT 494
201 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT 0x200 /* 512 */
202 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK 0x1ff
205 #define MS_REG_OVR_BKST 0x80 /* block status */
206 #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */
207 #define MS_REG_OVR_BKST_NG 0x00 /* NG */
209 /* Status Register 1 */
210 #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */
211 #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */
212 #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */
214 /* MemoryStick Register */
215 /* Status Register 0 */
216 #define MS_REG_ST0_WP 0x01 /* write protected */
217 #define MS_REG_ST0_WP_ON MS_REG_ST0_WP
219 #define MS_LIB_CTRL_RDONLY 0
220 #define MS_LIB_CTRL_WRPROTECT 1
223 #define ms_libconv_to_logical(pdx, PhyBlock) (((PhyBlock) >= (pdx)->MS_Lib.NumberOfPhyBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Phy2LogMap[PhyBlock])
224 #define ms_libconv_to_physical(pdx, LogBlock) (((LogBlock) >= (pdx)->MS_Lib.NumberOfLogBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Log2PhyMap[LogBlock])
226 #define ms_lib_ctrl_set(pdx, Flag) ((pdx)->MS_Lib.flags |= (1 << (Flag)))
227 #define ms_lib_ctrl_reset(pdx, Flag) ((pdx)->MS_Lib.flags &= ~(1 << (Flag)))
228 #define ms_lib_ctrl_check(pdx, Flag) ((pdx)->MS_Lib.flags & (1 << (Flag)))
230 #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))
231 #define ms_lib_clear_pagemap(pdx) memset((pdx)->MS_Lib.pagemap, 0, sizeof((pdx)->MS_Lib.pagemap))
232 #define memstick_logaddr(logadr1, logadr0) ((((u16)(logadr1)) << 8) | (logadr0))
236 #define SD_Insert BIT(0)
237 #define SD_Ready BIT(1)
238 #define SD_MediaChange BIT(2)
239 #define SD_IsMMC BIT(3)
240 #define SD_HiCapacity BIT(4)
241 #define SD_HiSpeed BIT(5)
242 #define SD_WtP BIT(6)
246 #define MS_Insert BIT(0)
247 #define MS_Ready BIT(1)
248 #define MS_MediaChange BIT(2)
249 #define MS_IsMSPro BIT(3)
250 #define MS_IsMSPHG BIT(4)
252 #define MS_WtP BIT(6)
256 #define SM_Insert BIT(0)
257 #define SM_Ready BIT(1)
258 #define SM_MediaChange BIT(2)
259 /* Bits 3-5 reserved */
260 #define SM_WtP BIT(6)
261 #define SM_IsMS BIT(7)
263 struct ms_bootblock_cis {
264 u8 bCistplDEVICE[6]; /* 0 */
265 u8 bCistplDEVICE0C[6]; /* 6 */
266 u8 bCistplJEDECC[4]; /* 12 */
267 u8 bCistplMANFID[6]; /* 16 */
268 u8 bCistplVER1[32]; /* 22 */
269 u8 bCistplFUNCID[4]; /* 54 */
270 u8 bCistplFUNCE0[4]; /* 58 */
271 u8 bCistplFUNCE1[5]; /* 62 */
272 u8 bCistplCONF[7]; /* 67 */
273 u8 bCistplCFTBLENT0[10];/* 74 */
274 u8 bCistplCFTBLENT1[8]; /* 84 */
275 u8 bCistplCFTBLENT2[12];/* 92 */
276 u8 bCistplCFTBLENT3[8]; /* 104 */
277 u8 bCistplCFTBLENT4[17];/* 112 */
278 u8 bCistplCFTBLENT5[8]; /* 129 */
279 u8 bCistplCFTBLENT6[17];/* 137 */
280 u8 bCistplCFTBLENT7[8]; /* 154 */
281 u8 bCistplNOLINK[3]; /* 162 */
284 struct ms_bootblock_idi {
285 #define MS_IDI_GENERAL_CONF 0x848A
286 u16 wIDIgeneralConfiguration; /* 0 */
287 u16 wIDInumberOfCylinder; /* 1 */
288 u16 wIDIreserved0; /* 2 */
289 u16 wIDInumberOfHead; /* 3 */
290 u16 wIDIbytesPerTrack; /* 4 */
291 u16 wIDIbytesPerSector; /* 5 */
292 u16 wIDIsectorsPerTrack; /* 6 */
293 u16 wIDItotalSectors[2]; /* 7-8 high,low */
294 u16 wIDIreserved1[11]; /* 9-19 */
295 u16 wIDIbufferType; /* 20 */
296 u16 wIDIbufferSize; /* 21 */
297 u16 wIDIlongCmdECC; /* 22 */
298 u16 wIDIfirmVersion[4]; /* 23-26 */
299 u16 wIDImodelName[20]; /* 27-46 */
300 u16 wIDIreserved2; /* 47 */
301 u16 wIDIlongWordSupported; /* 48 */
302 u16 wIDIdmaSupported; /* 49 */
303 u16 wIDIreserved3; /* 50 */
304 u16 wIDIpioTiming; /* 51 */
305 u16 wIDIdmaTiming; /* 52 */
306 u16 wIDItransferParameter; /* 53 */
307 u16 wIDIformattedCylinder; /* 54 */
308 u16 wIDIformattedHead; /* 55 */
309 u16 wIDIformattedSectorsPerTrack;/* 56 */
310 u16 wIDIformattedTotalSectors[2];/* 57-58 */
311 u16 wIDImultiSector; /* 59 */
312 u16 wIDIlbaSectors[2]; /* 60-61 */
313 u16 wIDIsingleWordDMA; /* 62 */
314 u16 wIDImultiWordDMA; /* 63 */
315 u16 wIDIreserved4[192]; /* 64-255 */
318 struct ms_bootblock_sysent_rec {
325 struct ms_bootblock_sysent {
326 struct ms_bootblock_sysent_rec entry[MS_NUMBER_OF_SYSTEM_ENTRY];
329 struct ms_bootblock_sysinf {
330 u8 bMsClass; /* must be 1 */
331 u8 bCardType; /* see below */
332 u16 wBlockSize; /* n KB */
333 u16 wBlockNumber; /* number of physical block */
334 u16 wTotalBlockNumber; /* number of logical block */
335 u16 wPageSize; /* must be 0x200 */
336 u8 bExtraSize; /* 0x10 */
340 u8 bAssemblyMakerCode;
341 u8 bAssemblyMachineCode[3];
342 u16 wMemoryMakerCode;
343 u16 wMemoryDeviceCode;
349 u16 wControllerChipNumber;
350 u16 wControllerFunction; /* New MS */
351 u8 bReserved3[9]; /* New MS */
352 u8 bParallelSupport; /* New MS */
353 u16 wFormatValue; /* New MS */
363 struct ms_bootblock_header {
367 u8 bNumberOfDataEntry;
371 struct ms_bootblock_page0 {
372 struct ms_bootblock_header header;
373 struct ms_bootblock_sysent sysent;
374 struct ms_bootblock_sysinf sysinf;
377 struct ms_bootblock_cis_idi {
379 struct ms_bootblock_cis cis;
384 struct ms_bootblock_idi idi;
390 /* ENE MS Lib struct */
391 struct ms_lib_type_extdat {
404 u32 NumberOfCylinder;
405 u32 SectorsPerCylinder;
406 u16 cardType; /* R/W, RO, Hybrid */
409 u16 NumberOfPhyBlock;
410 u16 NumberOfLogBlock;
412 u16 *Phy2LogMap; /* phy2log table */
413 u16 *Log2PhyMap; /* log2phy table */
415 unsigned char *pagemap[(MS_MAX_PAGES_PER_BLOCK + (MS_LIB_BITS_PER_BYTE-1)) / MS_LIB_BITS_PER_BYTE];
416 unsigned char *blkpag;
417 struct ms_lib_type_extdat *blkext;
418 unsigned char copybuf[512];
422 /* SD Block Length */
423 /* 2^9 = 512 Bytes, The HW maximum read/write data length */
424 #define SD_BLOCK_LEN 9
426 struct ene_ub6250_info {
428 /* I/O bounce buffer */
436 /* ----- SD Control Data ---------------- */
437 /*SD_REGISTER SD_Regs; */
443 /* SD/MMC New spec. */
446 u8 SD20_HIGH_CAPACITY;
450 u8 MMC_HIGH_CAPACITY;
452 /*----- MS Control Data ---------------- */
455 struct ms_lib_ctrl MS_Lib;
459 /*----- SM Control Data ---------------- */
463 unsigned char *testbuf;
468 /*------Power Managerment ---------------*/
472 static int ene_sd_init(struct us_data *us);
473 static int ene_ms_init(struct us_data *us);
474 static int ene_load_bincode(struct us_data *us, unsigned char flag);
476 static void ene_ub6250_info_destructor(void *extra)
478 struct ene_ub6250_info *info = (struct ene_ub6250_info *) extra;
485 static int ene_send_scsi_cmd(struct us_data *us, u8 fDir, void *buf, int use_sg)
487 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
488 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
491 unsigned int residue;
492 unsigned int cswlen = 0, partial = 0;
493 unsigned int transfer_length = bcb->DataTransferLength;
495 /* usb_stor_dbg(us, "transport --- ene_send_scsi_cmd\n"); */
496 /* send cmd to out endpoint */
497 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
498 bcb, US_BULK_CB_WRAP_LEN, NULL);
499 if (result != USB_STOR_XFER_GOOD) {
500 usb_stor_dbg(us, "send cmd to out endpoint fail ---\n");
501 return USB_STOR_TRANSPORT_ERROR;
505 unsigned int pipe = fDir;
507 if (fDir == FDIR_READ)
508 pipe = us->recv_bulk_pipe;
510 pipe = us->send_bulk_pipe;
514 result = usb_stor_bulk_srb(us, pipe, us->srb);
516 result = usb_stor_bulk_transfer_sg(us, pipe, buf,
517 transfer_length, 0, &partial);
519 if (result != USB_STOR_XFER_GOOD) {
520 usb_stor_dbg(us, "data transfer fail ---\n");
521 return USB_STOR_TRANSPORT_ERROR;
525 /* Get CSW for device status */
526 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
527 US_BULK_CS_WRAP_LEN, &cswlen);
529 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
530 usb_stor_dbg(us, "Received 0-length CSW; retrying...\n");
531 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
532 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
535 if (result == USB_STOR_XFER_STALLED) {
536 /* get the status again */
537 usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n");
538 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
539 bcs, US_BULK_CS_WRAP_LEN, NULL);
542 if (result != USB_STOR_XFER_GOOD)
543 return USB_STOR_TRANSPORT_ERROR;
545 /* check bulk status */
546 residue = le32_to_cpu(bcs->Residue);
549 * try to compute the actual residue, based on how much data
550 * was really transferred and what the device tells us
552 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
553 residue = min(residue, transfer_length);
555 scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
559 if (bcs->Status != US_BULK_STAT_OK)
560 return USB_STOR_TRANSPORT_ERROR;
562 return USB_STOR_TRANSPORT_GOOD;
565 static int do_scsi_request_sense(struct us_data *us, struct scsi_cmnd *srb)
567 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
568 unsigned char buf[18];
571 buf[0] = 0x70; /* Current error */
572 buf[2] = info->SrbStatus >> 16; /* Sense key */
573 buf[7] = 10; /* Additional length */
574 buf[12] = info->SrbStatus >> 8; /* ASC */
575 buf[13] = info->SrbStatus; /* ASCQ */
577 usb_stor_set_xfer_buf(buf, sizeof(buf), srb);
578 return USB_STOR_TRANSPORT_GOOD;
581 static int do_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
583 unsigned char data_ptr[36] = {
584 0x00, 0x00, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
585 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
586 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
587 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30 };
589 usb_stor_set_xfer_buf(data_ptr, 36, srb);
590 return USB_STOR_TRANSPORT_GOOD;
593 static int sd_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
595 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
597 if ((info->SD_Status & SD_Insert) && (info->SD_Status & SD_Ready))
598 return USB_STOR_TRANSPORT_GOOD;
601 return USB_STOR_TRANSPORT_GOOD;
604 return USB_STOR_TRANSPORT_GOOD;
607 static int sd_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
609 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
610 unsigned char mediaNoWP[12] = {
611 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
612 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
613 unsigned char mediaWP[12] = {
614 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
615 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
617 if (info->SD_Status & SD_WtP)
618 usb_stor_set_xfer_buf(mediaWP, 12, srb);
620 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
623 return USB_STOR_TRANSPORT_GOOD;
626 static int sd_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
630 unsigned int offset = 0;
631 unsigned char buf[8];
632 struct scatterlist *sg = NULL;
633 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
635 usb_stor_dbg(us, "sd_scsi_read_capacity\n");
636 if (info->SD_Status & SD_HiCapacity) {
638 if (info->SD_Status & SD_IsMMC)
639 bl_num = info->HC_C_SIZE-1;
641 bl_num = (info->HC_C_SIZE + 1) * 1024 - 1;
643 bl_len = 1 << (info->SD_READ_BL_LEN);
644 bl_num = info->SD_Block_Mult * (info->SD_C_SIZE + 1)
645 * (1 << (info->SD_C_SIZE_MULT + 2)) - 1;
647 info->bl_num = bl_num;
648 usb_stor_dbg(us, "bl_len = %x\n", bl_len);
649 usb_stor_dbg(us, "bl_num = %x\n", bl_num);
651 /*srb->request_bufflen = 8; */
652 buf[0] = (bl_num >> 24) & 0xff;
653 buf[1] = (bl_num >> 16) & 0xff;
654 buf[2] = (bl_num >> 8) & 0xff;
655 buf[3] = (bl_num >> 0) & 0xff;
656 buf[4] = (bl_len >> 24) & 0xff;
657 buf[5] = (bl_len >> 16) & 0xff;
658 buf[6] = (bl_len >> 8) & 0xff;
659 buf[7] = (bl_len >> 0) & 0xff;
661 usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
663 return USB_STOR_TRANSPORT_GOOD;
666 static int sd_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
669 unsigned char *cdb = srb->cmnd;
670 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
671 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
673 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
674 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
675 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
676 u32 bnByte = bn * 0x200;
677 u32 blenByte = blen * 0x200;
679 if (bn > info->bl_num)
680 return USB_STOR_TRANSPORT_ERROR;
682 result = ene_load_bincode(us, SD_RW_PATTERN);
683 if (result != USB_STOR_XFER_GOOD) {
684 usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
685 return USB_STOR_TRANSPORT_ERROR;
688 if (info->SD_Status & SD_HiCapacity)
691 /* set up the command wrapper */
692 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
693 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
694 bcb->DataTransferLength = blenByte;
695 bcb->Flags = US_BULK_FLAG_IN;
697 bcb->CDB[5] = (unsigned char)(bnByte);
698 bcb->CDB[4] = (unsigned char)(bnByte>>8);
699 bcb->CDB[3] = (unsigned char)(bnByte>>16);
700 bcb->CDB[2] = (unsigned char)(bnByte>>24);
702 result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
706 static int sd_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
709 unsigned char *cdb = srb->cmnd;
710 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
711 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
713 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
714 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
715 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
716 u32 bnByte = bn * 0x200;
717 u32 blenByte = blen * 0x200;
719 if (bn > info->bl_num)
720 return USB_STOR_TRANSPORT_ERROR;
722 result = ene_load_bincode(us, SD_RW_PATTERN);
723 if (result != USB_STOR_XFER_GOOD) {
724 usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
725 return USB_STOR_TRANSPORT_ERROR;
728 if (info->SD_Status & SD_HiCapacity)
731 /* set up the command wrapper */
732 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
733 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
734 bcb->DataTransferLength = blenByte;
737 bcb->CDB[5] = (unsigned char)(bnByte);
738 bcb->CDB[4] = (unsigned char)(bnByte>>8);
739 bcb->CDB[3] = (unsigned char)(bnByte>>16);
740 bcb->CDB[2] = (unsigned char)(bnByte>>24);
742 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
750 static int ms_lib_set_logicalpair(struct us_data *us, u16 logblk, u16 phyblk)
752 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
754 if ((logblk >= info->MS_Lib.NumberOfLogBlock) || (phyblk >= info->MS_Lib.NumberOfPhyBlock))
757 info->MS_Lib.Phy2LogMap[phyblk] = logblk;
758 info->MS_Lib.Log2PhyMap[logblk] = phyblk;
763 static int ms_lib_set_logicalblockmark(struct us_data *us, u16 phyblk, u16 mark)
765 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
767 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
770 info->MS_Lib.Phy2LogMap[phyblk] = mark;
775 static int ms_lib_set_initialerrorblock(struct us_data *us, u16 phyblk)
777 return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_INITIAL_ERROR);
780 static int ms_lib_set_bootblockmark(struct us_data *us, u16 phyblk)
782 return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_BOOT_BLOCK);
785 static int ms_lib_free_logicalmap(struct us_data *us)
787 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
789 kfree(info->MS_Lib.Phy2LogMap);
790 info->MS_Lib.Phy2LogMap = NULL;
792 kfree(info->MS_Lib.Log2PhyMap);
793 info->MS_Lib.Log2PhyMap = NULL;
798 static int ms_lib_alloc_logicalmap(struct us_data *us)
801 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
803 info->MS_Lib.Phy2LogMap = kmalloc_array(info->MS_Lib.NumberOfPhyBlock,
806 info->MS_Lib.Log2PhyMap = kmalloc_array(info->MS_Lib.NumberOfLogBlock,
810 if ((info->MS_Lib.Phy2LogMap == NULL) || (info->MS_Lib.Log2PhyMap == NULL)) {
811 ms_lib_free_logicalmap(us);
815 for (i = 0; i < info->MS_Lib.NumberOfPhyBlock; i++)
816 info->MS_Lib.Phy2LogMap[i] = MS_LB_NOT_USED;
818 for (i = 0; i < info->MS_Lib.NumberOfLogBlock; i++)
819 info->MS_Lib.Log2PhyMap[i] = MS_LB_NOT_USED;
824 static void ms_lib_clear_writebuf(struct us_data *us)
827 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
829 info->MS_Lib.wrtblk = (u16)-1;
830 ms_lib_clear_pagemap(info);
832 if (info->MS_Lib.blkpag)
833 memset(info->MS_Lib.blkpag, 0xff, info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector);
835 if (info->MS_Lib.blkext) {
836 for (i = 0; i < info->MS_Lib.PagesPerBlock; i++) {
837 info->MS_Lib.blkext[i].status1 = MS_REG_ST1_DEFAULT;
838 info->MS_Lib.blkext[i].ovrflg = MS_REG_OVR_DEFAULT;
839 info->MS_Lib.blkext[i].mngflg = MS_REG_MNG_DEFAULT;
840 info->MS_Lib.blkext[i].logadr = MS_LB_NOT_USED;
845 static int ms_count_freeblock(struct us_data *us, u16 PhyBlock)
848 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
850 Ende = PhyBlock + MS_PHYSICAL_BLOCKS_PER_SEGMENT;
851 for (Count = 0; PhyBlock < Ende; PhyBlock++) {
852 switch (info->MS_Lib.Phy2LogMap[PhyBlock]) {
854 case MS_LB_NOT_USED_ERASED:
865 static int ms_read_readpage(struct us_data *us, u32 PhyBlockAddr,
866 u8 PageNum, u32 *PageBuf, struct ms_lib_type_extdat *ExtraDat)
868 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
869 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
870 u8 *bbuf = info->bbuf;
872 u32 bn = PhyBlockAddr * 0x20 + PageNum;
874 result = ene_load_bincode(us, MS_RW_PATTERN);
875 if (result != USB_STOR_XFER_GOOD)
876 return USB_STOR_TRANSPORT_ERROR;
879 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
880 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
881 bcb->DataTransferLength = 0x200;
882 bcb->Flags = US_BULK_FLAG_IN;
885 bcb->CDB[1] = 0x02; /* in init.c ENE_MSInit() is 0x01 */
887 bcb->CDB[5] = (unsigned char)(bn);
888 bcb->CDB[4] = (unsigned char)(bn>>8);
889 bcb->CDB[3] = (unsigned char)(bn>>16);
890 bcb->CDB[2] = (unsigned char)(bn>>24);
892 result = ene_send_scsi_cmd(us, FDIR_READ, PageBuf, 0);
893 if (result != USB_STOR_XFER_GOOD)
894 return USB_STOR_TRANSPORT_ERROR;
897 /* Read Extra Data */
898 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
899 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
900 bcb->DataTransferLength = 0x4;
901 bcb->Flags = US_BULK_FLAG_IN;
905 bcb->CDB[5] = (unsigned char)(PageNum);
906 bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
907 bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
908 bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
911 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
912 if (result != USB_STOR_XFER_GOOD)
913 return USB_STOR_TRANSPORT_ERROR;
915 ExtraDat->reserved = 0;
916 ExtraDat->intr = 0x80; /* Not yet,fireware support */
917 ExtraDat->status0 = 0x10; /* Not yet,fireware support */
919 ExtraDat->status1 = 0x00; /* Not yet,fireware support */
920 ExtraDat->ovrflg = bbuf[0];
921 ExtraDat->mngflg = bbuf[1];
922 ExtraDat->logadr = memstick_logaddr(bbuf[2], bbuf[3]);
924 return USB_STOR_TRANSPORT_GOOD;
927 static int ms_lib_process_bootblock(struct us_data *us, u16 PhyBlock, u8 *PageData)
929 struct ms_bootblock_sysent *SysEntry;
930 struct ms_bootblock_sysinf *SysInfo;
934 struct ms_lib_type_extdat ExtraData;
935 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
937 PageBuffer = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
938 if (PageBuffer == NULL)
943 SysInfo = &(((struct ms_bootblock_page0 *)PageData)->sysinf);
945 if ((SysInfo->bMsClass != MS_SYSINF_MSCLASS_TYPE_1) ||
946 (be16_to_cpu(SysInfo->wPageSize) != MS_SYSINF_PAGE_SIZE) ||
947 ((SysInfo->bSecuritySupport & MS_SYSINF_SECURITY) == MS_SYSINF_SECURITY_SUPPORT) ||
948 (SysInfo->bReserved1 != MS_SYSINF_RESERVED1) ||
949 (SysInfo->bReserved2 != MS_SYSINF_RESERVED2) ||
950 (SysInfo->bFormatType != MS_SYSINF_FORMAT_FAT) ||
951 (SysInfo->bUsage != MS_SYSINF_USAGE_GENERAL))
954 switch (info->MS_Lib.cardType = SysInfo->bCardType) {
955 case MS_SYSINF_CARDTYPE_RDONLY:
956 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY);
958 case MS_SYSINF_CARDTYPE_RDWR:
959 ms_lib_ctrl_reset(info, MS_LIB_CTRL_RDONLY);
961 case MS_SYSINF_CARDTYPE_HYBRID:
966 info->MS_Lib.blockSize = be16_to_cpu(SysInfo->wBlockSize);
967 info->MS_Lib.NumberOfPhyBlock = be16_to_cpu(SysInfo->wBlockNumber);
968 info->MS_Lib.NumberOfLogBlock = be16_to_cpu(SysInfo->wTotalBlockNumber)-2;
969 info->MS_Lib.PagesPerBlock = info->MS_Lib.blockSize * SIZE_OF_KIRO / MS_BYTES_PER_PAGE;
970 info->MS_Lib.NumberOfSegment = info->MS_Lib.NumberOfPhyBlock / MS_PHYSICAL_BLOCKS_PER_SEGMENT;
971 info->MS_Model = be16_to_cpu(SysInfo->wMemorySize);
973 /*Allocate to all number of logicalblock and physicalblock */
974 if (ms_lib_alloc_logicalmap(us))
977 /* Mark the book block */
978 ms_lib_set_bootblockmark(us, PhyBlock);
980 SysEntry = &(((struct ms_bootblock_page0 *)PageData)->sysent);
982 for (i = 0; i < MS_NUMBER_OF_SYSTEM_ENTRY; i++) {
983 u32 EntryOffset, EntrySize;
985 EntryOffset = be32_to_cpu(SysEntry->entry[i].dwStart);
987 if (EntryOffset == 0xffffff)
989 EntrySize = be32_to_cpu(SysEntry->entry[i].dwSize);
994 if (EntryOffset + MS_BYTES_PER_PAGE + EntrySize > info->MS_Lib.blockSize * (u32)SIZE_OF_KIRO)
998 u8 PrevPageNumber = 0;
1001 if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_INVALID_BLOCK)
1004 while (EntrySize > 0) {
1006 PageNumber = (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1);
1007 if (PageNumber != PrevPageNumber) {
1008 switch (ms_read_readpage(us, PhyBlock, PageNumber, (u32 *)PageBuffer, &ExtraData)) {
1009 case MS_STATUS_SUCCESS:
1011 case MS_STATUS_WRITE_PROTECT:
1012 case MS_ERROR_FLASH_READ:
1013 case MS_STATUS_ERROR:
1018 PrevPageNumber = PageNumber;
1021 phyblk = be16_to_cpu(*(u16 *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)));
1022 if (phyblk < 0x0fff)
1023 ms_lib_set_initialerrorblock(us, phyblk);
1028 } else if (i == 1) { /* CIS/IDI */
1029 struct ms_bootblock_idi *idi;
1031 if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_CIS_IDI)
1034 switch (ms_read_readpage(us, PhyBlock, (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1), (u32 *)PageBuffer, &ExtraData)) {
1035 case MS_STATUS_SUCCESS:
1037 case MS_STATUS_WRITE_PROTECT:
1038 case MS_ERROR_FLASH_READ:
1039 case MS_STATUS_ERROR:
1044 idi = &((struct ms_bootblock_cis_idi *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)))->idi.idi;
1045 if (le16_to_cpu(idi->wIDIgeneralConfiguration) != MS_IDI_GENERAL_CONF)
1048 info->MS_Lib.BytesPerSector = le16_to_cpu(idi->wIDIbytesPerSector);
1049 if (info->MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE)
1058 ms_lib_free_logicalmap(us);
1066 static void ms_lib_free_writebuf(struct us_data *us)
1068 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1069 info->MS_Lib.wrtblk = (u16)-1; /* set to -1 */
1071 /* memset((fdoExt)->MS_Lib.pagemap, 0, sizeof((fdoExt)->MS_Lib.pagemap)) */
1073 ms_lib_clear_pagemap(info); /* (pdx)->MS_Lib.pagemap memset 0 in ms.h */
1075 if (info->MS_Lib.blkpag) {
1076 kfree(info->MS_Lib.blkpag); /* Arnold test ... */
1077 info->MS_Lib.blkpag = NULL;
1080 if (info->MS_Lib.blkext) {
1081 kfree(info->MS_Lib.blkext); /* Arnold test ... */
1082 info->MS_Lib.blkext = NULL;
1087 static void ms_lib_free_allocatedarea(struct us_data *us)
1089 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1091 ms_lib_free_writebuf(us); /* Free MS_Lib.pagemap */
1092 ms_lib_free_logicalmap(us); /* kfree MS_Lib.Phy2LogMap and MS_Lib.Log2PhyMap */
1094 /* set struct us point flag to 0 */
1095 info->MS_Lib.flags = 0;
1096 info->MS_Lib.BytesPerSector = 0;
1097 info->MS_Lib.SectorsPerCylinder = 0;
1099 info->MS_Lib.cardType = 0;
1100 info->MS_Lib.blockSize = 0;
1101 info->MS_Lib.PagesPerBlock = 0;
1103 info->MS_Lib.NumberOfPhyBlock = 0;
1104 info->MS_Lib.NumberOfLogBlock = 0;
1108 static int ms_lib_alloc_writebuf(struct us_data *us)
1110 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1112 info->MS_Lib.wrtblk = (u16)-1;
1114 info->MS_Lib.blkpag = kmalloc_array(info->MS_Lib.PagesPerBlock,
1115 info->MS_Lib.BytesPerSector,
1117 info->MS_Lib.blkext = kmalloc_array(info->MS_Lib.PagesPerBlock,
1118 sizeof(struct ms_lib_type_extdat),
1121 if ((info->MS_Lib.blkpag == NULL) || (info->MS_Lib.blkext == NULL)) {
1122 ms_lib_free_writebuf(us);
1126 ms_lib_clear_writebuf(us);
1131 static int ms_lib_force_setlogical_pair(struct us_data *us, u16 logblk, u16 phyblk)
1133 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1135 if (logblk == MS_LB_NOT_USED)
1138 if ((logblk >= info->MS_Lib.NumberOfLogBlock) ||
1139 (phyblk >= info->MS_Lib.NumberOfPhyBlock))
1142 info->MS_Lib.Phy2LogMap[phyblk] = logblk;
1143 info->MS_Lib.Log2PhyMap[logblk] = phyblk;
1148 static int ms_read_copyblock(struct us_data *us, u16 oldphy, u16 newphy,
1149 u16 PhyBlockAddr, u8 PageNum, unsigned char *buf, u16 len)
1151 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1154 result = ene_load_bincode(us, MS_RW_PATTERN);
1155 if (result != USB_STOR_XFER_GOOD)
1156 return USB_STOR_TRANSPORT_ERROR;
1158 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1159 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1160 bcb->DataTransferLength = 0x200*len;
1164 bcb->CDB[4] = (unsigned char)(oldphy);
1165 bcb->CDB[3] = (unsigned char)(oldphy>>8);
1166 bcb->CDB[2] = 0; /* (BYTE)(oldphy>>16) */
1167 bcb->CDB[7] = (unsigned char)(newphy);
1168 bcb->CDB[6] = (unsigned char)(newphy>>8);
1169 bcb->CDB[5] = 0; /* (BYTE)(newphy>>16) */
1170 bcb->CDB[9] = (unsigned char)(PhyBlockAddr);
1171 bcb->CDB[8] = (unsigned char)(PhyBlockAddr>>8);
1172 bcb->CDB[10] = PageNum;
1174 result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1175 if (result != USB_STOR_XFER_GOOD)
1176 return USB_STOR_TRANSPORT_ERROR;
1178 return USB_STOR_TRANSPORT_GOOD;
1181 static int ms_read_eraseblock(struct us_data *us, u32 PhyBlockAddr)
1183 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1185 u32 bn = PhyBlockAddr;
1187 result = ene_load_bincode(us, MS_RW_PATTERN);
1188 if (result != USB_STOR_XFER_GOOD)
1189 return USB_STOR_TRANSPORT_ERROR;
1191 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1192 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1193 bcb->DataTransferLength = 0x200;
1194 bcb->Flags = US_BULK_FLAG_IN;
1197 bcb->CDB[4] = (unsigned char)(bn);
1198 bcb->CDB[3] = (unsigned char)(bn>>8);
1199 bcb->CDB[2] = (unsigned char)(bn>>16);
1201 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1202 if (result != USB_STOR_XFER_GOOD)
1203 return USB_STOR_TRANSPORT_ERROR;
1205 return USB_STOR_TRANSPORT_GOOD;
1208 static int ms_lib_check_disableblock(struct us_data *us, u16 PhyBlock)
1210 unsigned char *PageBuf = NULL;
1211 u16 result = MS_STATUS_SUCCESS;
1213 struct ms_lib_type_extdat extdat;
1214 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1216 PageBuf = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1217 if (PageBuf == NULL) {
1218 result = MS_NO_MEMORY_ERROR;
1222 ms_read_readpage(us, PhyBlock, 1, (u32 *)PageBuf, &extdat);
1224 blk = be16_to_cpu(PageBuf[index]);
1225 if (blk == MS_LB_NOT_USED)
1227 if (blk == info->MS_Lib.Log2PhyMap[0]) {
1228 result = MS_ERROR_FLASH_READ;
1239 static int ms_lib_setacquired_errorblock(struct us_data *us, u16 phyblk)
1242 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1244 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1247 log = info->MS_Lib.Phy2LogMap[phyblk];
1249 if (log < info->MS_Lib.NumberOfLogBlock)
1250 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1252 if (info->MS_Lib.Phy2LogMap[phyblk] != MS_LB_INITIAL_ERROR)
1253 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_ACQUIRED_ERROR;
1258 static int ms_lib_overwrite_extra(struct us_data *us, u32 PhyBlockAddr,
1259 u8 PageNum, u8 OverwriteFlag)
1261 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1264 result = ene_load_bincode(us, MS_RW_PATTERN);
1265 if (result != USB_STOR_XFER_GOOD)
1266 return USB_STOR_TRANSPORT_ERROR;
1268 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1269 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1270 bcb->DataTransferLength = 0x4;
1271 bcb->Flags = US_BULK_FLAG_IN;
1274 bcb->CDB[5] = (unsigned char)(PageNum);
1275 bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
1276 bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
1277 bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
1278 bcb->CDB[6] = OverwriteFlag;
1283 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1284 if (result != USB_STOR_XFER_GOOD)
1285 return USB_STOR_TRANSPORT_ERROR;
1287 return USB_STOR_TRANSPORT_GOOD;
1290 static int ms_lib_error_phyblock(struct us_data *us, u16 phyblk)
1292 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1294 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1295 return MS_STATUS_ERROR;
1297 ms_lib_setacquired_errorblock(us, phyblk);
1299 if (ms_lib_iswritable(info))
1300 return ms_lib_overwrite_extra(us, phyblk, 0, (u8)(~MS_REG_OVR_BKST & BYTE_MASK));
1302 return MS_STATUS_SUCCESS;
1305 static int ms_lib_erase_phyblock(struct us_data *us, u16 phyblk)
1308 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1310 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1311 return MS_STATUS_ERROR;
1313 log = info->MS_Lib.Phy2LogMap[phyblk];
1315 if (log < info->MS_Lib.NumberOfLogBlock)
1316 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1318 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED;
1320 if (ms_lib_iswritable(info)) {
1321 switch (ms_read_eraseblock(us, phyblk)) {
1322 case MS_STATUS_SUCCESS:
1323 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED_ERASED;
1324 return MS_STATUS_SUCCESS;
1325 case MS_ERROR_FLASH_ERASE:
1326 case MS_STATUS_INT_ERROR:
1327 ms_lib_error_phyblock(us, phyblk);
1328 return MS_ERROR_FLASH_ERASE;
1329 case MS_STATUS_ERROR:
1331 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY); /* MS_LibCtrlSet will used by ENE_MSInit ,need check, and why us to info*/
1332 ms_lib_setacquired_errorblock(us, phyblk);
1333 return MS_STATUS_ERROR;
1337 ms_lib_setacquired_errorblock(us, phyblk);
1339 return MS_STATUS_SUCCESS;
1342 static int ms_lib_read_extra(struct us_data *us, u32 PhyBlock,
1343 u8 PageNum, struct ms_lib_type_extdat *ExtraDat)
1345 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1346 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1347 u8 *bbuf = info->bbuf;
1350 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1351 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1352 bcb->DataTransferLength = 0x4;
1353 bcb->Flags = US_BULK_FLAG_IN;
1356 bcb->CDB[5] = (unsigned char)(PageNum);
1357 bcb->CDB[4] = (unsigned char)(PhyBlock);
1358 bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
1359 bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
1362 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
1363 if (result != USB_STOR_XFER_GOOD)
1364 return USB_STOR_TRANSPORT_ERROR;
1366 ExtraDat->reserved = 0;
1367 ExtraDat->intr = 0x80; /* Not yet, waiting for fireware support */
1368 ExtraDat->status0 = 0x10; /* Not yet, waiting for fireware support */
1369 ExtraDat->status1 = 0x00; /* Not yet, waiting for fireware support */
1370 ExtraDat->ovrflg = bbuf[0];
1371 ExtraDat->mngflg = bbuf[1];
1372 ExtraDat->logadr = memstick_logaddr(bbuf[2], bbuf[3]);
1374 return USB_STOR_TRANSPORT_GOOD;
1377 static int ms_libsearch_block_from_physical(struct us_data *us, u16 phyblk)
1380 struct ms_lib_type_extdat extdat; /* need check */
1381 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1384 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1387 for (blk = phyblk + 1; blk != phyblk; blk++) {
1388 if ((blk & MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK) == 0)
1389 blk -= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1391 if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED_ERASED) {
1393 } else if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED) {
1394 switch (ms_lib_read_extra(us, blk, 0, &extdat)) {
1395 case MS_STATUS_SUCCESS:
1396 case MS_STATUS_SUCCESS_WITH_ECC:
1398 case MS_NOCARD_ERROR:
1399 return MS_NOCARD_ERROR;
1400 case MS_STATUS_INT_ERROR:
1402 case MS_ERROR_FLASH_READ:
1404 ms_lib_setacquired_errorblock(us, blk);
1408 if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1409 ms_lib_setacquired_errorblock(us, blk);
1413 switch (ms_lib_erase_phyblock(us, blk)) {
1414 case MS_STATUS_SUCCESS:
1416 case MS_STATUS_ERROR:
1418 case MS_ERROR_FLASH_ERASE:
1420 ms_lib_error_phyblock(us, blk);
1428 static int ms_libsearch_block_from_logical(struct us_data *us, u16 logblk)
1431 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1433 phyblk = ms_libconv_to_physical(info, logblk);
1434 if (phyblk >= MS_LB_ERROR) {
1435 if (logblk >= info->MS_Lib.NumberOfLogBlock)
1438 phyblk = (logblk + MS_NUMBER_OF_BOOT_BLOCK) / MS_LOGICAL_BLOCKS_PER_SEGMENT;
1439 phyblk *= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1440 phyblk += MS_PHYSICAL_BLOCKS_PER_SEGMENT - 1;
1443 return ms_libsearch_block_from_physical(us, phyblk);
1446 static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
1448 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
1450 /* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
1451 if ((info->MS_Status & MS_Insert) && (info->MS_Status & MS_Ready)) {
1452 return USB_STOR_TRANSPORT_GOOD;
1455 return USB_STOR_TRANSPORT_GOOD;
1458 return USB_STOR_TRANSPORT_GOOD;
1461 static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
1463 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1464 unsigned char mediaNoWP[12] = {
1465 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
1466 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1467 unsigned char mediaWP[12] = {
1468 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
1469 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1471 if (info->MS_Status & MS_WtP)
1472 usb_stor_set_xfer_buf(mediaWP, 12, srb);
1474 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
1476 return USB_STOR_TRANSPORT_GOOD;
1479 static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
1483 unsigned int offset = 0;
1484 unsigned char buf[8];
1485 struct scatterlist *sg = NULL;
1486 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1488 usb_stor_dbg(us, "ms_scsi_read_capacity\n");
1490 if (info->MS_Status & MS_IsMSPro)
1491 bl_num = info->MSP_TotalBlock - 1;
1493 bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1;
1495 info->bl_num = bl_num;
1496 usb_stor_dbg(us, "bl_len = %x\n", bl_len);
1497 usb_stor_dbg(us, "bl_num = %x\n", bl_num);
1499 /*srb->request_bufflen = 8; */
1500 buf[0] = (bl_num >> 24) & 0xff;
1501 buf[1] = (bl_num >> 16) & 0xff;
1502 buf[2] = (bl_num >> 8) & 0xff;
1503 buf[3] = (bl_num >> 0) & 0xff;
1504 buf[4] = (bl_len >> 24) & 0xff;
1505 buf[5] = (bl_len >> 16) & 0xff;
1506 buf[6] = (bl_len >> 8) & 0xff;
1507 buf[7] = (bl_len >> 0) & 0xff;
1509 usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
1511 return USB_STOR_TRANSPORT_GOOD;
1514 static void ms_lib_phy_to_log_range(u16 PhyBlock, u16 *LogStart, u16 *LogEnde)
1516 PhyBlock /= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1519 *LogStart = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT + (PhyBlock - 1) * MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1520 *LogEnde = *LogStart + MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1523 *LogEnde = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT;/*494*/
1527 static int ms_lib_read_extrablock(struct us_data *us, u32 PhyBlock,
1528 u8 PageNum, u8 blen, void *buf)
1530 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1533 /* Read Extra Data */
1534 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1535 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1536 bcb->DataTransferLength = 0x4 * blen;
1537 bcb->Flags = US_BULK_FLAG_IN;
1540 bcb->CDB[5] = (unsigned char)(PageNum);
1541 bcb->CDB[4] = (unsigned char)(PhyBlock);
1542 bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
1543 bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
1546 result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1547 if (result != USB_STOR_XFER_GOOD)
1548 return USB_STOR_TRANSPORT_ERROR;
1550 return USB_STOR_TRANSPORT_GOOD;
1553 static int ms_lib_scan_logicalblocknumber(struct us_data *us, u16 btBlk1st)
1555 u16 PhyBlock, newblk, i;
1556 u16 LogStart, LogEnde;
1557 struct ms_lib_type_extdat extdat;
1558 u32 count = 0, index = 0;
1559 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1560 u8 *bbuf = info->bbuf;
1562 for (PhyBlock = 0; PhyBlock < info->MS_Lib.NumberOfPhyBlock;) {
1563 ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde);
1565 for (i = 0; i < MS_PHYSICAL_BLOCKS_PER_SEGMENT; i++, PhyBlock++) {
1566 switch (ms_libconv_to_logical(info, PhyBlock)) {
1567 case MS_STATUS_ERROR:
1573 if (count == PhyBlock) {
1574 ms_lib_read_extrablock(us, PhyBlock, 0, 0x80,
1578 index = (PhyBlock % 0x80) * 4;
1580 extdat.ovrflg = bbuf[index];
1581 extdat.mngflg = bbuf[index+1];
1582 extdat.logadr = memstick_logaddr(bbuf[index+2],
1585 if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1586 ms_lib_setacquired_errorblock(us, PhyBlock);
1590 if ((extdat.mngflg & MS_REG_MNG_ATFLG) == MS_REG_MNG_ATFLG_ATTBL) {
1591 ms_lib_erase_phyblock(us, PhyBlock);
1595 if (extdat.logadr != MS_LB_NOT_USED) {
1596 if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) {
1597 ms_lib_erase_phyblock(us, PhyBlock);
1601 newblk = ms_libconv_to_physical(info, extdat.logadr);
1603 if (newblk != MS_LB_NOT_USED) {
1604 if (extdat.logadr == 0) {
1605 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1606 if (ms_lib_check_disableblock(us, btBlk1st)) {
1607 ms_lib_set_logicalpair(us, extdat.logadr, newblk);
1612 ms_lib_read_extra(us, newblk, 0, &extdat);
1613 if ((extdat.ovrflg & MS_REG_OVR_UDST) == MS_REG_OVR_UDST_UPDATING) {
1614 ms_lib_erase_phyblock(us, PhyBlock);
1617 ms_lib_erase_phyblock(us, newblk);
1621 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1626 return MS_STATUS_SUCCESS;
1630 static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
1633 unsigned char *cdb = srb->cmnd;
1634 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1635 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1637 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
1638 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
1639 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1640 u32 blenByte = blen * 0x200;
1642 if (bn > info->bl_num)
1643 return USB_STOR_TRANSPORT_ERROR;
1645 if (info->MS_Status & MS_IsMSPro) {
1646 result = ene_load_bincode(us, MSP_RW_PATTERN);
1647 if (result != USB_STOR_XFER_GOOD) {
1648 usb_stor_dbg(us, "Load MPS RW pattern Fail !!\n");
1649 return USB_STOR_TRANSPORT_ERROR;
1652 /* set up the command wrapper */
1653 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1654 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1655 bcb->DataTransferLength = blenByte;
1656 bcb->Flags = US_BULK_FLAG_IN;
1659 bcb->CDB[5] = (unsigned char)(bn);
1660 bcb->CDB[4] = (unsigned char)(bn>>8);
1661 bcb->CDB[3] = (unsigned char)(bn>>16);
1662 bcb->CDB[2] = (unsigned char)(bn>>24);
1664 result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
1673 buf = kmalloc(blenByte, GFP_KERNEL);
1675 return USB_STOR_TRANSPORT_ERROR;
1677 result = ene_load_bincode(us, MS_RW_PATTERN);
1678 if (result != USB_STOR_XFER_GOOD) {
1679 pr_info("Load MS RW pattern Fail !!\n");
1680 result = USB_STOR_TRANSPORT_ERROR;
1684 logblk = (u16)(bn / info->MS_Lib.PagesPerBlock);
1685 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1688 if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1689 len = info->MS_Lib.PagesPerBlock-PageNum;
1693 phyblk = ms_libconv_to_physical(info, logblk);
1694 blkno = phyblk * 0x20 + PageNum;
1696 /* set up the command wrapper */
1697 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1698 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1699 bcb->DataTransferLength = 0x200 * len;
1700 bcb->Flags = US_BULK_FLAG_IN;
1703 bcb->CDB[5] = (unsigned char)(blkno);
1704 bcb->CDB[4] = (unsigned char)(blkno>>8);
1705 bcb->CDB[3] = (unsigned char)(blkno>>16);
1706 bcb->CDB[2] = (unsigned char)(blkno>>24);
1708 result = ene_send_scsi_cmd(us, FDIR_READ, buf+offset, 0);
1709 if (result != USB_STOR_XFER_GOOD) {
1710 pr_info("MS_SCSI_Read --- result = %x\n", result);
1711 result = USB_STOR_TRANSPORT_ERROR;
1720 offset += MS_BYTES_PER_PAGE*len;
1722 usb_stor_set_xfer_buf(buf, blenByte, srb);
1729 static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
1732 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1733 unsigned char *cdb = srb->cmnd;
1734 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1736 u32 bn = ((cdb[2] << 24) & 0xff000000) |
1737 ((cdb[3] << 16) & 0x00ff0000) |
1738 ((cdb[4] << 8) & 0x0000ff00) |
1739 ((cdb[5] << 0) & 0x000000ff);
1740 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1741 u32 blenByte = blen * 0x200;
1743 if (bn > info->bl_num)
1744 return USB_STOR_TRANSPORT_ERROR;
1746 if (info->MS_Status & MS_IsMSPro) {
1747 result = ene_load_bincode(us, MSP_RW_PATTERN);
1748 if (result != USB_STOR_XFER_GOOD) {
1749 pr_info("Load MSP RW pattern Fail !!\n");
1750 return USB_STOR_TRANSPORT_ERROR;
1753 /* set up the command wrapper */
1754 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1755 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1756 bcb->DataTransferLength = blenByte;
1760 bcb->CDB[5] = (unsigned char)(bn);
1761 bcb->CDB[4] = (unsigned char)(bn>>8);
1762 bcb->CDB[3] = (unsigned char)(bn>>16);
1763 bcb->CDB[2] = (unsigned char)(bn>>24);
1765 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
1771 u16 len, oldphy, newphy;
1773 buf = kmalloc(blenByte, GFP_KERNEL);
1775 return USB_STOR_TRANSPORT_ERROR;
1776 usb_stor_set_xfer_buf(buf, blenByte, srb);
1778 result = ene_load_bincode(us, MS_RW_PATTERN);
1779 if (result != USB_STOR_XFER_GOOD) {
1780 pr_info("Load MS RW pattern Fail !!\n");
1781 result = USB_STOR_TRANSPORT_ERROR;
1785 PhyBlockAddr = (u16)(bn / info->MS_Lib.PagesPerBlock);
1786 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1789 if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1790 len = info->MS_Lib.PagesPerBlock-PageNum;
1794 oldphy = ms_libconv_to_physical(info, PhyBlockAddr); /* need check us <-> info */
1795 newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr);
1797 result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len);
1799 if (result != USB_STOR_XFER_GOOD) {
1800 pr_info("MS_SCSI_Write --- result = %x\n", result);
1801 result = USB_STOR_TRANSPORT_ERROR;
1805 info->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED;
1806 ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy);
1813 offset += MS_BYTES_PER_PAGE*len;
1825 static int ene_get_card_type(struct us_data *us, u16 index, void *buf)
1827 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1830 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1831 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1832 bcb->DataTransferLength = 0x01;
1833 bcb->Flags = US_BULK_FLAG_IN;
1835 bcb->CDB[2] = (unsigned char)(index>>8);
1836 bcb->CDB[3] = (unsigned char)index;
1838 result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1842 static int ene_get_card_status(struct us_data *us, u8 *buf)
1846 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1848 /*usb_stor_dbg(us, "transport --- ENE_ReadSDReg\n");*/
1849 reg4b = *(u32 *)&buf[0x18];
1850 info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f);
1852 tmpreg = (u16) reg4b;
1853 reg4b = *(u32 *)(&buf[0x14]);
1854 if ((info->SD_Status & SD_HiCapacity) && !(info->SD_Status & SD_IsMMC))
1855 info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
1857 info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
1858 info->SD_C_SIZE_MULT = (u8)(reg4b >> 7) & 0x07;
1859 if ((info->SD_Status & SD_HiCapacity) && (info->SD_Status & SD_IsMMC))
1860 info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
1862 if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
1863 info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN);
1864 info->SD_READ_BL_LEN = SD_BLOCK_LEN;
1866 info->SD_Block_Mult = 1;
1869 return USB_STOR_TRANSPORT_GOOD;
1872 static int ene_load_bincode(struct us_data *us, unsigned char flag)
1875 char *fw_name = NULL;
1876 unsigned char *buf = NULL;
1877 const struct firmware *sd_fw = NULL;
1878 int result = USB_STOR_TRANSPORT_ERROR;
1879 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1880 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1882 if (info->BIN_FLAG == flag)
1883 return USB_STOR_TRANSPORT_GOOD;
1887 case SD_INIT1_PATTERN:
1888 usb_stor_dbg(us, "SD_INIT1_PATTERN\n");
1889 fw_name = SD_INIT1_FIRMWARE;
1891 case SD_INIT2_PATTERN:
1892 usb_stor_dbg(us, "SD_INIT2_PATTERN\n");
1893 fw_name = SD_INIT2_FIRMWARE;
1896 usb_stor_dbg(us, "SD_RW_PATTERN\n");
1897 fw_name = SD_RW_FIRMWARE;
1900 case MS_INIT_PATTERN:
1901 usb_stor_dbg(us, "MS_INIT_PATTERN\n");
1902 fw_name = MS_INIT_FIRMWARE;
1904 case MSP_RW_PATTERN:
1905 usb_stor_dbg(us, "MSP_RW_PATTERN\n");
1906 fw_name = MSP_RW_FIRMWARE;
1909 usb_stor_dbg(us, "MS_RW_PATTERN\n");
1910 fw_name = MS_RW_FIRMWARE;
1913 usb_stor_dbg(us, "----------- Unknown PATTERN ----------\n");
1917 err = reject_firmware(&sd_fw, fw_name, &us->pusb_dev->dev);
1919 usb_stor_dbg(us, "load firmware %s failed\n", fw_name);
1922 buf = kmemdup(sd_fw->data, sd_fw->size, GFP_KERNEL);
1926 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1927 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1928 bcb->DataTransferLength = sd_fw->size;
1932 result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1933 if (us->srb != NULL)
1934 scsi_set_resid(us->srb, 0);
1935 info->BIN_FLAG = flag;
1939 release_firmware(sd_fw);
1943 static int ms_card_init(struct us_data *us)
1947 unsigned char *PageBuffer0 = NULL, *PageBuffer1 = NULL;
1948 struct ms_lib_type_extdat extdat;
1949 u16 btBlk1st, btBlk2nd;
1951 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1953 printk(KERN_INFO "MS_CardInit start\n");
1955 ms_lib_free_allocatedarea(us); /* Clean buffer and set struct us_data flag to 0 */
1957 /* get two PageBuffer */
1958 PageBuffer0 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1959 PageBuffer1 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1960 if ((PageBuffer0 == NULL) || (PageBuffer1 == NULL)) {
1961 result = MS_NO_MEMORY_ERROR;
1965 btBlk1st = btBlk2nd = MS_LB_NOT_USED;
1968 for (TmpBlock = 0; TmpBlock < MS_MAX_INITIAL_ERROR_BLOCKS+2; TmpBlock++) {
1970 switch (ms_read_readpage(us, TmpBlock, 0, (u32 *)PageBuffer0, &extdat)) {
1971 case MS_STATUS_SUCCESS:
1973 case MS_STATUS_INT_ERROR:
1975 case MS_STATUS_ERROR:
1980 if ((extdat.ovrflg & MS_REG_OVR_BKST) == MS_REG_OVR_BKST_NG)
1983 if (((extdat.mngflg & MS_REG_MNG_SYSFLG) == MS_REG_MNG_SYSFLG_USER) ||
1984 (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wBlockID) != MS_BOOT_BLOCK_ID) ||
1985 (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wFormatVersion) != MS_BOOT_BLOCK_FORMAT_VERSION) ||
1986 (((struct ms_bootblock_page0 *)PageBuffer0)->header.bNumberOfDataEntry != MS_BOOT_BLOCK_DATA_ENTRIES))
1989 if (btBlk1st != MS_LB_NOT_USED) {
1990 btBlk2nd = TmpBlock;
1994 btBlk1st = TmpBlock;
1995 memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE);
1996 if (extdat.status1 & (MS_REG_ST1_DTER | MS_REG_ST1_EXER | MS_REG_ST1_FGER))
2000 if (btBlk1st == MS_LB_NOT_USED) {
2001 result = MS_STATUS_ERROR;
2006 if ((extdat.status0 & MS_REG_ST0_WP) == MS_REG_ST0_WP_ON)
2007 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2009 result = MS_STATUS_ERROR;
2010 /* 1st Boot Block */
2011 if (btBlk1stErred == 0)
2012 result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1);
2014 /* 2nd Boot Block */
2015 if (result && (btBlk2nd != MS_LB_NOT_USED))
2016 result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0);
2019 result = MS_STATUS_ERROR;
2023 for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++)
2024 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2026 info->MS_Lib.Phy2LogMap[btBlk1st] = MS_LB_BOOT_BLOCK;
2028 if (btBlk2nd != MS_LB_NOT_USED) {
2029 for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++)
2030 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2032 info->MS_Lib.Phy2LogMap[btBlk2nd] = MS_LB_BOOT_BLOCK;
2035 result = ms_lib_scan_logicalblocknumber(us, btBlk1st);
2039 for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT;
2040 TmpBlock < info->MS_Lib.NumberOfPhyBlock;
2041 TmpBlock += MS_PHYSICAL_BLOCKS_PER_SEGMENT) {
2042 if (ms_count_freeblock(us, TmpBlock) == 0) {
2043 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2049 if (ms_lib_alloc_writebuf(us)) {
2050 result = MS_NO_MEMORY_ERROR;
2054 result = MS_STATUS_SUCCESS;
2060 printk(KERN_INFO "MS_CardInit end\n");
2064 static int ene_ms_init(struct us_data *us)
2066 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2068 u16 MSP_BlockSize, MSP_UserAreaBlocks;
2069 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2070 u8 *bbuf = info->bbuf;
2073 printk(KERN_INFO "transport --- ENE_MSInit\n");
2075 /* the same part to test ENE */
2077 result = ene_load_bincode(us, MS_INIT_PATTERN);
2078 if (result != USB_STOR_XFER_GOOD) {
2079 printk(KERN_ERR "Load MS Init Code Fail !!\n");
2080 return USB_STOR_TRANSPORT_ERROR;
2083 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2084 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2085 bcb->DataTransferLength = 0x200;
2086 bcb->Flags = US_BULK_FLAG_IN;
2090 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
2091 if (result != USB_STOR_XFER_GOOD) {
2092 printk(KERN_ERR "Execution MS Init Code Fail !!\n");
2093 return USB_STOR_TRANSPORT_ERROR;
2095 /* the same part to test ENE */
2096 info->MS_Status = bbuf[0];
2098 s = info->MS_Status;
2099 if ((s & MS_Insert) && (s & MS_Ready)) {
2100 printk(KERN_INFO "Insert = %x\n", !!(s & MS_Insert));
2101 printk(KERN_INFO "Ready = %x\n", !!(s & MS_Ready));
2102 printk(KERN_INFO "IsMSPro = %x\n", !!(s & MS_IsMSPro));
2103 printk(KERN_INFO "IsMSPHG = %x\n", !!(s & MS_IsMSPHG));
2104 printk(KERN_INFO "WtP= %x\n", !!(s & MS_WtP));
2105 if (s & MS_IsMSPro) {
2106 MSP_BlockSize = (bbuf[6] << 8) | bbuf[7];
2107 MSP_UserAreaBlocks = (bbuf[10] << 8) | bbuf[11];
2108 info->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
2110 ms_card_init(us); /* Card is MS (to ms.c)*/
2112 usb_stor_dbg(us, "MS Init Code OK !!\n");
2114 usb_stor_dbg(us, "MS Card Not Ready --- %x\n", bbuf[0]);
2115 return USB_STOR_TRANSPORT_ERROR;
2118 return USB_STOR_TRANSPORT_GOOD;
2121 static int ene_sd_init(struct us_data *us)
2124 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2125 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2126 u8 *bbuf = info->bbuf;
2128 usb_stor_dbg(us, "transport --- ENE_SDInit\n");
2129 /* SD Init Part-1 */
2130 result = ene_load_bincode(us, SD_INIT1_PATTERN);
2131 if (result != USB_STOR_XFER_GOOD) {
2132 usb_stor_dbg(us, "Load SD Init Code Part-1 Fail !!\n");
2133 return USB_STOR_TRANSPORT_ERROR;
2136 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2137 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2138 bcb->Flags = US_BULK_FLAG_IN;
2141 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
2142 if (result != USB_STOR_XFER_GOOD) {
2143 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2144 return USB_STOR_TRANSPORT_ERROR;
2147 /* SD Init Part-2 */
2148 result = ene_load_bincode(us, SD_INIT2_PATTERN);
2149 if (result != USB_STOR_XFER_GOOD) {
2150 usb_stor_dbg(us, "Load SD Init Code Part-2 Fail !!\n");
2151 return USB_STOR_TRANSPORT_ERROR;
2154 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2155 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2156 bcb->DataTransferLength = 0x200;
2157 bcb->Flags = US_BULK_FLAG_IN;
2160 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
2161 if (result != USB_STOR_XFER_GOOD) {
2162 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2163 return USB_STOR_TRANSPORT_ERROR;
2166 info->SD_Status = bbuf[0];
2167 if ((info->SD_Status & SD_Insert) && (info->SD_Status & SD_Ready)) {
2168 unsigned int s = info->SD_Status;
2170 ene_get_card_status(us, bbuf);
2171 usb_stor_dbg(us, "Insert = %x\n", !!(s & SD_Insert));
2172 usb_stor_dbg(us, "Ready = %x\n", !!(s & SD_Ready));
2173 usb_stor_dbg(us, "IsMMC = %x\n", !!(s & SD_IsMMC));
2174 usb_stor_dbg(us, "HiCapacity = %x\n", !!(s & SD_HiCapacity));
2175 usb_stor_dbg(us, "HiSpeed = %x\n", !!(s & SD_HiSpeed));
2176 usb_stor_dbg(us, "WtP = %x\n", !!(s & SD_WtP));
2178 usb_stor_dbg(us, "SD Card Not Ready --- %x\n", bbuf[0]);
2179 return USB_STOR_TRANSPORT_ERROR;
2181 return USB_STOR_TRANSPORT_GOOD;
2185 static int ene_init(struct us_data *us)
2189 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2190 u8 *bbuf = info->bbuf;
2192 result = ene_get_card_type(us, REG_CARD_STATUS, bbuf);
2193 if (result != USB_STOR_XFER_GOOD)
2194 return USB_STOR_TRANSPORT_ERROR;
2196 misc_reg03 = bbuf[0];
2197 if (misc_reg03 & 0x01) {
2198 if (!(info->SD_Status & SD_Ready)) {
2199 result = ene_sd_init(us);
2200 if (result != USB_STOR_XFER_GOOD)
2201 return USB_STOR_TRANSPORT_ERROR;
2204 if (misc_reg03 & 0x02) {
2205 if (!(info->MS_Status & MS_Ready)) {
2206 result = ene_ms_init(us);
2207 if (result != USB_STOR_XFER_GOOD)
2208 return USB_STOR_TRANSPORT_ERROR;
2214 /*----- sd_scsi_irp() ---------*/
2215 static int sd_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
2218 struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2220 switch (srb->cmnd[0]) {
2221 case TEST_UNIT_READY:
2222 result = sd_scsi_test_unit_ready(us, srb);
2225 result = do_scsi_request_sense(us, srb);
2228 result = do_scsi_inquiry(us, srb);
2231 result = sd_scsi_mode_sense(us, srb);
2235 result = SD_SCSI_Start_Stop(us, srb);
2239 result = sd_scsi_read_capacity(us, srb);
2242 result = sd_scsi_read(us, srb);
2245 result = sd_scsi_write(us, srb);
2248 info->SrbStatus = SS_ILLEGAL_REQUEST;
2249 result = USB_STOR_TRANSPORT_FAILED;
2252 if (result == USB_STOR_TRANSPORT_GOOD)
2253 info->SrbStatus = SS_SUCCESS;
2260 static int ms_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
2263 struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2265 switch (srb->cmnd[0]) {
2266 case TEST_UNIT_READY:
2267 result = ms_scsi_test_unit_ready(us, srb);
2270 result = do_scsi_request_sense(us, srb);
2273 result = do_scsi_inquiry(us, srb);
2276 result = ms_scsi_mode_sense(us, srb);
2279 result = ms_scsi_read_capacity(us, srb);
2282 result = ms_scsi_read(us, srb);
2285 result = ms_scsi_write(us, srb);
2288 info->SrbStatus = SS_ILLEGAL_REQUEST;
2289 result = USB_STOR_TRANSPORT_FAILED;
2292 if (result == USB_STOR_TRANSPORT_GOOD)
2293 info->SrbStatus = SS_SUCCESS;
2297 static int ene_transport(struct scsi_cmnd *srb, struct us_data *us)
2299 int result = USB_STOR_XFER_GOOD;
2300 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2302 /*US_DEBUG(usb_stor_show_command(us, srb)); */
2303 scsi_set_resid(srb, 0);
2304 if (unlikely(!(info->SD_Status & SD_Ready) || (info->MS_Status & MS_Ready)))
2305 result = ene_init(us);
2306 if (result == USB_STOR_XFER_GOOD) {
2307 result = USB_STOR_TRANSPORT_ERROR;
2308 if (info->SD_Status & SD_Ready)
2309 result = sd_scsi_irp(us, srb);
2311 if (info->MS_Status & MS_Ready)
2312 result = ms_scsi_irp(us, srb);
2317 static struct scsi_host_template ene_ub6250_host_template;
2319 static int ene_ub6250_probe(struct usb_interface *intf,
2320 const struct usb_device_id *id)
2325 struct ene_ub6250_info *info;
2327 result = usb_stor_probe1(&us, intf, id,
2328 (id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list,
2329 &ene_ub6250_host_template);
2333 /* FIXME: where should the code alloc extra buf ? */
2334 us->extra = kzalloc(sizeof(struct ene_ub6250_info), GFP_KERNEL);
2337 us->extra_destructor = ene_ub6250_info_destructor;
2339 info = (struct ene_ub6250_info *)(us->extra);
2340 info->bbuf = kmalloc(512, GFP_KERNEL);
2346 us->transport_name = "ene_ub6250";
2347 us->transport = ene_transport;
2350 result = usb_stor_probe2(us);
2354 /* probe card type */
2355 result = ene_get_card_type(us, REG_CARD_STATUS, info->bbuf);
2356 if (result != USB_STOR_XFER_GOOD) {
2357 usb_stor_disconnect(intf);
2358 return USB_STOR_TRANSPORT_ERROR;
2361 misc_reg03 = info->bbuf[0];
2362 if (!(misc_reg03 & 0x01)) {
2363 pr_info("ums_eneub6250: This driver only supports SD/MS cards. "
2364 "It does not support SM cards.\n");
2373 static int ene_ub6250_resume(struct usb_interface *iface)
2375 struct us_data *us = usb_get_intfdata(iface);
2376 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2378 mutex_lock(&us->dev_mutex);
2380 if (us->suspend_resume_hook)
2381 (us->suspend_resume_hook)(us, US_RESUME);
2383 mutex_unlock(&us->dev_mutex);
2385 info->Power_IsResum = true;
2386 /* info->SD_Status &= ~SD_Ready; */
2387 info->SD_Status = 0;
2388 info->MS_Status = 0;
2389 info->SM_Status = 0;
2394 static int ene_ub6250_reset_resume(struct usb_interface *iface)
2396 struct us_data *us = usb_get_intfdata(iface);
2397 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2399 /* Report the reset to the SCSI core */
2400 usb_stor_reset_resume(iface);
2403 * FIXME: Notify the subdrivers that they need to reinitialize
2406 info->Power_IsResum = true;
2407 /* info->SD_Status &= ~SD_Ready; */
2408 info->SD_Status = 0;
2409 info->MS_Status = 0;
2410 info->SM_Status = 0;
2417 #define ene_ub6250_resume NULL
2418 #define ene_ub6250_reset_resume NULL
2422 static struct usb_driver ene_ub6250_driver = {
2424 .probe = ene_ub6250_probe,
2425 .disconnect = usb_stor_disconnect,
2426 .suspend = usb_stor_suspend,
2427 .resume = ene_ub6250_resume,
2428 .reset_resume = ene_ub6250_reset_resume,
2429 .pre_reset = usb_stor_pre_reset,
2430 .post_reset = usb_stor_post_reset,
2431 .id_table = ene_ub6250_usb_ids,
2436 module_usb_stor_driver(ene_ub6250_driver, ene_ub6250_host_template, DRV_NAME);