blob: 071bdc238786d5ea3b24bfc586c06a0bd3104f9f [file] [log] [blame]
Al Cho126bb032010-09-08 00:42:32 -07001#include <linux/sched.h>
2#include <linux/errno.h>
3#include <linux/slab.h>
4
5#include <scsi/scsi.h>
6#include <scsi/scsi_eh.h>
7#include <scsi/scsi_device.h>
8
9#include "usb.h"
10#include "scsiglue.h"
11#include "transport.h"
12#include "init.h"
13
Al Choe93192a2010-11-26 19:06:39 +080014/*
15 * ENE_InitMedia():
16 */
Al Cho126bb032010-09-08 00:42:32 -070017int ENE_InitMedia(struct us_data *us)
18{
19 int result;
20 BYTE MiscReg03 = 0;
21
Al Choe93192a2010-11-26 19:06:39 +080022 printk(KERN_INFO "--- Init Media ---\n");
Al Cho126bb032010-09-08 00:42:32 -070023 result = ENE_Read_BYTE(us, REG_CARD_STATUS, &MiscReg03);
Al Choe93192a2010-11-26 19:06:39 +080024 if (result != USB_STOR_XFER_GOOD) {
25 printk(KERN_ERR "Read register fail !!\n");
Al Cho126bb032010-09-08 00:42:32 -070026 return USB_STOR_TRANSPORT_ERROR;
27 }
Al Choe93192a2010-11-26 19:06:39 +080028 printk(KERN_INFO "MiscReg03 = %x\n", MiscReg03);
Al Cho126bb032010-09-08 00:42:32 -070029
Al Choe93192a2010-11-26 19:06:39 +080030 if (MiscReg03 & 0x02) {
31 if (!us->SM_Status.Ready && !us->MS_Status.Ready) {
Al Cho126bb032010-09-08 00:42:32 -070032 result = ENE_SMInit(us);
Al Choe93192a2010-11-26 19:06:39 +080033 if (result != USB_STOR_XFER_GOOD) {
Cho, Yu-Chen20c3d7f2011-07-07 11:27:14 +080034 return USB_STOR_TRANSPORT_ERROR;
Al Cho126bb032010-09-08 00:42:32 -070035 }
36 }
37
38 }
39 return result;
40}
41
Al Choe93192a2010-11-26 19:06:39 +080042/*
43 * ENE_Read_BYTE() :
44 */
Al Cho126bb032010-09-08 00:42:32 -070045int ENE_Read_BYTE(struct us_data *us, WORD index, void *buf)
46{
47 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
48 int result;
49
Konstantin Katuev307ae1d2010-10-29 12:18:18 +110050 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -070051 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
52 bcb->DataTransferLength = 0x01;
53 bcb->Flags = 0x80;
54 bcb->CDB[0] = 0xED;
55 bcb->CDB[2] = (BYTE)(index>>8);
56 bcb->CDB[3] = (BYTE)index;
57
58 result = ENE_SendScsiCmd(us, FDIR_READ, buf, 0);
59 return result;
60}
61
Al Choe93192a2010-11-26 19:06:39 +080062/*
Al Choe93192a2010-11-26 19:06:39 +080063 *ENE_SMInit()
64 */
Al Cho126bb032010-09-08 00:42:32 -070065int ENE_SMInit(struct us_data *us)
66{
67 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
68 int result;
69 BYTE buf[0x200];
70
Al Choe93192a2010-11-26 19:06:39 +080071 printk(KERN_INFO "transport --- ENE_SMInit\n");
Al Cho126bb032010-09-08 00:42:32 -070072
73 result = ENE_LoadBinCode(us, SM_INIT_PATTERN);
Al Choe93192a2010-11-26 19:06:39 +080074 if (result != USB_STOR_XFER_GOOD) {
75 printk(KERN_INFO "Load SM Init Code Fail !!\n");
Al Cho126bb032010-09-08 00:42:32 -070076 return USB_STOR_TRANSPORT_ERROR;
77 }
78
Konstantin Katuev307ae1d2010-10-29 12:18:18 +110079 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -070080 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
81 bcb->DataTransferLength = 0x200;
82 bcb->Flags = 0x80;
83 bcb->CDB[0] = 0xF1;
84 bcb->CDB[1] = 0x01;
85
86 result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
Al Choe93192a2010-11-26 19:06:39 +080087 if (result != USB_STOR_XFER_GOOD) {
88 printk(KERN_ERR
Lucas De Marchi25985ed2011-03-30 22:57:33 -030089 "Execution SM Init Code Fail !! result = %x\n", result);
Al Cho126bb032010-09-08 00:42:32 -070090 return USB_STOR_TRANSPORT_ERROR;
91 }
92
93 us->SM_Status = *(PSM_STATUS)&buf[0];
94
95 us->SM_DeviceID = buf[1];
96 us->SM_CardID = buf[2];
97
Al Choe93192a2010-11-26 19:06:39 +080098 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);
Al Cho126bb032010-09-08 00:42:32 -0700104 MediaChange = 1;
105 Check_D_MediaFmt(us);
Al Choe93192a2010-11-26 19:06:39 +0800106 } else {
107 printk(KERN_ERR "SM Card Not Ready --- %x\n", buf[0]);
Al Cho126bb032010-09-08 00:42:32 -0700108 return USB_STOR_TRANSPORT_ERROR;
109 }
110
111 return USB_STOR_TRANSPORT_GOOD;
112}
113
Al Choe93192a2010-11-26 19:06:39 +0800114/*
Al Choe93192a2010-11-26 19:06:39 +0800115 * ENE_LoadBinCode()
116 */
Al Cho126bb032010-09-08 00:42:32 -0700117int ENE_LoadBinCode(struct us_data *us, BYTE flag)
118{
119 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
120 int result;
Al Choe93192a2010-11-26 19:06:39 +0800121 /* void *buf; */
Al Cho126bb032010-09-08 00:42:32 -0700122 PBYTE buf;
123
Al Choe93192a2010-11-26 19:06:39 +0800124 /* printk(KERN_INFO "transport --- ENE_LoadBinCode\n"); */
Al Cho126bb032010-09-08 00:42:32 -0700125 if (us->BIN_FLAG == flag)
126 return USB_STOR_TRANSPORT_GOOD;
127
128 buf = kmalloc(0x800, GFP_KERNEL);
Vasiliy Kulikovb1f5f542010-09-19 11:36:23 +0400129 if (buf == NULL)
130 return USB_STOR_TRANSPORT_ERROR;
Al Choe93192a2010-11-26 19:06:39 +0800131 switch (flag) {
Al Choe93192a2010-11-26 19:06:39 +0800132 /* For SS */
133 case SM_INIT_PATTERN:
134 printk(KERN_INFO "SM_INIT_PATTERN\n");
135 memcpy(buf, SM_Init, 0x800);
Al Cho126bb032010-09-08 00:42:32 -0700136 break;
Al Choe93192a2010-11-26 19:06:39 +0800137 case SM_RW_PATTERN:
138 printk(KERN_INFO "SM_RW_PATTERN\n");
139 memcpy(buf, SM_Rdwr, 0x800);
Al Cho126bb032010-09-08 00:42:32 -0700140 break;
141 }
142
Konstantin Katuev307ae1d2010-10-29 12:18:18 +1100143 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -0700144 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
145 bcb->DataTransferLength = 0x800;
Al Choe93192a2010-11-26 19:06:39 +0800146 bcb->Flags = 0x00;
Al Cho126bb032010-09-08 00:42:32 -0700147 bcb->CDB[0] = 0xEF;
148
149 result = ENE_SendScsiCmd(us, FDIR_WRITE, buf, 0);
150
151 kfree(buf);
152 us->BIN_FLAG = flag;
153 return result;
154}
155
Al Choe93192a2010-11-26 19:06:39 +0800156/*
157 * ENE_SendScsiCmd():
158 */
Al Cho126bb032010-09-08 00:42:32 -0700159int ENE_SendScsiCmd(struct us_data *us, BYTE fDir, void *buf, int use_sg)
160{
161 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
162 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
163
164 int result;
Al Choe93192a2010-11-26 19:06:39 +0800165 unsigned int transfer_length = bcb->DataTransferLength,
166 cswlen = 0, partial = 0;
Al Cho126bb032010-09-08 00:42:32 -0700167 unsigned int residue;
168
Al Choe93192a2010-11-26 19:06:39 +0800169 /* 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");
Al Cho126bb032010-09-08 00:42:32 -0700175 return USB_STOR_TRANSPORT_ERROR;
176 }
177
Al Choe93192a2010-11-26 19:06:39 +0800178 if (buf) {
Al Cho6efe04e2010-11-26 19:07:27 +0800179 unsigned int pipe = fDir;
180
181 if (fDir == FDIR_READ)
182 pipe = us->recv_bulk_pipe;
183 else
184 pipe = us->send_bulk_pipe;
185
Al Choe93192a2010-11-26 19:06:39 +0800186 /* Bulk */
Al Cho126bb032010-09-08 00:42:32 -0700187 if (use_sg)
188 result = usb_stor_bulk_srb(us, pipe, us->srb);
189 else
Al Choe93192a2010-11-26 19:06:39 +0800190 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");
Al Cho126bb032010-09-08 00:42:32 -0700194 return USB_STOR_TRANSPORT_ERROR;
195 }
196 }
197
Al Choe93192a2010-11-26 19:06:39 +0800198 /* 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);
Al Cho126bb032010-09-08 00:42:32 -0700201
Al Choe93192a2010-11-26 19:06:39 +0800202 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);
Al Cho126bb032010-09-08 00:42:32 -0700206 }
207
Al Choe93192a2010-11-26 19:06:39 +0800208 if (result == USB_STOR_XFER_STALLED) {
Al Cho126bb032010-09-08 00:42:32 -0700209 /* get the status again */
Al Choe93192a2010-11-26 19:06:39 +0800210 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);
Al Cho126bb032010-09-08 00:42:32 -0700213 }
214
215 if (result != USB_STOR_XFER_GOOD)
216 return USB_STOR_TRANSPORT_ERROR;
217
218 /* check bulk status */
219 residue = le32_to_cpu(bcs->Residue);
220
Al Choe93192a2010-11-26 19:06:39 +0800221 /*
222 * try to compute the actual residue, based on how much data
223 * was really transferred and what the device tells us
224 */
225 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
Al Cho126bb032010-09-08 00:42:32 -0700226 residue = min(residue, transfer_length);
huajun li41e568d2011-03-04 10:56:18 +0800227 if (us->srb)
228 scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
229 (int) residue));
Al Cho126bb032010-09-08 00:42:32 -0700230 }
231
232 if (bcs->Status != US_BULK_STAT_OK)
233 return USB_STOR_TRANSPORT_ERROR;
234
235 return USB_STOR_TRANSPORT_GOOD;
236}
237
Al Choe93192a2010-11-26 19:06:39 +0800238/*
239 * ENE_Read_Data()
240 */
Al Cho126bb032010-09-08 00:42:32 -0700241int ENE_Read_Data(struct us_data *us, void *buf, unsigned int length)
242{
243 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
244 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
245 int result;
246
Al Choe93192a2010-11-26 19:06:39 +0800247 /* printk(KERN_INFO "transport --- ENE_Read_Data\n"); */
248 /* set up the command wrapper */
Konstantin Katuev307ae1d2010-10-29 12:18:18 +1100249 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -0700250 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
251 bcb->DataTransferLength = length;
Al Choe93192a2010-11-26 19:06:39 +0800252 bcb->Flags = 0x80;
Al Cho126bb032010-09-08 00:42:32 -0700253 bcb->CDB[0] = 0xED;
254 bcb->CDB[2] = 0xFF;
255 bcb->CDB[3] = 0x81;
256
Al Choe93192a2010-11-26 19:06:39 +0800257 /* 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);
Al Cho126bb032010-09-08 00:42:32 -0700260 if (result != USB_STOR_XFER_GOOD)
261 return USB_STOR_TRANSPORT_ERROR;
262
Al Choe93192a2010-11-26 19:06:39 +0800263 /* R/W data */
264 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
265 buf, length, NULL);
Al Cho126bb032010-09-08 00:42:32 -0700266 if (result != USB_STOR_XFER_GOOD)
267 return USB_STOR_TRANSPORT_ERROR;
268
Al Choe93192a2010-11-26 19:06:39 +0800269 /* 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);
Al Cho126bb032010-09-08 00:42:32 -0700272 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;
276
277 return USB_STOR_TRANSPORT_GOOD;
278}
279
Al Choe93192a2010-11-26 19:06:39 +0800280/*
281 * ENE_Write_Data():
282 */
Al Cho126bb032010-09-08 00:42:32 -0700283int ENE_Write_Data(struct us_data *us, void *buf, unsigned int length)
284{
285 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
286 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
287 int result;
288
Al Choe93192a2010-11-26 19:06:39 +0800289 /* printk("transport --- ENE_Write_Data\n"); */
290 /* set up the command wrapper */
Konstantin Katuev307ae1d2010-10-29 12:18:18 +1100291 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
Al Cho126bb032010-09-08 00:42:32 -0700292 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
293 bcb->DataTransferLength = length;
Al Choe93192a2010-11-26 19:06:39 +0800294 bcb->Flags = 0x00;
Al Cho126bb032010-09-08 00:42:32 -0700295 bcb->CDB[0] = 0xEE;
296 bcb->CDB[2] = 0xFF;
297 bcb->CDB[3] = 0x81;
298
Al Choe93192a2010-11-26 19:06:39 +0800299 /* 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);
Al Cho126bb032010-09-08 00:42:32 -0700302 if (result != USB_STOR_XFER_GOOD)
303 return USB_STOR_TRANSPORT_ERROR;
304
Al Choe93192a2010-11-26 19:06:39 +0800305 /* R/W data */
306 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
307 buf, length, NULL);
Al Cho126bb032010-09-08 00:42:32 -0700308 if (result != USB_STOR_XFER_GOOD)
309 return USB_STOR_TRANSPORT_ERROR;
310
Al Choe93192a2010-11-26 19:06:39 +0800311 /* 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);
Al Cho126bb032010-09-08 00:42:32 -0700314 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;
318
319 return USB_STOR_TRANSPORT_GOOD;
320}
321
Al Choe93192a2010-11-26 19:06:39 +0800322/*
323 * usb_stor_print_cmd():
324 */
Al Cho126bb032010-09-08 00:42:32 -0700325void usb_stor_print_cmd(struct scsi_cmnd *srb)
326{
327 PBYTE Cdb = srb->cmnd;
328 DWORD cmd = Cdb[0];
Al Choe93192a2010-11-26 19:06:39 +0800329 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);
Al Cho126bb032010-09-08 00:42:32 -0700334
335 switch (cmd) {
336 case TEST_UNIT_READY:
Al Choe93192a2010-11-26 19:06:39 +0800337 /* printk(KERN_INFO
338 "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
Al Cho126bb032010-09-08 00:42:32 -0700339 break;
340 case INQUIRY:
Al Choe93192a2010-11-26 19:06:39 +0800341 printk(KERN_INFO "scsi cmd %X --- SCSIOP_INQUIRY\n", cmd);
Al Cho126bb032010-09-08 00:42:32 -0700342 break;
343 case MODE_SENSE:
Al Choe93192a2010-11-26 19:06:39 +0800344 printk(KERN_INFO "scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd);
Al Cho126bb032010-09-08 00:42:32 -0700345 break;
346 case START_STOP:
Al Choe93192a2010-11-26 19:06:39 +0800347 printk(KERN_INFO "scsi cmd %X --- SCSIOP_START_STOP\n", cmd);
Al Cho126bb032010-09-08 00:42:32 -0700348 break;
349 case READ_CAPACITY:
Al Choe93192a2010-11-26 19:06:39 +0800350 printk(KERN_INFO "scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd);
Al Cho126bb032010-09-08 00:42:32 -0700351 break;
352 case READ_10:
Al Choe93192a2010-11-26 19:06:39 +0800353 /* printk(KERN_INFO
354 "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
355 ,cmd, bn, blen); */
Al Cho126bb032010-09-08 00:42:32 -0700356 break;
357 case WRITE_10:
Al Choe93192a2010-11-26 19:06:39 +0800358 /* printk(KERN_INFO
359 "scsi cmd %X --- SCSIOP_WRITE,
360 bn = %X, blen = %X\n" , cmd, bn, blen); */
Al Cho126bb032010-09-08 00:42:32 -0700361 break;
362 case ALLOW_MEDIUM_REMOVAL:
Al Choe93192a2010-11-26 19:06:39 +0800363 printk(KERN_INFO
364 "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd);
Al Cho126bb032010-09-08 00:42:32 -0700365 break;
366 default:
Al Choe93192a2010-11-26 19:06:39 +0800367 printk(KERN_INFO "scsi cmd %X --- Other cmd\n", cmd);
Al Cho126bb032010-09-08 00:42:32 -0700368 break;
369 }
370 bn = 0;
371 blen = 0;
372}
373