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