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