staging:iio:dac:ad5791: Allow asymmetrical reference voltages
[zen-stable.git] / drivers / staging / bcm / InterfaceDld.c
blobbf6cc6a414bf70513049007e761c8526120186e2
1 #include "headers.h"
3 int InterfaceFileDownload(PVOID arg, struct file *flp, unsigned int on_chip_loc)
5 /*unsigned int reg = 0;*/
6 mm_segment_t oldfs = {0};
7 int errno = 0, len = 0; /*,is_config_file = 0*/
8 loff_t pos = 0;
9 PS_INTERFACE_ADAPTER psIntfAdapter = (PS_INTERFACE_ADAPTER)arg;
10 /*PMINI_ADAPTER Adapter = psIntfAdapter->psAdapter;*/
11 char *buff = kmalloc(MAX_TRANSFER_CTRL_BYTE_USB, GFP_KERNEL);
13 if (!buff)
14 return -ENOMEM;
16 while (1) {
17 oldfs = get_fs();
18 set_fs(get_ds());
19 len = vfs_read(flp, (void __force __user *)buff,
20 MAX_TRANSFER_CTRL_BYTE_USB, &pos);
21 set_fs(oldfs);
22 if (len <= 0) {
23 if (len < 0) {
24 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,
25 DBG_TYPE_INITEXIT, MP_INIT,
26 DBG_LVL_ALL, "len < 0");
27 errno = len;
28 } else {
29 errno = 0;
30 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,
31 DBG_TYPE_INITEXIT, MP_INIT,
32 DBG_LVL_ALL,
33 "Got end of file!");
35 break;
37 /* BCM_DEBUG_PRINT_BUFFER(Adapter,DBG_TYPE_INITEXIT, MP_INIT,
38 DBG_LVL_ALL, buff,
39 MAX_TRANSFER_CTRL_BYTE_USB);*/
40 errno = InterfaceWRM(psIntfAdapter, on_chip_loc, buff, len);
41 if (errno) {
42 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,
43 DBG_TYPE_PRINTK, 0, 0,
44 "WRM Failed! status: %d", errno);
45 break;
47 on_chip_loc += MAX_TRANSFER_CTRL_BYTE_USB;
50 kfree(buff);
51 return errno;
54 int InterfaceFileReadbackFromChip(PVOID arg, struct file *flp,
55 unsigned int on_chip_loc)
57 char *buff, *buff_readback;
58 unsigned int reg = 0;
59 mm_segment_t oldfs={0};
60 int errno=0, len=0, is_config_file = 0;
61 loff_t pos=0;
62 static int fw_down = 0;
63 INT Status = STATUS_SUCCESS;
64 PS_INTERFACE_ADAPTER psIntfAdapter = (PS_INTERFACE_ADAPTER)arg;
66 buff=kmalloc(MAX_TRANSFER_CTRL_BYTE_USB, GFP_DMA);
67 buff_readback=kmalloc(MAX_TRANSFER_CTRL_BYTE_USB , GFP_DMA);
68 if(!buff || !buff_readback)
70 kfree(buff);
71 kfree(buff_readback);
73 return -ENOMEM;
76 is_config_file = (on_chip_loc == CONFIG_BEGIN_ADDR)? 1:0;
78 memset(buff_readback, 0, MAX_TRANSFER_CTRL_BYTE_USB);
79 memset(buff, 0, MAX_TRANSFER_CTRL_BYTE_USB);
80 while(1)
82 oldfs=get_fs(); set_fs(get_ds());
83 len=vfs_read(flp, (void __force __user *)buff, MAX_TRANSFER_CTRL_BYTE_USB, &pos);
84 set_fs(oldfs);
85 fw_down++;
86 if(len<=0)
88 if(len<0)
90 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "len < 0");
91 errno=len;
93 else
95 errno = 0;
96 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "Got end of file!");
98 break;
102 Status = InterfaceRDM(psIntfAdapter, on_chip_loc, buff_readback, len);
103 if(Status)
105 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "RDM of len %d Failed! %d", len, reg);
106 goto exit;
108 reg++;
109 if((len-sizeof(unsigned int))<4)
111 if(memcmp(buff_readback, buff, len))
113 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "Firmware Download is not proper %d", fw_down);
114 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, MP_INIT,DBG_LVL_ALL,"Length is: %d",len);
115 Status = -EIO;
116 goto exit;
119 else
121 len-=4;
122 while(len)
124 if(*(unsigned int*)&buff_readback[len] != *(unsigned int *)&buff[len])
126 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "Firmware Download is not proper %d", fw_down);
127 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "Val from Binary %x, Val From Read Back %x ", *(unsigned int *)&buff[len], *(unsigned int*)&buff_readback[len]);
128 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "len =%x!!!", len);
129 Status = -EIO;
130 goto exit;
132 len-=4;
135 on_chip_loc+=MAX_TRANSFER_CTRL_BYTE_USB;
136 }/* End of while(1)*/
137 exit:
138 kfree(buff);
139 kfree(buff_readback);
140 return Status;
143 static int bcm_download_config_file(PMINI_ADAPTER Adapter,
144 FIRMWARE_INFO *psFwInfo)
146 int retval = STATUS_SUCCESS;
147 B_UINT32 value = 0;
149 if(Adapter->pstargetparams == NULL)
151 if((Adapter->pstargetparams =
152 kmalloc(sizeof(STARGETPARAMS), GFP_KERNEL)) == NULL)
154 return -ENOMEM;
157 if(psFwInfo->u32FirmwareLength != sizeof(STARGETPARAMS))
159 return -EIO;
161 retval = copy_from_user(Adapter->pstargetparams,
162 psFwInfo->pvMappedFirmwareAddress, psFwInfo->u32FirmwareLength);
163 if(retval)
165 kfree(Adapter->pstargetparams);
166 Adapter->pstargetparams = NULL;
167 return -EFAULT;
169 /* Parse the structure and then Download the Firmware */
170 beceem_parse_target_struct(Adapter);
172 //Initializing the NVM.
173 BcmInitNVM(Adapter);
175 retval = InitLedSettings (Adapter);
177 if(retval)
179 BCM_DEBUG_PRINT (Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "INIT LED Failed\n");
180 return retval;
183 if(Adapter->LEDInfo.led_thread_running & BCM_LED_THREAD_RUNNING_ACTIVELY)
185 Adapter->LEDInfo.bLedInitDone = FALSE;
186 Adapter->DriverState = DRIVER_INIT;
187 wake_up(&Adapter->LEDInfo.notify_led_event);
190 if(Adapter->LEDInfo.led_thread_running & BCM_LED_THREAD_RUNNING_ACTIVELY)
192 Adapter->DriverState = FW_DOWNLOAD;
193 wake_up(&Adapter->LEDInfo.notify_led_event);
196 /* Initialize the DDR Controller */
197 retval = ddr_init(Adapter);
198 if(retval)
200 BCM_DEBUG_PRINT (Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "DDR Init Failed\n");
201 return retval;
204 value = 0;
205 wrmalt(Adapter, EEPROM_CAL_DATA_INTERNAL_LOC - 4, &value, sizeof(value));
206 wrmalt(Adapter, EEPROM_CAL_DATA_INTERNAL_LOC - 8, &value, sizeof(value));
208 if(Adapter->eNVMType == NVM_FLASH)
210 retval = PropagateCalParamsFromFlashToMemory(Adapter);
211 if(retval)
213 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL,"propagaion of cal param failed with status :%d", retval);
214 return retval;
219 retval =buffDnldVerify(Adapter,(PUCHAR)Adapter->pstargetparams,sizeof(STARGETPARAMS),CONFIG_BEGIN_ADDR);
221 if(retval)
223 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "configuration file not downloaded properly");
225 else
226 Adapter->bCfgDownloaded = TRUE;
229 return retval;
231 static int bcm_compare_buff_contents(unsigned char *readbackbuff,
232 unsigned char *buff,unsigned int len)
234 int retval = STATUS_SUCCESS;
235 PMINI_ADAPTER Adapter = GET_BCM_ADAPTER(gblpnetdev);
236 if((len-sizeof(unsigned int))<4)
238 if(memcmp(readbackbuff , buff, len))
240 retval=-EINVAL;
243 else
245 len-=4;
246 while(len)
248 if(*(unsigned int*)&readbackbuff[len] !=
249 *(unsigned int *)&buff[len])
251 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "Firmware Download is not proper");
252 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "Val from Binary %x, Val From Read Back %x ", *(unsigned int *)&buff[len], *(unsigned int*)&readbackbuff[len]);
253 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "len =%x!!!", len);
254 retval=-EINVAL;
255 break;
257 len-=4;
260 return retval;
262 int bcm_ioctl_fw_download(PMINI_ADAPTER Adapter, FIRMWARE_INFO *psFwInfo)
264 int retval = STATUS_SUCCESS;
265 PUCHAR buff = NULL;
267 /* Config File is needed for the Driver to download the Config file and
268 Firmware. Check for the Config file to be first to be sent from the
269 Application
271 atomic_set (&Adapter->uiMBupdate, FALSE);
272 if(!Adapter->bCfgDownloaded &&
273 psFwInfo->u32StartingAddress != CONFIG_BEGIN_ADDR)
275 /*Can't Download Firmware.*/
276 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL,"Download the config File first\n");
277 return -EINVAL;
280 /* If Config File, Finish the DDR Settings and then Download CFG File */
281 if(psFwInfo->u32StartingAddress == CONFIG_BEGIN_ADDR)
283 retval = bcm_download_config_file (Adapter, psFwInfo);
285 else
288 buff = kzalloc(psFwInfo->u32FirmwareLength,GFP_KERNEL);
289 if(buff==NULL)
291 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL,"Failed in allocation memory");
292 return -ENOMEM;
294 retval = copy_from_user(buff,psFwInfo->pvMappedFirmwareAddress, psFwInfo->u32FirmwareLength);
295 if(retval != STATUS_SUCCESS)
297 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "copying buffer from user space failed");
298 retval = -EFAULT;
299 goto error ;
302 retval = buffDnldVerify(Adapter,
303 buff,
304 psFwInfo->u32FirmwareLength,
305 psFwInfo->u32StartingAddress);
306 if(retval != STATUS_SUCCESS)
308 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL,"f/w download failed status :%d", retval);
309 goto error;
312 error:
313 kfree(buff);
314 return retval;
317 static INT buffDnld(PMINI_ADAPTER Adapter, PUCHAR mappedbuffer, UINT u32FirmwareLength,
318 ULONG u32StartingAddress)
321 unsigned int len = 0;
322 int retval = STATUS_SUCCESS;
323 len = u32FirmwareLength;
325 while(u32FirmwareLength)
327 len = MIN_VAL (u32FirmwareLength, MAX_TRANSFER_CTRL_BYTE_USB);
328 retval = wrm (Adapter, u32StartingAddress, mappedbuffer, len);
329 if(retval)
331 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "wrm failed with status :%d", retval);
332 break;
334 u32StartingAddress += len;
335 u32FirmwareLength -= len;
336 mappedbuffer +=len;
338 return retval;
342 static INT buffRdbkVerify(PMINI_ADAPTER Adapter,
343 PUCHAR mappedbuffer, UINT u32FirmwareLength,
344 ULONG u32StartingAddress)
346 UINT len = u32FirmwareLength;
347 INT retval = STATUS_SUCCESS;
348 PUCHAR readbackbuff = kzalloc(MAX_TRANSFER_CTRL_BYTE_USB,GFP_KERNEL);
350 if(NULL == readbackbuff)
352 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "MEMORY ALLOCATION FAILED");
353 return -ENOMEM;
355 while (u32FirmwareLength && !retval)
358 len = MIN_VAL (u32FirmwareLength, MAX_TRANSFER_CTRL_BYTE_USB);
360 retval = rdm (Adapter, u32StartingAddress, readbackbuff, len);
361 if(retval)
363 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "rdm failed with status %d" ,retval);
364 break;
367 if (STATUS_SUCCESS != (retval = bcm_compare_buff_contents (readbackbuff, mappedbuffer, len)))
369 break;
371 u32StartingAddress += len;
372 u32FirmwareLength -= len;
373 mappedbuffer +=len;
374 }/* end of while (u32FirmwareLength && !retval) */
375 kfree(readbackbuff);
376 return retval;
379 INT buffDnldVerify(PMINI_ADAPTER Adapter, unsigned char *mappedbuffer, unsigned int u32FirmwareLength,
380 unsigned long u32StartingAddress)
382 INT status = STATUS_SUCCESS;
384 status = buffDnld(Adapter,mappedbuffer,u32FirmwareLength,u32StartingAddress);
385 if(status != STATUS_SUCCESS)
387 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL,"Buffer download failed");
388 goto error;
391 status= buffRdbkVerify(Adapter,mappedbuffer,u32FirmwareLength,u32StartingAddress);
392 if(status != STATUS_SUCCESS)
394 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL,"Buffer readback verifier failed");
395 goto error;
397 error:
398 return status;