1 #include <linux/sched.h>
2 #include <linux/errno.h>
3 #include <linux/slab.h>
6 #include <scsi/scsi_eh.h>
7 #include <scsi/scsi_device.h>
11 #include "transport.h"
14 BYTE IsSSFDCCompliance
;
16 extern DWORD MediaChange
;
17 extern int Check_D_MediaFmt(struct us_data
*);
19 //----- ENE_InitMedia() ----------------------------------------
20 int ENE_InitMedia(struct us_data
*us
)
25 printk("--- Init Media ---\n");
26 result
= ENE_Read_BYTE(us
, REG_CARD_STATUS
, &MiscReg03
);
27 if (result
!= USB_STOR_XFER_GOOD
)
29 printk("Read register fail !!\n");
30 return USB_STOR_TRANSPORT_ERROR
;
32 printk("MiscReg03 = %x\n", MiscReg03
);
36 if (!us
->SD_Status
.Ready
)
38 result
= ENE_SDInit(us
);
39 if (result
!= USB_STOR_XFER_GOOD
)
40 return USB_STOR_TRANSPORT_ERROR
;
46 if (!us
->SM_Status
.Ready
&& !us
->MS_Status
.Ready
)
48 result
= ENE_SMInit(us
);
49 if (result
!= USB_STOR_XFER_GOOD
)
51 result
= ENE_MSInit(us
);
52 if (result
!= USB_STOR_XFER_GOOD
)
53 return USB_STOR_TRANSPORT_ERROR
;
61 //----- ENE_Read_BYTE() ----------------------------------------
62 int ENE_Read_BYTE(struct us_data
*us
, WORD index
, void *buf
)
64 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
67 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
68 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
69 bcb
->DataTransferLength
= 0x01;
72 bcb
->CDB
[2] = (BYTE
)(index
>>8);
73 bcb
->CDB
[3] = (BYTE
)index
;
75 result
= ENE_SendScsiCmd(us
, FDIR_READ
, buf
, 0);
79 //----- ENE_SDInit() ---------------------
80 int ENE_SDInit(struct us_data
*us
)
82 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
86 printk("transport --- ENE_SDInit\n");
88 result
= ENE_LoadBinCode(us
, SD_INIT1_PATTERN
);
89 if (result
!= USB_STOR_XFER_GOOD
)
91 printk("Load SD Init Code Part-1 Fail !!\n");
92 return USB_STOR_TRANSPORT_ERROR
;
95 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
96 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
100 result
= ENE_SendScsiCmd(us
, FDIR_READ
, NULL
, 0);
101 if (result
!= USB_STOR_XFER_GOOD
)
103 printk("Exection SD Init Code Fail !!\n");
104 return USB_STOR_TRANSPORT_ERROR
;
108 result
= ENE_LoadBinCode(us
, SD_INIT2_PATTERN
);
109 if (result
!= USB_STOR_XFER_GOOD
)
111 printk("Load SD Init Code Part-2 Fail !!\n");
112 return USB_STOR_TRANSPORT_ERROR
;
115 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
116 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
117 bcb
->DataTransferLength
= 0x200;
121 result
= ENE_SendScsiCmd(us
, FDIR_READ
, &buf
, 0);
122 if (result
!= USB_STOR_XFER_GOOD
)
124 printk("Exection SD Init Code Fail !!\n");
125 return USB_STOR_TRANSPORT_ERROR
;
128 us
->SD_Status
= *(PSD_STATUS
)&buf
[0];
129 if (us
->SD_Status
.Insert
&& us
->SD_Status
.Ready
)
131 ENE_ReadSDReg(us
, (PBYTE
)&buf
);
132 printk("Insert = %x\n", us
->SD_Status
.Insert
);
133 printk("Ready = %x\n", us
->SD_Status
.Ready
);
134 printk("IsMMC = %x\n", us
->SD_Status
.IsMMC
);
135 printk("HiCapacity = %x\n", us
->SD_Status
.HiCapacity
);
136 printk("HiSpeed = %x\n", us
->SD_Status
.HiSpeed
);
137 printk("WtP = %x\n", us
->SD_Status
.WtP
);
141 printk("SD Card Not Ready --- %x\n", buf
[0]);
142 return USB_STOR_TRANSPORT_ERROR
;
144 return USB_STOR_TRANSPORT_GOOD
;
147 //----- ENE_MSInit() ----------------------------------------
148 int ENE_MSInit(struct us_data
*us
)
150 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
153 WORD MSP_BlockSize
, MSP_UserAreaBlocks
;
156 printk("transport --- ENE_MSInit\n");
157 result
= ENE_LoadBinCode(us
, MS_INIT_PATTERN
);
158 if (result
!= USB_STOR_XFER_GOOD
)
160 printk("Load MS Init Code Fail !!\n");
161 return USB_STOR_TRANSPORT_ERROR
;
164 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
165 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
166 bcb
->DataTransferLength
= 0x200;
171 result
= ENE_SendScsiCmd(us
, FDIR_READ
, &buf
, 0);
172 if (result
!= USB_STOR_XFER_GOOD
)
174 printk("Exection MS Init Code Fail !!\n");
175 return USB_STOR_TRANSPORT_ERROR
;
178 us
->MS_Status
= *(PMS_STATUS
)&buf
[0];
180 if (us
->MS_Status
.Insert
&& us
->MS_Status
.Ready
)
182 printk("Insert = %x\n", us
->MS_Status
.Insert
);
183 printk("Ready = %x\n", us
->MS_Status
.Ready
);
184 printk("IsMSPro = %x\n", us
->MS_Status
.IsMSPro
);
185 printk("IsMSPHG = %x\n", us
->MS_Status
.IsMSPHG
);
186 printk("WtP = %x\n", us
->MS_Status
.WtP
);
187 if (us
->MS_Status
.IsMSPro
)
189 MSP_BlockSize
= (buf
[6] <<8) | buf
[7];
190 MSP_UserAreaBlocks
= (buf
[10]<<8) | buf
[11];
191 us
->MSP_TotalBlock
= MSP_BlockSize
* MSP_UserAreaBlocks
;
195 printk("MS Init Code OK !!\n");
199 printk("MS Card Not Ready --- %x\n", buf
[0]);
200 return USB_STOR_TRANSPORT_ERROR
;
203 return USB_STOR_TRANSPORT_GOOD
;
206 //----- ENE_SMInit() ----------------------------------------
207 int ENE_SMInit(struct us_data
*us
)
209 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
213 printk("transport --- ENE_SMInit\n");
215 result
= ENE_LoadBinCode(us
, SM_INIT_PATTERN
);
216 if (result
!= USB_STOR_XFER_GOOD
)
218 printk("Load SM Init Code Fail !!\n");
219 return USB_STOR_TRANSPORT_ERROR
;
222 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
223 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
224 bcb
->DataTransferLength
= 0x200;
229 result
= ENE_SendScsiCmd(us
, FDIR_READ
, &buf
, 0);
230 if (result
!= USB_STOR_XFER_GOOD
)
232 printk("Exection SM Init Code Fail !! result = %x\n", result
);
233 return USB_STOR_TRANSPORT_ERROR
;
236 us
->SM_Status
= *(PSM_STATUS
)&buf
[0];
238 us
->SM_DeviceID
= buf
[1];
239 us
->SM_CardID
= buf
[2];
241 if (us
->SM_Status
.Insert
&& us
->SM_Status
.Ready
)
243 printk("Insert = %x\n", us
->SM_Status
.Insert
);
244 printk("Ready = %x\n", us
->SM_Status
.Ready
);
245 printk("WtP = %x\n", us
->SM_Status
.WtP
);
246 printk("DeviceID = %x\n", us
->SM_DeviceID
);
247 printk("CardID = %x\n", us
->SM_CardID
);
249 Check_D_MediaFmt(us
);
253 printk("SM Card Not Ready --- %x\n", buf
[0]);
254 return USB_STOR_TRANSPORT_ERROR
;
257 return USB_STOR_TRANSPORT_GOOD
;
260 //----- ENE_ReadSDReg() ----------------------------------------------
261 int ENE_ReadSDReg(struct us_data
*us
, u8
*RdBuf
)
266 //printk("transport --- ENE_ReadSDReg\n");
267 reg4b
= *(PDWORD
)&RdBuf
[0x18];
268 us
->SD_READ_BL_LEN
= (BYTE
)((reg4b
>> 8) & 0x0f);
270 tmpreg
= (WORD
) reg4b
;
271 reg4b
= *(PDWORD
)(&RdBuf
[0x14]);
272 if (us
->SD_Status
.HiCapacity
&& !us
->SD_Status
.IsMMC
)
273 us
->HC_C_SIZE
= (reg4b
>> 8) & 0x3fffff;
275 us
->SD_C_SIZE
= ((tmpreg
& 0x03) << 10) | (WORD
)(reg4b
>> 22);
276 us
->SD_C_SIZE_MULT
= (BYTE
)(reg4b
>> 7) & 0x07;
277 if (us
->SD_Status
.HiCapacity
&& us
->SD_Status
.IsMMC
)
278 us
->HC_C_SIZE
= *(PDWORD
)(&RdBuf
[0x100]);
280 if (us
->SD_READ_BL_LEN
> SD_BLOCK_LEN
)
282 us
->SD_Block_Mult
= 1 << (us
->SD_READ_BL_LEN
- SD_BLOCK_LEN
); us
->SD_READ_BL_LEN
= SD_BLOCK_LEN
;
285 { us
->SD_Block_Mult
= 1;
287 return USB_STOR_TRANSPORT_GOOD
;
290 //----- ENE_LoadBinCode() ---------------------
291 int ENE_LoadBinCode(struct us_data
*us
, BYTE flag
)
293 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
298 //printk("transport --- ENE_LoadBinCode\n");
299 if (us
->BIN_FLAG
== flag
)
300 return USB_STOR_TRANSPORT_GOOD
;
302 buf
= kmalloc(0x800, GFP_KERNEL
);
304 return USB_STOR_TRANSPORT_ERROR
;
308 case SD_INIT1_PATTERN
:
309 printk("SD_INIT1_PATTERN\n");
310 memcpy(buf
, SD_Init1
, 0x800);
312 case SD_INIT2_PATTERN
:
313 printk("SD_INIT2_PATTERN\n");
314 memcpy(buf
, SD_Init2
, 0x800);
317 printk("SD_RW_PATTERN\n");
318 memcpy(buf
, SD_Rdwr
, 0x800);
321 case MS_INIT_PATTERN
:
322 printk("MS_INIT_PATTERN\n");
323 memcpy(buf
, MS_Init
, 0x800);
326 printk("MSP_RW_PATTERN\n");
327 memcpy(buf
, MSP_Rdwr
, 0x800);
330 printk("MS_RW_PATTERN\n");
331 memcpy(buf
, MS_Rdwr
, 0x800);
334 case SM_INIT_PATTERN
:
335 printk("SM_INIT_PATTERN\n");
336 memcpy(buf
, SM_Init
, 0x800);
339 printk("SM_RW_PATTERN\n");
340 memcpy(buf
, SM_Rdwr
, 0x800);
344 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
345 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
346 bcb
->DataTransferLength
= 0x800;
350 result
= ENE_SendScsiCmd(us
, FDIR_WRITE
, buf
, 0);
357 //----- ENE_SendScsiCmd() ---------------------
358 int ENE_SendScsiCmd(struct us_data
*us
, BYTE fDir
, void *buf
, int use_sg
)
360 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
361 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
364 unsigned int transfer_length
=bcb
->DataTransferLength
, cswlen
=0, partial
=0;
365 unsigned int residue
;
367 //printk("transport --- ENE_SendScsiCmd\n");
368 // send cmd to out endpoint
369 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
, US_BULK_CB_WRAP_LEN
, NULL
);
370 if (result
!= USB_STOR_XFER_GOOD
)
372 printk("send cmd to out endpoint fail ---\n");
373 return USB_STOR_TRANSPORT_ERROR
;
378 unsigned int pipe
= fDir
== FDIR_READ
? us
->recv_bulk_pipe
: us
->send_bulk_pipe
;
381 result
= usb_stor_bulk_srb(us
, pipe
, us
->srb
);
383 result
= usb_stor_bulk_transfer_sg(us
, pipe
, buf
, transfer_length
, 0, &partial
);
384 if (result
!= USB_STOR_XFER_GOOD
)
386 printk("data transfer fail ---\n");
387 return USB_STOR_TRANSPORT_ERROR
;
391 // Get CSW for device status
392 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
, US_BULK_CS_WRAP_LEN
, &cswlen
);
394 if (result
== USB_STOR_XFER_SHORT
&& cswlen
== 0)
396 printk("Received 0-length CSW; retrying...\n");
397 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
, US_BULK_CS_WRAP_LEN
, &cswlen
);
400 if (result
== USB_STOR_XFER_STALLED
)
402 /* get the status again */
403 printk("Attempting to get CSW (2nd try)...\n");
404 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
, US_BULK_CS_WRAP_LEN
, NULL
);
407 if (result
!= USB_STOR_XFER_GOOD
)
408 return USB_STOR_TRANSPORT_ERROR
;
410 /* check bulk status */
411 residue
= le32_to_cpu(bcs
->Residue
);
413 /* try to compute the actual residue, based on how much data
414 * was really transferred and what the device tells us */
415 if (residue
&& !(us
->fflags
& US_FL_IGNORE_RESIDUE
))
417 residue
= min(residue
, transfer_length
);
418 scsi_set_resid(us
->srb
, max(scsi_get_resid(us
->srb
), (int) residue
));
421 if (bcs
->Status
!= US_BULK_STAT_OK
)
422 return USB_STOR_TRANSPORT_ERROR
;
424 return USB_STOR_TRANSPORT_GOOD
;
427 //----- ENE_Read_Data() ---------------------
428 int ENE_Read_Data(struct us_data
*us
, void *buf
, unsigned int length
)
430 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
431 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
434 //printk("transport --- ENE_Read_Data\n");
435 // set up the command wrapper
436 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
437 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
438 bcb
->DataTransferLength
= length
;
444 // send cmd to out endpoint
445 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
, US_BULK_CB_WRAP_LEN
, NULL
);
446 if (result
!= USB_STOR_XFER_GOOD
)
447 return USB_STOR_TRANSPORT_ERROR
;
450 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, buf
, length
, NULL
);
451 if (result
!= USB_STOR_XFER_GOOD
)
452 return USB_STOR_TRANSPORT_ERROR
;
454 // Get CSW for device status
455 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
, US_BULK_CS_WRAP_LEN
, NULL
);
456 if (result
!= USB_STOR_XFER_GOOD
)
457 return USB_STOR_TRANSPORT_ERROR
;
458 if (bcs
->Status
!= US_BULK_STAT_OK
)
459 return USB_STOR_TRANSPORT_ERROR
;
461 return USB_STOR_TRANSPORT_GOOD
;
464 //----- ENE_Write_Data() ---------------------
465 int ENE_Write_Data(struct us_data
*us
, void *buf
, unsigned int length
)
467 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
468 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
471 //printk("transport --- ENE_Write_Data\n");
472 // set up the command wrapper
473 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
474 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
475 bcb
->DataTransferLength
= length
;
481 // send cmd to out endpoint
482 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
, US_BULK_CB_WRAP_LEN
, NULL
);
483 if (result
!= USB_STOR_XFER_GOOD
)
484 return USB_STOR_TRANSPORT_ERROR
;
487 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, buf
, length
, NULL
);
488 if (result
!= USB_STOR_XFER_GOOD
)
489 return USB_STOR_TRANSPORT_ERROR
;
491 // Get CSW for device status
492 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
, US_BULK_CS_WRAP_LEN
, NULL
);
493 if (result
!= USB_STOR_XFER_GOOD
)
494 return USB_STOR_TRANSPORT_ERROR
;
495 if (bcs
->Status
!= US_BULK_STAT_OK
)
496 return USB_STOR_TRANSPORT_ERROR
;
498 return USB_STOR_TRANSPORT_GOOD
;
501 //----- usb_stor_print_cmd() ---------------------
502 void usb_stor_print_cmd(struct scsi_cmnd
*srb
)
504 PBYTE Cdb
= srb
->cmnd
;
506 DWORD bn
= ((Cdb
[2]<<24) & 0xff000000) | ((Cdb
[3]<<16) & 0x00ff0000) |
507 ((Cdb
[4]<< 8) & 0x0000ff00) | ((Cdb
[5]<< 0) & 0x000000ff);
508 WORD blen
= ((Cdb
[7]<< 8) & 0xff00) | ((Cdb
[8]<< 0) & 0x00ff);
511 case TEST_UNIT_READY
:
512 //printk("scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd);
515 printk("scsi cmd %X --- SCSIOP_INQUIRY\n", cmd
);
518 printk("scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd
);
521 printk("scsi cmd %X --- SCSIOP_START_STOP\n", cmd
);
524 printk("scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd
);
527 //printk("scsi cmd %X --- SCSIOP_READ, bn = %X, blen = %X\n", cmd, bn, blen);
530 //printk("scsi cmd %X --- SCSIOP_WRITE, bn = %X, blen = %X\n", cmd, bn, blen);
532 case ALLOW_MEDIUM_REMOVAL
:
533 printk("scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd
);
536 printk("scsi cmd %X --- Other cmd\n", cmd
);