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"
33 #define SD_INIT1_FIRMWARE "/*(DEBLOBBED)*/"
34 #define SD_INIT2_FIRMWARE "/*(DEBLOBBED)*/"
35 #define SD_RW_FIRMWARE "/*(DEBLOBBED)*/"
36 #define MS_INIT_FIRMWARE "/*(DEBLOBBED)*/"
37 #define MSP_RW_FIRMWARE "/*(DEBLOBBED)*/"
38 #define MS_RW_FIRMWARE "/*(DEBLOBBED)*/"
40 #define DRV_NAME "ums_eneub6250"
42 MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
43 MODULE_LICENSE("GPL");
47 * The table of devices
49 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
50 vendorName, productName, useProtocol, useTransport, \
51 initFunction, flags) \
52 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
53 .driver_info = (flags)}
55 static struct usb_device_id ene_ub6250_usb_ids[] = {
56 # include "unusual_ene_ub6250.h"
57 { } /* Terminating entry */
59 MODULE_DEVICE_TABLE(usb, ene_ub6250_usb_ids);
66 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
67 vendor_name, product_name, use_protocol, use_transport, \
68 init_function, Flags) \
70 .vendorName = vendor_name, \
71 .productName = product_name, \
72 .useProtocol = use_protocol, \
73 .useTransport = use_transport, \
74 .initFunction = init_function, \
77 static struct us_unusual_dev ene_ub6250_unusual_dev_list[] = {
78 # include "unusual_ene_ub6250.h"
79 { } /* Terminating entry */
86 /* ENE bin code len */
87 #define ENE_BIN_CODE_LEN 0x800
89 #define REG_CARD_STATUS 0xFF83
90 #define REG_HW_TRAP1 0xFF89
93 #define SS_SUCCESS 0x00 /* No Sense */
94 #define SS_NOT_READY 0x02
95 #define SS_MEDIUM_ERR 0x03
96 #define SS_HW_ERR 0x04
97 #define SS_ILLEGAL_REQUEST 0x05
98 #define SS_UNIT_ATTENTION 0x06
100 /* ENE Load FW Pattern */
101 #define SD_INIT1_PATTERN 1
102 #define SD_INIT2_PATTERN 2
103 #define SD_RW_PATTERN 3
104 #define MS_INIT_PATTERN 4
105 #define MSP_RW_PATTERN 5
106 #define MS_RW_PATTERN 6
107 #define SM_INIT_PATTERN 7
108 #define SM_RW_PATTERN 8
115 /* Status Register 1 */
116 #define MS_REG_ST1_MB 0x80 /* media busy */
117 #define MS_REG_ST1_FB1 0x40 /* flush busy 1 */
118 #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */
119 #define MS_REG_ST1_UCDT 0x10 /* unable to correct data */
120 #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */
121 #define MS_REG_ST1_UCEX 0x04 /* unable to correct extra */
122 #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */
123 #define MS_REG_ST1_UCFG 0x01 /* unable to correct overwrite flag */
124 #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)
127 #define MS_REG_OVR_BKST 0x80 /* block status */
128 #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */
129 #define MS_REG_OVR_BKST_NG 0x00 /* NG */
130 #define MS_REG_OVR_PGST0 0x40 /* page status */
131 #define MS_REG_OVR_PGST1 0x20
132 #define MS_REG_OVR_PGST_MASK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1)
133 #define MS_REG_OVR_PGST_OK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1) /* OK */
134 #define MS_REG_OVR_PGST_NG MS_REG_OVR_PGST1 /* NG */
135 #define MS_REG_OVR_PGST_DATA_ERROR 0x00 /* data error */
136 #define MS_REG_OVR_UDST 0x10 /* update status */
137 #define MS_REG_OVR_UDST_UPDATING 0x00 /* updating */
138 #define MS_REG_OVR_UDST_NO_UPDATE MS_REG_OVR_UDST
139 #define MS_REG_OVR_RESERVED 0x08
140 #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)
142 /* Management Flag */
143 #define MS_REG_MNG_SCMS0 0x20 /* serial copy management system */
144 #define MS_REG_MNG_SCMS1 0x10
145 #define MS_REG_MNG_SCMS_MASK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
146 #define MS_REG_MNG_SCMS_COPY_OK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
147 #define MS_REG_MNG_SCMS_ONE_COPY MS_REG_MNG_SCMS1
148 #define MS_REG_MNG_SCMS_NO_COPY 0x00
149 #define MS_REG_MNG_ATFLG 0x08 /* address transfer table flag */
150 #define MS_REG_MNG_ATFLG_OTHER MS_REG_MNG_ATFLG /* other */
151 #define MS_REG_MNG_ATFLG_ATTBL 0x00 /* address transfer table */
152 #define MS_REG_MNG_SYSFLG 0x04 /* system flag */
153 #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */
154 #define MS_REG_MNG_SYSFLG_BOOT 0x00 /* system block */
155 #define MS_REG_MNG_RESERVED 0xc3
156 #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)
159 #define MS_MAX_PAGES_PER_BLOCK 32
160 #define MS_MAX_INITIAL_ERROR_BLOCKS 10
161 #define MS_LIB_BITS_PER_BYTE 8
163 #define MS_SYSINF_FORMAT_FAT 1
164 #define MS_SYSINF_USAGE_GENERAL 0
166 #define MS_SYSINF_MSCLASS_TYPE_1 1
167 #define MS_SYSINF_PAGE_SIZE MS_BYTES_PER_PAGE /* fixed */
169 #define MS_SYSINF_CARDTYPE_RDONLY 1
170 #define MS_SYSINF_CARDTYPE_RDWR 2
171 #define MS_SYSINF_CARDTYPE_HYBRID 3
172 #define MS_SYSINF_SECURITY 0x01
173 #define MS_SYSINF_SECURITY_NO_SUPPORT MS_SYSINF_SECURITY
174 #define MS_SYSINF_SECURITY_SUPPORT 0
176 #define MS_SYSINF_RESERVED1 1
177 #define MS_SYSINF_RESERVED2 1
179 #define MS_SYSENT_TYPE_INVALID_BLOCK 0x01
180 #define MS_SYSENT_TYPE_CIS_IDI 0x0a /* CIS/IDI */
182 #define SIZE_OF_KIRO 1024
183 #define BYTE_MASK 0xff
186 #define MS_STATUS_WRITE_PROTECT 0x0106
187 #define MS_STATUS_SUCCESS 0x0000
188 #define MS_ERROR_FLASH_READ 0x8003
189 #define MS_ERROR_FLASH_ERASE 0x8005
190 #define MS_LB_ERROR 0xfff0
191 #define MS_LB_BOOT_BLOCK 0xfff1
192 #define MS_LB_INITIAL_ERROR 0xfff2
193 #define MS_STATUS_SUCCESS_WITH_ECC 0xfff3
194 #define MS_LB_ACQUIRED_ERROR 0xfff4
195 #define MS_LB_NOT_USED_ERASED 0xfff5
196 #define MS_NOCARD_ERROR 0xfff8
197 #define MS_NO_MEMORY_ERROR 0xfff9
198 #define MS_STATUS_INT_ERROR 0xfffa
199 #define MS_STATUS_ERROR 0xfffe
200 #define MS_LB_NOT_USED 0xffff
202 #define MS_REG_MNG_SYSFLG 0x04 /* system flag */
203 #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */
205 #define MS_BOOT_BLOCK_ID 0x0001
206 #define MS_BOOT_BLOCK_FORMAT_VERSION 0x0100
207 #define MS_BOOT_BLOCK_DATA_ENTRIES 2
209 #define MS_NUMBER_OF_SYSTEM_ENTRY 4
210 #define MS_NUMBER_OF_BOOT_BLOCK 2
211 #define MS_BYTES_PER_PAGE 512
212 #define MS_LOGICAL_BLOCKS_PER_SEGMENT 496
213 #define MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT 494
215 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT 0x200 /* 512 */
216 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK 0x1ff
219 #define MS_REG_OVR_BKST 0x80 /* block status */
220 #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */
221 #define MS_REG_OVR_BKST_NG 0x00 /* NG */
223 /* Status Register 1 */
224 #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */
225 #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */
226 #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */
228 /* MemoryStick Register */
229 /* Status Register 0 */
230 #define MS_REG_ST0_WP 0x01 /* write protected */
231 #define MS_REG_ST0_WP_ON MS_REG_ST0_WP
233 #define MS_LIB_CTRL_RDONLY 0
234 #define MS_LIB_CTRL_WRPROTECT 1
237 #define ms_libconv_to_logical(pdx, PhyBlock) (((PhyBlock) >= (pdx)->MS_Lib.NumberOfPhyBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Phy2LogMap[PhyBlock])
238 #define ms_libconv_to_physical(pdx, LogBlock) (((LogBlock) >= (pdx)->MS_Lib.NumberOfLogBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Log2PhyMap[LogBlock])
240 #define ms_lib_ctrl_set(pdx, Flag) ((pdx)->MS_Lib.flags |= (1 << (Flag)))
241 #define ms_lib_ctrl_reset(pdx, Flag) ((pdx)->MS_Lib.flags &= ~(1 << (Flag)))
242 #define ms_lib_ctrl_check(pdx, Flag) ((pdx)->MS_Lib.flags & (1 << (Flag)))
244 #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))
245 #define ms_lib_clear_pagemap(pdx) memset((pdx)->MS_Lib.pagemap, 0, sizeof((pdx)->MS_Lib.pagemap))
246 #define memstick_logaddr(logadr1, logadr0) ((((u16)(logadr1)) << 8) | (logadr0))
280 struct ms_bootblock_cis {
281 u8 bCistplDEVICE[6]; /* 0 */
282 u8 bCistplDEVICE0C[6]; /* 6 */
283 u8 bCistplJEDECC[4]; /* 12 */
284 u8 bCistplMANFID[6]; /* 16 */
285 u8 bCistplVER1[32]; /* 22 */
286 u8 bCistplFUNCID[4]; /* 54 */
287 u8 bCistplFUNCE0[4]; /* 58 */
288 u8 bCistplFUNCE1[5]; /* 62 */
289 u8 bCistplCONF[7]; /* 67 */
290 u8 bCistplCFTBLENT0[10];/* 74 */
291 u8 bCistplCFTBLENT1[8]; /* 84 */
292 u8 bCistplCFTBLENT2[12];/* 92 */
293 u8 bCistplCFTBLENT3[8]; /* 104 */
294 u8 bCistplCFTBLENT4[17];/* 112 */
295 u8 bCistplCFTBLENT5[8]; /* 129 */
296 u8 bCistplCFTBLENT6[17];/* 137 */
297 u8 bCistplCFTBLENT7[8]; /* 154 */
298 u8 bCistplNOLINK[3]; /* 162 */
301 struct ms_bootblock_idi {
302 #define MS_IDI_GENERAL_CONF 0x848A
303 u16 wIDIgeneralConfiguration; /* 0 */
304 u16 wIDInumberOfCylinder; /* 1 */
305 u16 wIDIreserved0; /* 2 */
306 u16 wIDInumberOfHead; /* 3 */
307 u16 wIDIbytesPerTrack; /* 4 */
308 u16 wIDIbytesPerSector; /* 5 */
309 u16 wIDIsectorsPerTrack; /* 6 */
310 u16 wIDItotalSectors[2]; /* 7-8 high,low */
311 u16 wIDIreserved1[11]; /* 9-19 */
312 u16 wIDIbufferType; /* 20 */
313 u16 wIDIbufferSize; /* 21 */
314 u16 wIDIlongCmdECC; /* 22 */
315 u16 wIDIfirmVersion[4]; /* 23-26 */
316 u16 wIDImodelName[20]; /* 27-46 */
317 u16 wIDIreserved2; /* 47 */
318 u16 wIDIlongWordSupported; /* 48 */
319 u16 wIDIdmaSupported; /* 49 */
320 u16 wIDIreserved3; /* 50 */
321 u16 wIDIpioTiming; /* 51 */
322 u16 wIDIdmaTiming; /* 52 */
323 u16 wIDItransferParameter; /* 53 */
324 u16 wIDIformattedCylinder; /* 54 */
325 u16 wIDIformattedHead; /* 55 */
326 u16 wIDIformattedSectorsPerTrack;/* 56 */
327 u16 wIDIformattedTotalSectors[2];/* 57-58 */
328 u16 wIDImultiSector; /* 59 */
329 u16 wIDIlbaSectors[2]; /* 60-61 */
330 u16 wIDIsingleWordDMA; /* 62 */
331 u16 wIDImultiWordDMA; /* 63 */
332 u16 wIDIreserved4[192]; /* 64-255 */
335 struct ms_bootblock_sysent_rec {
342 struct ms_bootblock_sysent {
343 struct ms_bootblock_sysent_rec entry[MS_NUMBER_OF_SYSTEM_ENTRY];
346 struct ms_bootblock_sysinf {
347 u8 bMsClass; /* must be 1 */
348 u8 bCardType; /* see below */
349 u16 wBlockSize; /* n KB */
350 u16 wBlockNumber; /* number of physical block */
351 u16 wTotalBlockNumber; /* number of logical block */
352 u16 wPageSize; /* must be 0x200 */
353 u8 bExtraSize; /* 0x10 */
357 u8 bAssemblyMakerCode;
358 u8 bAssemblyMachineCode[3];
359 u16 wMemoryMakerCode;
360 u16 wMemoryDeviceCode;
366 u16 wControllerChipNumber;
367 u16 wControllerFunction; /* New MS */
368 u8 bReserved3[9]; /* New MS */
369 u8 bParallelSupport; /* New MS */
370 u16 wFormatValue; /* New MS */
380 struct ms_bootblock_header {
384 u8 bNumberOfDataEntry;
388 struct ms_bootblock_page0 {
389 struct ms_bootblock_header header;
390 struct ms_bootblock_sysent sysent;
391 struct ms_bootblock_sysinf sysinf;
394 struct ms_bootblock_cis_idi {
396 struct ms_bootblock_cis cis;
401 struct ms_bootblock_idi idi;
407 /* ENE MS Lib struct */
408 struct ms_lib_type_extdat {
421 u32 NumberOfCylinder;
422 u32 SectorsPerCylinder;
423 u16 cardType; /* R/W, RO, Hybrid */
426 u16 NumberOfPhyBlock;
427 u16 NumberOfLogBlock;
429 u16 *Phy2LogMap; /* phy2log table */
430 u16 *Log2PhyMap; /* log2phy table */
432 unsigned char *pagemap[(MS_MAX_PAGES_PER_BLOCK + (MS_LIB_BITS_PER_BYTE-1)) / MS_LIB_BITS_PER_BYTE];
433 unsigned char *blkpag;
434 struct ms_lib_type_extdat *blkext;
435 unsigned char copybuf[512];
439 /* SD Block Length */
440 /* 2^9 = 512 Bytes, The HW maximum read/write data length */
441 #define SD_BLOCK_LEN 9
443 struct ene_ub6250_info {
445 /* I/O bounce buffer */
449 struct SD_STATUS SD_Status;
450 struct MS_STATUS MS_Status;
451 struct SM_STATUS SM_Status;
453 /* ----- SD Control Data ---------------- */
454 /*SD_REGISTER SD_Regs; */
460 /* SD/MMC New spec. */
463 u8 SD20_HIGH_CAPACITY;
467 u8 MMC_HIGH_CAPACITY;
469 /*----- MS Control Data ---------------- */
472 struct ms_lib_ctrl MS_Lib;
476 /*----- SM Control Data ---------------- */
480 unsigned char *testbuf;
485 /*------Power Managerment ---------------*/
489 static int ene_sd_init(struct us_data *us);
490 static int ene_ms_init(struct us_data *us);
491 static int ene_load_bincode(struct us_data *us, unsigned char flag);
493 static void ene_ub6250_info_destructor(void *extra)
495 struct ene_ub6250_info *info = (struct ene_ub6250_info *) extra;
502 static int ene_send_scsi_cmd(struct us_data *us, u8 fDir, void *buf, int use_sg)
504 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
505 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
508 unsigned int residue;
509 unsigned int cswlen = 0, partial = 0;
510 unsigned int transfer_length = bcb->DataTransferLength;
512 /* usb_stor_dbg(us, "transport --- ene_send_scsi_cmd\n"); */
513 /* send cmd to out endpoint */
514 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
515 bcb, US_BULK_CB_WRAP_LEN, NULL);
516 if (result != USB_STOR_XFER_GOOD) {
517 usb_stor_dbg(us, "send cmd to out endpoint fail ---\n");
518 return USB_STOR_TRANSPORT_ERROR;
522 unsigned int pipe = fDir;
524 if (fDir == FDIR_READ)
525 pipe = us->recv_bulk_pipe;
527 pipe = us->send_bulk_pipe;
531 result = usb_stor_bulk_srb(us, pipe, us->srb);
533 result = usb_stor_bulk_transfer_sg(us, pipe, buf,
534 transfer_length, 0, &partial);
536 if (result != USB_STOR_XFER_GOOD) {
537 usb_stor_dbg(us, "data transfer fail ---\n");
538 return USB_STOR_TRANSPORT_ERROR;
542 /* Get CSW for device status */
543 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
544 US_BULK_CS_WRAP_LEN, &cswlen);
546 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
547 usb_stor_dbg(us, "Received 0-length CSW; retrying...\n");
548 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
549 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
552 if (result == USB_STOR_XFER_STALLED) {
553 /* get the status again */
554 usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n");
555 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
556 bcs, US_BULK_CS_WRAP_LEN, NULL);
559 if (result != USB_STOR_XFER_GOOD)
560 return USB_STOR_TRANSPORT_ERROR;
562 /* check bulk status */
563 residue = le32_to_cpu(bcs->Residue);
565 /* try to compute the actual residue, based on how much data
566 * was really transferred and what the device tells us */
567 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
568 residue = min(residue, transfer_length);
570 scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
574 if (bcs->Status != US_BULK_STAT_OK)
575 return USB_STOR_TRANSPORT_ERROR;
577 return USB_STOR_TRANSPORT_GOOD;
580 static int sd_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
582 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
584 if (info->SD_Status.Insert && info->SD_Status.Ready)
585 return USB_STOR_TRANSPORT_GOOD;
588 return USB_STOR_TRANSPORT_GOOD;
591 return USB_STOR_TRANSPORT_GOOD;
594 static int sd_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
596 unsigned char data_ptr[36] = {
597 0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
598 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
599 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
600 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30 };
602 usb_stor_set_xfer_buf(data_ptr, 36, srb);
603 return USB_STOR_TRANSPORT_GOOD;
606 static int sd_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
608 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
609 unsigned char mediaNoWP[12] = {
610 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
611 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
612 unsigned char mediaWP[12] = {
613 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
614 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
616 if (info->SD_Status.WtP)
617 usb_stor_set_xfer_buf(mediaWP, 12, srb);
619 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
622 return USB_STOR_TRANSPORT_GOOD;
625 static int sd_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
629 unsigned int offset = 0;
630 unsigned char buf[8];
631 struct scatterlist *sg = NULL;
632 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
634 usb_stor_dbg(us, "sd_scsi_read_capacity\n");
635 if (info->SD_Status.HiCapacity) {
637 if (info->SD_Status.IsMMC)
638 bl_num = info->HC_C_SIZE-1;
640 bl_num = (info->HC_C_SIZE + 1) * 1024 - 1;
642 bl_len = 1 << (info->SD_READ_BL_LEN);
643 bl_num = info->SD_Block_Mult * (info->SD_C_SIZE + 1)
644 * (1 << (info->SD_C_SIZE_MULT + 2)) - 1;
646 info->bl_num = bl_num;
647 usb_stor_dbg(us, "bl_len = %x\n", bl_len);
648 usb_stor_dbg(us, "bl_num = %x\n", bl_num);
650 /*srb->request_bufflen = 8; */
651 buf[0] = (bl_num >> 24) & 0xff;
652 buf[1] = (bl_num >> 16) & 0xff;
653 buf[2] = (bl_num >> 8) & 0xff;
654 buf[3] = (bl_num >> 0) & 0xff;
655 buf[4] = (bl_len >> 24) & 0xff;
656 buf[5] = (bl_len >> 16) & 0xff;
657 buf[6] = (bl_len >> 8) & 0xff;
658 buf[7] = (bl_len >> 0) & 0xff;
660 usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
662 return USB_STOR_TRANSPORT_GOOD;
665 static int sd_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
668 unsigned char *cdb = srb->cmnd;
669 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
670 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
672 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
673 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
674 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
675 u32 bnByte = bn * 0x200;
676 u32 blenByte = blen * 0x200;
678 if (bn > info->bl_num)
679 return USB_STOR_TRANSPORT_ERROR;
681 result = ene_load_bincode(us, SD_RW_PATTERN);
682 if (result != USB_STOR_XFER_GOOD) {
683 usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
684 return USB_STOR_TRANSPORT_ERROR;
687 if (info->SD_Status.HiCapacity)
690 /* set up the command wrapper */
691 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
692 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
693 bcb->DataTransferLength = blenByte;
694 bcb->Flags = US_BULK_FLAG_IN;
696 bcb->CDB[5] = (unsigned char)(bnByte);
697 bcb->CDB[4] = (unsigned char)(bnByte>>8);
698 bcb->CDB[3] = (unsigned char)(bnByte>>16);
699 bcb->CDB[2] = (unsigned char)(bnByte>>24);
701 result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
705 static int sd_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
708 unsigned char *cdb = srb->cmnd;
709 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
710 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
712 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
713 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
714 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
715 u32 bnByte = bn * 0x200;
716 u32 blenByte = blen * 0x200;
718 if (bn > info->bl_num)
719 return USB_STOR_TRANSPORT_ERROR;
721 result = ene_load_bincode(us, SD_RW_PATTERN);
722 if (result != USB_STOR_XFER_GOOD) {
723 usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
724 return USB_STOR_TRANSPORT_ERROR;
727 if (info->SD_Status.HiCapacity)
730 /* set up the command wrapper */
731 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
732 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
733 bcb->DataTransferLength = blenByte;
736 bcb->CDB[5] = (unsigned char)(bnByte);
737 bcb->CDB[4] = (unsigned char)(bnByte>>8);
738 bcb->CDB[3] = (unsigned char)(bnByte>>16);
739 bcb->CDB[2] = (unsigned char)(bnByte>>24);
741 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
749 static int ms_lib_set_logicalpair(struct us_data *us, u16 logblk, u16 phyblk)
751 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
753 if ((logblk >= info->MS_Lib.NumberOfLogBlock) || (phyblk >= info->MS_Lib.NumberOfPhyBlock))
756 info->MS_Lib.Phy2LogMap[phyblk] = logblk;
757 info->MS_Lib.Log2PhyMap[logblk] = phyblk;
762 static int ms_lib_set_logicalblockmark(struct us_data *us, u16 phyblk, u16 mark)
764 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
766 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
769 info->MS_Lib.Phy2LogMap[phyblk] = mark;
774 static int ms_lib_set_initialerrorblock(struct us_data *us, u16 phyblk)
776 return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_INITIAL_ERROR);
779 static int ms_lib_set_bootblockmark(struct us_data *us, u16 phyblk)
781 return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_BOOT_BLOCK);
784 static int ms_lib_free_logicalmap(struct us_data *us)
786 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
788 kfree(info->MS_Lib.Phy2LogMap);
789 info->MS_Lib.Phy2LogMap = NULL;
791 kfree(info->MS_Lib.Log2PhyMap);
792 info->MS_Lib.Log2PhyMap = NULL;
797 static int ms_lib_alloc_logicalmap(struct us_data *us)
800 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
802 info->MS_Lib.Phy2LogMap = kmalloc(info->MS_Lib.NumberOfPhyBlock * sizeof(u16), GFP_KERNEL);
803 info->MS_Lib.Log2PhyMap = kmalloc(info->MS_Lib.NumberOfLogBlock * sizeof(u16), GFP_KERNEL);
805 if ((info->MS_Lib.Phy2LogMap == NULL) || (info->MS_Lib.Log2PhyMap == NULL)) {
806 ms_lib_free_logicalmap(us);
810 for (i = 0; i < info->MS_Lib.NumberOfPhyBlock; i++)
811 info->MS_Lib.Phy2LogMap[i] = MS_LB_NOT_USED;
813 for (i = 0; i < info->MS_Lib.NumberOfLogBlock; i++)
814 info->MS_Lib.Log2PhyMap[i] = MS_LB_NOT_USED;
819 static void ms_lib_clear_writebuf(struct us_data *us)
822 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
824 info->MS_Lib.wrtblk = (u16)-1;
825 ms_lib_clear_pagemap(info);
827 if (info->MS_Lib.blkpag)
828 memset(info->MS_Lib.blkpag, 0xff, info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector);
830 if (info->MS_Lib.blkext) {
831 for (i = 0; i < info->MS_Lib.PagesPerBlock; i++) {
832 info->MS_Lib.blkext[i].status1 = MS_REG_ST1_DEFAULT;
833 info->MS_Lib.blkext[i].ovrflg = MS_REG_OVR_DEFAULT;
834 info->MS_Lib.blkext[i].mngflg = MS_REG_MNG_DEFAULT;
835 info->MS_Lib.blkext[i].logadr = MS_LB_NOT_USED;
840 static int ms_count_freeblock(struct us_data *us, u16 PhyBlock)
843 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
845 Ende = PhyBlock + MS_PHYSICAL_BLOCKS_PER_SEGMENT;
846 for (Count = 0; PhyBlock < Ende; PhyBlock++) {
847 switch (info->MS_Lib.Phy2LogMap[PhyBlock]) {
849 case MS_LB_NOT_USED_ERASED:
859 static int ms_read_readpage(struct us_data *us, u32 PhyBlockAddr,
860 u8 PageNum, u32 *PageBuf, struct ms_lib_type_extdat *ExtraDat)
862 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
863 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
864 u8 *bbuf = info->bbuf;
866 u32 bn = PhyBlockAddr * 0x20 + PageNum;
868 /* printk(KERN_INFO "MS --- MS_ReaderReadPage,
869 PhyBlockAddr = %x, PageNum = %x\n", PhyBlockAddr, PageNum); */
871 result = ene_load_bincode(us, MS_RW_PATTERN);
872 if (result != USB_STOR_XFER_GOOD)
873 return USB_STOR_TRANSPORT_ERROR;
876 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
877 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
878 bcb->DataTransferLength = 0x200;
879 bcb->Flags = US_BULK_FLAG_IN;
882 bcb->CDB[1] = 0x02; /* in init.c ENE_MSInit() is 0x01 */
884 bcb->CDB[5] = (unsigned char)(bn);
885 bcb->CDB[4] = (unsigned char)(bn>>8);
886 bcb->CDB[3] = (unsigned char)(bn>>16);
887 bcb->CDB[2] = (unsigned char)(bn>>24);
889 result = ene_send_scsi_cmd(us, FDIR_READ, PageBuf, 0);
890 if (result != USB_STOR_XFER_GOOD)
891 return USB_STOR_TRANSPORT_ERROR;
894 /* Read Extra Data */
895 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
896 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
897 bcb->DataTransferLength = 0x4;
898 bcb->Flags = US_BULK_FLAG_IN;
902 bcb->CDB[5] = (unsigned char)(PageNum);
903 bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
904 bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
905 bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
908 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
909 if (result != USB_STOR_XFER_GOOD)
910 return USB_STOR_TRANSPORT_ERROR;
912 ExtraDat->reserved = 0;
913 ExtraDat->intr = 0x80; /* Not yet,fireware support */
914 ExtraDat->status0 = 0x10; /* Not yet,fireware support */
916 ExtraDat->status1 = 0x00; /* Not yet,fireware support */
917 ExtraDat->ovrflg = bbuf[0];
918 ExtraDat->mngflg = bbuf[1];
919 ExtraDat->logadr = memstick_logaddr(bbuf[2], bbuf[3]);
921 return USB_STOR_TRANSPORT_GOOD;
924 static int ms_lib_process_bootblock(struct us_data *us, u16 PhyBlock, u8 *PageData)
926 struct ms_bootblock_sysent *SysEntry;
927 struct ms_bootblock_sysinf *SysInfo;
931 struct ms_lib_type_extdat ExtraData;
932 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
934 PageBuffer = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
935 if (PageBuffer == NULL)
940 SysInfo = &(((struct ms_bootblock_page0 *)PageData)->sysinf);
942 if ((SysInfo->bMsClass != MS_SYSINF_MSCLASS_TYPE_1) ||
943 (be16_to_cpu(SysInfo->wPageSize) != MS_SYSINF_PAGE_SIZE) ||
944 ((SysInfo->bSecuritySupport & MS_SYSINF_SECURITY) == MS_SYSINF_SECURITY_SUPPORT) ||
945 (SysInfo->bReserved1 != MS_SYSINF_RESERVED1) ||
946 (SysInfo->bReserved2 != MS_SYSINF_RESERVED2) ||
947 (SysInfo->bFormatType != MS_SYSINF_FORMAT_FAT) ||
948 (SysInfo->bUsage != MS_SYSINF_USAGE_GENERAL))
951 switch (info->MS_Lib.cardType = SysInfo->bCardType) {
952 case MS_SYSINF_CARDTYPE_RDONLY:
953 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY);
955 case MS_SYSINF_CARDTYPE_RDWR:
956 ms_lib_ctrl_reset(info, MS_LIB_CTRL_RDONLY);
958 case MS_SYSINF_CARDTYPE_HYBRID:
963 info->MS_Lib.blockSize = be16_to_cpu(SysInfo->wBlockSize);
964 info->MS_Lib.NumberOfPhyBlock = be16_to_cpu(SysInfo->wBlockNumber);
965 info->MS_Lib.NumberOfLogBlock = be16_to_cpu(SysInfo->wTotalBlockNumber)-2;
966 info->MS_Lib.PagesPerBlock = info->MS_Lib.blockSize * SIZE_OF_KIRO / MS_BYTES_PER_PAGE;
967 info->MS_Lib.NumberOfSegment = info->MS_Lib.NumberOfPhyBlock / MS_PHYSICAL_BLOCKS_PER_SEGMENT;
968 info->MS_Model = be16_to_cpu(SysInfo->wMemorySize);
970 /*Allocate to all number of logicalblock and physicalblock */
971 if (ms_lib_alloc_logicalmap(us))
974 /* Mark the book block */
975 ms_lib_set_bootblockmark(us, PhyBlock);
977 SysEntry = &(((struct ms_bootblock_page0 *)PageData)->sysent);
979 for (i = 0; i < MS_NUMBER_OF_SYSTEM_ENTRY; i++) {
980 u32 EntryOffset, EntrySize;
982 EntryOffset = be32_to_cpu(SysEntry->entry[i].dwStart);
984 if (EntryOffset == 0xffffff)
986 EntrySize = be32_to_cpu(SysEntry->entry[i].dwSize);
991 if (EntryOffset + MS_BYTES_PER_PAGE + EntrySize > info->MS_Lib.blockSize * (u32)SIZE_OF_KIRO)
995 u8 PrevPageNumber = 0;
998 if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_INVALID_BLOCK)
1001 while (EntrySize > 0) {
1003 PageNumber = (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1);
1004 if (PageNumber != PrevPageNumber) {
1005 switch (ms_read_readpage(us, PhyBlock, PageNumber, (u32 *)PageBuffer, &ExtraData)) {
1006 case MS_STATUS_SUCCESS:
1008 case MS_STATUS_WRITE_PROTECT:
1009 case MS_ERROR_FLASH_READ:
1010 case MS_STATUS_ERROR:
1015 PrevPageNumber = PageNumber;
1018 phyblk = be16_to_cpu(*(u16 *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)));
1019 if (phyblk < 0x0fff)
1020 ms_lib_set_initialerrorblock(us, phyblk);
1025 } else if (i == 1) { /* CIS/IDI */
1026 struct ms_bootblock_idi *idi;
1028 if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_CIS_IDI)
1031 switch (ms_read_readpage(us, PhyBlock, (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1), (u32 *)PageBuffer, &ExtraData)) {
1032 case MS_STATUS_SUCCESS:
1034 case MS_STATUS_WRITE_PROTECT:
1035 case MS_ERROR_FLASH_READ:
1036 case MS_STATUS_ERROR:
1041 idi = &((struct ms_bootblock_cis_idi *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)))->idi.idi;
1042 if (le16_to_cpu(idi->wIDIgeneralConfiguration) != MS_IDI_GENERAL_CONF)
1045 info->MS_Lib.BytesPerSector = le16_to_cpu(idi->wIDIbytesPerSector);
1046 if (info->MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE)
1055 ms_lib_free_logicalmap(us);
1063 static void ms_lib_free_writebuf(struct us_data *us)
1065 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1066 info->MS_Lib.wrtblk = (u16)-1; /* set to -1 */
1068 /* memset((fdoExt)->MS_Lib.pagemap, 0, sizeof((fdoExt)->MS_Lib.pagemap)) */
1070 ms_lib_clear_pagemap(info); /* (pdx)->MS_Lib.pagemap memset 0 in ms.h */
1072 if (info->MS_Lib.blkpag) {
1073 kfree((u8 *)(info->MS_Lib.blkpag)); /* Arnold test ... */
1074 info->MS_Lib.blkpag = NULL;
1077 if (info->MS_Lib.blkext) {
1078 kfree((u8 *)(info->MS_Lib.blkext)); /* Arnold test ... */
1079 info->MS_Lib.blkext = NULL;
1084 static void ms_lib_free_allocatedarea(struct us_data *us)
1086 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1088 ms_lib_free_writebuf(us); /* Free MS_Lib.pagemap */
1089 ms_lib_free_logicalmap(us); /* kfree MS_Lib.Phy2LogMap and MS_Lib.Log2PhyMap */
1091 /* set struct us point flag to 0 */
1092 info->MS_Lib.flags = 0;
1093 info->MS_Lib.BytesPerSector = 0;
1094 info->MS_Lib.SectorsPerCylinder = 0;
1096 info->MS_Lib.cardType = 0;
1097 info->MS_Lib.blockSize = 0;
1098 info->MS_Lib.PagesPerBlock = 0;
1100 info->MS_Lib.NumberOfPhyBlock = 0;
1101 info->MS_Lib.NumberOfLogBlock = 0;
1105 static int ms_lib_alloc_writebuf(struct us_data *us)
1107 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1109 info->MS_Lib.wrtblk = (u16)-1;
1111 info->MS_Lib.blkpag = kmalloc(info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector, GFP_KERNEL);
1112 info->MS_Lib.blkext = kmalloc(info->MS_Lib.PagesPerBlock * sizeof(struct ms_lib_type_extdat), GFP_KERNEL);
1114 if ((info->MS_Lib.blkpag == NULL) || (info->MS_Lib.blkext == NULL)) {
1115 ms_lib_free_writebuf(us);
1119 ms_lib_clear_writebuf(us);
1124 static int ms_lib_force_setlogical_pair(struct us_data *us, u16 logblk, u16 phyblk)
1126 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1128 if (logblk == MS_LB_NOT_USED)
1131 if ((logblk >= info->MS_Lib.NumberOfLogBlock) ||
1132 (phyblk >= info->MS_Lib.NumberOfPhyBlock))
1135 info->MS_Lib.Phy2LogMap[phyblk] = logblk;
1136 info->MS_Lib.Log2PhyMap[logblk] = phyblk;
1141 static int ms_read_copyblock(struct us_data *us, u16 oldphy, u16 newphy,
1142 u16 PhyBlockAddr, u8 PageNum, unsigned char *buf, u16 len)
1144 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1147 /* printk(KERN_INFO "MS_ReaderCopyBlock --- PhyBlockAddr = %x,
1148 PageNum = %x\n", PhyBlockAddr, PageNum); */
1149 result = ene_load_bincode(us, MS_RW_PATTERN);
1150 if (result != USB_STOR_XFER_GOOD)
1151 return USB_STOR_TRANSPORT_ERROR;
1153 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1154 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1155 bcb->DataTransferLength = 0x200*len;
1159 bcb->CDB[4] = (unsigned char)(oldphy);
1160 bcb->CDB[3] = (unsigned char)(oldphy>>8);
1161 bcb->CDB[2] = 0; /* (BYTE)(oldphy>>16) */
1162 bcb->CDB[7] = (unsigned char)(newphy);
1163 bcb->CDB[6] = (unsigned char)(newphy>>8);
1164 bcb->CDB[5] = 0; /* (BYTE)(newphy>>16) */
1165 bcb->CDB[9] = (unsigned char)(PhyBlockAddr);
1166 bcb->CDB[8] = (unsigned char)(PhyBlockAddr>>8);
1167 bcb->CDB[10] = PageNum;
1169 result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1170 if (result != USB_STOR_XFER_GOOD)
1171 return USB_STOR_TRANSPORT_ERROR;
1173 return USB_STOR_TRANSPORT_GOOD;
1176 static int ms_read_eraseblock(struct us_data *us, u32 PhyBlockAddr)
1178 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1180 u32 bn = PhyBlockAddr;
1182 /* printk(KERN_INFO "MS --- ms_read_eraseblock,
1183 PhyBlockAddr = %x\n", PhyBlockAddr); */
1184 result = ene_load_bincode(us, MS_RW_PATTERN);
1185 if (result != USB_STOR_XFER_GOOD)
1186 return USB_STOR_TRANSPORT_ERROR;
1188 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1189 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1190 bcb->DataTransferLength = 0x200;
1191 bcb->Flags = US_BULK_FLAG_IN;
1194 bcb->CDB[4] = (unsigned char)(bn);
1195 bcb->CDB[3] = (unsigned char)(bn>>8);
1196 bcb->CDB[2] = (unsigned char)(bn>>16);
1198 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1199 if (result != USB_STOR_XFER_GOOD)
1200 return USB_STOR_TRANSPORT_ERROR;
1202 return USB_STOR_TRANSPORT_GOOD;
1205 static int ms_lib_check_disableblock(struct us_data *us, u16 PhyBlock)
1207 unsigned char *PageBuf = NULL;
1208 u16 result = MS_STATUS_SUCCESS;
1210 struct ms_lib_type_extdat extdat;
1211 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1213 PageBuf = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1214 if (PageBuf == NULL) {
1215 result = MS_NO_MEMORY_ERROR;
1219 ms_read_readpage(us, PhyBlock, 1, (u32 *)PageBuf, &extdat);
1221 blk = be16_to_cpu(PageBuf[index]);
1222 if (blk == MS_LB_NOT_USED)
1224 if (blk == info->MS_Lib.Log2PhyMap[0]) {
1225 result = MS_ERROR_FLASH_READ;
1236 static int ms_lib_setacquired_errorblock(struct us_data *us, u16 phyblk)
1239 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1241 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1244 log = info->MS_Lib.Phy2LogMap[phyblk];
1246 if (log < info->MS_Lib.NumberOfLogBlock)
1247 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1249 if (info->MS_Lib.Phy2LogMap[phyblk] != MS_LB_INITIAL_ERROR)
1250 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_ACQUIRED_ERROR;
1255 static int ms_lib_overwrite_extra(struct us_data *us, u32 PhyBlockAddr,
1256 u8 PageNum, u8 OverwriteFlag)
1258 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1261 /* printk("MS --- MS_LibOverwriteExtra,
1262 PhyBlockAddr = %x, PageNum = %x\n", PhyBlockAddr, PageNum); */
1263 result = ene_load_bincode(us, MS_RW_PATTERN);
1264 if (result != USB_STOR_XFER_GOOD)
1265 return USB_STOR_TRANSPORT_ERROR;
1267 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1268 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1269 bcb->DataTransferLength = 0x4;
1270 bcb->Flags = US_BULK_FLAG_IN;
1273 bcb->CDB[5] = (unsigned char)(PageNum);
1274 bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
1275 bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
1276 bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
1277 bcb->CDB[6] = OverwriteFlag;
1282 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1283 if (result != USB_STOR_XFER_GOOD)
1284 return USB_STOR_TRANSPORT_ERROR;
1286 return USB_STOR_TRANSPORT_GOOD;
1289 static int ms_lib_error_phyblock(struct us_data *us, u16 phyblk)
1291 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1293 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1294 return MS_STATUS_ERROR;
1296 ms_lib_setacquired_errorblock(us, phyblk);
1298 if (ms_lib_iswritable(info))
1299 return ms_lib_overwrite_extra(us, phyblk, 0, (u8)(~MS_REG_OVR_BKST & BYTE_MASK));
1301 return MS_STATUS_SUCCESS;
1304 static int ms_lib_erase_phyblock(struct us_data *us, u16 phyblk)
1307 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1309 if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1310 return MS_STATUS_ERROR;
1312 log = info->MS_Lib.Phy2LogMap[phyblk];
1314 if (log < info->MS_Lib.NumberOfLogBlock)
1315 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1317 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED;
1319 if (ms_lib_iswritable(info)) {
1320 switch (ms_read_eraseblock(us, phyblk)) {
1321 case MS_STATUS_SUCCESS:
1322 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED_ERASED;
1323 return MS_STATUS_SUCCESS;
1324 case MS_ERROR_FLASH_ERASE:
1325 case MS_STATUS_INT_ERROR:
1326 ms_lib_error_phyblock(us, phyblk);
1327 return MS_ERROR_FLASH_ERASE;
1328 case MS_STATUS_ERROR:
1330 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY); /* MS_LibCtrlSet will used by ENE_MSInit ,need check, and why us to info*/
1331 ms_lib_setacquired_errorblock(us, phyblk);
1332 return MS_STATUS_ERROR;
1336 ms_lib_setacquired_errorblock(us, phyblk);
1338 return MS_STATUS_SUCCESS;
1341 static int ms_lib_read_extra(struct us_data *us, u32 PhyBlock,
1342 u8 PageNum, struct ms_lib_type_extdat *ExtraDat)
1344 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1345 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1346 u8 *bbuf = info->bbuf;
1349 /* printk("MS_LibReadExtra --- PhyBlock = %x, PageNum = %x\n", PhyBlock, PageNum); */
1350 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1351 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1352 bcb->DataTransferLength = 0x4;
1353 bcb->Flags = US_BULK_FLAG_IN;
1356 bcb->CDB[5] = (unsigned char)(PageNum);
1357 bcb->CDB[4] = (unsigned char)(PhyBlock);
1358 bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
1359 bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
1362 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
1363 if (result != USB_STOR_XFER_GOOD)
1364 return USB_STOR_TRANSPORT_ERROR;
1366 ExtraDat->reserved = 0;
1367 ExtraDat->intr = 0x80; /* Not yet, waiting for fireware support */
1368 ExtraDat->status0 = 0x10; /* Not yet, waiting for fireware support */
1369 ExtraDat->status1 = 0x00; /* Not yet, waiting for fireware support */
1370 ExtraDat->ovrflg = bbuf[0];
1371 ExtraDat->mngflg = bbuf[1];
1372 ExtraDat->logadr = memstick_logaddr(bbuf[2], bbuf[3]);
1374 return USB_STOR_TRANSPORT_GOOD;
1377 static int ms_libsearch_block_from_physical(struct us_data *us, u16 phyblk)
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 Newblk = info->MS_Lib.Phy2LogMap[blk];
1393 if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED_ERASED) {
1395 } else if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED) {
1396 switch (ms_lib_read_extra(us, blk, 0, &extdat)) {
1397 case MS_STATUS_SUCCESS:
1398 case MS_STATUS_SUCCESS_WITH_ECC:
1400 case MS_NOCARD_ERROR:
1401 return MS_NOCARD_ERROR;
1402 case MS_STATUS_INT_ERROR:
1404 case MS_ERROR_FLASH_READ:
1406 ms_lib_setacquired_errorblock(us, blk);
1410 if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1411 ms_lib_setacquired_errorblock(us, blk);
1415 switch (ms_lib_erase_phyblock(us, blk)) {
1416 case MS_STATUS_SUCCESS:
1418 case MS_STATUS_ERROR:
1420 case MS_ERROR_FLASH_ERASE:
1422 ms_lib_error_phyblock(us, blk);
1430 static int ms_libsearch_block_from_logical(struct us_data *us, u16 logblk)
1433 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1435 phyblk = ms_libconv_to_physical(info, logblk);
1436 if (phyblk >= MS_LB_ERROR) {
1437 if (logblk >= info->MS_Lib.NumberOfLogBlock)
1440 phyblk = (logblk + MS_NUMBER_OF_BOOT_BLOCK) / MS_LOGICAL_BLOCKS_PER_SEGMENT;
1441 phyblk *= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1442 phyblk += MS_PHYSICAL_BLOCKS_PER_SEGMENT - 1;
1445 return ms_libsearch_block_from_physical(us, phyblk);
1448 static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
1450 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
1452 /* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
1453 if (info->MS_Status.Insert && info->MS_Status.Ready) {
1454 return USB_STOR_TRANSPORT_GOOD;
1457 return USB_STOR_TRANSPORT_GOOD;
1460 return USB_STOR_TRANSPORT_GOOD;
1463 static int ms_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
1465 /* pr_info("MS_SCSI_Inquiry\n"); */
1466 unsigned char data_ptr[36] = {
1467 0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
1468 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
1469 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
1470 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30};
1472 usb_stor_set_xfer_buf(data_ptr, 36, srb);
1473 return USB_STOR_TRANSPORT_GOOD;
1476 static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
1478 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1479 unsigned char mediaNoWP[12] = {
1480 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
1481 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1482 unsigned char mediaWP[12] = {
1483 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
1484 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1486 if (info->MS_Status.WtP)
1487 usb_stor_set_xfer_buf(mediaWP, 12, srb);
1489 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
1491 return USB_STOR_TRANSPORT_GOOD;
1494 static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
1498 unsigned int offset = 0;
1499 unsigned char buf[8];
1500 struct scatterlist *sg = NULL;
1501 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1503 usb_stor_dbg(us, "ms_scsi_read_capacity\n");
1505 if (info->MS_Status.IsMSPro)
1506 bl_num = info->MSP_TotalBlock - 1;
1508 bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1;
1510 info->bl_num = bl_num;
1511 usb_stor_dbg(us, "bl_len = %x\n", bl_len);
1512 usb_stor_dbg(us, "bl_num = %x\n", bl_num);
1514 /*srb->request_bufflen = 8; */
1515 buf[0] = (bl_num >> 24) & 0xff;
1516 buf[1] = (bl_num >> 16) & 0xff;
1517 buf[2] = (bl_num >> 8) & 0xff;
1518 buf[3] = (bl_num >> 0) & 0xff;
1519 buf[4] = (bl_len >> 24) & 0xff;
1520 buf[5] = (bl_len >> 16) & 0xff;
1521 buf[6] = (bl_len >> 8) & 0xff;
1522 buf[7] = (bl_len >> 0) & 0xff;
1524 usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
1526 return USB_STOR_TRANSPORT_GOOD;
1529 static void ms_lib_phy_to_log_range(u16 PhyBlock, u16 *LogStart, u16 *LogEnde)
1531 PhyBlock /= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1534 *LogStart = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT + (PhyBlock - 1) * MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1535 *LogEnde = *LogStart + MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1538 *LogEnde = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT;/*494*/
1542 static int ms_lib_read_extrablock(struct us_data *us, u32 PhyBlock,
1543 u8 PageNum, u8 blen, void *buf)
1545 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1548 /* printk("MS_LibReadExtraBlock --- PhyBlock = %x,
1549 PageNum = %x, blen = %x\n", PhyBlock, PageNum, blen); */
1551 /* Read Extra Data */
1552 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1553 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1554 bcb->DataTransferLength = 0x4 * blen;
1555 bcb->Flags = US_BULK_FLAG_IN;
1558 bcb->CDB[5] = (unsigned char)(PageNum);
1559 bcb->CDB[4] = (unsigned char)(PhyBlock);
1560 bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
1561 bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
1564 result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1565 if (result != USB_STOR_XFER_GOOD)
1566 return USB_STOR_TRANSPORT_ERROR;
1568 return USB_STOR_TRANSPORT_GOOD;
1571 static int ms_lib_scan_logicalblocknumber(struct us_data *us, u16 btBlk1st)
1573 u16 PhyBlock, newblk, i;
1574 u16 LogStart, LogEnde;
1575 struct ms_lib_type_extdat extdat;
1576 u32 count = 0, index = 0;
1577 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1578 u8 *bbuf = info->bbuf;
1580 for (PhyBlock = 0; PhyBlock < info->MS_Lib.NumberOfPhyBlock;) {
1581 ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde);
1583 for (i = 0; i < MS_PHYSICAL_BLOCKS_PER_SEGMENT; i++, PhyBlock++) {
1584 switch (ms_libconv_to_logical(info, PhyBlock)) {
1585 case MS_STATUS_ERROR:
1591 if (count == PhyBlock) {
1592 ms_lib_read_extrablock(us, PhyBlock, 0, 0x80,
1596 index = (PhyBlock % 0x80) * 4;
1598 extdat.ovrflg = bbuf[index];
1599 extdat.mngflg = bbuf[index+1];
1600 extdat.logadr = memstick_logaddr(bbuf[index+2],
1603 if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1604 ms_lib_setacquired_errorblock(us, PhyBlock);
1608 if ((extdat.mngflg & MS_REG_MNG_ATFLG) == MS_REG_MNG_ATFLG_ATTBL) {
1609 ms_lib_erase_phyblock(us, PhyBlock);
1613 if (extdat.logadr != MS_LB_NOT_USED) {
1614 if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) {
1615 ms_lib_erase_phyblock(us, PhyBlock);
1619 newblk = ms_libconv_to_physical(info, extdat.logadr);
1621 if (newblk != MS_LB_NOT_USED) {
1622 if (extdat.logadr == 0) {
1623 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1624 if (ms_lib_check_disableblock(us, btBlk1st)) {
1625 ms_lib_set_logicalpair(us, extdat.logadr, newblk);
1630 ms_lib_read_extra(us, newblk, 0, &extdat);
1631 if ((extdat.ovrflg & MS_REG_OVR_UDST) == MS_REG_OVR_UDST_UPDATING) {
1632 ms_lib_erase_phyblock(us, PhyBlock);
1635 ms_lib_erase_phyblock(us, newblk);
1639 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1644 return MS_STATUS_SUCCESS;
1648 static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
1651 unsigned char *cdb = srb->cmnd;
1652 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1653 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1655 u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
1656 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
1657 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1658 u32 blenByte = blen * 0x200;
1660 if (bn > info->bl_num)
1661 return USB_STOR_TRANSPORT_ERROR;
1663 if (info->MS_Status.IsMSPro) {
1664 result = ene_load_bincode(us, MSP_RW_PATTERN);
1665 if (result != USB_STOR_XFER_GOOD) {
1666 usb_stor_dbg(us, "Load MPS RW pattern Fail !!\n");
1667 return USB_STOR_TRANSPORT_ERROR;
1670 /* set up the command wrapper */
1671 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1672 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1673 bcb->DataTransferLength = blenByte;
1674 bcb->Flags = US_BULK_FLAG_IN;
1677 bcb->CDB[5] = (unsigned char)(bn);
1678 bcb->CDB[4] = (unsigned char)(bn>>8);
1679 bcb->CDB[3] = (unsigned char)(bn>>16);
1680 bcb->CDB[2] = (unsigned char)(bn>>24);
1682 result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
1691 buf = kmalloc(blenByte, GFP_KERNEL);
1693 return USB_STOR_TRANSPORT_ERROR;
1695 result = ene_load_bincode(us, MS_RW_PATTERN);
1696 if (result != USB_STOR_XFER_GOOD) {
1697 pr_info("Load MS RW pattern Fail !!\n");
1698 result = USB_STOR_TRANSPORT_ERROR;
1702 logblk = (u16)(bn / info->MS_Lib.PagesPerBlock);
1703 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1706 if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1707 len = info->MS_Lib.PagesPerBlock-PageNum;
1711 phyblk = ms_libconv_to_physical(info, logblk);
1712 blkno = phyblk * 0x20 + PageNum;
1714 /* set up the command wrapper */
1715 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1716 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1717 bcb->DataTransferLength = 0x200 * len;
1718 bcb->Flags = US_BULK_FLAG_IN;
1721 bcb->CDB[5] = (unsigned char)(blkno);
1722 bcb->CDB[4] = (unsigned char)(blkno>>8);
1723 bcb->CDB[3] = (unsigned char)(blkno>>16);
1724 bcb->CDB[2] = (unsigned char)(blkno>>24);
1726 result = ene_send_scsi_cmd(us, FDIR_READ, buf+offset, 0);
1727 if (result != USB_STOR_XFER_GOOD) {
1728 pr_info("MS_SCSI_Read --- result = %x\n", result);
1729 result = USB_STOR_TRANSPORT_ERROR;
1738 offset += MS_BYTES_PER_PAGE*len;
1740 usb_stor_set_xfer_buf(buf, blenByte, srb);
1747 static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
1750 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1751 unsigned char *cdb = srb->cmnd;
1752 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1754 u32 bn = ((cdb[2] << 24) & 0xff000000) |
1755 ((cdb[3] << 16) & 0x00ff0000) |
1756 ((cdb[4] << 8) & 0x0000ff00) |
1757 ((cdb[5] << 0) & 0x000000ff);
1758 u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1759 u32 blenByte = blen * 0x200;
1761 if (bn > info->bl_num)
1762 return USB_STOR_TRANSPORT_ERROR;
1764 if (info->MS_Status.IsMSPro) {
1765 result = ene_load_bincode(us, MSP_RW_PATTERN);
1766 if (result != USB_STOR_XFER_GOOD) {
1767 pr_info("Load MSP RW pattern Fail !!\n");
1768 return USB_STOR_TRANSPORT_ERROR;
1771 /* set up the command wrapper */
1772 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1773 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1774 bcb->DataTransferLength = blenByte;
1778 bcb->CDB[5] = (unsigned char)(bn);
1779 bcb->CDB[4] = (unsigned char)(bn>>8);
1780 bcb->CDB[3] = (unsigned char)(bn>>16);
1781 bcb->CDB[2] = (unsigned char)(bn>>24);
1783 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
1789 u16 len, oldphy, newphy;
1791 buf = kmalloc(blenByte, GFP_KERNEL);
1793 return USB_STOR_TRANSPORT_ERROR;
1794 usb_stor_set_xfer_buf(buf, blenByte, srb);
1796 result = ene_load_bincode(us, MS_RW_PATTERN);
1797 if (result != USB_STOR_XFER_GOOD) {
1798 pr_info("Load MS RW pattern Fail !!\n");
1799 result = USB_STOR_TRANSPORT_ERROR;
1803 PhyBlockAddr = (u16)(bn / info->MS_Lib.PagesPerBlock);
1804 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1807 if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1808 len = info->MS_Lib.PagesPerBlock-PageNum;
1812 oldphy = ms_libconv_to_physical(info, PhyBlockAddr); /* need check us <-> info */
1813 newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr);
1815 result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len);
1817 if (result != USB_STOR_XFER_GOOD) {
1818 pr_info("MS_SCSI_Write --- result = %x\n", result);
1819 result = USB_STOR_TRANSPORT_ERROR;
1823 info->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED;
1824 ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy);
1831 offset += MS_BYTES_PER_PAGE*len;
1843 static int ene_get_card_type(struct us_data *us, u16 index, void *buf)
1845 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1848 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1849 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1850 bcb->DataTransferLength = 0x01;
1851 bcb->Flags = US_BULK_FLAG_IN;
1853 bcb->CDB[2] = (unsigned char)(index>>8);
1854 bcb->CDB[3] = (unsigned char)index;
1856 result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1860 static int ene_get_card_status(struct us_data *us, u8 *buf)
1864 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1866 /*usb_stor_dbg(us, "transport --- ENE_ReadSDReg\n");*/
1867 reg4b = *(u32 *)&buf[0x18];
1868 info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f);
1870 tmpreg = (u16) reg4b;
1871 reg4b = *(u32 *)(&buf[0x14]);
1872 if (info->SD_Status.HiCapacity && !info->SD_Status.IsMMC)
1873 info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
1875 info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
1876 info->SD_C_SIZE_MULT = (u8)(reg4b >> 7) & 0x07;
1877 if (info->SD_Status.HiCapacity && info->SD_Status.IsMMC)
1878 info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
1880 if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
1881 info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN);
1882 info->SD_READ_BL_LEN = SD_BLOCK_LEN;
1884 info->SD_Block_Mult = 1;
1887 return USB_STOR_TRANSPORT_GOOD;
1890 static int ene_load_bincode(struct us_data *us, unsigned char flag)
1893 char *fw_name = NULL;
1894 unsigned char *buf = NULL;
1895 const struct firmware *sd_fw = NULL;
1896 int result = USB_STOR_TRANSPORT_ERROR;
1897 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1898 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1900 if (info->BIN_FLAG == flag)
1901 return USB_STOR_TRANSPORT_GOOD;
1905 case SD_INIT1_PATTERN:
1906 usb_stor_dbg(us, "SD_INIT1_PATTERN\n");
1907 fw_name = SD_INIT1_FIRMWARE;
1909 case SD_INIT2_PATTERN:
1910 usb_stor_dbg(us, "SD_INIT2_PATTERN\n");
1911 fw_name = SD_INIT2_FIRMWARE;
1914 usb_stor_dbg(us, "SD_RW_PATTERN\n");
1915 fw_name = SD_RW_FIRMWARE;
1918 case MS_INIT_PATTERN:
1919 usb_stor_dbg(us, "MS_INIT_PATTERN\n");
1920 fw_name = MS_INIT_FIRMWARE;
1922 case MSP_RW_PATTERN:
1923 usb_stor_dbg(us, "MSP_RW_PATTERN\n");
1924 fw_name = MSP_RW_FIRMWARE;
1927 usb_stor_dbg(us, "MS_RW_PATTERN\n");
1928 fw_name = MS_RW_FIRMWARE;
1931 usb_stor_dbg(us, "----------- Unknown PATTERN ----------\n");
1935 err = reject_firmware(&sd_fw, fw_name, &us->pusb_dev->dev);
1937 usb_stor_dbg(us, "load firmware %s failed\n", fw_name);
1940 buf = kmemdup(sd_fw->data, sd_fw->size, GFP_KERNEL);
1944 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1945 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1946 bcb->DataTransferLength = sd_fw->size;
1950 result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1951 if (us->srb != NULL)
1952 scsi_set_resid(us->srb, 0);
1953 info->BIN_FLAG = flag;
1957 release_firmware(sd_fw);
1961 static int ms_card_init(struct us_data *us)
1965 unsigned char *PageBuffer0 = NULL, *PageBuffer1 = NULL;
1966 struct ms_lib_type_extdat extdat;
1967 u16 btBlk1st, btBlk2nd;
1969 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1971 printk(KERN_INFO "MS_CardInit start\n");
1973 ms_lib_free_allocatedarea(us); /* Clean buffer and set struct us_data flag to 0 */
1975 /* get two PageBuffer */
1976 PageBuffer0 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1977 PageBuffer1 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1978 if ((PageBuffer0 == NULL) || (PageBuffer1 == NULL)) {
1979 result = MS_NO_MEMORY_ERROR;
1983 btBlk1st = btBlk2nd = MS_LB_NOT_USED;
1986 for (TmpBlock = 0; TmpBlock < MS_MAX_INITIAL_ERROR_BLOCKS+2; TmpBlock++) {
1988 switch (ms_read_readpage(us, TmpBlock, 0, (u32 *)PageBuffer0, &extdat)) {
1989 case MS_STATUS_SUCCESS:
1991 case MS_STATUS_INT_ERROR:
1993 case MS_STATUS_ERROR:
1998 if ((extdat.ovrflg & MS_REG_OVR_BKST) == MS_REG_OVR_BKST_NG)
2001 if (((extdat.mngflg & MS_REG_MNG_SYSFLG) == MS_REG_MNG_SYSFLG_USER) ||
2002 (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wBlockID) != MS_BOOT_BLOCK_ID) ||
2003 (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wFormatVersion) != MS_BOOT_BLOCK_FORMAT_VERSION) ||
2004 (((struct ms_bootblock_page0 *)PageBuffer0)->header.bNumberOfDataEntry != MS_BOOT_BLOCK_DATA_ENTRIES))
2007 if (btBlk1st != MS_LB_NOT_USED) {
2008 btBlk2nd = TmpBlock;
2012 btBlk1st = TmpBlock;
2013 memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE);
2014 if (extdat.status1 & (MS_REG_ST1_DTER | MS_REG_ST1_EXER | MS_REG_ST1_FGER))
2018 if (btBlk1st == MS_LB_NOT_USED) {
2019 result = MS_STATUS_ERROR;
2024 if ((extdat.status0 & MS_REG_ST0_WP) == MS_REG_ST0_WP_ON)
2025 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2027 result = MS_STATUS_ERROR;
2028 /* 1st Boot Block */
2029 if (btBlk1stErred == 0)
2030 result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1);
2032 /* 2nd Boot Block */
2033 if (result && (btBlk2nd != MS_LB_NOT_USED))
2034 result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0);
2037 result = MS_STATUS_ERROR;
2041 for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++)
2042 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2044 info->MS_Lib.Phy2LogMap[btBlk1st] = MS_LB_BOOT_BLOCK;
2046 if (btBlk2nd != MS_LB_NOT_USED) {
2047 for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++)
2048 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2050 info->MS_Lib.Phy2LogMap[btBlk2nd] = MS_LB_BOOT_BLOCK;
2053 result = ms_lib_scan_logicalblocknumber(us, btBlk1st);
2057 for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT;
2058 TmpBlock < info->MS_Lib.NumberOfPhyBlock;
2059 TmpBlock += MS_PHYSICAL_BLOCKS_PER_SEGMENT) {
2060 if (ms_count_freeblock(us, TmpBlock) == 0) {
2061 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2067 if (ms_lib_alloc_writebuf(us)) {
2068 result = MS_NO_MEMORY_ERROR;
2072 result = MS_STATUS_SUCCESS;
2078 printk(KERN_INFO "MS_CardInit end\n");
2082 static int ene_ms_init(struct us_data *us)
2084 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2086 u16 MSP_BlockSize, MSP_UserAreaBlocks;
2087 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2088 u8 *bbuf = info->bbuf;
2090 printk(KERN_INFO "transport --- ENE_MSInit\n");
2092 /* the same part to test ENE */
2094 result = ene_load_bincode(us, MS_INIT_PATTERN);
2095 if (result != USB_STOR_XFER_GOOD) {
2096 printk(KERN_ERR "Load MS Init Code Fail !!\n");
2097 return USB_STOR_TRANSPORT_ERROR;
2100 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2101 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2102 bcb->DataTransferLength = 0x200;
2103 bcb->Flags = US_BULK_FLAG_IN;
2107 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
2108 if (result != USB_STOR_XFER_GOOD) {
2109 printk(KERN_ERR "Execution MS Init Code Fail !!\n");
2110 return USB_STOR_TRANSPORT_ERROR;
2112 /* the same part to test ENE */
2113 info->MS_Status = *(struct MS_STATUS *) bbuf;
2115 if (info->MS_Status.Insert && info->MS_Status.Ready) {
2116 printk(KERN_INFO "Insert = %x\n", info->MS_Status.Insert);
2117 printk(KERN_INFO "Ready = %x\n", info->MS_Status.Ready);
2118 printk(KERN_INFO "IsMSPro = %x\n", info->MS_Status.IsMSPro);
2119 printk(KERN_INFO "IsMSPHG = %x\n", info->MS_Status.IsMSPHG);
2120 printk(KERN_INFO "WtP= %x\n", info->MS_Status.WtP);
2121 if (info->MS_Status.IsMSPro) {
2122 MSP_BlockSize = (bbuf[6] << 8) | bbuf[7];
2123 MSP_UserAreaBlocks = (bbuf[10] << 8) | bbuf[11];
2124 info->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
2126 ms_card_init(us); /* Card is MS (to ms.c)*/
2128 usb_stor_dbg(us, "MS Init Code OK !!\n");
2130 usb_stor_dbg(us, "MS Card Not Ready --- %x\n", bbuf[0]);
2131 return USB_STOR_TRANSPORT_ERROR;
2134 return USB_STOR_TRANSPORT_GOOD;
2137 static int ene_sd_init(struct us_data *us)
2140 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2141 struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2142 u8 *bbuf = info->bbuf;
2144 usb_stor_dbg(us, "transport --- ENE_SDInit\n");
2145 /* SD Init Part-1 */
2146 result = ene_load_bincode(us, SD_INIT1_PATTERN);
2147 if (result != USB_STOR_XFER_GOOD) {
2148 usb_stor_dbg(us, "Load SD Init Code Part-1 Fail !!\n");
2149 return USB_STOR_TRANSPORT_ERROR;
2152 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2153 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2154 bcb->Flags = US_BULK_FLAG_IN;
2157 result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
2158 if (result != USB_STOR_XFER_GOOD) {
2159 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2160 return USB_STOR_TRANSPORT_ERROR;
2163 /* SD Init Part-2 */
2164 result = ene_load_bincode(us, SD_INIT2_PATTERN);
2165 if (result != USB_STOR_XFER_GOOD) {
2166 usb_stor_dbg(us, "Load SD Init Code Part-2 Fail !!\n");
2167 return USB_STOR_TRANSPORT_ERROR;
2170 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2171 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2172 bcb->DataTransferLength = 0x200;
2173 bcb->Flags = US_BULK_FLAG_IN;
2176 result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
2177 if (result != USB_STOR_XFER_GOOD) {
2178 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2179 return USB_STOR_TRANSPORT_ERROR;
2182 info->SD_Status = *(struct SD_STATUS *) bbuf;
2183 if (info->SD_Status.Insert && info->SD_Status.Ready) {
2184 struct SD_STATUS *s = &info->SD_Status;
2186 ene_get_card_status(us, bbuf);
2187 usb_stor_dbg(us, "Insert = %x\n", s->Insert);
2188 usb_stor_dbg(us, "Ready = %x\n", s->Ready);
2189 usb_stor_dbg(us, "IsMMC = %x\n", s->IsMMC);
2190 usb_stor_dbg(us, "HiCapacity = %x\n", s->HiCapacity);
2191 usb_stor_dbg(us, "HiSpeed = %x\n", s->HiSpeed);
2192 usb_stor_dbg(us, "WtP = %x\n", s->WtP);
2194 usb_stor_dbg(us, "SD Card Not Ready --- %x\n", bbuf[0]);
2195 return USB_STOR_TRANSPORT_ERROR;
2197 return USB_STOR_TRANSPORT_GOOD;
2201 static int ene_init(struct us_data *us)
2205 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2206 u8 *bbuf = info->bbuf;
2208 result = ene_get_card_type(us, REG_CARD_STATUS, bbuf);
2209 if (result != USB_STOR_XFER_GOOD)
2210 return USB_STOR_TRANSPORT_ERROR;
2212 misc_reg03 = bbuf[0];
2213 if (misc_reg03 & 0x01) {
2214 if (!info->SD_Status.Ready) {
2215 result = ene_sd_init(us);
2216 if (result != USB_STOR_XFER_GOOD)
2217 return USB_STOR_TRANSPORT_ERROR;
2220 if (misc_reg03 & 0x02) {
2221 if (!info->MS_Status.Ready) {
2222 result = ene_ms_init(us);
2223 if (result != USB_STOR_XFER_GOOD)
2224 return USB_STOR_TRANSPORT_ERROR;
2230 /*----- sd_scsi_irp() ---------*/
2231 static int sd_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
2234 struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2236 info->SrbStatus = SS_SUCCESS;
2237 switch (srb->cmnd[0]) {
2238 case TEST_UNIT_READY:
2239 result = sd_scsi_test_unit_ready(us, srb);
2242 result = sd_scsi_inquiry(us, srb);
2245 result = sd_scsi_mode_sense(us, srb);
2249 result = SD_SCSI_Start_Stop(us, srb);
2253 result = sd_scsi_read_capacity(us, srb);
2256 result = sd_scsi_read(us, srb);
2259 result = sd_scsi_write(us, srb);
2262 info->SrbStatus = SS_ILLEGAL_REQUEST;
2263 result = USB_STOR_TRANSPORT_FAILED;
2272 static int ms_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
2275 struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2276 info->SrbStatus = SS_SUCCESS;
2277 switch (srb->cmnd[0]) {
2278 case TEST_UNIT_READY:
2279 result = ms_scsi_test_unit_ready(us, srb);
2282 result = ms_scsi_inquiry(us, srb);
2285 result = ms_scsi_mode_sense(us, srb);
2288 result = ms_scsi_read_capacity(us, srb);
2291 result = ms_scsi_read(us, srb);
2294 result = ms_scsi_write(us, srb);
2297 info->SrbStatus = SS_ILLEGAL_REQUEST;
2298 result = USB_STOR_TRANSPORT_FAILED;
2304 static int ene_transport(struct scsi_cmnd *srb, struct us_data *us)
2306 int result = USB_STOR_XFER_GOOD;
2307 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2309 /*US_DEBUG(usb_stor_show_command(us, srb)); */
2310 scsi_set_resid(srb, 0);
2311 if (unlikely(!(info->SD_Status.Ready || info->MS_Status.Ready)))
2312 result = ene_init(us);
2313 if (result == USB_STOR_XFER_GOOD) {
2314 result = USB_STOR_TRANSPORT_ERROR;
2315 if (info->SD_Status.Ready)
2316 result = sd_scsi_irp(us, srb);
2318 if (info->MS_Status.Ready)
2319 result = ms_scsi_irp(us, srb);
2324 static struct scsi_host_template ene_ub6250_host_template;
2326 static int ene_ub6250_probe(struct usb_interface *intf,
2327 const struct usb_device_id *id)
2332 struct ene_ub6250_info *info;
2334 result = usb_stor_probe1(&us, intf, id,
2335 (id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list,
2336 &ene_ub6250_host_template);
2340 /* FIXME: where should the code alloc extra buf ? */
2341 us->extra = kzalloc(sizeof(struct ene_ub6250_info), GFP_KERNEL);
2344 us->extra_destructor = ene_ub6250_info_destructor;
2346 info = (struct ene_ub6250_info *)(us->extra);
2347 info->bbuf = kmalloc(512, GFP_KERNEL);
2353 us->transport_name = "ene_ub6250";
2354 us->transport = ene_transport;
2357 result = usb_stor_probe2(us);
2361 /* probe card type */
2362 result = ene_get_card_type(us, REG_CARD_STATUS, info->bbuf);
2363 if (result != USB_STOR_XFER_GOOD) {
2364 usb_stor_disconnect(intf);
2365 return USB_STOR_TRANSPORT_ERROR;
2368 misc_reg03 = info->bbuf[0];
2369 if (!(misc_reg03 & 0x01)) {
2370 pr_info("ums_eneub6250: This driver only supports SD/MS cards. "
2371 "It does not support SM cards.\n");
2380 static int ene_ub6250_resume(struct usb_interface *iface)
2383 struct us_data *us = usb_get_intfdata(iface);
2384 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2386 mutex_lock(&us->dev_mutex);
2388 if (us->suspend_resume_hook)
2389 (us->suspend_resume_hook)(us, US_RESUME);
2391 mutex_unlock(&us->dev_mutex);
2393 info->Power_IsResum = true;
2394 /*info->SD_Status.Ready = 0; */
2395 info->SD_Status = *(struct SD_STATUS *)&tmp;
2396 info->MS_Status = *(struct MS_STATUS *)&tmp;
2397 info->SM_Status = *(struct SM_STATUS *)&tmp;
2402 static int ene_ub6250_reset_resume(struct usb_interface *iface)
2405 struct us_data *us = usb_get_intfdata(iface);
2406 struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2408 /* Report the reset to the SCSI core */
2409 usb_stor_reset_resume(iface);
2411 /* FIXME: Notify the subdrivers that they need to reinitialize
2413 info->Power_IsResum = true;
2414 /*info->SD_Status.Ready = 0; */
2415 info->SD_Status = *(struct SD_STATUS *)&tmp;
2416 info->MS_Status = *(struct MS_STATUS *)&tmp;
2417 info->SM_Status = *(struct SM_STATUS *)&tmp;
2424 #define ene_ub6250_resume NULL
2425 #define ene_ub6250_reset_resume NULL
2429 static struct usb_driver ene_ub6250_driver = {
2431 .probe = ene_ub6250_probe,
2432 .disconnect = usb_stor_disconnect,
2433 .suspend = usb_stor_suspend,
2434 .resume = ene_ub6250_resume,
2435 .reset_resume = ene_ub6250_reset_resume,
2436 .pre_reset = usb_stor_pre_reset,
2437 .post_reset = usb_stor_post_reset,
2438 .id_table = ene_ub6250_usb_ids,
2443 module_usb_stor_driver(ene_ub6250_driver, ene_ub6250_host_template, DRV_NAME);