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
;
20 int ENE_InitMedia(struct us_data
*us
)
25 printk(KERN_INFO
"--- Init Media ---\n");
26 result
= ENE_Read_BYTE(us
, REG_CARD_STATUS
, &MiscReg03
);
27 if (result
!= USB_STOR_XFER_GOOD
) {
28 printk(KERN_ERR
"Read register fail !!\n");
29 return USB_STOR_TRANSPORT_ERROR
;
31 printk(KERN_INFO
"MiscReg03 = %x\n", MiscReg03
);
33 if (MiscReg03
& 0x02) {
34 if (!us
->SM_Status
.Ready
&& !us
->MS_Status
.Ready
) {
35 result
= ENE_SMInit(us
);
36 if (result
!= USB_STOR_XFER_GOOD
) {
37 result
= ENE_MSInit(us
);
38 if (result
!= USB_STOR_XFER_GOOD
)
39 return USB_STOR_TRANSPORT_ERROR
;
50 int ENE_Read_BYTE(struct us_data
*us
, WORD index
, void *buf
)
52 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
55 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
56 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
57 bcb
->DataTransferLength
= 0x01;
60 bcb
->CDB
[2] = (BYTE
)(index
>>8);
61 bcb
->CDB
[3] = (BYTE
)index
;
63 result
= ENE_SendScsiCmd(us
, FDIR_READ
, buf
, 0);
70 int ENE_MSInit(struct us_data
*us
)
72 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
75 WORD MSP_BlockSize
, MSP_UserAreaBlocks
;
77 printk(KERN_INFO
"transport --- ENE_MSInit\n");
78 result
= ENE_LoadBinCode(us
, MS_INIT_PATTERN
);
79 if (result
!= USB_STOR_XFER_GOOD
) {
80 printk(KERN_ERR
"Load MS Init Code Fail !!\n");
81 return USB_STOR_TRANSPORT_ERROR
;
84 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
85 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
86 bcb
->DataTransferLength
= 0x200;
91 result
= ENE_SendScsiCmd(us
, FDIR_READ
, &buf
, 0);
92 if (result
!= USB_STOR_XFER_GOOD
) {
93 printk(KERN_ERR
"Execution MS Init Code Fail !!\n");
94 return USB_STOR_TRANSPORT_ERROR
;
97 us
->MS_Status
= *(PMS_STATUS
)&buf
[0];
99 if (us
->MS_Status
.Insert
&& us
->MS_Status
.Ready
) {
100 printk(KERN_INFO
"Insert = %x\n", us
->MS_Status
.Insert
);
101 printk(KERN_INFO
"Ready = %x\n", us
->MS_Status
.Ready
);
102 printk(KERN_INFO
"IsMSPro = %x\n", us
->MS_Status
.IsMSPro
);
103 printk(KERN_INFO
"IsMSPHG = %x\n", us
->MS_Status
.IsMSPHG
);
104 printk(KERN_INFO
"WtP = %x\n", us
->MS_Status
.WtP
);
105 if (us
->MS_Status
.IsMSPro
) {
106 MSP_BlockSize
= (buf
[6] << 8) | buf
[7];
107 MSP_UserAreaBlocks
= (buf
[10] << 8) | buf
[11];
108 us
->MSP_TotalBlock
= MSP_BlockSize
* MSP_UserAreaBlocks
;
112 printk(KERN_INFO
"MS Init Code OK !!\n");
114 printk(KERN_INFO
"MS Card Not Ready --- %x\n", buf
[0]);
115 return USB_STOR_TRANSPORT_ERROR
;
118 return USB_STOR_TRANSPORT_GOOD
;
124 int ENE_SMInit(struct us_data
*us
)
126 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
130 printk(KERN_INFO
"transport --- ENE_SMInit\n");
132 result
= ENE_LoadBinCode(us
, SM_INIT_PATTERN
);
133 if (result
!= USB_STOR_XFER_GOOD
) {
134 printk(KERN_INFO
"Load SM Init Code Fail !!\n");
135 return USB_STOR_TRANSPORT_ERROR
;
138 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
139 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
140 bcb
->DataTransferLength
= 0x200;
145 result
= ENE_SendScsiCmd(us
, FDIR_READ
, &buf
, 0);
146 if (result
!= USB_STOR_XFER_GOOD
) {
148 "Execution SM Init Code Fail !! result = %x\n", result
);
149 return USB_STOR_TRANSPORT_ERROR
;
152 us
->SM_Status
= *(PSM_STATUS
)&buf
[0];
154 us
->SM_DeviceID
= buf
[1];
155 us
->SM_CardID
= buf
[2];
157 if (us
->SM_Status
.Insert
&& us
->SM_Status
.Ready
) {
158 printk(KERN_INFO
"Insert = %x\n", us
->SM_Status
.Insert
);
159 printk(KERN_INFO
"Ready = %x\n", us
->SM_Status
.Ready
);
160 printk(KERN_INFO
"WtP = %x\n", us
->SM_Status
.WtP
);
161 printk(KERN_INFO
"DeviceID = %x\n", us
->SM_DeviceID
);
162 printk(KERN_INFO
"CardID = %x\n", us
->SM_CardID
);
164 Check_D_MediaFmt(us
);
166 printk(KERN_ERR
"SM Card Not Ready --- %x\n", buf
[0]);
167 return USB_STOR_TRANSPORT_ERROR
;
170 return USB_STOR_TRANSPORT_GOOD
;
176 int ENE_LoadBinCode(struct us_data
*us
, BYTE flag
)
178 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
183 /* printk(KERN_INFO "transport --- ENE_LoadBinCode\n"); */
184 if (us
->BIN_FLAG
== flag
)
185 return USB_STOR_TRANSPORT_GOOD
;
187 buf
= kmalloc(0x800, GFP_KERNEL
);
189 return USB_STOR_TRANSPORT_ERROR
;
192 case MS_INIT_PATTERN
:
193 printk(KERN_INFO
"MS_INIT_PATTERN\n");
194 memcpy(buf
, MS_Init
, 0x800);
197 printk(KERN_INFO
"MSP_RW_PATTERN\n");
198 memcpy(buf
, MSP_Rdwr
, 0x800);
201 printk(KERN_INFO
"MS_RW_PATTERN\n");
202 memcpy(buf
, MS_Rdwr
, 0x800);
205 case SM_INIT_PATTERN
:
206 printk(KERN_INFO
"SM_INIT_PATTERN\n");
207 memcpy(buf
, SM_Init
, 0x800);
210 printk(KERN_INFO
"SM_RW_PATTERN\n");
211 memcpy(buf
, SM_Rdwr
, 0x800);
215 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
216 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
217 bcb
->DataTransferLength
= 0x800;
221 result
= ENE_SendScsiCmd(us
, FDIR_WRITE
, buf
, 0);
231 int ENE_SendScsiCmd(struct us_data
*us
, BYTE fDir
, void *buf
, int use_sg
)
233 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
234 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
237 unsigned int transfer_length
= bcb
->DataTransferLength
,
238 cswlen
= 0, partial
= 0;
239 unsigned int residue
;
241 /* printk(KERN_INFO "transport --- ENE_SendScsiCmd\n"); */
242 /* send cmd to out endpoint */
243 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
244 bcb
, US_BULK_CB_WRAP_LEN
, NULL
);
245 if (result
!= USB_STOR_XFER_GOOD
) {
246 printk(KERN_ERR
"send cmd to out endpoint fail ---\n");
247 return USB_STOR_TRANSPORT_ERROR
;
251 unsigned int pipe
= fDir
;
253 if (fDir
== FDIR_READ
)
254 pipe
= us
->recv_bulk_pipe
;
256 pipe
= us
->send_bulk_pipe
;
260 result
= usb_stor_bulk_srb(us
, pipe
, us
->srb
);
262 result
= usb_stor_bulk_transfer_sg(us
, pipe
, buf
,
263 transfer_length
, 0, &partial
);
264 if (result
!= USB_STOR_XFER_GOOD
) {
265 printk(KERN_ERR
"data transfer fail ---\n");
266 return USB_STOR_TRANSPORT_ERROR
;
270 /* Get CSW for device status */
271 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
272 US_BULK_CS_WRAP_LEN
, &cswlen
);
274 if (result
== USB_STOR_XFER_SHORT
&& cswlen
== 0) {
275 printk(KERN_WARNING
"Received 0-length CSW; retrying...\n");
276 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
277 bcs
, US_BULK_CS_WRAP_LEN
, &cswlen
);
280 if (result
== USB_STOR_XFER_STALLED
) {
281 /* get the status again */
282 printk(KERN_WARNING
"Attempting to get CSW (2nd try)...\n");
283 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
284 bcs
, US_BULK_CS_WRAP_LEN
, NULL
);
287 if (result
!= USB_STOR_XFER_GOOD
)
288 return USB_STOR_TRANSPORT_ERROR
;
290 /* check bulk status */
291 residue
= le32_to_cpu(bcs
->Residue
);
294 * try to compute the actual residue, based on how much data
295 * was really transferred and what the device tells us
297 if (residue
&& !(us
->fflags
& US_FL_IGNORE_RESIDUE
)) {
298 residue
= min(residue
, transfer_length
);
300 scsi_set_resid(us
->srb
, max(scsi_get_resid(us
->srb
),
304 if (bcs
->Status
!= US_BULK_STAT_OK
)
305 return USB_STOR_TRANSPORT_ERROR
;
307 return USB_STOR_TRANSPORT_GOOD
;
313 int ENE_Read_Data(struct us_data
*us
, void *buf
, unsigned int length
)
315 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
316 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
319 /* printk(KERN_INFO "transport --- ENE_Read_Data\n"); */
320 /* set up the command wrapper */
321 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
322 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
323 bcb
->DataTransferLength
= length
;
329 /* send cmd to out endpoint */
330 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
,
331 US_BULK_CB_WRAP_LEN
, NULL
);
332 if (result
!= USB_STOR_XFER_GOOD
)
333 return USB_STOR_TRANSPORT_ERROR
;
336 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
338 if (result
!= USB_STOR_XFER_GOOD
)
339 return USB_STOR_TRANSPORT_ERROR
;
341 /* Get CSW for device status */
342 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
343 US_BULK_CS_WRAP_LEN
, NULL
);
344 if (result
!= USB_STOR_XFER_GOOD
)
345 return USB_STOR_TRANSPORT_ERROR
;
346 if (bcs
->Status
!= US_BULK_STAT_OK
)
347 return USB_STOR_TRANSPORT_ERROR
;
349 return USB_STOR_TRANSPORT_GOOD
;
355 int ENE_Write_Data(struct us_data
*us
, void *buf
, unsigned int length
)
357 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
358 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
361 /* printk("transport --- ENE_Write_Data\n"); */
362 /* set up the command wrapper */
363 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
364 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
365 bcb
->DataTransferLength
= length
;
371 /* send cmd to out endpoint */
372 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
,
373 US_BULK_CB_WRAP_LEN
, NULL
);
374 if (result
!= USB_STOR_XFER_GOOD
)
375 return USB_STOR_TRANSPORT_ERROR
;
378 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
380 if (result
!= USB_STOR_XFER_GOOD
)
381 return USB_STOR_TRANSPORT_ERROR
;
383 /* Get CSW for device status */
384 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
385 US_BULK_CS_WRAP_LEN
, NULL
);
386 if (result
!= USB_STOR_XFER_GOOD
)
387 return USB_STOR_TRANSPORT_ERROR
;
388 if (bcs
->Status
!= US_BULK_STAT_OK
)
389 return USB_STOR_TRANSPORT_ERROR
;
391 return USB_STOR_TRANSPORT_GOOD
;
395 * usb_stor_print_cmd():
397 void usb_stor_print_cmd(struct scsi_cmnd
*srb
)
399 PBYTE Cdb
= srb
->cmnd
;
401 DWORD bn
= ((Cdb
[2] << 24) & 0xff000000) |
402 ((Cdb
[3] << 16) & 0x00ff0000) |
403 ((Cdb
[4] << 8) & 0x0000ff00) |
404 ((Cdb
[5] << 0) & 0x000000ff);
405 WORD blen
= ((Cdb
[7] << 8) & 0xff00) | ((Cdb
[8] << 0) & 0x00ff);
408 case TEST_UNIT_READY
:
410 "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
413 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_INQUIRY\n", cmd
);
416 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd
);
419 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_START_STOP\n", cmd
);
422 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd
);
426 "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
431 "scsi cmd %X --- SCSIOP_WRITE,
432 bn = %X, blen = %X\n" , cmd, bn, blen); */
434 case ALLOW_MEDIUM_REMOVAL
:
436 "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd
);
439 printk(KERN_INFO
"scsi cmd %X --- Other cmd\n", cmd
);