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