• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #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 
14 /*
15  * ENE_InitMedia():
16  */
ENE_InitMedia(struct us_data * us)17 int ENE_InitMedia(struct us_data *us)
18 {
19 	int	result;
20 	BYTE	MiscReg03 = 0;
21 
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;
27 	}
28 	printk(KERN_INFO "MiscReg03 = %x\n", MiscReg03);
29 
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;
35 		}
36 
37 	}
38 	return result;
39 }
40 
41 /*
42  * ENE_Read_BYTE() :
43  */
ENE_Read_BYTE(struct us_data * us,WORD index,void * buf)44 int ENE_Read_BYTE(struct us_data *us, WORD index, void *buf)
45 {
46 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
47 	int result;
48 
49 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
50 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
51 	bcb->DataTransferLength	= 0x01;
52 	bcb->Flags			= 0x80;
53 	bcb->CDB[0]			= 0xED;
54 	bcb->CDB[2]			= (BYTE)(index>>8);
55 	bcb->CDB[3]			= (BYTE)index;
56 
57 	result = ENE_SendScsiCmd(us, FDIR_READ, buf, 0);
58 	return result;
59 }
60 
61 /*
62  *ENE_SMInit()
63  */
ENE_SMInit(struct us_data * us)64 int ENE_SMInit(struct us_data *us)
65 {
66 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
67 	int	result;
68 	BYTE	buf[0x200];
69 
70 	printk(KERN_INFO "transport --- ENE_SMInit\n");
71 
72 	result = ENE_LoadBinCode(us, SM_INIT_PATTERN);
73 	if (result != USB_STOR_XFER_GOOD) {
74 		printk(KERN_INFO "Load SM Init Code Fail !!\n");
75 		return USB_STOR_TRANSPORT_ERROR;
76 	}
77 
78 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
79 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
80 	bcb->DataTransferLength	= 0x200;
81 	bcb->Flags			= 0x80;
82 	bcb->CDB[0]			= 0xF1;
83 	bcb->CDB[1]			= 0x01;
84 
85 	result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
86 	if (result != USB_STOR_XFER_GOOD) {
87 		printk(KERN_ERR
88 		       "Execution SM Init Code Fail !! result = %x\n", result);
89 		return USB_STOR_TRANSPORT_ERROR;
90 	}
91 
92 	us->SM_Status = *(PSM_STATUS)&buf[0];
93 
94 	us->SM_DeviceID = buf[1];
95 	us->SM_CardID   = buf[2];
96 
97 	if (us->SM_Status.Insert && us->SM_Status.Ready) {
98 		printk(KERN_INFO "Insert     = %x\n", us->SM_Status.Insert);
99 		printk(KERN_INFO "Ready      = %x\n", us->SM_Status.Ready);
100 		printk(KERN_INFO "WtP        = %x\n", us->SM_Status.WtP);
101 		printk(KERN_INFO "DeviceID   = %x\n", us->SM_DeviceID);
102 		printk(KERN_INFO "CardID     = %x\n", us->SM_CardID);
103 		MediaChange = 1;
104 		Check_D_MediaFmt(us);
105 	} else {
106 		printk(KERN_ERR "SM Card Not Ready --- %x\n", buf[0]);
107 		return USB_STOR_TRANSPORT_ERROR;
108 	}
109 
110 	return USB_STOR_TRANSPORT_GOOD;
111 }
112 
113 /*
114  * ENE_LoadBinCode()
115  */
ENE_LoadBinCode(struct us_data * us,BYTE flag)116 int ENE_LoadBinCode(struct us_data *us, BYTE flag)
117 {
118 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
119 	int result;
120 	/* void *buf; */
121 	PBYTE buf;
122 
123 	/* printk(KERN_INFO "transport --- ENE_LoadBinCode\n"); */
124 	if (us->BIN_FLAG == flag)
125 		return USB_STOR_TRANSPORT_GOOD;
126 
127 	buf = kmalloc(0x800, GFP_KERNEL);
128 	if (buf == NULL)
129 		return USB_STOR_TRANSPORT_ERROR;
130 	switch (flag) {
131 	/* For SS */
132 	case SM_INIT_PATTERN:
133 		printk(KERN_INFO "SM_INIT_PATTERN\n");
134 		memcpy(buf, SM_Init, 0x800);
135 		break;
136 	case SM_RW_PATTERN:
137 		printk(KERN_INFO "SM_RW_PATTERN\n");
138 		memcpy(buf, SM_Rdwr, 0x800);
139 		break;
140 	}
141 
142 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
143 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
144 	bcb->DataTransferLength = 0x800;
145 	bcb->Flags = 0x00;
146 	bcb->CDB[0] = 0xEF;
147 
148 	result = ENE_SendScsiCmd(us, FDIR_WRITE, buf, 0);
149 
150 	kfree(buf);
151 	us->BIN_FLAG = flag;
152 	return result;
153 }
154 
155 /*
156  * ENE_SendScsiCmd():
157  */
ENE_SendScsiCmd(struct us_data * us,BYTE fDir,void * buf,int use_sg)158 int ENE_SendScsiCmd(struct us_data *us, BYTE fDir, void *buf, int use_sg)
159 {
160 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
161 	struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
162 
163 	int result;
164 	unsigned int transfer_length = bcb->DataTransferLength,
165 		     cswlen = 0, partial = 0;
166 	unsigned int residue;
167 
168 	/* printk(KERN_INFO "transport --- ENE_SendScsiCmd\n"); */
169 	/* send cmd to out endpoint */
170 	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
171 					    bcb, US_BULK_CB_WRAP_LEN, NULL);
172 	if (result != USB_STOR_XFER_GOOD) {
173 		printk(KERN_ERR "send cmd to out endpoint fail ---\n");
174 		return USB_STOR_TRANSPORT_ERROR;
175 	}
176 
177 	if (buf) {
178 		unsigned int pipe = fDir;
179 
180 		if (fDir == FDIR_READ)
181 			pipe = us->recv_bulk_pipe;
182 		else
183 			pipe = us->send_bulk_pipe;
184 
185 		/* Bulk */
186 		if (use_sg)
187 			result = usb_stor_bulk_srb(us, pipe, us->srb);
188 		else
189 			result = usb_stor_bulk_transfer_sg(us, pipe, buf,
190 						transfer_length, 0, &partial);
191 		if (result != USB_STOR_XFER_GOOD) {
192 			printk(KERN_ERR "data transfer fail ---\n");
193 			return USB_STOR_TRANSPORT_ERROR;
194 		}
195 	}
196 
197 	/* Get CSW for device status */
198 	result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
199 						US_BULK_CS_WRAP_LEN, &cswlen);
200 
201 	if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
202 		printk(KERN_WARNING "Received 0-length CSW; retrying...\n");
203 		result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
204 					bcs, US_BULK_CS_WRAP_LEN, &cswlen);
205 	}
206 
207 	if (result == USB_STOR_XFER_STALLED) {
208 		/* get the status again */
209 		printk(KERN_WARNING "Attempting to get CSW (2nd try)...\n");
210 		result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
211 						bcs, US_BULK_CS_WRAP_LEN, NULL);
212 	}
213 
214 	if (result != USB_STOR_XFER_GOOD)
215 		return USB_STOR_TRANSPORT_ERROR;
216 
217 	/* check bulk status */
218 	residue = le32_to_cpu(bcs->Residue);
219 
220 	/*
221 	 * try to compute the actual residue, based on how much data
222 	 * was really transferred and what the device tells us
223 	 */
224 	if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
225 		residue = min(residue, transfer_length);
226 		if (us->srb)
227 			scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
228 					(int) residue));
229 	}
230 
231 	if (bcs->Status != US_BULK_STAT_OK)
232 		return USB_STOR_TRANSPORT_ERROR;
233 
234 	return USB_STOR_TRANSPORT_GOOD;
235 }
236 
237 /*
238  * ENE_Read_Data()
239  */
ENE_Read_Data(struct us_data * us,void * buf,unsigned int length)240 int ENE_Read_Data(struct us_data *us, void *buf, unsigned int length)
241 {
242 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
243 	struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
244 	int result;
245 
246 	/* printk(KERN_INFO "transport --- ENE_Read_Data\n"); */
247 	/* set up the command wrapper */
248 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
249 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
250 	bcb->DataTransferLength = length;
251 	bcb->Flags = 0x80;
252 	bcb->CDB[0] = 0xED;
253 	bcb->CDB[2] = 0xFF;
254 	bcb->CDB[3] = 0x81;
255 
256 	/* send cmd to out endpoint */
257 	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
258 						US_BULK_CB_WRAP_LEN, NULL);
259 	if (result != USB_STOR_XFER_GOOD)
260 		return USB_STOR_TRANSPORT_ERROR;
261 
262 	/* R/W data */
263 	result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
264 						buf, length, NULL);
265 	if (result != USB_STOR_XFER_GOOD)
266 		return USB_STOR_TRANSPORT_ERROR;
267 
268 	/* Get CSW for device status */
269 	result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
270 						US_BULK_CS_WRAP_LEN, NULL);
271 	if (result != USB_STOR_XFER_GOOD)
272 		return USB_STOR_TRANSPORT_ERROR;
273 	if (bcs->Status != US_BULK_STAT_OK)
274 		return USB_STOR_TRANSPORT_ERROR;
275 
276 	return USB_STOR_TRANSPORT_GOOD;
277 }
278 
279 /*
280  * ENE_Write_Data():
281  */
ENE_Write_Data(struct us_data * us,void * buf,unsigned int length)282 int ENE_Write_Data(struct us_data *us, void *buf, unsigned int length)
283 {
284 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
285 	struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
286 	int result;
287 
288 	/* printk("transport --- ENE_Write_Data\n"); */
289 	/* set up the command wrapper */
290 	memset(bcb, 0, sizeof(struct bulk_cb_wrap));
291 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
292 	bcb->DataTransferLength = length;
293 	bcb->Flags = 0x00;
294 	bcb->CDB[0] = 0xEE;
295 	bcb->CDB[2] = 0xFF;
296 	bcb->CDB[3] = 0x81;
297 
298 	/* send cmd to out endpoint */
299 	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
300 						US_BULK_CB_WRAP_LEN, NULL);
301 	if (result != USB_STOR_XFER_GOOD)
302 		return USB_STOR_TRANSPORT_ERROR;
303 
304 	/* R/W data */
305 	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
306 						buf, length, NULL);
307 	if (result != USB_STOR_XFER_GOOD)
308 		return USB_STOR_TRANSPORT_ERROR;
309 
310 	/* Get CSW for device status */
311 	result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
312 						US_BULK_CS_WRAP_LEN, NULL);
313 	if (result != USB_STOR_XFER_GOOD)
314 		return USB_STOR_TRANSPORT_ERROR;
315 	if (bcs->Status != US_BULK_STAT_OK)
316 		return USB_STOR_TRANSPORT_ERROR;
317 
318 	return USB_STOR_TRANSPORT_GOOD;
319 }
320 
321 /*
322  * usb_stor_print_cmd():
323  */
usb_stor_print_cmd(struct scsi_cmnd * srb)324 void usb_stor_print_cmd(struct scsi_cmnd *srb)
325 {
326 	PBYTE	Cdb = srb->cmnd;
327 	DWORD	cmd = Cdb[0];
328 	DWORD	bn  =	((Cdb[2] << 24) & 0xff000000) |
329 			((Cdb[3] << 16) & 0x00ff0000) |
330 			((Cdb[4] << 8) & 0x0000ff00) |
331 			((Cdb[5] << 0) & 0x000000ff);
332 	WORD	blen = ((Cdb[7] << 8) & 0xff00) | ((Cdb[8] << 0) & 0x00ff);
333 
334 	switch (cmd) {
335 	case TEST_UNIT_READY:
336 		/* printk(KERN_INFO
337 			 "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
338 		break;
339 	case INQUIRY:
340 		printk(KERN_INFO "scsi cmd %X --- SCSIOP_INQUIRY\n", cmd);
341 		break;
342 	case MODE_SENSE:
343 		printk(KERN_INFO "scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd);
344 		break;
345 	case START_STOP:
346 		printk(KERN_INFO "scsi cmd %X --- SCSIOP_START_STOP\n", cmd);
347 		break;
348 	case READ_CAPACITY:
349 		printk(KERN_INFO "scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd);
350 		break;
351 	case READ_10:
352 		/*  printk(KERN_INFO
353 			   "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
354 			   ,cmd, bn, blen); */
355 		break;
356 	case WRITE_10:
357 		/* printk(KERN_INFO
358 			  "scsi cmd %X --- SCSIOP_WRITE,
359 			  bn = %X, blen = %X\n" , cmd, bn, blen); */
360 		break;
361 	case ALLOW_MEDIUM_REMOVAL:
362 		printk(KERN_INFO
363 			"scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd);
364 		break;
365 	default:
366 		printk(KERN_INFO "scsi cmd %X --- Other cmd\n", cmd);
367 		break;
368 	}
369 	bn = 0;
370 	blen = 0;
371 }
372 
373