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 return USB_STOR_TRANSPORT_ERROR
;
44 int ENE_Read_BYTE(struct us_data
*us
, WORD index
, void *buf
)
46 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
49 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
50 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
51 bcb
->DataTransferLength
= 0x01;
54 bcb
->CDB
[2] = (BYTE
)(index
>>8);
55 bcb
->CDB
[3] = (BYTE
)index
;
57 result
= ENE_SendScsiCmd(us
, FDIR_READ
, buf
, 0);
64 int ENE_SMInit(struct us_data
*us
)
66 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
70 printk(KERN_INFO
"transport --- ENE_SMInit\n");
72 result
= ENE_LoadBinCode(us
, SM_INIT_PATTERN
);
73 if (result
!= USB_STOR_XFER_GOOD
) {
74 printk(KERN_INFO
"Load SM Init Code Fail !!\n");
75 return USB_STOR_TRANSPORT_ERROR
;
78 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
79 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
80 bcb
->DataTransferLength
= 0x200;
85 result
= ENE_SendScsiCmd(us
, FDIR_READ
, &buf
, 0);
86 if (result
!= USB_STOR_XFER_GOOD
) {
88 "Execution SM Init Code Fail !! result = %x\n", result
);
89 return USB_STOR_TRANSPORT_ERROR
;
92 us
->SM_Status
= *(PSM_STATUS
)&buf
[0];
94 us
->SM_DeviceID
= buf
[1];
95 us
->SM_CardID
= buf
[2];
97 if (us
->SM_Status
.Insert
&& us
->SM_Status
.Ready
) {
98 printk(KERN_INFO
"Insert = %x\n", us
->SM_Status
.Insert
);
99 printk(KERN_INFO
"Ready = %x\n", us
->SM_Status
.Ready
);
100 printk(KERN_INFO
"WtP = %x\n", us
->SM_Status
.WtP
);
101 printk(KERN_INFO
"DeviceID = %x\n", us
->SM_DeviceID
);
102 printk(KERN_INFO
"CardID = %x\n", us
->SM_CardID
);
104 Check_D_MediaFmt(us
);
106 printk(KERN_ERR
"SM Card Not Ready --- %x\n", buf
[0]);
107 return USB_STOR_TRANSPORT_ERROR
;
110 return USB_STOR_TRANSPORT_GOOD
;
116 int ENE_LoadBinCode(struct us_data
*us
, BYTE flag
)
118 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
123 /* printk(KERN_INFO "transport --- ENE_LoadBinCode\n"); */
124 if (us
->BIN_FLAG
== flag
)
125 return USB_STOR_TRANSPORT_GOOD
;
127 buf
= kmalloc(0x800, GFP_KERNEL
);
129 return USB_STOR_TRANSPORT_ERROR
;
132 case SM_INIT_PATTERN
:
133 printk(KERN_INFO
"SM_INIT_PATTERN\n");
134 memcpy(buf
, SM_Init
, 0x800);
137 printk(KERN_INFO
"SM_RW_PATTERN\n");
138 memcpy(buf
, SM_Rdwr
, 0x800);
142 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
143 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
144 bcb
->DataTransferLength
= 0x800;
148 result
= ENE_SendScsiCmd(us
, FDIR_WRITE
, buf
, 0);
158 int ENE_SendScsiCmd(struct us_data
*us
, BYTE fDir
, void *buf
, int use_sg
)
160 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
161 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
164 unsigned int transfer_length
= bcb
->DataTransferLength
,
165 cswlen
= 0, partial
= 0;
166 unsigned int residue
;
168 /* printk(KERN_INFO "transport --- ENE_SendScsiCmd\n"); */
169 /* send cmd to out endpoint */
170 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
171 bcb
, US_BULK_CB_WRAP_LEN
, NULL
);
172 if (result
!= USB_STOR_XFER_GOOD
) {
173 printk(KERN_ERR
"send cmd to out endpoint fail ---\n");
174 return USB_STOR_TRANSPORT_ERROR
;
178 unsigned int pipe
= fDir
;
180 if (fDir
== FDIR_READ
)
181 pipe
= us
->recv_bulk_pipe
;
183 pipe
= us
->send_bulk_pipe
;
187 result
= usb_stor_bulk_srb(us
, pipe
, us
->srb
);
189 result
= usb_stor_bulk_transfer_sg(us
, pipe
, buf
,
190 transfer_length
, 0, &partial
);
191 if (result
!= USB_STOR_XFER_GOOD
) {
192 printk(KERN_ERR
"data transfer fail ---\n");
193 return USB_STOR_TRANSPORT_ERROR
;
197 /* Get CSW for device status */
198 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
199 US_BULK_CS_WRAP_LEN
, &cswlen
);
201 if (result
== USB_STOR_XFER_SHORT
&& cswlen
== 0) {
202 printk(KERN_WARNING
"Received 0-length CSW; retrying...\n");
203 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
204 bcs
, US_BULK_CS_WRAP_LEN
, &cswlen
);
207 if (result
== USB_STOR_XFER_STALLED
) {
208 /* get the status again */
209 printk(KERN_WARNING
"Attempting to get CSW (2nd try)...\n");
210 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
211 bcs
, US_BULK_CS_WRAP_LEN
, NULL
);
214 if (result
!= USB_STOR_XFER_GOOD
)
215 return USB_STOR_TRANSPORT_ERROR
;
217 /* check bulk status */
218 residue
= le32_to_cpu(bcs
->Residue
);
221 * try to compute the actual residue, based on how much data
222 * was really transferred and what the device tells us
224 if (residue
&& !(us
->fflags
& US_FL_IGNORE_RESIDUE
)) {
225 residue
= min(residue
, transfer_length
);
227 scsi_set_resid(us
->srb
, max(scsi_get_resid(us
->srb
),
231 if (bcs
->Status
!= US_BULK_STAT_OK
)
232 return USB_STOR_TRANSPORT_ERROR
;
234 return USB_STOR_TRANSPORT_GOOD
;
240 int ENE_Read_Data(struct us_data
*us
, void *buf
, unsigned int length
)
242 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
243 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
246 /* printk(KERN_INFO "transport --- ENE_Read_Data\n"); */
247 /* set up the command wrapper */
248 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
249 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
250 bcb
->DataTransferLength
= length
;
256 /* send cmd to out endpoint */
257 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
,
258 US_BULK_CB_WRAP_LEN
, NULL
);
259 if (result
!= USB_STOR_XFER_GOOD
)
260 return USB_STOR_TRANSPORT_ERROR
;
263 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
265 if (result
!= USB_STOR_XFER_GOOD
)
266 return USB_STOR_TRANSPORT_ERROR
;
268 /* Get CSW for device status */
269 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
270 US_BULK_CS_WRAP_LEN
, NULL
);
271 if (result
!= USB_STOR_XFER_GOOD
)
272 return USB_STOR_TRANSPORT_ERROR
;
273 if (bcs
->Status
!= US_BULK_STAT_OK
)
274 return USB_STOR_TRANSPORT_ERROR
;
276 return USB_STOR_TRANSPORT_GOOD
;
282 int ENE_Write_Data(struct us_data
*us
, void *buf
, unsigned int length
)
284 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
285 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
288 /* printk("transport --- ENE_Write_Data\n"); */
289 /* set up the command wrapper */
290 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
291 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
292 bcb
->DataTransferLength
= length
;
298 /* send cmd to out endpoint */
299 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
,
300 US_BULK_CB_WRAP_LEN
, NULL
);
301 if (result
!= USB_STOR_XFER_GOOD
)
302 return USB_STOR_TRANSPORT_ERROR
;
305 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
307 if (result
!= USB_STOR_XFER_GOOD
)
308 return USB_STOR_TRANSPORT_ERROR
;
310 /* Get CSW for device status */
311 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
312 US_BULK_CS_WRAP_LEN
, NULL
);
313 if (result
!= USB_STOR_XFER_GOOD
)
314 return USB_STOR_TRANSPORT_ERROR
;
315 if (bcs
->Status
!= US_BULK_STAT_OK
)
316 return USB_STOR_TRANSPORT_ERROR
;
318 return USB_STOR_TRANSPORT_GOOD
;
322 * usb_stor_print_cmd():
324 void usb_stor_print_cmd(struct scsi_cmnd
*srb
)
326 PBYTE Cdb
= srb
->cmnd
;
328 DWORD bn
= ((Cdb
[2] << 24) & 0xff000000) |
329 ((Cdb
[3] << 16) & 0x00ff0000) |
330 ((Cdb
[4] << 8) & 0x0000ff00) |
331 ((Cdb
[5] << 0) & 0x000000ff);
332 WORD blen
= ((Cdb
[7] << 8) & 0xff00) | ((Cdb
[8] << 0) & 0x00ff);
335 case TEST_UNIT_READY
:
337 "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
340 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_INQUIRY\n", cmd
);
343 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd
);
346 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_START_STOP\n", cmd
);
349 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd
);
353 "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
358 "scsi cmd %X --- SCSIOP_WRITE,
359 bn = %X, blen = %X\n" , cmd, bn, blen); */
361 case ALLOW_MEDIUM_REMOVAL
:
363 "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd
);
366 printk(KERN_INFO
"scsi cmd %X --- Other cmd\n", cmd
);