GNU Linux-libre 5.16.19-gnu
[releases.git] / drivers / usb / storage / ene_ub6250.c
1 // SPDX-License-Identifier: GPL-2.0+
2 #include <linux/jiffies.h>
3 #include <linux/errno.h>
4 #include <linux/module.h>
5 #include <linux/slab.h>
6
7 #include <scsi/scsi.h>
8 #include <scsi/scsi_cmnd.h>
9
10 #include <linux/firmware.h>
11
12 #include "usb.h"
13 #include "transport.h"
14 #include "protocol.h"
15 #include "debug.h"
16 #include "scsiglue.h"
17
18 #define SD_INIT1_FIRMWARE "/*(DEBLOBBED)*/"
19 #define SD_INIT2_FIRMWARE "/*(DEBLOBBED)*/"
20 #define SD_RW_FIRMWARE "/*(DEBLOBBED)*/"
21 #define MS_INIT_FIRMWARE "/*(DEBLOBBED)*/"
22 #define MSP_RW_FIRMWARE "/*(DEBLOBBED)*/"
23 #define MS_RW_FIRMWARE "/*(DEBLOBBED)*/"
24
25 #define DRV_NAME "ums_eneub6250"
26
27 MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
28 MODULE_LICENSE("GPL");
29 MODULE_IMPORT_NS(USB_STORAGE);
30 /*(DEBLOBBED)*/
31
32 /*
33  * The table of devices
34  */
35 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
36                     vendorName, productName, useProtocol, useTransport, \
37                     initFunction, flags) \
38 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
39         .driver_info = (flags)}
40
41 static struct usb_device_id ene_ub6250_usb_ids[] = {
42 #       include "unusual_ene_ub6250.h"
43         { }             /* Terminating entry */
44 };
45 MODULE_DEVICE_TABLE(usb, ene_ub6250_usb_ids);
46
47 #undef UNUSUAL_DEV
48
49 /*
50  * The flags table
51  */
52 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
53                     vendor_name, product_name, use_protocol, use_transport, \
54                     init_function, Flags) \
55 { \
56         .vendorName = vendor_name,      \
57         .productName = product_name,    \
58         .useProtocol = use_protocol,    \
59         .useTransport = use_transport,  \
60         .initFunction = init_function,  \
61 }
62
63 static struct us_unusual_dev ene_ub6250_unusual_dev_list[] = {
64 #       include "unusual_ene_ub6250.h"
65         { }             /* Terminating entry */
66 };
67
68 #undef UNUSUAL_DEV
69
70
71
72 /* ENE bin code len */
73 #define ENE_BIN_CODE_LEN    0x800
74 /* EnE HW Register */
75 #define REG_CARD_STATUS     0xFF83
76 #define REG_HW_TRAP1        0xFF89
77
78 /* SRB Status */
79 #define SS_SUCCESS              0x000000        /* No Sense */
80 #define SS_NOT_READY            0x023A00        /* Medium not present */
81 #define SS_MEDIUM_ERR           0x031100        /* Unrecovered read error */
82 #define SS_HW_ERR               0x040800        /* Communication failure */
83 #define SS_ILLEGAL_REQUEST      0x052000        /* Invalid command */
84 #define SS_UNIT_ATTENTION       0x062900        /* Reset occurred */
85
86 /* ENE Load FW Pattern */
87 #define SD_INIT1_PATTERN   1
88 #define SD_INIT2_PATTERN   2
89 #define SD_RW_PATTERN      3
90 #define MS_INIT_PATTERN    4
91 #define MSP_RW_PATTERN     5
92 #define MS_RW_PATTERN      6
93 #define SM_INIT_PATTERN    7
94 #define SM_RW_PATTERN      8
95
96 #define FDIR_WRITE         0
97 #define FDIR_READ          1
98
99 /* For MS Card */
100
101 /* Status Register 1 */
102 #define MS_REG_ST1_MB           0x80    /* media busy */
103 #define MS_REG_ST1_FB1          0x40    /* flush busy 1 */
104 #define MS_REG_ST1_DTER         0x20    /* error on data(corrected) */
105 #define MS_REG_ST1_UCDT         0x10    /* unable to correct data */
106 #define MS_REG_ST1_EXER         0x08    /* error on extra(corrected) */
107 #define MS_REG_ST1_UCEX         0x04    /* unable to correct extra */
108 #define MS_REG_ST1_FGER         0x02    /* error on overwrite flag(corrected) */
109 #define MS_REG_ST1_UCFG         0x01    /* unable to correct overwrite flag */
110 #define MS_REG_ST1_DEFAULT      (MS_REG_ST1_MB | MS_REG_ST1_FB1 | MS_REG_ST1_DTER | MS_REG_ST1_UCDT | MS_REG_ST1_EXER | MS_REG_ST1_UCEX | MS_REG_ST1_FGER | MS_REG_ST1_UCFG)
111
112 /* Overwrite Area */
113 #define MS_REG_OVR_BKST         0x80            /* block status */
114 #define MS_REG_OVR_BKST_OK      MS_REG_OVR_BKST     /* OK */
115 #define MS_REG_OVR_BKST_NG      0x00            /* NG */
116 #define MS_REG_OVR_PGST0        0x40            /* page status */
117 #define MS_REG_OVR_PGST1        0x20
118 #define MS_REG_OVR_PGST_MASK    (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1)
119 #define MS_REG_OVR_PGST_OK      (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1) /* OK */
120 #define MS_REG_OVR_PGST_NG      MS_REG_OVR_PGST1                      /* NG */
121 #define MS_REG_OVR_PGST_DATA_ERROR      0x00        /* data error */
122 #define MS_REG_OVR_UDST                 0x10        /* update status */
123 #define MS_REG_OVR_UDST_UPDATING        0x00        /* updating */
124 #define MS_REG_OVR_UDST_NO_UPDATE       MS_REG_OVR_UDST
125 #define MS_REG_OVR_RESERVED     0x08
126 #define MS_REG_OVR_DEFAULT      (MS_REG_OVR_BKST_OK | MS_REG_OVR_PGST_OK | MS_REG_OVR_UDST_NO_UPDATE | MS_REG_OVR_RESERVED)
127
128 /* Management Flag */
129 #define MS_REG_MNG_SCMS0        0x20    /* serial copy management system */
130 #define MS_REG_MNG_SCMS1        0x10
131 #define MS_REG_MNG_SCMS_MASK            (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
132 #define MS_REG_MNG_SCMS_COPY_OK         (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
133 #define MS_REG_MNG_SCMS_ONE_COPY        MS_REG_MNG_SCMS1
134 #define MS_REG_MNG_SCMS_NO_COPY 0x00
135 #define MS_REG_MNG_ATFLG        0x08    /* address transfer table flag */
136 #define MS_REG_MNG_ATFLG_OTHER  MS_REG_MNG_ATFLG    /* other */
137 #define MS_REG_MNG_ATFLG_ATTBL  0x00    /* address transfer table */
138 #define MS_REG_MNG_SYSFLG       0x04    /* system flag */
139 #define MS_REG_MNG_SYSFLG_USER  MS_REG_MNG_SYSFLG   /* user block */
140 #define MS_REG_MNG_SYSFLG_BOOT  0x00    /* system block */
141 #define MS_REG_MNG_RESERVED     0xc3
142 #define MS_REG_MNG_DEFAULT      (MS_REG_MNG_SCMS_COPY_OK | MS_REG_MNG_ATFLG_OTHER | MS_REG_MNG_SYSFLG_USER | MS_REG_MNG_RESERVED)
143
144
145 #define MS_MAX_PAGES_PER_BLOCK          32
146 #define MS_MAX_INITIAL_ERROR_BLOCKS     10
147 #define MS_LIB_BITS_PER_BYTE            8
148
149 #define MS_SYSINF_FORMAT_FAT            1
150 #define MS_SYSINF_USAGE_GENERAL         0
151
152 #define MS_SYSINF_MSCLASS_TYPE_1        1
153 #define MS_SYSINF_PAGE_SIZE             MS_BYTES_PER_PAGE /* fixed */
154
155 #define MS_SYSINF_CARDTYPE_RDONLY       1
156 #define MS_SYSINF_CARDTYPE_RDWR         2
157 #define MS_SYSINF_CARDTYPE_HYBRID       3
158 #define MS_SYSINF_SECURITY              0x01
159 #define MS_SYSINF_SECURITY_NO_SUPPORT   MS_SYSINF_SECURITY
160 #define MS_SYSINF_SECURITY_SUPPORT      0
161
162 #define MS_SYSINF_RESERVED1             1
163 #define MS_SYSINF_RESERVED2             1
164
165 #define MS_SYSENT_TYPE_INVALID_BLOCK    0x01
166 #define MS_SYSENT_TYPE_CIS_IDI          0x0a    /* CIS/IDI */
167
168 #define SIZE_OF_KIRO            1024
169 #define BYTE_MASK               0xff
170
171 /* ms error code */
172 #define MS_STATUS_WRITE_PROTECT 0x0106
173 #define MS_STATUS_SUCCESS       0x0000
174 #define MS_ERROR_FLASH_READ     0x8003
175 #define MS_ERROR_FLASH_ERASE    0x8005
176 #define MS_LB_ERROR             0xfff0
177 #define MS_LB_BOOT_BLOCK        0xfff1
178 #define MS_LB_INITIAL_ERROR     0xfff2
179 #define MS_STATUS_SUCCESS_WITH_ECC 0xfff3
180 #define MS_LB_ACQUIRED_ERROR    0xfff4
181 #define MS_LB_NOT_USED_ERASED   0xfff5
182 #define MS_NOCARD_ERROR         0xfff8
183 #define MS_NO_MEMORY_ERROR      0xfff9
184 #define MS_STATUS_INT_ERROR     0xfffa
185 #define MS_STATUS_ERROR         0xfffe
186 #define MS_LB_NOT_USED          0xffff
187
188 #define MS_REG_MNG_SYSFLG       0x04    /* system flag */
189 #define MS_REG_MNG_SYSFLG_USER  MS_REG_MNG_SYSFLG   /* user block */
190
191 #define MS_BOOT_BLOCK_ID                        0x0001
192 #define MS_BOOT_BLOCK_FORMAT_VERSION            0x0100
193 #define MS_BOOT_BLOCK_DATA_ENTRIES              2
194
195 #define MS_NUMBER_OF_SYSTEM_ENTRY               4
196 #define MS_NUMBER_OF_BOOT_BLOCK                 2
197 #define MS_BYTES_PER_PAGE                       512
198 #define MS_LOGICAL_BLOCKS_PER_SEGMENT           496
199 #define MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT        494
200
201 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT          0x200 /* 512 */
202 #define MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK     0x1ff
203
204 /* overwrite area */
205 #define MS_REG_OVR_BKST         0x80            /* block status */
206 #define MS_REG_OVR_BKST_OK      MS_REG_OVR_BKST /* OK */
207 #define MS_REG_OVR_BKST_NG      0x00            /* NG */
208
209 /* Status Register 1 */
210 #define MS_REG_ST1_DTER         0x20    /* error on data(corrected) */
211 #define MS_REG_ST1_EXER         0x08    /* error on extra(corrected) */
212 #define MS_REG_ST1_FGER         0x02    /* error on overwrite flag(corrected) */
213
214 /* MemoryStick Register */
215 /* Status Register 0 */
216 #define MS_REG_ST0_WP           0x01    /* write protected */
217 #define MS_REG_ST0_WP_ON        MS_REG_ST0_WP
218
219 #define MS_LIB_CTRL_RDONLY      0
220 #define MS_LIB_CTRL_WRPROTECT   1
221
222 /*dphy->log table */
223 #define ms_libconv_to_logical(pdx, PhyBlock) (((PhyBlock) >= (pdx)->MS_Lib.NumberOfPhyBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Phy2LogMap[PhyBlock])
224 #define ms_libconv_to_physical(pdx, LogBlock) (((LogBlock) >= (pdx)->MS_Lib.NumberOfLogBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Log2PhyMap[LogBlock])
225
226 #define ms_lib_ctrl_set(pdx, Flag)      ((pdx)->MS_Lib.flags |= (1 << (Flag)))
227 #define ms_lib_ctrl_reset(pdx, Flag)    ((pdx)->MS_Lib.flags &= ~(1 << (Flag)))
228 #define ms_lib_ctrl_check(pdx, Flag)    ((pdx)->MS_Lib.flags & (1 << (Flag)))
229
230 #define ms_lib_iswritable(pdx) ((ms_lib_ctrl_check((pdx), MS_LIB_CTRL_RDONLY) == 0) && (ms_lib_ctrl_check(pdx, MS_LIB_CTRL_WRPROTECT) == 0))
231 #define ms_lib_clear_pagemap(pdx) memset((pdx)->MS_Lib.pagemap, 0, sizeof((pdx)->MS_Lib.pagemap))
232 #define memstick_logaddr(logadr1, logadr0) ((((u16)(logadr1)) << 8) | (logadr0))
233
234
235 /* SD_STATUS bits */
236 #define SD_Insert       BIT(0)
237 #define SD_Ready        BIT(1)
238 #define SD_MediaChange  BIT(2)
239 #define SD_IsMMC        BIT(3)
240 #define SD_HiCapacity   BIT(4)
241 #define SD_HiSpeed      BIT(5)
242 #define SD_WtP          BIT(6)
243                         /* Bit 7 reserved */
244
245 /* MS_STATUS bits */
246 #define MS_Insert       BIT(0)
247 #define MS_Ready        BIT(1)
248 #define MS_MediaChange  BIT(2)
249 #define MS_IsMSPro      BIT(3)
250 #define MS_IsMSPHG      BIT(4)
251                         /* Bit 5 reserved */
252 #define MS_WtP          BIT(6)
253                         /* Bit 7 reserved */
254
255 /* SM_STATUS bits */
256 #define SM_Insert       BIT(0)
257 #define SM_Ready        BIT(1)
258 #define SM_MediaChange  BIT(2)
259                         /* Bits 3-5 reserved */
260 #define SM_WtP          BIT(6)
261 #define SM_IsMS         BIT(7)
262
263 struct ms_bootblock_cis {
264         u8 bCistplDEVICE[6];    /* 0 */
265         u8 bCistplDEVICE0C[6];  /* 6 */
266         u8 bCistplJEDECC[4];    /* 12 */
267         u8 bCistplMANFID[6];    /* 16 */
268         u8 bCistplVER1[32];     /* 22 */
269         u8 bCistplFUNCID[4];    /* 54 */
270         u8 bCistplFUNCE0[4];    /* 58 */
271         u8 bCistplFUNCE1[5];    /* 62 */
272         u8 bCistplCONF[7];      /* 67 */
273         u8 bCistplCFTBLENT0[10];/* 74 */
274         u8 bCistplCFTBLENT1[8]; /* 84 */
275         u8 bCistplCFTBLENT2[12];/* 92 */
276         u8 bCistplCFTBLENT3[8]; /* 104 */
277         u8 bCistplCFTBLENT4[17];/* 112 */
278         u8 bCistplCFTBLENT5[8]; /* 129 */
279         u8 bCistplCFTBLENT6[17];/* 137 */
280         u8 bCistplCFTBLENT7[8]; /* 154 */
281         u8 bCistplNOLINK[3];    /* 162 */
282 } ;
283
284 struct ms_bootblock_idi {
285 #define MS_IDI_GENERAL_CONF 0x848A
286         u16 wIDIgeneralConfiguration;   /* 0 */
287         u16 wIDInumberOfCylinder;       /* 1 */
288         u16 wIDIreserved0;              /* 2 */
289         u16 wIDInumberOfHead;           /* 3 */
290         u16 wIDIbytesPerTrack;          /* 4 */
291         u16 wIDIbytesPerSector;         /* 5 */
292         u16 wIDIsectorsPerTrack;        /* 6 */
293         u16 wIDItotalSectors[2];        /* 7-8  high,low */
294         u16 wIDIreserved1[11];          /* 9-19 */
295         u16 wIDIbufferType;             /* 20 */
296         u16 wIDIbufferSize;             /* 21 */
297         u16 wIDIlongCmdECC;             /* 22 */
298         u16 wIDIfirmVersion[4];         /* 23-26 */
299         u16 wIDImodelName[20];          /* 27-46 */
300         u16 wIDIreserved2;              /* 47 */
301         u16 wIDIlongWordSupported;      /* 48 */
302         u16 wIDIdmaSupported;           /* 49 */
303         u16 wIDIreserved3;              /* 50 */
304         u16 wIDIpioTiming;              /* 51 */
305         u16 wIDIdmaTiming;              /* 52 */
306         u16 wIDItransferParameter;      /* 53 */
307         u16 wIDIformattedCylinder;      /* 54 */
308         u16 wIDIformattedHead;          /* 55 */
309         u16 wIDIformattedSectorsPerTrack;/* 56 */
310         u16 wIDIformattedTotalSectors[2];/* 57-58 */
311         u16 wIDImultiSector;            /* 59 */
312         u16 wIDIlbaSectors[2];          /* 60-61 */
313         u16 wIDIsingleWordDMA;          /* 62 */
314         u16 wIDImultiWordDMA;           /* 63 */
315         u16 wIDIreserved4[192];         /* 64-255 */
316 };
317
318 struct ms_bootblock_sysent_rec {
319         u32 dwStart;
320         u32 dwSize;
321         u8 bType;
322         u8 bReserved[3];
323 };
324
325 struct ms_bootblock_sysent {
326         struct ms_bootblock_sysent_rec entry[MS_NUMBER_OF_SYSTEM_ENTRY];
327 };
328
329 struct ms_bootblock_sysinf {
330         u8 bMsClass;                    /* must be 1 */
331         u8 bCardType;                   /* see below */
332         u16 wBlockSize;                 /* n KB */
333         u16 wBlockNumber;               /* number of physical block */
334         u16 wTotalBlockNumber;          /* number of logical block */
335         u16 wPageSize;                  /* must be 0x200 */
336         u8 bExtraSize;                  /* 0x10 */
337         u8 bSecuritySupport;
338         u8 bAssemblyDate[8];
339         u8 bFactoryArea[4];
340         u8 bAssemblyMakerCode;
341         u8 bAssemblyMachineCode[3];
342         u16 wMemoryMakerCode;
343         u16 wMemoryDeviceCode;
344         u16 wMemorySize;
345         u8 bReserved1;
346         u8 bReserved2;
347         u8 bVCC;
348         u8 bVPP;
349         u16 wControllerChipNumber;
350         u16 wControllerFunction;        /* New MS */
351         u8 bReserved3[9];               /* New MS */
352         u8 bParallelSupport;            /* New MS */
353         u16 wFormatValue;               /* New MS */
354         u8 bFormatType;
355         u8 bUsage;
356         u8 bDeviceType;
357         u8 bReserved4[22];
358         u8 bFUValue3;
359         u8 bFUValue4;
360         u8 bReserved5[15];
361 };
362
363 struct ms_bootblock_header {
364         u16 wBlockID;
365         u16 wFormatVersion;
366         u8 bReserved1[184];
367         u8 bNumberOfDataEntry;
368         u8 bReserved2[179];
369 };
370
371 struct ms_bootblock_page0 {
372         struct ms_bootblock_header header;
373         struct ms_bootblock_sysent sysent;
374         struct ms_bootblock_sysinf sysinf;
375 };
376
377 struct ms_bootblock_cis_idi {
378         union {
379                 struct ms_bootblock_cis cis;
380                 u8 dmy[256];
381         } cis;
382
383         union {
384                 struct ms_bootblock_idi idi;
385                 u8 dmy[256];
386         } idi;
387
388 };
389
390 /* ENE MS Lib struct */
391 struct ms_lib_type_extdat {
392         u8 reserved;
393         u8 intr;
394         u8 status0;
395         u8 status1;
396         u8 ovrflg;
397         u8 mngflg;
398         u16 logadr;
399 };
400
401 struct ms_lib_ctrl {
402         u32 flags;
403         u32 BytesPerSector;
404         u32 NumberOfCylinder;
405         u32 SectorsPerCylinder;
406         u16 cardType;                   /* R/W, RO, Hybrid */
407         u16 blockSize;
408         u16 PagesPerBlock;
409         u16 NumberOfPhyBlock;
410         u16 NumberOfLogBlock;
411         u16 NumberOfSegment;
412         u16 *Phy2LogMap;                /* phy2log table */
413         u16 *Log2PhyMap;                /* log2phy table */
414         u16 wrtblk;
415         unsigned char *pagemap[(MS_MAX_PAGES_PER_BLOCK + (MS_LIB_BITS_PER_BYTE-1)) / MS_LIB_BITS_PER_BYTE];
416         unsigned char *blkpag;
417         struct ms_lib_type_extdat *blkext;
418         unsigned char copybuf[512];
419 };
420
421
422 /* SD Block Length */
423 /* 2^9 = 512 Bytes, The HW maximum read/write data length */
424 #define SD_BLOCK_LEN  9
425
426 struct ene_ub6250_info {
427
428         /* I/O bounce buffer */
429         u8              *bbuf;
430
431         /* for 6250 code */
432         u8              SD_Status;
433         u8              MS_Status;
434         u8              SM_Status;
435
436         /* ----- SD Control Data ---------------- */
437         /*SD_REGISTER SD_Regs; */
438         u16             SD_Block_Mult;
439         u8              SD_READ_BL_LEN;
440         u16             SD_C_SIZE;
441         u8              SD_C_SIZE_MULT;
442
443         /* SD/MMC New spec. */
444         u8              SD_SPEC_VER;
445         u8              SD_CSD_VER;
446         u8              SD20_HIGH_CAPACITY;
447         u32             HC_C_SIZE;
448         u8              MMC_SPEC_VER;
449         u8              MMC_BusWidth;
450         u8              MMC_HIGH_CAPACITY;
451
452         /*----- MS Control Data ---------------- */
453         bool            MS_SWWP;
454         u32             MSP_TotalBlock;
455         struct ms_lib_ctrl MS_Lib;
456         bool            MS_IsRWPage;
457         u16             MS_Model;
458
459         /*----- SM Control Data ---------------- */
460         u8              SM_DeviceID;
461         u8              SM_CardID;
462
463         unsigned char   *testbuf;
464         u8              BIN_FLAG;
465         u32             bl_num;
466         int             SrbStatus;
467
468         /*------Power Managerment ---------------*/
469         bool            Power_IsResum;
470 };
471
472 static int ene_sd_init(struct us_data *us);
473 static int ene_ms_init(struct us_data *us);
474 static int ene_load_bincode(struct us_data *us, unsigned char flag);
475
476 static void ene_ub6250_info_destructor(void *extra)
477 {
478         struct ene_ub6250_info *info = (struct ene_ub6250_info *) extra;
479
480         if (!extra)
481                 return;
482         kfree(info->bbuf);
483 }
484
485 static int ene_send_scsi_cmd(struct us_data *us, u8 fDir, void *buf, int use_sg)
486 {
487         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
488         struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
489
490         int result;
491         unsigned int residue;
492         unsigned int cswlen = 0, partial = 0;
493         unsigned int transfer_length = bcb->DataTransferLength;
494
495         /* usb_stor_dbg(us, "transport --- ene_send_scsi_cmd\n"); */
496         /* send cmd to out endpoint */
497         result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
498                                             bcb, US_BULK_CB_WRAP_LEN, NULL);
499         if (result != USB_STOR_XFER_GOOD) {
500                 usb_stor_dbg(us, "send cmd to out endpoint fail ---\n");
501                 return USB_STOR_TRANSPORT_ERROR;
502         }
503
504         if (buf) {
505                 unsigned int pipe = fDir;
506
507                 if (fDir  == FDIR_READ)
508                         pipe = us->recv_bulk_pipe;
509                 else
510                         pipe = us->send_bulk_pipe;
511
512                 /* Bulk */
513                 if (use_sg) {
514                         result = usb_stor_bulk_srb(us, pipe, us->srb);
515                 } else {
516                         result = usb_stor_bulk_transfer_sg(us, pipe, buf,
517                                                 transfer_length, 0, &partial);
518                 }
519                 if (result != USB_STOR_XFER_GOOD) {
520                         usb_stor_dbg(us, "data transfer fail ---\n");
521                         return USB_STOR_TRANSPORT_ERROR;
522                 }
523         }
524
525         /* Get CSW for device status */
526         result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
527                                             US_BULK_CS_WRAP_LEN, &cswlen);
528
529         if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
530                 usb_stor_dbg(us, "Received 0-length CSW; retrying...\n");
531                 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
532                                             bcs, US_BULK_CS_WRAP_LEN, &cswlen);
533         }
534
535         if (result == USB_STOR_XFER_STALLED) {
536                 /* get the status again */
537                 usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n");
538                 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
539                                                 bcs, US_BULK_CS_WRAP_LEN, NULL);
540         }
541
542         if (result != USB_STOR_XFER_GOOD)
543                 return USB_STOR_TRANSPORT_ERROR;
544
545         /* check bulk status */
546         residue = le32_to_cpu(bcs->Residue);
547
548         /*
549          * try to compute the actual residue, based on how much data
550          * was really transferred and what the device tells us
551          */
552         if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
553                 residue = min(residue, transfer_length);
554                 if (us->srb != NULL)
555                         scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
556                                                                 residue));
557         }
558
559         if (bcs->Status != US_BULK_STAT_OK)
560                 return USB_STOR_TRANSPORT_ERROR;
561
562         return USB_STOR_TRANSPORT_GOOD;
563 }
564
565 static int do_scsi_request_sense(struct us_data *us, struct scsi_cmnd *srb)
566 {
567         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
568         unsigned char buf[18];
569
570         memset(buf, 0, 18);
571         buf[0] = 0x70;                          /* Current error */
572         buf[2] = info->SrbStatus >> 16;         /* Sense key */
573         buf[7] = 10;                            /* Additional length */
574         buf[12] = info->SrbStatus >> 8;         /* ASC */
575         buf[13] = info->SrbStatus;              /* ASCQ */
576
577         usb_stor_set_xfer_buf(buf, sizeof(buf), srb);
578         return USB_STOR_TRANSPORT_GOOD;
579 }
580
581 static int do_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
582 {
583         unsigned char data_ptr[36] = {
584                 0x00, 0x00, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
585                 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
586                 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
587                 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30 };
588
589         usb_stor_set_xfer_buf(data_ptr, 36, srb);
590         return USB_STOR_TRANSPORT_GOOD;
591 }
592
593 static int sd_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
594 {
595         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
596
597         if ((info->SD_Status & SD_Insert) && (info->SD_Status & SD_Ready))
598                 return USB_STOR_TRANSPORT_GOOD;
599         else {
600                 ene_sd_init(us);
601                 return USB_STOR_TRANSPORT_GOOD;
602         }
603
604         return USB_STOR_TRANSPORT_GOOD;
605 }
606
607 static int sd_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
608 {
609         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
610         unsigned char mediaNoWP[12] = {
611                 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
612                 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
613         unsigned char mediaWP[12]   = {
614                 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
615                 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
616
617         if (info->SD_Status & SD_WtP)
618                 usb_stor_set_xfer_buf(mediaWP, 12, srb);
619         else
620                 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
621
622
623         return USB_STOR_TRANSPORT_GOOD;
624 }
625
626 static int sd_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
627 {
628         u32     bl_num;
629         u32     bl_len;
630         unsigned int offset = 0;
631         unsigned char    buf[8];
632         struct scatterlist *sg = NULL;
633         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
634
635         usb_stor_dbg(us, "sd_scsi_read_capacity\n");
636         if (info->SD_Status & SD_HiCapacity) {
637                 bl_len = 0x200;
638                 if (info->SD_Status & SD_IsMMC)
639                         bl_num = info->HC_C_SIZE-1;
640                 else
641                         bl_num = (info->HC_C_SIZE + 1) * 1024 - 1;
642         } else {
643                 bl_len = 1 << (info->SD_READ_BL_LEN);
644                 bl_num = info->SD_Block_Mult * (info->SD_C_SIZE + 1)
645                                 * (1 << (info->SD_C_SIZE_MULT + 2)) - 1;
646         }
647         info->bl_num = bl_num;
648         usb_stor_dbg(us, "bl_len = %x\n", bl_len);
649         usb_stor_dbg(us, "bl_num = %x\n", bl_num);
650
651         /*srb->request_bufflen = 8; */
652         buf[0] = (bl_num >> 24) & 0xff;
653         buf[1] = (bl_num >> 16) & 0xff;
654         buf[2] = (bl_num >> 8) & 0xff;
655         buf[3] = (bl_num >> 0) & 0xff;
656         buf[4] = (bl_len >> 24) & 0xff;
657         buf[5] = (bl_len >> 16) & 0xff;
658         buf[6] = (bl_len >> 8) & 0xff;
659         buf[7] = (bl_len >> 0) & 0xff;
660
661         usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
662
663         return USB_STOR_TRANSPORT_GOOD;
664 }
665
666 static int sd_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
667 {
668         int result;
669         unsigned char *cdb = srb->cmnd;
670         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
671         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
672
673         u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
674                  ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
675         u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
676         u32 bnByte = bn * 0x200;
677         u32 blenByte = blen * 0x200;
678
679         if (bn > info->bl_num)
680                 return USB_STOR_TRANSPORT_ERROR;
681
682         result = ene_load_bincode(us, SD_RW_PATTERN);
683         if (result != USB_STOR_XFER_GOOD) {
684                 usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
685                 return USB_STOR_TRANSPORT_ERROR;
686         }
687
688         if (info->SD_Status & SD_HiCapacity)
689                 bnByte = bn;
690
691         /* set up the command wrapper */
692         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
693         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
694         bcb->DataTransferLength = blenByte;
695         bcb->Flags  = US_BULK_FLAG_IN;
696         bcb->CDB[0] = 0xF1;
697         bcb->CDB[5] = (unsigned char)(bnByte);
698         bcb->CDB[4] = (unsigned char)(bnByte>>8);
699         bcb->CDB[3] = (unsigned char)(bnByte>>16);
700         bcb->CDB[2] = (unsigned char)(bnByte>>24);
701
702         result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
703         return result;
704 }
705
706 static int sd_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
707 {
708         int result;
709         unsigned char *cdb = srb->cmnd;
710         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
711         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
712
713         u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
714                  ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
715         u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
716         u32 bnByte = bn * 0x200;
717         u32 blenByte = blen * 0x200;
718
719         if (bn > info->bl_num)
720                 return USB_STOR_TRANSPORT_ERROR;
721
722         result = ene_load_bincode(us, SD_RW_PATTERN);
723         if (result != USB_STOR_XFER_GOOD) {
724                 usb_stor_dbg(us, "Load SD RW pattern Fail !!\n");
725                 return USB_STOR_TRANSPORT_ERROR;
726         }
727
728         if (info->SD_Status & SD_HiCapacity)
729                 bnByte = bn;
730
731         /* set up the command wrapper */
732         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
733         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
734         bcb->DataTransferLength = blenByte;
735         bcb->Flags  = 0x00;
736         bcb->CDB[0] = 0xF0;
737         bcb->CDB[5] = (unsigned char)(bnByte);
738         bcb->CDB[4] = (unsigned char)(bnByte>>8);
739         bcb->CDB[3] = (unsigned char)(bnByte>>16);
740         bcb->CDB[2] = (unsigned char)(bnByte>>24);
741
742         result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
743         return result;
744 }
745
746 /*
747  * ENE MS Card
748  */
749
750 static int ms_lib_set_logicalpair(struct us_data *us, u16 logblk, u16 phyblk)
751 {
752         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
753
754         if ((logblk >= info->MS_Lib.NumberOfLogBlock) || (phyblk >= info->MS_Lib.NumberOfPhyBlock))
755                 return (u32)-1;
756
757         info->MS_Lib.Phy2LogMap[phyblk] = logblk;
758         info->MS_Lib.Log2PhyMap[logblk] = phyblk;
759
760         return 0;
761 }
762
763 static int ms_lib_set_logicalblockmark(struct us_data *us, u16 phyblk, u16 mark)
764 {
765         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
766
767         if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
768                 return (u32)-1;
769
770         info->MS_Lib.Phy2LogMap[phyblk] = mark;
771
772         return 0;
773 }
774
775 static int ms_lib_set_initialerrorblock(struct us_data *us, u16 phyblk)
776 {
777         return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_INITIAL_ERROR);
778 }
779
780 static int ms_lib_set_bootblockmark(struct us_data *us, u16 phyblk)
781 {
782         return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_BOOT_BLOCK);
783 }
784
785 static int ms_lib_free_logicalmap(struct us_data *us)
786 {
787         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
788
789         kfree(info->MS_Lib.Phy2LogMap);
790         info->MS_Lib.Phy2LogMap = NULL;
791
792         kfree(info->MS_Lib.Log2PhyMap);
793         info->MS_Lib.Log2PhyMap = NULL;
794
795         return 0;
796 }
797
798 static int ms_lib_alloc_logicalmap(struct us_data *us)
799 {
800         u32  i;
801         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
802
803         info->MS_Lib.Phy2LogMap = kmalloc_array(info->MS_Lib.NumberOfPhyBlock,
804                                                 sizeof(u16),
805                                                 GFP_KERNEL);
806         info->MS_Lib.Log2PhyMap = kmalloc_array(info->MS_Lib.NumberOfLogBlock,
807                                                 sizeof(u16),
808                                                 GFP_KERNEL);
809
810         if ((info->MS_Lib.Phy2LogMap == NULL) || (info->MS_Lib.Log2PhyMap == NULL)) {
811                 ms_lib_free_logicalmap(us);
812                 return (u32)-1;
813         }
814
815         for (i = 0; i < info->MS_Lib.NumberOfPhyBlock; i++)
816                 info->MS_Lib.Phy2LogMap[i] = MS_LB_NOT_USED;
817
818         for (i = 0; i < info->MS_Lib.NumberOfLogBlock; i++)
819                 info->MS_Lib.Log2PhyMap[i] = MS_LB_NOT_USED;
820
821         return 0;
822 }
823
824 static void ms_lib_clear_writebuf(struct us_data *us)
825 {
826         int i;
827         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
828
829         info->MS_Lib.wrtblk = (u16)-1;
830         ms_lib_clear_pagemap(info);
831
832         if (info->MS_Lib.blkpag)
833                 memset(info->MS_Lib.blkpag, 0xff, info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector);
834
835         if (info->MS_Lib.blkext) {
836                 for (i = 0; i < info->MS_Lib.PagesPerBlock; i++) {
837                         info->MS_Lib.blkext[i].status1 = MS_REG_ST1_DEFAULT;
838                         info->MS_Lib.blkext[i].ovrflg = MS_REG_OVR_DEFAULT;
839                         info->MS_Lib.blkext[i].mngflg = MS_REG_MNG_DEFAULT;
840                         info->MS_Lib.blkext[i].logadr = MS_LB_NOT_USED;
841                 }
842         }
843 }
844
845 static int ms_count_freeblock(struct us_data *us, u16 PhyBlock)
846 {
847         u32 Ende, Count;
848         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
849
850         Ende = PhyBlock + MS_PHYSICAL_BLOCKS_PER_SEGMENT;
851         for (Count = 0; PhyBlock < Ende; PhyBlock++) {
852                 switch (info->MS_Lib.Phy2LogMap[PhyBlock]) {
853                 case MS_LB_NOT_USED:
854                 case MS_LB_NOT_USED_ERASED:
855                         Count++;
856                         break;
857                 default:
858                         break;
859                 }
860         }
861
862         return Count;
863 }
864
865 static int ms_read_readpage(struct us_data *us, u32 PhyBlockAddr,
866                 u8 PageNum, u32 *PageBuf, struct ms_lib_type_extdat *ExtraDat)
867 {
868         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
869         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
870         u8 *bbuf = info->bbuf;
871         int result;
872         u32 bn = PhyBlockAddr * 0x20 + PageNum;
873
874         result = ene_load_bincode(us, MS_RW_PATTERN);
875         if (result != USB_STOR_XFER_GOOD)
876                 return USB_STOR_TRANSPORT_ERROR;
877
878         /* Read Page Data */
879         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
880         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
881         bcb->DataTransferLength = 0x200;
882         bcb->Flags      = US_BULK_FLAG_IN;
883         bcb->CDB[0]     = 0xF1;
884
885         bcb->CDB[1]     = 0x02; /* in init.c ENE_MSInit() is 0x01 */
886
887         bcb->CDB[5]     = (unsigned char)(bn);
888         bcb->CDB[4]     = (unsigned char)(bn>>8);
889         bcb->CDB[3]     = (unsigned char)(bn>>16);
890         bcb->CDB[2]     = (unsigned char)(bn>>24);
891
892         result = ene_send_scsi_cmd(us, FDIR_READ, PageBuf, 0);
893         if (result != USB_STOR_XFER_GOOD)
894                 return USB_STOR_TRANSPORT_ERROR;
895
896
897         /* Read Extra Data */
898         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
899         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
900         bcb->DataTransferLength = 0x4;
901         bcb->Flags      = US_BULK_FLAG_IN;
902         bcb->CDB[0]     = 0xF1;
903         bcb->CDB[1]     = 0x03;
904
905         bcb->CDB[5]     = (unsigned char)(PageNum);
906         bcb->CDB[4]     = (unsigned char)(PhyBlockAddr);
907         bcb->CDB[3]     = (unsigned char)(PhyBlockAddr>>8);
908         bcb->CDB[2]     = (unsigned char)(PhyBlockAddr>>16);
909         bcb->CDB[6]     = 0x01;
910
911         result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
912         if (result != USB_STOR_XFER_GOOD)
913                 return USB_STOR_TRANSPORT_ERROR;
914
915         ExtraDat->reserved = 0;
916         ExtraDat->intr     = 0x80;  /* Not yet,fireware support */
917         ExtraDat->status0  = 0x10;  /* Not yet,fireware support */
918
919         ExtraDat->status1  = 0x00;  /* Not yet,fireware support */
920         ExtraDat->ovrflg   = bbuf[0];
921         ExtraDat->mngflg   = bbuf[1];
922         ExtraDat->logadr   = memstick_logaddr(bbuf[2], bbuf[3]);
923
924         return USB_STOR_TRANSPORT_GOOD;
925 }
926
927 static int ms_lib_process_bootblock(struct us_data *us, u16 PhyBlock, u8 *PageData)
928 {
929         struct ms_bootblock_sysent *SysEntry;
930         struct ms_bootblock_sysinf *SysInfo;
931         u32 i, result;
932         u8 PageNumber;
933         u8 *PageBuffer;
934         struct ms_lib_type_extdat ExtraData;
935         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
936
937         PageBuffer = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
938         if (PageBuffer == NULL)
939                 return (u32)-1;
940
941         result = (u32)-1;
942
943         SysInfo = &(((struct ms_bootblock_page0 *)PageData)->sysinf);
944
945         if ((SysInfo->bMsClass != MS_SYSINF_MSCLASS_TYPE_1) ||
946                 (be16_to_cpu(SysInfo->wPageSize) != MS_SYSINF_PAGE_SIZE) ||
947                 ((SysInfo->bSecuritySupport & MS_SYSINF_SECURITY) == MS_SYSINF_SECURITY_SUPPORT) ||
948                 (SysInfo->bReserved1 != MS_SYSINF_RESERVED1) ||
949                 (SysInfo->bReserved2 != MS_SYSINF_RESERVED2) ||
950                 (SysInfo->bFormatType != MS_SYSINF_FORMAT_FAT) ||
951                 (SysInfo->bUsage != MS_SYSINF_USAGE_GENERAL))
952                 goto exit;
953                 /* */
954         switch (info->MS_Lib.cardType = SysInfo->bCardType) {
955         case MS_SYSINF_CARDTYPE_RDONLY:
956                 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY);
957                 break;
958         case MS_SYSINF_CARDTYPE_RDWR:
959                 ms_lib_ctrl_reset(info, MS_LIB_CTRL_RDONLY);
960                 break;
961         case MS_SYSINF_CARDTYPE_HYBRID:
962         default:
963                 goto exit;
964         }
965
966         info->MS_Lib.blockSize = be16_to_cpu(SysInfo->wBlockSize);
967         info->MS_Lib.NumberOfPhyBlock = be16_to_cpu(SysInfo->wBlockNumber);
968         info->MS_Lib.NumberOfLogBlock = be16_to_cpu(SysInfo->wTotalBlockNumber)-2;
969         info->MS_Lib.PagesPerBlock = info->MS_Lib.blockSize * SIZE_OF_KIRO / MS_BYTES_PER_PAGE;
970         info->MS_Lib.NumberOfSegment = info->MS_Lib.NumberOfPhyBlock / MS_PHYSICAL_BLOCKS_PER_SEGMENT;
971         info->MS_Model = be16_to_cpu(SysInfo->wMemorySize);
972
973         /*Allocate to all number of logicalblock and physicalblock */
974         if (ms_lib_alloc_logicalmap(us))
975                 goto exit;
976
977         /* Mark the book block */
978         ms_lib_set_bootblockmark(us, PhyBlock);
979
980         SysEntry = &(((struct ms_bootblock_page0 *)PageData)->sysent);
981
982         for (i = 0; i < MS_NUMBER_OF_SYSTEM_ENTRY; i++) {
983                 u32  EntryOffset, EntrySize;
984
985                 EntryOffset = be32_to_cpu(SysEntry->entry[i].dwStart);
986
987                 if (EntryOffset == 0xffffff)
988                         continue;
989                 EntrySize = be32_to_cpu(SysEntry->entry[i].dwSize);
990
991                 if (EntrySize == 0)
992                         continue;
993
994                 if (EntryOffset + MS_BYTES_PER_PAGE + EntrySize > info->MS_Lib.blockSize * (u32)SIZE_OF_KIRO)
995                         continue;
996
997                 if (i == 0) {
998                         u8 PrevPageNumber = 0;
999                         u16 phyblk;
1000
1001                         if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_INVALID_BLOCK)
1002                                 goto exit;
1003
1004                         while (EntrySize > 0) {
1005
1006                                 PageNumber = (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1);
1007                                 if (PageNumber != PrevPageNumber) {
1008                                         switch (ms_read_readpage(us, PhyBlock, PageNumber, (u32 *)PageBuffer, &ExtraData)) {
1009                                         case MS_STATUS_SUCCESS:
1010                                                 break;
1011                                         case MS_STATUS_WRITE_PROTECT:
1012                                         case MS_ERROR_FLASH_READ:
1013                                         case MS_STATUS_ERROR:
1014                                         default:
1015                                                 goto exit;
1016                                         }
1017
1018                                         PrevPageNumber = PageNumber;
1019                                 }
1020
1021                                 phyblk = be16_to_cpu(*(u16 *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)));
1022                                 if (phyblk < 0x0fff)
1023                                         ms_lib_set_initialerrorblock(us, phyblk);
1024
1025                                 EntryOffset += 2;
1026                                 EntrySize -= 2;
1027                         }
1028                 } else if (i == 1) {  /* CIS/IDI */
1029                         struct ms_bootblock_idi *idi;
1030
1031                         if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_CIS_IDI)
1032                                 goto exit;
1033
1034                         switch (ms_read_readpage(us, PhyBlock, (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1), (u32 *)PageBuffer, &ExtraData)) {
1035                         case MS_STATUS_SUCCESS:
1036                                 break;
1037                         case MS_STATUS_WRITE_PROTECT:
1038                         case MS_ERROR_FLASH_READ:
1039                         case MS_STATUS_ERROR:
1040                         default:
1041                                 goto exit;
1042                         }
1043
1044                         idi = &((struct ms_bootblock_cis_idi *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)))->idi.idi;
1045                         if (le16_to_cpu(idi->wIDIgeneralConfiguration) != MS_IDI_GENERAL_CONF)
1046                                 goto exit;
1047
1048                         info->MS_Lib.BytesPerSector = le16_to_cpu(idi->wIDIbytesPerSector);
1049                         if (info->MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE)
1050                                 goto exit;
1051                 }
1052         } /* End for .. */
1053
1054         result = 0;
1055
1056 exit:
1057         if (result)
1058                 ms_lib_free_logicalmap(us);
1059
1060         kfree(PageBuffer);
1061
1062         result = 0;
1063         return result;
1064 }
1065
1066 static void ms_lib_free_writebuf(struct us_data *us)
1067 {
1068         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1069         info->MS_Lib.wrtblk = (u16)-1; /* set to -1 */
1070
1071         /* memset((fdoExt)->MS_Lib.pagemap, 0, sizeof((fdoExt)->MS_Lib.pagemap)) */
1072
1073         ms_lib_clear_pagemap(info); /* (pdx)->MS_Lib.pagemap memset 0 in ms.h */
1074
1075         if (info->MS_Lib.blkpag) {
1076                 kfree(info->MS_Lib.blkpag);  /* Arnold test ... */
1077                 info->MS_Lib.blkpag = NULL;
1078         }
1079
1080         if (info->MS_Lib.blkext) {
1081                 kfree(info->MS_Lib.blkext);  /* Arnold test ... */
1082                 info->MS_Lib.blkext = NULL;
1083         }
1084 }
1085
1086
1087 static void ms_lib_free_allocatedarea(struct us_data *us)
1088 {
1089         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1090
1091         ms_lib_free_writebuf(us); /* Free MS_Lib.pagemap */
1092         ms_lib_free_logicalmap(us); /* kfree MS_Lib.Phy2LogMap and MS_Lib.Log2PhyMap */
1093
1094         /* set struct us point flag to 0 */
1095         info->MS_Lib.flags = 0;
1096         info->MS_Lib.BytesPerSector = 0;
1097         info->MS_Lib.SectorsPerCylinder = 0;
1098
1099         info->MS_Lib.cardType = 0;
1100         info->MS_Lib.blockSize = 0;
1101         info->MS_Lib.PagesPerBlock = 0;
1102
1103         info->MS_Lib.NumberOfPhyBlock = 0;
1104         info->MS_Lib.NumberOfLogBlock = 0;
1105 }
1106
1107
1108 static int ms_lib_alloc_writebuf(struct us_data *us)
1109 {
1110         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1111
1112         info->MS_Lib.wrtblk = (u16)-1;
1113
1114         info->MS_Lib.blkpag = kmalloc_array(info->MS_Lib.PagesPerBlock,
1115                                             info->MS_Lib.BytesPerSector,
1116                                             GFP_KERNEL);
1117         info->MS_Lib.blkext = kmalloc_array(info->MS_Lib.PagesPerBlock,
1118                                             sizeof(struct ms_lib_type_extdat),
1119                                             GFP_KERNEL);
1120
1121         if ((info->MS_Lib.blkpag == NULL) || (info->MS_Lib.blkext == NULL)) {
1122                 ms_lib_free_writebuf(us);
1123                 return (u32)-1;
1124         }
1125
1126         ms_lib_clear_writebuf(us);
1127
1128         return 0;
1129 }
1130
1131 static int ms_lib_force_setlogical_pair(struct us_data *us, u16 logblk, u16 phyblk)
1132 {
1133         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1134
1135         if (logblk == MS_LB_NOT_USED)
1136                 return 0;
1137
1138         if ((logblk >= info->MS_Lib.NumberOfLogBlock) ||
1139                 (phyblk >= info->MS_Lib.NumberOfPhyBlock))
1140                 return (u32)-1;
1141
1142         info->MS_Lib.Phy2LogMap[phyblk] = logblk;
1143         info->MS_Lib.Log2PhyMap[logblk] = phyblk;
1144
1145         return 0;
1146 }
1147
1148 static int ms_read_copyblock(struct us_data *us, u16 oldphy, u16 newphy,
1149                         u16 PhyBlockAddr, u8 PageNum, unsigned char *buf, u16 len)
1150 {
1151         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1152         int result;
1153
1154         result = ene_load_bincode(us, MS_RW_PATTERN);
1155         if (result != USB_STOR_XFER_GOOD)
1156                 return USB_STOR_TRANSPORT_ERROR;
1157
1158         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1159         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1160         bcb->DataTransferLength = 0x200*len;
1161         bcb->Flags = 0x00;
1162         bcb->CDB[0] = 0xF0;
1163         bcb->CDB[1] = 0x08;
1164         bcb->CDB[4] = (unsigned char)(oldphy);
1165         bcb->CDB[3] = (unsigned char)(oldphy>>8);
1166         bcb->CDB[2] = 0; /* (BYTE)(oldphy>>16) */
1167         bcb->CDB[7] = (unsigned char)(newphy);
1168         bcb->CDB[6] = (unsigned char)(newphy>>8);
1169         bcb->CDB[5] = 0; /* (BYTE)(newphy>>16) */
1170         bcb->CDB[9] = (unsigned char)(PhyBlockAddr);
1171         bcb->CDB[8] = (unsigned char)(PhyBlockAddr>>8);
1172         bcb->CDB[10] = PageNum;
1173
1174         result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1175         if (result != USB_STOR_XFER_GOOD)
1176                 return USB_STOR_TRANSPORT_ERROR;
1177
1178         return USB_STOR_TRANSPORT_GOOD;
1179 }
1180
1181 static int ms_read_eraseblock(struct us_data *us, u32 PhyBlockAddr)
1182 {
1183         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1184         int result;
1185         u32 bn = PhyBlockAddr;
1186
1187         result = ene_load_bincode(us, MS_RW_PATTERN);
1188         if (result != USB_STOR_XFER_GOOD)
1189                 return USB_STOR_TRANSPORT_ERROR;
1190
1191         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1192         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1193         bcb->DataTransferLength = 0x200;
1194         bcb->Flags = US_BULK_FLAG_IN;
1195         bcb->CDB[0] = 0xF2;
1196         bcb->CDB[1] = 0x06;
1197         bcb->CDB[4] = (unsigned char)(bn);
1198         bcb->CDB[3] = (unsigned char)(bn>>8);
1199         bcb->CDB[2] = (unsigned char)(bn>>16);
1200
1201         result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1202         if (result != USB_STOR_XFER_GOOD)
1203                 return USB_STOR_TRANSPORT_ERROR;
1204
1205         return USB_STOR_TRANSPORT_GOOD;
1206 }
1207
1208 static int ms_lib_check_disableblock(struct us_data *us, u16 PhyBlock)
1209 {
1210         unsigned char *PageBuf = NULL;
1211         u16 result = MS_STATUS_SUCCESS;
1212         u16 blk, index = 0;
1213         struct ms_lib_type_extdat extdat;
1214         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1215
1216         PageBuf = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1217         if (PageBuf == NULL) {
1218                 result = MS_NO_MEMORY_ERROR;
1219                 goto exit;
1220         }
1221
1222         ms_read_readpage(us, PhyBlock, 1, (u32 *)PageBuf, &extdat);
1223         do {
1224                 blk = be16_to_cpu(PageBuf[index]);
1225                 if (blk == MS_LB_NOT_USED)
1226                         break;
1227                 if (blk == info->MS_Lib.Log2PhyMap[0]) {
1228                         result = MS_ERROR_FLASH_READ;
1229                         break;
1230                 }
1231                 index++;
1232         } while (1);
1233
1234 exit:
1235         kfree(PageBuf);
1236         return result;
1237 }
1238
1239 static int ms_lib_setacquired_errorblock(struct us_data *us, u16 phyblk)
1240 {
1241         u16 log;
1242         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1243
1244         if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1245                 return (u32)-1;
1246
1247         log = info->MS_Lib.Phy2LogMap[phyblk];
1248
1249         if (log < info->MS_Lib.NumberOfLogBlock)
1250                 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1251
1252         if (info->MS_Lib.Phy2LogMap[phyblk] != MS_LB_INITIAL_ERROR)
1253                 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_ACQUIRED_ERROR;
1254
1255         return 0;
1256 }
1257
1258 static int ms_lib_overwrite_extra(struct us_data *us, u32 PhyBlockAddr,
1259                                 u8 PageNum, u8 OverwriteFlag)
1260 {
1261         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1262         int result;
1263
1264         result = ene_load_bincode(us, MS_RW_PATTERN);
1265         if (result != USB_STOR_XFER_GOOD)
1266                 return USB_STOR_TRANSPORT_ERROR;
1267
1268         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1269         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1270         bcb->DataTransferLength = 0x4;
1271         bcb->Flags = US_BULK_FLAG_IN;
1272         bcb->CDB[0] = 0xF2;
1273         bcb->CDB[1] = 0x05;
1274         bcb->CDB[5] = (unsigned char)(PageNum);
1275         bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
1276         bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
1277         bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
1278         bcb->CDB[6] = OverwriteFlag;
1279         bcb->CDB[7] = 0xFF;
1280         bcb->CDB[8] = 0xFF;
1281         bcb->CDB[9] = 0xFF;
1282
1283         result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1284         if (result != USB_STOR_XFER_GOOD)
1285                 return USB_STOR_TRANSPORT_ERROR;
1286
1287         return USB_STOR_TRANSPORT_GOOD;
1288 }
1289
1290 static int ms_lib_error_phyblock(struct us_data *us, u16 phyblk)
1291 {
1292         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1293
1294         if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1295                 return MS_STATUS_ERROR;
1296
1297         ms_lib_setacquired_errorblock(us, phyblk);
1298
1299         if (ms_lib_iswritable(info))
1300                 return ms_lib_overwrite_extra(us, phyblk, 0, (u8)(~MS_REG_OVR_BKST & BYTE_MASK));
1301
1302         return MS_STATUS_SUCCESS;
1303 }
1304
1305 static int ms_lib_erase_phyblock(struct us_data *us, u16 phyblk)
1306 {
1307         u16 log;
1308         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1309
1310         if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1311                 return MS_STATUS_ERROR;
1312
1313         log = info->MS_Lib.Phy2LogMap[phyblk];
1314
1315         if (log < info->MS_Lib.NumberOfLogBlock)
1316                 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1317
1318         info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED;
1319
1320         if (ms_lib_iswritable(info)) {
1321                 switch (ms_read_eraseblock(us, phyblk)) {
1322                 case MS_STATUS_SUCCESS:
1323                         info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED_ERASED;
1324                         return MS_STATUS_SUCCESS;
1325                 case MS_ERROR_FLASH_ERASE:
1326                 case MS_STATUS_INT_ERROR:
1327                         ms_lib_error_phyblock(us, phyblk);
1328                         return MS_ERROR_FLASH_ERASE;
1329                 case MS_STATUS_ERROR:
1330                 default:
1331                         ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY); /* MS_LibCtrlSet will used by ENE_MSInit ,need check, and why us to info*/
1332                         ms_lib_setacquired_errorblock(us, phyblk);
1333                         return MS_STATUS_ERROR;
1334                 }
1335         }
1336
1337         ms_lib_setacquired_errorblock(us, phyblk);
1338
1339         return MS_STATUS_SUCCESS;
1340 }
1341
1342 static int ms_lib_read_extra(struct us_data *us, u32 PhyBlock,
1343                                 u8 PageNum, struct ms_lib_type_extdat *ExtraDat)
1344 {
1345         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1346         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1347         u8 *bbuf = info->bbuf;
1348         int result;
1349
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;
1354         bcb->CDB[0]     = 0xF1;
1355         bcb->CDB[1]     = 0x03;
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);
1360         bcb->CDB[6]     = 0x01;
1361
1362         result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
1363         if (result != USB_STOR_XFER_GOOD)
1364                 return USB_STOR_TRANSPORT_ERROR;
1365
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]);
1373
1374         return USB_STOR_TRANSPORT_GOOD;
1375 }
1376
1377 static int ms_libsearch_block_from_physical(struct us_data *us, u16 phyblk)
1378 {
1379         u16 blk;
1380         struct ms_lib_type_extdat extdat; /* need check */
1381         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1382
1383
1384         if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1385                 return MS_LB_ERROR;
1386
1387         for (blk = phyblk + 1; blk != phyblk; blk++) {
1388                 if ((blk & MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK) == 0)
1389                         blk -= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1390
1391                 if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED_ERASED) {
1392                         return blk;
1393                 } else if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED) {
1394                         switch (ms_lib_read_extra(us, blk, 0, &extdat)) {
1395                         case MS_STATUS_SUCCESS:
1396                         case MS_STATUS_SUCCESS_WITH_ECC:
1397                                 break;
1398                         case MS_NOCARD_ERROR:
1399                                 return MS_NOCARD_ERROR;
1400                         case MS_STATUS_INT_ERROR:
1401                                 return MS_LB_ERROR;
1402                         case MS_ERROR_FLASH_READ:
1403                         default:
1404                                 ms_lib_setacquired_errorblock(us, blk);
1405                                 continue;
1406                         } /* End switch */
1407
1408                         if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1409                                 ms_lib_setacquired_errorblock(us, blk);
1410                                 continue;
1411                         }
1412
1413                         switch (ms_lib_erase_phyblock(us, blk)) {
1414                         case MS_STATUS_SUCCESS:
1415                                 return blk;
1416                         case MS_STATUS_ERROR:
1417                                 return MS_LB_ERROR;
1418                         case MS_ERROR_FLASH_ERASE:
1419                         default:
1420                                 ms_lib_error_phyblock(us, blk);
1421                                 break;
1422                         }
1423                 }
1424         } /* End for */
1425
1426         return MS_LB_ERROR;
1427 }
1428 static int ms_libsearch_block_from_logical(struct us_data *us, u16 logblk)
1429 {
1430         u16 phyblk;
1431         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1432
1433         phyblk = ms_libconv_to_physical(info, logblk);
1434         if (phyblk >= MS_LB_ERROR) {
1435                 if (logblk >= info->MS_Lib.NumberOfLogBlock)
1436                         return MS_LB_ERROR;
1437
1438                 phyblk = (logblk + MS_NUMBER_OF_BOOT_BLOCK) / MS_LOGICAL_BLOCKS_PER_SEGMENT;
1439                 phyblk *= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1440                 phyblk += MS_PHYSICAL_BLOCKS_PER_SEGMENT - 1;
1441         }
1442
1443         return ms_libsearch_block_from_physical(us, phyblk);
1444 }
1445
1446 static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
1447 {
1448         struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
1449
1450         /* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
1451         if ((info->MS_Status & MS_Insert) && (info->MS_Status & MS_Ready)) {
1452                 return USB_STOR_TRANSPORT_GOOD;
1453         } else {
1454                 ene_ms_init(us);
1455                 return USB_STOR_TRANSPORT_GOOD;
1456         }
1457
1458         return USB_STOR_TRANSPORT_GOOD;
1459 }
1460
1461 static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
1462 {
1463         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1464         unsigned char mediaNoWP[12] = {
1465                 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
1466                 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1467         unsigned char mediaWP[12]   = {
1468                 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
1469                 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1470
1471         if (info->MS_Status & MS_WtP)
1472                 usb_stor_set_xfer_buf(mediaWP, 12, srb);
1473         else
1474                 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
1475
1476         return USB_STOR_TRANSPORT_GOOD;
1477 }
1478
1479 static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
1480 {
1481         u32   bl_num;
1482         u16    bl_len;
1483         unsigned int offset = 0;
1484         unsigned char    buf[8];
1485         struct scatterlist *sg = NULL;
1486         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1487
1488         usb_stor_dbg(us, "ms_scsi_read_capacity\n");
1489         bl_len = 0x200;
1490         if (info->MS_Status & MS_IsMSPro)
1491                 bl_num = info->MSP_TotalBlock - 1;
1492         else
1493                 bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1;
1494
1495         info->bl_num = bl_num;
1496         usb_stor_dbg(us, "bl_len = %x\n", bl_len);
1497         usb_stor_dbg(us, "bl_num = %x\n", bl_num);
1498
1499         /*srb->request_bufflen = 8; */
1500         buf[0] = (bl_num >> 24) & 0xff;
1501         buf[1] = (bl_num >> 16) & 0xff;
1502         buf[2] = (bl_num >> 8) & 0xff;
1503         buf[3] = (bl_num >> 0) & 0xff;
1504         buf[4] = (bl_len >> 24) & 0xff;
1505         buf[5] = (bl_len >> 16) & 0xff;
1506         buf[6] = (bl_len >> 8) & 0xff;
1507         buf[7] = (bl_len >> 0) & 0xff;
1508
1509         usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
1510
1511         return USB_STOR_TRANSPORT_GOOD;
1512 }
1513
1514 static void ms_lib_phy_to_log_range(u16 PhyBlock, u16 *LogStart, u16 *LogEnde)
1515 {
1516         PhyBlock /= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1517
1518         if (PhyBlock) {
1519                 *LogStart = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT + (PhyBlock - 1) * MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1520                 *LogEnde = *LogStart + MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1521         } else {
1522                 *LogStart = 0;
1523                 *LogEnde = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT;/*494*/
1524         }
1525 }
1526
1527 static int ms_lib_read_extrablock(struct us_data *us, u32 PhyBlock,
1528         u8 PageNum, u8 blen, void *buf)
1529 {
1530         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1531         int     result;
1532
1533         /* Read Extra Data */
1534         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1535         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1536         bcb->DataTransferLength = 0x4 * blen;
1537         bcb->Flags      = US_BULK_FLAG_IN;
1538         bcb->CDB[0]     = 0xF1;
1539         bcb->CDB[1]     = 0x03;
1540         bcb->CDB[5]     = (unsigned char)(PageNum);
1541         bcb->CDB[4]     = (unsigned char)(PhyBlock);
1542         bcb->CDB[3]     = (unsigned char)(PhyBlock>>8);
1543         bcb->CDB[2]     = (unsigned char)(PhyBlock>>16);
1544         bcb->CDB[6]     = blen;
1545
1546         result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1547         if (result != USB_STOR_XFER_GOOD)
1548                 return USB_STOR_TRANSPORT_ERROR;
1549
1550         return USB_STOR_TRANSPORT_GOOD;
1551 }
1552
1553 static int ms_lib_scan_logicalblocknumber(struct us_data *us, u16 btBlk1st)
1554 {
1555         u16 PhyBlock, newblk, i;
1556         u16 LogStart, LogEnde;
1557         struct ms_lib_type_extdat extdat;
1558         u32 count = 0, index = 0;
1559         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1560         u8 *bbuf = info->bbuf;
1561
1562         for (PhyBlock = 0; PhyBlock < info->MS_Lib.NumberOfPhyBlock;) {
1563                 ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde);
1564
1565                 for (i = 0; i < MS_PHYSICAL_BLOCKS_PER_SEGMENT; i++, PhyBlock++) {
1566                         switch (ms_libconv_to_logical(info, PhyBlock)) {
1567                         case MS_STATUS_ERROR:
1568                                 continue;
1569                         default:
1570                                 break;
1571                         }
1572
1573                         if (count == PhyBlock) {
1574                                 ms_lib_read_extrablock(us, PhyBlock, 0, 0x80,
1575                                                 bbuf);
1576                                 count += 0x80;
1577                         }
1578                         index = (PhyBlock % 0x80) * 4;
1579
1580                         extdat.ovrflg = bbuf[index];
1581                         extdat.mngflg = bbuf[index+1];
1582                         extdat.logadr = memstick_logaddr(bbuf[index+2],
1583                                         bbuf[index+3]);
1584
1585                         if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1586                                 ms_lib_setacquired_errorblock(us, PhyBlock);
1587                                 continue;
1588                         }
1589
1590                         if ((extdat.mngflg & MS_REG_MNG_ATFLG) == MS_REG_MNG_ATFLG_ATTBL) {
1591                                 ms_lib_erase_phyblock(us, PhyBlock);
1592                                 continue;
1593                         }
1594
1595                         if (extdat.logadr != MS_LB_NOT_USED) {
1596                                 if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) {
1597                                         ms_lib_erase_phyblock(us, PhyBlock);
1598                                         continue;
1599                                 }
1600
1601                                 newblk = ms_libconv_to_physical(info, extdat.logadr);
1602
1603                                 if (newblk != MS_LB_NOT_USED) {
1604                                         if (extdat.logadr == 0) {
1605                                                 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1606                                                 if (ms_lib_check_disableblock(us, btBlk1st)) {
1607                                                         ms_lib_set_logicalpair(us, extdat.logadr, newblk);
1608                                                         continue;
1609                                                 }
1610                                         }
1611
1612                                         ms_lib_read_extra(us, newblk, 0, &extdat);
1613                                         if ((extdat.ovrflg & MS_REG_OVR_UDST) == MS_REG_OVR_UDST_UPDATING) {
1614                                                 ms_lib_erase_phyblock(us, PhyBlock);
1615                                                 continue;
1616                                         } else {
1617                                                 ms_lib_erase_phyblock(us, newblk);
1618                                         }
1619                                 }
1620
1621                                 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1622                         }
1623                 }
1624         } /* End for ... */
1625
1626         return MS_STATUS_SUCCESS;
1627 }
1628
1629
1630 static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
1631 {
1632         int result;
1633         unsigned char *cdb = srb->cmnd;
1634         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1635         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1636
1637         u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
1638                 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
1639         u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1640         u32 blenByte = blen * 0x200;
1641
1642         if (bn > info->bl_num)
1643                 return USB_STOR_TRANSPORT_ERROR;
1644
1645         if (info->MS_Status & MS_IsMSPro) {
1646                 result = ene_load_bincode(us, MSP_RW_PATTERN);
1647                 if (result != USB_STOR_XFER_GOOD) {
1648                         usb_stor_dbg(us, "Load MPS RW pattern Fail !!\n");
1649                         return USB_STOR_TRANSPORT_ERROR;
1650                 }
1651
1652                 /* set up the command wrapper */
1653                 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1654                 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1655                 bcb->DataTransferLength = blenByte;
1656                 bcb->Flags  = US_BULK_FLAG_IN;
1657                 bcb->CDB[0] = 0xF1;
1658                 bcb->CDB[1] = 0x02;
1659                 bcb->CDB[5] = (unsigned char)(bn);
1660                 bcb->CDB[4] = (unsigned char)(bn>>8);
1661                 bcb->CDB[3] = (unsigned char)(bn>>16);
1662                 bcb->CDB[2] = (unsigned char)(bn>>24);
1663
1664                 result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
1665         } else {
1666                 void *buf;
1667                 int offset = 0;
1668                 u16 phyblk, logblk;
1669                 u8 PageNum;
1670                 u16 len;
1671                 u32 blkno;
1672
1673                 buf = kmalloc(blenByte, GFP_KERNEL);
1674                 if (buf == NULL)
1675                         return USB_STOR_TRANSPORT_ERROR;
1676
1677                 result = ene_load_bincode(us, MS_RW_PATTERN);
1678                 if (result != USB_STOR_XFER_GOOD) {
1679                         pr_info("Load MS RW pattern Fail !!\n");
1680                         result = USB_STOR_TRANSPORT_ERROR;
1681                         goto exit;
1682                 }
1683
1684                 logblk  = (u16)(bn / info->MS_Lib.PagesPerBlock);
1685                 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1686
1687                 while (1) {
1688                         if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1689                                 len = info->MS_Lib.PagesPerBlock-PageNum;
1690                         else
1691                                 len = blen;
1692
1693                         phyblk = ms_libconv_to_physical(info, logblk);
1694                         blkno  = phyblk * 0x20 + PageNum;
1695
1696                         /* set up the command wrapper */
1697                         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1698                         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1699                         bcb->DataTransferLength = 0x200 * len;
1700                         bcb->Flags  = US_BULK_FLAG_IN;
1701                         bcb->CDB[0] = 0xF1;
1702                         bcb->CDB[1] = 0x02;
1703                         bcb->CDB[5] = (unsigned char)(blkno);
1704                         bcb->CDB[4] = (unsigned char)(blkno>>8);
1705                         bcb->CDB[3] = (unsigned char)(blkno>>16);
1706                         bcb->CDB[2] = (unsigned char)(blkno>>24);
1707
1708                         result = ene_send_scsi_cmd(us, FDIR_READ, buf+offset, 0);
1709                         if (result != USB_STOR_XFER_GOOD) {
1710                                 pr_info("MS_SCSI_Read --- result = %x\n", result);
1711                                 result = USB_STOR_TRANSPORT_ERROR;
1712                                 goto exit;
1713                         }
1714
1715                         blen -= len;
1716                         if (blen <= 0)
1717                                 break;
1718                         logblk++;
1719                         PageNum = 0;
1720                         offset += MS_BYTES_PER_PAGE*len;
1721                 }
1722                 usb_stor_set_xfer_buf(buf, blenByte, srb);
1723 exit:
1724                 kfree(buf);
1725         }
1726         return result;
1727 }
1728
1729 static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
1730 {
1731         int result;
1732         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1733         unsigned char *cdb = srb->cmnd;
1734         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1735
1736         u32 bn = ((cdb[2] << 24) & 0xff000000) |
1737                         ((cdb[3] << 16) & 0x00ff0000) |
1738                         ((cdb[4] << 8) & 0x0000ff00) |
1739                         ((cdb[5] << 0) & 0x000000ff);
1740         u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1741         u32 blenByte = blen * 0x200;
1742
1743         if (bn > info->bl_num)
1744                 return USB_STOR_TRANSPORT_ERROR;
1745
1746         if (info->MS_Status & MS_IsMSPro) {
1747                 result = ene_load_bincode(us, MSP_RW_PATTERN);
1748                 if (result != USB_STOR_XFER_GOOD) {
1749                         pr_info("Load MSP RW pattern Fail !!\n");
1750                         return USB_STOR_TRANSPORT_ERROR;
1751                 }
1752
1753                 /* set up the command wrapper */
1754                 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1755                 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1756                 bcb->DataTransferLength = blenByte;
1757                 bcb->Flags  = 0x00;
1758                 bcb->CDB[0] = 0xF0;
1759                 bcb->CDB[1] = 0x04;
1760                 bcb->CDB[5] = (unsigned char)(bn);
1761                 bcb->CDB[4] = (unsigned char)(bn>>8);
1762                 bcb->CDB[3] = (unsigned char)(bn>>16);
1763                 bcb->CDB[2] = (unsigned char)(bn>>24);
1764
1765                 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
1766         } else {
1767                 void *buf;
1768                 int offset = 0;
1769                 u16 PhyBlockAddr;
1770                 u8 PageNum;
1771                 u16 len, oldphy, newphy;
1772
1773                 buf = kmalloc(blenByte, GFP_KERNEL);
1774                 if (buf == NULL)
1775                         return USB_STOR_TRANSPORT_ERROR;
1776                 usb_stor_set_xfer_buf(buf, blenByte, srb);
1777
1778                 result = ene_load_bincode(us, MS_RW_PATTERN);
1779                 if (result != USB_STOR_XFER_GOOD) {
1780                         pr_info("Load MS RW pattern Fail !!\n");
1781                         result = USB_STOR_TRANSPORT_ERROR;
1782                         goto exit;
1783                 }
1784
1785                 PhyBlockAddr = (u16)(bn / info->MS_Lib.PagesPerBlock);
1786                 PageNum      = (u8)(bn % info->MS_Lib.PagesPerBlock);
1787
1788                 while (1) {
1789                         if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1790                                 len = info->MS_Lib.PagesPerBlock-PageNum;
1791                         else
1792                                 len = blen;
1793
1794                         oldphy = ms_libconv_to_physical(info, PhyBlockAddr); /* need check us <-> info */
1795                         newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr);
1796
1797                         result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len);
1798
1799                         if (result != USB_STOR_XFER_GOOD) {
1800                                 pr_info("MS_SCSI_Write --- result = %x\n", result);
1801                                 result =  USB_STOR_TRANSPORT_ERROR;
1802                                 goto exit;
1803                         }
1804
1805                         info->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED;
1806                         ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy);
1807
1808                         blen -= len;
1809                         if (blen <= 0)
1810                                 break;
1811                         PhyBlockAddr++;
1812                         PageNum = 0;
1813                         offset += MS_BYTES_PER_PAGE*len;
1814                 }
1815 exit:
1816                 kfree(buf);
1817         }
1818         return result;
1819 }
1820
1821 /*
1822  * ENE MS Card
1823  */
1824
1825 static int ene_get_card_type(struct us_data *us, u16 index, void *buf)
1826 {
1827         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1828         int result;
1829
1830         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1831         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1832         bcb->DataTransferLength = 0x01;
1833         bcb->Flags                      = US_BULK_FLAG_IN;
1834         bcb->CDB[0]                     = 0xED;
1835         bcb->CDB[2]                     = (unsigned char)(index>>8);
1836         bcb->CDB[3]                     = (unsigned char)index;
1837
1838         result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1839         return result;
1840 }
1841
1842 static int ene_get_card_status(struct us_data *us, u8 *buf)
1843 {
1844         u16 tmpreg;
1845         u32 reg4b;
1846         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1847
1848         /*usb_stor_dbg(us, "transport --- ENE_ReadSDReg\n");*/
1849         reg4b = *(u32 *)&buf[0x18];
1850         info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f);
1851
1852         tmpreg = (u16) reg4b;
1853         reg4b = *(u32 *)(&buf[0x14]);
1854         if ((info->SD_Status & SD_HiCapacity) && !(info->SD_Status & SD_IsMMC))
1855                 info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
1856
1857         info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
1858         info->SD_C_SIZE_MULT = (u8)(reg4b >> 7)  & 0x07;
1859         if ((info->SD_Status & SD_HiCapacity) && (info->SD_Status & SD_IsMMC))
1860                 info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
1861
1862         if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
1863                 info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN);
1864                 info->SD_READ_BL_LEN = SD_BLOCK_LEN;
1865         } else {
1866                 info->SD_Block_Mult = 1;
1867         }
1868
1869         return USB_STOR_TRANSPORT_GOOD;
1870 }
1871
1872 static int ene_load_bincode(struct us_data *us, unsigned char flag)
1873 {
1874         int err;
1875         char *fw_name = NULL;
1876         unsigned char *buf = NULL;
1877         const struct firmware *sd_fw = NULL;
1878         int result = USB_STOR_TRANSPORT_ERROR;
1879         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1880         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1881
1882         if (info->BIN_FLAG == flag)
1883                 return USB_STOR_TRANSPORT_GOOD;
1884
1885         switch (flag) {
1886         /* For SD */
1887         case SD_INIT1_PATTERN:
1888                 usb_stor_dbg(us, "SD_INIT1_PATTERN\n");
1889                 fw_name = SD_INIT1_FIRMWARE;
1890                 break;
1891         case SD_INIT2_PATTERN:
1892                 usb_stor_dbg(us, "SD_INIT2_PATTERN\n");
1893                 fw_name = SD_INIT2_FIRMWARE;
1894                 break;
1895         case SD_RW_PATTERN:
1896                 usb_stor_dbg(us, "SD_RW_PATTERN\n");
1897                 fw_name = SD_RW_FIRMWARE;
1898                 break;
1899         /* For MS */
1900         case MS_INIT_PATTERN:
1901                 usb_stor_dbg(us, "MS_INIT_PATTERN\n");
1902                 fw_name = MS_INIT_FIRMWARE;
1903                 break;
1904         case MSP_RW_PATTERN:
1905                 usb_stor_dbg(us, "MSP_RW_PATTERN\n");
1906                 fw_name = MSP_RW_FIRMWARE;
1907                 break;
1908         case MS_RW_PATTERN:
1909                 usb_stor_dbg(us, "MS_RW_PATTERN\n");
1910                 fw_name = MS_RW_FIRMWARE;
1911                 break;
1912         default:
1913                 usb_stor_dbg(us, "----------- Unknown PATTERN ----------\n");
1914                 goto nofw;
1915         }
1916
1917         err = reject_firmware(&sd_fw, fw_name, &us->pusb_dev->dev);
1918         if (err) {
1919                 usb_stor_dbg(us, "load firmware %s failed\n", fw_name);
1920                 goto nofw;
1921         }
1922         buf = kmemdup(sd_fw->data, sd_fw->size, GFP_KERNEL);
1923         if (buf == NULL)
1924                 goto nofw;
1925
1926         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1927         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1928         bcb->DataTransferLength = sd_fw->size;
1929         bcb->Flags = 0x00;
1930         bcb->CDB[0] = 0xEF;
1931
1932         result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1933         if (us->srb != NULL)
1934                 scsi_set_resid(us->srb, 0);
1935         info->BIN_FLAG = flag;
1936         kfree(buf);
1937
1938 nofw:
1939         release_firmware(sd_fw);
1940         return result;
1941 }
1942
1943 static int ms_card_init(struct us_data *us)
1944 {
1945         u32 result;
1946         u16 TmpBlock;
1947         unsigned char *PageBuffer0 = NULL, *PageBuffer1 = NULL;
1948         struct ms_lib_type_extdat extdat;
1949         u16 btBlk1st, btBlk2nd;
1950         u32 btBlk1stErred;
1951         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1952
1953         printk(KERN_INFO "MS_CardInit start\n");
1954
1955         ms_lib_free_allocatedarea(us); /* Clean buffer and set struct us_data flag to 0 */
1956
1957         /* get two PageBuffer */
1958         PageBuffer0 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1959         PageBuffer1 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1960         if ((PageBuffer0 == NULL) || (PageBuffer1 == NULL)) {
1961                 result = MS_NO_MEMORY_ERROR;
1962                 goto exit;
1963         }
1964
1965         btBlk1st = btBlk2nd = MS_LB_NOT_USED;
1966         btBlk1stErred = 0;
1967
1968         for (TmpBlock = 0; TmpBlock < MS_MAX_INITIAL_ERROR_BLOCKS+2; TmpBlock++) {
1969
1970                 switch (ms_read_readpage(us, TmpBlock, 0, (u32 *)PageBuffer0, &extdat)) {
1971                 case MS_STATUS_SUCCESS:
1972                         break;
1973                 case MS_STATUS_INT_ERROR:
1974                         break;
1975                 case MS_STATUS_ERROR:
1976                 default:
1977                         continue;
1978                 }
1979
1980                 if ((extdat.ovrflg & MS_REG_OVR_BKST) == MS_REG_OVR_BKST_NG)
1981                         continue;
1982
1983                 if (((extdat.mngflg & MS_REG_MNG_SYSFLG) == MS_REG_MNG_SYSFLG_USER) ||
1984                         (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wBlockID) != MS_BOOT_BLOCK_ID) ||
1985                         (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wFormatVersion) != MS_BOOT_BLOCK_FORMAT_VERSION) ||
1986                         (((struct ms_bootblock_page0 *)PageBuffer0)->header.bNumberOfDataEntry != MS_BOOT_BLOCK_DATA_ENTRIES))
1987                                 continue;
1988
1989                 if (btBlk1st != MS_LB_NOT_USED) {
1990                         btBlk2nd = TmpBlock;
1991                         break;
1992                 }
1993
1994                 btBlk1st = TmpBlock;
1995                 memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE);
1996                 if (extdat.status1 & (MS_REG_ST1_DTER | MS_REG_ST1_EXER | MS_REG_ST1_FGER))
1997                         btBlk1stErred = 1;
1998         }
1999
2000         if (btBlk1st == MS_LB_NOT_USED) {
2001                 result = MS_STATUS_ERROR;
2002                 goto exit;
2003         }
2004
2005         /* write protect */
2006         if ((extdat.status0 & MS_REG_ST0_WP) == MS_REG_ST0_WP_ON)
2007                 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2008
2009         result = MS_STATUS_ERROR;
2010         /* 1st Boot Block */
2011         if (btBlk1stErred == 0)
2012                 result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1);
2013                 /* 1st */
2014         /* 2nd Boot Block */
2015         if (result && (btBlk2nd != MS_LB_NOT_USED))
2016                 result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0);
2017
2018         if (result) {
2019                 result = MS_STATUS_ERROR;
2020                 goto exit;
2021         }
2022
2023         for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++)
2024                 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2025
2026         info->MS_Lib.Phy2LogMap[btBlk1st] = MS_LB_BOOT_BLOCK;
2027
2028         if (btBlk2nd != MS_LB_NOT_USED) {
2029                 for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++)
2030                         info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2031
2032                 info->MS_Lib.Phy2LogMap[btBlk2nd] = MS_LB_BOOT_BLOCK;
2033         }
2034
2035         result = ms_lib_scan_logicalblocknumber(us, btBlk1st);
2036         if (result)
2037                 goto exit;
2038
2039         for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT;
2040                 TmpBlock < info->MS_Lib.NumberOfPhyBlock;
2041                 TmpBlock += MS_PHYSICAL_BLOCKS_PER_SEGMENT) {
2042                 if (ms_count_freeblock(us, TmpBlock) == 0) {
2043                         ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2044                         break;
2045                 }
2046         }
2047
2048         /* write */
2049         if (ms_lib_alloc_writebuf(us)) {
2050                 result = MS_NO_MEMORY_ERROR;
2051                 goto exit;
2052         }
2053
2054         result = MS_STATUS_SUCCESS;
2055
2056 exit:
2057         kfree(PageBuffer1);
2058         kfree(PageBuffer0);
2059
2060         printk(KERN_INFO "MS_CardInit end\n");
2061         return result;
2062 }
2063
2064 static int ene_ms_init(struct us_data *us)
2065 {
2066         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2067         int result;
2068         u16 MSP_BlockSize, MSP_UserAreaBlocks;
2069         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2070         u8 *bbuf = info->bbuf;
2071         unsigned int s;
2072
2073         printk(KERN_INFO "transport --- ENE_MSInit\n");
2074
2075         /* the same part to test ENE */
2076
2077         result = ene_load_bincode(us, MS_INIT_PATTERN);
2078         if (result != USB_STOR_XFER_GOOD) {
2079                 printk(KERN_ERR "Load MS Init Code Fail !!\n");
2080                 return USB_STOR_TRANSPORT_ERROR;
2081         }
2082
2083         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2084         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2085         bcb->DataTransferLength = 0x200;
2086         bcb->Flags      = US_BULK_FLAG_IN;
2087         bcb->CDB[0]     = 0xF1;
2088         bcb->CDB[1]     = 0x01;
2089
2090         result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
2091         if (result != USB_STOR_XFER_GOOD) {
2092                 printk(KERN_ERR "Execution MS Init Code Fail !!\n");
2093                 return USB_STOR_TRANSPORT_ERROR;
2094         }
2095         /* the same part to test ENE */
2096         info->MS_Status = bbuf[0];
2097
2098         s = info->MS_Status;
2099         if ((s & MS_Insert) && (s & MS_Ready)) {
2100                 printk(KERN_INFO "Insert     = %x\n", !!(s & MS_Insert));
2101                 printk(KERN_INFO "Ready      = %x\n", !!(s & MS_Ready));
2102                 printk(KERN_INFO "IsMSPro    = %x\n", !!(s & MS_IsMSPro));
2103                 printk(KERN_INFO "IsMSPHG    = %x\n", !!(s & MS_IsMSPHG));
2104                 printk(KERN_INFO "WtP= %x\n", !!(s & MS_WtP));
2105                 if (s & MS_IsMSPro) {
2106                         MSP_BlockSize      = (bbuf[6] << 8) | bbuf[7];
2107                         MSP_UserAreaBlocks = (bbuf[10] << 8) | bbuf[11];
2108                         info->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
2109                 } else {
2110                         ms_card_init(us); /* Card is MS (to ms.c)*/
2111                 }
2112                 usb_stor_dbg(us, "MS Init Code OK !!\n");
2113         } else {
2114                 usb_stor_dbg(us, "MS Card Not Ready --- %x\n", bbuf[0]);
2115                 return USB_STOR_TRANSPORT_ERROR;
2116         }
2117
2118         return USB_STOR_TRANSPORT_GOOD;
2119 }
2120
2121 static int ene_sd_init(struct us_data *us)
2122 {
2123         int result;
2124         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2125         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2126         u8 *bbuf = info->bbuf;
2127
2128         usb_stor_dbg(us, "transport --- ENE_SDInit\n");
2129         /* SD Init Part-1 */
2130         result = ene_load_bincode(us, SD_INIT1_PATTERN);
2131         if (result != USB_STOR_XFER_GOOD) {
2132                 usb_stor_dbg(us, "Load SD Init Code Part-1 Fail !!\n");
2133                 return USB_STOR_TRANSPORT_ERROR;
2134         }
2135
2136         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2137         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2138         bcb->Flags = US_BULK_FLAG_IN;
2139         bcb->CDB[0] = 0xF2;
2140
2141         result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
2142         if (result != USB_STOR_XFER_GOOD) {
2143                 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2144                 return USB_STOR_TRANSPORT_ERROR;
2145         }
2146
2147         /* SD Init Part-2 */
2148         result = ene_load_bincode(us, SD_INIT2_PATTERN);
2149         if (result != USB_STOR_XFER_GOOD) {
2150                 usb_stor_dbg(us, "Load SD Init Code Part-2 Fail !!\n");
2151                 return USB_STOR_TRANSPORT_ERROR;
2152         }
2153
2154         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2155         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2156         bcb->DataTransferLength = 0x200;
2157         bcb->Flags              = US_BULK_FLAG_IN;
2158         bcb->CDB[0]             = 0xF1;
2159
2160         result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
2161         if (result != USB_STOR_XFER_GOOD) {
2162                 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2163                 return USB_STOR_TRANSPORT_ERROR;
2164         }
2165
2166         info->SD_Status = bbuf[0];
2167         if ((info->SD_Status & SD_Insert) && (info->SD_Status & SD_Ready)) {
2168                 unsigned int s = info->SD_Status;
2169
2170                 ene_get_card_status(us, bbuf);
2171                 usb_stor_dbg(us, "Insert     = %x\n", !!(s & SD_Insert));
2172                 usb_stor_dbg(us, "Ready      = %x\n", !!(s & SD_Ready));
2173                 usb_stor_dbg(us, "IsMMC      = %x\n", !!(s & SD_IsMMC));
2174                 usb_stor_dbg(us, "HiCapacity = %x\n", !!(s & SD_HiCapacity));
2175                 usb_stor_dbg(us, "HiSpeed    = %x\n", !!(s & SD_HiSpeed));
2176                 usb_stor_dbg(us, "WtP        = %x\n", !!(s & SD_WtP));
2177         } else {
2178                 usb_stor_dbg(us, "SD Card Not Ready --- %x\n", bbuf[0]);
2179                 return USB_STOR_TRANSPORT_ERROR;
2180         }
2181         return USB_STOR_TRANSPORT_GOOD;
2182 }
2183
2184
2185 static int ene_init(struct us_data *us)
2186 {
2187         int result;
2188         u8  misc_reg03;
2189         struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2190         u8 *bbuf = info->bbuf;
2191
2192         result = ene_get_card_type(us, REG_CARD_STATUS, bbuf);
2193         if (result != USB_STOR_XFER_GOOD)
2194                 return USB_STOR_TRANSPORT_ERROR;
2195
2196         misc_reg03 = bbuf[0];
2197         if (misc_reg03 & 0x01) {
2198                 if (!(info->SD_Status & SD_Ready)) {
2199                         result = ene_sd_init(us);
2200                         if (result != USB_STOR_XFER_GOOD)
2201                                 return USB_STOR_TRANSPORT_ERROR;
2202                 }
2203         }
2204         if (misc_reg03 & 0x02) {
2205                 if (!(info->MS_Status & MS_Ready)) {
2206                         result = ene_ms_init(us);
2207                         if (result != USB_STOR_XFER_GOOD)
2208                                 return USB_STOR_TRANSPORT_ERROR;
2209                 }
2210         }
2211         return result;
2212 }
2213
2214 /*----- sd_scsi_irp() ---------*/
2215 static int sd_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
2216 {
2217         int    result;
2218         struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2219
2220         switch (srb->cmnd[0]) {
2221         case TEST_UNIT_READY:
2222                 result = sd_scsi_test_unit_ready(us, srb);
2223                 break; /* 0x00 */
2224         case REQUEST_SENSE:
2225                 result = do_scsi_request_sense(us, srb);
2226                 break; /* 0x03 */
2227         case INQUIRY:
2228                 result = do_scsi_inquiry(us, srb);
2229                 break; /* 0x12 */
2230         case MODE_SENSE:
2231                 result = sd_scsi_mode_sense(us, srb);
2232                 break; /* 0x1A */
2233         /*
2234         case START_STOP:
2235                 result = SD_SCSI_Start_Stop(us, srb);
2236                 break; //0x1B
2237         */
2238         case READ_CAPACITY:
2239                 result = sd_scsi_read_capacity(us, srb);
2240                 break; /* 0x25 */
2241         case READ_10:
2242                 result = sd_scsi_read(us, srb);
2243                 break; /* 0x28 */
2244         case WRITE_10:
2245                 result = sd_scsi_write(us, srb);
2246                 break; /* 0x2A */
2247         default:
2248                 info->SrbStatus = SS_ILLEGAL_REQUEST;
2249                 result = USB_STOR_TRANSPORT_FAILED;
2250                 break;
2251         }
2252         if (result == USB_STOR_TRANSPORT_GOOD)
2253                 info->SrbStatus = SS_SUCCESS;
2254         return result;
2255 }
2256
2257 /*
2258  * ms_scsi_irp()
2259  */
2260 static int ms_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
2261 {
2262         int result;
2263         struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2264
2265         switch (srb->cmnd[0]) {
2266         case TEST_UNIT_READY:
2267                 result = ms_scsi_test_unit_ready(us, srb);
2268                 break; /* 0x00 */
2269         case REQUEST_SENSE:
2270                 result = do_scsi_request_sense(us, srb);
2271                 break; /* 0x03 */
2272         case INQUIRY:
2273                 result = do_scsi_inquiry(us, srb);
2274                 break; /* 0x12 */
2275         case MODE_SENSE:
2276                 result = ms_scsi_mode_sense(us, srb);
2277                 break; /* 0x1A */
2278         case READ_CAPACITY:
2279                 result = ms_scsi_read_capacity(us, srb);
2280                 break; /* 0x25 */
2281         case READ_10:
2282                 result = ms_scsi_read(us, srb);
2283                 break; /* 0x28 */
2284         case WRITE_10:
2285                 result = ms_scsi_write(us, srb);
2286                 break;  /* 0x2A */
2287         default:
2288                 info->SrbStatus = SS_ILLEGAL_REQUEST;
2289                 result = USB_STOR_TRANSPORT_FAILED;
2290                 break;
2291         }
2292         if (result == USB_STOR_TRANSPORT_GOOD)
2293                 info->SrbStatus = SS_SUCCESS;
2294         return result;
2295 }
2296
2297 static int ene_transport(struct scsi_cmnd *srb, struct us_data *us)
2298 {
2299         int result = USB_STOR_XFER_GOOD;
2300         struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2301
2302         /*US_DEBUG(usb_stor_show_command(us, srb)); */
2303         scsi_set_resid(srb, 0);
2304         if (unlikely(!(info->SD_Status & SD_Ready) || (info->MS_Status & MS_Ready)))
2305                 result = ene_init(us);
2306         if (result == USB_STOR_XFER_GOOD) {
2307                 result = USB_STOR_TRANSPORT_ERROR;
2308                 if (info->SD_Status & SD_Ready)
2309                         result = sd_scsi_irp(us, srb);
2310
2311                 if (info->MS_Status & MS_Ready)
2312                         result = ms_scsi_irp(us, srb);
2313         }
2314         return result;
2315 }
2316
2317 static struct scsi_host_template ene_ub6250_host_template;
2318
2319 static int ene_ub6250_probe(struct usb_interface *intf,
2320                          const struct usb_device_id *id)
2321 {
2322         int result;
2323         u8  misc_reg03;
2324         struct us_data *us;
2325         struct ene_ub6250_info *info;
2326
2327         result = usb_stor_probe1(&us, intf, id,
2328                    (id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list,
2329                    &ene_ub6250_host_template);
2330         if (result)
2331                 return result;
2332
2333         /* FIXME: where should the code alloc extra buf ? */
2334         us->extra = kzalloc(sizeof(struct ene_ub6250_info), GFP_KERNEL);
2335         if (!us->extra)
2336                 return -ENOMEM;
2337         us->extra_destructor = ene_ub6250_info_destructor;
2338
2339         info = (struct ene_ub6250_info *)(us->extra);
2340         info->bbuf = kmalloc(512, GFP_KERNEL);
2341         if (!info->bbuf) {
2342                 kfree(us->extra);
2343                 return -ENOMEM;
2344         }
2345
2346         us->transport_name = "ene_ub6250";
2347         us->transport = ene_transport;
2348         us->max_lun = 0;
2349
2350         result = usb_stor_probe2(us);
2351         if (result)
2352                 return result;
2353
2354         /* probe card type */
2355         result = ene_get_card_type(us, REG_CARD_STATUS, info->bbuf);
2356         if (result != USB_STOR_XFER_GOOD) {
2357                 usb_stor_disconnect(intf);
2358                 return USB_STOR_TRANSPORT_ERROR;
2359         }
2360
2361         misc_reg03 = info->bbuf[0];
2362         if (!(misc_reg03 & 0x01)) {
2363                 pr_info("ums_eneub6250: This driver only supports SD/MS cards. "
2364                         "It does not support SM cards.\n");
2365         }
2366
2367         return result;
2368 }
2369
2370
2371 #ifdef CONFIG_PM
2372
2373 static int ene_ub6250_resume(struct usb_interface *iface)
2374 {
2375         struct us_data *us = usb_get_intfdata(iface);
2376         struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2377
2378         mutex_lock(&us->dev_mutex);
2379
2380         if (us->suspend_resume_hook)
2381                 (us->suspend_resume_hook)(us, US_RESUME);
2382
2383         mutex_unlock(&us->dev_mutex);
2384
2385         info->Power_IsResum = true;
2386         /* info->SD_Status &= ~SD_Ready; */
2387         info->SD_Status = 0;
2388         info->MS_Status = 0;
2389         info->SM_Status = 0;
2390
2391         return 0;
2392 }
2393
2394 static int ene_ub6250_reset_resume(struct usb_interface *iface)
2395 {
2396         struct us_data *us = usb_get_intfdata(iface);
2397         struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2398
2399         /* Report the reset to the SCSI core */
2400         usb_stor_reset_resume(iface);
2401
2402         /*
2403          * FIXME: Notify the subdrivers that they need to reinitialize
2404          * the device
2405          */
2406         info->Power_IsResum = true;
2407         /* info->SD_Status &= ~SD_Ready; */
2408         info->SD_Status = 0;
2409         info->MS_Status = 0;
2410         info->SM_Status = 0;
2411
2412         return 0;
2413 }
2414
2415 #else
2416
2417 #define ene_ub6250_resume               NULL
2418 #define ene_ub6250_reset_resume         NULL
2419
2420 #endif
2421
2422 static struct usb_driver ene_ub6250_driver = {
2423         .name =         DRV_NAME,
2424         .probe =        ene_ub6250_probe,
2425         .disconnect =   usb_stor_disconnect,
2426         .suspend =      usb_stor_suspend,
2427         .resume =       ene_ub6250_resume,
2428         .reset_resume = ene_ub6250_reset_resume,
2429         .pre_reset =    usb_stor_pre_reset,
2430         .post_reset =   usb_stor_post_reset,
2431         .id_table =     ene_ub6250_usb_ids,
2432         .soft_unbind =  1,
2433         .no_dynamic_id = 1,
2434 };
2435
2436 module_usb_stor_driver(ene_ub6250_driver, ene_ub6250_host_template, DRV_NAME);