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");
32 * The table of devices
34 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
35 vendorName, productName, useProtocol, useTransport, \
36 initFunction, flags) \
37 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
38 .driver_info = (flags)}
40 static struct usb_device_id ene_ub6250_usb_ids[] = {
41 # include "unusual_ene_ub6250.h"
42 { } /* Terminating entry */
44 MODULE_DEVICE_TABLE(usb, ene_ub6250_usb_ids);
51 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
52 vendor_name, product_name, use_protocol, use_transport, \
53 init_function, Flags) \
55 .vendorName = vendor_name, \
56 .productName = product_name, \
57 .useProtocol = use_protocol, \
58 .useTransport = use_transport, \
59 .initFunction = init_function, \
62 static struct us_unusual_dev ene_ub6250_unusual_dev_list[] = {
63 # include "unusual_ene_ub6250.h"
64 { } /* Terminating entry */
71 /* ENE bin code len */
72 #define ENE_BIN_CODE_LEN 0x800
74 #define REG_CARD_STATUS 0xFF83
75 #define REG_HW_TRAP1 0xFF89
78 #define SS_SUCCESS 0x000000 /* No Sense */
79 #define SS_NOT_READY 0x023A00 /* Medium not present */
80 #define SS_MEDIUM_ERR 0x031100 /* Unrecovered read error */
81 #define SS_HW_ERR 0x040800 /* Communication failure */
82 #define SS_ILLEGAL_REQUEST 0x052000 /* Invalid command */
83 #define SS_UNIT_ATTENTION 0x062900 /* Reset occurred */
85 /* ENE Load FW Pattern */
86 #define SD_INIT1_PATTERN 1
87 #define SD_INIT2_PATTERN 2
88 #define SD_RW_PATTERN 3
89 #define MS_INIT_PATTERN 4
90 #define MSP_RW_PATTERN 5
91 #define MS_RW_PATTERN 6
92 #define SM_INIT_PATTERN 7
93 #define SM_RW_PATTERN 8
100 /* Status Register 1 */
101 #define MS_REG_ST1_MB 0x80 /* media busy */
102 #define MS_REG_ST1_FB1 0x40 /* flush busy 1 */
103 #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */
104 #define MS_REG_ST1_UCDT 0x10 /* unable to correct data */
105 #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */
106 #define MS_REG_ST1_UCEX 0x04 /* unable to correct extra */
107 #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */
108 #define MS_REG_ST1_UCFG 0x01 /* unable to correct overwrite flag */
109 #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)
112 #define MS_REG_OVR_BKST 0x80 /* block status */
113 #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */
114 #define MS_REG_OVR_BKST_NG 0x00 /* NG */
115 #define MS_REG_OVR_PGST0 0x40 /* page status */
116 #define MS_REG_OVR_PGST1 0x20
117 #define MS_REG_OVR_PGST_MASK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1)
118 #define MS_REG_OVR_PGST_OK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1) /* OK */
119 #define MS_REG_OVR_PGST_NG MS_REG_OVR_PGST1 /* NG */
120 #define MS_REG_OVR_PGST_DATA_ERROR 0x00 /* data error */
121 #define MS_REG_OVR_UDST 0x10 /* update status */
122 #define MS_REG_OVR_UDST_UPDATING 0x00 /* updating */
123 #define MS_REG_OVR_UDST_NO_UPDATE MS_REG_OVR_UDST
124 #define MS_REG_OVR_RESERVED 0x08
125 #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)
127 /* Management Flag */
128 #define MS_REG_MNG_SCMS0 0x20 /* serial copy management system */
129 #define MS_REG_MNG_SCMS1 0x10
130 #define MS_REG_MNG_SCMS_MASK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
131 #define MS_REG_MNG_SCMS_COPY_OK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
132 #define MS_REG_MNG_SCMS_ONE_COPY MS_REG_MNG_SCMS1
133 #define MS_REG_MNG_SCMS_NO_COPY 0x00
134 #define MS_REG_MNG_ATFLG 0x08 /* address transfer table flag */
135 #define MS_REG_MNG_ATFLG_OTHER MS_REG_MNG_ATFLG /* other */
136 #define MS_REG_MNG_ATFLG_ATTBL 0x00 /* address transfer table */
137 #define MS_REG_MNG_SYSFLG 0x04 /* system flag */
138 #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */
139 #define MS_REG_MNG_SYSFLG_BOOT 0x00 /* system block */
140 #define MS_REG_MNG_RESERVED 0xc3
141 #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)
144 #define MS_MAX_PAGES_PER_BLOCK 32
145 #define MS_MAX_INITIAL_ERROR_BLOCKS 10
146 #define MS_LIB_BITS_PER_BYTE 8
148 #define MS_SYSINF_FORMAT_FAT 1
149 #define MS_SYSINF_USAGE_GENERAL 0
151 #define MS_SYSINF_MSCLASS_TYPE_1 1
152 #define MS_SYSINF_PAGE_SIZE MS_BYTES_PER_PAGE /* fixed */
154 #define MS_SYSINF_CARDTYPE_RDONLY 1
155 #define MS_SYSINF_CARDTYPE_RDWR 2
156 #define MS_SYSINF_CARDTYPE_HYBRID 3
157 #define MS_SYSINF_SECURITY 0x01
158 #define MS_SYSINF_SECURITY_NO_SUPPORT MS_SYSINF_SECURITY
159 #define MS_SYSINF_SECURITY_SUPPORT 0
161 #define MS_SYSINF_RESERVED1 1
162 #define MS_SYSINF_RESERVED2 1
164 #define MS_SYSENT_TYPE_INVALID_BLOCK 0x01
165 #define MS_SYSENT_TYPE_CIS_IDI 0x0a /* CIS/IDI */
167 #define SIZE_OF_KIRO 1024
168 #define BYTE_MASK 0xff
171 #define MS_STATUS_WRITE_PROTECT 0x0106
172 #define MS_STATUS_SUCCESS 0x0000
173 #define MS_ERROR_FLASH_READ 0x8003
174 #define MS_ERROR_FLASH_ERASE 0x8005
175 #define MS_LB_ERROR 0xfff0
176 #define MS_LB_BOOT_BLOCK 0xfff1
177 #define MS_LB_INITIAL_ERROR 0xfff2
178 #define MS_STATUS_SUCCESS_WITH_ECC 0xfff3
179 #define MS_LB_ACQUIRED_ERROR 0xfff4
180 #define MS_LB_NOT_USED_ERASED 0xfff5
181 #define MS_NOCARD_ERROR 0xfff8
182 #define MS_NO_MEMORY_ERROR 0xfff9
183 #define MS_STATUS_INT_ERROR 0xfffa
184 #define MS_STATUS_ERROR 0xfffe
185 #define MS_LB_NOT_USED 0xffff
187 #define MS_REG_MNG_SYSFLG 0x04 /* system flag */
188 #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */
190 #define MS_BOOT_BLOCK_ID 0x0001
191 #define MS_BOOT_BLOCK_FORMAT_VERSION 0x0100
192 #define MS_BOOT_BLOCK_DATA_ENTRIES 2
194 #define MS_NUMBER_OF_SYSTEM_ENTRY 4
195 #define MS_NUMBER_OF_BOOT_BLOCK 2
196 #define MS_BYTES_PER_PAGE 512
197 #define MS_LOGICAL_BLOCKS_PER_SEGMENT 496
198 #define MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT 494
200 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT 0x200 /* 512 */
201 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK 0x1ff
204 #define MS_REG_OVR_BKST 0x80 /* block status */
205 #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */
206 #define MS_REG_OVR_BKST_NG 0x00 /* NG */
208 /* Status Register 1 */
209 #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */
210 #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */
211 #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */
213 /* MemoryStick Register */
214 /* Status Register 0 */
215 #define MS_REG_ST0_WP 0x01 /* write protected */
216 #define MS_REG_ST0_WP_ON MS_REG_ST0_WP
218 #define MS_LIB_CTRL_RDONLY 0
219 #define MS_LIB_CTRL_WRPROTECT 1
222 #define ms_libconv_to_logical(pdx, PhyBlock) (((PhyBlock) >= (pdx)->MS_Lib.NumberOfPhyBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Phy2LogMap[PhyBlock])
223 #define ms_libconv_to_physical(pdx, LogBlock) (((LogBlock) >= (pdx)->MS_Lib.NumberOfLogBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Log2PhyMap[LogBlock])
225 #define ms_lib_ctrl_set(pdx, Flag) ((pdx)->MS_Lib.flags |= (1 << (Flag)))
226 #define ms_lib_ctrl_reset(pdx, Flag) ((pdx)->MS_Lib.flags &= ~(1 << (Flag)))
227 #define ms_lib_ctrl_check(pdx, Flag) ((pdx)->MS_Lib.flags & (1 << (Flag)))
229 #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))
230 #define ms_lib_clear_pagemap(pdx) memset((pdx)->MS_Lib.pagemap, 0, sizeof((pdx)->MS_Lib.pagemap))
231 #define memstick_logaddr(logadr1, logadr0) ((((u16)(logadr1)) << 8) | (logadr0))
265 struct ms_bootblock_cis {
266 u8 bCistplDEVICE[6]; /* 0 */
267 u8 bCistplDEVICE0C[6]; /* 6 */
268 u8 bCistplJEDECC[4]; /* 12 */
269 u8 bCistplMANFID[6]; /* 16 */
270 u8 bCistplVER1[32]; /* 22 */
271 u8 bCistplFUNCID[4]; /* 54 */
272 u8 bCistplFUNCE0[4]; /* 58 */
273 u8 bCistplFUNCE1[5]; /* 62 */
274 u8 bCistplCONF[7]; /* 67 */
275 u8 bCistplCFTBLENT0[10];/* 74 */
276 u8 bCistplCFTBLENT1[8]; /* 84 */
277 u8 bCistplCFTBLENT2[12];/* 92 */
278 u8 bCistplCFTBLENT3[8]; /* 104 */
279 u8 bCistplCFTBLENT4[17];/* 112 */
280 u8 bCistplCFTBLENT5[8]; /* 129 */
281 u8 bCistplCFTBLENT6[17];/* 137 */
282 u8 bCistplCFTBLENT7[8]; /* 154 */
283 u8 bCistplNOLINK[3]; /* 162 */
286 struct ms_bootblock_idi {
287 #define MS_IDI_GENERAL_CONF 0x848A
288 u16 wIDIgeneralConfiguration; /* 0 */
289 u16 wIDInumberOfCylinder; /* 1 */
290 u16 wIDIreserved0; /* 2 */
291 u16 wIDInumberOfHead; /* 3 */
292 u16 wIDIbytesPerTrack; /* 4 */
293 u16 wIDIbytesPerSector; /* 5 */
294 u16 wIDIsectorsPerTrack; /* 6 */
295 u16 wIDItotalSectors[2]; /* 7-8 high,low */
296 u16 wIDIreserved1[11]; /* 9-19 */
297 u16 wIDIbufferType; /* 20 */
298 u16 wIDIbufferSize; /* 21 */
299 u16 wIDIlongCmdECC; /* 22 */
300 u16 wIDIfirmVersion[4]; /* 23-26 */
301 u16 wIDImodelName[20]; /* 27-46 */
302 u16 wIDIreserved2; /* 47 */
303 u16 wIDIlongWordSupported; /* 48 */
304 u16 wIDIdmaSupported; /* 49 */
305 u16 wIDIreserved3; /* 50 */
306 u16 wIDIpioTiming; /* 51 */
307 u16 wIDIdmaTiming; /* 52 */
308 u16 wIDItransferParameter; /* 53 */
309 u16 wIDIformattedCylinder; /* 54 */
310 u16 wIDIformattedHead; /* 55 */
311 u16 wIDIformattedSectorsPerTrack;/* 56 */
312 u16 wIDIformattedTotalSectors[2];/* 57-58 */
313 u16 wIDImultiSector; /* 59 */
314 u16 wIDIlbaSectors[2]; /* 60-61 */
315 u16 wIDIsingleWordDMA; /* 62 */
316 u16 wIDImultiWordDMA; /* 63 */
317 u16 wIDIreserved4[192]; /* 64-255 */
320 struct ms_bootblock_sysent_rec {
327 struct ms_bootblock_sysent {
328 struct ms_bootblock_sysent_rec entry[MS_NUMBER_OF_SYSTEM_ENTRY];
331 struct ms_bootblock_sysinf {
332 u8 bMsClass; /* must be 1 */
333 u8 bCardType; /* see below */
334 u16 wBlockSize; /* n KB */
335 u16 wBlockNumber; /* number of physical block */
336 u16 wTotalBlockNumber; /* number of logical block */
337 u16 wPageSize; /* must be 0x200 */
338 u8 bExtraSize; /* 0x10 */
342 u8 bAssemblyMakerCode;
343 u8 bAssemblyMachineCode[3];
344 u16 wMemoryMakerCode;
345 u16 wMemoryDeviceCode;
351 u16 wControllerChipNumber;
352 u16 wControllerFunction; /* New MS */
353 u8 bReserved3[9]; /* New MS */
354 u8 bParallelSupport; /* New MS */
355 u16 wFormatValue; /* New MS */
365 struct ms_bootblock_header {
369 u8 bNumberOfDataEntry;
373 struct ms_bootblock_page0 {
374 struct ms_bootblock_header header;
375 struct ms_bootblock_sysent sysent;
376 struct ms_bootblock_sysinf sysinf;
379 struct ms_bootblock_cis_idi {
381 struct ms_bootblock_cis cis;
386 struct ms_bootblock_idi idi;
392 /* ENE MS Lib struct */
393 struct ms_lib_type_extdat {
406 u32 NumberOfCylinder;
407 u32 SectorsPerCylinder;
408 u16 cardType; /* R/W, RO, Hybrid */
411 u16 NumberOfPhyBlock;
412 u16 NumberOfLogBlock;
414 u16 *Phy2LogMap; /* phy2log table */
415 u16 *Log2PhyMap; /* log2phy table */
417 unsigned char *pagemap[(MS_MAX_PAGES_PER_BLOCK + (MS_LIB_BITS_PER_BYTE-1)) / MS_LIB_BITS_PER_BYTE];
418 unsigned char *blkpag;
419 struct ms_lib_type_extdat *blkext;
420 unsigned char copybuf[512];
424 /* SD Block Length */
425 /* 2^9 = 512 Bytes, The HW maximum read/write data length */
426 #define SD_BLOCK_LEN 9
428 struct ene_ub6250_info {
430 /* I/O bounce buffer */
434 struct SD_STATUS SD_Status;
435 struct MS_STATUS MS_Status;
436 struct SM_STATUS SM_Status;
438 /* ----- SD Control Data ---------------- */
439 /*SD_REGISTER SD_Regs; */
445 /* SD/MMC New spec. */
448 u8 SD20_HIGH_CAPACITY;
452 u8 MMC_HIGH_CAPACITY;
454 /*----- MS Control Data ---------------- */
457 struct ms_lib_ctrl MS_Lib;
461 /*----- SM Control Data ---------------- */
465 unsigned char *testbuf;
470 /*------Power Managerment ---------------*/
474 static int ene_sd_init(struct us_data *us);
475 static int ene_ms_init(struct us_data *us);
476 static int ene_load_bincode(struct us_data *us, unsigned char flag);
478 static void ene_ub6250_info_destructor(void *extra)
480 struct ene_ub6250_info *info = (struct ene_ub6250_info *) extra;
487 static int ene_send_scsi_cmd(struct us_data *us, u8 fDir, void *buf, int use_sg)
489 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
490 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
493 unsigned int residue;
494 unsigned int cswlen = 0, partial = 0;
495 unsigned int transfer_length = bcb->DataTransferLength;
497 /* usb_stor_dbg(us, "transport --- ene_send_scsi_cmd\n"); */
498 /* send cmd to out endpoint */
499 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
500 bcb, US_BULK_CB_WRAP_LEN, NULL);
501 if (result != USB_STOR_XFER_GOOD) {
502 usb_stor_dbg(us, "send cmd to out endpoint fail ---\n");
503 return USB_STOR_TRANSPORT_ERROR;
507 unsigned int pipe = fDir;
509 if (fDir == FDIR_READ)
510 pipe = us->recv_bulk_pipe;
512 pipe = us->send_bulk_pipe;
516 result = usb_stor_bulk_srb(us, pipe, us->srb);
518 result = usb_stor_bulk_transfer_sg(us, pipe, buf,
519 transfer_length, 0, &partial);
521 if (result != USB_STOR_XFER_GOOD) {
522 usb_stor_dbg(us, "data transfer fail ---\n");
523 return USB_STOR_TRANSPORT_ERROR;
527 /* Get CSW for device status */
528 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
529 US_BULK_CS_WRAP_LEN, &cswlen);
531 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
532 usb_stor_dbg(us, "Received 0-length CSW; retrying...\n");
533 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
534 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
537 if (result == USB_STOR_XFER_STALLED) {
538 /* get the status again */
539 usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n");
540 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
541 bcs, US_BULK_CS_WRAP_LEN, NULL);
544 if (result != USB_STOR_XFER_GOOD)
545 return USB_STOR_TRANSPORT_ERROR;
547 /* check bulk status */
548 residue = le32_to_cpu(bcs->Residue);
551 * try to compute the actual residue, based on how much data
552 * was really transferred and what the device tells us
554 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
555 residue = min(residue, transfer_length);
557 scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
561 if (bcs->Status != US_BULK_STAT_OK)
562 return USB_STOR_TRANSPORT_ERROR;
564 return USB_STOR_TRANSPORT_GOOD;
567 static int do_scsi_request_sense(struct us_data *us, struct scsi_cmnd *srb)
569 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
570 unsigned char buf[18];
573 buf[0] = 0x70; /* Current error */
574 buf[2] = info->SrbStatus >> 16; /* Sense key */
575 buf[7] = 10; /* Additional length */
576 buf[12] = info->SrbStatus >> 8; /* ASC */
577 buf[13] = info->SrbStatus; /* ASCQ */
579 usb_stor_set_xfer_buf(buf, sizeof(buf), srb);
580 return USB_STOR_TRANSPORT_GOOD;
583 static int do_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
585 unsigned char data_ptr[36] = {
586 0x00, 0x00, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
587 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
588 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
589 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30 };
591 usb_stor_set_xfer_buf(data_ptr, 36, srb);
592 return USB_STOR_TRANSPORT_GOOD;
595 static int sd_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
597 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
599 if (info->SD_Status.Insert && info->SD_Status.Ready)
600 return USB_STOR_TRANSPORT_GOOD;
603 return USB_STOR_TRANSPORT_GOOD;
606 return USB_STOR_TRANSPORT_GOOD;
609 static int sd_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
611 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
612 unsigned char mediaNoWP[12] = {
613 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
614 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
615 unsigned char mediaWP[12] = {
616 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
617 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
619 if (info->SD_Status.WtP)
620 usb_stor_set_xfer_buf(mediaWP, 12, srb);
622 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
625 return USB_STOR_TRANSPORT_GOOD;
628 static int sd_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
632 unsigned int offset = 0;
633 unsigned char buf[8];
634 struct scatterlist *sg = NULL;
635 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
637 usb_stor_dbg(us, "sd_scsi_read_capacity\n");
638 if (info->SD_Status.HiCapacity) {
640 if (info->SD_Status.IsMMC)
641 bl_num = info->HC_C_SIZE-1;
643 bl_num = (info->HC_C_SIZE + 1) * 1024 - 1;
645 bl_len = 1 << (info->SD_READ_BL_LEN);
646 bl_num = info->SD_Block_Mult * (info->SD_C_SIZE + 1)
647 * (1 << (info->SD_C_SIZE_MULT + 2)) - 1;
649 info->bl_num = bl_num;
650 usb_stor_dbg(us, "bl_len = %x\n", bl_len);
651 usb_stor_dbg(us, "bl_num = %x\n", bl_num);
653 /*srb->request_bufflen = 8; */
654 buf[0] = (bl_num >> 24) & 0xff;
655 buf[1] = (bl_num >> 16) & 0xff;
656 buf[2] = (bl_num >> 8) & 0xff;
657 buf[3] = (bl_num >> 0) & 0xff;
658 buf[4] = (bl_len >> 24) & 0xff;
659 buf[5] = (bl_len >> 16) & 0xff;
660 buf[6] = (bl_len >> 8) & 0xff;
661 buf[7] = (bl_len >> 0) & 0xff;
663 usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
665 return USB_STOR_TRANSPORT_GOOD;
668 static int sd_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
671 unsigned char *cdb = srb->cmnd;
672 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
673 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
675 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
676 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
677 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
678 u32 bnByte = bn * 0x200;
679 u32 blenByte = blen * 0x200;
681 if (bn > info->bl_num)
682 return USB_STOR_TRANSPORT_ERROR;
684 result = ene_load_bincode(us, SD_RW_PATTERN);
685 if (result != USB_STOR_XFER_GOOD) {
686 usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
687 return USB_STOR_TRANSPORT_ERROR;
690 if (info->SD_Status.HiCapacity)
693 /* set up the command wrapper */
694 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
695 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
696 bcb->DataTransferLength = blenByte;
697 bcb->Flags = US_BULK_FLAG_IN;
699 bcb->CDB[5] = (unsigned char)(bnByte);
700 bcb->CDB[4] = (unsigned char)(bnByte>>8);
701 bcb->CDB[3] = (unsigned char)(bnByte>>16);
702 bcb->CDB[2] = (unsigned char)(bnByte>>24);
704 result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
708 static int sd_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
711 unsigned char *cdb = srb->cmnd;
712 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
713 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
715 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
716 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
717 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
718 u32 bnByte = bn * 0x200;
719 u32 blenByte = blen * 0x200;
721 if (bn > info->bl_num)
722 return USB_STOR_TRANSPORT_ERROR;
724 result = ene_load_bincode(us, SD_RW_PATTERN);
725 if (result != USB_STOR_XFER_GOOD) {
726 usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
727 return USB_STOR_TRANSPORT_ERROR;
730 if (info->SD_Status.HiCapacity)
733 /* set up the command wrapper */
734 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
735 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
736 bcb->DataTransferLength = blenByte;
739 bcb->CDB[5] = (unsigned char)(bnByte);
740 bcb->CDB[4] = (unsigned char)(bnByte>>8);
741 bcb->CDB[3] = (unsigned char)(bnByte>>16);
742 bcb->CDB[2] = (unsigned char)(bnByte>>24);
744 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
752 static int ms_lib_set_logicalpair(struct us_data *us, u16 logblk, u16 phyblk)
754 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
756 if ((logblk >= info->MS_Lib.NumberOfLogBlock) || (phyblk >= info->MS_Lib.NumberOfPhyBlock))
759 info->MS_Lib.Phy2LogMap[phyblk] = logblk;
760 info->MS_Lib.Log2PhyMap[logblk] = phyblk;
765 static int ms_lib_set_logicalblockmark(struct us_data *us, u16 phyblk, u16 mark)
767 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
769 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
772 info->MS_Lib.Phy2LogMap[phyblk] = mark;
777 static int ms_lib_set_initialerrorblock(struct us_data *us, u16 phyblk)
779 return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_INITIAL_ERROR);
782 static int ms_lib_set_bootblockmark(struct us_data *us, u16 phyblk)
784 return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_BOOT_BLOCK);
787 static int ms_lib_free_logicalmap(struct us_data *us)
789 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
791 kfree(info->MS_Lib.Phy2LogMap);
792 info->MS_Lib.Phy2LogMap = NULL;
794 kfree(info->MS_Lib.Log2PhyMap);
795 info->MS_Lib.Log2PhyMap = NULL;
800 static int ms_lib_alloc_logicalmap(struct us_data *us)
803 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
805 info->MS_Lib.Phy2LogMap = kmalloc_array(info->MS_Lib.NumberOfPhyBlock,
808 info->MS_Lib.Log2PhyMap = kmalloc_array(info->MS_Lib.NumberOfLogBlock,
812 if ((info->MS_Lib.Phy2LogMap == NULL) || (info->MS_Lib.Log2PhyMap == NULL)) {
813 ms_lib_free_logicalmap(us);
817 for (i = 0; i < info->MS_Lib.NumberOfPhyBlock; i++)
818 info->MS_Lib.Phy2LogMap[i] = MS_LB_NOT_USED;
820 for (i = 0; i < info->MS_Lib.NumberOfLogBlock; i++)
821 info->MS_Lib.Log2PhyMap[i] = MS_LB_NOT_USED;
826 static void ms_lib_clear_writebuf(struct us_data *us)
829 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
831 info->MS_Lib.wrtblk = (u16)-1;
832 ms_lib_clear_pagemap(info);
834 if (info->MS_Lib.blkpag)
835 memset(info->MS_Lib.blkpag, 0xff, info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector);
837 if (info->MS_Lib.blkext) {
838 for (i = 0; i < info->MS_Lib.PagesPerBlock; i++) {
839 info->MS_Lib.blkext[i].status1 = MS_REG_ST1_DEFAULT;
840 info->MS_Lib.blkext[i].ovrflg = MS_REG_OVR_DEFAULT;
841 info->MS_Lib.blkext[i].mngflg = MS_REG_MNG_DEFAULT;
842 info->MS_Lib.blkext[i].logadr = MS_LB_NOT_USED;
847 static int ms_count_freeblock(struct us_data *us, u16 PhyBlock)
850 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
852 Ende = PhyBlock + MS_PHYSICAL_BLOCKS_PER_SEGMENT;
853 for (Count = 0; PhyBlock < Ende; PhyBlock++) {
854 switch (info->MS_Lib.Phy2LogMap[PhyBlock]) {
856 case MS_LB_NOT_USED_ERASED:
866 static int ms_read_readpage(struct us_data *us, u32 PhyBlockAddr,
867 u8 PageNum, u32 *PageBuf, struct ms_lib_type_extdat *ExtraDat)
869 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
870 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
871 u8 *bbuf = info->bbuf;
873 u32 bn = PhyBlockAddr * 0x20 + PageNum;
875 result = ene_load_bincode(us, MS_RW_PATTERN);
876 if (result != USB_STOR_XFER_GOOD)
877 return USB_STOR_TRANSPORT_ERROR;
880 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
881 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
882 bcb->DataTransferLength = 0x200;
883 bcb->Flags = US_BULK_FLAG_IN;
886 bcb->CDB[1] = 0x02; /* in init.c ENE_MSInit() is 0x01 */
888 bcb->CDB[5] = (unsigned char)(bn);
889 bcb->CDB[4] = (unsigned char)(bn>>8);
890 bcb->CDB[3] = (unsigned char)(bn>>16);
891 bcb->CDB[2] = (unsigned char)(bn>>24);
893 result = ene_send_scsi_cmd(us, FDIR_READ, PageBuf, 0);
894 if (result != USB_STOR_XFER_GOOD)
895 return USB_STOR_TRANSPORT_ERROR;
898 /* Read Extra Data */
899 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
900 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
901 bcb->DataTransferLength = 0x4;
902 bcb->Flags = US_BULK_FLAG_IN;
906 bcb->CDB[5] = (unsigned char)(PageNum);
907 bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
908 bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
909 bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
912 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
913 if (result != USB_STOR_XFER_GOOD)
914 return USB_STOR_TRANSPORT_ERROR;
916 ExtraDat->reserved = 0;
917 ExtraDat->intr = 0x80; /* Not yet,fireware support */
918 ExtraDat->status0 = 0x10; /* Not yet,fireware support */
920 ExtraDat->status1 = 0x00; /* Not yet,fireware support */
921 ExtraDat->ovrflg = bbuf[0];
922 ExtraDat->mngflg = bbuf[1];
923 ExtraDat->logadr = memstick_logaddr(bbuf[2], bbuf[3]);
925 return USB_STOR_TRANSPORT_GOOD;
928 static int ms_lib_process_bootblock(struct us_data *us, u16 PhyBlock, u8 *PageData)
930 struct ms_bootblock_sysent *SysEntry;
931 struct ms_bootblock_sysinf *SysInfo;
935 struct ms_lib_type_extdat ExtraData;
936 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
938 PageBuffer = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
939 if (PageBuffer == NULL)
944 SysInfo = &(((struct ms_bootblock_page0 *)PageData)->sysinf);
946 if ((SysInfo->bMsClass != MS_SYSINF_MSCLASS_TYPE_1) ||
947 (be16_to_cpu(SysInfo->wPageSize) != MS_SYSINF_PAGE_SIZE) ||
948 ((SysInfo->bSecuritySupport & MS_SYSINF_SECURITY) == MS_SYSINF_SECURITY_SUPPORT) ||
949 (SysInfo->bReserved1 != MS_SYSINF_RESERVED1) ||
950 (SysInfo->bReserved2 != MS_SYSINF_RESERVED2) ||
951 (SysInfo->bFormatType != MS_SYSINF_FORMAT_FAT) ||
952 (SysInfo->bUsage != MS_SYSINF_USAGE_GENERAL))
955 switch (info->MS_Lib.cardType = SysInfo->bCardType) {
956 case MS_SYSINF_CARDTYPE_RDONLY:
957 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY);
959 case MS_SYSINF_CARDTYPE_RDWR:
960 ms_lib_ctrl_reset(info, MS_LIB_CTRL_RDONLY);
962 case MS_SYSINF_CARDTYPE_HYBRID:
967 info->MS_Lib.blockSize = be16_to_cpu(SysInfo->wBlockSize);
968 info->MS_Lib.NumberOfPhyBlock = be16_to_cpu(SysInfo->wBlockNumber);
969 info->MS_Lib.NumberOfLogBlock = be16_to_cpu(SysInfo->wTotalBlockNumber)-2;
970 info->MS_Lib.PagesPerBlock = info->MS_Lib.blockSize * SIZE_OF_KIRO / MS_BYTES_PER_PAGE;
971 info->MS_Lib.NumberOfSegment = info->MS_Lib.NumberOfPhyBlock / MS_PHYSICAL_BLOCKS_PER_SEGMENT;
972 info->MS_Model = be16_to_cpu(SysInfo->wMemorySize);
974 /*Allocate to all number of logicalblock and physicalblock */
975 if (ms_lib_alloc_logicalmap(us))
978 /* Mark the book block */
979 ms_lib_set_bootblockmark(us, PhyBlock);
981 SysEntry = &(((struct ms_bootblock_page0 *)PageData)->sysent);
983 for (i = 0; i < MS_NUMBER_OF_SYSTEM_ENTRY; i++) {
984 u32 EntryOffset, EntrySize;
986 EntryOffset = be32_to_cpu(SysEntry->entry[i].dwStart);
988 if (EntryOffset == 0xffffff)
990 EntrySize = be32_to_cpu(SysEntry->entry[i].dwSize);
995 if (EntryOffset + MS_BYTES_PER_PAGE + EntrySize > info->MS_Lib.blockSize * (u32)SIZE_OF_KIRO)
999 u8 PrevPageNumber = 0;
1002 if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_INVALID_BLOCK)
1005 while (EntrySize > 0) {
1007 PageNumber = (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1);
1008 if (PageNumber != PrevPageNumber) {
1009 switch (ms_read_readpage(us, PhyBlock, PageNumber, (u32 *)PageBuffer, &ExtraData)) {
1010 case MS_STATUS_SUCCESS:
1012 case MS_STATUS_WRITE_PROTECT:
1013 case MS_ERROR_FLASH_READ:
1014 case MS_STATUS_ERROR:
1019 PrevPageNumber = PageNumber;
1022 phyblk = be16_to_cpu(*(u16 *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)));
1023 if (phyblk < 0x0fff)
1024 ms_lib_set_initialerrorblock(us, phyblk);
1029 } else if (i == 1) { /* CIS/IDI */
1030 struct ms_bootblock_idi *idi;
1032 if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_CIS_IDI)
1035 switch (ms_read_readpage(us, PhyBlock, (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1), (u32 *)PageBuffer, &ExtraData)) {
1036 case MS_STATUS_SUCCESS:
1038 case MS_STATUS_WRITE_PROTECT:
1039 case MS_ERROR_FLASH_READ:
1040 case MS_STATUS_ERROR:
1045 idi = &((struct ms_bootblock_cis_idi *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)))->idi.idi;
1046 if (le16_to_cpu(idi->wIDIgeneralConfiguration) != MS_IDI_GENERAL_CONF)
1049 info->MS_Lib.BytesPerSector = le16_to_cpu(idi->wIDIbytesPerSector);
1050 if (info->MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE)
1059 ms_lib_free_logicalmap(us);
1067 static void ms_lib_free_writebuf(struct us_data *us)
1069 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1070 info->MS_Lib.wrtblk = (u16)-1; /* set to -1 */
1072 /* memset((fdoExt)->MS_Lib.pagemap, 0, sizeof((fdoExt)->MS_Lib.pagemap)) */
1074 ms_lib_clear_pagemap(info); /* (pdx)->MS_Lib.pagemap memset 0 in ms.h */
1076 if (info->MS_Lib.blkpag) {
1077 kfree(info->MS_Lib.blkpag); /* Arnold test ... */
1078 info->MS_Lib.blkpag = NULL;
1081 if (info->MS_Lib.blkext) {
1082 kfree(info->MS_Lib.blkext); /* Arnold test ... */
1083 info->MS_Lib.blkext = NULL;
1088 static void ms_lib_free_allocatedarea(struct us_data *us)
1090 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1092 ms_lib_free_writebuf(us); /* Free MS_Lib.pagemap */
1093 ms_lib_free_logicalmap(us); /* kfree MS_Lib.Phy2LogMap and MS_Lib.Log2PhyMap */
1095 /* set struct us point flag to 0 */
1096 info->MS_Lib.flags = 0;
1097 info->MS_Lib.BytesPerSector = 0;
1098 info->MS_Lib.SectorsPerCylinder = 0;
1100 info->MS_Lib.cardType = 0;
1101 info->MS_Lib.blockSize = 0;
1102 info->MS_Lib.PagesPerBlock = 0;
1104 info->MS_Lib.NumberOfPhyBlock = 0;
1105 info->MS_Lib.NumberOfLogBlock = 0;
1109 static int ms_lib_alloc_writebuf(struct us_data *us)
1111 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1113 info->MS_Lib.wrtblk = (u16)-1;
1115 info->MS_Lib.blkpag = kmalloc_array(info->MS_Lib.PagesPerBlock,
1116 info->MS_Lib.BytesPerSector,
1118 info->MS_Lib.blkext = kmalloc_array(info->MS_Lib.PagesPerBlock,
1119 sizeof(struct ms_lib_type_extdat),
1122 if ((info->MS_Lib.blkpag == NULL) || (info->MS_Lib.blkext == NULL)) {
1123 ms_lib_free_writebuf(us);
1127 ms_lib_clear_writebuf(us);
1132 static int ms_lib_force_setlogical_pair(struct us_data *us, u16 logblk, u16 phyblk)
1134 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1136 if (logblk == MS_LB_NOT_USED)
1139 if ((logblk >= info->MS_Lib.NumberOfLogBlock) ||
1140 (phyblk >= info->MS_Lib.NumberOfPhyBlock))
1143 info->MS_Lib.Phy2LogMap[phyblk] = logblk;
1144 info->MS_Lib.Log2PhyMap[logblk] = phyblk;
1149 static int ms_read_copyblock(struct us_data *us, u16 oldphy, u16 newphy,
1150 u16 PhyBlockAddr, u8 PageNum, unsigned char *buf, u16 len)
1152 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1155 result = ene_load_bincode(us, MS_RW_PATTERN);
1156 if (result != USB_STOR_XFER_GOOD)
1157 return USB_STOR_TRANSPORT_ERROR;
1159 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1160 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1161 bcb->DataTransferLength = 0x200*len;
1165 bcb->CDB[4] = (unsigned char)(oldphy);
1166 bcb->CDB[3] = (unsigned char)(oldphy>>8);
1167 bcb->CDB[2] = 0; /* (BYTE)(oldphy>>16) */
1168 bcb->CDB[7] = (unsigned char)(newphy);
1169 bcb->CDB[6] = (unsigned char)(newphy>>8);
1170 bcb->CDB[5] = 0; /* (BYTE)(newphy>>16) */
1171 bcb->CDB[9] = (unsigned char)(PhyBlockAddr);
1172 bcb->CDB[8] = (unsigned char)(PhyBlockAddr>>8);
1173 bcb->CDB[10] = PageNum;
1175 result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1176 if (result != USB_STOR_XFER_GOOD)
1177 return USB_STOR_TRANSPORT_ERROR;
1179 return USB_STOR_TRANSPORT_GOOD;
1182 static int ms_read_eraseblock(struct us_data *us, u32 PhyBlockAddr)
1184 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1186 u32 bn = PhyBlockAddr;
1188 result = ene_load_bincode(us, MS_RW_PATTERN);
1189 if (result != USB_STOR_XFER_GOOD)
1190 return USB_STOR_TRANSPORT_ERROR;
1192 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1193 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1194 bcb->DataTransferLength = 0x200;
1195 bcb->Flags = US_BULK_FLAG_IN;
1198 bcb->CDB[4] = (unsigned char)(bn);
1199 bcb->CDB[3] = (unsigned char)(bn>>8);
1200 bcb->CDB[2] = (unsigned char)(bn>>16);
1202 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1203 if (result != USB_STOR_XFER_GOOD)
1204 return USB_STOR_TRANSPORT_ERROR;
1206 return USB_STOR_TRANSPORT_GOOD;
1209 static int ms_lib_check_disableblock(struct us_data *us, u16 PhyBlock)
1211 unsigned char *PageBuf = NULL;
1212 u16 result = MS_STATUS_SUCCESS;
1214 struct ms_lib_type_extdat extdat;
1215 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1217 PageBuf = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1218 if (PageBuf == NULL) {
1219 result = MS_NO_MEMORY_ERROR;
1223 ms_read_readpage(us, PhyBlock, 1, (u32 *)PageBuf, &extdat);
1225 blk = be16_to_cpu(PageBuf[index]);
1226 if (blk == MS_LB_NOT_USED)
1228 if (blk == info->MS_Lib.Log2PhyMap[0]) {
1229 result = MS_ERROR_FLASH_READ;
1240 static int ms_lib_setacquired_errorblock(struct us_data *us, u16 phyblk)
1243 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1245 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1248 log = info->MS_Lib.Phy2LogMap[phyblk];
1250 if (log < info->MS_Lib.NumberOfLogBlock)
1251 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1253 if (info->MS_Lib.Phy2LogMap[phyblk] != MS_LB_INITIAL_ERROR)
1254 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_ACQUIRED_ERROR;
1259 static int ms_lib_overwrite_extra(struct us_data *us, u32 PhyBlockAddr,
1260 u8 PageNum, u8 OverwriteFlag)
1262 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1265 result = ene_load_bincode(us, MS_RW_PATTERN);
1266 if (result != USB_STOR_XFER_GOOD)
1267 return USB_STOR_TRANSPORT_ERROR;
1269 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1270 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1271 bcb->DataTransferLength = 0x4;
1272 bcb->Flags = US_BULK_FLAG_IN;
1275 bcb->CDB[5] = (unsigned char)(PageNum);
1276 bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
1277 bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
1278 bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
1279 bcb->CDB[6] = OverwriteFlag;
1284 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1285 if (result != USB_STOR_XFER_GOOD)
1286 return USB_STOR_TRANSPORT_ERROR;
1288 return USB_STOR_TRANSPORT_GOOD;
1291 static int ms_lib_error_phyblock(struct us_data *us, u16 phyblk)
1293 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1295 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1296 return MS_STATUS_ERROR;
1298 ms_lib_setacquired_errorblock(us, phyblk);
1300 if (ms_lib_iswritable(info))
1301 return ms_lib_overwrite_extra(us, phyblk, 0, (u8)(~MS_REG_OVR_BKST & BYTE_MASK));
1303 return MS_STATUS_SUCCESS;
1306 static int ms_lib_erase_phyblock(struct us_data *us, u16 phyblk)
1309 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1311 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1312 return MS_STATUS_ERROR;
1314 log = info->MS_Lib.Phy2LogMap[phyblk];
1316 if (log < info->MS_Lib.NumberOfLogBlock)
1317 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1319 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED;
1321 if (ms_lib_iswritable(info)) {
1322 switch (ms_read_eraseblock(us, phyblk)) {
1323 case MS_STATUS_SUCCESS:
1324 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED_ERASED;
1325 return MS_STATUS_SUCCESS;
1326 case MS_ERROR_FLASH_ERASE:
1327 case MS_STATUS_INT_ERROR:
1328 ms_lib_error_phyblock(us, phyblk);
1329 return MS_ERROR_FLASH_ERASE;
1330 case MS_STATUS_ERROR:
1332 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY); /* MS_LibCtrlSet will used by ENE_MSInit ,need check, and why us to info*/
1333 ms_lib_setacquired_errorblock(us, phyblk);
1334 return MS_STATUS_ERROR;
1338 ms_lib_setacquired_errorblock(us, phyblk);
1340 return MS_STATUS_SUCCESS;
1343 static int ms_lib_read_extra(struct us_data *us, u32 PhyBlock,
1344 u8 PageNum, struct ms_lib_type_extdat *ExtraDat)
1346 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1347 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1348 u8 *bbuf = info->bbuf;
1351 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1352 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1353 bcb->DataTransferLength = 0x4;
1354 bcb->Flags = US_BULK_FLAG_IN;
1357 bcb->CDB[5] = (unsigned char)(PageNum);
1358 bcb->CDB[4] = (unsigned char)(PhyBlock);
1359 bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
1360 bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
1363 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
1364 if (result != USB_STOR_XFER_GOOD)
1365 return USB_STOR_TRANSPORT_ERROR;
1367 ExtraDat->reserved = 0;
1368 ExtraDat->intr = 0x80; /* Not yet, waiting for fireware support */
1369 ExtraDat->status0 = 0x10; /* Not yet, waiting for fireware support */
1370 ExtraDat->status1 = 0x00; /* Not yet, waiting for fireware support */
1371 ExtraDat->ovrflg = bbuf[0];
1372 ExtraDat->mngflg = bbuf[1];
1373 ExtraDat->logadr = memstick_logaddr(bbuf[2], bbuf[3]);
1375 return USB_STOR_TRANSPORT_GOOD;
1378 static int ms_libsearch_block_from_physical(struct us_data *us, u16 phyblk)
1381 struct ms_lib_type_extdat extdat; /* need check */
1382 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1385 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1388 for (blk = phyblk + 1; blk != phyblk; blk++) {
1389 if ((blk & MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK) == 0)
1390 blk -= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1392 if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED_ERASED) {
1394 } else if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED) {
1395 switch (ms_lib_read_extra(us, blk, 0, &extdat)) {
1396 case MS_STATUS_SUCCESS:
1397 case MS_STATUS_SUCCESS_WITH_ECC:
1399 case MS_NOCARD_ERROR:
1400 return MS_NOCARD_ERROR;
1401 case MS_STATUS_INT_ERROR:
1403 case MS_ERROR_FLASH_READ:
1405 ms_lib_setacquired_errorblock(us, blk);
1409 if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1410 ms_lib_setacquired_errorblock(us, blk);
1414 switch (ms_lib_erase_phyblock(us, blk)) {
1415 case MS_STATUS_SUCCESS:
1417 case MS_STATUS_ERROR:
1419 case MS_ERROR_FLASH_ERASE:
1421 ms_lib_error_phyblock(us, blk);
1429 static int ms_libsearch_block_from_logical(struct us_data *us, u16 logblk)
1432 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1434 phyblk = ms_libconv_to_physical(info, logblk);
1435 if (phyblk >= MS_LB_ERROR) {
1436 if (logblk >= info->MS_Lib.NumberOfLogBlock)
1439 phyblk = (logblk + MS_NUMBER_OF_BOOT_BLOCK) / MS_LOGICAL_BLOCKS_PER_SEGMENT;
1440 phyblk *= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1441 phyblk += MS_PHYSICAL_BLOCKS_PER_SEGMENT - 1;
1444 return ms_libsearch_block_from_physical(us, phyblk);
1447 static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
1449 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
1451 /* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
1452 if (info->MS_Status.Insert && info->MS_Status.Ready) {
1453 return USB_STOR_TRANSPORT_GOOD;
1456 return USB_STOR_TRANSPORT_GOOD;
1459 return USB_STOR_TRANSPORT_GOOD;
1462 static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
1464 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1465 unsigned char mediaNoWP[12] = {
1466 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
1467 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1468 unsigned char mediaWP[12] = {
1469 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
1470 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1472 if (info->MS_Status.WtP)
1473 usb_stor_set_xfer_buf(mediaWP, 12, srb);
1475 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
1477 return USB_STOR_TRANSPORT_GOOD;
1480 static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
1484 unsigned int offset = 0;
1485 unsigned char buf[8];
1486 struct scatterlist *sg = NULL;
1487 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1489 usb_stor_dbg(us, "ms_scsi_read_capacity\n");
1491 if (info->MS_Status.IsMSPro)
1492 bl_num = info->MSP_TotalBlock - 1;
1494 bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1;
1496 info->bl_num = bl_num;
1497 usb_stor_dbg(us, "bl_len = %x\n", bl_len);
1498 usb_stor_dbg(us, "bl_num = %x\n", bl_num);
1500 /*srb->request_bufflen = 8; */
1501 buf[0] = (bl_num >> 24) & 0xff;
1502 buf[1] = (bl_num >> 16) & 0xff;
1503 buf[2] = (bl_num >> 8) & 0xff;
1504 buf[3] = (bl_num >> 0) & 0xff;
1505 buf[4] = (bl_len >> 24) & 0xff;
1506 buf[5] = (bl_len >> 16) & 0xff;
1507 buf[6] = (bl_len >> 8) & 0xff;
1508 buf[7] = (bl_len >> 0) & 0xff;
1510 usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
1512 return USB_STOR_TRANSPORT_GOOD;
1515 static void ms_lib_phy_to_log_range(u16 PhyBlock, u16 *LogStart, u16 *LogEnde)
1517 PhyBlock /= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1520 *LogStart = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT + (PhyBlock - 1) * MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1521 *LogEnde = *LogStart + MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1524 *LogEnde = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT;/*494*/
1528 static int ms_lib_read_extrablock(struct us_data *us, u32 PhyBlock,
1529 u8 PageNum, u8 blen, void *buf)
1531 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1534 /* Read Extra Data */
1535 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1536 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1537 bcb->DataTransferLength = 0x4 * blen;
1538 bcb->Flags = US_BULK_FLAG_IN;
1541 bcb->CDB[5] = (unsigned char)(PageNum);
1542 bcb->CDB[4] = (unsigned char)(PhyBlock);
1543 bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
1544 bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
1547 result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1548 if (result != USB_STOR_XFER_GOOD)
1549 return USB_STOR_TRANSPORT_ERROR;
1551 return USB_STOR_TRANSPORT_GOOD;
1554 static int ms_lib_scan_logicalblocknumber(struct us_data *us, u16 btBlk1st)
1556 u16 PhyBlock, newblk, i;
1557 u16 LogStart, LogEnde;
1558 struct ms_lib_type_extdat extdat;
1559 u32 count = 0, index = 0;
1560 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1561 u8 *bbuf = info->bbuf;
1563 for (PhyBlock = 0; PhyBlock < info->MS_Lib.NumberOfPhyBlock;) {
1564 ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde);
1566 for (i = 0; i < MS_PHYSICAL_BLOCKS_PER_SEGMENT; i++, PhyBlock++) {
1567 switch (ms_libconv_to_logical(info, PhyBlock)) {
1568 case MS_STATUS_ERROR:
1574 if (count == PhyBlock) {
1575 ms_lib_read_extrablock(us, PhyBlock, 0, 0x80,
1579 index = (PhyBlock % 0x80) * 4;
1581 extdat.ovrflg = bbuf[index];
1582 extdat.mngflg = bbuf[index+1];
1583 extdat.logadr = memstick_logaddr(bbuf[index+2],
1586 if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1587 ms_lib_setacquired_errorblock(us, PhyBlock);
1591 if ((extdat.mngflg & MS_REG_MNG_ATFLG) == MS_REG_MNG_ATFLG_ATTBL) {
1592 ms_lib_erase_phyblock(us, PhyBlock);
1596 if (extdat.logadr != MS_LB_NOT_USED) {
1597 if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) {
1598 ms_lib_erase_phyblock(us, PhyBlock);
1602 newblk = ms_libconv_to_physical(info, extdat.logadr);
1604 if (newblk != MS_LB_NOT_USED) {
1605 if (extdat.logadr == 0) {
1606 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1607 if (ms_lib_check_disableblock(us, btBlk1st)) {
1608 ms_lib_set_logicalpair(us, extdat.logadr, newblk);
1613 ms_lib_read_extra(us, newblk, 0, &extdat);
1614 if ((extdat.ovrflg & MS_REG_OVR_UDST) == MS_REG_OVR_UDST_UPDATING) {
1615 ms_lib_erase_phyblock(us, PhyBlock);
1618 ms_lib_erase_phyblock(us, newblk);
1622 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1627 return MS_STATUS_SUCCESS;
1631 static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
1634 unsigned char *cdb = srb->cmnd;
1635 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1636 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1638 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
1639 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
1640 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1641 u32 blenByte = blen * 0x200;
1643 if (bn > info->bl_num)
1644 return USB_STOR_TRANSPORT_ERROR;
1646 if (info->MS_Status.IsMSPro) {
1647 result = ene_load_bincode(us, MSP_RW_PATTERN);
1648 if (result != USB_STOR_XFER_GOOD) {
1649 usb_stor_dbg(us, "Load MPS RW pattern Fail !!\n");
1650 return USB_STOR_TRANSPORT_ERROR;
1653 /* set up the command wrapper */
1654 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1655 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1656 bcb->DataTransferLength = blenByte;
1657 bcb->Flags = US_BULK_FLAG_IN;
1660 bcb->CDB[5] = (unsigned char)(bn);
1661 bcb->CDB[4] = (unsigned char)(bn>>8);
1662 bcb->CDB[3] = (unsigned char)(bn>>16);
1663 bcb->CDB[2] = (unsigned char)(bn>>24);
1665 result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
1674 buf = kmalloc(blenByte, GFP_KERNEL);
1676 return USB_STOR_TRANSPORT_ERROR;
1678 result = ene_load_bincode(us, MS_RW_PATTERN);
1679 if (result != USB_STOR_XFER_GOOD) {
1680 pr_info("Load MS RW pattern Fail !!\n");
1681 result = USB_STOR_TRANSPORT_ERROR;
1685 logblk = (u16)(bn / info->MS_Lib.PagesPerBlock);
1686 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1689 if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1690 len = info->MS_Lib.PagesPerBlock-PageNum;
1694 phyblk = ms_libconv_to_physical(info, logblk);
1695 blkno = phyblk * 0x20 + PageNum;
1697 /* set up the command wrapper */
1698 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1699 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1700 bcb->DataTransferLength = 0x200 * len;
1701 bcb->Flags = US_BULK_FLAG_IN;
1704 bcb->CDB[5] = (unsigned char)(blkno);
1705 bcb->CDB[4] = (unsigned char)(blkno>>8);
1706 bcb->CDB[3] = (unsigned char)(blkno>>16);
1707 bcb->CDB[2] = (unsigned char)(blkno>>24);
1709 result = ene_send_scsi_cmd(us, FDIR_READ, buf+offset, 0);
1710 if (result != USB_STOR_XFER_GOOD) {
1711 pr_info("MS_SCSI_Read --- result = %x\n", result);
1712 result = USB_STOR_TRANSPORT_ERROR;
1721 offset += MS_BYTES_PER_PAGE*len;
1723 usb_stor_set_xfer_buf(buf, blenByte, srb);
1730 static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
1733 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1734 unsigned char *cdb = srb->cmnd;
1735 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1737 u32 bn = ((cdb[2] << 24) & 0xff000000) |
1738 ((cdb[3] << 16) & 0x00ff0000) |
1739 ((cdb[4] << 8) & 0x0000ff00) |
1740 ((cdb[5] << 0) & 0x000000ff);
1741 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1742 u32 blenByte = blen * 0x200;
1744 if (bn > info->bl_num)
1745 return USB_STOR_TRANSPORT_ERROR;
1747 if (info->MS_Status.IsMSPro) {
1748 result = ene_load_bincode(us, MSP_RW_PATTERN);
1749 if (result != USB_STOR_XFER_GOOD) {
1750 pr_info("Load MSP RW pattern Fail !!\n");
1751 return USB_STOR_TRANSPORT_ERROR;
1754 /* set up the command wrapper */
1755 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1756 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1757 bcb->DataTransferLength = blenByte;
1761 bcb->CDB[5] = (unsigned char)(bn);
1762 bcb->CDB[4] = (unsigned char)(bn>>8);
1763 bcb->CDB[3] = (unsigned char)(bn>>16);
1764 bcb->CDB[2] = (unsigned char)(bn>>24);
1766 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
1772 u16 len, oldphy, newphy;
1774 buf = kmalloc(blenByte, GFP_KERNEL);
1776 return USB_STOR_TRANSPORT_ERROR;
1777 usb_stor_set_xfer_buf(buf, blenByte, srb);
1779 result = ene_load_bincode(us, MS_RW_PATTERN);
1780 if (result != USB_STOR_XFER_GOOD) {
1781 pr_info("Load MS RW pattern Fail !!\n");
1782 result = USB_STOR_TRANSPORT_ERROR;
1786 PhyBlockAddr = (u16)(bn / info->MS_Lib.PagesPerBlock);
1787 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1790 if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1791 len = info->MS_Lib.PagesPerBlock-PageNum;
1795 oldphy = ms_libconv_to_physical(info, PhyBlockAddr); /* need check us <-> info */
1796 newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr);
1798 result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len);
1800 if (result != USB_STOR_XFER_GOOD) {
1801 pr_info("MS_SCSI_Write --- result = %x\n", result);
1802 result = USB_STOR_TRANSPORT_ERROR;
1806 info->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED;
1807 ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy);
1814 offset += MS_BYTES_PER_PAGE*len;
1826 static int ene_get_card_type(struct us_data *us, u16 index, void *buf)
1828 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1831 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1832 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1833 bcb->DataTransferLength = 0x01;
1834 bcb->Flags = US_BULK_FLAG_IN;
1836 bcb->CDB[2] = (unsigned char)(index>>8);
1837 bcb->CDB[3] = (unsigned char)index;
1839 result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1843 static int ene_get_card_status(struct us_data *us, u8 *buf)
1847 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1849 /*usb_stor_dbg(us, "transport --- ENE_ReadSDReg\n");*/
1850 reg4b = *(u32 *)&buf[0x18];
1851 info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f);
1853 tmpreg = (u16) reg4b;
1854 reg4b = *(u32 *)(&buf[0x14]);
1855 if (info->SD_Status.HiCapacity && !info->SD_Status.IsMMC)
1856 info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
1858 info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
1859 info->SD_C_SIZE_MULT = (u8)(reg4b >> 7) & 0x07;
1860 if (info->SD_Status.HiCapacity && info->SD_Status.IsMMC)
1861 info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
1863 if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
1864 info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN);
1865 info->SD_READ_BL_LEN = SD_BLOCK_LEN;
1867 info->SD_Block_Mult = 1;
1870 return USB_STOR_TRANSPORT_GOOD;
1873 static int ene_load_bincode(struct us_data *us, unsigned char flag)
1876 char *fw_name = NULL;
1877 unsigned char *buf = NULL;
1878 const struct firmware *sd_fw = NULL;
1879 int result = USB_STOR_TRANSPORT_ERROR;
1880 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1881 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1883 if (info->BIN_FLAG == flag)
1884 return USB_STOR_TRANSPORT_GOOD;
1888 case SD_INIT1_PATTERN:
1889 usb_stor_dbg(us, "SD_INIT1_PATTERN\n");
1890 fw_name = SD_INIT1_FIRMWARE;
1892 case SD_INIT2_PATTERN:
1893 usb_stor_dbg(us, "SD_INIT2_PATTERN\n");
1894 fw_name = SD_INIT2_FIRMWARE;
1897 usb_stor_dbg(us, "SD_RW_PATTERN\n");
1898 fw_name = SD_RW_FIRMWARE;
1901 case MS_INIT_PATTERN:
1902 usb_stor_dbg(us, "MS_INIT_PATTERN\n");
1903 fw_name = MS_INIT_FIRMWARE;
1905 case MSP_RW_PATTERN:
1906 usb_stor_dbg(us, "MSP_RW_PATTERN\n");
1907 fw_name = MSP_RW_FIRMWARE;
1910 usb_stor_dbg(us, "MS_RW_PATTERN\n");
1911 fw_name = MS_RW_FIRMWARE;
1914 usb_stor_dbg(us, "----------- Unknown PATTERN ----------\n");
1918 err = reject_firmware(&sd_fw, fw_name, &us->pusb_dev->dev);
1920 usb_stor_dbg(us, "load firmware %s failed\n", fw_name);
1923 buf = kmemdup(sd_fw->data, sd_fw->size, GFP_KERNEL);
1927 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1928 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1929 bcb->DataTransferLength = sd_fw->size;
1933 result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1934 if (us->srb != NULL)
1935 scsi_set_resid(us->srb, 0);
1936 info->BIN_FLAG = flag;
1940 release_firmware(sd_fw);
1944 static int ms_card_init(struct us_data *us)
1948 unsigned char *PageBuffer0 = NULL, *PageBuffer1 = NULL;
1949 struct ms_lib_type_extdat extdat;
1950 u16 btBlk1st, btBlk2nd;
1952 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1954 printk(KERN_INFO "MS_CardInit start\n");
1956 ms_lib_free_allocatedarea(us); /* Clean buffer and set struct us_data flag to 0 */
1958 /* get two PageBuffer */
1959 PageBuffer0 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1960 PageBuffer1 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1961 if ((PageBuffer0 == NULL) || (PageBuffer1 == NULL)) {
1962 result = MS_NO_MEMORY_ERROR;
1966 btBlk1st = btBlk2nd = MS_LB_NOT_USED;
1969 for (TmpBlock = 0; TmpBlock < MS_MAX_INITIAL_ERROR_BLOCKS+2; TmpBlock++) {
1971 switch (ms_read_readpage(us, TmpBlock, 0, (u32 *)PageBuffer0, &extdat)) {
1972 case MS_STATUS_SUCCESS:
1974 case MS_STATUS_INT_ERROR:
1976 case MS_STATUS_ERROR:
1981 if ((extdat.ovrflg & MS_REG_OVR_BKST) == MS_REG_OVR_BKST_NG)
1984 if (((extdat.mngflg & MS_REG_MNG_SYSFLG) == MS_REG_MNG_SYSFLG_USER) ||
1985 (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wBlockID) != MS_BOOT_BLOCK_ID) ||
1986 (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wFormatVersion) != MS_BOOT_BLOCK_FORMAT_VERSION) ||
1987 (((struct ms_bootblock_page0 *)PageBuffer0)->header.bNumberOfDataEntry != MS_BOOT_BLOCK_DATA_ENTRIES))
1990 if (btBlk1st != MS_LB_NOT_USED) {
1991 btBlk2nd = TmpBlock;
1995 btBlk1st = TmpBlock;
1996 memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE);
1997 if (extdat.status1 & (MS_REG_ST1_DTER | MS_REG_ST1_EXER | MS_REG_ST1_FGER))
2001 if (btBlk1st == MS_LB_NOT_USED) {
2002 result = MS_STATUS_ERROR;
2007 if ((extdat.status0 & MS_REG_ST0_WP) == MS_REG_ST0_WP_ON)
2008 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2010 result = MS_STATUS_ERROR;
2011 /* 1st Boot Block */
2012 if (btBlk1stErred == 0)
2013 result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1);
2015 /* 2nd Boot Block */
2016 if (result && (btBlk2nd != MS_LB_NOT_USED))
2017 result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0);
2020 result = MS_STATUS_ERROR;
2024 for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++)
2025 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2027 info->MS_Lib.Phy2LogMap[btBlk1st] = MS_LB_BOOT_BLOCK;
2029 if (btBlk2nd != MS_LB_NOT_USED) {
2030 for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++)
2031 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2033 info->MS_Lib.Phy2LogMap[btBlk2nd] = MS_LB_BOOT_BLOCK;
2036 result = ms_lib_scan_logicalblocknumber(us, btBlk1st);
2040 for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT;
2041 TmpBlock < info->MS_Lib.NumberOfPhyBlock;
2042 TmpBlock += MS_PHYSICAL_BLOCKS_PER_SEGMENT) {
2043 if (ms_count_freeblock(us, TmpBlock) == 0) {
2044 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2050 if (ms_lib_alloc_writebuf(us)) {
2051 result = MS_NO_MEMORY_ERROR;
2055 result = MS_STATUS_SUCCESS;
2061 printk(KERN_INFO "MS_CardInit end\n");
2065 static int ene_ms_init(struct us_data *us)
2067 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2069 u16 MSP_BlockSize, MSP_UserAreaBlocks;
2070 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2071 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 = *(struct MS_STATUS *) bbuf;
2098 if (info->MS_Status.Insert && info->MS_Status.Ready) {
2099 printk(KERN_INFO "Insert = %x\n", info->MS_Status.Insert);
2100 printk(KERN_INFO "Ready = %x\n", info->MS_Status.Ready);
2101 printk(KERN_INFO "IsMSPro = %x\n", info->MS_Status.IsMSPro);
2102 printk(KERN_INFO "IsMSPHG = %x\n", info->MS_Status.IsMSPHG);
2103 printk(KERN_INFO "WtP= %x\n", info->MS_Status.WtP);
2104 if (info->MS_Status.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 = *(struct SD_STATUS *) bbuf;
2166 if (info->SD_Status.Insert && info->SD_Status.Ready) {
2167 struct SD_STATUS *s = &info->SD_Status;
2169 ene_get_card_status(us, bbuf);
2170 usb_stor_dbg(us, "Insert = %x\n", s->Insert);
2171 usb_stor_dbg(us, "Ready = %x\n", s->Ready);
2172 usb_stor_dbg(us, "IsMMC = %x\n", s->IsMMC);
2173 usb_stor_dbg(us, "HiCapacity = %x\n", s->HiCapacity);
2174 usb_stor_dbg(us, "HiSpeed = %x\n", s->HiSpeed);
2175 usb_stor_dbg(us, "WtP = %x\n", s->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.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.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.Ready || info->MS_Status.Ready)))
2304 result = ene_init(us);
2305 if (result == USB_STOR_XFER_GOOD) {
2306 result = USB_STOR_TRANSPORT_ERROR;
2307 if (info->SD_Status.Ready)
2308 result = sd_scsi_irp(us, srb);
2310 if (info->MS_Status.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)
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.Ready = 0; */
2387 info->SD_Status = *(struct SD_STATUS *)&tmp;
2388 info->MS_Status = *(struct MS_STATUS *)&tmp;
2389 info->SM_Status = *(struct SM_STATUS *)&tmp;
2394 static int ene_ub6250_reset_resume(struct usb_interface *iface)
2397 struct us_data *us = usb_get_intfdata(iface);
2398 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2400 /* Report the reset to the SCSI core */
2401 usb_stor_reset_resume(iface);
2404 * FIXME: Notify the subdrivers that they need to reinitialize
2407 info->Power_IsResum = true;
2408 /*info->SD_Status.Ready = 0; */
2409 info->SD_Status = *(struct SD_STATUS *)&tmp;
2410 info->MS_Status = *(struct MS_STATUS *)&tmp;
2411 info->SM_Status = *(struct SM_STATUS *)&tmp;
2418 #define ene_ub6250_resume NULL
2419 #define ene_ub6250_reset_resume NULL
2423 static struct usb_driver ene_ub6250_driver = {
2425 .probe = ene_ub6250_probe,
2426 .disconnect = usb_stor_disconnect,
2427 .suspend = usb_stor_suspend,
2428 .resume = ene_ub6250_resume,
2429 .reset_resume = ene_ub6250_reset_resume,
2430 .pre_reset = usb_stor_pre_reset,
2431 .post_reset = usb_stor_post_reset,
2432 .id_table = ene_ub6250_usb_ids,
2437 module_usb_stor_driver(ene_ub6250_driver, ene_ub6250_host_template, DRV_NAME);