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
;
45 int ENE_Read_BYTE(struct us_data
*us
, WORD index
, void *buf
)
47 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
50 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
51 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
52 bcb
->DataTransferLength
= 0x01;
55 bcb
->CDB
[2] = (BYTE
)(index
>>8);
56 bcb
->CDB
[3] = (BYTE
)index
;
58 result
= ENE_SendScsiCmd(us
, FDIR_READ
, buf
, 0);
65 int ENE_SMInit(struct us_data
*us
)
67 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
71 printk(KERN_INFO
"transport --- ENE_SMInit\n");
73 result
= ENE_LoadBinCode(us
, SM_INIT_PATTERN
);
74 if (result
!= USB_STOR_XFER_GOOD
) {
75 printk(KERN_INFO
"Load SM Init Code Fail !!\n");
76 return USB_STOR_TRANSPORT_ERROR
;
79 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
80 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
81 bcb
->DataTransferLength
= 0x200;
86 result
= ENE_SendScsiCmd(us
, FDIR_READ
, &buf
, 0);
87 if (result
!= USB_STOR_XFER_GOOD
) {
89 "Execution SM Init Code Fail !! result = %x\n", result
);
90 return USB_STOR_TRANSPORT_ERROR
;
93 us
->SM_Status
= *(PSM_STATUS
)&buf
[0];
95 us
->SM_DeviceID
= buf
[1];
96 us
->SM_CardID
= buf
[2];
98 if (us
->SM_Status
.Insert
&& us
->SM_Status
.Ready
) {
99 printk(KERN_INFO
"Insert = %x\n", us
->SM_Status
.Insert
);
100 printk(KERN_INFO
"Ready = %x\n", us
->SM_Status
.Ready
);
101 printk(KERN_INFO
"WtP = %x\n", us
->SM_Status
.WtP
);
102 printk(KERN_INFO
"DeviceID = %x\n", us
->SM_DeviceID
);
103 printk(KERN_INFO
"CardID = %x\n", us
->SM_CardID
);
105 Check_D_MediaFmt(us
);
107 printk(KERN_ERR
"SM Card Not Ready --- %x\n", buf
[0]);
108 return USB_STOR_TRANSPORT_ERROR
;
111 return USB_STOR_TRANSPORT_GOOD
;
117 int ENE_LoadBinCode(struct us_data
*us
, BYTE flag
)
119 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
124 /* printk(KERN_INFO "transport --- ENE_LoadBinCode\n"); */
125 if (us
->BIN_FLAG
== flag
)
126 return USB_STOR_TRANSPORT_GOOD
;
128 buf
= kmalloc(0x800, GFP_KERNEL
);
130 return USB_STOR_TRANSPORT_ERROR
;
133 case SM_INIT_PATTERN
:
134 printk(KERN_INFO
"SM_INIT_PATTERN\n");
135 memcpy(buf
, SM_Init
, 0x800);
138 printk(KERN_INFO
"SM_RW_PATTERN\n");
139 memcpy(buf
, SM_Rdwr
, 0x800);
143 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
144 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
145 bcb
->DataTransferLength
= 0x800;
149 result
= ENE_SendScsiCmd(us
, FDIR_WRITE
, buf
, 0);
159 int ENE_SendScsiCmd(struct us_data
*us
, BYTE fDir
, void *buf
, int use_sg
)
161 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
162 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
165 unsigned int transfer_length
= bcb
->DataTransferLength
,
166 cswlen
= 0, partial
= 0;
167 unsigned int residue
;
169 /* printk(KERN_INFO "transport --- ENE_SendScsiCmd\n"); */
170 /* send cmd to out endpoint */
171 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
172 bcb
, US_BULK_CB_WRAP_LEN
, NULL
);
173 if (result
!= USB_STOR_XFER_GOOD
) {
174 printk(KERN_ERR
"send cmd to out endpoint fail ---\n");
175 return USB_STOR_TRANSPORT_ERROR
;
179 unsigned int pipe
= fDir
;
181 if (fDir
== FDIR_READ
)
182 pipe
= us
->recv_bulk_pipe
;
184 pipe
= us
->send_bulk_pipe
;
188 result
= usb_stor_bulk_srb(us
, pipe
, us
->srb
);
190 result
= usb_stor_bulk_transfer_sg(us
, pipe
, buf
,
191 transfer_length
, 0, &partial
);
192 if (result
!= USB_STOR_XFER_GOOD
) {
193 printk(KERN_ERR
"data transfer fail ---\n");
194 return USB_STOR_TRANSPORT_ERROR
;
198 /* Get CSW for device status */
199 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
200 US_BULK_CS_WRAP_LEN
, &cswlen
);
202 if (result
== USB_STOR_XFER_SHORT
&& cswlen
== 0) {
203 printk(KERN_WARNING
"Received 0-length CSW; retrying...\n");
204 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
205 bcs
, US_BULK_CS_WRAP_LEN
, &cswlen
);
208 if (result
== USB_STOR_XFER_STALLED
) {
209 /* get the status again */
210 printk(KERN_WARNING
"Attempting to get CSW (2nd try)...\n");
211 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
212 bcs
, US_BULK_CS_WRAP_LEN
, NULL
);
215 if (result
!= USB_STOR_XFER_GOOD
)
216 return USB_STOR_TRANSPORT_ERROR
;
218 /* check bulk status */
219 residue
= le32_to_cpu(bcs
->Residue
);
222 * try to compute the actual residue, based on how much data
223 * was really transferred and what the device tells us
225 if (residue
&& !(us
->fflags
& US_FL_IGNORE_RESIDUE
)) {
226 residue
= min(residue
, transfer_length
);
228 scsi_set_resid(us
->srb
, max(scsi_get_resid(us
->srb
),
232 if (bcs
->Status
!= US_BULK_STAT_OK
)
233 return USB_STOR_TRANSPORT_ERROR
;
235 return USB_STOR_TRANSPORT_GOOD
;
241 int ENE_Read_Data(struct us_data
*us
, void *buf
, unsigned int length
)
243 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
244 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
247 /* printk(KERN_INFO "transport --- ENE_Read_Data\n"); */
248 /* set up the command wrapper */
249 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
250 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
251 bcb
->DataTransferLength
= length
;
257 /* send cmd to out endpoint */
258 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
,
259 US_BULK_CB_WRAP_LEN
, NULL
);
260 if (result
!= USB_STOR_XFER_GOOD
)
261 return USB_STOR_TRANSPORT_ERROR
;
264 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
266 if (result
!= USB_STOR_XFER_GOOD
)
267 return USB_STOR_TRANSPORT_ERROR
;
269 /* Get CSW for device status */
270 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
271 US_BULK_CS_WRAP_LEN
, NULL
);
272 if (result
!= USB_STOR_XFER_GOOD
)
273 return USB_STOR_TRANSPORT_ERROR
;
274 if (bcs
->Status
!= US_BULK_STAT_OK
)
275 return USB_STOR_TRANSPORT_ERROR
;
277 return USB_STOR_TRANSPORT_GOOD
;
283 int ENE_Write_Data(struct us_data
*us
, void *buf
, unsigned int length
)
285 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
286 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
289 /* printk("transport --- ENE_Write_Data\n"); */
290 /* set up the command wrapper */
291 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
292 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
293 bcb
->DataTransferLength
= length
;
299 /* send cmd to out endpoint */
300 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
,
301 US_BULK_CB_WRAP_LEN
, NULL
);
302 if (result
!= USB_STOR_XFER_GOOD
)
303 return USB_STOR_TRANSPORT_ERROR
;
306 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
308 if (result
!= USB_STOR_XFER_GOOD
)
309 return USB_STOR_TRANSPORT_ERROR
;
311 /* Get CSW for device status */
312 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
313 US_BULK_CS_WRAP_LEN
, NULL
);
314 if (result
!= USB_STOR_XFER_GOOD
)
315 return USB_STOR_TRANSPORT_ERROR
;
316 if (bcs
->Status
!= US_BULK_STAT_OK
)
317 return USB_STOR_TRANSPORT_ERROR
;
319 return USB_STOR_TRANSPORT_GOOD
;
323 * usb_stor_print_cmd():
325 void usb_stor_print_cmd(struct scsi_cmnd
*srb
)
327 PBYTE Cdb
= srb
->cmnd
;
329 DWORD bn
= ((Cdb
[2] << 24) & 0xff000000) |
330 ((Cdb
[3] << 16) & 0x00ff0000) |
331 ((Cdb
[4] << 8) & 0x0000ff00) |
332 ((Cdb
[5] << 0) & 0x000000ff);
333 WORD blen
= ((Cdb
[7] << 8) & 0xff00) | ((Cdb
[8] << 0) & 0x00ff);
336 case TEST_UNIT_READY
:
338 "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
341 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_INQUIRY\n", cmd
);
344 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd
);
347 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_START_STOP\n", cmd
);
350 printk(KERN_INFO
"scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd
);
354 "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
359 "scsi cmd %X --- SCSIOP_WRITE,
360 bn = %X, blen = %X\n" , cmd, bn, blen); */
362 case ALLOW_MEDIUM_REMOVAL
:
364 "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd
);
367 printk(KERN_INFO
"scsi cmd %X --- Other cmd\n", cmd
);