staging: speakup: avoid out-of-range access in synth_add()
[pandora-kernel.git] / drivers / staging / keucr / init.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 #include "init.h"
13
14 /*
15  * ENE_InitMedia():
16  */
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         }
39         return result;
40 }
41
42 /*
43  * ENE_Read_BYTE() :
44  */
45 int 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
50         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
51         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
62 /*
63  *ENE_SMInit()
64  */
65 int 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
71         printk(KERN_INFO "transport --- ENE_SMInit\n");
72
73         result = ENE_LoadBinCode(us, SM_INIT_PATTERN);
74         if (result != USB_STOR_XFER_GOOD) {
75                 printk(KERN_INFO "Load SM Init Code Fail !!\n");
76                 return USB_STOR_TRANSPORT_ERROR;
77         }
78
79         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
80         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);
87         if (result != USB_STOR_XFER_GOOD) {
88                 printk(KERN_ERR
89                        "Execution SM Init Code Fail !! result = %x\n", result);
90                 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
98         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);
104                 MediaChange = 1;
105                 Check_D_MediaFmt(us);
106         } else {
107                 printk(KERN_ERR "SM Card Not Ready --- %x\n", buf[0]);
108                 return USB_STOR_TRANSPORT_ERROR;
109         }
110
111         return USB_STOR_TRANSPORT_GOOD;
112 }
113
114 /*
115  * ENE_LoadBinCode()
116  */
117 int ENE_LoadBinCode(struct us_data *us, BYTE flag)
118 {
119         struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
120         int result;
121         /* void *buf; */
122         PBYTE buf;
123
124         /* printk(KERN_INFO "transport --- ENE_LoadBinCode\n"); */
125         if (us->BIN_FLAG == flag)
126                 return USB_STOR_TRANSPORT_GOOD;
127
128         buf = kmalloc(0x800, GFP_KERNEL);
129         if (buf == NULL)
130                 return USB_STOR_TRANSPORT_ERROR;
131         switch (flag) {
132         /* For SS */
133         case SM_INIT_PATTERN:
134                 printk(KERN_INFO "SM_INIT_PATTERN\n");
135                 memcpy(buf, SM_Init, 0x800);
136                 break;
137         case SM_RW_PATTERN:
138                 printk(KERN_INFO "SM_RW_PATTERN\n");
139                 memcpy(buf, SM_Rdwr, 0x800);
140                 break;
141         }
142
143         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
144         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
145         bcb->DataTransferLength = 0x800;
146         bcb->Flags = 0x00;
147         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
156 /*
157  * ENE_SendScsiCmd():
158  */
159 int 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;
165         unsigned int transfer_length = bcb->DataTransferLength,
166                      cswlen = 0, partial = 0;
167         unsigned int residue;
168
169         /* 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");
175                 return USB_STOR_TRANSPORT_ERROR;
176         }
177
178         if (buf) {
179                 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
186                 /* Bulk */
187                 if (use_sg)
188                         result = usb_stor_bulk_srb(us, pipe, us->srb);
189                 else
190                         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");
194                         return USB_STOR_TRANSPORT_ERROR;
195                 }
196         }
197
198         /* 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);
201
202         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);
206         }
207
208         if (result == USB_STOR_XFER_STALLED) {
209                 /* get the status again */
210                 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);
213         }
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
221         /*
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)) {
226                 residue = min(residue, transfer_length);
227                 if (us->srb)
228                         scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
229                                         (int) residue));
230         }
231
232         if (bcs->Status != US_BULK_STAT_OK)
233                 return USB_STOR_TRANSPORT_ERROR;
234
235         return USB_STOR_TRANSPORT_GOOD;
236 }
237
238 /*
239  * ENE_Read_Data()
240  */
241 int 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
247         /* printk(KERN_INFO "transport --- ENE_Read_Data\n"); */
248         /* set up the command wrapper */
249         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
250         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
251         bcb->DataTransferLength = length;
252         bcb->Flags = 0x80;
253         bcb->CDB[0] = 0xED;
254         bcb->CDB[2] = 0xFF;
255         bcb->CDB[3] = 0x81;
256
257         /* 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);
260         if (result != USB_STOR_XFER_GOOD)
261                 return USB_STOR_TRANSPORT_ERROR;
262
263         /* R/W data */
264         result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
265                                                 buf, length, NULL);
266         if (result != USB_STOR_XFER_GOOD)
267                 return USB_STOR_TRANSPORT_ERROR;
268
269         /* 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);
272         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
280 /*
281  * ENE_Write_Data():
282  */
283 int 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
289         /* printk("transport --- ENE_Write_Data\n"); */
290         /* set up the command wrapper */
291         memset(bcb, 0, sizeof(struct bulk_cb_wrap));
292         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
293         bcb->DataTransferLength = length;
294         bcb->Flags = 0x00;
295         bcb->CDB[0] = 0xEE;
296         bcb->CDB[2] = 0xFF;
297         bcb->CDB[3] = 0x81;
298
299         /* 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);
302         if (result != USB_STOR_XFER_GOOD)
303                 return USB_STOR_TRANSPORT_ERROR;
304
305         /* R/W data */
306         result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
307                                                 buf, length, NULL);
308         if (result != USB_STOR_XFER_GOOD)
309                 return USB_STOR_TRANSPORT_ERROR;
310
311         /* 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);
314         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
322 /*
323  * usb_stor_print_cmd():
324  */
325 void usb_stor_print_cmd(struct scsi_cmnd *srb)
326 {
327         PBYTE   Cdb = srb->cmnd;
328         DWORD   cmd = Cdb[0];
329         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);
334
335         switch (cmd) {
336         case TEST_UNIT_READY:
337                 /* printk(KERN_INFO
338                          "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
339                 break;
340         case INQUIRY:
341                 printk(KERN_INFO "scsi cmd %X --- SCSIOP_INQUIRY\n", cmd);
342                 break;
343         case MODE_SENSE:
344                 printk(KERN_INFO "scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd);
345                 break;
346         case START_STOP:
347                 printk(KERN_INFO "scsi cmd %X --- SCSIOP_START_STOP\n", cmd);
348                 break;
349         case READ_CAPACITY:
350                 printk(KERN_INFO "scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd);
351                 break;
352         case READ_10:
353                 /*  printk(KERN_INFO
354                            "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
355                            ,cmd, bn, blen); */
356                 break;
357         case WRITE_10:
358                 /* printk(KERN_INFO
359                           "scsi cmd %X --- SCSIOP_WRITE,
360                           bn = %X, blen = %X\n" , cmd, bn, blen); */
361                 break;
362         case ALLOW_MEDIUM_REMOVAL:
363                 printk(KERN_INFO
364                         "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd);
365                 break;
366         default:
367                 printk(KERN_INFO "scsi cmd %X --- Other cmd\n", cmd);
368                 break;
369         }
370         bn = 0;
371         blen = 0;
372 }
373