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:
864 static int ms_read_readpage(struct us_data *us, u32 PhyBlockAddr,
865 u8 PageNum, u32 *PageBuf, struct ms_lib_type_extdat *ExtraDat)
867 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
868 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
869 u8 *bbuf = info->bbuf;
871 u32 bn = PhyBlockAddr * 0x20 + PageNum;
873 result = ene_load_bincode(us, MS_RW_PATTERN);
874 if (result != USB_STOR_XFER_GOOD)
875 return USB_STOR_TRANSPORT_ERROR;
878 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
879 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
880 bcb->DataTransferLength = 0x200;
881 bcb->Flags = US_BULK_FLAG_IN;
884 bcb->CDB[1] = 0x02; /* in init.c ENE_MSInit() is 0x01 */
886 bcb->CDB[5] = (unsigned char)(bn);
887 bcb->CDB[4] = (unsigned char)(bn>>8);
888 bcb->CDB[3] = (unsigned char)(bn>>16);
889 bcb->CDB[2] = (unsigned char)(bn>>24);
891 result = ene_send_scsi_cmd(us, FDIR_READ, PageBuf, 0);
892 if (result != USB_STOR_XFER_GOOD)
893 return USB_STOR_TRANSPORT_ERROR;
896 /* Read Extra Data */
897 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
898 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
899 bcb->DataTransferLength = 0x4;
900 bcb->Flags = US_BULK_FLAG_IN;
904 bcb->CDB[5] = (unsigned char)(PageNum);
905 bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
906 bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
907 bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
910 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
911 if (result != USB_STOR_XFER_GOOD)
912 return USB_STOR_TRANSPORT_ERROR;
914 ExtraDat->reserved = 0;
915 ExtraDat->intr = 0x80; /* Not yet,fireware support */
916 ExtraDat->status0 = 0x10; /* Not yet,fireware support */
918 ExtraDat->status1 = 0x00; /* Not yet,fireware support */
919 ExtraDat->ovrflg = bbuf[0];
920 ExtraDat->mngflg = bbuf[1];
921 ExtraDat->logadr = memstick_logaddr(bbuf[2], bbuf[3]);
923 return USB_STOR_TRANSPORT_GOOD;
926 static int ms_lib_process_bootblock(struct us_data *us, u16 PhyBlock, u8 *PageData)
928 struct ms_bootblock_sysent *SysEntry;
929 struct ms_bootblock_sysinf *SysInfo;
933 struct ms_lib_type_extdat ExtraData;
934 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
936 PageBuffer = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
937 if (PageBuffer == NULL)
942 SysInfo = &(((struct ms_bootblock_page0 *)PageData)->sysinf);
944 if ((SysInfo->bMsClass != MS_SYSINF_MSCLASS_TYPE_1) ||
945 (be16_to_cpu(SysInfo->wPageSize) != MS_SYSINF_PAGE_SIZE) ||
946 ((SysInfo->bSecuritySupport & MS_SYSINF_SECURITY) == MS_SYSINF_SECURITY_SUPPORT) ||
947 (SysInfo->bReserved1 != MS_SYSINF_RESERVED1) ||
948 (SysInfo->bReserved2 != MS_SYSINF_RESERVED2) ||
949 (SysInfo->bFormatType != MS_SYSINF_FORMAT_FAT) ||
950 (SysInfo->bUsage != MS_SYSINF_USAGE_GENERAL))
953 switch (info->MS_Lib.cardType = SysInfo->bCardType) {
954 case MS_SYSINF_CARDTYPE_RDONLY:
955 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY);
957 case MS_SYSINF_CARDTYPE_RDWR:
958 ms_lib_ctrl_reset(info, MS_LIB_CTRL_RDONLY);
960 case MS_SYSINF_CARDTYPE_HYBRID:
965 info->MS_Lib.blockSize = be16_to_cpu(SysInfo->wBlockSize);
966 info->MS_Lib.NumberOfPhyBlock = be16_to_cpu(SysInfo->wBlockNumber);
967 info->MS_Lib.NumberOfLogBlock = be16_to_cpu(SysInfo->wTotalBlockNumber)-2;
968 info->MS_Lib.PagesPerBlock = info->MS_Lib.blockSize * SIZE_OF_KIRO / MS_BYTES_PER_PAGE;
969 info->MS_Lib.NumberOfSegment = info->MS_Lib.NumberOfPhyBlock / MS_PHYSICAL_BLOCKS_PER_SEGMENT;
970 info->MS_Model = be16_to_cpu(SysInfo->wMemorySize);
972 /*Allocate to all number of logicalblock and physicalblock */
973 if (ms_lib_alloc_logicalmap(us))
976 /* Mark the book block */
977 ms_lib_set_bootblockmark(us, PhyBlock);
979 SysEntry = &(((struct ms_bootblock_page0 *)PageData)->sysent);
981 for (i = 0; i < MS_NUMBER_OF_SYSTEM_ENTRY; i++) {
982 u32 EntryOffset, EntrySize;
984 EntryOffset = be32_to_cpu(SysEntry->entry[i].dwStart);
986 if (EntryOffset == 0xffffff)
988 EntrySize = be32_to_cpu(SysEntry->entry[i].dwSize);
993 if (EntryOffset + MS_BYTES_PER_PAGE + EntrySize > info->MS_Lib.blockSize * (u32)SIZE_OF_KIRO)
997 u8 PrevPageNumber = 0;
1000 if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_INVALID_BLOCK)
1003 while (EntrySize > 0) {
1005 PageNumber = (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1);
1006 if (PageNumber != PrevPageNumber) {
1007 switch (ms_read_readpage(us, PhyBlock, PageNumber, (u32 *)PageBuffer, &ExtraData)) {
1008 case MS_STATUS_SUCCESS:
1010 case MS_STATUS_WRITE_PROTECT:
1011 case MS_ERROR_FLASH_READ:
1012 case MS_STATUS_ERROR:
1017 PrevPageNumber = PageNumber;
1020 phyblk = be16_to_cpu(*(u16 *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)));
1021 if (phyblk < 0x0fff)
1022 ms_lib_set_initialerrorblock(us, phyblk);
1027 } else if (i == 1) { /* CIS/IDI */
1028 struct ms_bootblock_idi *idi;
1030 if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_CIS_IDI)
1033 switch (ms_read_readpage(us, PhyBlock, (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1), (u32 *)PageBuffer, &ExtraData)) {
1034 case MS_STATUS_SUCCESS:
1036 case MS_STATUS_WRITE_PROTECT:
1037 case MS_ERROR_FLASH_READ:
1038 case MS_STATUS_ERROR:
1043 idi = &((struct ms_bootblock_cis_idi *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)))->idi.idi;
1044 if (le16_to_cpu(idi->wIDIgeneralConfiguration) != MS_IDI_GENERAL_CONF)
1047 info->MS_Lib.BytesPerSector = le16_to_cpu(idi->wIDIbytesPerSector);
1048 if (info->MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE)
1057 ms_lib_free_logicalmap(us);
1065 static void ms_lib_free_writebuf(struct us_data *us)
1067 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1068 info->MS_Lib.wrtblk = (u16)-1; /* set to -1 */
1070 /* memset((fdoExt)->MS_Lib.pagemap, 0, sizeof((fdoExt)->MS_Lib.pagemap)) */
1072 ms_lib_clear_pagemap(info); /* (pdx)->MS_Lib.pagemap memset 0 in ms.h */
1074 if (info->MS_Lib.blkpag) {
1075 kfree(info->MS_Lib.blkpag); /* Arnold test ... */
1076 info->MS_Lib.blkpag = NULL;
1079 if (info->MS_Lib.blkext) {
1080 kfree(info->MS_Lib.blkext); /* Arnold test ... */
1081 info->MS_Lib.blkext = NULL;
1086 static void ms_lib_free_allocatedarea(struct us_data *us)
1088 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1090 ms_lib_free_writebuf(us); /* Free MS_Lib.pagemap */
1091 ms_lib_free_logicalmap(us); /* kfree MS_Lib.Phy2LogMap and MS_Lib.Log2PhyMap */
1093 /* set struct us point flag to 0 */
1094 info->MS_Lib.flags = 0;
1095 info->MS_Lib.BytesPerSector = 0;
1096 info->MS_Lib.SectorsPerCylinder = 0;
1098 info->MS_Lib.cardType = 0;
1099 info->MS_Lib.blockSize = 0;
1100 info->MS_Lib.PagesPerBlock = 0;
1102 info->MS_Lib.NumberOfPhyBlock = 0;
1103 info->MS_Lib.NumberOfLogBlock = 0;
1107 static int ms_lib_alloc_writebuf(struct us_data *us)
1109 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1111 info->MS_Lib.wrtblk = (u16)-1;
1113 info->MS_Lib.blkpag = kmalloc_array(info->MS_Lib.PagesPerBlock,
1114 info->MS_Lib.BytesPerSector,
1116 info->MS_Lib.blkext = kmalloc_array(info->MS_Lib.PagesPerBlock,
1117 sizeof(struct ms_lib_type_extdat),
1120 if ((info->MS_Lib.blkpag == NULL) || (info->MS_Lib.blkext == NULL)) {
1121 ms_lib_free_writebuf(us);
1125 ms_lib_clear_writebuf(us);
1130 static int ms_lib_force_setlogical_pair(struct us_data *us, u16 logblk, u16 phyblk)
1132 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1134 if (logblk == MS_LB_NOT_USED)
1137 if ((logblk >= info->MS_Lib.NumberOfLogBlock) ||
1138 (phyblk >= info->MS_Lib.NumberOfPhyBlock))
1141 info->MS_Lib.Phy2LogMap[phyblk] = logblk;
1142 info->MS_Lib.Log2PhyMap[logblk] = phyblk;
1147 static int ms_read_copyblock(struct us_data *us, u16 oldphy, u16 newphy,
1148 u16 PhyBlockAddr, u8 PageNum, unsigned char *buf, u16 len)
1150 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1153 result = ene_load_bincode(us, MS_RW_PATTERN);
1154 if (result != USB_STOR_XFER_GOOD)
1155 return USB_STOR_TRANSPORT_ERROR;
1157 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1158 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1159 bcb->DataTransferLength = 0x200*len;
1163 bcb->CDB[4] = (unsigned char)(oldphy);
1164 bcb->CDB[3] = (unsigned char)(oldphy>>8);
1165 bcb->CDB[2] = 0; /* (BYTE)(oldphy>>16) */
1166 bcb->CDB[7] = (unsigned char)(newphy);
1167 bcb->CDB[6] = (unsigned char)(newphy>>8);
1168 bcb->CDB[5] = 0; /* (BYTE)(newphy>>16) */
1169 bcb->CDB[9] = (unsigned char)(PhyBlockAddr);
1170 bcb->CDB[8] = (unsigned char)(PhyBlockAddr>>8);
1171 bcb->CDB[10] = PageNum;
1173 result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1174 if (result != USB_STOR_XFER_GOOD)
1175 return USB_STOR_TRANSPORT_ERROR;
1177 return USB_STOR_TRANSPORT_GOOD;
1180 static int ms_read_eraseblock(struct us_data *us, u32 PhyBlockAddr)
1182 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1184 u32 bn = PhyBlockAddr;
1186 result = ene_load_bincode(us, MS_RW_PATTERN);
1187 if (result != USB_STOR_XFER_GOOD)
1188 return USB_STOR_TRANSPORT_ERROR;
1190 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1191 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1192 bcb->DataTransferLength = 0x200;
1193 bcb->Flags = US_BULK_FLAG_IN;
1196 bcb->CDB[4] = (unsigned char)(bn);
1197 bcb->CDB[3] = (unsigned char)(bn>>8);
1198 bcb->CDB[2] = (unsigned char)(bn>>16);
1200 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1201 if (result != USB_STOR_XFER_GOOD)
1202 return USB_STOR_TRANSPORT_ERROR;
1204 return USB_STOR_TRANSPORT_GOOD;
1207 static int ms_lib_check_disableblock(struct us_data *us, u16 PhyBlock)
1209 unsigned char *PageBuf = NULL;
1210 u16 result = MS_STATUS_SUCCESS;
1212 struct ms_lib_type_extdat extdat;
1213 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1215 PageBuf = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1216 if (PageBuf == NULL) {
1217 result = MS_NO_MEMORY_ERROR;
1221 ms_read_readpage(us, PhyBlock, 1, (u32 *)PageBuf, &extdat);
1223 blk = be16_to_cpu(PageBuf[index]);
1224 if (blk == MS_LB_NOT_USED)
1226 if (blk == info->MS_Lib.Log2PhyMap[0]) {
1227 result = MS_ERROR_FLASH_READ;
1238 static int ms_lib_setacquired_errorblock(struct us_data *us, u16 phyblk)
1241 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1243 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1246 log = info->MS_Lib.Phy2LogMap[phyblk];
1248 if (log < info->MS_Lib.NumberOfLogBlock)
1249 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1251 if (info->MS_Lib.Phy2LogMap[phyblk] != MS_LB_INITIAL_ERROR)
1252 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_ACQUIRED_ERROR;
1257 static int ms_lib_overwrite_extra(struct us_data *us, u32 PhyBlockAddr,
1258 u8 PageNum, u8 OverwriteFlag)
1260 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1263 result = ene_load_bincode(us, MS_RW_PATTERN);
1264 if (result != USB_STOR_XFER_GOOD)
1265 return USB_STOR_TRANSPORT_ERROR;
1267 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1268 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1269 bcb->DataTransferLength = 0x4;
1270 bcb->Flags = US_BULK_FLAG_IN;
1273 bcb->CDB[5] = (unsigned char)(PageNum);
1274 bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
1275 bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
1276 bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
1277 bcb->CDB[6] = OverwriteFlag;
1282 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1283 if (result != USB_STOR_XFER_GOOD)
1284 return USB_STOR_TRANSPORT_ERROR;
1286 return USB_STOR_TRANSPORT_GOOD;
1289 static int ms_lib_error_phyblock(struct us_data *us, u16 phyblk)
1291 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1293 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1294 return MS_STATUS_ERROR;
1296 ms_lib_setacquired_errorblock(us, phyblk);
1298 if (ms_lib_iswritable(info))
1299 return ms_lib_overwrite_extra(us, phyblk, 0, (u8)(~MS_REG_OVR_BKST & BYTE_MASK));
1301 return MS_STATUS_SUCCESS;
1304 static int ms_lib_erase_phyblock(struct us_data *us, u16 phyblk)
1307 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1309 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1310 return MS_STATUS_ERROR;
1312 log = info->MS_Lib.Phy2LogMap[phyblk];
1314 if (log < info->MS_Lib.NumberOfLogBlock)
1315 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1317 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED;
1319 if (ms_lib_iswritable(info)) {
1320 switch (ms_read_eraseblock(us, phyblk)) {
1321 case MS_STATUS_SUCCESS:
1322 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED_ERASED;
1323 return MS_STATUS_SUCCESS;
1324 case MS_ERROR_FLASH_ERASE:
1325 case MS_STATUS_INT_ERROR:
1326 ms_lib_error_phyblock(us, phyblk);
1327 return MS_ERROR_FLASH_ERASE;
1328 case MS_STATUS_ERROR:
1330 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY); /* MS_LibCtrlSet will used by ENE_MSInit ,need check, and why us to info*/
1331 ms_lib_setacquired_errorblock(us, phyblk);
1332 return MS_STATUS_ERROR;
1336 ms_lib_setacquired_errorblock(us, phyblk);
1338 return MS_STATUS_SUCCESS;
1341 static int ms_lib_read_extra(struct us_data *us, u32 PhyBlock,
1342 u8 PageNum, struct ms_lib_type_extdat *ExtraDat)
1344 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1345 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1346 u8 *bbuf = info->bbuf;
1349 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1350 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1351 bcb->DataTransferLength = 0x4;
1352 bcb->Flags = US_BULK_FLAG_IN;
1355 bcb->CDB[5] = (unsigned char)(PageNum);
1356 bcb->CDB[4] = (unsigned char)(PhyBlock);
1357 bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
1358 bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
1361 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
1362 if (result != USB_STOR_XFER_GOOD)
1363 return USB_STOR_TRANSPORT_ERROR;
1365 ExtraDat->reserved = 0;
1366 ExtraDat->intr = 0x80; /* Not yet, waiting for fireware support */
1367 ExtraDat->status0 = 0x10; /* Not yet, waiting for fireware support */
1368 ExtraDat->status1 = 0x00; /* Not yet, waiting for fireware support */
1369 ExtraDat->ovrflg = bbuf[0];
1370 ExtraDat->mngflg = bbuf[1];
1371 ExtraDat->logadr = memstick_logaddr(bbuf[2], bbuf[3]);
1373 return USB_STOR_TRANSPORT_GOOD;
1376 static int ms_libsearch_block_from_physical(struct us_data *us, u16 phyblk)
1379 struct ms_lib_type_extdat extdat; /* need check */
1380 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1383 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1386 for (blk = phyblk + 1; blk != phyblk; blk++) {
1387 if ((blk & MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK) == 0)
1388 blk -= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1390 if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED_ERASED) {
1392 } else if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED) {
1393 switch (ms_lib_read_extra(us, blk, 0, &extdat)) {
1394 case MS_STATUS_SUCCESS:
1395 case MS_STATUS_SUCCESS_WITH_ECC:
1397 case MS_NOCARD_ERROR:
1398 return MS_NOCARD_ERROR;
1399 case MS_STATUS_INT_ERROR:
1401 case MS_ERROR_FLASH_READ:
1403 ms_lib_setacquired_errorblock(us, blk);
1407 if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1408 ms_lib_setacquired_errorblock(us, blk);
1412 switch (ms_lib_erase_phyblock(us, blk)) {
1413 case MS_STATUS_SUCCESS:
1415 case MS_STATUS_ERROR:
1417 case MS_ERROR_FLASH_ERASE:
1419 ms_lib_error_phyblock(us, blk);
1427 static int ms_libsearch_block_from_logical(struct us_data *us, u16 logblk)
1430 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1432 phyblk = ms_libconv_to_physical(info, logblk);
1433 if (phyblk >= MS_LB_ERROR) {
1434 if (logblk >= info->MS_Lib.NumberOfLogBlock)
1437 phyblk = (logblk + MS_NUMBER_OF_BOOT_BLOCK) / MS_LOGICAL_BLOCKS_PER_SEGMENT;
1438 phyblk *= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1439 phyblk += MS_PHYSICAL_BLOCKS_PER_SEGMENT - 1;
1442 return ms_libsearch_block_from_physical(us, phyblk);
1445 static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
1447 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
1449 /* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
1450 if ((info->MS_Status & MS_Insert) && (info->MS_Status & MS_Ready)) {
1451 return USB_STOR_TRANSPORT_GOOD;
1454 return USB_STOR_TRANSPORT_GOOD;
1457 return USB_STOR_TRANSPORT_GOOD;
1460 static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
1462 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1463 unsigned char mediaNoWP[12] = {
1464 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
1465 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1466 unsigned char mediaWP[12] = {
1467 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
1468 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1470 if (info->MS_Status & MS_WtP)
1471 usb_stor_set_xfer_buf(mediaWP, 12, srb);
1473 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
1475 return USB_STOR_TRANSPORT_GOOD;
1478 static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
1482 unsigned int offset = 0;
1483 unsigned char buf[8];
1484 struct scatterlist *sg = NULL;
1485 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1487 usb_stor_dbg(us, "ms_scsi_read_capacity\n");
1489 if (info->MS_Status & MS_IsMSPro)
1490 bl_num = info->MSP_TotalBlock - 1;
1492 bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1;
1494 info->bl_num = bl_num;
1495 usb_stor_dbg(us, "bl_len = %x\n", bl_len);
1496 usb_stor_dbg(us, "bl_num = %x\n", bl_num);
1498 /*srb->request_bufflen = 8; */
1499 buf[0] = (bl_num >> 24) & 0xff;
1500 buf[1] = (bl_num >> 16) & 0xff;
1501 buf[2] = (bl_num >> 8) & 0xff;
1502 buf[3] = (bl_num >> 0) & 0xff;
1503 buf[4] = (bl_len >> 24) & 0xff;
1504 buf[5] = (bl_len >> 16) & 0xff;
1505 buf[6] = (bl_len >> 8) & 0xff;
1506 buf[7] = (bl_len >> 0) & 0xff;
1508 usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
1510 return USB_STOR_TRANSPORT_GOOD;
1513 static void ms_lib_phy_to_log_range(u16 PhyBlock, u16 *LogStart, u16 *LogEnde)
1515 PhyBlock /= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1518 *LogStart = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT + (PhyBlock - 1) * MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1519 *LogEnde = *LogStart + MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1522 *LogEnde = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT;/*494*/
1526 static int ms_lib_read_extrablock(struct us_data *us, u32 PhyBlock,
1527 u8 PageNum, u8 blen, void *buf)
1529 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1532 /* Read Extra Data */
1533 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1534 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1535 bcb->DataTransferLength = 0x4 * blen;
1536 bcb->Flags = US_BULK_FLAG_IN;
1539 bcb->CDB[5] = (unsigned char)(PageNum);
1540 bcb->CDB[4] = (unsigned char)(PhyBlock);
1541 bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
1542 bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
1545 result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1546 if (result != USB_STOR_XFER_GOOD)
1547 return USB_STOR_TRANSPORT_ERROR;
1549 return USB_STOR_TRANSPORT_GOOD;
1552 static int ms_lib_scan_logicalblocknumber(struct us_data *us, u16 btBlk1st)
1554 u16 PhyBlock, newblk, i;
1555 u16 LogStart, LogEnde;
1556 struct ms_lib_type_extdat extdat;
1557 u32 count = 0, index = 0;
1558 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1559 u8 *bbuf = info->bbuf;
1561 for (PhyBlock = 0; PhyBlock < info->MS_Lib.NumberOfPhyBlock;) {
1562 ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde);
1564 for (i = 0; i < MS_PHYSICAL_BLOCKS_PER_SEGMENT; i++, PhyBlock++) {
1565 switch (ms_libconv_to_logical(info, PhyBlock)) {
1566 case MS_STATUS_ERROR:
1572 if (count == PhyBlock) {
1573 ms_lib_read_extrablock(us, PhyBlock, 0, 0x80,
1577 index = (PhyBlock % 0x80) * 4;
1579 extdat.ovrflg = bbuf[index];
1580 extdat.mngflg = bbuf[index+1];
1581 extdat.logadr = memstick_logaddr(bbuf[index+2],
1584 if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1585 ms_lib_setacquired_errorblock(us, PhyBlock);
1589 if ((extdat.mngflg & MS_REG_MNG_ATFLG) == MS_REG_MNG_ATFLG_ATTBL) {
1590 ms_lib_erase_phyblock(us, PhyBlock);
1594 if (extdat.logadr != MS_LB_NOT_USED) {
1595 if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) {
1596 ms_lib_erase_phyblock(us, PhyBlock);
1600 newblk = ms_libconv_to_physical(info, extdat.logadr);
1602 if (newblk != MS_LB_NOT_USED) {
1603 if (extdat.logadr == 0) {
1604 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1605 if (ms_lib_check_disableblock(us, btBlk1st)) {
1606 ms_lib_set_logicalpair(us, extdat.logadr, newblk);
1611 ms_lib_read_extra(us, newblk, 0, &extdat);
1612 if ((extdat.ovrflg & MS_REG_OVR_UDST) == MS_REG_OVR_UDST_UPDATING) {
1613 ms_lib_erase_phyblock(us, PhyBlock);
1616 ms_lib_erase_phyblock(us, newblk);
1620 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1625 return MS_STATUS_SUCCESS;
1629 static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
1632 unsigned char *cdb = srb->cmnd;
1633 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1634 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1636 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
1637 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
1638 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1639 u32 blenByte = blen * 0x200;
1641 if (bn > info->bl_num)
1642 return USB_STOR_TRANSPORT_ERROR;
1644 if (info->MS_Status & MS_IsMSPro) {
1645 result = ene_load_bincode(us, MSP_RW_PATTERN);
1646 if (result != USB_STOR_XFER_GOOD) {
1647 usb_stor_dbg(us, "Load MPS RW pattern Fail !!\n");
1648 return USB_STOR_TRANSPORT_ERROR;
1651 /* set up the command wrapper */
1652 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1653 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1654 bcb->DataTransferLength = blenByte;
1655 bcb->Flags = US_BULK_FLAG_IN;
1658 bcb->CDB[5] = (unsigned char)(bn);
1659 bcb->CDB[4] = (unsigned char)(bn>>8);
1660 bcb->CDB[3] = (unsigned char)(bn>>16);
1661 bcb->CDB[2] = (unsigned char)(bn>>24);
1663 result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
1672 buf = kmalloc(blenByte, GFP_KERNEL);
1674 return USB_STOR_TRANSPORT_ERROR;
1676 result = ene_load_bincode(us, MS_RW_PATTERN);
1677 if (result != USB_STOR_XFER_GOOD) {
1678 pr_info("Load MS RW pattern Fail !!\n");
1679 result = USB_STOR_TRANSPORT_ERROR;
1683 logblk = (u16)(bn / info->MS_Lib.PagesPerBlock);
1684 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1687 if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1688 len = info->MS_Lib.PagesPerBlock-PageNum;
1692 phyblk = ms_libconv_to_physical(info, logblk);
1693 blkno = phyblk * 0x20 + PageNum;
1695 /* set up the command wrapper */
1696 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1697 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1698 bcb->DataTransferLength = 0x200 * len;
1699 bcb->Flags = US_BULK_FLAG_IN;
1702 bcb->CDB[5] = (unsigned char)(blkno);
1703 bcb->CDB[4] = (unsigned char)(blkno>>8);
1704 bcb->CDB[3] = (unsigned char)(blkno>>16);
1705 bcb->CDB[2] = (unsigned char)(blkno>>24);
1707 result = ene_send_scsi_cmd(us, FDIR_READ, buf+offset, 0);
1708 if (result != USB_STOR_XFER_GOOD) {
1709 pr_info("MS_SCSI_Read --- result = %x\n", result);
1710 result = USB_STOR_TRANSPORT_ERROR;
1719 offset += MS_BYTES_PER_PAGE*len;
1721 usb_stor_set_xfer_buf(buf, blenByte, srb);
1728 static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
1731 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1732 unsigned char *cdb = srb->cmnd;
1733 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1735 u32 bn = ((cdb[2] << 24) & 0xff000000) |
1736 ((cdb[3] << 16) & 0x00ff0000) |
1737 ((cdb[4] << 8) & 0x0000ff00) |
1738 ((cdb[5] << 0) & 0x000000ff);
1739 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1740 u32 blenByte = blen * 0x200;
1742 if (bn > info->bl_num)
1743 return USB_STOR_TRANSPORT_ERROR;
1745 if (info->MS_Status & MS_IsMSPro) {
1746 result = ene_load_bincode(us, MSP_RW_PATTERN);
1747 if (result != USB_STOR_XFER_GOOD) {
1748 pr_info("Load MSP RW pattern Fail !!\n");
1749 return USB_STOR_TRANSPORT_ERROR;
1752 /* set up the command wrapper */
1753 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1754 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1755 bcb->DataTransferLength = blenByte;
1759 bcb->CDB[5] = (unsigned char)(bn);
1760 bcb->CDB[4] = (unsigned char)(bn>>8);
1761 bcb->CDB[3] = (unsigned char)(bn>>16);
1762 bcb->CDB[2] = (unsigned char)(bn>>24);
1764 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
1770 u16 len, oldphy, newphy;
1772 buf = kmalloc(blenByte, GFP_KERNEL);
1774 return USB_STOR_TRANSPORT_ERROR;
1775 usb_stor_set_xfer_buf(buf, blenByte, srb);
1777 result = ene_load_bincode(us, MS_RW_PATTERN);
1778 if (result != USB_STOR_XFER_GOOD) {
1779 pr_info("Load MS RW pattern Fail !!\n");
1780 result = USB_STOR_TRANSPORT_ERROR;
1784 PhyBlockAddr = (u16)(bn / info->MS_Lib.PagesPerBlock);
1785 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1788 if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1789 len = info->MS_Lib.PagesPerBlock-PageNum;
1793 oldphy = ms_libconv_to_physical(info, PhyBlockAddr); /* need check us <-> info */
1794 newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr);
1796 result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len);
1798 if (result != USB_STOR_XFER_GOOD) {
1799 pr_info("MS_SCSI_Write --- result = %x\n", result);
1800 result = USB_STOR_TRANSPORT_ERROR;
1804 info->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED;
1805 ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy);
1812 offset += MS_BYTES_PER_PAGE*len;
1824 static int ene_get_card_type(struct us_data *us, u16 index, void *buf)
1826 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1829 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1830 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1831 bcb->DataTransferLength = 0x01;
1832 bcb->Flags = US_BULK_FLAG_IN;
1834 bcb->CDB[2] = (unsigned char)(index>>8);
1835 bcb->CDB[3] = (unsigned char)index;
1837 result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1841 static int ene_get_card_status(struct us_data *us, u8 *buf)
1845 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1847 /*usb_stor_dbg(us, "transport --- ENE_ReadSDReg\n");*/
1848 reg4b = *(u32 *)&buf[0x18];
1849 info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f);
1851 tmpreg = (u16) reg4b;
1852 reg4b = *(u32 *)(&buf[0x14]);
1853 if ((info->SD_Status & SD_HiCapacity) && !(info->SD_Status & SD_IsMMC))
1854 info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
1856 info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
1857 info->SD_C_SIZE_MULT = (u8)(reg4b >> 7) & 0x07;
1858 if ((info->SD_Status & SD_HiCapacity) && (info->SD_Status & SD_IsMMC))
1859 info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
1861 if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
1862 info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN);
1863 info->SD_READ_BL_LEN = SD_BLOCK_LEN;
1865 info->SD_Block_Mult = 1;
1868 return USB_STOR_TRANSPORT_GOOD;
1871 static int ene_load_bincode(struct us_data *us, unsigned char flag)
1874 char *fw_name = NULL;
1875 unsigned char *buf = NULL;
1876 const struct firmware *sd_fw = NULL;
1877 int result = USB_STOR_TRANSPORT_ERROR;
1878 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1879 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1881 if (info->BIN_FLAG == flag)
1882 return USB_STOR_TRANSPORT_GOOD;
1886 case SD_INIT1_PATTERN:
1887 usb_stor_dbg(us, "SD_INIT1_PATTERN\n");
1888 fw_name = SD_INIT1_FIRMWARE;
1890 case SD_INIT2_PATTERN:
1891 usb_stor_dbg(us, "SD_INIT2_PATTERN\n");
1892 fw_name = SD_INIT2_FIRMWARE;
1895 usb_stor_dbg(us, "SD_RW_PATTERN\n");
1896 fw_name = SD_RW_FIRMWARE;
1899 case MS_INIT_PATTERN:
1900 usb_stor_dbg(us, "MS_INIT_PATTERN\n");
1901 fw_name = MS_INIT_FIRMWARE;
1903 case MSP_RW_PATTERN:
1904 usb_stor_dbg(us, "MSP_RW_PATTERN\n");
1905 fw_name = MSP_RW_FIRMWARE;
1908 usb_stor_dbg(us, "MS_RW_PATTERN\n");
1909 fw_name = MS_RW_FIRMWARE;
1912 usb_stor_dbg(us, "----------- Unknown PATTERN ----------\n");
1916 err = reject_firmware(&sd_fw, fw_name, &us->pusb_dev->dev);
1918 usb_stor_dbg(us, "load firmware %s failed\n", fw_name);
1921 buf = kmemdup(sd_fw->data, sd_fw->size, GFP_KERNEL);
1925 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1926 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1927 bcb->DataTransferLength = sd_fw->size;
1931 result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1932 if (us->srb != NULL)
1933 scsi_set_resid(us->srb, 0);
1934 info->BIN_FLAG = flag;
1938 release_firmware(sd_fw);
1942 static int ms_card_init(struct us_data *us)
1946 unsigned char *PageBuffer0 = NULL, *PageBuffer1 = NULL;
1947 struct ms_lib_type_extdat extdat;
1948 u16 btBlk1st, btBlk2nd;
1950 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1952 printk(KERN_INFO "MS_CardInit start\n");
1954 ms_lib_free_allocatedarea(us); /* Clean buffer and set struct us_data flag to 0 */
1956 /* get two PageBuffer */
1957 PageBuffer0 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1958 PageBuffer1 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1959 if ((PageBuffer0 == NULL) || (PageBuffer1 == NULL)) {
1960 result = MS_NO_MEMORY_ERROR;
1964 btBlk1st = btBlk2nd = MS_LB_NOT_USED;
1967 for (TmpBlock = 0; TmpBlock < MS_MAX_INITIAL_ERROR_BLOCKS+2; TmpBlock++) {
1969 switch (ms_read_readpage(us, TmpBlock, 0, (u32 *)PageBuffer0, &extdat)) {
1970 case MS_STATUS_SUCCESS:
1972 case MS_STATUS_INT_ERROR:
1974 case MS_STATUS_ERROR:
1979 if ((extdat.ovrflg & MS_REG_OVR_BKST) == MS_REG_OVR_BKST_NG)
1982 if (((extdat.mngflg & MS_REG_MNG_SYSFLG) == MS_REG_MNG_SYSFLG_USER) ||
1983 (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wBlockID) != MS_BOOT_BLOCK_ID) ||
1984 (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wFormatVersion) != MS_BOOT_BLOCK_FORMAT_VERSION) ||
1985 (((struct ms_bootblock_page0 *)PageBuffer0)->header.bNumberOfDataEntry != MS_BOOT_BLOCK_DATA_ENTRIES))
1988 if (btBlk1st != MS_LB_NOT_USED) {
1989 btBlk2nd = TmpBlock;
1993 btBlk1st = TmpBlock;
1994 memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE);
1995 if (extdat.status1 & (MS_REG_ST1_DTER | MS_REG_ST1_EXER | MS_REG_ST1_FGER))
1999 if (btBlk1st == MS_LB_NOT_USED) {
2000 result = MS_STATUS_ERROR;
2005 if ((extdat.status0 & MS_REG_ST0_WP) == MS_REG_ST0_WP_ON)
2006 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2008 result = MS_STATUS_ERROR;
2009 /* 1st Boot Block */
2010 if (btBlk1stErred == 0)
2011 result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1);
2013 /* 2nd Boot Block */
2014 if (result && (btBlk2nd != MS_LB_NOT_USED))
2015 result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0);
2018 result = MS_STATUS_ERROR;
2022 for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++)
2023 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2025 info->MS_Lib.Phy2LogMap[btBlk1st] = MS_LB_BOOT_BLOCK;
2027 if (btBlk2nd != MS_LB_NOT_USED) {
2028 for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++)
2029 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2031 info->MS_Lib.Phy2LogMap[btBlk2nd] = MS_LB_BOOT_BLOCK;
2034 result = ms_lib_scan_logicalblocknumber(us, btBlk1st);
2038 for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT;
2039 TmpBlock < info->MS_Lib.NumberOfPhyBlock;
2040 TmpBlock += MS_PHYSICAL_BLOCKS_PER_SEGMENT) {
2041 if (ms_count_freeblock(us, TmpBlock) == 0) {
2042 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2048 if (ms_lib_alloc_writebuf(us)) {
2049 result = MS_NO_MEMORY_ERROR;
2053 result = MS_STATUS_SUCCESS;
2059 printk(KERN_INFO "MS_CardInit end\n");
2063 static int ene_ms_init(struct us_data *us)
2065 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2067 u16 MSP_BlockSize, MSP_UserAreaBlocks;
2068 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2069 u8 *bbuf = info->bbuf;
2072 printk(KERN_INFO "transport --- ENE_MSInit\n");
2074 /* the same part to test ENE */
2076 result = ene_load_bincode(us, MS_INIT_PATTERN);
2077 if (result != USB_STOR_XFER_GOOD) {
2078 printk(KERN_ERR "Load MS Init Code Fail !!\n");
2079 return USB_STOR_TRANSPORT_ERROR;
2082 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2083 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2084 bcb->DataTransferLength = 0x200;
2085 bcb->Flags = US_BULK_FLAG_IN;
2089 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
2090 if (result != USB_STOR_XFER_GOOD) {
2091 printk(KERN_ERR "Execution MS Init Code Fail !!\n");
2092 return USB_STOR_TRANSPORT_ERROR;
2094 /* the same part to test ENE */
2095 info->MS_Status = bbuf[0];
2097 s = info->MS_Status;
2098 if ((s & MS_Insert) && (s & MS_Ready)) {
2099 printk(KERN_INFO "Insert = %x\n", !!(s & MS_Insert));
2100 printk(KERN_INFO "Ready = %x\n", !!(s & MS_Ready));
2101 printk(KERN_INFO "IsMSPro = %x\n", !!(s & MS_IsMSPro));
2102 printk(KERN_INFO "IsMSPHG = %x\n", !!(s & MS_IsMSPHG));
2103 printk(KERN_INFO "WtP= %x\n", !!(s & MS_WtP));
2104 if (s & MS_IsMSPro) {
2105 MSP_BlockSize = (bbuf[6] << 8) | bbuf[7];
2106 MSP_UserAreaBlocks = (bbuf[10] << 8) | bbuf[11];
2107 info->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
2109 ms_card_init(us); /* Card is MS (to ms.c)*/
2111 usb_stor_dbg(us, "MS Init Code OK !!\n");
2113 usb_stor_dbg(us, "MS Card Not Ready --- %x\n", bbuf[0]);
2114 return USB_STOR_TRANSPORT_ERROR;
2117 return USB_STOR_TRANSPORT_GOOD;
2120 static int ene_sd_init(struct us_data *us)
2123 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2124 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2125 u8 *bbuf = info->bbuf;
2127 usb_stor_dbg(us, "transport --- ENE_SDInit\n");
2128 /* SD Init Part-1 */
2129 result = ene_load_bincode(us, SD_INIT1_PATTERN);
2130 if (result != USB_STOR_XFER_GOOD) {
2131 usb_stor_dbg(us, "Load SD Init Code Part-1 Fail !!\n");
2132 return USB_STOR_TRANSPORT_ERROR;
2135 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2136 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2137 bcb->Flags = US_BULK_FLAG_IN;
2140 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
2141 if (result != USB_STOR_XFER_GOOD) {
2142 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2143 return USB_STOR_TRANSPORT_ERROR;
2146 /* SD Init Part-2 */
2147 result = ene_load_bincode(us, SD_INIT2_PATTERN);
2148 if (result != USB_STOR_XFER_GOOD) {
2149 usb_stor_dbg(us, "Load SD Init Code Part-2 Fail !!\n");
2150 return USB_STOR_TRANSPORT_ERROR;
2153 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2154 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2155 bcb->DataTransferLength = 0x200;
2156 bcb->Flags = US_BULK_FLAG_IN;
2159 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
2160 if (result != USB_STOR_XFER_GOOD) {
2161 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2162 return USB_STOR_TRANSPORT_ERROR;
2165 info->SD_Status = bbuf[0];
2166 if ((info->SD_Status & SD_Insert) && (info->SD_Status & SD_Ready)) {
2167 unsigned int s = info->SD_Status;
2169 ene_get_card_status(us, bbuf);
2170 usb_stor_dbg(us, "Insert = %x\n", !!(s & SD_Insert));
2171 usb_stor_dbg(us, "Ready = %x\n", !!(s & SD_Ready));
2172 usb_stor_dbg(us, "IsMMC = %x\n", !!(s & SD_IsMMC));
2173 usb_stor_dbg(us, "HiCapacity = %x\n", !!(s & SD_HiCapacity));
2174 usb_stor_dbg(us, "HiSpeed = %x\n", !!(s & SD_HiSpeed));
2175 usb_stor_dbg(us, "WtP = %x\n", !!(s & SD_WtP));
2177 usb_stor_dbg(us, "SD Card Not Ready --- %x\n", bbuf[0]);
2178 return USB_STOR_TRANSPORT_ERROR;
2180 return USB_STOR_TRANSPORT_GOOD;
2184 static int ene_init(struct us_data *us)
2188 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2189 u8 *bbuf = info->bbuf;
2191 result = ene_get_card_type(us, REG_CARD_STATUS, bbuf);
2192 if (result != USB_STOR_XFER_GOOD)
2193 return USB_STOR_TRANSPORT_ERROR;
2195 misc_reg03 = bbuf[0];
2196 if (misc_reg03 & 0x01) {
2197 if (!(info->SD_Status & SD_Ready)) {
2198 result = ene_sd_init(us);
2199 if (result != USB_STOR_XFER_GOOD)
2200 return USB_STOR_TRANSPORT_ERROR;
2203 if (misc_reg03 & 0x02) {
2204 if (!(info->MS_Status & MS_Ready)) {
2205 result = ene_ms_init(us);
2206 if (result != USB_STOR_XFER_GOOD)
2207 return USB_STOR_TRANSPORT_ERROR;
2213 /*----- sd_scsi_irp() ---------*/
2214 static int sd_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
2217 struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2219 switch (srb->cmnd[0]) {
2220 case TEST_UNIT_READY:
2221 result = sd_scsi_test_unit_ready(us, srb);
2224 result = do_scsi_request_sense(us, srb);
2227 result = do_scsi_inquiry(us, srb);
2230 result = sd_scsi_mode_sense(us, srb);
2234 result = SD_SCSI_Start_Stop(us, srb);
2238 result = sd_scsi_read_capacity(us, srb);
2241 result = sd_scsi_read(us, srb);
2244 result = sd_scsi_write(us, srb);
2247 info->SrbStatus = SS_ILLEGAL_REQUEST;
2248 result = USB_STOR_TRANSPORT_FAILED;
2251 if (result == USB_STOR_TRANSPORT_GOOD)
2252 info->SrbStatus = SS_SUCCESS;
2259 static int ms_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
2262 struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2264 switch (srb->cmnd[0]) {
2265 case TEST_UNIT_READY:
2266 result = ms_scsi_test_unit_ready(us, srb);
2269 result = do_scsi_request_sense(us, srb);
2272 result = do_scsi_inquiry(us, srb);
2275 result = ms_scsi_mode_sense(us, srb);
2278 result = ms_scsi_read_capacity(us, srb);
2281 result = ms_scsi_read(us, srb);
2284 result = ms_scsi_write(us, srb);
2287 info->SrbStatus = SS_ILLEGAL_REQUEST;
2288 result = USB_STOR_TRANSPORT_FAILED;
2291 if (result == USB_STOR_TRANSPORT_GOOD)
2292 info->SrbStatus = SS_SUCCESS;
2296 static int ene_transport(struct scsi_cmnd *srb, struct us_data *us)
2298 int result = USB_STOR_XFER_GOOD;
2299 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2301 /*US_DEBUG(usb_stor_show_command(us, srb)); */
2302 scsi_set_resid(srb, 0);
2303 if (unlikely(!(info->SD_Status & SD_Ready) || (info->MS_Status & MS_Ready)))
2304 result = ene_init(us);
2305 if (result == USB_STOR_XFER_GOOD) {
2306 result = USB_STOR_TRANSPORT_ERROR;
2307 if (info->SD_Status & SD_Ready)
2308 result = sd_scsi_irp(us, srb);
2310 if (info->MS_Status & MS_Ready)
2311 result = ms_scsi_irp(us, srb);
2316 static struct scsi_host_template ene_ub6250_host_template;
2318 static int ene_ub6250_probe(struct usb_interface *intf,
2319 const struct usb_device_id *id)
2324 struct ene_ub6250_info *info;
2326 result = usb_stor_probe1(&us, intf, id,
2327 (id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list,
2328 &ene_ub6250_host_template);
2332 /* FIXME: where should the code alloc extra buf ? */
2333 us->extra = kzalloc(sizeof(struct ene_ub6250_info), GFP_KERNEL);
2336 us->extra_destructor = ene_ub6250_info_destructor;
2338 info = (struct ene_ub6250_info *)(us->extra);
2339 info->bbuf = kmalloc(512, GFP_KERNEL);
2345 us->transport_name = "ene_ub6250";
2346 us->transport = ene_transport;
2349 result = usb_stor_probe2(us);
2353 /* probe card type */
2354 result = ene_get_card_type(us, REG_CARD_STATUS, info->bbuf);
2355 if (result != USB_STOR_XFER_GOOD) {
2356 usb_stor_disconnect(intf);
2357 return USB_STOR_TRANSPORT_ERROR;
2360 misc_reg03 = info->bbuf[0];
2361 if (!(misc_reg03 & 0x01)) {
2362 pr_info("ums_eneub6250: This driver only supports SD/MS cards. "
2363 "It does not support SM cards.\n");
2372 static int ene_ub6250_resume(struct usb_interface *iface)
2374 struct us_data *us = usb_get_intfdata(iface);
2375 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2377 mutex_lock(&us->dev_mutex);
2379 if (us->suspend_resume_hook)
2380 (us->suspend_resume_hook)(us, US_RESUME);
2382 mutex_unlock(&us->dev_mutex);
2384 info->Power_IsResum = true;
2385 /* info->SD_Status &= ~SD_Ready; */
2386 info->SD_Status = 0;
2387 info->MS_Status = 0;
2388 info->SM_Status = 0;
2393 static int ene_ub6250_reset_resume(struct usb_interface *iface)
2395 struct us_data *us = usb_get_intfdata(iface);
2396 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2398 /* Report the reset to the SCSI core */
2399 usb_stor_reset_resume(iface);
2402 * FIXME: Notify the subdrivers that they need to reinitialize
2405 info->Power_IsResum = true;
2406 /* info->SD_Status &= ~SD_Ready; */
2407 info->SD_Status = 0;
2408 info->MS_Status = 0;
2409 info->SM_Status = 0;
2416 #define ene_ub6250_resume NULL
2417 #define ene_ub6250_reset_resume NULL
2421 static struct usb_driver ene_ub6250_driver = {
2423 .probe = ene_ub6250_probe,
2424 .disconnect = usb_stor_disconnect,
2425 .suspend = usb_stor_suspend,
2426 .resume = ene_ub6250_resume,
2427 .reset_resume = ene_ub6250_reset_resume,
2428 .pre_reset = usb_stor_pre_reset,
2429 .post_reset = usb_stor_post_reset,
2430 .id_table = ene_ub6250_usb_ids,
2435 module_usb_stor_driver(ene_ub6250_driver, ene_ub6250_host_template, DRV_NAME);