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