Realtek cr: Add autosuspend function.
[zen-stable.git] / drivers / staging / keucr / init.c
blobb5a89375df22f5a41be617d887d79b11da3dc7d8
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"
15 * ENE_InitMedia():
17 int ENE_InitMedia(struct us_data *us)
19 int result;
20 BYTE MiscReg03 = 0;
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;
28 printk(KERN_INFO "MiscReg03 = %x\n", MiscReg03);
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 result = ENE_MSInit(us);
35 if (result != USB_STOR_XFER_GOOD)
36 return USB_STOR_TRANSPORT_ERROR;
41 return result;
45 * ENE_Read_BYTE() :
47 int ENE_Read_BYTE(struct us_data *us, WORD index, void *buf)
49 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
50 int result;
52 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
53 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
54 bcb->DataTransferLength = 0x01;
55 bcb->Flags = 0x80;
56 bcb->CDB[0] = 0xED;
57 bcb->CDB[2] = (BYTE)(index>>8);
58 bcb->CDB[3] = (BYTE)index;
60 result = ENE_SendScsiCmd(us, FDIR_READ, buf, 0);
61 return result;
65 * ENE_MSInit():
67 int ENE_MSInit(struct us_data *us)
69 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
70 int result;
71 BYTE buf[0x200];
72 WORD MSP_BlockSize, MSP_UserAreaBlocks;
74 printk(KERN_INFO "transport --- ENE_MSInit\n");
75 result = ENE_LoadBinCode(us, MS_INIT_PATTERN);
76 if (result != USB_STOR_XFER_GOOD) {
77 printk(KERN_ERR "Load MS Init Code Fail !!\n");
78 return USB_STOR_TRANSPORT_ERROR;
81 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
82 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
83 bcb->DataTransferLength = 0x200;
84 bcb->Flags = 0x80;
85 bcb->CDB[0] = 0xF1;
86 bcb->CDB[1] = 0x01;
88 result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
89 if (result != USB_STOR_XFER_GOOD) {
90 printk(KERN_ERR "Execution MS Init Code Fail !!\n");
91 return USB_STOR_TRANSPORT_ERROR;
94 us->MS_Status = *(PMS_STATUS)&buf[0];
96 if (us->MS_Status.Insert && us->MS_Status.Ready) {
97 printk(KERN_INFO "Insert = %x\n", us->MS_Status.Insert);
98 printk(KERN_INFO "Ready = %x\n", us->MS_Status.Ready);
99 printk(KERN_INFO "IsMSPro = %x\n", us->MS_Status.IsMSPro);
100 printk(KERN_INFO "IsMSPHG = %x\n", us->MS_Status.IsMSPHG);
101 printk(KERN_INFO "WtP = %x\n", us->MS_Status.WtP);
102 if (us->MS_Status.IsMSPro) {
103 MSP_BlockSize = (buf[6] << 8) | buf[7];
104 MSP_UserAreaBlocks = (buf[10] << 8) | buf[11];
105 us->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
106 } else {
107 MS_CardInit(us);
109 printk(KERN_INFO "MS Init Code OK !!\n");
110 } else {
111 printk(KERN_INFO "MS Card Not Ready --- %x\n", buf[0]);
112 return USB_STOR_TRANSPORT_ERROR;
115 return USB_STOR_TRANSPORT_GOOD;
119 *ENE_SMInit()
121 int ENE_SMInit(struct us_data *us)
123 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
124 int result;
125 BYTE buf[0x200];
127 printk(KERN_INFO "transport --- ENE_SMInit\n");
129 result = ENE_LoadBinCode(us, SM_INIT_PATTERN);
130 if (result != USB_STOR_XFER_GOOD) {
131 printk(KERN_INFO "Load SM Init Code Fail !!\n");
132 return USB_STOR_TRANSPORT_ERROR;
135 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
136 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
137 bcb->DataTransferLength = 0x200;
138 bcb->Flags = 0x80;
139 bcb->CDB[0] = 0xF1;
140 bcb->CDB[1] = 0x01;
142 result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
143 if (result != USB_STOR_XFER_GOOD) {
144 printk(KERN_ERR
145 "Execution SM Init Code Fail !! result = %x\n", result);
146 return USB_STOR_TRANSPORT_ERROR;
149 us->SM_Status = *(PSM_STATUS)&buf[0];
151 us->SM_DeviceID = buf[1];
152 us->SM_CardID = buf[2];
154 if (us->SM_Status.Insert && us->SM_Status.Ready) {
155 printk(KERN_INFO "Insert = %x\n", us->SM_Status.Insert);
156 printk(KERN_INFO "Ready = %x\n", us->SM_Status.Ready);
157 printk(KERN_INFO "WtP = %x\n", us->SM_Status.WtP);
158 printk(KERN_INFO "DeviceID = %x\n", us->SM_DeviceID);
159 printk(KERN_INFO "CardID = %x\n", us->SM_CardID);
160 MediaChange = 1;
161 Check_D_MediaFmt(us);
162 } else {
163 printk(KERN_ERR "SM Card Not Ready --- %x\n", buf[0]);
164 return USB_STOR_TRANSPORT_ERROR;
167 return USB_STOR_TRANSPORT_GOOD;
171 * ENE_LoadBinCode()
173 int ENE_LoadBinCode(struct us_data *us, BYTE flag)
175 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
176 int result;
177 /* void *buf; */
178 PBYTE buf;
180 /* printk(KERN_INFO "transport --- ENE_LoadBinCode\n"); */
181 if (us->BIN_FLAG == flag)
182 return USB_STOR_TRANSPORT_GOOD;
184 buf = kmalloc(0x800, GFP_KERNEL);
185 if (buf == NULL)
186 return USB_STOR_TRANSPORT_ERROR;
187 switch (flag) {
188 /* For MS */
189 case MS_INIT_PATTERN:
190 printk(KERN_INFO "MS_INIT_PATTERN\n");
191 memcpy(buf, MS_Init, 0x800);
192 break;
193 case MSP_RW_PATTERN:
194 printk(KERN_INFO "MSP_RW_PATTERN\n");
195 memcpy(buf, MSP_Rdwr, 0x800);
196 break;
197 case MS_RW_PATTERN:
198 printk(KERN_INFO "MS_RW_PATTERN\n");
199 memcpy(buf, MS_Rdwr, 0x800);
200 break;
201 /* For SS */
202 case SM_INIT_PATTERN:
203 printk(KERN_INFO "SM_INIT_PATTERN\n");
204 memcpy(buf, SM_Init, 0x800);
205 break;
206 case SM_RW_PATTERN:
207 printk(KERN_INFO "SM_RW_PATTERN\n");
208 memcpy(buf, SM_Rdwr, 0x800);
209 break;
212 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
213 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
214 bcb->DataTransferLength = 0x800;
215 bcb->Flags = 0x00;
216 bcb->CDB[0] = 0xEF;
218 result = ENE_SendScsiCmd(us, FDIR_WRITE, buf, 0);
220 kfree(buf);
221 us->BIN_FLAG = flag;
222 return result;
226 * ENE_SendScsiCmd():
228 int ENE_SendScsiCmd(struct us_data *us, BYTE fDir, void *buf, int use_sg)
230 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
231 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
233 int result;
234 unsigned int transfer_length = bcb->DataTransferLength,
235 cswlen = 0, partial = 0;
236 unsigned int residue;
238 /* printk(KERN_INFO "transport --- ENE_SendScsiCmd\n"); */
239 /* send cmd to out endpoint */
240 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
241 bcb, US_BULK_CB_WRAP_LEN, NULL);
242 if (result != USB_STOR_XFER_GOOD) {
243 printk(KERN_ERR "send cmd to out endpoint fail ---\n");
244 return USB_STOR_TRANSPORT_ERROR;
247 if (buf) {
248 unsigned int pipe = fDir;
250 if (fDir == FDIR_READ)
251 pipe = us->recv_bulk_pipe;
252 else
253 pipe = us->send_bulk_pipe;
255 /* Bulk */
256 if (use_sg)
257 result = usb_stor_bulk_srb(us, pipe, us->srb);
258 else
259 result = usb_stor_bulk_transfer_sg(us, pipe, buf,
260 transfer_length, 0, &partial);
261 if (result != USB_STOR_XFER_GOOD) {
262 printk(KERN_ERR "data transfer fail ---\n");
263 return USB_STOR_TRANSPORT_ERROR;
267 /* Get CSW for device status */
268 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
269 US_BULK_CS_WRAP_LEN, &cswlen);
271 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
272 printk(KERN_WARNING "Received 0-length CSW; retrying...\n");
273 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
274 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
277 if (result == USB_STOR_XFER_STALLED) {
278 /* get the status again */
279 printk(KERN_WARNING "Attempting to get CSW (2nd try)...\n");
280 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
281 bcs, US_BULK_CS_WRAP_LEN, NULL);
284 if (result != USB_STOR_XFER_GOOD)
285 return USB_STOR_TRANSPORT_ERROR;
287 /* check bulk status */
288 residue = le32_to_cpu(bcs->Residue);
291 * try to compute the actual residue, based on how much data
292 * was really transferred and what the device tells us
294 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
295 residue = min(residue, transfer_length);
296 if (us->srb)
297 scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
298 (int) residue));
301 if (bcs->Status != US_BULK_STAT_OK)
302 return USB_STOR_TRANSPORT_ERROR;
304 return USB_STOR_TRANSPORT_GOOD;
308 * ENE_Read_Data()
310 int ENE_Read_Data(struct us_data *us, void *buf, unsigned int length)
312 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
313 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
314 int result;
316 /* printk(KERN_INFO "transport --- ENE_Read_Data\n"); */
317 /* set up the command wrapper */
318 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
319 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
320 bcb->DataTransferLength = length;
321 bcb->Flags = 0x80;
322 bcb->CDB[0] = 0xED;
323 bcb->CDB[2] = 0xFF;
324 bcb->CDB[3] = 0x81;
326 /* send cmd to out endpoint */
327 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
328 US_BULK_CB_WRAP_LEN, NULL);
329 if (result != USB_STOR_XFER_GOOD)
330 return USB_STOR_TRANSPORT_ERROR;
332 /* R/W data */
333 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
334 buf, length, NULL);
335 if (result != USB_STOR_XFER_GOOD)
336 return USB_STOR_TRANSPORT_ERROR;
338 /* Get CSW for device status */
339 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
340 US_BULK_CS_WRAP_LEN, NULL);
341 if (result != USB_STOR_XFER_GOOD)
342 return USB_STOR_TRANSPORT_ERROR;
343 if (bcs->Status != US_BULK_STAT_OK)
344 return USB_STOR_TRANSPORT_ERROR;
346 return USB_STOR_TRANSPORT_GOOD;
350 * ENE_Write_Data():
352 int ENE_Write_Data(struct us_data *us, void *buf, unsigned int length)
354 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
355 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
356 int result;
358 /* printk("transport --- ENE_Write_Data\n"); */
359 /* set up the command wrapper */
360 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
361 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
362 bcb->DataTransferLength = length;
363 bcb->Flags = 0x00;
364 bcb->CDB[0] = 0xEE;
365 bcb->CDB[2] = 0xFF;
366 bcb->CDB[3] = 0x81;
368 /* send cmd to out endpoint */
369 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
370 US_BULK_CB_WRAP_LEN, NULL);
371 if (result != USB_STOR_XFER_GOOD)
372 return USB_STOR_TRANSPORT_ERROR;
374 /* R/W data */
375 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
376 buf, length, NULL);
377 if (result != USB_STOR_XFER_GOOD)
378 return USB_STOR_TRANSPORT_ERROR;
380 /* Get CSW for device status */
381 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
382 US_BULK_CS_WRAP_LEN, NULL);
383 if (result != USB_STOR_XFER_GOOD)
384 return USB_STOR_TRANSPORT_ERROR;
385 if (bcs->Status != US_BULK_STAT_OK)
386 return USB_STOR_TRANSPORT_ERROR;
388 return USB_STOR_TRANSPORT_GOOD;
392 * usb_stor_print_cmd():
394 void usb_stor_print_cmd(struct scsi_cmnd *srb)
396 PBYTE Cdb = srb->cmnd;
397 DWORD cmd = Cdb[0];
398 DWORD bn = ((Cdb[2] << 24) & 0xff000000) |
399 ((Cdb[3] << 16) & 0x00ff0000) |
400 ((Cdb[4] << 8) & 0x0000ff00) |
401 ((Cdb[5] << 0) & 0x000000ff);
402 WORD blen = ((Cdb[7] << 8) & 0xff00) | ((Cdb[8] << 0) & 0x00ff);
404 switch (cmd) {
405 case TEST_UNIT_READY:
406 /* printk(KERN_INFO
407 "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
408 break;
409 case INQUIRY:
410 printk(KERN_INFO "scsi cmd %X --- SCSIOP_INQUIRY\n", cmd);
411 break;
412 case MODE_SENSE:
413 printk(KERN_INFO "scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd);
414 break;
415 case START_STOP:
416 printk(KERN_INFO "scsi cmd %X --- SCSIOP_START_STOP\n", cmd);
417 break;
418 case READ_CAPACITY:
419 printk(KERN_INFO "scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd);
420 break;
421 case READ_10:
422 /* printk(KERN_INFO
423 "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
424 ,cmd, bn, blen); */
425 break;
426 case WRITE_10:
427 /* printk(KERN_INFO
428 "scsi cmd %X --- SCSIOP_WRITE,
429 bn = %X, blen = %X\n" , cmd, bn, blen); */
430 break;
431 case ALLOW_MEDIUM_REMOVAL:
432 printk(KERN_INFO
433 "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd);
434 break;
435 default:
436 printk(KERN_INFO "scsi cmd %X --- Other cmd\n", cmd);
437 break;
439 bn = 0;
440 blen = 0;