GNU Linux-libre 5.10.217-gnu1
[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                 default:
857                         break;
858                 }
859         }
860
861         return Count;
862 }
863
864 static int ms_read_readpage(struct us_data *us, u32 PhyBlockAddr,
865                 u8 PageNum, u32 *PageBuf, struct ms_lib_type_extdat *ExtraDat)
866 {
867         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
868         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
869         u8 *bbuf = info->bbuf;
870         int result;
871         u32 bn = PhyBlockAddr * 0x20 + PageNum;
872
873         result = ene_load_bincode(us, MS_RW_PATTERN);
874         if (result != USB_STOR_XFER_GOOD)
875                 return USB_STOR_TRANSPORT_ERROR;
876
877         /* Read Page Data */
878         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
879         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
880         bcb->DataTransferLength = 0x200;
881         bcb->Flags      = US_BULK_FLAG_IN;
882         bcb->CDB[0]     = 0xF1;
883
884         bcb->CDB[1]     = 0x02; /* in init.c ENE_MSInit() is 0x01 */
885
886         bcb->CDB[5]     = (unsigned char)(bn);
887         bcb->CDB[4]     = (unsigned char)(bn>>8);
888         bcb->CDB[3]     = (unsigned char)(bn>>16);
889         bcb->CDB[2]     = (unsigned char)(bn>>24);
890
891         result = ene_send_scsi_cmd(us, FDIR_READ, PageBuf, 0);
892         if (result != USB_STOR_XFER_GOOD)
893                 return USB_STOR_TRANSPORT_ERROR;
894
895
896         /* Read Extra Data */
897         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
898         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
899         bcb->DataTransferLength = 0x4;
900         bcb->Flags      = US_BULK_FLAG_IN;
901         bcb->CDB[0]     = 0xF1;
902         bcb->CDB[1]     = 0x03;
903
904         bcb->CDB[5]     = (unsigned char)(PageNum);
905         bcb->CDB[4]     = (unsigned char)(PhyBlockAddr);
906         bcb->CDB[3]     = (unsigned char)(PhyBlockAddr>>8);
907         bcb->CDB[2]     = (unsigned char)(PhyBlockAddr>>16);
908         bcb->CDB[6]     = 0x01;
909
910         result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
911         if (result != USB_STOR_XFER_GOOD)
912                 return USB_STOR_TRANSPORT_ERROR;
913
914         ExtraDat->reserved = 0;
915         ExtraDat->intr     = 0x80;  /* Not yet,fireware support */
916         ExtraDat->status0  = 0x10;  /* Not yet,fireware support */
917
918         ExtraDat->status1  = 0x00;  /* Not yet,fireware support */
919         ExtraDat->ovrflg   = bbuf[0];
920         ExtraDat->mngflg   = bbuf[1];
921         ExtraDat->logadr   = memstick_logaddr(bbuf[2], bbuf[3]);
922
923         return USB_STOR_TRANSPORT_GOOD;
924 }
925
926 static int ms_lib_process_bootblock(struct us_data *us, u16 PhyBlock, u8 *PageData)
927 {
928         struct ms_bootblock_sysent *SysEntry;
929         struct ms_bootblock_sysinf *SysInfo;
930         u32 i, result;
931         u8 PageNumber;
932         u8 *PageBuffer;
933         struct ms_lib_type_extdat ExtraData;
934         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
935
936         PageBuffer = kzalloc(MS_BYTES_PER_PAGE * 2, GFP_KERNEL);
937         if (PageBuffer == NULL)
938                 return (u32)-1;
939
940         result = (u32)-1;
941
942         SysInfo = &(((struct ms_bootblock_page0 *)PageData)->sysinf);
943
944         if ((SysInfo->bMsClass != MS_SYSINF_MSCLASS_TYPE_1) ||
945                 (be16_to_cpu(SysInfo->wPageSize) != MS_SYSINF_PAGE_SIZE) ||
946                 ((SysInfo->bSecuritySupport & MS_SYSINF_SECURITY) == MS_SYSINF_SECURITY_SUPPORT) ||
947                 (SysInfo->bReserved1 != MS_SYSINF_RESERVED1) ||
948                 (SysInfo->bReserved2 != MS_SYSINF_RESERVED2) ||
949                 (SysInfo->bFormatType != MS_SYSINF_FORMAT_FAT) ||
950                 (SysInfo->bUsage != MS_SYSINF_USAGE_GENERAL))
951                 goto exit;
952                 /* */
953         switch (info->MS_Lib.cardType = SysInfo->bCardType) {
954         case MS_SYSINF_CARDTYPE_RDONLY:
955                 ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY);
956                 break;
957         case MS_SYSINF_CARDTYPE_RDWR:
958                 ms_lib_ctrl_reset(info, MS_LIB_CTRL_RDONLY);
959                 break;
960         case MS_SYSINF_CARDTYPE_HYBRID:
961         default:
962                 goto exit;
963         }
964
965         info->MS_Lib.blockSize = be16_to_cpu(SysInfo->wBlockSize);
966         info->MS_Lib.NumberOfPhyBlock = be16_to_cpu(SysInfo->wBlockNumber);
967         info->MS_Lib.NumberOfLogBlock = be16_to_cpu(SysInfo->wTotalBlockNumber)-2;
968         info->MS_Lib.PagesPerBlock = info->MS_Lib.blockSize * SIZE_OF_KIRO / MS_BYTES_PER_PAGE;
969         info->MS_Lib.NumberOfSegment = info->MS_Lib.NumberOfPhyBlock / MS_PHYSICAL_BLOCKS_PER_SEGMENT;
970         info->MS_Model = be16_to_cpu(SysInfo->wMemorySize);
971
972         /*Allocate to all number of logicalblock and physicalblock */
973         if (ms_lib_alloc_logicalmap(us))
974                 goto exit;
975
976         /* Mark the book block */
977         ms_lib_set_bootblockmark(us, PhyBlock);
978
979         SysEntry = &(((struct ms_bootblock_page0 *)PageData)->sysent);
980
981         for (i = 0; i < MS_NUMBER_OF_SYSTEM_ENTRY; i++) {
982                 u32  EntryOffset, EntrySize;
983
984                 EntryOffset = be32_to_cpu(SysEntry->entry[i].dwStart);
985
986                 if (EntryOffset == 0xffffff)
987                         continue;
988                 EntrySize = be32_to_cpu(SysEntry->entry[i].dwSize);
989
990                 if (EntrySize == 0)
991                         continue;
992
993                 if (EntryOffset + MS_BYTES_PER_PAGE + EntrySize > info->MS_Lib.blockSize * (u32)SIZE_OF_KIRO)
994                         continue;
995
996                 if (i == 0) {
997                         u8 PrevPageNumber = 0;
998                         u16 phyblk;
999
1000                         if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_INVALID_BLOCK)
1001                                 goto exit;
1002
1003                         while (EntrySize > 0) {
1004
1005                                 PageNumber = (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1);
1006                                 if (PageNumber != PrevPageNumber) {
1007                                         switch (ms_read_readpage(us, PhyBlock, PageNumber, (u32 *)PageBuffer, &ExtraData)) {
1008                                         case MS_STATUS_SUCCESS:
1009                                                 break;
1010                                         case MS_STATUS_WRITE_PROTECT:
1011                                         case MS_ERROR_FLASH_READ:
1012                                         case MS_STATUS_ERROR:
1013                                         default:
1014                                                 goto exit;
1015                                         }
1016
1017                                         PrevPageNumber = PageNumber;
1018                                 }
1019
1020                                 phyblk = be16_to_cpu(*(u16 *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)));
1021                                 if (phyblk < 0x0fff)
1022                                         ms_lib_set_initialerrorblock(us, phyblk);
1023
1024                                 EntryOffset += 2;
1025                                 EntrySize -= 2;
1026                         }
1027                 } else if (i == 1) {  /* CIS/IDI */
1028                         struct ms_bootblock_idi *idi;
1029
1030                         if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_CIS_IDI)
1031                                 goto exit;
1032
1033                         switch (ms_read_readpage(us, PhyBlock, (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1), (u32 *)PageBuffer, &ExtraData)) {
1034                         case MS_STATUS_SUCCESS:
1035                                 break;
1036                         case MS_STATUS_WRITE_PROTECT:
1037                         case MS_ERROR_FLASH_READ:
1038                         case MS_STATUS_ERROR:
1039                         default:
1040                                 goto exit;
1041                         }
1042
1043                         idi = &((struct ms_bootblock_cis_idi *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)))->idi.idi;
1044                         if (le16_to_cpu(idi->wIDIgeneralConfiguration) != MS_IDI_GENERAL_CONF)
1045                                 goto exit;
1046
1047                         info->MS_Lib.BytesPerSector = le16_to_cpu(idi->wIDIbytesPerSector);
1048                         if (info->MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE)
1049                                 goto exit;
1050                 }
1051         } /* End for .. */
1052
1053         result = 0;
1054
1055 exit:
1056         if (result)
1057                 ms_lib_free_logicalmap(us);
1058
1059         kfree(PageBuffer);
1060
1061         result = 0;
1062         return result;
1063 }
1064
1065 static void ms_lib_free_writebuf(struct us_data *us)
1066 {
1067         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1068         info->MS_Lib.wrtblk = (u16)-1; /* set to -1 */
1069
1070         /* memset((fdoExt)->MS_Lib.pagemap, 0, sizeof((fdoExt)->MS_Lib.pagemap)) */
1071
1072         ms_lib_clear_pagemap(info); /* (pdx)->MS_Lib.pagemap memset 0 in ms.h */
1073
1074         if (info->MS_Lib.blkpag) {
1075                 kfree(info->MS_Lib.blkpag);  /* Arnold test ... */
1076                 info->MS_Lib.blkpag = NULL;
1077         }
1078
1079         if (info->MS_Lib.blkext) {
1080                 kfree(info->MS_Lib.blkext);  /* Arnold test ... */
1081                 info->MS_Lib.blkext = NULL;
1082         }
1083 }
1084
1085
1086 static void ms_lib_free_allocatedarea(struct us_data *us)
1087 {
1088         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1089
1090         ms_lib_free_writebuf(us); /* Free MS_Lib.pagemap */
1091         ms_lib_free_logicalmap(us); /* kfree MS_Lib.Phy2LogMap and MS_Lib.Log2PhyMap */
1092
1093         /* set struct us point flag to 0 */
1094         info->MS_Lib.flags = 0;
1095         info->MS_Lib.BytesPerSector = 0;
1096         info->MS_Lib.SectorsPerCylinder = 0;
1097
1098         info->MS_Lib.cardType = 0;
1099         info->MS_Lib.blockSize = 0;
1100         info->MS_Lib.PagesPerBlock = 0;
1101
1102         info->MS_Lib.NumberOfPhyBlock = 0;
1103         info->MS_Lib.NumberOfLogBlock = 0;
1104 }
1105
1106
1107 static int ms_lib_alloc_writebuf(struct us_data *us)
1108 {
1109         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1110
1111         info->MS_Lib.wrtblk = (u16)-1;
1112
1113         info->MS_Lib.blkpag = kmalloc_array(info->MS_Lib.PagesPerBlock,
1114                                             info->MS_Lib.BytesPerSector,
1115                                             GFP_KERNEL);
1116         info->MS_Lib.blkext = kmalloc_array(info->MS_Lib.PagesPerBlock,
1117                                             sizeof(struct ms_lib_type_extdat),
1118                                             GFP_KERNEL);
1119
1120         if ((info->MS_Lib.blkpag == NULL) || (info->MS_Lib.blkext == NULL)) {
1121                 ms_lib_free_writebuf(us);
1122                 return (u32)-1;
1123         }
1124
1125         ms_lib_clear_writebuf(us);
1126
1127         return 0;
1128 }
1129
1130 static int ms_lib_force_setlogical_pair(struct us_data *us, u16 logblk, u16 phyblk)
1131 {
1132         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1133
1134         if (logblk == MS_LB_NOT_USED)
1135                 return 0;
1136
1137         if ((logblk >= info->MS_Lib.NumberOfLogBlock) ||
1138                 (phyblk >= info->MS_Lib.NumberOfPhyBlock))
1139                 return (u32)-1;
1140
1141         info->MS_Lib.Phy2LogMap[phyblk] = logblk;
1142         info->MS_Lib.Log2PhyMap[logblk] = phyblk;
1143
1144         return 0;
1145 }
1146
1147 static int ms_read_copyblock(struct us_data *us, u16 oldphy, u16 newphy,
1148                         u16 PhyBlockAddr, u8 PageNum, unsigned char *buf, u16 len)
1149 {
1150         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1151         int result;
1152
1153         result = ene_load_bincode(us, MS_RW_PATTERN);
1154         if (result != USB_STOR_XFER_GOOD)
1155                 return USB_STOR_TRANSPORT_ERROR;
1156
1157         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1158         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1159         bcb->DataTransferLength = 0x200*len;
1160         bcb->Flags = 0x00;
1161         bcb->CDB[0] = 0xF0;
1162         bcb->CDB[1] = 0x08;
1163         bcb->CDB[4] = (unsigned char)(oldphy);
1164         bcb->CDB[3] = (unsigned char)(oldphy>>8);
1165         bcb->CDB[2] = 0; /* (BYTE)(oldphy>>16) */
1166         bcb->CDB[7] = (unsigned char)(newphy);
1167         bcb->CDB[6] = (unsigned char)(newphy>>8);
1168         bcb->CDB[5] = 0; /* (BYTE)(newphy>>16) */
1169         bcb->CDB[9] = (unsigned char)(PhyBlockAddr);
1170         bcb->CDB[8] = (unsigned char)(PhyBlockAddr>>8);
1171         bcb->CDB[10] = PageNum;
1172
1173         result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1174         if (result != USB_STOR_XFER_GOOD)
1175                 return USB_STOR_TRANSPORT_ERROR;
1176
1177         return USB_STOR_TRANSPORT_GOOD;
1178 }
1179
1180 static int ms_read_eraseblock(struct us_data *us, u32 PhyBlockAddr)
1181 {
1182         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1183         int result;
1184         u32 bn = PhyBlockAddr;
1185
1186         result = ene_load_bincode(us, MS_RW_PATTERN);
1187         if (result != USB_STOR_XFER_GOOD)
1188                 return USB_STOR_TRANSPORT_ERROR;
1189
1190         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1191         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1192         bcb->DataTransferLength = 0x200;
1193         bcb->Flags = US_BULK_FLAG_IN;
1194         bcb->CDB[0] = 0xF2;
1195         bcb->CDB[1] = 0x06;
1196         bcb->CDB[4] = (unsigned char)(bn);
1197         bcb->CDB[3] = (unsigned char)(bn>>8);
1198         bcb->CDB[2] = (unsigned char)(bn>>16);
1199
1200         result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1201         if (result != USB_STOR_XFER_GOOD)
1202                 return USB_STOR_TRANSPORT_ERROR;
1203
1204         return USB_STOR_TRANSPORT_GOOD;
1205 }
1206
1207 static int ms_lib_check_disableblock(struct us_data *us, u16 PhyBlock)
1208 {
1209         unsigned char *PageBuf = NULL;
1210         u16 result = MS_STATUS_SUCCESS;
1211         u16 blk, index = 0;
1212         struct ms_lib_type_extdat extdat;
1213         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1214
1215         PageBuf = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1216         if (PageBuf == NULL) {
1217                 result = MS_NO_MEMORY_ERROR;
1218                 goto exit;
1219         }
1220
1221         ms_read_readpage(us, PhyBlock, 1, (u32 *)PageBuf, &extdat);
1222         do {
1223                 blk = be16_to_cpu(PageBuf[index]);
1224                 if (blk == MS_LB_NOT_USED)
1225                         break;
1226                 if (blk == info->MS_Lib.Log2PhyMap[0]) {
1227                         result = MS_ERROR_FLASH_READ;
1228                         break;
1229                 }
1230                 index++;
1231         } while (1);
1232
1233 exit:
1234         kfree(PageBuf);
1235         return result;
1236 }
1237
1238 static int ms_lib_setacquired_errorblock(struct us_data *us, u16 phyblk)
1239 {
1240         u16 log;
1241         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1242
1243         if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1244                 return (u32)-1;
1245
1246         log = info->MS_Lib.Phy2LogMap[phyblk];
1247
1248         if (log < info->MS_Lib.NumberOfLogBlock)
1249                 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1250
1251         if (info->MS_Lib.Phy2LogMap[phyblk] != MS_LB_INITIAL_ERROR)
1252                 info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_ACQUIRED_ERROR;
1253
1254         return 0;
1255 }
1256
1257 static int ms_lib_overwrite_extra(struct us_data *us, u32 PhyBlockAddr,
1258                                 u8 PageNum, u8 OverwriteFlag)
1259 {
1260         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1261         int result;
1262
1263         result = ene_load_bincode(us, MS_RW_PATTERN);
1264         if (result != USB_STOR_XFER_GOOD)
1265                 return USB_STOR_TRANSPORT_ERROR;
1266
1267         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1268         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1269         bcb->DataTransferLength = 0x4;
1270         bcb->Flags = US_BULK_FLAG_IN;
1271         bcb->CDB[0] = 0xF2;
1272         bcb->CDB[1] = 0x05;
1273         bcb->CDB[5] = (unsigned char)(PageNum);
1274         bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
1275         bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
1276         bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
1277         bcb->CDB[6] = OverwriteFlag;
1278         bcb->CDB[7] = 0xFF;
1279         bcb->CDB[8] = 0xFF;
1280         bcb->CDB[9] = 0xFF;
1281
1282         result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
1283         if (result != USB_STOR_XFER_GOOD)
1284                 return USB_STOR_TRANSPORT_ERROR;
1285
1286         return USB_STOR_TRANSPORT_GOOD;
1287 }
1288
1289 static int ms_lib_error_phyblock(struct us_data *us, u16 phyblk)
1290 {
1291         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1292
1293         if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1294                 return MS_STATUS_ERROR;
1295
1296         ms_lib_setacquired_errorblock(us, phyblk);
1297
1298         if (ms_lib_iswritable(info))
1299                 return ms_lib_overwrite_extra(us, phyblk, 0, (u8)(~MS_REG_OVR_BKST & BYTE_MASK));
1300
1301         return MS_STATUS_SUCCESS;
1302 }
1303
1304 static int ms_lib_erase_phyblock(struct us_data *us, u16 phyblk)
1305 {
1306         u16 log;
1307         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1308
1309         if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1310                 return MS_STATUS_ERROR;
1311
1312         log = info->MS_Lib.Phy2LogMap[phyblk];
1313
1314         if (log < info->MS_Lib.NumberOfLogBlock)
1315                 info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
1316
1317         info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED;
1318
1319         if (ms_lib_iswritable(info)) {
1320                 switch (ms_read_eraseblock(us, phyblk)) {
1321                 case MS_STATUS_SUCCESS:
1322                         info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED_ERASED;
1323                         return MS_STATUS_SUCCESS;
1324                 case MS_ERROR_FLASH_ERASE:
1325                 case MS_STATUS_INT_ERROR:
1326                         ms_lib_error_phyblock(us, phyblk);
1327                         return MS_ERROR_FLASH_ERASE;
1328                 case MS_STATUS_ERROR:
1329                 default:
1330                         ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY); /* MS_LibCtrlSet will used by ENE_MSInit ,need check, and why us to info*/
1331                         ms_lib_setacquired_errorblock(us, phyblk);
1332                         return MS_STATUS_ERROR;
1333                 }
1334         }
1335
1336         ms_lib_setacquired_errorblock(us, phyblk);
1337
1338         return MS_STATUS_SUCCESS;
1339 }
1340
1341 static int ms_lib_read_extra(struct us_data *us, u32 PhyBlock,
1342                                 u8 PageNum, struct ms_lib_type_extdat *ExtraDat)
1343 {
1344         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1345         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1346         u8 *bbuf = info->bbuf;
1347         int result;
1348
1349         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1350         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1351         bcb->DataTransferLength = 0x4;
1352         bcb->Flags      = US_BULK_FLAG_IN;
1353         bcb->CDB[0]     = 0xF1;
1354         bcb->CDB[1]     = 0x03;
1355         bcb->CDB[5]     = (unsigned char)(PageNum);
1356         bcb->CDB[4]     = (unsigned char)(PhyBlock);
1357         bcb->CDB[3]     = (unsigned char)(PhyBlock>>8);
1358         bcb->CDB[2]     = (unsigned char)(PhyBlock>>16);
1359         bcb->CDB[6]     = 0x01;
1360
1361         result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
1362         if (result != USB_STOR_XFER_GOOD)
1363                 return USB_STOR_TRANSPORT_ERROR;
1364
1365         ExtraDat->reserved = 0;
1366         ExtraDat->intr     = 0x80;  /* Not yet, waiting for fireware support */
1367         ExtraDat->status0  = 0x10;  /* Not yet, waiting for fireware support */
1368         ExtraDat->status1  = 0x00;  /* Not yet, waiting for fireware support */
1369         ExtraDat->ovrflg   = bbuf[0];
1370         ExtraDat->mngflg   = bbuf[1];
1371         ExtraDat->logadr   = memstick_logaddr(bbuf[2], bbuf[3]);
1372
1373         return USB_STOR_TRANSPORT_GOOD;
1374 }
1375
1376 static int ms_libsearch_block_from_physical(struct us_data *us, u16 phyblk)
1377 {
1378         u16 blk;
1379         struct ms_lib_type_extdat extdat; /* need check */
1380         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1381
1382
1383         if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
1384                 return MS_LB_ERROR;
1385
1386         for (blk = phyblk + 1; blk != phyblk; blk++) {
1387                 if ((blk & MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK) == 0)
1388                         blk -= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1389
1390                 if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED_ERASED) {
1391                         return blk;
1392                 } else if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED) {
1393                         switch (ms_lib_read_extra(us, blk, 0, &extdat)) {
1394                         case MS_STATUS_SUCCESS:
1395                         case MS_STATUS_SUCCESS_WITH_ECC:
1396                                 break;
1397                         case MS_NOCARD_ERROR:
1398                                 return MS_NOCARD_ERROR;
1399                         case MS_STATUS_INT_ERROR:
1400                                 return MS_LB_ERROR;
1401                         case MS_ERROR_FLASH_READ:
1402                         default:
1403                                 ms_lib_setacquired_errorblock(us, blk);
1404                                 continue;
1405                         } /* End switch */
1406
1407                         if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1408                                 ms_lib_setacquired_errorblock(us, blk);
1409                                 continue;
1410                         }
1411
1412                         switch (ms_lib_erase_phyblock(us, blk)) {
1413                         case MS_STATUS_SUCCESS:
1414                                 return blk;
1415                         case MS_STATUS_ERROR:
1416                                 return MS_LB_ERROR;
1417                         case MS_ERROR_FLASH_ERASE:
1418                         default:
1419                                 ms_lib_error_phyblock(us, blk);
1420                                 break;
1421                         }
1422                 }
1423         } /* End for */
1424
1425         return MS_LB_ERROR;
1426 }
1427 static int ms_libsearch_block_from_logical(struct us_data *us, u16 logblk)
1428 {
1429         u16 phyblk;
1430         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1431
1432         phyblk = ms_libconv_to_physical(info, logblk);
1433         if (phyblk >= MS_LB_ERROR) {
1434                 if (logblk >= info->MS_Lib.NumberOfLogBlock)
1435                         return MS_LB_ERROR;
1436
1437                 phyblk = (logblk + MS_NUMBER_OF_BOOT_BLOCK) / MS_LOGICAL_BLOCKS_PER_SEGMENT;
1438                 phyblk *= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1439                 phyblk += MS_PHYSICAL_BLOCKS_PER_SEGMENT - 1;
1440         }
1441
1442         return ms_libsearch_block_from_physical(us, phyblk);
1443 }
1444
1445 static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
1446 {
1447         struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
1448
1449         /* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
1450         if ((info->MS_Status & MS_Insert) && (info->MS_Status & MS_Ready)) {
1451                 return USB_STOR_TRANSPORT_GOOD;
1452         } else {
1453                 ene_ms_init(us);
1454                 return USB_STOR_TRANSPORT_GOOD;
1455         }
1456
1457         return USB_STOR_TRANSPORT_GOOD;
1458 }
1459
1460 static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
1461 {
1462         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1463         unsigned char mediaNoWP[12] = {
1464                 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
1465                 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1466         unsigned char mediaWP[12]   = {
1467                 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
1468                 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
1469
1470         if (info->MS_Status & MS_WtP)
1471                 usb_stor_set_xfer_buf(mediaWP, 12, srb);
1472         else
1473                 usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
1474
1475         return USB_STOR_TRANSPORT_GOOD;
1476 }
1477
1478 static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
1479 {
1480         u32   bl_num;
1481         u16    bl_len;
1482         unsigned int offset = 0;
1483         unsigned char    buf[8];
1484         struct scatterlist *sg = NULL;
1485         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1486
1487         usb_stor_dbg(us, "ms_scsi_read_capacity\n");
1488         bl_len = 0x200;
1489         if (info->MS_Status & MS_IsMSPro)
1490                 bl_num = info->MSP_TotalBlock - 1;
1491         else
1492                 bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1;
1493
1494         info->bl_num = bl_num;
1495         usb_stor_dbg(us, "bl_len = %x\n", bl_len);
1496         usb_stor_dbg(us, "bl_num = %x\n", bl_num);
1497
1498         /*srb->request_bufflen = 8; */
1499         buf[0] = (bl_num >> 24) & 0xff;
1500         buf[1] = (bl_num >> 16) & 0xff;
1501         buf[2] = (bl_num >> 8) & 0xff;
1502         buf[3] = (bl_num >> 0) & 0xff;
1503         buf[4] = (bl_len >> 24) & 0xff;
1504         buf[5] = (bl_len >> 16) & 0xff;
1505         buf[6] = (bl_len >> 8) & 0xff;
1506         buf[7] = (bl_len >> 0) & 0xff;
1507
1508         usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
1509
1510         return USB_STOR_TRANSPORT_GOOD;
1511 }
1512
1513 static void ms_lib_phy_to_log_range(u16 PhyBlock, u16 *LogStart, u16 *LogEnde)
1514 {
1515         PhyBlock /= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
1516
1517         if (PhyBlock) {
1518                 *LogStart = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT + (PhyBlock - 1) * MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1519                 *LogEnde = *LogStart + MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
1520         } else {
1521                 *LogStart = 0;
1522                 *LogEnde = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT;/*494*/
1523         }
1524 }
1525
1526 static int ms_lib_read_extrablock(struct us_data *us, u32 PhyBlock,
1527         u8 PageNum, u8 blen, void *buf)
1528 {
1529         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1530         int     result;
1531
1532         /* Read Extra Data */
1533         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1534         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1535         bcb->DataTransferLength = 0x4 * blen;
1536         bcb->Flags      = US_BULK_FLAG_IN;
1537         bcb->CDB[0]     = 0xF1;
1538         bcb->CDB[1]     = 0x03;
1539         bcb->CDB[5]     = (unsigned char)(PageNum);
1540         bcb->CDB[4]     = (unsigned char)(PhyBlock);
1541         bcb->CDB[3]     = (unsigned char)(PhyBlock>>8);
1542         bcb->CDB[2]     = (unsigned char)(PhyBlock>>16);
1543         bcb->CDB[6]     = blen;
1544
1545         result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1546         if (result != USB_STOR_XFER_GOOD)
1547                 return USB_STOR_TRANSPORT_ERROR;
1548
1549         return USB_STOR_TRANSPORT_GOOD;
1550 }
1551
1552 static int ms_lib_scan_logicalblocknumber(struct us_data *us, u16 btBlk1st)
1553 {
1554         u16 PhyBlock, newblk, i;
1555         u16 LogStart, LogEnde;
1556         struct ms_lib_type_extdat extdat;
1557         u32 count = 0, index = 0;
1558         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1559         u8 *bbuf = info->bbuf;
1560
1561         for (PhyBlock = 0; PhyBlock < info->MS_Lib.NumberOfPhyBlock;) {
1562                 ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde);
1563
1564                 for (i = 0; i < MS_PHYSICAL_BLOCKS_PER_SEGMENT; i++, PhyBlock++) {
1565                         switch (ms_libconv_to_logical(info, PhyBlock)) {
1566                         case MS_STATUS_ERROR:
1567                                 continue;
1568                         default:
1569                                 break;
1570                         }
1571
1572                         if (count == PhyBlock) {
1573                                 ms_lib_read_extrablock(us, PhyBlock, 0, 0x80,
1574                                                 bbuf);
1575                                 count += 0x80;
1576                         }
1577                         index = (PhyBlock % 0x80) * 4;
1578
1579                         extdat.ovrflg = bbuf[index];
1580                         extdat.mngflg = bbuf[index+1];
1581                         extdat.logadr = memstick_logaddr(bbuf[index+2],
1582                                         bbuf[index+3]);
1583
1584                         if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
1585                                 ms_lib_setacquired_errorblock(us, PhyBlock);
1586                                 continue;
1587                         }
1588
1589                         if ((extdat.mngflg & MS_REG_MNG_ATFLG) == MS_REG_MNG_ATFLG_ATTBL) {
1590                                 ms_lib_erase_phyblock(us, PhyBlock);
1591                                 continue;
1592                         }
1593
1594                         if (extdat.logadr != MS_LB_NOT_USED) {
1595                                 if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) {
1596                                         ms_lib_erase_phyblock(us, PhyBlock);
1597                                         continue;
1598                                 }
1599
1600                                 newblk = ms_libconv_to_physical(info, extdat.logadr);
1601
1602                                 if (newblk != MS_LB_NOT_USED) {
1603                                         if (extdat.logadr == 0) {
1604                                                 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1605                                                 if (ms_lib_check_disableblock(us, btBlk1st)) {
1606                                                         ms_lib_set_logicalpair(us, extdat.logadr, newblk);
1607                                                         continue;
1608                                                 }
1609                                         }
1610
1611                                         ms_lib_read_extra(us, newblk, 0, &extdat);
1612                                         if ((extdat.ovrflg & MS_REG_OVR_UDST) == MS_REG_OVR_UDST_UPDATING) {
1613                                                 ms_lib_erase_phyblock(us, PhyBlock);
1614                                                 continue;
1615                                         } else {
1616                                                 ms_lib_erase_phyblock(us, newblk);
1617                                         }
1618                                 }
1619
1620                                 ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
1621                         }
1622                 }
1623         } /* End for ... */
1624
1625         return MS_STATUS_SUCCESS;
1626 }
1627
1628
1629 static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
1630 {
1631         int result;
1632         unsigned char *cdb = srb->cmnd;
1633         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1634         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1635
1636         u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
1637                 ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
1638         u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1639         u32 blenByte = blen * 0x200;
1640
1641         if (bn > info->bl_num)
1642                 return USB_STOR_TRANSPORT_ERROR;
1643
1644         if (info->MS_Status & MS_IsMSPro) {
1645                 result = ene_load_bincode(us, MSP_RW_PATTERN);
1646                 if (result != USB_STOR_XFER_GOOD) {
1647                         usb_stor_dbg(us, "Load MPS RW pattern Fail !!\n");
1648                         return USB_STOR_TRANSPORT_ERROR;
1649                 }
1650
1651                 /* set up the command wrapper */
1652                 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1653                 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1654                 bcb->DataTransferLength = blenByte;
1655                 bcb->Flags  = US_BULK_FLAG_IN;
1656                 bcb->CDB[0] = 0xF1;
1657                 bcb->CDB[1] = 0x02;
1658                 bcb->CDB[5] = (unsigned char)(bn);
1659                 bcb->CDB[4] = (unsigned char)(bn>>8);
1660                 bcb->CDB[3] = (unsigned char)(bn>>16);
1661                 bcb->CDB[2] = (unsigned char)(bn>>24);
1662
1663                 result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
1664         } else {
1665                 void *buf;
1666                 int offset = 0;
1667                 u16 phyblk, logblk;
1668                 u8 PageNum;
1669                 u16 len;
1670                 u32 blkno;
1671
1672                 buf = kmalloc(blenByte, GFP_KERNEL);
1673                 if (buf == NULL)
1674                         return USB_STOR_TRANSPORT_ERROR;
1675
1676                 result = ene_load_bincode(us, MS_RW_PATTERN);
1677                 if (result != USB_STOR_XFER_GOOD) {
1678                         pr_info("Load MS RW pattern Fail !!\n");
1679                         result = USB_STOR_TRANSPORT_ERROR;
1680                         goto exit;
1681                 }
1682
1683                 logblk  = (u16)(bn / info->MS_Lib.PagesPerBlock);
1684                 PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
1685
1686                 while (1) {
1687                         if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1688                                 len = info->MS_Lib.PagesPerBlock-PageNum;
1689                         else
1690                                 len = blen;
1691
1692                         phyblk = ms_libconv_to_physical(info, logblk);
1693                         blkno  = phyblk * 0x20 + PageNum;
1694
1695                         /* set up the command wrapper */
1696                         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1697                         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1698                         bcb->DataTransferLength = 0x200 * len;
1699                         bcb->Flags  = US_BULK_FLAG_IN;
1700                         bcb->CDB[0] = 0xF1;
1701                         bcb->CDB[1] = 0x02;
1702                         bcb->CDB[5] = (unsigned char)(blkno);
1703                         bcb->CDB[4] = (unsigned char)(blkno>>8);
1704                         bcb->CDB[3] = (unsigned char)(blkno>>16);
1705                         bcb->CDB[2] = (unsigned char)(blkno>>24);
1706
1707                         result = ene_send_scsi_cmd(us, FDIR_READ, buf+offset, 0);
1708                         if (result != USB_STOR_XFER_GOOD) {
1709                                 pr_info("MS_SCSI_Read --- result = %x\n", result);
1710                                 result = USB_STOR_TRANSPORT_ERROR;
1711                                 goto exit;
1712                         }
1713
1714                         blen -= len;
1715                         if (blen <= 0)
1716                                 break;
1717                         logblk++;
1718                         PageNum = 0;
1719                         offset += MS_BYTES_PER_PAGE*len;
1720                 }
1721                 usb_stor_set_xfer_buf(buf, blenByte, srb);
1722 exit:
1723                 kfree(buf);
1724         }
1725         return result;
1726 }
1727
1728 static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
1729 {
1730         int result;
1731         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1732         unsigned char *cdb = srb->cmnd;
1733         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1734
1735         u32 bn = ((cdb[2] << 24) & 0xff000000) |
1736                         ((cdb[3] << 16) & 0x00ff0000) |
1737                         ((cdb[4] << 8) & 0x0000ff00) |
1738                         ((cdb[5] << 0) & 0x000000ff);
1739         u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
1740         u32 blenByte = blen * 0x200;
1741
1742         if (bn > info->bl_num)
1743                 return USB_STOR_TRANSPORT_ERROR;
1744
1745         if (info->MS_Status & MS_IsMSPro) {
1746                 result = ene_load_bincode(us, MSP_RW_PATTERN);
1747                 if (result != USB_STOR_XFER_GOOD) {
1748                         pr_info("Load MSP RW pattern Fail !!\n");
1749                         return USB_STOR_TRANSPORT_ERROR;
1750                 }
1751
1752                 /* set up the command wrapper */
1753                 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1754                 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1755                 bcb->DataTransferLength = blenByte;
1756                 bcb->Flags  = 0x00;
1757                 bcb->CDB[0] = 0xF0;
1758                 bcb->CDB[1] = 0x04;
1759                 bcb->CDB[5] = (unsigned char)(bn);
1760                 bcb->CDB[4] = (unsigned char)(bn>>8);
1761                 bcb->CDB[3] = (unsigned char)(bn>>16);
1762                 bcb->CDB[2] = (unsigned char)(bn>>24);
1763
1764                 result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
1765         } else {
1766                 void *buf;
1767                 int offset = 0;
1768                 u16 PhyBlockAddr;
1769                 u8 PageNum;
1770                 u16 len, oldphy, newphy;
1771
1772                 buf = kmalloc(blenByte, GFP_KERNEL);
1773                 if (buf == NULL)
1774                         return USB_STOR_TRANSPORT_ERROR;
1775                 usb_stor_set_xfer_buf(buf, blenByte, srb);
1776
1777                 result = ene_load_bincode(us, MS_RW_PATTERN);
1778                 if (result != USB_STOR_XFER_GOOD) {
1779                         pr_info("Load MS RW pattern Fail !!\n");
1780                         result = USB_STOR_TRANSPORT_ERROR;
1781                         goto exit;
1782                 }
1783
1784                 PhyBlockAddr = (u16)(bn / info->MS_Lib.PagesPerBlock);
1785                 PageNum      = (u8)(bn % info->MS_Lib.PagesPerBlock);
1786
1787                 while (1) {
1788                         if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
1789                                 len = info->MS_Lib.PagesPerBlock-PageNum;
1790                         else
1791                                 len = blen;
1792
1793                         oldphy = ms_libconv_to_physical(info, PhyBlockAddr); /* need check us <-> info */
1794                         newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr);
1795
1796                         result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len);
1797
1798                         if (result != USB_STOR_XFER_GOOD) {
1799                                 pr_info("MS_SCSI_Write --- result = %x\n", result);
1800                                 result =  USB_STOR_TRANSPORT_ERROR;
1801                                 goto exit;
1802                         }
1803
1804                         info->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED;
1805                         ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy);
1806
1807                         blen -= len;
1808                         if (blen <= 0)
1809                                 break;
1810                         PhyBlockAddr++;
1811                         PageNum = 0;
1812                         offset += MS_BYTES_PER_PAGE*len;
1813                 }
1814 exit:
1815                 kfree(buf);
1816         }
1817         return result;
1818 }
1819
1820 /*
1821  * ENE MS Card
1822  */
1823
1824 static int ene_get_card_type(struct us_data *us, u16 index, void *buf)
1825 {
1826         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1827         int result;
1828
1829         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1830         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1831         bcb->DataTransferLength = 0x01;
1832         bcb->Flags                      = US_BULK_FLAG_IN;
1833         bcb->CDB[0]                     = 0xED;
1834         bcb->CDB[2]                     = (unsigned char)(index>>8);
1835         bcb->CDB[3]                     = (unsigned char)index;
1836
1837         result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
1838         return result;
1839 }
1840
1841 static int ene_get_card_status(struct us_data *us, u8 *buf)
1842 {
1843         u16 tmpreg;
1844         u32 reg4b;
1845         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1846
1847         /*usb_stor_dbg(us, "transport --- ENE_ReadSDReg\n");*/
1848         reg4b = *(u32 *)&buf[0x18];
1849         info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f);
1850
1851         tmpreg = (u16) reg4b;
1852         reg4b = *(u32 *)(&buf[0x14]);
1853         if ((info->SD_Status & SD_HiCapacity) && !(info->SD_Status & SD_IsMMC))
1854                 info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
1855
1856         info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
1857         info->SD_C_SIZE_MULT = (u8)(reg4b >> 7)  & 0x07;
1858         if ((info->SD_Status & SD_HiCapacity) && (info->SD_Status & SD_IsMMC))
1859                 info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
1860
1861         if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
1862                 info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN);
1863                 info->SD_READ_BL_LEN = SD_BLOCK_LEN;
1864         } else {
1865                 info->SD_Block_Mult = 1;
1866         }
1867
1868         return USB_STOR_TRANSPORT_GOOD;
1869 }
1870
1871 static int ene_load_bincode(struct us_data *us, unsigned char flag)
1872 {
1873         int err;
1874         char *fw_name = NULL;
1875         unsigned char *buf = NULL;
1876         const struct firmware *sd_fw = NULL;
1877         int result = USB_STOR_TRANSPORT_ERROR;
1878         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1879         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1880
1881         if (info->BIN_FLAG == flag)
1882                 return USB_STOR_TRANSPORT_GOOD;
1883
1884         switch (flag) {
1885         /* For SD */
1886         case SD_INIT1_PATTERN:
1887                 usb_stor_dbg(us, "SD_INIT1_PATTERN\n");
1888                 fw_name = SD_INIT1_FIRMWARE;
1889                 break;
1890         case SD_INIT2_PATTERN:
1891                 usb_stor_dbg(us, "SD_INIT2_PATTERN\n");
1892                 fw_name = SD_INIT2_FIRMWARE;
1893                 break;
1894         case SD_RW_PATTERN:
1895                 usb_stor_dbg(us, "SD_RW_PATTERN\n");
1896                 fw_name = SD_RW_FIRMWARE;
1897                 break;
1898         /* For MS */
1899         case MS_INIT_PATTERN:
1900                 usb_stor_dbg(us, "MS_INIT_PATTERN\n");
1901                 fw_name = MS_INIT_FIRMWARE;
1902                 break;
1903         case MSP_RW_PATTERN:
1904                 usb_stor_dbg(us, "MSP_RW_PATTERN\n");
1905                 fw_name = MSP_RW_FIRMWARE;
1906                 break;
1907         case MS_RW_PATTERN:
1908                 usb_stor_dbg(us, "MS_RW_PATTERN\n");
1909                 fw_name = MS_RW_FIRMWARE;
1910                 break;
1911         default:
1912                 usb_stor_dbg(us, "----------- Unknown PATTERN ----------\n");
1913                 goto nofw;
1914         }
1915
1916         err = reject_firmware(&sd_fw, fw_name, &us->pusb_dev->dev);
1917         if (err) {
1918                 usb_stor_dbg(us, "load firmware %s failed\n", fw_name);
1919                 goto nofw;
1920         }
1921         buf = kmemdup(sd_fw->data, sd_fw->size, GFP_KERNEL);
1922         if (buf == NULL)
1923                 goto nofw;
1924
1925         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
1926         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1927         bcb->DataTransferLength = sd_fw->size;
1928         bcb->Flags = 0x00;
1929         bcb->CDB[0] = 0xEF;
1930
1931         result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
1932         if (us->srb != NULL)
1933                 scsi_set_resid(us->srb, 0);
1934         info->BIN_FLAG = flag;
1935         kfree(buf);
1936
1937 nofw:
1938         release_firmware(sd_fw);
1939         return result;
1940 }
1941
1942 static int ms_card_init(struct us_data *us)
1943 {
1944         u32 result;
1945         u16 TmpBlock;
1946         unsigned char *PageBuffer0 = NULL, *PageBuffer1 = NULL;
1947         struct ms_lib_type_extdat extdat;
1948         u16 btBlk1st, btBlk2nd;
1949         u32 btBlk1stErred;
1950         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
1951
1952         printk(KERN_INFO "MS_CardInit start\n");
1953
1954         ms_lib_free_allocatedarea(us); /* Clean buffer and set struct us_data flag to 0 */
1955
1956         /* get two PageBuffer */
1957         PageBuffer0 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1958         PageBuffer1 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
1959         if ((PageBuffer0 == NULL) || (PageBuffer1 == NULL)) {
1960                 result = MS_NO_MEMORY_ERROR;
1961                 goto exit;
1962         }
1963
1964         btBlk1st = btBlk2nd = MS_LB_NOT_USED;
1965         btBlk1stErred = 0;
1966
1967         for (TmpBlock = 0; TmpBlock < MS_MAX_INITIAL_ERROR_BLOCKS+2; TmpBlock++) {
1968
1969                 switch (ms_read_readpage(us, TmpBlock, 0, (u32 *)PageBuffer0, &extdat)) {
1970                 case MS_STATUS_SUCCESS:
1971                         break;
1972                 case MS_STATUS_INT_ERROR:
1973                         break;
1974                 case MS_STATUS_ERROR:
1975                 default:
1976                         continue;
1977                 }
1978
1979                 if ((extdat.ovrflg & MS_REG_OVR_BKST) == MS_REG_OVR_BKST_NG)
1980                         continue;
1981
1982                 if (((extdat.mngflg & MS_REG_MNG_SYSFLG) == MS_REG_MNG_SYSFLG_USER) ||
1983                         (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wBlockID) != MS_BOOT_BLOCK_ID) ||
1984                         (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wFormatVersion) != MS_BOOT_BLOCK_FORMAT_VERSION) ||
1985                         (((struct ms_bootblock_page0 *)PageBuffer0)->header.bNumberOfDataEntry != MS_BOOT_BLOCK_DATA_ENTRIES))
1986                                 continue;
1987
1988                 if (btBlk1st != MS_LB_NOT_USED) {
1989                         btBlk2nd = TmpBlock;
1990                         break;
1991                 }
1992
1993                 btBlk1st = TmpBlock;
1994                 memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE);
1995                 if (extdat.status1 & (MS_REG_ST1_DTER | MS_REG_ST1_EXER | MS_REG_ST1_FGER))
1996                         btBlk1stErred = 1;
1997         }
1998
1999         if (btBlk1st == MS_LB_NOT_USED) {
2000                 result = MS_STATUS_ERROR;
2001                 goto exit;
2002         }
2003
2004         /* write protect */
2005         if ((extdat.status0 & MS_REG_ST0_WP) == MS_REG_ST0_WP_ON)
2006                 ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2007
2008         result = MS_STATUS_ERROR;
2009         /* 1st Boot Block */
2010         if (btBlk1stErred == 0)
2011                 result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1);
2012                 /* 1st */
2013         /* 2nd Boot Block */
2014         if (result && (btBlk2nd != MS_LB_NOT_USED))
2015                 result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0);
2016
2017         if (result) {
2018                 result = MS_STATUS_ERROR;
2019                 goto exit;
2020         }
2021
2022         for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++)
2023                 info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2024
2025         info->MS_Lib.Phy2LogMap[btBlk1st] = MS_LB_BOOT_BLOCK;
2026
2027         if (btBlk2nd != MS_LB_NOT_USED) {
2028                 for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++)
2029                         info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
2030
2031                 info->MS_Lib.Phy2LogMap[btBlk2nd] = MS_LB_BOOT_BLOCK;
2032         }
2033
2034         result = ms_lib_scan_logicalblocknumber(us, btBlk1st);
2035         if (result)
2036                 goto exit;
2037
2038         for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT;
2039                 TmpBlock < info->MS_Lib.NumberOfPhyBlock;
2040                 TmpBlock += MS_PHYSICAL_BLOCKS_PER_SEGMENT) {
2041                 if (ms_count_freeblock(us, TmpBlock) == 0) {
2042                         ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
2043                         break;
2044                 }
2045         }
2046
2047         /* write */
2048         if (ms_lib_alloc_writebuf(us)) {
2049                 result = MS_NO_MEMORY_ERROR;
2050                 goto exit;
2051         }
2052
2053         result = MS_STATUS_SUCCESS;
2054
2055 exit:
2056         kfree(PageBuffer1);
2057         kfree(PageBuffer0);
2058
2059         printk(KERN_INFO "MS_CardInit end\n");
2060         return result;
2061 }
2062
2063 static int ene_ms_init(struct us_data *us)
2064 {
2065         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2066         int result;
2067         u16 MSP_BlockSize, MSP_UserAreaBlocks;
2068         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2069         u8 *bbuf = info->bbuf;
2070         unsigned int s;
2071
2072         printk(KERN_INFO "transport --- ENE_MSInit\n");
2073
2074         /* the same part to test ENE */
2075
2076         result = ene_load_bincode(us, MS_INIT_PATTERN);
2077         if (result != USB_STOR_XFER_GOOD) {
2078                 printk(KERN_ERR "Load MS Init Code Fail !!\n");
2079                 return USB_STOR_TRANSPORT_ERROR;
2080         }
2081
2082         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2083         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2084         bcb->DataTransferLength = 0x200;
2085         bcb->Flags      = US_BULK_FLAG_IN;
2086         bcb->CDB[0]     = 0xF1;
2087         bcb->CDB[1]     = 0x01;
2088
2089         result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
2090         if (result != USB_STOR_XFER_GOOD) {
2091                 printk(KERN_ERR "Execution MS Init Code Fail !!\n");
2092                 return USB_STOR_TRANSPORT_ERROR;
2093         }
2094         /* the same part to test ENE */
2095         info->MS_Status = bbuf[0];
2096
2097         s = info->MS_Status;
2098         if ((s & MS_Insert) && (s & MS_Ready)) {
2099                 printk(KERN_INFO "Insert     = %x\n", !!(s & MS_Insert));
2100                 printk(KERN_INFO "Ready      = %x\n", !!(s & MS_Ready));
2101                 printk(KERN_INFO "IsMSPro    = %x\n", !!(s & MS_IsMSPro));
2102                 printk(KERN_INFO "IsMSPHG    = %x\n", !!(s & MS_IsMSPHG));
2103                 printk(KERN_INFO "WtP= %x\n", !!(s & MS_WtP));
2104                 if (s & MS_IsMSPro) {
2105                         MSP_BlockSize      = (bbuf[6] << 8) | bbuf[7];
2106                         MSP_UserAreaBlocks = (bbuf[10] << 8) | bbuf[11];
2107                         info->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
2108                 } else {
2109                         ms_card_init(us); /* Card is MS (to ms.c)*/
2110                 }
2111                 usb_stor_dbg(us, "MS Init Code OK !!\n");
2112         } else {
2113                 usb_stor_dbg(us, "MS Card Not Ready --- %x\n", bbuf[0]);
2114                 return USB_STOR_TRANSPORT_ERROR;
2115         }
2116
2117         return USB_STOR_TRANSPORT_GOOD;
2118 }
2119
2120 static int ene_sd_init(struct us_data *us)
2121 {
2122         int result;
2123         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
2124         struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
2125         u8 *bbuf = info->bbuf;
2126
2127         usb_stor_dbg(us, "transport --- ENE_SDInit\n");
2128         /* SD Init Part-1 */
2129         result = ene_load_bincode(us, SD_INIT1_PATTERN);
2130         if (result != USB_STOR_XFER_GOOD) {
2131                 usb_stor_dbg(us, "Load SD Init Code Part-1 Fail !!\n");
2132                 return USB_STOR_TRANSPORT_ERROR;
2133         }
2134
2135         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2136         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2137         bcb->Flags = US_BULK_FLAG_IN;
2138         bcb->CDB[0] = 0xF2;
2139
2140         result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
2141         if (result != USB_STOR_XFER_GOOD) {
2142                 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2143                 return USB_STOR_TRANSPORT_ERROR;
2144         }
2145
2146         /* SD Init Part-2 */
2147         result = ene_load_bincode(us, SD_INIT2_PATTERN);
2148         if (result != USB_STOR_XFER_GOOD) {
2149                 usb_stor_dbg(us, "Load SD Init Code Part-2 Fail !!\n");
2150                 return USB_STOR_TRANSPORT_ERROR;
2151         }
2152
2153         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
2154         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
2155         bcb->DataTransferLength = 0x200;
2156         bcb->Flags              = US_BULK_FLAG_IN;
2157         bcb->CDB[0]             = 0xF1;
2158
2159         result = ene_send_scsi_cmd(us, FDIR_READ, bbuf, 0);
2160         if (result != USB_STOR_XFER_GOOD) {
2161                 usb_stor_dbg(us, "Execution SD Init Code Fail !!\n");
2162                 return USB_STOR_TRANSPORT_ERROR;
2163         }
2164
2165         info->SD_Status = bbuf[0];
2166         if ((info->SD_Status & SD_Insert) && (info->SD_Status & SD_Ready)) {
2167                 unsigned int s = info->SD_Status;
2168
2169                 ene_get_card_status(us, bbuf);
2170                 usb_stor_dbg(us, "Insert     = %x\n", !!(s & SD_Insert));
2171                 usb_stor_dbg(us, "Ready      = %x\n", !!(s & SD_Ready));
2172                 usb_stor_dbg(us, "IsMMC      = %x\n", !!(s & SD_IsMMC));
2173                 usb_stor_dbg(us, "HiCapacity = %x\n", !!(s & SD_HiCapacity));
2174                 usb_stor_dbg(us, "HiSpeed    = %x\n", !!(s & SD_HiSpeed));
2175                 usb_stor_dbg(us, "WtP        = %x\n", !!(s & SD_WtP));
2176         } else {
2177                 usb_stor_dbg(us, "SD Card Not Ready --- %x\n", bbuf[0]);
2178                 return USB_STOR_TRANSPORT_ERROR;
2179         }
2180         return USB_STOR_TRANSPORT_GOOD;
2181 }
2182
2183
2184 static int ene_init(struct us_data *us)
2185 {
2186         int result;
2187         u8  misc_reg03;
2188         struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2189         u8 *bbuf = info->bbuf;
2190
2191         result = ene_get_card_type(us, REG_CARD_STATUS, bbuf);
2192         if (result != USB_STOR_XFER_GOOD)
2193                 return USB_STOR_TRANSPORT_ERROR;
2194
2195         misc_reg03 = bbuf[0];
2196         if (misc_reg03 & 0x01) {
2197                 if (!(info->SD_Status & SD_Ready)) {
2198                         result = ene_sd_init(us);
2199                         if (result != USB_STOR_XFER_GOOD)
2200                                 return USB_STOR_TRANSPORT_ERROR;
2201                 }
2202         }
2203         if (misc_reg03 & 0x02) {
2204                 if (!(info->MS_Status & MS_Ready)) {
2205                         result = ene_ms_init(us);
2206                         if (result != USB_STOR_XFER_GOOD)
2207                                 return USB_STOR_TRANSPORT_ERROR;
2208                 }
2209         }
2210         return result;
2211 }
2212
2213 /*----- sd_scsi_irp() ---------*/
2214 static int sd_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
2215 {
2216         int    result;
2217         struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2218
2219         switch (srb->cmnd[0]) {
2220         case TEST_UNIT_READY:
2221                 result = sd_scsi_test_unit_ready(us, srb);
2222                 break; /* 0x00 */
2223         case REQUEST_SENSE:
2224                 result = do_scsi_request_sense(us, srb);
2225                 break; /* 0x03 */
2226         case INQUIRY:
2227                 result = do_scsi_inquiry(us, srb);
2228                 break; /* 0x12 */
2229         case MODE_SENSE:
2230                 result = sd_scsi_mode_sense(us, srb);
2231                 break; /* 0x1A */
2232         /*
2233         case START_STOP:
2234                 result = SD_SCSI_Start_Stop(us, srb);
2235                 break; //0x1B
2236         */
2237         case READ_CAPACITY:
2238                 result = sd_scsi_read_capacity(us, srb);
2239                 break; /* 0x25 */
2240         case READ_10:
2241                 result = sd_scsi_read(us, srb);
2242                 break; /* 0x28 */
2243         case WRITE_10:
2244                 result = sd_scsi_write(us, srb);
2245                 break; /* 0x2A */
2246         default:
2247                 info->SrbStatus = SS_ILLEGAL_REQUEST;
2248                 result = USB_STOR_TRANSPORT_FAILED;
2249                 break;
2250         }
2251         if (result == USB_STOR_TRANSPORT_GOOD)
2252                 info->SrbStatus = SS_SUCCESS;
2253         return result;
2254 }
2255
2256 /*
2257  * ms_scsi_irp()
2258  */
2259 static int ms_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
2260 {
2261         int result;
2262         struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
2263
2264         switch (srb->cmnd[0]) {
2265         case TEST_UNIT_READY:
2266                 result = ms_scsi_test_unit_ready(us, srb);
2267                 break; /* 0x00 */
2268         case REQUEST_SENSE:
2269                 result = do_scsi_request_sense(us, srb);
2270                 break; /* 0x03 */
2271         case INQUIRY:
2272                 result = do_scsi_inquiry(us, srb);
2273                 break; /* 0x12 */
2274         case MODE_SENSE:
2275                 result = ms_scsi_mode_sense(us, srb);
2276                 break; /* 0x1A */
2277         case READ_CAPACITY:
2278                 result = ms_scsi_read_capacity(us, srb);
2279                 break; /* 0x25 */
2280         case READ_10:
2281                 result = ms_scsi_read(us, srb);
2282                 break; /* 0x28 */
2283         case WRITE_10:
2284                 result = ms_scsi_write(us, srb);
2285                 break;  /* 0x2A */
2286         default:
2287                 info->SrbStatus = SS_ILLEGAL_REQUEST;
2288                 result = USB_STOR_TRANSPORT_FAILED;
2289                 break;
2290         }
2291         if (result == USB_STOR_TRANSPORT_GOOD)
2292                 info->SrbStatus = SS_SUCCESS;
2293         return result;
2294 }
2295
2296 static int ene_transport(struct scsi_cmnd *srb, struct us_data *us)
2297 {
2298         int result = USB_STOR_XFER_GOOD;
2299         struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2300
2301         /*US_DEBUG(usb_stor_show_command(us, srb)); */
2302         scsi_set_resid(srb, 0);
2303         if (unlikely(!(info->SD_Status & SD_Ready) || (info->MS_Status & MS_Ready)))
2304                 result = ene_init(us);
2305         if (result == USB_STOR_XFER_GOOD) {
2306                 result = USB_STOR_TRANSPORT_ERROR;
2307                 if (info->SD_Status & SD_Ready)
2308                         result = sd_scsi_irp(us, srb);
2309
2310                 if (info->MS_Status & MS_Ready)
2311                         result = ms_scsi_irp(us, srb);
2312         }
2313         return result;
2314 }
2315
2316 static struct scsi_host_template ene_ub6250_host_template;
2317
2318 static int ene_ub6250_probe(struct usb_interface *intf,
2319                          const struct usb_device_id *id)
2320 {
2321         int result;
2322         u8  misc_reg03;
2323         struct us_data *us;
2324         struct ene_ub6250_info *info;
2325
2326         result = usb_stor_probe1(&us, intf, id,
2327                    (id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list,
2328                    &ene_ub6250_host_template);
2329         if (result)
2330                 return result;
2331
2332         /* FIXME: where should the code alloc extra buf ? */
2333         us->extra = kzalloc(sizeof(struct ene_ub6250_info), GFP_KERNEL);
2334         if (!us->extra)
2335                 return -ENOMEM;
2336         us->extra_destructor = ene_ub6250_info_destructor;
2337
2338         info = (struct ene_ub6250_info *)(us->extra);
2339         info->bbuf = kmalloc(512, GFP_KERNEL);
2340         if (!info->bbuf) {
2341                 kfree(us->extra);
2342                 return -ENOMEM;
2343         }
2344
2345         us->transport_name = "ene_ub6250";
2346         us->transport = ene_transport;
2347         us->max_lun = 0;
2348
2349         result = usb_stor_probe2(us);
2350         if (result)
2351                 return result;
2352
2353         /* probe card type */
2354         result = ene_get_card_type(us, REG_CARD_STATUS, info->bbuf);
2355         if (result != USB_STOR_XFER_GOOD) {
2356                 usb_stor_disconnect(intf);
2357                 return USB_STOR_TRANSPORT_ERROR;
2358         }
2359
2360         misc_reg03 = info->bbuf[0];
2361         if (!(misc_reg03 & 0x01)) {
2362                 pr_info("ums_eneub6250: This driver only supports SD/MS cards. "
2363                         "It does not support SM cards.\n");
2364         }
2365
2366         return result;
2367 }
2368
2369
2370 #ifdef CONFIG_PM
2371
2372 static int ene_ub6250_resume(struct usb_interface *iface)
2373 {
2374         struct us_data *us = usb_get_intfdata(iface);
2375         struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2376
2377         mutex_lock(&us->dev_mutex);
2378
2379         if (us->suspend_resume_hook)
2380                 (us->suspend_resume_hook)(us, US_RESUME);
2381
2382         mutex_unlock(&us->dev_mutex);
2383
2384         info->Power_IsResum = true;
2385         /* info->SD_Status &= ~SD_Ready; */
2386         info->SD_Status = 0;
2387         info->MS_Status = 0;
2388         info->SM_Status = 0;
2389
2390         return 0;
2391 }
2392
2393 static int ene_ub6250_reset_resume(struct usb_interface *iface)
2394 {
2395         struct us_data *us = usb_get_intfdata(iface);
2396         struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
2397
2398         /* Report the reset to the SCSI core */
2399         usb_stor_reset_resume(iface);
2400
2401         /*
2402          * FIXME: Notify the subdrivers that they need to reinitialize
2403          * the device
2404          */
2405         info->Power_IsResum = true;
2406         /* info->SD_Status &= ~SD_Ready; */
2407         info->SD_Status = 0;
2408         info->MS_Status = 0;
2409         info->SM_Status = 0;
2410
2411         return 0;
2412 }
2413
2414 #else
2415
2416 #define ene_ub6250_resume               NULL
2417 #define ene_ub6250_reset_resume         NULL
2418
2419 #endif
2420
2421 static struct usb_driver ene_ub6250_driver = {
2422         .name =         DRV_NAME,
2423         .probe =        ene_ub6250_probe,
2424         .disconnect =   usb_stor_disconnect,
2425         .suspend =      usb_stor_suspend,
2426         .resume =       ene_ub6250_resume,
2427         .reset_resume = ene_ub6250_reset_resume,
2428         .pre_reset =    usb_stor_pre_reset,
2429         .post_reset =   usb_stor_post_reset,
2430         .id_table =     ene_ub6250_usb_ids,
2431         .soft_unbind =  1,
2432         .no_dynamic_id = 1,
2433 };
2434
2435 module_usb_stor_driver(ene_ub6250_driver, ene_ub6250_host_template, DRV_NAME);