Merge branch 'usb-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-kernel.git] / drivers / staging / keucr / sdscsi.c
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
13 int SD_SCSI_Test_Unit_Ready (struct us_data *us, struct scsi_cmnd *srb);
14 int SD_SCSI_Inquiry         (struct us_data *us, struct scsi_cmnd *srb);
15 int SD_SCSI_Mode_Sense      (struct us_data *us, struct scsi_cmnd *srb);
16 int SD_SCSI_Start_Stop      (struct us_data *us, struct scsi_cmnd *srb);
17 int SD_SCSI_Read_Capacity   (struct us_data *us, struct scsi_cmnd *srb);
18 int SD_SCSI_Read            (struct us_data *us, struct scsi_cmnd *srb);
19 int SD_SCSI_Write           (struct us_data *us, struct scsi_cmnd *srb);
20
21 //----- SD_SCSIIrp() --------------------------------------------------
22 int SD_SCSIIrp(struct us_data *us, struct scsi_cmnd *srb)
23 {
24         int    result;
25
26         us->SrbStatus = SS_SUCCESS;
27         switch (srb->cmnd[0])
28         {
29                 case TEST_UNIT_READY :  result = SD_SCSI_Test_Unit_Ready (us, srb);  break; //0x00
30                 case INQUIRY         :  result = SD_SCSI_Inquiry         (us, srb);  break; //0x12
31                 case MODE_SENSE      :  result = SD_SCSI_Mode_Sense      (us, srb);  break; //0x1A
32 //              case START_STOP      :  result = SD_SCSI_Start_Stop      (us, srb);  break; //0x1B
33                 case READ_CAPACITY   :  result = SD_SCSI_Read_Capacity   (us, srb);  break; //0x25
34                 case READ_10         :  result = SD_SCSI_Read            (us, srb);  break; //0x28
35                 case WRITE_10        :  result = SD_SCSI_Write           (us, srb);  break; //0x2A
36
37                 default:
38                         us->SrbStatus = SS_ILLEGAL_REQUEST;
39                         result = USB_STOR_TRANSPORT_FAILED;
40                         break;
41         }
42         return result;
43 }
44
45 //----- SD_SCSI_Test_Unit_Ready() --------------------------------------------------
46 int SD_SCSI_Test_Unit_Ready(struct us_data *us, struct scsi_cmnd *srb)
47 {
48         //printk("SD_SCSI_Test_Unit_Ready\n");
49         if (us->SD_Status.Insert && us->SD_Status.Ready)
50                 return USB_STOR_TRANSPORT_GOOD;
51         else
52         {
53                 ENE_SDInit(us);
54                 return USB_STOR_TRANSPORT_GOOD;
55         }
56                 
57         return USB_STOR_TRANSPORT_GOOD;
58 }
59
60 //----- SD_SCSI_Inquiry() --------------------------------------------------
61 int SD_SCSI_Inquiry(struct us_data *us, struct scsi_cmnd *srb)
62 {
63         //printk("SD_SCSI_Inquiry\n");
64         BYTE data_ptr[36] = {0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55, 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61, 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30};
65
66         usb_stor_set_xfer_buf(us, data_ptr, 36, srb, TO_XFER_BUF);
67         return USB_STOR_TRANSPORT_GOOD;
68 }
69
70
71 //----- SD_SCSI_Mode_Sense() --------------------------------------------------
72 int SD_SCSI_Mode_Sense(struct us_data *us, struct scsi_cmnd *srb)
73 {
74         BYTE    mediaNoWP[12] = {0x0b,0x00,0x00,0x08,0x00,0x00,0x71,0xc0,0x00,0x00,0x02,0x00};
75         BYTE    mediaWP[12]   = {0x0b,0x00,0x80,0x08,0x00,0x00,0x71,0xc0,0x00,0x00,0x02,0x00};
76
77         if (us->SD_Status.WtP)
78                 usb_stor_set_xfer_buf(us, mediaWP, 12, srb, TO_XFER_BUF);
79         else
80                 usb_stor_set_xfer_buf(us, mediaNoWP, 12, srb, TO_XFER_BUF);
81
82
83         return USB_STOR_TRANSPORT_GOOD;
84 }
85
86 //----- SD_SCSI_Read_Capacity() --------------------------------------------------
87 int SD_SCSI_Read_Capacity(struct us_data *us, struct scsi_cmnd *srb)
88 {
89         unsigned int offset = 0;
90         struct scatterlist *sg = NULL;
91         DWORD   bl_num;
92         WORD    bl_len;
93         BYTE    buf[8];
94
95         printk("SD_SCSI_Read_Capacity\n");
96         if ( us->SD_Status.HiCapacity )
97         {
98                 bl_len = 0x200;
99                 if (us->SD_Status.IsMMC)
100                         bl_num = us->HC_C_SIZE-1;
101                 else
102                         bl_num = (us->HC_C_SIZE + 1) * 1024 - 1;
103         }
104         else
105         {
106                 bl_len = 1<<(us->SD_READ_BL_LEN);
107                 bl_num = us->SD_Block_Mult*(us->SD_C_SIZE+1)*(1<<(us->SD_C_SIZE_MULT+2)) - 1;
108         }
109         us->bl_num = bl_num;
110         printk("bl_len = %x\n", bl_len);
111         printk("bl_num = %x\n", bl_num);
112
113         //srb->request_bufflen = 8;
114         buf[0] = (bl_num>>24) & 0xff;
115         buf[1] = (bl_num>>16) & 0xff;
116         buf[2] = (bl_num>> 8) & 0xff;
117         buf[3] = (bl_num>> 0) & 0xff;
118         buf[4] = (bl_len>>24) & 0xff;
119         buf[5] = (bl_len>>16) & 0xff;
120         buf[6] = (bl_len>> 8) & 0xff;
121         buf[7] = (bl_len>> 0) & 0xff;
122         
123         usb_stor_access_xfer_buf(us, buf, 8, srb, &sg, &offset, TO_XFER_BUF);
124         //usb_stor_set_xfer_buf(us, buf, srb->request_bufflen, srb, TO_XFER_BUF);
125
126         return USB_STOR_TRANSPORT_GOOD;
127 }
128
129 //----- SD_SCSI_Read() --------------------------------------------------
130 int SD_SCSI_Read(struct us_data *us, struct scsi_cmnd *srb)
131 {
132         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
133         int result;
134         PBYTE   Cdb = srb->cmnd;
135         DWORD bn  =  ((Cdb[2]<<24) & 0xff000000) | ((Cdb[3]<<16) & 0x00ff0000) |
136                    ((Cdb[4]<< 8) & 0x0000ff00) | ((Cdb[5]<< 0) & 0x000000ff);
137         WORD  blen = ((Cdb[7]<< 8) & 0xff00)     | ((Cdb[8]<< 0) & 0x00ff);
138         DWORD bnByte = bn * 0x200;
139         DWORD   blenByte = blen * 0x200;
140
141         if (bn > us->bl_num)
142                 return USB_STOR_TRANSPORT_ERROR;
143
144         result = ENE_LoadBinCode(us, SD_RW_PATTERN);
145         if (result != USB_STOR_XFER_GOOD)
146         {
147                 printk("Load SD RW pattern Fail !!\n");
148                 return USB_STOR_TRANSPORT_ERROR;
149         }
150
151         if ( us->SD_Status.HiCapacity )
152                 bnByte = bn;
153                 
154         // set up the command wrapper
155         memset(bcb, 0, sizeof(bcb));
156         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
157         bcb->DataTransferLength = blenByte;
158         bcb->Flags  = 0x80;
159         bcb->CDB[0] = 0xF1;
160         bcb->CDB[5] = (BYTE)(bnByte);
161         bcb->CDB[4] = (BYTE)(bnByte>>8);
162         bcb->CDB[3] = (BYTE)(bnByte>>16);
163         bcb->CDB[2] = (BYTE)(bnByte>>24);
164
165         result = ENE_SendScsiCmd(us, FDIR_READ, scsi_sglist(srb), 1);
166         return result;
167 }
168
169 //----- SD_SCSI_Write() --------------------------------------------------
170 int SD_SCSI_Write(struct us_data *us, struct scsi_cmnd *srb)
171 {
172         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
173         int result;
174         PBYTE   Cdb = srb->cmnd;
175         DWORD bn  =  ((Cdb[2]<<24) & 0xff000000) | ((Cdb[3]<<16) & 0x00ff0000) |
176                    ((Cdb[4]<< 8) & 0x0000ff00) | ((Cdb[5]<< 0) & 0x000000ff);
177         WORD  blen = ((Cdb[7]<< 8) & 0xff00)     | ((Cdb[8]<< 0) & 0x00ff);
178         DWORD bnByte = bn * 0x200;
179         DWORD   blenByte = blen * 0x200;
180
181         if (bn > us->bl_num)
182                 return USB_STOR_TRANSPORT_ERROR;
183
184         result = ENE_LoadBinCode(us, SD_RW_PATTERN);
185         if (result != USB_STOR_XFER_GOOD)
186         {
187                 printk("Load SD RW pattern Fail !!\n");
188                 return USB_STOR_TRANSPORT_ERROR;
189         }
190
191         if ( us->SD_Status.HiCapacity )
192                 bnByte = bn;
193
194         // set up the command wrapper
195         memset(bcb, 0, sizeof(bcb));
196         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
197         bcb->DataTransferLength = blenByte;
198         bcb->Flags  = 0x00;
199         bcb->CDB[0] = 0xF0;
200         bcb->CDB[5] = (BYTE)(bnByte);
201         bcb->CDB[4] = (BYTE)(bnByte>>8);
202         bcb->CDB[3] = (BYTE)(bnByte>>16);
203         bcb->CDB[2] = (BYTE)(bnByte>>24);
204
205         result = ENE_SendScsiCmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
206         return result;
207 }
208
209
210