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 dev_info(&us
->pusb_dev
->dev
, "--- Init Media ---\n");
23 result
= ene_read_byte(us
, REG_CARD_STATUS
, &MiscReg03
);
24 if (result
!= USB_STOR_XFER_GOOD
) {
25 dev_err(&us
->pusb_dev
->dev
, "Failed to read register\n");
26 return USB_STOR_TRANSPORT_ERROR
;
28 dev_info(&us
->pusb_dev
->dev
, "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 dev_dbg(&us
->pusb_dev
->dev
, "transport --- ENE_SMInit\n");
72 result
= ENE_LoadBinCode(us
, SM_INIT_PATTERN
);
73 if (result
!= USB_STOR_XFER_GOOD
) {
74 dev_info(&us
->pusb_dev
->dev
,
75 "Failed to load SmartMedia init code\n: result= %x\n",
77 return USB_STOR_TRANSPORT_ERROR
;
80 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
81 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
82 bcb
->DataTransferLength
= 0x200;
87 result
= ENE_SendScsiCmd(us
, FDIR_READ
, &buf
, 0);
88 if (result
!= USB_STOR_XFER_GOOD
) {
89 dev_err(&us
->pusb_dev
->dev
,
90 "Failed to load SmartMedia init code: result = %x\n",
92 return USB_STOR_TRANSPORT_ERROR
;
95 us
->SM_Status
= *(struct keucr_sm_status
*)&buf
[0];
97 us
->SM_DeviceID
= buf
[1];
98 us
->SM_CardID
= buf
[2];
100 if (us
->SM_Status
.Insert
&& us
->SM_Status
.Ready
) {
101 dev_info(&us
->pusb_dev
->dev
, "Insert = %x\n",
102 us
->SM_Status
.Insert
);
103 dev_info(&us
->pusb_dev
->dev
, "Ready = %x\n",
104 us
->SM_Status
.Ready
);
105 dev_info(&us
->pusb_dev
->dev
, "WtP = %x\n",
107 dev_info(&us
->pusb_dev
->dev
, "DeviceID = %x\n",
109 dev_info(&us
->pusb_dev
->dev
, "CardID = %x\n",
112 Check_D_MediaFmt(us
);
114 dev_err(&us
->pusb_dev
->dev
,
115 "SmartMedia Card Not Ready --- %x\n", buf
[0]);
116 return USB_STOR_TRANSPORT_ERROR
;
119 return USB_STOR_TRANSPORT_GOOD
;
125 int ENE_LoadBinCode(struct us_data
*us
, BYTE flag
)
127 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
132 /* dev_info(&us->pusb_dev->dev, "transport --- ENE_LoadBinCode\n"); */
133 if (us
->BIN_FLAG
== flag
)
134 return USB_STOR_TRANSPORT_GOOD
;
136 buf
= kmalloc(0x800, GFP_KERNEL
);
138 return USB_STOR_TRANSPORT_ERROR
;
141 case SM_INIT_PATTERN
:
142 dev_dbg(&us
->pusb_dev
->dev
, "SM_INIT_PATTERN\n");
143 memcpy(buf
, SM_Init
, 0x800);
146 dev_dbg(&us
->pusb_dev
->dev
, "SM_RW_PATTERN\n");
147 memcpy(buf
, SM_Rdwr
, 0x800);
151 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
152 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
153 bcb
->DataTransferLength
= 0x800;
157 result
= ENE_SendScsiCmd(us
, FDIR_WRITE
, buf
, 0);
167 int ENE_SendScsiCmd(struct us_data
*us
, BYTE fDir
, void *buf
, int use_sg
)
169 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
170 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
173 unsigned int transfer_length
= bcb
->DataTransferLength
,
174 cswlen
= 0, partial
= 0;
175 unsigned int residue
;
177 /* dev_dbg(&us->pusb_dev->dev, "transport --- ENE_SendScsiCmd\n"); */
178 /* send cmd to out endpoint */
179 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
180 bcb
, US_BULK_CB_WRAP_LEN
, NULL
);
181 if (result
!= USB_STOR_XFER_GOOD
) {
182 dev_err(&us
->pusb_dev
->dev
,
183 "send cmd to out endpoint fail ---\n");
184 return USB_STOR_TRANSPORT_ERROR
;
188 unsigned int pipe
= fDir
;
190 if (fDir
== FDIR_READ
)
191 pipe
= us
->recv_bulk_pipe
;
193 pipe
= us
->send_bulk_pipe
;
197 result
= usb_stor_bulk_srb(us
, pipe
, us
->srb
);
199 result
= usb_stor_bulk_transfer_sg(us
, pipe
, buf
,
200 transfer_length
, 0, &partial
);
201 if (result
!= USB_STOR_XFER_GOOD
) {
202 dev_err(&us
->pusb_dev
->dev
, "data transfer fail ---\n");
203 return USB_STOR_TRANSPORT_ERROR
;
207 /* Get CSW for device status */
208 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
209 US_BULK_CS_WRAP_LEN
, &cswlen
);
211 if (result
== USB_STOR_XFER_SHORT
&& cswlen
== 0) {
212 dev_warn(&us
->pusb_dev
->dev
,
213 "Received 0-length CSW; retrying...\n");
214 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
215 bcs
, US_BULK_CS_WRAP_LEN
, &cswlen
);
218 if (result
== USB_STOR_XFER_STALLED
) {
219 /* get the status again */
220 dev_warn(&us
->pusb_dev
->dev
,
221 "Attempting to get CSW (2nd try)...\n");
222 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
223 bcs
, US_BULK_CS_WRAP_LEN
, NULL
);
226 if (result
!= USB_STOR_XFER_GOOD
)
227 return USB_STOR_TRANSPORT_ERROR
;
229 /* check bulk status */
230 residue
= le32_to_cpu(bcs
->Residue
);
233 * try to compute the actual residue, based on how much data
234 * was really transferred and what the device tells us
236 if (residue
&& !(us
->fflags
& US_FL_IGNORE_RESIDUE
)) {
237 residue
= min(residue
, transfer_length
);
239 scsi_set_resid(us
->srb
, max(scsi_get_resid(us
->srb
),
243 if (bcs
->Status
!= US_BULK_STAT_OK
)
244 return USB_STOR_TRANSPORT_ERROR
;
246 return USB_STOR_TRANSPORT_GOOD
;
252 int ENE_Read_Data(struct us_data
*us
, void *buf
, unsigned int length
)
254 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
255 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
258 /* dev_dbg(&us->pusb_dev->dev, "transport --- ENE_Read_Data\n"); */
259 /* set up the command wrapper */
260 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
261 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
262 bcb
->DataTransferLength
= length
;
268 /* send cmd to out endpoint */
269 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
,
270 US_BULK_CB_WRAP_LEN
, NULL
);
271 if (result
!= USB_STOR_XFER_GOOD
)
272 return USB_STOR_TRANSPORT_ERROR
;
275 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
277 if (result
!= USB_STOR_XFER_GOOD
)
278 return USB_STOR_TRANSPORT_ERROR
;
280 /* Get CSW for device status */
281 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
282 US_BULK_CS_WRAP_LEN
, NULL
);
283 if (result
!= USB_STOR_XFER_GOOD
)
284 return USB_STOR_TRANSPORT_ERROR
;
285 if (bcs
->Status
!= US_BULK_STAT_OK
)
286 return USB_STOR_TRANSPORT_ERROR
;
288 return USB_STOR_TRANSPORT_GOOD
;
294 int ENE_Write_Data(struct us_data
*us
, void *buf
, unsigned int length
)
296 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
297 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
300 /* printk("transport --- ENE_Write_Data\n"); */
301 /* set up the command wrapper */
302 memset(bcb
, 0, sizeof(struct bulk_cb_wrap
));
303 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
304 bcb
->DataTransferLength
= length
;
310 /* send cmd to out endpoint */
311 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
, bcb
,
312 US_BULK_CB_WRAP_LEN
, NULL
);
313 if (result
!= USB_STOR_XFER_GOOD
)
314 return USB_STOR_TRANSPORT_ERROR
;
317 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
319 if (result
!= USB_STOR_XFER_GOOD
)
320 return USB_STOR_TRANSPORT_ERROR
;
322 /* Get CSW for device status */
323 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
324 US_BULK_CS_WRAP_LEN
, NULL
);
325 if (result
!= USB_STOR_XFER_GOOD
)
326 return USB_STOR_TRANSPORT_ERROR
;
327 if (bcs
->Status
!= US_BULK_STAT_OK
)
328 return USB_STOR_TRANSPORT_ERROR
;
330 return USB_STOR_TRANSPORT_GOOD
;