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"
17 int ENE_InitMedia(struct us_data
*us
)
22 printk(KERN_INFO
"--- Init Media ---\n");
23 result
= ENE_Read_BYTE(us
, REG_CARD_STATUS
, &MiscReg03
);
24 if (result
!= USB_STOR_XFER_GOOD
) {
25 printk(KERN_ERR
"Read register fail !!\n");
26 return USB_STOR_TRANSPORT_ERROR
;
28 printk(KERN_INFO
"MiscReg03 = %x\n", MiscReg03
);
30 if (MiscReg03
& 0x02) {
31 if (!us
->SM_Status
.Ready
&& !us
->MS_Status
.Ready
) {
32 result
= ENE_SMInit(us
);
33 if (result
!= USB_STOR_XFER_GOOD
) {
34 result
= ENE_MSInit(us
);
35 if (result
!= USB_STOR_XFER_GOOD
)
36 return USB_STOR_TRANSPORT_ERROR
;
47 int ENE_Read_BYTE(struct us_data
*us
, WORD index
, void *buf
)
49 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
52 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
53 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
54 bcb
->DataTransferLength
= 0x01;
57 bcb
->CDB
[2] = (BYTE
)(index
>>8);
58 bcb
->CDB
[3] = (BYTE
)index
;
60 result
= ENE_SendScsiCmd(us
, FDIR_READ
, buf
, 0);
67 int ENE_MSInit(struct us_data
*us
)
69 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
72 WORD MSP_BlockSize
, MSP_UserAreaBlocks
;
74 printk(KERN_INFO
"transport --- ENE_MSInit\n");
75 result
= ENE_LoadBinCode(us
, MS_INIT_PATTERN
);
76 if (result
!= USB_STOR_XFER_GOOD
) {
77 printk(KERN_ERR
"Load MS Init Code Fail !!\n");
78 return USB_STOR_TRANSPORT_ERROR
;
81 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
82 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
83 bcb
->DataTransferLength
= 0x200;
88 result
= ENE_SendScsiCmd(us
, FDIR_READ
, &buf
, 0);
89 if (result
!= USB_STOR_XFER_GOOD
) {
90 printk(KERN_ERR
"Execution MS Init Code Fail !!\n");
91 return USB_STOR_TRANSPORT_ERROR
;
94 us
->MS_Status
= *(PMS_STATUS
)&buf
[0];
96 if (us
->MS_Status
.Insert
&& us
->MS_Status
.Ready
) {
97 printk(KERN_INFO
"Insert = %x\n", us
->MS_Status
.Insert
);
98 printk(KERN_INFO
"Ready = %x\n", us
->MS_Status
.Ready
);
99 printk(KERN_INFO
"IsMSPro = %x\n", us
->MS_Status
.IsMSPro
);
100 printk(KERN_INFO
"IsMSPHG = %x\n", us
->MS_Status
.IsMSPHG
);
101 printk(KERN_INFO
"WtP = %x\n", us
->MS_Status
.WtP
);
102 if (us
->MS_Status
.IsMSPro
) {
103 MSP_BlockSize
= (buf
[6] << 8) | buf
[7];
104 MSP_UserAreaBlocks
= (buf
[10] << 8) | buf
[11];
105 us
->MSP_TotalBlock
= MSP_BlockSize
* MSP_UserAreaBlocks
;
109 printk(KERN_INFO
"MS Init Code OK !!\n");
111 printk(KERN_INFO
"MS Card Not Ready --- %x\n", buf
[0]);
112 return USB_STOR_TRANSPORT_ERROR
;
115 return USB_STOR_TRANSPORT_GOOD
;
121 int ENE_SMInit(struct us_data
*us
)
123 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
127 printk(KERN_INFO
"transport --- ENE_SMInit\n");
129 result
= ENE_LoadBinCode(us
, SM_INIT_PATTERN
);
130 if (result
!= USB_STOR_XFER_GOOD
) {
131 printk(KERN_INFO
"Load SM Init Code Fail !!\n");
132 return USB_STOR_TRANSPORT_ERROR
;
135 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
136 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
137 bcb
->DataTransferLength
= 0x200;
142 result
= ENE_SendScsiCmd(us
, FDIR_READ
, &buf
, 0);
143 if (result
!= USB_STOR_XFER_GOOD
) {
145 "Execution SM Init Code Fail !! result = %x\n", result
);
146 return USB_STOR_TRANSPORT_ERROR
;
149 us
->SM_Status
= *(PSM_STATUS
)&buf
[0];
151 us
->SM_DeviceID
= buf
[1];
152 us
->SM_CardID
= buf
[2];
154 if (us
->SM_Status
.Insert
&& us
->SM_Status
.Ready
) {
155 printk(KERN_INFO
"Insert = %x\n", us
->SM_Status
.Insert
);
156 printk(KERN_INFO
"Ready = %x\n", us
->SM_Status
.Ready
);
157 printk(KERN_INFO
"WtP = %x\n", us
->SM_Status
.WtP
);
158 printk(KERN_INFO
"DeviceID = %x\n", us
->SM_DeviceID
);
159 printk(KERN_INFO
"CardID = %x\n", us
->SM_CardID
);
161 Check_D_MediaFmt(us
);
163 printk(KERN_ERR
"SM Card Not Ready --- %x\n", buf
[0]);
164 return USB_STOR_TRANSPORT_ERROR
;
167 return USB_STOR_TRANSPORT_GOOD
;
173 int ENE_LoadBinCode(struct us_data
*us
, BYTE flag
)
175 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
180 /* printk(KERN_INFO "transport --- ENE_LoadBinCode\n"); */
181 if (us
->BIN_FLAG
== flag
)
182 return USB_STOR_TRANSPORT_GOOD
;
184 buf
= kmalloc(0x800, GFP_KERNEL
);
186 return USB_STOR_TRANSPORT_ERROR
;
189 case MS_INIT_PATTERN
:
190 printk(KERN_INFO
"MS_INIT_PATTERN\n");
191 memcpy(buf
, MS_Init
, 0x800);
194 printk(KERN_INFO
"MSP_RW_PATTERN\n");
195 memcpy(buf
, MSP_Rdwr
, 0x800);
198 printk(KERN_INFO
"MS_RW_PATTERN\n");
199 memcpy(buf
, MS_Rdwr
, 0x800);
202 case SM_INIT_PATTERN
:
203 printk(KERN_INFO
"SM_INIT_PATTERN\n");
204 memcpy(buf
, SM_Init
, 0x800);
207 printk(KERN_INFO
"SM_RW_PATTERN\n");
208 memcpy(buf
, SM_Rdwr
, 0x800);
212 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
213 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
214 bcb
->DataTransferLength
= 0x800;
218 result
= ENE_SendScsiCmd(us
, FDIR_WRITE
, buf
, 0);
228 int ENE_SendScsiCmd(struct us_data
*us
, BYTE fDir
, void *buf
, int use_sg
)
230 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
231 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
234 unsigned int transfer_length
= bcb
->DataTransferLength
,
235 cswlen
= 0, partial
= 0;
236 unsigned int residue
;
238 /* printk(KERN_INFO "transport --- ENE_SendScsiCmd\n"); */
239 /* send cmd to out endpoint */
240 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
241 bcb
, US_BULK_CB_WRAP_LEN
, NULL
);
242 if (result
!= USB_STOR_XFER_GOOD
) {
243 printk(KERN_ERR
"send cmd to out endpoint fail ---\n");
244 return USB_STOR_TRANSPORT_ERROR
;
248 unsigned int pipe
= fDir
;
250 if (fDir
== FDIR_READ
)
251 pipe
= us
->recv_bulk_pipe
;
253 pipe
= us
->send_bulk_pipe
;
257 result
= usb_stor_bulk_srb(us
, pipe
, us
->srb
);
259 result
= usb_stor_bulk_transfer_sg(us
, pipe
, buf
,
260 transfer_length
, 0, &partial
);
261 if (result
!= USB_STOR_XFER_GOOD
) {
262 printk(KERN_ERR
"data transfer fail ---\n");
263 return USB_STOR_TRANSPORT_ERROR
;
267 /* Get CSW for device status */
268 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
269 US_BULK_CS_WRAP_LEN
, &cswlen
);
271 if (result
== USB_STOR_XFER_SHORT
&& cswlen
== 0) {
272 printk(KERN_WARNING
"Received 0-length CSW; retrying...\n");
273 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
274 bcs
, US_BULK_CS_WRAP_LEN
, &cswlen
);
277 if (result
== USB_STOR_XFER_STALLED
) {
278 /* get the status again */
279 printk(KERN_WARNING
"Attempting to get CSW (2nd try)...\n");
280 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
281 bcs
, US_BULK_CS_WRAP_LEN
, NULL
);
284 if (result
!= USB_STOR_XFER_GOOD
)
285 return USB_STOR_TRANSPORT_ERROR
;
287 /* check bulk status */
288 residue
= le32_to_cpu(bcs
->Residue
);
291 * try to compute the actual residue, based on how much data
292 * was really transferred and what the device tells us
294 if (residue
&& !(us
->fflags
& US_FL_IGNORE_RESIDUE
)) {
295 residue
= min(residue
, transfer_length
);
297 scsi_set_resid(us
->srb
, max(scsi_get_resid(us
->srb
),
301 if (bcs
->Status
!= US_BULK_STAT_OK
)
302 return USB_STOR_TRANSPORT_ERROR
;
304 return USB_STOR_TRANSPORT_GOOD
;
310 int ENE_Read_Data(struct us_data
*us
, void *buf
, unsigned int length
)
312 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
313 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
316 /* printk(KERN_INFO "transport --- ENE_Read_Data\n"); */
317 /* set up the command wrapper */
318 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
319 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
320 bcb
->DataTransferLength
= length
;
326 /* send cmd to out endpoint */
327 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
,
328 US_BULK_CB_WRAP_LEN
, NULL
);
329 if (result
!= USB_STOR_XFER_GOOD
)
330 return USB_STOR_TRANSPORT_ERROR
;
333 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
335 if (result
!= USB_STOR_XFER_GOOD
)
336 return USB_STOR_TRANSPORT_ERROR
;
338 /* Get CSW for device status */
339 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
340 US_BULK_CS_WRAP_LEN
, NULL
);
341 if (result
!= USB_STOR_XFER_GOOD
)
342 return USB_STOR_TRANSPORT_ERROR
;
343 if (bcs
->Status
!= US_BULK_STAT_OK
)
344 return USB_STOR_TRANSPORT_ERROR
;
346 return USB_STOR_TRANSPORT_GOOD
;
352 int ENE_Write_Data(struct us_data
*us
, void *buf
, unsigned int length
)
354 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
355 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
358 /* printk("transport --- ENE_Write_Data\n"); */
359 /* set up the command wrapper */
360 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
361 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
362 bcb
->DataTransferLength
= length
;
368 /* send cmd to out endpoint */
369 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
,
370 US_BULK_CB_WRAP_LEN
, NULL
);
371 if (result
!= USB_STOR_XFER_GOOD
)
372 return USB_STOR_TRANSPORT_ERROR
;
375 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
377 if (result
!= USB_STOR_XFER_GOOD
)
378 return USB_STOR_TRANSPORT_ERROR
;
380 /* Get CSW for device status */
381 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
382 US_BULK_CS_WRAP_LEN
, NULL
);
383 if (result
!= USB_STOR_XFER_GOOD
)
384 return USB_STOR_TRANSPORT_ERROR
;
385 if (bcs
->Status
!= US_BULK_STAT_OK
)
386 return USB_STOR_TRANSPORT_ERROR
;
388 return USB_STOR_TRANSPORT_GOOD
;
392 * usb_stor_print_cmd():
394 void usb_stor_print_cmd(struct scsi_cmnd
*srb
)
396 PBYTE Cdb
= srb
->cmnd
;
398 DWORD bn
= ((Cdb
[2] << 24) & 0xff000000) |
399 ((Cdb
[3] << 16) & 0x00ff0000) |
400 ((Cdb
[4] << 8) & 0x0000ff00) |
401 ((Cdb
[5] << 0) & 0x000000ff);
402 WORD blen
= ((Cdb
[7] << 8) & 0xff00) | ((Cdb
[8] << 0) & 0x00ff);
405 case TEST_UNIT_READY
:
407 "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
410 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_INQUIRY\n", cmd
);
413 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd
);
416 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_START_STOP\n", cmd
);
419 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd
);
423 "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
428 "scsi cmd %X --- SCSIOP_WRITE,
429 bn = %X, blen = %X\n" , cmd, bn, blen); */
431 case ALLOW_MEDIUM_REMOVAL
:
433 "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd
);
436 printk(KERN_INFO
"scsi cmd %X --- Other cmd\n", cmd
);