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 & 0x01) {
34 if (!us->SD_Status.Ready) {
35 result = ENE_SDInit(us);
36 if (result != USB_STOR_XFER_GOOD)
37 return USB_STOR_TRANSPORT_ERROR;
41 if (MiscReg03 & 0x02) {
42 if (!us->SM_Status.Ready && !us->MS_Status.Ready) {
43 result = ENE_SMInit(us);
44 if (result != USB_STOR_XFER_GOOD) {
45 result = ENE_MSInit(us);
46 if (result != USB_STOR_XFER_GOOD)
47 return USB_STOR_TRANSPORT_ERROR;
58 int ENE_Read_BYTE(struct us_data *us, WORD index, void *buf)
60 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
63 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
64 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
65 bcb->DataTransferLength = 0x01;
68 bcb->CDB[2] = (BYTE)(index>>8);
69 bcb->CDB[3] = (BYTE)index;
71 result = ENE_SendScsiCmd(us, FDIR_READ, buf, 0);
78 int ENE_SDInit(struct us_data *us)
80 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
84 printk(KERN_INFO "transport --- ENE_SDInit\n");
86 result = ENE_LoadBinCode(us, SD_INIT1_PATTERN);
87 if (result != USB_STOR_XFER_GOOD) {
88 printk(KERN_ERR "Load SD Init Code Part-1 Fail !!\n");
89 return USB_STOR_TRANSPORT_ERROR;
92 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
93 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
97 result = ENE_SendScsiCmd(us, FDIR_READ, NULL, 0);
98 if (result != USB_STOR_XFER_GOOD) {
99 printk(KERN_ERR "Exection SD Init Code Fail !!\n");
100 return USB_STOR_TRANSPORT_ERROR;
104 result = ENE_LoadBinCode(us, SD_INIT2_PATTERN);
105 if (result != USB_STOR_XFER_GOOD) {
106 printk(KERN_ERR "Load SD Init Code Part-2 Fail !!\n");
107 return USB_STOR_TRANSPORT_ERROR;
110 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
111 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
112 bcb->DataTransferLength = 0x200;
116 result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
117 if (result != USB_STOR_XFER_GOOD) {
118 printk(KERN_ERR "Exection SD Init Code Fail !!\n");
119 return USB_STOR_TRANSPORT_ERROR;
122 us->SD_Status = *(PSD_STATUS)&buf[0];
123 if (us->SD_Status.Insert && us->SD_Status.Ready) {
124 ENE_ReadSDReg(us, (PBYTE)&buf);
125 printk(KERN_INFO "Insert = %x\n", us->SD_Status.Insert);
126 printk(KERN_INFO "Ready = %x\n", us->SD_Status.Ready);
127 printk(KERN_INFO "IsMMC = %x\n", us->SD_Status.IsMMC);
128 printk(KERN_INFO "HiCapacity = %x\n", us->SD_Status.HiCapacity);
129 printk(KERN_INFO "HiSpeed = %x\n", us->SD_Status.HiSpeed);
130 printk(KERN_INFO "WtP = %x\n", us->SD_Status.WtP);
132 printk(KERN_ERR "SD Card Not Ready --- %x\n", buf[0]);
133 return USB_STOR_TRANSPORT_ERROR;
135 return USB_STOR_TRANSPORT_GOOD;
141 int ENE_MSInit(struct us_data *us)
143 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
146 WORD MSP_BlockSize, MSP_UserAreaBlocks;
148 printk(KERN_INFO "transport --- ENE_MSInit\n");
149 result = ENE_LoadBinCode(us, MS_INIT_PATTERN);
150 if (result != USB_STOR_XFER_GOOD) {
151 printk(KERN_ERR "Load MS Init Code Fail !!\n");
152 return USB_STOR_TRANSPORT_ERROR;
155 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
156 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
157 bcb->DataTransferLength = 0x200;
162 result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
163 if (result != USB_STOR_XFER_GOOD) {
164 printk(KERN_ERR "Exection MS Init Code Fail !!\n");
165 return USB_STOR_TRANSPORT_ERROR;
168 us->MS_Status = *(PMS_STATUS)&buf[0];
170 if (us->MS_Status.Insert && us->MS_Status.Ready) {
171 printk(KERN_INFO "Insert = %x\n", us->MS_Status.Insert);
172 printk(KERN_INFO "Ready = %x\n", us->MS_Status.Ready);
173 printk(KERN_INFO "IsMSPro = %x\n", us->MS_Status.IsMSPro);
174 printk(KERN_INFO "IsMSPHG = %x\n", us->MS_Status.IsMSPHG);
175 printk(KERN_INFO "WtP = %x\n", us->MS_Status.WtP);
176 if (us->MS_Status.IsMSPro) {
177 MSP_BlockSize = (buf[6] << 8) | buf[7];
178 MSP_UserAreaBlocks = (buf[10] << 8) | buf[11];
179 us->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
183 printk(KERN_INFO "MS Init Code OK !!\n");
185 printk(KERN_INFO "MS Card Not Ready --- %x\n", buf[0]);
186 return USB_STOR_TRANSPORT_ERROR;
189 return USB_STOR_TRANSPORT_GOOD;
195 int ENE_SMInit(struct us_data *us)
197 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
201 printk(KERN_INFO "transport --- ENE_SMInit\n");
203 result = ENE_LoadBinCode(us, SM_INIT_PATTERN);
204 if (result != USB_STOR_XFER_GOOD) {
205 printk(KERN_INFO "Load SM Init Code Fail !!\n");
206 return USB_STOR_TRANSPORT_ERROR;
209 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
210 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
211 bcb->DataTransferLength = 0x200;
216 result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
217 if (result != USB_STOR_XFER_GOOD) {
219 "Exection SM Init Code Fail !! result = %x\n", result);
220 return USB_STOR_TRANSPORT_ERROR;
223 us->SM_Status = *(PSM_STATUS)&buf[0];
225 us->SM_DeviceID = buf[1];
226 us->SM_CardID = buf[2];
228 if (us->SM_Status.Insert && us->SM_Status.Ready) {
229 printk(KERN_INFO "Insert = %x\n", us->SM_Status.Insert);
230 printk(KERN_INFO "Ready = %x\n", us->SM_Status.Ready);
231 printk(KERN_INFO "WtP = %x\n", us->SM_Status.WtP);
232 printk(KERN_INFO "DeviceID = %x\n", us->SM_DeviceID);
233 printk(KERN_INFO "CardID = %x\n", us->SM_CardID);
235 Check_D_MediaFmt(us);
237 printk(KERN_ERR "SM Card Not Ready --- %x\n", buf[0]);
238 return USB_STOR_TRANSPORT_ERROR;
241 return USB_STOR_TRANSPORT_GOOD;
247 int ENE_ReadSDReg(struct us_data *us, u8 *RdBuf)
252 /* printk(KERN_INFO "transport --- ENE_ReadSDReg\n"); */
253 reg4b = *(PDWORD)&RdBuf[0x18];
254 us->SD_READ_BL_LEN = (BYTE)((reg4b >> 8) & 0x0f);
256 tmpreg = (WORD) reg4b;
257 reg4b = *(PDWORD)(&RdBuf[0x14]);
258 if (us->SD_Status.HiCapacity && !us->SD_Status.IsMMC)
259 us->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
261 us->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (WORD)(reg4b >> 22);
262 us->SD_C_SIZE_MULT = (BYTE)(reg4b >> 7) & 0x07;
263 if (us->SD_Status.HiCapacity && us->SD_Status.IsMMC)
264 us->HC_C_SIZE = *(PDWORD)(&RdBuf[0x100]);
266 if (us->SD_READ_BL_LEN > SD_BLOCK_LEN) {
268 1 << (us->SD_READ_BL_LEN - SD_BLOCK_LEN);
269 us->SD_READ_BL_LEN = SD_BLOCK_LEN;
271 us->SD_Block_Mult = 1;
273 return USB_STOR_TRANSPORT_GOOD;
279 int ENE_LoadBinCode(struct us_data *us, BYTE flag)
281 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
286 /* printk(KERN_INFO "transport --- ENE_LoadBinCode\n"); */
287 if (us->BIN_FLAG == flag)
288 return USB_STOR_TRANSPORT_GOOD;
290 buf = kmalloc(0x800, GFP_KERNEL);
292 return USB_STOR_TRANSPORT_ERROR;
295 case SD_INIT1_PATTERN:
296 printk(KERN_INFO "SD_INIT1_PATTERN\n");
297 memcpy(buf, SD_Init1, 0x800);
299 case SD_INIT2_PATTERN:
300 printk(KERN_INFO "SD_INIT2_PATTERN\n");
301 memcpy(buf, SD_Init2, 0x800);
304 printk(KERN_INFO "SD_RW_PATTERN\n");
305 memcpy(buf, SD_Rdwr, 0x800);
308 case MS_INIT_PATTERN:
309 printk(KERN_INFO "MS_INIT_PATTERN\n");
310 memcpy(buf, MS_Init, 0x800);
313 printk(KERN_INFO "MSP_RW_PATTERN\n");
314 memcpy(buf, MSP_Rdwr, 0x800);
317 printk(KERN_INFO "MS_RW_PATTERN\n");
318 memcpy(buf, MS_Rdwr, 0x800);
321 case SM_INIT_PATTERN:
322 printk(KERN_INFO "SM_INIT_PATTERN\n");
323 memcpy(buf, SM_Init, 0x800);
326 printk(KERN_INFO "SM_RW_PATTERN\n");
327 memcpy(buf, SM_Rdwr, 0x800);
331 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
332 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
333 bcb->DataTransferLength = 0x800;
337 result = ENE_SendScsiCmd(us, FDIR_WRITE, buf, 0);
347 int ENE_SendScsiCmd(struct us_data *us, BYTE fDir, void *buf, int use_sg)
349 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
350 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
353 unsigned int transfer_length = bcb->DataTransferLength,
354 cswlen = 0, partial = 0;
355 unsigned int residue;
357 /* printk(KERN_INFO "transport --- ENE_SendScsiCmd\n"); */
358 /* send cmd to out endpoint */
359 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
360 bcb, US_BULK_CB_WRAP_LEN, NULL);
361 if (result != USB_STOR_XFER_GOOD) {
362 printk(KERN_ERR "send cmd to out endpoint fail ---\n");
363 return USB_STOR_TRANSPORT_ERROR;
367 unsigned int pipe = fDir;
369 if (fDir == FDIR_READ)
370 pipe = us->recv_bulk_pipe;
372 pipe = us->send_bulk_pipe;
376 result = usb_stor_bulk_srb(us, pipe, us->srb);
378 result = usb_stor_bulk_transfer_sg(us, pipe, buf,
379 transfer_length, 0, &partial);
380 if (result != USB_STOR_XFER_GOOD) {
381 printk(KERN_ERR "data transfer fail ---\n");
382 return USB_STOR_TRANSPORT_ERROR;
386 /* Get CSW for device status */
387 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
388 US_BULK_CS_WRAP_LEN, &cswlen);
390 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
391 printk(KERN_WARNING "Received 0-length CSW; retrying...\n");
392 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
393 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
396 if (result == USB_STOR_XFER_STALLED) {
397 /* get the status again */
398 printk(KERN_WARNING "Attempting to get CSW (2nd try)...\n");
399 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
400 bcs, US_BULK_CS_WRAP_LEN, NULL);
403 if (result != USB_STOR_XFER_GOOD)
404 return USB_STOR_TRANSPORT_ERROR;
406 /* check bulk status */
407 residue = le32_to_cpu(bcs->Residue);
410 * try to compute the actual residue, based on how much data
411 * was really transferred and what the device tells us
413 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
414 residue = min(residue, transfer_length);
415 scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
419 if (bcs->Status != US_BULK_STAT_OK)
420 return USB_STOR_TRANSPORT_ERROR;
422 return USB_STOR_TRANSPORT_GOOD;
428 int ENE_Read_Data(struct us_data *us, void *buf, unsigned int length)
430 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
431 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
434 /* printk(KERN_INFO "transport --- ENE_Read_Data\n"); */
435 /* set up the command wrapper */
436 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
437 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
438 bcb->DataTransferLength = length;
444 /* send cmd to out endpoint */
445 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
446 US_BULK_CB_WRAP_LEN, NULL);
447 if (result != USB_STOR_XFER_GOOD)
448 return USB_STOR_TRANSPORT_ERROR;
451 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
453 if (result != USB_STOR_XFER_GOOD)
454 return USB_STOR_TRANSPORT_ERROR;
456 /* Get CSW for device status */
457 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
458 US_BULK_CS_WRAP_LEN, NULL);
459 if (result != USB_STOR_XFER_GOOD)
460 return USB_STOR_TRANSPORT_ERROR;
461 if (bcs->Status != US_BULK_STAT_OK)
462 return USB_STOR_TRANSPORT_ERROR;
464 return USB_STOR_TRANSPORT_GOOD;
470 int ENE_Write_Data(struct us_data *us, void *buf, unsigned int length)
472 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
473 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
476 /* printk("transport --- ENE_Write_Data\n"); */
477 /* set up the command wrapper */
478 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
479 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
480 bcb->DataTransferLength = length;
486 /* send cmd to out endpoint */
487 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
488 US_BULK_CB_WRAP_LEN, NULL);
489 if (result != USB_STOR_XFER_GOOD)
490 return USB_STOR_TRANSPORT_ERROR;
493 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
495 if (result != USB_STOR_XFER_GOOD)
496 return USB_STOR_TRANSPORT_ERROR;
498 /* Get CSW for device status */
499 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
500 US_BULK_CS_WRAP_LEN, NULL);
501 if (result != USB_STOR_XFER_GOOD)
502 return USB_STOR_TRANSPORT_ERROR;
503 if (bcs->Status != US_BULK_STAT_OK)
504 return USB_STOR_TRANSPORT_ERROR;
506 return USB_STOR_TRANSPORT_GOOD;
510 * usb_stor_print_cmd():
512 void usb_stor_print_cmd(struct scsi_cmnd *srb)
514 PBYTE Cdb = srb->cmnd;
516 DWORD bn = ((Cdb[2] << 24) & 0xff000000) |
517 ((Cdb[3] << 16) & 0x00ff0000) |
518 ((Cdb[4] << 8) & 0x0000ff00) |
519 ((Cdb[5] << 0) & 0x000000ff);
520 WORD blen = ((Cdb[7] << 8) & 0xff00) | ((Cdb[8] << 0) & 0x00ff);
523 case TEST_UNIT_READY:
525 "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
528 printk(KERN_INFO "scsi cmd %X --- SCSIOP_INQUIRY\n", cmd);
531 printk(KERN_INFO "scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd);
534 printk(KERN_INFO "scsi cmd %X --- SCSIOP_START_STOP\n", cmd);
537 printk(KERN_INFO "scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd);
541 "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
546 "scsi cmd %X --- SCSIOP_WRITE,
547 bn = %X, blen = %X\n" , cmd, bn, blen); */
549 case ALLOW_MEDIUM_REMOVAL:
551 "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd);
554 printk(KERN_INFO "scsi cmd %X --- Other cmd\n", cmd);