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