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