blob: cb7190e0e18a18d64e5fe0076e6038137a58be4f [file] [log] [blame]
Cho, Yu-Chen615df842011-04-01 14:38:40 +08001#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
Al Cho126bb032010-09-08 00:42:32 -07003#include <linux/sched.h>
4#include <linux/errno.h>
5#include <linux/slab.h>
6
7#include <scsi/scsi.h>
8#include <scsi/scsi_eh.h>
9#include <scsi/scsi_device.h>
10
11#include "usb.h"
12#include "scsiglue.h"
13#include "transport.h"
14
Cho, Yu-Chen70d83702011-05-18 18:40:10 +080015/*
16 * MS_SCSI_Test_Unit_Ready()
17 */
Al Cho126bb032010-09-08 00:42:32 -070018int MS_SCSI_Test_Unit_Ready(struct us_data *us, struct scsi_cmnd *srb)
19{
Cho, Yu-Chen615df842011-04-01 14:38:40 +080020 /* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
Al Cho126bb032010-09-08 00:42:32 -070021 if (us->MS_Status.Insert && us->MS_Status.Ready)
22 return USB_STOR_TRANSPORT_GOOD;
Cho, Yu-Chen70d83702011-05-18 18:40:10 +080023 else {
Al Cho126bb032010-09-08 00:42:32 -070024 ENE_MSInit(us);
25 return USB_STOR_TRANSPORT_GOOD;
26 }
Cho, Yu-Chen70d83702011-05-18 18:40:10 +080027
Al Cho126bb032010-09-08 00:42:32 -070028 return USB_STOR_TRANSPORT_GOOD;
29}
30
Cho, Yu-Chen70d83702011-05-18 18:40:10 +080031/*
32 * MS_SCSI_Inquiry()
33 */
Al Cho126bb032010-09-08 00:42:32 -070034int MS_SCSI_Inquiry(struct us_data *us, struct scsi_cmnd *srb)
35{
Cho, Yu-Chen615df842011-04-01 14:38:40 +080036 /* pr_info("MS_SCSI_Inquiry\n"); */
Cho, Yu-Chen70d83702011-05-18 18:40:10 +080037 BYTE data_ptr[36] = {0x00, 0x80, 0x02, 0x00, 0x1F, 0x00,
38 0x00, 0x00, 0x55, 0x53, 0x42, 0x32,
39 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
40 0x72, 0x64, 0x52, 0x65, 0x61, 0x64,
41 0x65, 0x72, 0x20, 0x20, 0x20, 0x20,
42 0x20, 0x20, 0x30, 0x31, 0x30, 0x30};
Al Cho126bb032010-09-08 00:42:32 -070043
44 usb_stor_set_xfer_buf(us, data_ptr, 36, srb, TO_XFER_BUF);
45 return USB_STOR_TRANSPORT_GOOD;
46}
47
Cho, Yu-Chen70d83702011-05-18 18:40:10 +080048/*
49 * MS_SCSI_Mode_Sense()
50 */
Al Cho126bb032010-09-08 00:42:32 -070051int MS_SCSI_Mode_Sense(struct us_data *us, struct scsi_cmnd *srb)
52{
Cho, Yu-Chen70d83702011-05-18 18:40:10 +080053 BYTE mediaNoWP[12] = {0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
54 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00};
55 BYTE mediaWP[12] = {0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
56 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00};
Al Cho126bb032010-09-08 00:42:32 -070057
58 if (us->MS_Status.WtP)
59 usb_stor_set_xfer_buf(us, mediaWP, 12, srb, TO_XFER_BUF);
60 else
61 usb_stor_set_xfer_buf(us, mediaNoWP, 12, srb, TO_XFER_BUF);
62
63
64 return USB_STOR_TRANSPORT_GOOD;
65}
66
Cho, Yu-Chen70d83702011-05-18 18:40:10 +080067/*
68 * MS_SCSI_Read_Capacity()
69 */
Al Cho126bb032010-09-08 00:42:32 -070070int MS_SCSI_Read_Capacity(struct us_data *us, struct scsi_cmnd *srb)
71{
72 unsigned int offset = 0;
73 struct scatterlist *sg = NULL;
74 DWORD bl_num;
75 WORD bl_len;
76 BYTE buf[8];
77
Cho, Yu-Chen615df842011-04-01 14:38:40 +080078 pr_info("MS_SCSI_Read_Capacity\n");
Al Cho126bb032010-09-08 00:42:32 -070079
80 bl_len = 0x200;
Cho, Yu-Chen70d83702011-05-18 18:40:10 +080081 if (us->MS_Status.IsMSPro)
Al Cho126bb032010-09-08 00:42:32 -070082 bl_num = us->MSP_TotalBlock - 1;
83 else
Cho, Yu-Chen70d83702011-05-18 18:40:10 +080084 bl_num = us->MS_Lib.NumberOfLogBlock *
85 us->MS_Lib.blockSize * 2 - 1;
Al Cho126bb032010-09-08 00:42:32 -070086
87 us->bl_num = bl_num;
Cho, Yu-Chen615df842011-04-01 14:38:40 +080088 pr_info("bl_len = %x\n", bl_len);
89 pr_info("bl_num = %x\n", bl_num);
Al Cho126bb032010-09-08 00:42:32 -070090
Cho, Yu-Chen70d83702011-05-18 18:40:10 +080091 /* srb->request_bufflen = 8; */
92 buf[0] = (bl_num >> 24) & 0xff;
93 buf[1] = (bl_num >> 16) & 0xff;
94 buf[2] = (bl_num >> 8) & 0xff;
95 buf[3] = (bl_num >> 0) & 0xff;
96 buf[4] = (bl_len >> 24) & 0xff;
97 buf[5] = (bl_len >> 16) & 0xff;
98 buf[6] = (bl_len >> 8) & 0xff;
99 buf[7] = (bl_len >> 0) & 0xff;
100
Al Cho126bb032010-09-08 00:42:32 -0700101 usb_stor_access_xfer_buf(us, buf, 8, srb, &sg, &offset, TO_XFER_BUF);
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800102 /* usb_stor_set_xfer_buf(us, buf, srb->request_bufflen,
103 srb, TO_XFER_BUF); */
Al Cho126bb032010-09-08 00:42:32 -0700104
105 return USB_STOR_TRANSPORT_GOOD;
106}
107
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800108/*
109 * MS_SCSI_Read()
110 */
Al Cho126bb032010-09-08 00:42:32 -0700111int MS_SCSI_Read(struct us_data *us, struct scsi_cmnd *srb)
112{
113 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800114 int result = 0;
Al Cho126bb032010-09-08 00:42:32 -0700115 PBYTE Cdb = srb->cmnd;
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800116 DWORD bn = ((Cdb[2] << 24) & 0xff000000) |
117 ((Cdb[3] << 16) & 0x00ff0000) |
118 ((Cdb[4] << 8) & 0x0000ff00) |
119 ((Cdb[5] << 0) & 0x000000ff);
120 WORD blen = ((Cdb[7] << 8) & 0xff00) | ((Cdb[8] << 0) & 0x00ff);
Al Cho126bb032010-09-08 00:42:32 -0700121 DWORD blenByte = blen * 0x200;
122
Cho, Yu-Chen615df842011-04-01 14:38:40 +0800123 /* pr_info("SCSIOP_READ --- bn = %X, blen = %X, srb->use_sg = %X\n",
124 bn, blen, srb->use_sg); */
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800125
Al Cho126bb032010-09-08 00:42:32 -0700126 if (bn > us->bl_num)
127 return USB_STOR_TRANSPORT_ERROR;
128
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800129 if (us->MS_Status.IsMSPro) {
Al Cho126bb032010-09-08 00:42:32 -0700130 result = ENE_LoadBinCode(us, MSP_RW_PATTERN);
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800131 if (result != USB_STOR_XFER_GOOD) {
Cho, Yu-Chen615df842011-04-01 14:38:40 +0800132 pr_info("Load MSP RW pattern Fail !!\n");
Al Cho126bb032010-09-08 00:42:32 -0700133 return USB_STOR_TRANSPORT_ERROR;
134 }
135
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800136 /* set up the command wrapper */
Konstantin Katuev307ae1d2010-10-29 12:18:18 +1100137 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -0700138 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
139 bcb->DataTransferLength = blenByte;
140 bcb->Flags = 0x80;
141 bcb->CDB[0] = 0xF1;
142 bcb->CDB[1] = 0x02;
143 bcb->CDB[5] = (BYTE)(bn);
144 bcb->CDB[4] = (BYTE)(bn>>8);
145 bcb->CDB[3] = (BYTE)(bn>>16);
146 bcb->CDB[2] = (BYTE)(bn>>24);
147
148 result = ENE_SendScsiCmd(us, FDIR_READ, scsi_sglist(srb), 1);
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800149 } else {
Al Cho126bb032010-09-08 00:42:32 -0700150 void *buf;
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800151 int offset = 0;
Al Cho126bb032010-09-08 00:42:32 -0700152 WORD phyblk, logblk;
153 BYTE PageNum;
154 WORD len;
155 DWORD blkno;
156
157 buf = kmalloc(blenByte, GFP_KERNEL);
Vasiliy Kulikovb1f5f542010-09-19 11:36:23 +0400158 if (buf == NULL)
159 return USB_STOR_TRANSPORT_ERROR;
Al Cho126bb032010-09-08 00:42:32 -0700160
161 result = ENE_LoadBinCode(us, MS_RW_PATTERN);
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800162 if (result != USB_STOR_XFER_GOOD) {
Cho, Yu-Chen615df842011-04-01 14:38:40 +0800163 pr_info("Load MS RW pattern Fail !!\n");
Al Cho126bb032010-09-08 00:42:32 -0700164 result = USB_STOR_TRANSPORT_ERROR;
165 goto exit;
166 }
167
168 logblk = (WORD)(bn / us->MS_Lib.PagesPerBlock);
169 PageNum = (BYTE)(bn % us->MS_Lib.PagesPerBlock);
170
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800171 while (1) {
172 if (blen > (us->MS_Lib.PagesPerBlock-PageNum))
Al Cho126bb032010-09-08 00:42:32 -0700173 len = us->MS_Lib.PagesPerBlock-PageNum;
174 else
175 len = blen;
176
177 phyblk = MS_LibConv2Physical(us, logblk);
178 blkno = phyblk * 0x20 + PageNum;
179
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800180 /* set up the command wrapper */
Konstantin Katuev307ae1d2010-10-29 12:18:18 +1100181 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -0700182 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
183 bcb->DataTransferLength = 0x200 * len;
184 bcb->Flags = 0x80;
185 bcb->CDB[0] = 0xF1;
186 bcb->CDB[1] = 0x02;
187 bcb->CDB[5] = (BYTE)(blkno);
188 bcb->CDB[4] = (BYTE)(blkno>>8);
189 bcb->CDB[3] = (BYTE)(blkno>>16);
190 bcb->CDB[2] = (BYTE)(blkno>>24);
191
192 result = ENE_SendScsiCmd(us, FDIR_READ, buf+offset, 0);
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800193 if (result != USB_STOR_XFER_GOOD) {
Cho, Yu-Chen615df842011-04-01 14:38:40 +0800194 pr_info("MS_SCSI_Read --- result = %x\n",
195 result);
Al Cho126bb032010-09-08 00:42:32 -0700196 result = USB_STOR_TRANSPORT_ERROR;
197 goto exit;
198 }
199
200 blen -= len;
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800201 if (blen <= 0)
Al Cho126bb032010-09-08 00:42:32 -0700202 break;
203 logblk++;
204 PageNum = 0;
205 offset += MS_BYTES_PER_PAGE*len;
206 }
207 usb_stor_set_xfer_buf(us, buf, blenByte, srb, TO_XFER_BUF);
208exit:
209 kfree(buf);
210 }
211 return result;
212}
213
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800214/*
215 * MS_SCSI_Write()
216 */
Al Cho126bb032010-09-08 00:42:32 -0700217int MS_SCSI_Write(struct us_data *us, struct scsi_cmnd *srb)
218{
219 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800220 int result = 0;
Al Cho126bb032010-09-08 00:42:32 -0700221 PBYTE Cdb = srb->cmnd;
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800222 DWORD bn = ((Cdb[2] << 24) & 0xff000000) |
223 ((Cdb[3] << 16) & 0x00ff0000) |
224 ((Cdb[4] << 8) & 0x0000ff00) |
225 ((Cdb[5] << 0) & 0x000000ff);
226 WORD blen = ((Cdb[7] << 8) & 0xff00) | ((Cdb[8] << 0) & 0x00ff);
Al Cho126bb032010-09-08 00:42:32 -0700227 DWORD blenByte = blen * 0x200;
228
229 if (bn > us->bl_num)
230 return USB_STOR_TRANSPORT_ERROR;
231
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800232 if (us->MS_Status.IsMSPro) {
Al Cho126bb032010-09-08 00:42:32 -0700233 result = ENE_LoadBinCode(us, MSP_RW_PATTERN);
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800234 if (result != USB_STOR_XFER_GOOD) {
Cho, Yu-Chen615df842011-04-01 14:38:40 +0800235 pr_info("Load MSP RW pattern Fail !!\n");
Al Cho126bb032010-09-08 00:42:32 -0700236 return USB_STOR_TRANSPORT_ERROR;
237 }
238
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800239 /* set up the command wrapper */
Konstantin Katuev307ae1d2010-10-29 12:18:18 +1100240 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -0700241 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
242 bcb->DataTransferLength = blenByte;
243 bcb->Flags = 0x00;
244 bcb->CDB[0] = 0xF0;
245 bcb->CDB[1] = 0x04;
246 bcb->CDB[5] = (BYTE)(bn);
247 bcb->CDB[4] = (BYTE)(bn>>8);
248 bcb->CDB[3] = (BYTE)(bn>>16);
249 bcb->CDB[2] = (BYTE)(bn>>24);
250
251 result = ENE_SendScsiCmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800252 } else {
Al Cho126bb032010-09-08 00:42:32 -0700253 void *buf;
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800254 int offset = 0;
Al Cho126bb032010-09-08 00:42:32 -0700255 WORD PhyBlockAddr;
256 BYTE PageNum;
257 DWORD result;
258 WORD len, oldphy, newphy;
259
260 buf = kmalloc(blenByte, GFP_KERNEL);
Vasiliy Kulikovb1f5f542010-09-19 11:36:23 +0400261 if (buf == NULL)
262 return USB_STOR_TRANSPORT_ERROR;
Al Cho126bb032010-09-08 00:42:32 -0700263 usb_stor_set_xfer_buf(us, buf, blenByte, srb, FROM_XFER_BUF);
264
265 result = ENE_LoadBinCode(us, MS_RW_PATTERN);
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800266 if (result != USB_STOR_XFER_GOOD) {
Cho, Yu-Chen615df842011-04-01 14:38:40 +0800267 pr_info("Load MS RW pattern Fail !!\n");
Al Cho126bb032010-09-08 00:42:32 -0700268 result = USB_STOR_TRANSPORT_ERROR;
269 goto exit;
270 }
271
272 PhyBlockAddr = (WORD)(bn / us->MS_Lib.PagesPerBlock);
273 PageNum = (BYTE)(bn % us->MS_Lib.PagesPerBlock);
274
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800275 while (1) {
276 if (blen > (us->MS_Lib.PagesPerBlock-PageNum))
Al Cho126bb032010-09-08 00:42:32 -0700277 len = us->MS_Lib.PagesPerBlock-PageNum;
278 else
279 len = blen;
280
281 oldphy = MS_LibConv2Physical(us, PhyBlockAddr);
282 newphy = MS_LibSearchBlockFromLogical(us, PhyBlockAddr);
283
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800284 result = MS_ReaderCopyBlock(us, oldphy, newphy,
285 PhyBlockAddr, PageNum,
286 buf+offset, len);
287 if (result != USB_STOR_XFER_GOOD) {
Cho, Yu-Chen615df842011-04-01 14:38:40 +0800288 pr_info("MS_SCSI_Write --- result = %x\n",
289 result);
Al Cho126bb032010-09-08 00:42:32 -0700290 result = USB_STOR_TRANSPORT_ERROR;
291 goto exit;
292 }
293
294 us->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED;
295 MS_LibForceSetLogicalPair(us, PhyBlockAddr, newphy);
296
297 blen -= len;
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800298 if (blen <= 0)
Al Cho126bb032010-09-08 00:42:32 -0700299 break;
300 PhyBlockAddr++;
301 PageNum = 0;
302 offset += MS_BYTES_PER_PAGE*len;
303 }
304exit:
305 kfree(buf);
306 }
307 return result;
308}
309
Cho, Yu-Chen70d83702011-05-18 18:40:10 +0800310/*
311 * MS_SCSIIrp()
312 */
313int MS_SCSIIrp(struct us_data *us, struct scsi_cmnd *srb)
314{
315 int result;
316
317 us->SrbStatus = SS_SUCCESS;
318 switch (srb->cmnd[0]) {
319 case TEST_UNIT_READY:
320 result = MS_SCSI_Test_Unit_Ready(us, srb);
321 break; /* 0x00 */
322 case INQUIRY:
323 result = MS_SCSI_Inquiry(us, srb);
324 break; /* 0x12 */
325 case MODE_SENSE:
326 result = MS_SCSI_Mode_Sense(us, srb);
327 break; /* 0x1A */
328 case READ_CAPACITY:
329 result = MS_SCSI_Read_Capacity(us, srb);
330 break; /* 0x25 */
331 case READ_10:
332 result = MS_SCSI_Read(us, srb);
333 break; /* 0x28 */
334 case WRITE_10:
335 result = MS_SCSI_Write(us, srb);
336 break; /* 0x2A */
337 default:
338 us->SrbStatus = SS_ILLEGAL_REQUEST;
339 result = USB_STOR_TRANSPORT_FAILED;
340 break;
341 }
342 return result;
343}
344