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