Merge remote-tracking branch 'cleancache/linux-next'
[linux-2.6/next.git] / drivers / staging / keucr / init.c
blob8af7c84daee28d7645f6adb99ef8a1c2c0009964
1 #include <linux/sched.h>
2 #include <linux/errno.h>
3 #include <linux/slab.h>
5 #include <scsi/scsi.h>
6 #include <scsi/scsi_eh.h>
7 #include <scsi/scsi_device.h>
9 #include "usb.h"
10 #include "scsiglue.h"
11 #include "transport.h"
12 #include "init.h"
14 BYTE IsSSFDCCompliance;
15 BYTE IsXDCompliance;
18 * ENE_InitMedia():
20 int ENE_InitMedia(struct us_data *us)
22 int result;
23 BYTE MiscReg03 = 0;
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 & 0x02) {
34 if (!us->SM_Status.Ready && !us->MS_Status.Ready) {
35 result = ENE_SMInit(us);
36 if (result != USB_STOR_XFER_GOOD) {
37 result = ENE_MSInit(us);
38 if (result != USB_STOR_XFER_GOOD)
39 return USB_STOR_TRANSPORT_ERROR;
44 return result;
48 * ENE_Read_BYTE() :
50 int ENE_Read_BYTE(struct us_data *us, WORD index, void *buf)
52 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
53 int result;
55 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
56 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
57 bcb->DataTransferLength = 0x01;
58 bcb->Flags = 0x80;
59 bcb->CDB[0] = 0xED;
60 bcb->CDB[2] = (BYTE)(index>>8);
61 bcb->CDB[3] = (BYTE)index;
63 result = ENE_SendScsiCmd(us, FDIR_READ, buf, 0);
64 return result;
68 * ENE_MSInit():
70 int ENE_MSInit(struct us_data *us)
72 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
73 int result;
74 BYTE buf[0x200];
75 WORD MSP_BlockSize, MSP_UserAreaBlocks;
77 printk(KERN_INFO "transport --- ENE_MSInit\n");
78 result = ENE_LoadBinCode(us, MS_INIT_PATTERN);
79 if (result != USB_STOR_XFER_GOOD) {
80 printk(KERN_ERR "Load MS Init Code Fail !!\n");
81 return USB_STOR_TRANSPORT_ERROR;
84 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
85 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
86 bcb->DataTransferLength = 0x200;
87 bcb->Flags = 0x80;
88 bcb->CDB[0] = 0xF1;
89 bcb->CDB[1] = 0x01;
91 result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
92 if (result != USB_STOR_XFER_GOOD) {
93 printk(KERN_ERR "Execution MS Init Code Fail !!\n");
94 return USB_STOR_TRANSPORT_ERROR;
97 us->MS_Status = *(PMS_STATUS)&buf[0];
99 if (us->MS_Status.Insert && us->MS_Status.Ready) {
100 printk(KERN_INFO "Insert = %x\n", us->MS_Status.Insert);
101 printk(KERN_INFO "Ready = %x\n", us->MS_Status.Ready);
102 printk(KERN_INFO "IsMSPro = %x\n", us->MS_Status.IsMSPro);
103 printk(KERN_INFO "IsMSPHG = %x\n", us->MS_Status.IsMSPHG);
104 printk(KERN_INFO "WtP = %x\n", us->MS_Status.WtP);
105 if (us->MS_Status.IsMSPro) {
106 MSP_BlockSize = (buf[6] << 8) | buf[7];
107 MSP_UserAreaBlocks = (buf[10] << 8) | buf[11];
108 us->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
109 } else {
110 MS_CardInit(us);
112 printk(KERN_INFO "MS Init Code OK !!\n");
113 } else {
114 printk(KERN_INFO "MS Card Not Ready --- %x\n", buf[0]);
115 return USB_STOR_TRANSPORT_ERROR;
118 return USB_STOR_TRANSPORT_GOOD;
122 *ENE_SMInit()
124 int ENE_SMInit(struct us_data *us)
126 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
127 int result;
128 BYTE buf[0x200];
130 printk(KERN_INFO "transport --- ENE_SMInit\n");
132 result = ENE_LoadBinCode(us, SM_INIT_PATTERN);
133 if (result != USB_STOR_XFER_GOOD) {
134 printk(KERN_INFO "Load SM Init Code Fail !!\n");
135 return USB_STOR_TRANSPORT_ERROR;
138 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
139 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
140 bcb->DataTransferLength = 0x200;
141 bcb->Flags = 0x80;
142 bcb->CDB[0] = 0xF1;
143 bcb->CDB[1] = 0x01;
145 result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
146 if (result != USB_STOR_XFER_GOOD) {
147 printk(KERN_ERR
148 "Execution SM Init Code Fail !! result = %x\n", result);
149 return USB_STOR_TRANSPORT_ERROR;
152 us->SM_Status = *(PSM_STATUS)&buf[0];
154 us->SM_DeviceID = buf[1];
155 us->SM_CardID = buf[2];
157 if (us->SM_Status.Insert && us->SM_Status.Ready) {
158 printk(KERN_INFO "Insert = %x\n", us->SM_Status.Insert);
159 printk(KERN_INFO "Ready = %x\n", us->SM_Status.Ready);
160 printk(KERN_INFO "WtP = %x\n", us->SM_Status.WtP);
161 printk(KERN_INFO "DeviceID = %x\n", us->SM_DeviceID);
162 printk(KERN_INFO "CardID = %x\n", us->SM_CardID);
163 MediaChange = 1;
164 Check_D_MediaFmt(us);
165 } else {
166 printk(KERN_ERR "SM Card Not Ready --- %x\n", buf[0]);
167 return USB_STOR_TRANSPORT_ERROR;
170 return USB_STOR_TRANSPORT_GOOD;
174 * ENE_LoadBinCode()
176 int ENE_LoadBinCode(struct us_data *us, BYTE flag)
178 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
179 int result;
180 /* void *buf; */
181 PBYTE buf;
183 /* printk(KERN_INFO "transport --- ENE_LoadBinCode\n"); */
184 if (us->BIN_FLAG == flag)
185 return USB_STOR_TRANSPORT_GOOD;
187 buf = kmalloc(0x800, GFP_KERNEL);
188 if (buf == NULL)
189 return USB_STOR_TRANSPORT_ERROR;
190 switch (flag) {
191 /* For MS */
192 case MS_INIT_PATTERN:
193 printk(KERN_INFO "MS_INIT_PATTERN\n");
194 memcpy(buf, MS_Init, 0x800);
195 break;
196 case MSP_RW_PATTERN:
197 printk(KERN_INFO "MSP_RW_PATTERN\n");
198 memcpy(buf, MSP_Rdwr, 0x800);
199 break;
200 case MS_RW_PATTERN:
201 printk(KERN_INFO "MS_RW_PATTERN\n");
202 memcpy(buf, MS_Rdwr, 0x800);
203 break;
204 /* For SS */
205 case SM_INIT_PATTERN:
206 printk(KERN_INFO "SM_INIT_PATTERN\n");
207 memcpy(buf, SM_Init, 0x800);
208 break;
209 case SM_RW_PATTERN:
210 printk(KERN_INFO "SM_RW_PATTERN\n");
211 memcpy(buf, SM_Rdwr, 0x800);
212 break;
215 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
216 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
217 bcb->DataTransferLength = 0x800;
218 bcb->Flags = 0x00;
219 bcb->CDB[0] = 0xEF;
221 result = ENE_SendScsiCmd(us, FDIR_WRITE, buf, 0);
223 kfree(buf);
224 us->BIN_FLAG = flag;
225 return result;
229 * ENE_SendScsiCmd():
231 int ENE_SendScsiCmd(struct us_data *us, BYTE fDir, void *buf, int use_sg)
233 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
234 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
236 int result;
237 unsigned int transfer_length = bcb->DataTransferLength,
238 cswlen = 0, partial = 0;
239 unsigned int residue;
241 /* printk(KERN_INFO "transport --- ENE_SendScsiCmd\n"); */
242 /* send cmd to out endpoint */
243 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
244 bcb, US_BULK_CB_WRAP_LEN, NULL);
245 if (result != USB_STOR_XFER_GOOD) {
246 printk(KERN_ERR "send cmd to out endpoint fail ---\n");
247 return USB_STOR_TRANSPORT_ERROR;
250 if (buf) {
251 unsigned int pipe = fDir;
253 if (fDir == FDIR_READ)
254 pipe = us->recv_bulk_pipe;
255 else
256 pipe = us->send_bulk_pipe;
258 /* Bulk */
259 if (use_sg)
260 result = usb_stor_bulk_srb(us, pipe, us->srb);
261 else
262 result = usb_stor_bulk_transfer_sg(us, pipe, buf,
263 transfer_length, 0, &partial);
264 if (result != USB_STOR_XFER_GOOD) {
265 printk(KERN_ERR "data transfer fail ---\n");
266 return USB_STOR_TRANSPORT_ERROR;
270 /* Get CSW for device status */
271 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
272 US_BULK_CS_WRAP_LEN, &cswlen);
274 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
275 printk(KERN_WARNING "Received 0-length CSW; retrying...\n");
276 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
277 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
280 if (result == USB_STOR_XFER_STALLED) {
281 /* get the status again */
282 printk(KERN_WARNING "Attempting to get CSW (2nd try)...\n");
283 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
284 bcs, US_BULK_CS_WRAP_LEN, NULL);
287 if (result != USB_STOR_XFER_GOOD)
288 return USB_STOR_TRANSPORT_ERROR;
290 /* check bulk status */
291 residue = le32_to_cpu(bcs->Residue);
294 * try to compute the actual residue, based on how much data
295 * was really transferred and what the device tells us
297 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
298 residue = min(residue, transfer_length);
299 if (us->srb)
300 scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
301 (int) residue));
304 if (bcs->Status != US_BULK_STAT_OK)
305 return USB_STOR_TRANSPORT_ERROR;
307 return USB_STOR_TRANSPORT_GOOD;
311 * ENE_Read_Data()
313 int ENE_Read_Data(struct us_data *us, void *buf, unsigned int length)
315 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
316 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
317 int result;
319 /* printk(KERN_INFO "transport --- ENE_Read_Data\n"); */
320 /* set up the command wrapper */
321 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
322 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
323 bcb->DataTransferLength = length;
324 bcb->Flags = 0x80;
325 bcb->CDB[0] = 0xED;
326 bcb->CDB[2] = 0xFF;
327 bcb->CDB[3] = 0x81;
329 /* send cmd to out endpoint */
330 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
331 US_BULK_CB_WRAP_LEN, NULL);
332 if (result != USB_STOR_XFER_GOOD)
333 return USB_STOR_TRANSPORT_ERROR;
335 /* R/W data */
336 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
337 buf, length, NULL);
338 if (result != USB_STOR_XFER_GOOD)
339 return USB_STOR_TRANSPORT_ERROR;
341 /* Get CSW for device status */
342 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
343 US_BULK_CS_WRAP_LEN, NULL);
344 if (result != USB_STOR_XFER_GOOD)
345 return USB_STOR_TRANSPORT_ERROR;
346 if (bcs->Status != US_BULK_STAT_OK)
347 return USB_STOR_TRANSPORT_ERROR;
349 return USB_STOR_TRANSPORT_GOOD;
353 * ENE_Write_Data():
355 int ENE_Write_Data(struct us_data *us, void *buf, unsigned int length)
357 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
358 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
359 int result;
361 /* printk("transport --- ENE_Write_Data\n"); */
362 /* set up the command wrapper */
363 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
364 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
365 bcb->DataTransferLength = length;
366 bcb->Flags = 0x00;
367 bcb->CDB[0] = 0xEE;
368 bcb->CDB[2] = 0xFF;
369 bcb->CDB[3] = 0x81;
371 /* send cmd to out endpoint */
372 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
373 US_BULK_CB_WRAP_LEN, NULL);
374 if (result != USB_STOR_XFER_GOOD)
375 return USB_STOR_TRANSPORT_ERROR;
377 /* R/W data */
378 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
379 buf, length, NULL);
380 if (result != USB_STOR_XFER_GOOD)
381 return USB_STOR_TRANSPORT_ERROR;
383 /* Get CSW for device status */
384 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
385 US_BULK_CS_WRAP_LEN, NULL);
386 if (result != USB_STOR_XFER_GOOD)
387 return USB_STOR_TRANSPORT_ERROR;
388 if (bcs->Status != US_BULK_STAT_OK)
389 return USB_STOR_TRANSPORT_ERROR;
391 return USB_STOR_TRANSPORT_GOOD;
395 * usb_stor_print_cmd():
397 void usb_stor_print_cmd(struct scsi_cmnd *srb)
399 PBYTE Cdb = srb->cmnd;
400 DWORD cmd = Cdb[0];
401 DWORD bn = ((Cdb[2] << 24) & 0xff000000) |
402 ((Cdb[3] << 16) & 0x00ff0000) |
403 ((Cdb[4] << 8) & 0x0000ff00) |
404 ((Cdb[5] << 0) & 0x000000ff);
405 WORD blen = ((Cdb[7] << 8) & 0xff00) | ((Cdb[8] << 0) & 0x00ff);
407 switch (cmd) {
408 case TEST_UNIT_READY:
409 /* printk(KERN_INFO
410 "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
411 break;
412 case INQUIRY:
413 printk(KERN_INFO "scsi cmd %X --- SCSIOP_INQUIRY\n", cmd);
414 break;
415 case MODE_SENSE:
416 printk(KERN_INFO "scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd);
417 break;
418 case START_STOP:
419 printk(KERN_INFO "scsi cmd %X --- SCSIOP_START_STOP\n", cmd);
420 break;
421 case READ_CAPACITY:
422 printk(KERN_INFO "scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd);
423 break;
424 case READ_10:
425 /* printk(KERN_INFO
426 "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
427 ,cmd, bn, blen); */
428 break;
429 case WRITE_10:
430 /* printk(KERN_INFO
431 "scsi cmd %X --- SCSIOP_WRITE,
432 bn = %X, blen = %X\n" , cmd, bn, blen); */
433 break;
434 case ALLOW_MEDIUM_REMOVAL:
435 printk(KERN_INFO
436 "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd);
437 break;
438 default:
439 printk(KERN_INFO "scsi cmd %X --- Other cmd\n", cmd);
440 break;
442 bn = 0;
443 blen = 0;