3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the
5 * Free Software Foundation; either version 2, or (at your option) any
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 675 Mass Ave, Cambridge, MA 02139, USA.
17 #include <linux/jiffies.h>
18 #include <linux/errno.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
22 #include <scsi/scsi.h>
23 #include <scsi/scsi_cmnd.h>
25 #include <linux/firmware.h>
28 #include "transport.h"
32 #define SD_INIT1_FIRMWARE "/*(DEBLOBBED)*/"
33 #define SD_INIT2_FIRMWARE "/*(DEBLOBBED)*/"
34 #define SD_RW_FIRMWARE "/*(DEBLOBBED)*/"
35 #define MS_INIT_FIRMWARE "/*(DEBLOBBED)*/"
36 #define MSP_RW_FIRMWARE "/*(DEBLOBBED)*/"
37 #define MS_RW_FIRMWARE "/*(DEBLOBBED)*/"
39 MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
40 MODULE_LICENSE("GPL");
44 * The table of devices
46 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
47 vendorName, productName, useProtocol, useTransport, \
48 initFunction, flags) \
49 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
50 .driver_info = (flags)}
52 static struct usb_device_id ene_ub6250_usb_ids[] = {
53 # include "unusual_ene_ub6250.h"
54 { } /* Terminating entry */
56 MODULE_DEVICE_TABLE(usb, ene_ub6250_usb_ids);
63 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
64 vendor_name, product_name, use_protocol, use_transport, \
65 init_function, Flags) \
67 .vendorName = vendor_name, \
68 .productName = product_name, \
69 .useProtocol = use_protocol, \
70 .useTransport = use_transport, \
71 .initFunction = init_function, \
74 static struct us_unusual_dev ene_ub6250_unusual_dev_list[] = {
75 # include "unusual_ene_ub6250.h"
76 { } /* Terminating entry */
83 /* ENE bin code len */
84 #define ENE_BIN_CODE_LEN 0x800
86 #define REG_CARD_STATUS 0xFF83
87 #define REG_HW_TRAP1 0xFF89
90 #define SS_SUCCESS 0x00 /* No Sense */
91 #define SS_NOT_READY 0x02
92 #define SS_MEDIUM_ERR 0x03
93 #define SS_HW_ERR 0x04
94 #define SS_ILLEGAL_REQUEST 0x05
95 #define SS_UNIT_ATTENTION 0x06
97 /* ENE Load FW Pattern */
98 #define SD_INIT1_PATTERN 1
99 #define SD_INIT2_PATTERN 2
100 #define SD_RW_PATTERN 3
101 #define MS_INIT_PATTERN 4
102 #define MSP_RW_PATTERN 5
103 #define MS_RW_PATTERN 6
104 #define SM_INIT_PATTERN 7
105 #define SM_RW_PATTERN 8
112 /* Status Register 1 */
113 #define MS_REG_ST1_MB 0x80 /* media busy */
114 #define MS_REG_ST1_FB1 0x40 /* flush busy 1 */
115 #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */
116 #define MS_REG_ST1_UCDT 0x10 /* unable to correct data */
117 #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */
118 #define MS_REG_ST1_UCEX 0x04 /* unable to correct extra */
119 #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */
120 #define MS_REG_ST1_UCFG 0x01 /* unable to correct overwrite flag */
121 #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)
124 #define MS_REG_OVR_BKST 0x80 /* block status */
125 #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */
126 #define MS_REG_OVR_BKST_NG 0x00 /* NG */
127 #define MS_REG_OVR_PGST0 0x40 /* page status */
128 #define MS_REG_OVR_PGST1 0x20
129 #define MS_REG_OVR_PGST_MASK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1)
130 #define MS_REG_OVR_PGST_OK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1) /* OK */
131 #define MS_REG_OVR_PGST_NG MS_REG_OVR_PGST1 /* NG */
132 #define MS_REG_OVR_PGST_DATA_ERROR 0x00 /* data error */
133 #define MS_REG_OVR_UDST 0x10 /* update status */
134 #define MS_REG_OVR_UDST_UPDATING 0x00 /* updating */
135 #define MS_REG_OVR_UDST_NO_UPDATE MS_REG_OVR_UDST
136 #define MS_REG_OVR_RESERVED 0x08
137 #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)
139 /* Management Flag */
140 #define MS_REG_MNG_SCMS0 0x20 /* serial copy management system */
141 #define MS_REG_MNG_SCMS1 0x10
142 #define MS_REG_MNG_SCMS_MASK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
143 #define MS_REG_MNG_SCMS_COPY_OK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
144 #define MS_REG_MNG_SCMS_ONE_COPY MS_REG_MNG_SCMS1
145 #define MS_REG_MNG_SCMS_NO_COPY 0x00
146 #define MS_REG_MNG_ATFLG 0x08 /* address transfer table flag */
147 #define MS_REG_MNG_ATFLG_OTHER MS_REG_MNG_ATFLG /* other */
148 #define MS_REG_MNG_ATFLG_ATTBL 0x00 /* address transfer table */
149 #define MS_REG_MNG_SYSFLG 0x04 /* system flag */
150 #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */
151 #define MS_REG_MNG_SYSFLG_BOOT 0x00 /* system block */
152 #define MS_REG_MNG_RESERVED 0xc3
153 #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)
156 #define MS_MAX_PAGES_PER_BLOCK 32
157 #define MS_MAX_INITIAL_ERROR_BLOCKS 10
158 #define MS_LIB_BITS_PER_BYTE 8
160 #define MS_SYSINF_FORMAT_FAT 1
161 #define MS_SYSINF_USAGE_GENERAL 0
163 #define MS_SYSINF_MSCLASS_TYPE_1 1
164 #define MS_SYSINF_PAGE_SIZE MS_BYTES_PER_PAGE /* fixed */
166 #define MS_SYSINF_CARDTYPE_RDONLY 1
167 #define MS_SYSINF_CARDTYPE_RDWR 2
168 #define MS_SYSINF_CARDTYPE_HYBRID 3
169 #define MS_SYSINF_SECURITY 0x01
170 #define MS_SYSINF_SECURITY_NO_SUPPORT MS_SYSINF_SECURITY
171 #define MS_SYSINF_SECURITY_SUPPORT 0
173 #define MS_SYSINF_RESERVED1 1
174 #define MS_SYSINF_RESERVED2 1
176 #define MS_SYSENT_TYPE_INVALID_BLOCK 0x01
177 #define MS_SYSENT_TYPE_CIS_IDI 0x0a /* CIS/IDI */
179 #define SIZE_OF_KIRO 1024
180 #define BYTE_MASK 0xff
183 #define MS_STATUS_WRITE_PROTECT 0x0106
184 #define MS_STATUS_SUCCESS 0x0000
185 #define MS_ERROR_FLASH_READ 0x8003
186 #define MS_ERROR_FLASH_ERASE 0x8005
187 #define MS_LB_ERROR 0xfff0
188 #define MS_LB_BOOT_BLOCK 0xfff1
189 #define MS_LB_INITIAL_ERROR 0xfff2
190 #define MS_STATUS_SUCCESS_WITH_ECC 0xfff3
191 #define MS_LB_ACQUIRED_ERROR 0xfff4
192 #define MS_LB_NOT_USED_ERASED 0xfff5
193 #define MS_NOCARD_ERROR 0xfff8
194 #define MS_NO_MEMORY_ERROR 0xfff9
195 #define MS_STATUS_INT_ERROR 0xfffa
196 #define MS_STATUS_ERROR 0xfffe
197 #define MS_LB_NOT_USED 0xffff
199 #define MS_REG_MNG_SYSFLG 0x04 /* system flag */
200 #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */
202 #define MS_BOOT_BLOCK_ID 0x0001
203 #define MS_BOOT_BLOCK_FORMAT_VERSION 0x0100
204 #define MS_BOOT_BLOCK_DATA_ENTRIES 2
206 #define MS_NUMBER_OF_SYSTEM_ENTRY 4
207 #define MS_NUMBER_OF_BOOT_BLOCK 2
208 #define MS_BYTES_PER_PAGE 512
209 #define MS_LOGICAL_BLOCKS_PER_SEGMENT 496
210 #define MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT 494
212 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT 0x200 /* 512 */
213 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK 0x1ff
216 #define MS_REG_OVR_BKST 0x80 /* block status */
217 #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */
218 #define MS_REG_OVR_BKST_NG 0x00 /* NG */
220 /* Status Register 1 */
221 #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */
222 #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */
223 #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */
225 /* MemoryStick Register */
226 /* Status Register 0 */
227 #define MS_REG_ST0_WP 0x01 /* write protected */
228 #define MS_REG_ST0_WP_ON MS_REG_ST0_WP
230 #define MS_LIB_CTRL_RDONLY 0
231 #define MS_LIB_CTRL_WRPROTECT 1
234 #define ms_libconv_to_logical(pdx, PhyBlock) (((PhyBlock) >= (pdx)->MS_Lib.NumberOfPhyBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Phy2LogMap[PhyBlock])
235 #define ms_libconv_to_physical(pdx, LogBlock) (((LogBlock) >= (pdx)->MS_Lib.NumberOfLogBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Log2PhyMap[LogBlock])
237 #define ms_lib_ctrl_set(pdx, Flag) ((pdx)->MS_Lib.flags |= (1 << (Flag)))
238 #define ms_lib_ctrl_reset(pdx, Flag) ((pdx)->MS_Lib.flags &= ~(1 << (Flag)))
239 #define ms_lib_ctrl_check(pdx, Flag) ((pdx)->MS_Lib.flags & (1 << (Flag)))
241 #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))
242 #define ms_lib_clear_pagemap(pdx) memset((pdx)->MS_Lib.pagemap, 0, sizeof((pdx)->MS_Lib.pagemap))
243 #define memstick_logaddr(logadr1, logadr0) ((((u16)(logadr1)) << 8) | (logadr0))
277 struct ms_bootblock_cis {
278 u8 bCistplDEVICE[6]; /* 0 */
279 u8 bCistplDEVICE0C[6]; /* 6 */
280 u8 bCistplJEDECC[4]; /* 12 */
281 u8 bCistplMANFID[6]; /* 16 */
282 u8 bCistplVER1[32]; /* 22 */
283 u8 bCistplFUNCID[4]; /* 54 */
284 u8 bCistplFUNCE0[4]; /* 58 */
285 u8 bCistplFUNCE1[5]; /* 62 */
286 u8 bCistplCONF[7]; /* 67 */
287 u8 bCistplCFTBLENT0[10];/* 74 */
288 u8 bCistplCFTBLENT1[8]; /* 84 */
289 u8 bCistplCFTBLENT2[12];/* 92 */
290 u8 bCistplCFTBLENT3[8]; /* 104 */
291 u8 bCistplCFTBLENT4[17];/* 112 */
292 u8 bCistplCFTBLENT5[8]; /* 129 */
293 u8 bCistplCFTBLENT6[17];/* 137 */
294 u8 bCistplCFTBLENT7[8]; /* 154 */
295 u8 bCistplNOLINK[3]; /* 162 */
298 struct ms_bootblock_idi {
299 #define MS_IDI_GENERAL_CONF 0x848A
300 u16 wIDIgeneralConfiguration; /* 0 */
301 u16 wIDInumberOfCylinder; /* 1 */
302 u16 wIDIreserved0; /* 2 */
303 u16 wIDInumberOfHead; /* 3 */
304 u16 wIDIbytesPerTrack; /* 4 */
305 u16 wIDIbytesPerSector; /* 5 */
306 u16 wIDIsectorsPerTrack; /* 6 */
307 u16 wIDItotalSectors[2]; /* 7-8 high,low */
308 u16 wIDIreserved1[11]; /* 9-19 */
309 u16 wIDIbufferType; /* 20 */
310 u16 wIDIbufferSize; /* 21 */
311 u16 wIDIlongCmdECC; /* 22 */
312 u16 wIDIfirmVersion[4]; /* 23-26 */
313 u16 wIDImodelName[20]; /* 27-46 */
314 u16 wIDIreserved2; /* 47 */
315 u16 wIDIlongWordSupported; /* 48 */
316 u16 wIDIdmaSupported; /* 49 */
317 u16 wIDIreserved3; /* 50 */
318 u16 wIDIpioTiming; /* 51 */
319 u16 wIDIdmaTiming; /* 52 */
320 u16 wIDItransferParameter; /* 53 */
321 u16 wIDIformattedCylinder; /* 54 */
322 u16 wIDIformattedHead; /* 55 */
323 u16 wIDIformattedSectorsPerTrack;/* 56 */
324 u16 wIDIformattedTotalSectors[2];/* 57-58 */
325 u16 wIDImultiSector; /* 59 */
326 u16 wIDIlbaSectors[2]; /* 60-61 */
327 u16 wIDIsingleWordDMA; /* 62 */
328 u16 wIDImultiWordDMA; /* 63 */
329 u16 wIDIreserved4[192]; /* 64-255 */
332 struct ms_bootblock_sysent_rec {
339 struct ms_bootblock_sysent {
340 struct ms_bootblock_sysent_rec entry[MS_NUMBER_OF_SYSTEM_ENTRY];
343 struct ms_bootblock_sysinf {
344 u8 bMsClass; /* must be 1 */
345 u8 bCardType; /* see below */
346 u16 wBlockSize; /* n KB */
347 u16 wBlockNumber; /* number of physical block */
348 u16 wTotalBlockNumber; /* number of logical block */
349 u16 wPageSize; /* must be 0x200 */
350 u8 bExtraSize; /* 0x10 */
354 u8 bAssemblyMakerCode;
355 u8 bAssemblyMachineCode[3];
356 u16 wMemoryMakerCode;
357 u16 wMemoryDeviceCode;
363 u16 wControllerChipNumber;
364 u16 wControllerFunction; /* New MS */
365 u8 bReserved3[9]; /* New MS */
366 u8 bParallelSupport; /* New MS */
367 u16 wFormatValue; /* New MS */
377 struct ms_bootblock_header {
381 u8 bNumberOfDataEntry;
385 struct ms_bootblock_page0 {
386 struct ms_bootblock_header header;
387 struct ms_bootblock_sysent sysent;
388 struct ms_bootblock_sysinf sysinf;
391 struct ms_bootblock_cis_idi {
393 struct ms_bootblock_cis cis;
398 struct ms_bootblock_idi idi;
404 /* ENE MS Lib struct */
405 struct ms_lib_type_extdat {
418 u32 NumberOfCylinder;
419 u32 SectorsPerCylinder;
420 u16 cardType; /* R/W, RO, Hybrid */
423 u16 NumberOfPhyBlock;
424 u16 NumberOfLogBlock;
426 u16 *Phy2LogMap; /* phy2log table */
427 u16 *Log2PhyMap; /* log2phy table */
429 unsigned char *pagemap[(MS_MAX_PAGES_PER_BLOCK + (MS_LIB_BITS_PER_BYTE-1)) / MS_LIB_BITS_PER_BYTE];
430 unsigned char *blkpag;
431 struct ms_lib_type_extdat *blkext;
432 unsigned char copybuf[512];
436 /* SD Block Length */
437 /* 2^9 = 512 Bytes, The HW maximum read/write data length */
438 #define SD_BLOCK_LEN 9
440 struct ene_ub6250_info {
442 struct SD_STATUS SD_Status;
443 struct MS_STATUS MS_Status;
444 struct SM_STATUS SM_Status;
446 /* ----- SD Control Data ---------------- */
447 /*SD_REGISTER SD_Regs; */
453 /* SD/MMC New spec. */
456 u8 SD20_HIGH_CAPACITY;
460 u8 MMC_HIGH_CAPACITY;
462 /*----- MS Control Data ---------------- */
465 struct ms_lib_ctrl MS_Lib;
469 /*----- SM Control Data ---------------- */
473 unsigned char *testbuf;
478 /*------Power Managerment ---------------*/
482 static int ene_sd_init(struct us_data *us);
483 static int ene_ms_init(struct us_data *us);
484 static int ene_load_bincode(struct us_data *us, unsigned char flag);
486 static void ene_ub6250_info_destructor(void *extra)
492 static int ene_send_scsi_cmd(struct us_data *us, u8 fDir, void *buf, int use_sg)
494 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
495 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
498 unsigned int residue;
499 unsigned int cswlen = 0, partial = 0;
500 unsigned int transfer_length = bcb->DataTransferLength;
502 /* usb_stor_dbg(us, "transport --- ene_send_scsi_cmd\n"); */
503 /* send cmd to out endpoint */
504 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
505 bcb, US_BULK_CB_WRAP_LEN, NULL);
506 if (result != USB_STOR_XFER_GOOD) {
507 usb_stor_dbg(us, "send cmd to out endpoint fail ---\n");
508 return USB_STOR_TRANSPORT_ERROR;
512 unsigned int pipe = fDir;
514 if (fDir == FDIR_READ)
515 pipe = us->recv_bulk_pipe;
517 pipe = us->send_bulk_pipe;
521 result = usb_stor_bulk_srb(us, pipe, us->srb);
523 result = usb_stor_bulk_transfer_sg(us, pipe, buf,
524 transfer_length, 0, &partial);
526 if (result != USB_STOR_XFER_GOOD) {
527 usb_stor_dbg(us, "data transfer fail ---\n");
528 return USB_STOR_TRANSPORT_ERROR;
532 /* Get CSW for device status */
533 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
534 US_BULK_CS_WRAP_LEN, &cswlen);
536 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
537 usb_stor_dbg(us, "Received 0-length CSW; retrying...\n");
538 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
539 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
542 if (result == USB_STOR_XFER_STALLED) {
543 /* get the status again */
544 usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n");
545 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
546 bcs, US_BULK_CS_WRAP_LEN, NULL);
549 if (result != USB_STOR_XFER_GOOD)
550 return USB_STOR_TRANSPORT_ERROR;
552 /* check bulk status */
553 residue = le32_to_cpu(bcs->Residue);
555 /* try to compute the actual residue, based on how much data
556 * was really transferred and what the device tells us */
557 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
558 residue = min(residue, transfer_length);
560 scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
564 if (bcs->Status != US_BULK_STAT_OK)
565 return USB_STOR_TRANSPORT_ERROR;
567 return USB_STOR_TRANSPORT_GOOD;
570 static int sd_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
572 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
574 if (info->SD_Status.Insert && info->SD_Status.Ready)
575 return USB_STOR_TRANSPORT_GOOD;
578 return USB_STOR_TRANSPORT_GOOD;
581 return USB_STOR_TRANSPORT_GOOD;
584 static int sd_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
586 unsigned char data_ptr[36] = {
587 0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
588 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
589 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
590 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30 };
592 usb_stor_set_xfer_buf(data_ptr, 36, srb);
593 return USB_STOR_TRANSPORT_GOOD;
596 static int sd_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
598 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
599 unsigned char mediaNoWP[12] = {
600 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
601 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
602 unsigned char mediaWP[12] = {
603 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
604 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
606 if (info->SD_Status.WtP)
607 usb_stor_set_xfer_buf(mediaWP, 12, srb);
609 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
612 return USB_STOR_TRANSPORT_GOOD;
615 static int sd_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
619 unsigned int offset = 0;
620 unsigned char buf[8];
621 struct scatterlist *sg = NULL;
622 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
624 usb_stor_dbg(us, "sd_scsi_read_capacity\n");
625 if (info->SD_Status.HiCapacity) {
627 if (info->SD_Status.IsMMC)
628 bl_num = info->HC_C_SIZE-1;
630 bl_num = (info->HC_C_SIZE + 1) * 1024 - 1;
632 bl_len = 1 << (info->SD_READ_BL_LEN);
633 bl_num = info->SD_Block_Mult * (info->SD_C_SIZE + 1)
634 * (1 << (info->SD_C_SIZE_MULT + 2)) - 1;
636 info->bl_num = bl_num;
637 usb_stor_dbg(us, "bl_len = %x\n", bl_len);
638 usb_stor_dbg(us, "bl_num = %x\n", bl_num);
640 /*srb->request_bufflen = 8; */
641 buf[0] = (bl_num >> 24) & 0xff;
642 buf[1] = (bl_num >> 16) & 0xff;
643 buf[2] = (bl_num >> 8) & 0xff;
644 buf[3] = (bl_num >> 0) & 0xff;
645 buf[4] = (bl_len >> 24) & 0xff;
646 buf[5] = (bl_len >> 16) & 0xff;
647 buf[6] = (bl_len >> 8) & 0xff;
648 buf[7] = (bl_len >> 0) & 0xff;
650 usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
652 return USB_STOR_TRANSPORT_GOOD;
655 static int sd_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
658 unsigned char *cdb = srb->cmnd;
659 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
660 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
662 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
663 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
664 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
665 u32 bnByte = bn * 0x200;
666 u32 blenByte = blen * 0x200;
668 if (bn > info->bl_num)
669 return USB_STOR_TRANSPORT_ERROR;
671 result = ene_load_bincode(us, SD_RW_PATTERN);
672 if (result != USB_STOR_XFER_GOOD) {
673 usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
674 return USB_STOR_TRANSPORT_ERROR;
677 if (info->SD_Status.HiCapacity)
680 /* set up the command wrapper */
681 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
682 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
683 bcb->DataTransferLength = blenByte;
684 bcb->Flags = US_BULK_FLAG_IN;
686 bcb->CDB[5] = (unsigned char)(bnByte);
687 bcb->CDB[4] = (unsigned char)(bnByte>>8);
688 bcb->CDB[3] = (unsigned char)(bnByte>>16);
689 bcb->CDB[2] = (unsigned char)(bnByte>>24);
691 result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
695 static int sd_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
698 unsigned char *cdb = srb->cmnd;
699 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
700 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
702 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
703 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
704 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
705 u32 bnByte = bn * 0x200;
706 u32 blenByte = blen * 0x200;
708 if (bn > info->bl_num)
709 return USB_STOR_TRANSPORT_ERROR;
711 result = ene_load_bincode(us, SD_RW_PATTERN);
712 if (result != USB_STOR_XFER_GOOD) {
713 usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
714 return USB_STOR_TRANSPORT_ERROR;
717 if (info->SD_Status.HiCapacity)
720 /* set up the command wrapper */
721 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
722 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
723 bcb->DataTransferLength = blenByte;
726 bcb->CDB[5] = (unsigned char)(bnByte);
727 bcb->CDB[4] = (unsigned char)(bnByte>>8);
728 bcb->CDB[3] = (unsigned char)(bnByte>>16);
729 bcb->CDB[2] = (unsigned char)(bnByte>>24);
731 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
739 static int ms_lib_set_logicalpair(struct us_data *us, u16 logblk, u16 phyblk)
741 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
743 if ((logblk >= info->MS_Lib.NumberOfLogBlock) || (phyblk >= info->MS_Lib.NumberOfPhyBlock))
746 info->MS_Lib.Phy2LogMap[phyblk] = logblk;
747 info->MS_Lib.Log2PhyMap[logblk] = phyblk;
752 static int ms_lib_set_logicalblockmark(struct us_data *us, u16 phyblk, u16 mark)
754 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
756 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
759 info->MS_Lib.Phy2LogMap[phyblk] = mark;
764 static int ms_lib_set_initialerrorblock(struct us_data *us, u16 phyblk)
766 return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_INITIAL_ERROR);
769 static int ms_lib_set_bootblockmark(struct us_data *us, u16 phyblk)
771 return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_BOOT_BLOCK);
774 static int ms_lib_free_logicalmap(struct us_data *us)
776 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
778 kfree(info->MS_Lib.Phy2LogMap);
779 info->MS_Lib.Phy2LogMap = NULL;
781 kfree(info->MS_Lib.Log2PhyMap);
782 info->MS_Lib.Log2PhyMap = NULL;
787 static int ms_lib_alloc_logicalmap(struct us_data *us)
790 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
792 info->MS_Lib.Phy2LogMap = kmalloc(info->MS_Lib.NumberOfPhyBlock * sizeof(u16), GFP_KERNEL);
793 info->MS_Lib.Log2PhyMap = kmalloc(info->MS_Lib.NumberOfLogBlock * sizeof(u16), GFP_KERNEL);
795 if ((info->MS_Lib.Phy2LogMap == NULL) || (info->MS_Lib.Log2PhyMap == NULL)) {
796 ms_lib_free_logicalmap(us);
800 for (i = 0; i < info->MS_Lib.NumberOfPhyBlock; i++)
801 info->MS_Lib.Phy2LogMap[i] = MS_LB_NOT_USED;
803 for (i = 0; i < info->MS_Lib.NumberOfLogBlock; i++)
804 info->MS_Lib.Log2PhyMap[i] = MS_LB_NOT_USED;
809 static void ms_lib_clear_writebuf(struct us_data *us)
812 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
814 info->MS_Lib.wrtblk = (u16)-1;
815 ms_lib_clear_pagemap(info);
817 if (info->MS_Lib.blkpag)
818 memset(info->MS_Lib.blkpag, 0xff, info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector);
820 if (info->MS_Lib.blkext) {
821 for (i = 0; i < info->MS_Lib.PagesPerBlock; i++) {
822 info->MS_Lib.blkext[i].status1 = MS_REG_ST1_DEFAULT;
823 info->MS_Lib.blkext[i].ovrflg = MS_REG_OVR_DEFAULT;
824 info->MS_Lib.blkext[i].mngflg = MS_REG_MNG_DEFAULT;
825 info->MS_Lib.blkext[i].logadr = MS_LB_NOT_USED;
830 static int ms_count_freeblock(struct us_data *us, u16 PhyBlock)
833 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
835 Ende = PhyBlock + MS_PHYSICAL_BLOCKS_PER_SEGMENT;
836 for (Count = 0; PhyBlock < Ende; PhyBlock++) {
837 switch (info->MS_Lib.Phy2LogMap[PhyBlock]) {
839 case MS_LB_NOT_USED_ERASED:
849 static int ms_read_readpage(struct us_data *us, u32 PhyBlockAddr,
850 u8 PageNum, u32 *PageBuf, struct ms_lib_type_extdat *ExtraDat)
852 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
855 u32 bn = PhyBlockAddr * 0x20 + PageNum;
857 /* printk(KERN_INFO "MS --- MS_ReaderReadPage,
858 PhyBlockAddr = %x, PageNum = %x\n", PhyBlockAddr, PageNum); */
860 result = ene_load_bincode(us, MS_RW_PATTERN);
861 if (result != USB_STOR_XFER_GOOD)
862 return USB_STOR_TRANSPORT_ERROR;
865 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
866 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
867 bcb->DataTransferLength = 0x200;
868 bcb->Flags = US_BULK_FLAG_IN;
871 bcb->CDB[1] = 0x02; /* in init.c ENE_MSInit() is 0x01 */
873 bcb->CDB[5] = (unsigned char)(bn);
874 bcb->CDB[4] = (unsigned char)(bn>>8);
875 bcb->CDB[3] = (unsigned char)(bn>>16);
876 bcb->CDB[2] = (unsigned char)(bn>>24);
878 result = ene_send_scsi_cmd(us, FDIR_READ, PageBuf, 0);
879 if (result != USB_STOR_XFER_GOOD)
880 return USB_STOR_TRANSPORT_ERROR;
883 /* Read Extra Data */
884 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
885 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
886 bcb->DataTransferLength = 0x4;
887 bcb->Flags = US_BULK_FLAG_IN;
891 bcb->CDB[5] = (unsigned char)(PageNum);
892 bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
893 bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
894 bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
897 result = ene_send_scsi_cmd(us, FDIR_READ, &ExtBuf, 0);
898 if (result != USB_STOR_XFER_GOOD)
899 return USB_STOR_TRANSPORT_ERROR;
901 ExtraDat->reserved = 0;
902 ExtraDat->intr = 0x80; /* Not yet,fireware support */
903 ExtraDat->status0 = 0x10; /* Not yet,fireware support */
905 ExtraDat->status1 = 0x00; /* Not yet,fireware support */
906 ExtraDat->ovrflg = ExtBuf[0];
907 ExtraDat->mngflg = ExtBuf[1];
908 ExtraDat->logadr = memstick_logaddr(ExtBuf[2], ExtBuf[3]);
910 return USB_STOR_TRANSPORT_GOOD;
913 static int ms_lib_process_bootblock(struct us_data *us, u16 PhyBlock, u8 *PageData)
915 struct ms_bootblock_sysent *SysEntry;
916 struct ms_bootblock_sysinf *SysInfo;
920 struct ms_lib_type_extdat ExtraData;
921 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
923 PageBuffer = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
924 if (PageBuffer == NULL)
929 SysInfo = &(((struct ms_bootblock_page0 *)PageData)->sysinf);
931 if ((SysInfo->bMsClass != MS_SYSINF_MSCLASS_TYPE_1) ||
932 (be16_to_cpu(SysInfo->wPageSize) != MS_SYSINF_PAGE_SIZE) ||
933 ((SysInfo->bSecuritySupport & MS_SYSINF_SECURITY) == MS_SYSINF_SECURITY_SUPPORT) ||
934 (SysInfo->bReserved1 != MS_SYSINF_RESERVED1) ||
935 (SysInfo->bReserved2 != MS_SYSINF_RESERVED2) ||
936 (SysInfo->bFormatType != MS_SYSINF_FORMAT_FAT) ||
937 (SysInfo->bUsage != MS_SYSINF_USAGE_GENERAL))
940 switch (info->MS_Lib.cardType = SysInfo->bCardType) {
941 case MS_SYSINF_CARDTYPE_RDONLY:
942 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY);
944 case MS_SYSINF_CARDTYPE_RDWR:
945 ms_lib_ctrl_reset(info, MS_LIB_CTRL_RDONLY);
947 case MS_SYSINF_CARDTYPE_HYBRID:
952 info->MS_Lib.blockSize = be16_to_cpu(SysInfo->wBlockSize);
953 info->MS_Lib.NumberOfPhyBlock = be16_to_cpu(SysInfo->wBlockNumber);
954 info->MS_Lib.NumberOfLogBlock = be16_to_cpu(SysInfo->wTotalBlockNumber)-2;
955 info->MS_Lib.PagesPerBlock = info->MS_Lib.blockSize * SIZE_OF_KIRO / MS_BYTES_PER_PAGE;
956 info->MS_Lib.NumberOfSegment = info->MS_Lib.NumberOfPhyBlock / MS_PHYSICAL_BLOCKS_PER_SEGMENT;
957 info->MS_Model = be16_to_cpu(SysInfo->wMemorySize);
959 /*Allocate to all number of logicalblock and physicalblock */
960 if (ms_lib_alloc_logicalmap(us))
963 /* Mark the book block */
964 ms_lib_set_bootblockmark(us, PhyBlock);
966 SysEntry = &(((struct ms_bootblock_page0 *)PageData)->sysent);
968 for (i = 0; i < MS_NUMBER_OF_SYSTEM_ENTRY; i++) {
969 u32 EntryOffset, EntrySize;
971 EntryOffset = be32_to_cpu(SysEntry->entry[i].dwStart);
973 if (EntryOffset == 0xffffff)
975 EntrySize = be32_to_cpu(SysEntry->entry[i].dwSize);
980 if (EntryOffset + MS_BYTES_PER_PAGE + EntrySize > info->MS_Lib.blockSize * (u32)SIZE_OF_KIRO)
984 u8 PrevPageNumber = 0;
987 if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_INVALID_BLOCK)
990 while (EntrySize > 0) {
992 PageNumber = (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1);
993 if (PageNumber != PrevPageNumber) {
994 switch (ms_read_readpage(us, PhyBlock, PageNumber, (u32 *)PageBuffer, &ExtraData)) {
995 case MS_STATUS_SUCCESS:
997 case MS_STATUS_WRITE_PROTECT:
998 case MS_ERROR_FLASH_READ:
999 case MS_STATUS_ERROR:
1004 PrevPageNumber = PageNumber;
1007 phyblk = be16_to_cpu(*(u16 *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)));
1008 if (phyblk < 0x0fff)
1009 ms_lib_set_initialerrorblock(us, phyblk);
1014 } else if (i == 1) { /* CIS/IDI */
1015 struct ms_bootblock_idi *idi;
1017 if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_CIS_IDI)
1020 switch (ms_read_readpage(us, PhyBlock, (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1), (u32 *)PageBuffer, &ExtraData)) {
1021 case MS_STATUS_SUCCESS:
1023 case MS_STATUS_WRITE_PROTECT:
1024 case MS_ERROR_FLASH_READ:
1025 case MS_STATUS_ERROR:
1030 idi = &((struct ms_bootblock_cis_idi *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)))->idi.idi;
1031 if (le16_to_cpu(idi->wIDIgeneralConfiguration) != MS_IDI_GENERAL_CONF)
1034 info->MS_Lib.BytesPerSector = le16_to_cpu(idi->wIDIbytesPerSector);
1035 if (info->MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE)
1044 ms_lib_free_logicalmap(us);
1052 static void ms_lib_free_writebuf(struct us_data *us)
1054 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1055 info->MS_Lib.wrtblk = (u16)-1; /* set to -1 */
1057 /* memset((fdoExt)->MS_Lib.pagemap, 0, sizeof((fdoExt)->MS_Lib.pagemap)) */
1059 ms_lib_clear_pagemap(info); /* (pdx)->MS_Lib.pagemap memset 0 in ms.h */
1061 if (info->MS_Lib.blkpag) {
1062 kfree((u8 *)(info->MS_Lib.blkpag)); /* Arnold test ... */
1063 info->MS_Lib.blkpag = NULL;
1066 if (info->MS_Lib.blkext) {
1067 kfree((u8 *)(info->MS_Lib.blkext)); /* Arnold test ... */
1068 info->MS_Lib.blkext = NULL;
1073 static void ms_lib_free_allocatedarea(struct us_data *us)
1075 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1077 ms_lib_free_writebuf(us); /* Free MS_Lib.pagemap */
1078 ms_lib_free_logicalmap(us); /* kfree MS_Lib.Phy2LogMap and MS_Lib.Log2PhyMap */
1080 /* set struct us point flag to 0 */
1081 info->MS_Lib.flags = 0;
1082 info->MS_Lib.BytesPerSector = 0;
1083 info->MS_Lib.SectorsPerCylinder = 0;
1085 info->MS_Lib.cardType = 0;
1086 info->MS_Lib.blockSize = 0;
1087 info->MS_Lib.PagesPerBlock = 0;
1089 info->MS_Lib.NumberOfPhyBlock = 0;
1090 info->MS_Lib.NumberOfLogBlock = 0;
1094 static int ms_lib_alloc_writebuf(struct us_data *us)
1096 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1098 info->MS_Lib.wrtblk = (u16)-1;
1100 info->MS_Lib.blkpag = kmalloc(info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector, GFP_KERNEL);
1101 info->MS_Lib.blkext = kmalloc(info->MS_Lib.PagesPerBlock * sizeof(struct ms_lib_type_extdat), GFP_KERNEL);
1103 if ((info->MS_Lib.blkpag == NULL) || (info->MS_Lib.blkext == NULL)) {
1104 ms_lib_free_writebuf(us);
1108 ms_lib_clear_writebuf(us);
1113 static int ms_lib_force_setlogical_pair(struct us_data *us, u16 logblk, u16 phyblk)
1115 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1117 if (logblk == MS_LB_NOT_USED)
1120 if ((logblk >= info->MS_Lib.NumberOfLogBlock) ||
1121 (phyblk >= info->MS_Lib.NumberOfPhyBlock))
1124 info->MS_Lib.Phy2LogMap[phyblk] = logblk;
1125 info->MS_Lib.Log2PhyMap[logblk] = phyblk;
1130 static int ms_read_copyblock(struct us_data *us, u16 oldphy, u16 newphy,
1131 u16 PhyBlockAddr, u8 PageNum, unsigned char *buf, u16 len)
1133 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1136 /* printk(KERN_INFO "MS_ReaderCopyBlock --- PhyBlockAddr = %x,
1137 PageNum = %x\n", PhyBlockAddr, PageNum); */
1138 result = ene_load_bincode(us, MS_RW_PATTERN);
1139 if (result != USB_STOR_XFER_GOOD)
1140 return USB_STOR_TRANSPORT_ERROR;
1142 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1143 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1144 bcb->DataTransferLength = 0x200*len;
1148 bcb->CDB[4] = (unsigned char)(oldphy);
1149 bcb->CDB[3] = (unsigned char)(oldphy>>8);
1150 bcb->CDB[2] = 0; /* (BYTE)(oldphy>>16) */
1151 bcb->CDB[7] = (unsigned char)(newphy);
1152 bcb->CDB[6] = (unsigned char)(newphy>>8);
1153 bcb->CDB[5] = 0; /* (BYTE)(newphy>>16) */
1154 bcb->CDB[9] = (unsigned char)(PhyBlockAddr);
1155 bcb->CDB[8] = (unsigned char)(PhyBlockAddr>>8);
1156 bcb->CDB[10] = PageNum;
1158 result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1159 if (result != USB_STOR_XFER_GOOD)
1160 return USB_STOR_TRANSPORT_ERROR;
1162 return USB_STOR_TRANSPORT_GOOD;
1165 static int ms_read_eraseblock(struct us_data *us, u32 PhyBlockAddr)
1167 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1169 u32 bn = PhyBlockAddr;
1171 /* printk(KERN_INFO "MS --- ms_read_eraseblock,
1172 PhyBlockAddr = %x\n", PhyBlockAddr); */
1173 result = ene_load_bincode(us, MS_RW_PATTERN);
1174 if (result != USB_STOR_XFER_GOOD)
1175 return USB_STOR_TRANSPORT_ERROR;
1177 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1178 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1179 bcb->DataTransferLength = 0x200;
1180 bcb->Flags = US_BULK_FLAG_IN;
1183 bcb->CDB[4] = (unsigned char)(bn);
1184 bcb->CDB[3] = (unsigned char)(bn>>8);
1185 bcb->CDB[2] = (unsigned char)(bn>>16);
1187 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1188 if (result != USB_STOR_XFER_GOOD)
1189 return USB_STOR_TRANSPORT_ERROR;
1191 return USB_STOR_TRANSPORT_GOOD;
1194 static int ms_lib_check_disableblock(struct us_data *us, u16 PhyBlock)
1196 unsigned char *PageBuf = NULL;
1197 u16 result = MS_STATUS_SUCCESS;
1199 struct ms_lib_type_extdat extdat;
1200 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1202 PageBuf = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1203 if (PageBuf == NULL) {
1204 result = MS_NO_MEMORY_ERROR;
1208 ms_read_readpage(us, PhyBlock, 1, (u32 *)PageBuf, &extdat);
1210 blk = be16_to_cpu(PageBuf[index]);
1211 if (blk == MS_LB_NOT_USED)
1213 if (blk == info->MS_Lib.Log2PhyMap[0]) {
1214 result = MS_ERROR_FLASH_READ;
1225 static int ms_lib_setacquired_errorblock(struct us_data *us, u16 phyblk)
1228 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1230 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1233 log = info->MS_Lib.Phy2LogMap[phyblk];
1235 if (log < info->MS_Lib.NumberOfLogBlock)
1236 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1238 if (info->MS_Lib.Phy2LogMap[phyblk] != MS_LB_INITIAL_ERROR)
1239 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_ACQUIRED_ERROR;
1244 static int ms_lib_overwrite_extra(struct us_data *us, u32 PhyBlockAddr,
1245 u8 PageNum, u8 OverwriteFlag)
1247 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1250 /* printk("MS --- MS_LibOverwriteExtra,
1251 PhyBlockAddr = %x, PageNum = %x\n", PhyBlockAddr, PageNum); */
1252 result = ene_load_bincode(us, MS_RW_PATTERN);
1253 if (result != USB_STOR_XFER_GOOD)
1254 return USB_STOR_TRANSPORT_ERROR;
1256 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1257 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1258 bcb->DataTransferLength = 0x4;
1259 bcb->Flags = US_BULK_FLAG_IN;
1262 bcb->CDB[5] = (unsigned char)(PageNum);
1263 bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
1264 bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
1265 bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
1266 bcb->CDB[6] = OverwriteFlag;
1271 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1272 if (result != USB_STOR_XFER_GOOD)
1273 return USB_STOR_TRANSPORT_ERROR;
1275 return USB_STOR_TRANSPORT_GOOD;
1278 static int ms_lib_error_phyblock(struct us_data *us, u16 phyblk)
1280 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1282 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1283 return MS_STATUS_ERROR;
1285 ms_lib_setacquired_errorblock(us, phyblk);
1287 if (ms_lib_iswritable(info))
1288 return ms_lib_overwrite_extra(us, phyblk, 0, (u8)(~MS_REG_OVR_BKST & BYTE_MASK));
1290 return MS_STATUS_SUCCESS;
1293 static int ms_lib_erase_phyblock(struct us_data *us, u16 phyblk)
1296 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1298 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1299 return MS_STATUS_ERROR;
1301 log = info->MS_Lib.Phy2LogMap[phyblk];
1303 if (log < info->MS_Lib.NumberOfLogBlock)
1304 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1306 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED;
1308 if (ms_lib_iswritable(info)) {
1309 switch (ms_read_eraseblock(us, phyblk)) {
1310 case MS_STATUS_SUCCESS:
1311 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED_ERASED;
1312 return MS_STATUS_SUCCESS;
1313 case MS_ERROR_FLASH_ERASE:
1314 case MS_STATUS_INT_ERROR:
1315 ms_lib_error_phyblock(us, phyblk);
1316 return MS_ERROR_FLASH_ERASE;
1317 case MS_STATUS_ERROR:
1319 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY); /* MS_LibCtrlSet will used by ENE_MSInit ,need check, and why us to info*/
1320 ms_lib_setacquired_errorblock(us, phyblk);
1321 return MS_STATUS_ERROR;
1325 ms_lib_setacquired_errorblock(us, phyblk);
1327 return MS_STATUS_SUCCESS;
1330 static int ms_lib_read_extra(struct us_data *us, u32 PhyBlock,
1331 u8 PageNum, struct ms_lib_type_extdat *ExtraDat)
1333 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1337 /* printk("MS_LibReadExtra --- PhyBlock = %x, PageNum = %x\n", PhyBlock, PageNum); */
1338 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1339 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1340 bcb->DataTransferLength = 0x4;
1341 bcb->Flags = US_BULK_FLAG_IN;
1344 bcb->CDB[5] = (unsigned char)(PageNum);
1345 bcb->CDB[4] = (unsigned char)(PhyBlock);
1346 bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
1347 bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
1350 result = ene_send_scsi_cmd(us, FDIR_READ, &ExtBuf, 0);
1351 if (result != USB_STOR_XFER_GOOD)
1352 return USB_STOR_TRANSPORT_ERROR;
1354 ExtraDat->reserved = 0;
1355 ExtraDat->intr = 0x80; /* Not yet, waiting for fireware support */
1356 ExtraDat->status0 = 0x10; /* Not yet, waiting for fireware support */
1357 ExtraDat->status1 = 0x00; /* Not yet, waiting for fireware support */
1358 ExtraDat->ovrflg = ExtBuf[0];
1359 ExtraDat->mngflg = ExtBuf[1];
1360 ExtraDat->logadr = memstick_logaddr(ExtBuf[2], ExtBuf[3]);
1362 return USB_STOR_TRANSPORT_GOOD;
1365 static int ms_libsearch_block_from_physical(struct us_data *us, u16 phyblk)
1369 struct ms_lib_type_extdat extdat; /* need check */
1370 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1373 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1376 for (blk = phyblk + 1; blk != phyblk; blk++) {
1377 if ((blk & MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK) == 0)
1378 blk -= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1380 Newblk = info->MS_Lib.Phy2LogMap[blk];
1381 if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED_ERASED) {
1383 } else if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED) {
1384 switch (ms_lib_read_extra(us, blk, 0, &extdat)) {
1385 case MS_STATUS_SUCCESS:
1386 case MS_STATUS_SUCCESS_WITH_ECC:
1388 case MS_NOCARD_ERROR:
1389 return MS_NOCARD_ERROR;
1390 case MS_STATUS_INT_ERROR:
1392 case MS_ERROR_FLASH_READ:
1394 ms_lib_setacquired_errorblock(us, blk);
1398 if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1399 ms_lib_setacquired_errorblock(us, blk);
1403 switch (ms_lib_erase_phyblock(us, blk)) {
1404 case MS_STATUS_SUCCESS:
1406 case MS_STATUS_ERROR:
1408 case MS_ERROR_FLASH_ERASE:
1410 ms_lib_error_phyblock(us, blk);
1418 static int ms_libsearch_block_from_logical(struct us_data *us, u16 logblk)
1421 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1423 phyblk = ms_libconv_to_physical(info, logblk);
1424 if (phyblk >= MS_LB_ERROR) {
1425 if (logblk >= info->MS_Lib.NumberOfLogBlock)
1428 phyblk = (logblk + MS_NUMBER_OF_BOOT_BLOCK) / MS_LOGICAL_BLOCKS_PER_SEGMENT;
1429 phyblk *= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1430 phyblk += MS_PHYSICAL_BLOCKS_PER_SEGMENT - 1;
1433 return ms_libsearch_block_from_physical(us, phyblk);
1436 static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
1438 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
1440 /* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
1441 if (info->MS_Status.Insert && info->MS_Status.Ready) {
1442 return USB_STOR_TRANSPORT_GOOD;
1445 return USB_STOR_TRANSPORT_GOOD;
1448 return USB_STOR_TRANSPORT_GOOD;
1451 static int ms_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
1453 /* pr_info("MS_SCSI_Inquiry\n"); */
1454 unsigned char data_ptr[36] = {
1455 0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
1456 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
1457 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
1458 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30};
1460 usb_stor_set_xfer_buf(data_ptr, 36, srb);
1461 return USB_STOR_TRANSPORT_GOOD;
1464 static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
1466 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1467 unsigned char mediaNoWP[12] = {
1468 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
1469 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1470 unsigned char mediaWP[12] = {
1471 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
1472 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1474 if (info->MS_Status.WtP)
1475 usb_stor_set_xfer_buf(mediaWP, 12, srb);
1477 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
1479 return USB_STOR_TRANSPORT_GOOD;
1482 static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
1486 unsigned int offset = 0;
1487 unsigned char buf[8];
1488 struct scatterlist *sg = NULL;
1489 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1491 usb_stor_dbg(us, "ms_scsi_read_capacity\n");
1493 if (info->MS_Status.IsMSPro)
1494 bl_num = info->MSP_TotalBlock - 1;
1496 bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1;
1498 info->bl_num = bl_num;
1499 usb_stor_dbg(us, "bl_len = %x\n", bl_len);
1500 usb_stor_dbg(us, "bl_num = %x\n", bl_num);
1502 /*srb->request_bufflen = 8; */
1503 buf[0] = (bl_num >> 24) & 0xff;
1504 buf[1] = (bl_num >> 16) & 0xff;
1505 buf[2] = (bl_num >> 8) & 0xff;
1506 buf[3] = (bl_num >> 0) & 0xff;
1507 buf[4] = (bl_len >> 24) & 0xff;
1508 buf[5] = (bl_len >> 16) & 0xff;
1509 buf[6] = (bl_len >> 8) & 0xff;
1510 buf[7] = (bl_len >> 0) & 0xff;
1512 usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
1514 return USB_STOR_TRANSPORT_GOOD;
1517 static void ms_lib_phy_to_log_range(u16 PhyBlock, u16 *LogStart, u16 *LogEnde)
1519 PhyBlock /= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1522 *LogStart = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT + (PhyBlock - 1) * MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1523 *LogEnde = *LogStart + MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1526 *LogEnde = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT;/*494*/
1530 static int ms_lib_read_extrablock(struct us_data *us, u32 PhyBlock,
1531 u8 PageNum, u8 blen, void *buf)
1533 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1536 /* printk("MS_LibReadExtraBlock --- PhyBlock = %x,
1537 PageNum = %x, blen = %x\n", PhyBlock, PageNum, blen); */
1539 /* Read Extra Data */
1540 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1541 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1542 bcb->DataTransferLength = 0x4 * blen;
1543 bcb->Flags = US_BULK_FLAG_IN;
1546 bcb->CDB[5] = (unsigned char)(PageNum);
1547 bcb->CDB[4] = (unsigned char)(PhyBlock);
1548 bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
1549 bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
1552 result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1553 if (result != USB_STOR_XFER_GOOD)
1554 return USB_STOR_TRANSPORT_ERROR;
1556 return USB_STOR_TRANSPORT_GOOD;
1559 static int ms_lib_scan_logicalblocknumber(struct us_data *us, u16 btBlk1st)
1561 u16 PhyBlock, newblk, i;
1562 u16 LogStart, LogEnde;
1563 struct ms_lib_type_extdat extdat;
1565 u32 count = 0, index = 0;
1566 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1568 for (PhyBlock = 0; PhyBlock < info->MS_Lib.NumberOfPhyBlock;) {
1569 ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde);
1571 for (i = 0; i < MS_PHYSICAL_BLOCKS_PER_SEGMENT; i++, PhyBlock++) {
1572 switch (ms_libconv_to_logical(info, PhyBlock)) {
1573 case MS_STATUS_ERROR:
1579 if (count == PhyBlock) {
1580 ms_lib_read_extrablock(us, PhyBlock, 0, 0x80, &buf);
1583 index = (PhyBlock % 0x80) * 4;
1585 extdat.ovrflg = buf[index];
1586 extdat.mngflg = buf[index+1];
1587 extdat.logadr = memstick_logaddr(buf[index+2], buf[index+3]);
1589 if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1590 ms_lib_setacquired_errorblock(us, PhyBlock);
1594 if ((extdat.mngflg & MS_REG_MNG_ATFLG) == MS_REG_MNG_ATFLG_ATTBL) {
1595 ms_lib_erase_phyblock(us, PhyBlock);
1599 if (extdat.logadr != MS_LB_NOT_USED) {
1600 if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) {
1601 ms_lib_erase_phyblock(us, PhyBlock);
1605 newblk = ms_libconv_to_physical(info, extdat.logadr);
1607 if (newblk != MS_LB_NOT_USED) {
1608 if (extdat.logadr == 0) {
1609 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1610 if (ms_lib_check_disableblock(us, btBlk1st)) {
1611 ms_lib_set_logicalpair(us, extdat.logadr, newblk);
1616 ms_lib_read_extra(us, newblk, 0, &extdat);
1617 if ((extdat.ovrflg & MS_REG_OVR_UDST) == MS_REG_OVR_UDST_UPDATING) {
1618 ms_lib_erase_phyblock(us, PhyBlock);
1621 ms_lib_erase_phyblock(us, newblk);
1625 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1630 return MS_STATUS_SUCCESS;
1634 static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
1637 unsigned char *cdb = srb->cmnd;
1638 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1639 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1641 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
1642 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
1643 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1644 u32 blenByte = blen * 0x200;
1646 if (bn > info->bl_num)
1647 return USB_STOR_TRANSPORT_ERROR;
1649 if (info->MS_Status.IsMSPro) {
1650 result = ene_load_bincode(us, MSP_RW_PATTERN);
1651 if (result != USB_STOR_XFER_GOOD) {
1652 usb_stor_dbg(us, "Load MPS RW pattern Fail !!\n");
1653 return USB_STOR_TRANSPORT_ERROR;
1656 /* set up the command wrapper */
1657 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1658 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1659 bcb->DataTransferLength = blenByte;
1660 bcb->Flags = US_BULK_FLAG_IN;
1663 bcb->CDB[5] = (unsigned char)(bn);
1664 bcb->CDB[4] = (unsigned char)(bn>>8);
1665 bcb->CDB[3] = (unsigned char)(bn>>16);
1666 bcb->CDB[2] = (unsigned char)(bn>>24);
1668 result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
1677 buf = kmalloc(blenByte, GFP_KERNEL);
1679 return USB_STOR_TRANSPORT_ERROR;
1681 result = ene_load_bincode(us, MS_RW_PATTERN);
1682 if (result != USB_STOR_XFER_GOOD) {
1683 pr_info("Load MS RW pattern Fail !!\n");
1684 result = USB_STOR_TRANSPORT_ERROR;
1688 logblk = (u16)(bn / info->MS_Lib.PagesPerBlock);
1689 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1692 if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1693 len = info->MS_Lib.PagesPerBlock-PageNum;
1697 phyblk = ms_libconv_to_physical(info, logblk);
1698 blkno = phyblk * 0x20 + PageNum;
1700 /* set up the command wrapper */
1701 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1702 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1703 bcb->DataTransferLength = 0x200 * len;
1704 bcb->Flags = US_BULK_FLAG_IN;
1707 bcb->CDB[5] = (unsigned char)(blkno);
1708 bcb->CDB[4] = (unsigned char)(blkno>>8);
1709 bcb->CDB[3] = (unsigned char)(blkno>>16);
1710 bcb->CDB[2] = (unsigned char)(blkno>>24);
1712 result = ene_send_scsi_cmd(us, FDIR_READ, buf+offset, 0);
1713 if (result != USB_STOR_XFER_GOOD) {
1714 pr_info("MS_SCSI_Read --- result = %x\n", result);
1715 result = USB_STOR_TRANSPORT_ERROR;
1724 offset += MS_BYTES_PER_PAGE*len;
1726 usb_stor_set_xfer_buf(buf, blenByte, srb);
1733 static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
1736 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1737 unsigned char *cdb = srb->cmnd;
1738 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1740 u32 bn = ((cdb[2] << 24) & 0xff000000) |
1741 ((cdb[3] << 16) & 0x00ff0000) |
1742 ((cdb[4] << 8) & 0x0000ff00) |
1743 ((cdb[5] << 0) & 0x000000ff);
1744 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1745 u32 blenByte = blen * 0x200;
1747 if (bn > info->bl_num)
1748 return USB_STOR_TRANSPORT_ERROR;
1750 if (info->MS_Status.IsMSPro) {
1751 result = ene_load_bincode(us, MSP_RW_PATTERN);
1752 if (result != USB_STOR_XFER_GOOD) {
1753 pr_info("Load MSP RW pattern Fail !!\n");
1754 return USB_STOR_TRANSPORT_ERROR;
1757 /* set up the command wrapper */
1758 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1759 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1760 bcb->DataTransferLength = blenByte;
1764 bcb->CDB[5] = (unsigned char)(bn);
1765 bcb->CDB[4] = (unsigned char)(bn>>8);
1766 bcb->CDB[3] = (unsigned char)(bn>>16);
1767 bcb->CDB[2] = (unsigned char)(bn>>24);
1769 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
1775 u16 len, oldphy, newphy;
1777 buf = kmalloc(blenByte, GFP_KERNEL);
1779 return USB_STOR_TRANSPORT_ERROR;
1780 usb_stor_set_xfer_buf(buf, blenByte, srb);
1782 result = ene_load_bincode(us, MS_RW_PATTERN);
1783 if (result != USB_STOR_XFER_GOOD) {
1784 pr_info("Load MS RW pattern Fail !!\n");
1785 result = USB_STOR_TRANSPORT_ERROR;
1789 PhyBlockAddr = (u16)(bn / info->MS_Lib.PagesPerBlock);
1790 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1793 if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1794 len = info->MS_Lib.PagesPerBlock-PageNum;
1798 oldphy = ms_libconv_to_physical(info, PhyBlockAddr); /* need check us <-> info */
1799 newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr);
1801 result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len);
1803 if (result != USB_STOR_XFER_GOOD) {
1804 pr_info("MS_SCSI_Write --- result = %x\n", result);
1805 result = USB_STOR_TRANSPORT_ERROR;
1809 info->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED;
1810 ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy);
1817 offset += MS_BYTES_PER_PAGE*len;
1829 static int ene_get_card_type(struct us_data *us, u16 index, void *buf)
1831 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1834 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1835 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1836 bcb->DataTransferLength = 0x01;
1837 bcb->Flags = US_BULK_FLAG_IN;
1839 bcb->CDB[2] = (unsigned char)(index>>8);
1840 bcb->CDB[3] = (unsigned char)index;
1842 result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1846 static int ene_get_card_status(struct us_data *us, u8 *buf)
1850 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1852 /*usb_stor_dbg(us, "transport --- ENE_ReadSDReg\n");*/
1853 reg4b = *(u32 *)&buf[0x18];
1854 info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f);
1856 tmpreg = (u16) reg4b;
1857 reg4b = *(u32 *)(&buf[0x14]);
1858 if (info->SD_Status.HiCapacity && !info->SD_Status.IsMMC)
1859 info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
1861 info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
1862 info->SD_C_SIZE_MULT = (u8)(reg4b >> 7) & 0x07;
1863 if (info->SD_Status.HiCapacity && info->SD_Status.IsMMC)
1864 info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
1866 if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
1867 info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN);
1868 info->SD_READ_BL_LEN = SD_BLOCK_LEN;
1870 info->SD_Block_Mult = 1;
1873 return USB_STOR_TRANSPORT_GOOD;
1876 static int ene_load_bincode(struct us_data *us, unsigned char flag)
1879 char *fw_name = NULL;
1880 unsigned char *buf = NULL;
1881 const struct firmware *sd_fw = NULL;
1882 int result = USB_STOR_TRANSPORT_ERROR;
1883 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1884 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1886 if (info->BIN_FLAG == flag)
1887 return USB_STOR_TRANSPORT_GOOD;
1891 case SD_INIT1_PATTERN:
1892 usb_stor_dbg(us, "SD_INIT1_PATTERN\n");
1893 fw_name = SD_INIT1_FIRMWARE;
1895 case SD_INIT2_PATTERN:
1896 usb_stor_dbg(us, "SD_INIT2_PATTERN\n");
1897 fw_name = SD_INIT2_FIRMWARE;
1900 usb_stor_dbg(us, "SD_RW_PATTERN\n");
1901 fw_name = SD_RW_FIRMWARE;
1904 case MS_INIT_PATTERN:
1905 usb_stor_dbg(us, "MS_INIT_PATTERN\n");
1906 fw_name = MS_INIT_FIRMWARE;
1908 case MSP_RW_PATTERN:
1909 usb_stor_dbg(us, "MSP_RW_PATTERN\n");
1910 fw_name = MSP_RW_FIRMWARE;
1913 usb_stor_dbg(us, "MS_RW_PATTERN\n");
1914 fw_name = MS_RW_FIRMWARE;
1917 usb_stor_dbg(us, "----------- Unknown PATTERN ----------\n");
1921 err = reject_firmware(&sd_fw, fw_name, &us->pusb_dev->dev);
1923 usb_stor_dbg(us, "load firmware %s failed\n", fw_name);
1926 buf = kmalloc(sd_fw->size, GFP_KERNEL);
1930 memcpy(buf, sd_fw->data, sd_fw->size);
1931 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1932 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1933 bcb->DataTransferLength = sd_fw->size;
1937 result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1938 info->BIN_FLAG = flag;
1942 release_firmware(sd_fw);
1946 static int ms_card_init(struct us_data *us)
1950 unsigned char *PageBuffer0 = NULL, *PageBuffer1 = NULL;
1951 struct ms_lib_type_extdat extdat;
1952 u16 btBlk1st, btBlk2nd;
1954 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1956 printk(KERN_INFO "MS_CardInit start\n");
1958 ms_lib_free_allocatedarea(us); /* Clean buffer and set struct us_data flag to 0 */
1960 /* get two PageBuffer */
1961 PageBuffer0 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1962 PageBuffer1 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1963 if ((PageBuffer0 == NULL) || (PageBuffer1 == NULL)) {
1964 result = MS_NO_MEMORY_ERROR;
1968 btBlk1st = btBlk2nd = MS_LB_NOT_USED;
1971 for (TmpBlock = 0; TmpBlock < MS_MAX_INITIAL_ERROR_BLOCKS+2; TmpBlock++) {
1973 switch (ms_read_readpage(us, TmpBlock, 0, (u32 *)PageBuffer0, &extdat)) {
1974 case MS_STATUS_SUCCESS:
1976 case MS_STATUS_INT_ERROR:
1978 case MS_STATUS_ERROR:
1983 if ((extdat.ovrflg & MS_REG_OVR_BKST) == MS_REG_OVR_BKST_NG)
1986 if (((extdat.mngflg & MS_REG_MNG_SYSFLG) == MS_REG_MNG_SYSFLG_USER) ||
1987 (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wBlockID) != MS_BOOT_BLOCK_ID) ||
1988 (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wFormatVersion) != MS_BOOT_BLOCK_FORMAT_VERSION) ||
1989 (((struct ms_bootblock_page0 *)PageBuffer0)->header.bNumberOfDataEntry != MS_BOOT_BLOCK_DATA_ENTRIES))
1992 if (btBlk1st != MS_LB_NOT_USED) {
1993 btBlk2nd = TmpBlock;
1997 btBlk1st = TmpBlock;
1998 memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE);
1999 if (extdat.status1 & (MS_REG_ST1_DTER | MS_REG_ST1_EXER | MS_REG_ST1_FGER))
2003 if (btBlk1st == MS_LB_NOT_USED) {
2004 result = MS_STATUS_ERROR;
2009 if ((extdat.status0 & MS_REG_ST0_WP) == MS_REG_ST0_WP_ON)
2010 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2012 result = MS_STATUS_ERROR;
2013 /* 1st Boot Block */
2014 if (btBlk1stErred == 0)
2015 result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1);
2017 /* 2nd Boot Block */
2018 if (result && (btBlk2nd != MS_LB_NOT_USED))
2019 result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0);
2022 result = MS_STATUS_ERROR;
2026 for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++)
2027 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2029 info->MS_Lib.Phy2LogMap[btBlk1st] = MS_LB_BOOT_BLOCK;
2031 if (btBlk2nd != MS_LB_NOT_USED) {
2032 for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++)
2033 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2035 info->MS_Lib.Phy2LogMap[btBlk2nd] = MS_LB_BOOT_BLOCK;
2038 result = ms_lib_scan_logicalblocknumber(us, btBlk1st);
2042 for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT;
2043 TmpBlock < info->MS_Lib.NumberOfPhyBlock;
2044 TmpBlock += MS_PHYSICAL_BLOCKS_PER_SEGMENT) {
2045 if (ms_count_freeblock(us, TmpBlock) == 0) {
2046 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2052 if (ms_lib_alloc_writebuf(us)) {
2053 result = MS_NO_MEMORY_ERROR;
2057 result = MS_STATUS_SUCCESS;
2063 printk(KERN_INFO "MS_CardInit end\n");
2067 static int ene_ms_init(struct us_data *us)
2069 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2072 u16 MSP_BlockSize, MSP_UserAreaBlocks;
2073 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2075 printk(KERN_INFO "transport --- ENE_MSInit\n");
2077 /* the same part to test ENE */
2079 result = ene_load_bincode(us, MS_INIT_PATTERN);
2080 if (result != USB_STOR_XFER_GOOD) {
2081 printk(KERN_ERR "Load MS Init Code Fail !!\n");
2082 return USB_STOR_TRANSPORT_ERROR;
2085 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2086 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2087 bcb->DataTransferLength = 0x200;
2088 bcb->Flags = US_BULK_FLAG_IN;
2092 result = ene_send_scsi_cmd(us, FDIR_READ, &buf, 0);
2093 if (result != USB_STOR_XFER_GOOD) {
2094 printk(KERN_ERR "Execution MS Init Code Fail !!\n");
2095 return USB_STOR_TRANSPORT_ERROR;
2097 /* the same part to test ENE */
2098 info->MS_Status = *(struct MS_STATUS *)&buf[0];
2100 if (info->MS_Status.Insert && info->MS_Status.Ready) {
2101 printk(KERN_INFO "Insert = %x\n", info->MS_Status.Insert);
2102 printk(KERN_INFO "Ready = %x\n", info->MS_Status.Ready);
2103 printk(KERN_INFO "IsMSPro = %x\n", info->MS_Status.IsMSPro);
2104 printk(KERN_INFO "IsMSPHG = %x\n", info->MS_Status.IsMSPHG);
2105 printk(KERN_INFO "WtP= %x\n", info->MS_Status.WtP);
2106 if (info->MS_Status.IsMSPro) {
2107 MSP_BlockSize = (buf[6] << 8) | buf[7];
2108 MSP_UserAreaBlocks = (buf[10] << 8) | buf[11];
2109 info->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
2111 ms_card_init(us); /* Card is MS (to ms.c)*/
2113 usb_stor_dbg(us, "MS Init Code OK !!\n");
2115 usb_stor_dbg(us, "MS Card Not Ready --- %x\n", buf[0]);
2116 return USB_STOR_TRANSPORT_ERROR;
2119 return USB_STOR_TRANSPORT_GOOD;
2122 static int ene_sd_init(struct us_data *us)
2126 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2127 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2129 usb_stor_dbg(us, "transport --- ENE_SDInit\n");
2130 /* SD Init Part-1 */
2131 result = ene_load_bincode(us, SD_INIT1_PATTERN);
2132 if (result != USB_STOR_XFER_GOOD) {
2133 usb_stor_dbg(us, "Load SD Init Code Part-1 Fail !!\n");
2134 return USB_STOR_TRANSPORT_ERROR;
2137 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2138 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2139 bcb->Flags = US_BULK_FLAG_IN;
2142 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
2143 if (result != USB_STOR_XFER_GOOD) {
2144 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2145 return USB_STOR_TRANSPORT_ERROR;
2148 /* SD Init Part-2 */
2149 result = ene_load_bincode(us, SD_INIT2_PATTERN);
2150 if (result != USB_STOR_XFER_GOOD) {
2151 usb_stor_dbg(us, "Load SD Init Code Part-2 Fail !!\n");
2152 return USB_STOR_TRANSPORT_ERROR;
2155 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2156 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2157 bcb->DataTransferLength = 0x200;
2158 bcb->Flags = US_BULK_FLAG_IN;
2161 result = ene_send_scsi_cmd(us, FDIR_READ, &buf, 0);
2162 if (result != USB_STOR_XFER_GOOD) {
2163 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2164 return USB_STOR_TRANSPORT_ERROR;
2167 info->SD_Status = *(struct SD_STATUS *)&buf[0];
2168 if (info->SD_Status.Insert && info->SD_Status.Ready) {
2169 struct SD_STATUS *s = &info->SD_Status;
2171 ene_get_card_status(us, (unsigned char *)&buf);
2172 usb_stor_dbg(us, "Insert = %x\n", s->Insert);
2173 usb_stor_dbg(us, "Ready = %x\n", s->Ready);
2174 usb_stor_dbg(us, "IsMMC = %x\n", s->IsMMC);
2175 usb_stor_dbg(us, "HiCapacity = %x\n", s->HiCapacity);
2176 usb_stor_dbg(us, "HiSpeed = %x\n", s->HiSpeed);
2177 usb_stor_dbg(us, "WtP = %x\n", s->WtP);
2179 usb_stor_dbg(us, "SD Card Not Ready --- %x\n", buf[0]);
2180 return USB_STOR_TRANSPORT_ERROR;
2182 return USB_STOR_TRANSPORT_GOOD;
2186 static int ene_init(struct us_data *us)
2190 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2192 result = ene_get_card_type(us, REG_CARD_STATUS, &misc_reg03);
2193 if (result != USB_STOR_XFER_GOOD)
2194 return USB_STOR_TRANSPORT_ERROR;
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 info->SrbStatus = SS_SUCCESS;
2220 switch (srb->cmnd[0]) {
2221 case TEST_UNIT_READY:
2222 result = sd_scsi_test_unit_ready(us, srb);
2225 result = sd_scsi_inquiry(us, srb);
2228 result = sd_scsi_mode_sense(us, srb);
2232 result = SD_SCSI_Start_Stop(us, srb);
2236 result = sd_scsi_read_capacity(us, srb);
2239 result = sd_scsi_read(us, srb);
2242 result = sd_scsi_write(us, srb);
2245 info->SrbStatus = SS_ILLEGAL_REQUEST;
2246 result = USB_STOR_TRANSPORT_FAILED;
2255 static int ms_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
2258 struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2259 info->SrbStatus = SS_SUCCESS;
2260 switch (srb->cmnd[0]) {
2261 case TEST_UNIT_READY:
2262 result = ms_scsi_test_unit_ready(us, srb);
2265 result = ms_scsi_inquiry(us, srb);
2268 result = ms_scsi_mode_sense(us, srb);
2271 result = ms_scsi_read_capacity(us, srb);
2274 result = ms_scsi_read(us, srb);
2277 result = ms_scsi_write(us, srb);
2280 info->SrbStatus = SS_ILLEGAL_REQUEST;
2281 result = USB_STOR_TRANSPORT_FAILED;
2287 static int ene_transport(struct scsi_cmnd *srb, struct us_data *us)
2290 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2292 /*US_DEBUG(usb_stor_show_command(us, srb)); */
2293 scsi_set_resid(srb, 0);
2294 if (unlikely(!(info->SD_Status.Ready || info->MS_Status.Ready))) {
2295 result = ene_init(us);
2297 if (info->SD_Status.Ready)
2298 result = sd_scsi_irp(us, srb);
2300 if (info->MS_Status.Ready)
2301 result = ms_scsi_irp(us, srb);
2307 static int ene_ub6250_probe(struct usb_interface *intf,
2308 const struct usb_device_id *id)
2314 result = usb_stor_probe1(&us, intf, id,
2315 (id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list);
2319 /* FIXME: where should the code alloc extra buf ? */
2321 us->extra = kzalloc(sizeof(struct ene_ub6250_info), GFP_KERNEL);
2324 us->extra_destructor = ene_ub6250_info_destructor;
2327 us->transport_name = "ene_ub6250";
2328 us->transport = ene_transport;
2331 result = usb_stor_probe2(us);
2335 /* probe card type */
2336 result = ene_get_card_type(us, REG_CARD_STATUS, &misc_reg03);
2337 if (result != USB_STOR_XFER_GOOD) {
2338 usb_stor_disconnect(intf);
2339 return USB_STOR_TRANSPORT_ERROR;
2342 if (!(misc_reg03 & 0x01)) {
2343 pr_info("ums_eneub6250: The driver only supports SD/MS card. "
2344 "To use SM card, please build driver/staging/keucr\n");
2353 static int ene_ub6250_resume(struct usb_interface *iface)
2356 struct us_data *us = usb_get_intfdata(iface);
2357 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2359 mutex_lock(&us->dev_mutex);
2361 if (us->suspend_resume_hook)
2362 (us->suspend_resume_hook)(us, US_RESUME);
2364 mutex_unlock(&us->dev_mutex);
2366 info->Power_IsResum = true;
2367 /*info->SD_Status.Ready = 0; */
2368 info->SD_Status = *(struct SD_STATUS *)&tmp;
2369 info->MS_Status = *(struct MS_STATUS *)&tmp;
2370 info->SM_Status = *(struct SM_STATUS *)&tmp;
2375 static int ene_ub6250_reset_resume(struct usb_interface *iface)
2378 struct us_data *us = usb_get_intfdata(iface);
2379 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2381 /* Report the reset to the SCSI core */
2382 usb_stor_reset_resume(iface);
2384 /* FIXME: Notify the subdrivers that they need to reinitialize
2386 info->Power_IsResum = true;
2387 /*info->SD_Status.Ready = 0; */
2388 info->SD_Status = *(struct SD_STATUS *)&tmp;
2389 info->MS_Status = *(struct MS_STATUS *)&tmp;
2390 info->SM_Status = *(struct SM_STATUS *)&tmp;
2397 #define ene_ub6250_resume NULL
2398 #define ene_ub6250_reset_resume NULL
2402 static struct usb_driver ene_ub6250_driver = {
2403 .name = "ums_eneub6250",
2404 .probe = ene_ub6250_probe,
2405 .disconnect = usb_stor_disconnect,
2406 .suspend = usb_stor_suspend,
2407 .resume = ene_ub6250_resume,
2408 .reset_resume = ene_ub6250_reset_resume,
2409 .pre_reset = usb_stor_pre_reset,
2410 .post_reset = usb_stor_post_reset,
2411 .id_table = ene_ub6250_usb_ids,
2416 module_usb_driver(ene_ub6250_driver);