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