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