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