Staging: bcm: Bcmchar: Fix style issues on bcm_char_release()
[firefly-linux-kernel-4.4.55.git] / drivers / staging / bcm / Bcmchar.c
1 #include <linux/fs.h>
2
3 #include "headers.h"
4 /***************************************************************
5 * Function        - bcm_char_open()
6 *
7 * Description - This is the "open" entry point for the character
8 *                               driver.
9 *
10 * Parameters  - inode: Pointer to the Inode structure of char device
11 *                               filp : File pointer of the char device
12 *
13 * Returns         - Zero(Success)
14 ****************************************************************/
15
16 static int bcm_char_open(struct inode *inode, struct file * filp)
17 {
18         PMINI_ADAPTER       Adapter = NULL;
19         PPER_TARANG_DATA    pTarang = NULL;
20
21         Adapter = GET_BCM_ADAPTER(gblpnetdev);
22         pTarang = (PPER_TARANG_DATA)kmalloc(sizeof(PER_TARANG_DATA),
23                                             GFP_KERNEL);
24         if (!pTarang)
25                 return -ENOMEM;
26
27         memset(pTarang, 0, sizeof(PER_TARANG_DATA));
28         pTarang->Adapter = Adapter;
29         pTarang->RxCntrlMsgBitMask = 0xFFFFFFFF & ~(1 << 0xB);
30
31         down(&Adapter->RxAppControlQueuelock);
32         pTarang->next = Adapter->pTarangs;
33         Adapter->pTarangs = pTarang;
34         up(&Adapter->RxAppControlQueuelock);
35
36         /* Store the Adapter structure */
37         filp->private_data = pTarang;
38
39         /*Start Queuing the control response Packets*/
40         atomic_inc(&Adapter->ApplicationRunning);
41
42         nonseekable_open(inode, filp);
43         return 0;
44 }
45
46 static int bcm_char_release(struct inode *inode, struct file *filp)
47 {
48         PPER_TARANG_DATA pTarang, tmp, ptmp;
49         PMINI_ADAPTER Adapter = NULL;
50         struct sk_buff *pkt, *npkt;
51
52         pTarang = (PPER_TARANG_DATA)filp->private_data;
53
54         if (pTarang == NULL) {
55                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0,
56                                 "ptarang is null\n");
57                 return 0;
58         }
59
60         Adapter = pTarang->Adapter;
61
62         down(&Adapter->RxAppControlQueuelock);
63
64         tmp = Adapter->pTarangs;
65         for (ptmp = NULL; tmp; ptmp = tmp, tmp = tmp->next) {
66                 if (tmp == pTarang)
67                         break;
68         }
69
70         if (tmp) {
71                 if (!ptmp)
72                         Adapter->pTarangs = tmp->next;
73                 else
74                         ptmp->next = tmp->next;
75         } else {
76                 up(&Adapter->RxAppControlQueuelock);
77                 return 0;
78         }
79
80         pkt = pTarang->RxAppControlHead;
81         while (pkt) {
82                 npkt = pkt->next;
83                 kfree_skb(pkt);
84                 pkt = npkt;
85         }
86
87         up(&Adapter->RxAppControlQueuelock);
88
89         /*Stop Queuing the control response Packets*/
90         atomic_dec(&Adapter->ApplicationRunning);
91
92         kfree(pTarang);
93
94         /* remove this filp from the asynchronously notified filp's */
95         filp->private_data = NULL;
96         return 0;
97 }
98
99 static ssize_t bcm_char_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
100 {
101         PPER_TARANG_DATA pTarang = filp->private_data;
102         PMINI_ADAPTER   Adapter = pTarang->Adapter;
103         struct sk_buff* Packet = NULL;
104         ssize_t         PktLen = 0;
105         int             wait_ret_val=0;
106
107         wait_ret_val = wait_event_interruptible(Adapter->process_read_wait_queue,
108                 (pTarang->RxAppControlHead || Adapter->device_removed));
109         if((wait_ret_val == -ERESTARTSYS))
110         {
111                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Exiting as i've been asked to exit!!!\n");
112                 return wait_ret_val;
113         }
114
115         if(Adapter->device_removed)
116         {
117                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Device Removed... Killing the Apps...\n");
118                 return -ENODEV;
119         }
120
121         if(FALSE == Adapter->fw_download_done)
122                 return -EACCES;
123
124     down( &Adapter->RxAppControlQueuelock);
125
126         if(pTarang->RxAppControlHead)
127         {
128                 Packet = pTarang->RxAppControlHead;
129                 DEQUEUEPACKET(pTarang->RxAppControlHead,pTarang->RxAppControlTail);
130                 pTarang->AppCtrlQueueLen--;
131         }
132
133     up(&Adapter->RxAppControlQueuelock);
134
135         if(Packet)
136         {
137                 PktLen = Packet->len;
138                 if(copy_to_user(buf, Packet->data, min_t(size_t, PktLen, size)))
139                 {
140                         dev_kfree_skb(Packet);
141                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "\nReturning from copy to user failure \n");
142                         return -EFAULT;
143                 }
144                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,
145                                 "Read %zd Bytes From Adapter packet = %p by process %d!\n",
146                                 PktLen, Packet, current->pid);
147                 dev_kfree_skb(Packet);
148         }
149
150     BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "<====\n");
151     return PktLen;
152 }
153
154 static long bcm_char_ioctl(struct file *filp, UINT cmd, ULONG arg)
155 {
156         PPER_TARANG_DATA  pTarang = filp->private_data;
157         void __user *argp = (void __user *)arg;
158         PMINI_ADAPTER   Adapter = pTarang->Adapter;
159         INT                     Status = STATUS_FAILURE;
160         int timeout = 0;
161         IOCTL_BUFFER    IoBuffer;
162
163         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Parameters Passed to control IOCTL cmd=0x%X arg=0x%lX", cmd, arg);
164
165         if(_IOC_TYPE(cmd) != BCM_IOCTL)
166                 return -EFAULT;
167         if(_IOC_DIR(cmd) & _IOC_READ)
168                 Status = !access_ok(VERIFY_WRITE, argp, _IOC_SIZE(cmd));
169         else if (_IOC_DIR(cmd) & _IOC_WRITE)
170             Status = !access_ok(VERIFY_READ, argp, _IOC_SIZE(cmd));
171         else if (_IOC_NONE == (_IOC_DIR(cmd) & _IOC_NONE))
172             Status = STATUS_SUCCESS;
173
174         if(Status)
175                 return -EFAULT;
176
177         if(Adapter->device_removed)
178         {
179                 return -EFAULT;
180         }
181
182         if(FALSE == Adapter->fw_download_done)
183         {
184                 switch (cmd)
185                 {
186                         case IOCTL_MAC_ADDR_REQ:
187                         case IOCTL_LINK_REQ:
188                         case IOCTL_CM_REQUEST:
189                         case IOCTL_SS_INFO_REQ:
190                         case IOCTL_SEND_CONTROL_MESSAGE:
191                         case IOCTL_IDLE_REQ:
192                         case IOCTL_BCM_GPIO_SET_REQUEST:
193                         case IOCTL_BCM_GPIO_STATUS_REQUEST:
194                                 return -EACCES;
195                         default:
196                                 break;
197                 }
198         }
199
200         Status = vendorextnIoctl(Adapter, cmd, arg);
201         if(Status != CONTINUE_COMMON_PATH )
202                  return Status;
203
204         switch(cmd){
205                 // Rdms for Swin Idle...
206                 case IOCTL_BCM_REGISTER_READ_PRIVATE:
207                 {
208                         RDM_BUFFER  sRdmBuffer = {0};
209                         PCHAR temp_buff;
210                         UINT Bufflen;
211
212                         /* Copy Ioctl Buffer structure */
213                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
214                                 return -EFAULT;
215
216                         if (IoBuffer.InputLength > sizeof(sRdmBuffer))
217                                 return -EINVAL;
218
219                         if(copy_from_user(&sRdmBuffer, IoBuffer.InputBuffer, IoBuffer.InputLength))
220                                 return -EFAULT;
221
222                         /* FIXME: need to restrict BuffLen */
223                         Bufflen = IoBuffer.OutputLength + (4 - IoBuffer.OutputLength%4)%4;
224                         temp_buff = kmalloc(Bufflen, GFP_KERNEL);
225                         if(!temp_buff)
226                                 return -ENOMEM;
227
228                         Status = rdmalt(Adapter, (UINT)sRdmBuffer.Register,
229                                         (PUINT)temp_buff, Bufflen);
230                         if(Status == STATUS_SUCCESS)
231                         {
232                                 if(copy_to_user(IoBuffer.OutputBuffer, temp_buff, IoBuffer.OutputLength))
233                                         Status = -EFAULT;
234                         }
235
236                         kfree(temp_buff);
237                         break;
238                 }
239                 case IOCTL_BCM_REGISTER_WRITE_PRIVATE:
240                 {
241                         WRM_BUFFER  sWrmBuffer = {0};
242                         UINT uiTempVar=0;
243                         /* Copy Ioctl Buffer structure */
244
245                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
246                                 return -EFAULT;
247
248                         if (IoBuffer.InputLength > sizeof(sWrmBuffer))
249                                 return -EINVAL;
250
251                         /* Get WrmBuffer structure */
252                         if(copy_from_user(&sWrmBuffer, IoBuffer.InputBuffer, IoBuffer.InputLength))
253                                 return -EFAULT;
254
255                         uiTempVar = sWrmBuffer.Register & EEPROM_REJECT_MASK;
256                         if(!((Adapter->pstargetparams->m_u32Customize) & VSG_MODE) &&
257                                 ((uiTempVar == EEPROM_REJECT_REG_1)||
258                                 (uiTempVar == EEPROM_REJECT_REG_2) ||
259                                 (uiTempVar == EEPROM_REJECT_REG_3) ||
260                                 (uiTempVar == EEPROM_REJECT_REG_4)))
261                         {
262                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "EEPROM Access Denied, not in VSG Mode\n");
263                                 return -EFAULT;
264                         }
265                         Status = wrmalt(Adapter, (UINT)sWrmBuffer.Register,
266                                                 (PUINT)sWrmBuffer.Data, sizeof(ULONG));
267                         if(Status == STATUS_SUCCESS)
268                         {
269                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"WRM Done\n");
270                         }
271                         else
272                         {
273                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "WRM Failed\n");
274                                 Status = -EFAULT;
275                         }
276                         break;
277                 }
278
279                 case IOCTL_BCM_REGISTER_READ:
280                 case IOCTL_BCM_EEPROM_REGISTER_READ:
281                 {
282                         RDM_BUFFER  sRdmBuffer = {0};
283                         PCHAR temp_buff = NULL;
284                         UINT uiTempVar = 0;
285                         if((Adapter->IdleMode == TRUE) ||
286                                 (Adapter->bShutStatus ==TRUE) ||
287                                 (Adapter->bPreparingForLowPowerMode ==TRUE))
288                         {
289                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Device in Idle Mode, Blocking Rdms\n");
290                                 return -EACCES;
291                         }
292                         /* Copy Ioctl Buffer structure */
293                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
294                                 return -EFAULT;
295
296                         if (IoBuffer.InputLength > sizeof(sRdmBuffer))
297                                 return -EINVAL;
298
299                         if(copy_from_user(&sRdmBuffer, IoBuffer.InputBuffer, IoBuffer.InputLength))
300                                 return -EFAULT;
301
302                         /* FIXME: don't trust user supplied length */
303                         temp_buff = kmalloc(IoBuffer.OutputLength, GFP_KERNEL);
304                         if(!temp_buff)
305                                 return STATUS_FAILURE;
306
307                         if((((ULONG)sRdmBuffer.Register & 0x0F000000) != 0x0F000000) ||
308                            ((ULONG)sRdmBuffer.Register & 0x3))
309                         {
310                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "RDM Done On invalid Address : %x Access Denied.\n",
311                                         (int)sRdmBuffer.Register);
312                                 return -EINVAL;
313                         }
314
315                         uiTempVar = sRdmBuffer.Register & EEPROM_REJECT_MASK;
316                         Status = rdmaltWithLock(Adapter, (UINT)sRdmBuffer.Register,
317                                                 (PUINT)temp_buff, IoBuffer.OutputLength);
318                         if(Status == STATUS_SUCCESS)
319                                 if(copy_to_user(IoBuffer.OutputBuffer, temp_buff, IoBuffer.OutputLength))
320                                         Status = -EFAULT;
321
322                         kfree(temp_buff);
323                         break;
324                 }
325                 case IOCTL_BCM_REGISTER_WRITE:
326                 case IOCTL_BCM_EEPROM_REGISTER_WRITE:
327                 {
328                         WRM_BUFFER  sWrmBuffer = {0};
329                         UINT uiTempVar=0;
330                         if((Adapter->IdleMode == TRUE) ||
331                                 (Adapter->bShutStatus ==TRUE) ||
332                                 (Adapter->bPreparingForLowPowerMode ==TRUE))
333                         {
334                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Device in Idle Mode, Blocking Wrms\n");
335                                 return -EACCES;
336                         }
337
338                         /* Copy Ioctl Buffer structure */
339                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
340                                 return -EFAULT;
341
342                         if (IoBuffer.InputLength > sizeof(sWrmBuffer))
343                                 return -EINVAL;
344
345                         /* Get WrmBuffer structure */
346                         if(copy_from_user(&sWrmBuffer, IoBuffer.InputBuffer, IoBuffer.InputLength))
347                                 return -EFAULT;
348
349                         if( (((ULONG)sWrmBuffer.Register & 0x0F000000) != 0x0F000000) ||
350                                         ((ULONG)sWrmBuffer.Register & 0x3) )
351                         {
352                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "WRM Done On invalid Address : %x Access Denied.\n",
353                                                 (int)sWrmBuffer.Register);
354                                 return -EINVAL;
355                         }
356
357                         uiTempVar = sWrmBuffer.Register & EEPROM_REJECT_MASK;
358                         if(!((Adapter->pstargetparams->m_u32Customize) & VSG_MODE) &&
359                                 ((uiTempVar == EEPROM_REJECT_REG_1)||
360                                 (uiTempVar == EEPROM_REJECT_REG_2) ||
361                                 (uiTempVar == EEPROM_REJECT_REG_3) ||
362                                 (uiTempVar == EEPROM_REJECT_REG_4)) &&
363                                 (cmd == IOCTL_BCM_REGISTER_WRITE))
364                         {
365                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "EEPROM Access Denied, not in VSG Mode\n");
366                                 return -EFAULT;
367                         }
368
369                         Status = wrmaltWithLock(Adapter, (UINT)sWrmBuffer.Register,
370                                                         (PUINT)sWrmBuffer.Data, sWrmBuffer.Length);
371                         if(Status == STATUS_SUCCESS)
372                         {
373                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, OSAL_DBG, DBG_LVL_ALL, "WRM Done\n");
374                         }
375                         else
376                         {
377                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "WRM Failed\n");
378                                 Status = -EFAULT;
379                         }
380                         break;
381                 }
382                 case IOCTL_BCM_GPIO_SET_REQUEST:
383                 {
384                         UCHAR ucResetValue[4];
385                         UINT value =0;
386                         UINT uiBit = 0;
387                 UINT uiOperation = 0;
388
389                         GPIO_INFO   gpio_info = {0};
390                         if((Adapter->IdleMode == TRUE) ||
391                                 (Adapter->bShutStatus ==TRUE) ||
392                                 (Adapter->bPreparingForLowPowerMode ==TRUE))
393                         {
394                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"GPIO Can't be set/clear in Low power Mode");
395                                 return -EACCES;
396                         }
397                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
398                                 return -EFAULT;
399                         if (IoBuffer.InputLength > sizeof(gpio_info))
400                                 return -EINVAL;
401                         if(copy_from_user(&gpio_info, IoBuffer.InputBuffer, IoBuffer.InputLength))
402                                 return -EFAULT;
403                         uiBit  = gpio_info.uiGpioNumber;
404                         uiOperation = gpio_info.uiGpioValue;
405
406                         value= (1<<uiBit);
407
408                         if(IsReqGpioIsLedInNVM(Adapter,value) ==FALSE)
409                         {
410                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Sorry, Requested GPIO<0x%X> is not correspond to LED !!!",value);
411                                 Status = -EINVAL;
412                                 break;
413                         }
414
415
416                         if(uiOperation)//Set - setting 1
417                         {
418                                 //Set the gpio output register
419                                 Status = wrmaltWithLock(Adapter,BCM_GPIO_OUTPUT_SET_REG ,
420                                                 (PUINT)(&value), sizeof(UINT));
421                                 if(Status == STATUS_SUCCESS)
422                                 {
423                     BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Set the GPIO bit\n");
424                                 }
425                     else
426                         {
427                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Failed to set the %dth GPIO \n",uiBit);
428                         break;
429                 }
430                         }
431                         else//Unset - setting 0
432                         {
433                                 //Set the gpio output register
434                                 Status = wrmaltWithLock(Adapter,BCM_GPIO_OUTPUT_CLR_REG ,
435                                                 (PUINT)(&value), sizeof(UINT));
436                                 if(Status == STATUS_SUCCESS)
437                                 {
438                     BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Set the GPIO bit\n");
439                                 }
440                     else
441                         {
442                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Failed to clear the %dth GPIO \n",uiBit);
443                         break;
444                 }
445                         }
446
447                         Status = rdmaltWithLock(Adapter, (UINT)GPIO_MODE_REGISTER,
448                                         (PUINT)ucResetValue, sizeof(UINT));
449                         if (STATUS_SUCCESS != Status)
450             {
451                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"GPIO_MODE_REGISTER read failed");
452                                 break;
453                         }
454                         //Set the gpio mode register to output
455                         *(UINT*)ucResetValue |= (1<<uiBit);
456                         Status = wrmaltWithLock(Adapter,GPIO_MODE_REGISTER ,
457                                         (PUINT)ucResetValue, sizeof(UINT));
458                         if(Status == STATUS_SUCCESS)
459                         {
460                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Set the GPIO to output Mode\n");
461                         }
462             else
463             {
464                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Failed to put GPIO in Output Mode\n");
465                 break;
466             }
467                 }
468                 break;
469                 case BCM_LED_THREAD_STATE_CHANGE_REQ:
470                 {
471                         USER_THREAD_REQ threadReq = { 0 };
472                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"User made LED thread InActive");
473
474                         if((Adapter->IdleMode == TRUE) ||
475                                 (Adapter->bShutStatus ==TRUE) ||
476                                 (Adapter->bPreparingForLowPowerMode ==TRUE))
477                         {
478                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"GPIO Can't be set/clear in Low power Mode");
479                                 Status = -EACCES;
480                                 break;
481                         }
482
483                         if (copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
484                                 return -EFAULT;
485
486                         if (IoBuffer.InputLength > sizeof(threadReq))
487                                 return -EINVAL;
488
489                         if (copy_from_user(&threadReq, IoBuffer.InputBuffer, IoBuffer.InputLength))
490                                 return -EFAULT;
491
492                         //if LED thread is running(Actively or Inactively) set it state to make inactive
493                         if(Adapter->LEDInfo.led_thread_running)
494                         {
495                                 if(threadReq.ThreadState == LED_THREAD_ACTIVATION_REQ)
496                                 {
497                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Activating thread req");
498                                         Adapter->DriverState = LED_THREAD_ACTIVE;
499                                 }
500                                 else
501                                 {
502                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"DeActivating Thread req.....");
503                                         Adapter->DriverState = LED_THREAD_INACTIVE;
504                                 }
505
506                                 //signal thread.
507                                 wake_up(&Adapter->LEDInfo.notify_led_event);
508
509                         }
510                 }
511                 break;
512                 case IOCTL_BCM_GPIO_STATUS_REQUEST:
513                 {
514                         ULONG uiBit = 0;
515                         UCHAR ucRead[4];
516                         GPIO_INFO   gpio_info = {0};
517                         if((Adapter->IdleMode == TRUE) ||
518                                 (Adapter->bShutStatus ==TRUE) ||
519                                 (Adapter->bPreparingForLowPowerMode ==TRUE))
520                                 return -EACCES;
521                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
522                                 return -EFAULT;
523                         if (IoBuffer.InputLength > sizeof(gpio_info))
524                                 return -EINVAL;
525                         if(copy_from_user(&gpio_info, IoBuffer.InputBuffer, IoBuffer.InputLength))
526                                 return -EFAULT;
527                 uiBit  = gpio_info.uiGpioNumber;
528                                   //Set the gpio output register
529                                 Status = rdmaltWithLock(Adapter, (UINT)GPIO_PIN_STATE_REGISTER,
530                         (PUINT)ucRead, sizeof(UINT));
531                 if(Status != STATUS_SUCCESS)
532                 {
533                     BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "RDM Failed\n");
534                                         return Status;
535                 }
536
537                         }
538                         break;
539                         case IOCTL_BCM_GPIO_MULTI_REQUEST:
540                         {
541                                 UCHAR ucResetValue[4];
542                                 GPIO_MULTI_INFO gpio_multi_info[MAX_IDX];
543                                 PGPIO_MULTI_INFO pgpio_multi_info = (PGPIO_MULTI_INFO)gpio_multi_info;
544
545                                 memset( pgpio_multi_info, 0, MAX_IDX * sizeof( GPIO_MULTI_INFO));
546
547                                 if((Adapter->IdleMode == TRUE) ||
548                                 (Adapter->bShutStatus ==TRUE) ||
549                                 (Adapter->bPreparingForLowPowerMode ==TRUE))
550                                         return -EINVAL;
551                                 if (copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
552                                         return -EFAULT;
553                                 if (IoBuffer.InputLength > sizeof(gpio_multi_info))
554                                         return -EINVAL;
555                                 if (copy_from_user(&gpio_multi_info, IoBuffer.InputBuffer, IoBuffer.InputLength))
556                                         return -EFAULT;
557
558                                 if(IsReqGpioIsLedInNVM(Adapter,pgpio_multi_info[WIMAX_IDX].uiGPIOMask)== FALSE)
559                                 {
560                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Sorry, Requested GPIO<0x%X> is not correspond to NVM LED bit map<0x%X>!!!",pgpio_multi_info[WIMAX_IDX].uiGPIOMask,Adapter->gpioBitMap);
561                                         Status = -EINVAL;
562                                         break;
563                                 }
564
565                                 /* Set the gpio output register */
566
567                                 if( ( pgpio_multi_info[WIMAX_IDX].uiGPIOMask) &
568                                         ( pgpio_multi_info[WIMAX_IDX].uiGPIOCommand))
569                                 {
570                                         /* Set 1's in GPIO OUTPUT REGISTER */
571                                         *(UINT*) ucResetValue =  pgpio_multi_info[WIMAX_IDX].uiGPIOMask &
572                                                                                  pgpio_multi_info[WIMAX_IDX].uiGPIOCommand &
573                                                                                          pgpio_multi_info[WIMAX_IDX].uiGPIOValue;
574
575                                         if( *(UINT*) ucResetValue)
576                                                 Status = wrmaltWithLock( Adapter, BCM_GPIO_OUTPUT_SET_REG , (PUINT) ucResetValue, sizeof(ULONG));
577
578                                         if( Status != STATUS_SUCCESS)
579                                         {
580                                                 BCM_DEBUG_PRINT( Adapter,DBG_TYPE_PRINTK, 0, 0,"WRM to BCM_GPIO_OUTPUT_SET_REG Failed.");
581                                                 return Status;
582                                         }
583
584                                         /* Clear to 0's in GPIO OUTPUT REGISTER */
585                                         *(UINT*) ucResetValue = (pgpio_multi_info[WIMAX_IDX].uiGPIOMask &
586                                                         pgpio_multi_info[WIMAX_IDX].uiGPIOCommand &
587                                                         ( ~( pgpio_multi_info[WIMAX_IDX].uiGPIOValue)));
588
589                                         if( *(UINT*) ucResetValue)
590                                                 Status = wrmaltWithLock( Adapter, BCM_GPIO_OUTPUT_CLR_REG , (PUINT) ucResetValue, sizeof(ULONG));
591
592                                         if( Status != STATUS_SUCCESS)
593                                         {
594                                                 BCM_DEBUG_PRINT( Adapter,DBG_TYPE_PRINTK, 0, 0,"WRM to BCM_GPIO_OUTPUT_CLR_REG Failed." );
595                                                 return Status;
596                                         }
597                                 }
598
599                                 if( pgpio_multi_info[WIMAX_IDX].uiGPIOMask)
600                                 {
601                                         Status = rdmaltWithLock(Adapter, (UINT)GPIO_PIN_STATE_REGISTER, (PUINT)ucResetValue, sizeof(UINT));
602
603                                         if(Status != STATUS_SUCCESS)
604                                         {
605                                                 BCM_DEBUG_PRINT( Adapter,DBG_TYPE_PRINTK, 0, 0,"RDM to GPIO_PIN_STATE_REGISTER Failed.");
606                                                 return Status;
607                                         }
608
609                                         pgpio_multi_info[WIMAX_IDX].uiGPIOValue = ( *(UINT*)ucResetValue &
610                                                                                         pgpio_multi_info[WIMAX_IDX].uiGPIOMask);
611                                 }
612
613                                 Status = copy_to_user(IoBuffer.OutputBuffer, &gpio_multi_info, IoBuffer.OutputLength);
614                                 if(Status)
615                                 {
616                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Failed while copying Content to IOBufer for user space err:%d",Status);
617                                         break;
618                                 }
619                         }
620                         break;
621                 case IOCTL_BCM_GPIO_MODE_REQUEST:
622                 {
623                         UCHAR ucResetValue[4];
624                         GPIO_MULTI_MODE gpio_multi_mode[MAX_IDX];
625                         PGPIO_MULTI_MODE pgpio_multi_mode = ( PGPIO_MULTI_MODE) gpio_multi_mode;
626
627                         if((Adapter->IdleMode == TRUE) ||
628                                 (Adapter->bShutStatus ==TRUE) ||
629                                 (Adapter->bPreparingForLowPowerMode ==TRUE))
630                                         return -EINVAL;
631
632                         if (copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
633                                 return -EFAULT;
634                         if (IoBuffer.InputLength > sizeof(gpio_multi_mode))
635                                 return -EINVAL;
636                         if (copy_from_user(&gpio_multi_mode, IoBuffer.InputBuffer, IoBuffer.InputLength))
637                                 return -EFAULT;
638
639                         Status = rdmaltWithLock( Adapter, ( UINT) GPIO_MODE_REGISTER, ( PUINT) ucResetValue, sizeof( UINT));
640                         if( STATUS_SUCCESS != Status)
641                         {
642                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Read of GPIO_MODE_REGISTER failed");
643                                 return Status;
644                         }
645
646                         //Validating the request
647                         if(IsReqGpioIsLedInNVM(Adapter,pgpio_multi_mode[WIMAX_IDX].uiGPIOMask)== FALSE)
648                         {
649                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Sorry, Requested GPIO<0x%X> is not correspond to NVM LED bit map<0x%X>!!!",pgpio_multi_mode[WIMAX_IDX].uiGPIOMask,Adapter->gpioBitMap);
650                                 Status = -EINVAL;
651                                 break;
652                         }
653
654                         if( pgpio_multi_mode[WIMAX_IDX].uiGPIOMask)
655                         {
656                                 /* write all OUT's (1's) */
657                                 *( UINT*) ucResetValue |= ( pgpio_multi_mode[WIMAX_IDX].uiGPIOMode &
658                                                                 pgpio_multi_mode[WIMAX_IDX].uiGPIOMask);
659                                 /* write all IN's (0's) */
660                                 *( UINT*) ucResetValue &= ~( ( ~pgpio_multi_mode[WIMAX_IDX].uiGPIOMode) &
661                                                                 pgpio_multi_mode[WIMAX_IDX].uiGPIOMask);
662
663                                 /* Currently implemented return the modes of all GPIO's
664                                  * else needs to bit AND with  mask
665                                  * */
666                                 pgpio_multi_mode[WIMAX_IDX].uiGPIOMode = *(UINT*)ucResetValue;
667
668                                 Status = wrmaltWithLock( Adapter, GPIO_MODE_REGISTER , ( PUINT) ucResetValue, sizeof( ULONG));
669                                 if( Status == STATUS_SUCCESS)
670                                 {
671                                         BCM_DEBUG_PRINT( Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "WRM to GPIO_MODE_REGISTER Done");
672                                 }
673                                 else
674                                 {
675                                         BCM_DEBUG_PRINT( Adapter,DBG_TYPE_PRINTK, 0, 0,"WRM to GPIO_MODE_REGISTER Failed");
676                                         Status = -EFAULT;
677                                         break;
678                                 }
679                         }
680                         else /* if uiGPIOMask is 0 then return mode register configuration */
681                         {
682                                 pgpio_multi_mode[WIMAX_IDX].uiGPIOMode = *( UINT*) ucResetValue;
683                         }
684                         Status = copy_to_user(IoBuffer.OutputBuffer, &gpio_multi_mode, IoBuffer.OutputLength);
685                         if(Status)
686                         {
687                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Failed while copying Content to IOBufer for user space err:%d",Status);
688                                 break;
689                         }
690                 }
691                 break;
692
693                 case IOCTL_MAC_ADDR_REQ:
694                 case IOCTL_LINK_REQ:
695                 case IOCTL_CM_REQUEST:
696                 case IOCTL_SS_INFO_REQ:
697                 case IOCTL_SEND_CONTROL_MESSAGE:
698                 case IOCTL_IDLE_REQ:
699                 {
700                         PVOID pvBuffer=NULL;
701
702                         /* Copy Ioctl Buffer structure */
703                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
704                                 return -EFAULT;
705
706                         /* FIXME: don't accept any length from user */
707                         pvBuffer = kmalloc(IoBuffer.InputLength, GFP_KERNEL);
708                         if(!pvBuffer)
709                                 return -ENOMEM;
710
711                         if(copy_from_user(pvBuffer, IoBuffer.InputBuffer, IoBuffer.InputLength))
712                         {
713                                 Status = -EFAULT;
714                                 kfree(pvBuffer);
715                                 break;
716                         }
717
718                         down(&Adapter->LowPowerModeSync);
719                         Status = wait_event_interruptible_timeout(Adapter->lowpower_mode_wait_queue,
720                                                                                                         !Adapter->bPreparingForLowPowerMode,
721                                                                                                         (1 * HZ));
722                         if(Status == -ERESTARTSYS)
723                                         goto cntrlEnd;
724
725                         if(Adapter->bPreparingForLowPowerMode)
726                         {
727                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Preparing Idle Mode is still True - Hence Rejecting control message\n");
728                                 Status = STATUS_FAILURE ;
729                                 goto cntrlEnd ;
730                         }
731                         Status = CopyBufferToControlPacket(Adapter, (PVOID)pvBuffer);
732                 cntrlEnd:
733                         up(&Adapter->LowPowerModeSync);
734                         kfree(pvBuffer);
735                         break;
736                 }
737                 case IOCTL_BCM_BUFFER_DOWNLOAD_START:
738                 {
739                         INT NVMAccess = down_trylock(&Adapter->NVMRdmWrmLock) ;
740                         if(NVMAccess)
741                         {
742                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, " IOCTL_BCM_CHIP_RESET not allowed as EEPROM Read/Write is in progress\n");
743                                 return -EACCES;
744                         }
745                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Starting the firmware download PID =0x%x!!!!\n", current->pid);
746                     if(!down_trylock(&Adapter->fw_download_sema))
747                         {
748                                 Adapter->bBinDownloaded=FALSE;
749                                 Adapter->fw_download_process_pid=current->pid;
750                                 Adapter->bCfgDownloaded=FALSE;
751                                 Adapter->fw_download_done=FALSE;
752                                 netif_carrier_off(Adapter->dev);
753                                 netif_stop_queue(Adapter->dev);
754                                 Status = reset_card_proc(Adapter);
755                                 if(Status)
756                                 {
757                                         pr_err(PFX "%s: reset_card_proc Failed!\n", Adapter->dev->name);
758                                         up(&Adapter->fw_download_sema);
759                                         up(&Adapter->NVMRdmWrmLock);
760                                         break;
761                                 }
762                                 mdelay(10);
763                         }
764                         else
765                         {
766
767                                 Status = -EBUSY;
768
769                         }
770                         up(&Adapter->NVMRdmWrmLock);
771                         break;
772                 }
773                 case IOCTL_BCM_BUFFER_DOWNLOAD:
774                         {
775                                 FIRMWARE_INFO   *psFwInfo = NULL;
776                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Starting the firmware download PID =0x%x!!!!\n", current->pid);
777                         do{
778                                 if(!down_trylock(&Adapter->fw_download_sema))
779                                 {
780                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Invalid way to download buffer. Use Start and then call this!!!\n");
781                                         Status=-EINVAL;
782                                         break;
783                                 }
784
785                                 /* Copy Ioctl Buffer structure */
786                                 if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
787                                         return -EFAULT;
788
789                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Length for FW DLD is : %lx\n",
790                                                                                 IoBuffer.InputLength);
791
792                                 if (IoBuffer.InputLength > sizeof(FIRMWARE_INFO))
793                                         return -EINVAL;
794
795                                 psFwInfo = kmalloc(sizeof(*psFwInfo), GFP_KERNEL);
796                                 if(!psFwInfo)
797                                         return -ENOMEM;
798
799                                 if(copy_from_user(psFwInfo, IoBuffer.InputBuffer, IoBuffer.InputLength))
800                                         return -EFAULT;
801
802                                 if(!psFwInfo->pvMappedFirmwareAddress ||
803                                                 (psFwInfo->u32FirmwareLength == 0))
804                                 {
805                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Something else is wrong %lu\n",
806                                         psFwInfo->u32FirmwareLength);
807                                         Status = -EINVAL;
808                                         break;
809                                 }
810                                 Status = bcm_ioctl_fw_download(Adapter, psFwInfo);
811                                 if(Status != STATUS_SUCCESS)
812                                 {
813                                         if(psFwInfo->u32StartingAddress==CONFIG_BEGIN_ADDR)
814                                         {
815                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "IOCTL: Configuration File Upload Failed\n");
816                                         }
817                                         else
818                                         {
819                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "IOCTL: Firmware File Upload Failed\n");
820                                         }
821                                         //up(&Adapter->fw_download_sema);
822
823                                         if(Adapter->LEDInfo.led_thread_running & BCM_LED_THREAD_RUNNING_ACTIVELY)
824                                         {
825                                                 Adapter->DriverState = DRIVER_INIT;
826                                                 Adapter->LEDInfo.bLedInitDone = FALSE;
827                                                 wake_up(&Adapter->LEDInfo.notify_led_event);
828                                         }
829                                 }
830                                 break ;
831                           }while(0);
832
833                           if(Status != STATUS_SUCCESS)
834                                         up(&Adapter->fw_download_sema);
835                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, OSAL_DBG, DBG_LVL_ALL, "IOCTL: Firmware File Uploaded\n");
836                                 kfree(psFwInfo);
837                                 break;
838                         }
839                 case IOCTL_BCM_BUFFER_DOWNLOAD_STOP:
840                 {
841                         INT NVMAccess = down_trylock(&Adapter->NVMRdmWrmLock);
842                         if(NVMAccess)
843                         {
844                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, " FW download blocked as EEPROM Read/Write is in progress\n");
845                                 up(&Adapter->fw_download_sema);
846                                 return -EACCES;
847                         }
848                         if(down_trylock(&Adapter->fw_download_sema))
849                         {
850                                 Adapter->bBinDownloaded=TRUE;
851                                 Adapter->bCfgDownloaded=TRUE;
852                                 atomic_set(&Adapter->CurrNumFreeTxDesc, 0);
853
854                                 Adapter->CurrNumRecvDescs=0;
855                                 Adapter->downloadDDR = 0;
856
857                                 //setting the Mips to Run
858                                 Status = run_card_proc(Adapter);
859                                 if(Status)
860                                 {
861                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Firm Download Failed\n");
862                                         up(&Adapter->fw_download_sema);
863                                         up(&Adapter->NVMRdmWrmLock);
864                                         break;
865                                 }
866                                 else
867                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Firm Download Over...\n");
868                                 mdelay(10);
869                                 /* Wait for MailBox Interrupt */
870                                 if(StartInterruptUrb((PS_INTERFACE_ADAPTER)Adapter->pvInterfaceAdapter))
871                                 {
872                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Unable to send interrupt...\n");
873                                 }
874                                 timeout = 5*HZ;
875                                 Adapter->waiting_to_fw_download_done = FALSE;
876                                 wait_event_timeout(Adapter->ioctl_fw_dnld_wait_queue,
877                                         Adapter->waiting_to_fw_download_done, timeout);
878                                 Adapter->fw_download_process_pid=INVALID_PID;
879                                 Adapter->fw_download_done=TRUE;
880                                 atomic_set(&Adapter->CurrNumFreeTxDesc, 0);
881                                 Adapter->CurrNumRecvDescs = 0;
882                                 Adapter->PrevNumRecvDescs = 0;
883                                 atomic_set(&Adapter->cntrlpktCnt,0);
884                 Adapter->LinkUpStatus = 0;
885                 Adapter->LinkStatus = 0;
886
887                                 if(Adapter->LEDInfo.led_thread_running & BCM_LED_THREAD_RUNNING_ACTIVELY)
888                                 {
889                                         Adapter->DriverState = FW_DOWNLOAD_DONE;
890                                         wake_up(&Adapter->LEDInfo.notify_led_event);
891                                 }
892
893                                 if(!timeout)
894                                 {
895                                         Status = -ENODEV;
896                                 }
897                         }
898                         else
899                         {
900                                 Status = -EINVAL;
901                         }
902                         up(&Adapter->fw_download_sema);
903                         up(&Adapter->NVMRdmWrmLock);
904                         break;
905                 }
906                 case IOCTL_BE_BUCKET_SIZE:
907                         Status = 0;
908                         if (get_user(Adapter->BEBucketSize, (unsigned long __user *)arg))
909                                 Status = -EFAULT;
910                         break;
911
912                 case IOCTL_RTPS_BUCKET_SIZE:
913                         Status = 0;
914                         if (get_user(Adapter->rtPSBucketSize, (unsigned long __user *)arg))
915                                 Status = -EFAULT;
916                         break;
917                 case IOCTL_CHIP_RESET:
918             {
919                         INT NVMAccess = down_trylock(&Adapter->NVMRdmWrmLock);
920                         if(NVMAccess)
921                         {
922                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, " IOCTL_BCM_CHIP_RESET not allowed as EEPROM Read/Write is in progress\n");
923                                 return -EACCES;
924                         }
925                         down(&Adapter->RxAppControlQueuelock);
926                         Status = reset_card_proc(Adapter);
927                         flushAllAppQ();
928                         up(&Adapter->RxAppControlQueuelock);
929                         up(&Adapter->NVMRdmWrmLock);
930                         ResetCounters(Adapter);
931                         break;
932                 }
933                 case IOCTL_QOS_THRESHOLD:
934                 {
935                         USHORT uiLoopIndex;
936
937                         Status = 0;
938                         for (uiLoopIndex = 0; uiLoopIndex < NO_OF_QUEUES; uiLoopIndex++) {
939                                 if (get_user(Adapter->PackInfo[uiLoopIndex].uiThreshold,
940                                                 (unsigned long __user *)arg)) {
941                                         Status = -EFAULT;
942                                         break;
943                                 }
944                         }
945                         break;
946                 }
947
948                 case IOCTL_DUMP_PACKET_INFO:
949
950                         DumpPackInfo(Adapter);
951                 DumpPhsRules(&Adapter->stBCMPhsContext);
952                         Status = STATUS_SUCCESS;
953                         break;
954
955                 case IOCTL_GET_PACK_INFO:
956                         if(copy_to_user(argp, &Adapter->PackInfo, sizeof(PacketInfo)*NO_OF_QUEUES))
957                                 return -EFAULT;
958                         Status = STATUS_SUCCESS;
959                         break;
960                 case IOCTL_BCM_SWITCH_TRANSFER_MODE:
961                 {
962                         UINT uiData = 0;
963                         if(copy_from_user(&uiData, argp, sizeof(UINT)))
964                                 return -EFAULT;
965
966                         if(uiData)      /* Allow All Packets */
967                         {
968                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_SWITCH_TRANSFER_MODE: ETH_PACKET_TUNNELING_MODE\n");
969                                 Adapter->TransferMode = ETH_PACKET_TUNNELING_MODE;
970                         }
971                         else    /* Allow IP only Packets */
972                         {
973                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_SWITCH_TRANSFER_MODE: IP_PACKET_ONLY_MODE\n");
974                                 Adapter->TransferMode = IP_PACKET_ONLY_MODE;
975                         }
976                         Status = STATUS_SUCCESS;
977                         break;
978                 }
979
980                 case IOCTL_BCM_GET_DRIVER_VERSION:
981                 {
982                         /* Copy Ioctl Buffer structure */
983                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
984                                 return -EFAULT;
985
986                         if(copy_to_user(IoBuffer.OutputBuffer, VER_FILEVERSION_STR, IoBuffer.OutputLength))
987                                 return -EFAULT;
988                         Status = STATUS_SUCCESS;
989                         break;
990                 }
991                 case IOCTL_BCM_GET_CURRENT_STATUS:
992                 {
993                         LINK_STATE link_state;
994
995                         /* Copy Ioctl Buffer structure */
996                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
997                         {
998                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "copy_from_user failed..\n");
999                                 Status = -EFAULT;
1000                                 break;
1001                         }
1002                         if (IoBuffer.OutputLength != sizeof(link_state)) {
1003                                 Status = -EINVAL;
1004                                 break;
1005                         }
1006
1007                         memset(&link_state, 0, sizeof(link_state));
1008                         link_state.bIdleMode = Adapter->IdleMode;
1009                         link_state.bShutdownMode = Adapter->bShutStatus;
1010                         link_state.ucLinkStatus = Adapter->LinkStatus;
1011
1012                         if (copy_to_user(IoBuffer.OutputBuffer, &link_state,
1013                                          min_t(size_t, sizeof(link_state), IoBuffer.OutputLength)))
1014                         {
1015                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Copy_to_user Failed..\n");
1016                                 Status = -EFAULT;
1017                                 break;
1018                         }
1019                         Status = STATUS_SUCCESS;
1020                         break;
1021                 }
1022         case IOCTL_BCM_SET_MAC_TRACING:
1023         {
1024             UINT  tracing_flag;
1025
1026             /* copy ioctl Buffer structure */
1027             if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1028                     return -EFAULT;
1029
1030             if(copy_from_user(&tracing_flag,IoBuffer.InputBuffer,sizeof(UINT)))
1031                     return -EFAULT;
1032
1033             if (tracing_flag)
1034                 Adapter->pTarangs->MacTracingEnabled = TRUE;
1035             else
1036                 Adapter->pTarangs->MacTracingEnabled = FALSE;
1037             break;
1038         }
1039                 case IOCTL_BCM_GET_DSX_INDICATION:
1040                 {
1041                         ULONG ulSFId=0;
1042                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1043                                 return -EFAULT;
1044
1045                         if(IoBuffer.OutputLength < sizeof(stLocalSFAddIndicationAlt))
1046                         {
1047                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,
1048                                                 "Mismatch req: %lx needed is =0x%zx!!!",
1049                                                 IoBuffer.OutputLength, sizeof(stLocalSFAddIndicationAlt));
1050                                 return -EINVAL;
1051                         }
1052
1053                         if(copy_from_user(&ulSFId, IoBuffer.InputBuffer, sizeof(ulSFId)))
1054                                 return -EFAULT;
1055
1056                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Get DSX Data SF ID is =%lx\n", ulSFId );
1057                         get_dsx_sf_data_to_application(Adapter, ulSFId, IoBuffer.OutputBuffer);
1058                         Status=STATUS_SUCCESS;
1059                 }
1060                 break;
1061                 case IOCTL_BCM_GET_HOST_MIBS:
1062                 {
1063                         PVOID temp_buff;
1064
1065                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1066                                 return -EFAULT;
1067
1068                         if(IoBuffer.OutputLength != sizeof(S_MIBS_HOST_STATS_MIBS))
1069                         {
1070                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,
1071                                                 "Length Check failed %lu %zd\n",
1072                                                 IoBuffer.OutputLength, sizeof(S_MIBS_HOST_STATS_MIBS));
1073                                 return -EINVAL;
1074                         }
1075
1076                         /* FIXME: HOST_STATS are too big for kmalloc (122048)! */
1077                         temp_buff = kzalloc(sizeof(S_MIBS_HOST_STATS_MIBS), GFP_KERNEL);
1078                         if(!temp_buff)
1079                                 return STATUS_FAILURE;
1080
1081                         Status = ProcessGetHostMibs(Adapter, temp_buff);
1082                         GetDroppedAppCntrlPktMibs(temp_buff, pTarang);
1083
1084                         if (Status != STATUS_FAILURE)
1085                                 if(copy_to_user(IoBuffer.OutputBuffer, temp_buff, sizeof(S_MIBS_HOST_STATS_MIBS)))
1086                                         Status = -EFAULT;
1087
1088                         kfree(temp_buff);
1089                         break;
1090                 }
1091
1092                 case IOCTL_BCM_WAKE_UP_DEVICE_FROM_IDLE:
1093                         if((FALSE == Adapter->bTriedToWakeUpFromlowPowerMode) && (TRUE==Adapter->IdleMode))
1094                         {
1095                                 Adapter->usIdleModePattern = ABORT_IDLE_MODE;
1096                                 Adapter->bWakeUpDevice = TRUE;
1097                                 wake_up(&Adapter->process_rx_cntrlpkt);
1098                         }
1099                         Status = STATUS_SUCCESS;
1100                         break;
1101
1102                 case IOCTL_BCM_BULK_WRM:
1103                         {
1104                                 PBULKWRM_BUFFER pBulkBuffer;
1105                                 UINT uiTempVar=0;
1106                                 PCHAR pvBuffer = NULL;
1107
1108                                 if((Adapter->IdleMode == TRUE) ||
1109                                         (Adapter->bShutStatus ==TRUE) ||
1110                                         (Adapter->bPreparingForLowPowerMode ==TRUE))
1111                                 {
1112                     BCM_DEBUG_PRINT (Adapter, DBG_TYPE_PRINTK, 0, 0, "Device in Idle/Shutdown Mode, Blocking Wrms\n");
1113                                         Status = -EACCES;
1114                                         break;
1115                                 }
1116
1117                                 /* Copy Ioctl Buffer structure */
1118                                 if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1119                                         return -EFAULT;
1120
1121                                 /* FIXME: restrict length */
1122                                 pvBuffer = kmalloc(IoBuffer.InputLength, GFP_KERNEL);
1123                                 if(!pvBuffer)
1124                                         return -ENOMEM;
1125
1126                                 /* Get WrmBuffer structure */
1127                                 if(copy_from_user(pvBuffer, IoBuffer.InputBuffer, IoBuffer.InputLength))
1128                                 {
1129                                         kfree(pvBuffer);
1130                                         Status = -EFAULT;
1131                                         break;
1132                                 }
1133
1134                                 pBulkBuffer = (PBULKWRM_BUFFER)pvBuffer;
1135
1136                                 if(((ULONG)pBulkBuffer->Register & 0x0F000000) != 0x0F000000 ||
1137                                         ((ULONG)pBulkBuffer->Register & 0x3))
1138                                 {
1139                                         kfree(pvBuffer);
1140                     BCM_DEBUG_PRINT (Adapter, DBG_TYPE_PRINTK, 0, 0,"WRM Done On invalid Address : %x Access Denied.\n",(int)pBulkBuffer->Register);
1141                                         Status = -EINVAL;
1142                                         break;
1143                                 }
1144
1145
1146                                 uiTempVar = pBulkBuffer->Register & EEPROM_REJECT_MASK;
1147                                 if(!((Adapter->pstargetparams->m_u32Customize)&VSG_MODE)
1148                                 &&      ((uiTempVar == EEPROM_REJECT_REG_1)||
1149                                                 (uiTempVar == EEPROM_REJECT_REG_2) ||
1150                                         (uiTempVar == EEPROM_REJECT_REG_3) ||
1151                                         (uiTempVar == EEPROM_REJECT_REG_4)) &&
1152                                         (cmd == IOCTL_BCM_REGISTER_WRITE))
1153                                 {
1154                                         kfree(pvBuffer);
1155                     BCM_DEBUG_PRINT (Adapter, DBG_TYPE_PRINTK, 0, 0,"EEPROM Access Denied, not in VSG Mode\n");
1156                                         Status = -EFAULT;
1157                                         break;
1158                                 }
1159
1160                                 if(pBulkBuffer->SwapEndian == FALSE)
1161                                         Status = wrmWithLock(Adapter, (UINT)pBulkBuffer->Register, (PCHAR)pBulkBuffer->Values, IoBuffer.InputLength - 2*sizeof(ULONG));
1162                                 else
1163                                         Status = wrmaltWithLock(Adapter, (UINT)pBulkBuffer->Register, (PUINT)pBulkBuffer->Values, IoBuffer.InputLength - 2*sizeof(ULONG));
1164
1165                                 if(Status != STATUS_SUCCESS)
1166                                 {
1167                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "WRM Failed\n");
1168                                 }
1169
1170                                 kfree(pvBuffer);
1171                                 break;
1172                         }
1173
1174                 case IOCTL_BCM_GET_NVM_SIZE:
1175                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1176                                 return -EFAULT;
1177
1178                         if(Adapter->eNVMType == NVM_EEPROM || Adapter->eNVMType == NVM_FLASH ) {
1179                                 if(copy_to_user(IoBuffer.OutputBuffer, &Adapter->uiNVMDSDSize, sizeof(UINT)))
1180                                         return -EFAULT;
1181                         }
1182                         Status = STATUS_SUCCESS ;
1183                         break;
1184
1185                 case IOCTL_BCM_CAL_INIT :
1186
1187                         {
1188                                 UINT uiSectorSize = 0 ;
1189                                 if(Adapter->eNVMType == NVM_FLASH)
1190                                 {
1191                                         if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1192                                                 return -EFAULT;
1193
1194                                         if (copy_from_user(&uiSectorSize, IoBuffer.InputBuffer, sizeof(UINT)))
1195                                                 return -EFAULT;
1196
1197                                         if((uiSectorSize < MIN_SECTOR_SIZE) || (uiSectorSize > MAX_SECTOR_SIZE))
1198                                         {
1199                                                 if (copy_to_user(IoBuffer.OutputBuffer, &Adapter->uiSectorSize,
1200                                                                  sizeof(UINT)))
1201                                                         return -EFAULT;
1202                                         }
1203                                         else
1204                                         {
1205                                                 if(IsFlash2x(Adapter))
1206                                                 {
1207                                                         if (copy_to_user(IoBuffer.OutputBuffer,
1208                                                                          &Adapter->uiSectorSize ,
1209                                                                          sizeof(UINT)))
1210                                                             return -EFAULT;
1211                                                 }
1212                                                 else
1213                                                 {
1214                                                         if((TRUE == Adapter->bShutStatus) ||
1215                                                            (TRUE == Adapter->IdleMode))
1216                                                         {
1217                                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Device is in Idle/Shutdown Mode\n");
1218                                                                 return -EACCES;
1219                                                         }
1220
1221                                                         Adapter->uiSectorSize = uiSectorSize ;
1222                                                         BcmUpdateSectorSize(Adapter,Adapter->uiSectorSize);
1223                                                 }
1224                                         }
1225                                         Status = STATUS_SUCCESS ;
1226                                 }
1227                                 else
1228                                 {
1229                                         Status = STATUS_FAILURE;
1230                                 }
1231                         }
1232                         break;
1233         case IOCTL_BCM_SET_DEBUG :
1234 #ifdef DEBUG
1235             {
1236                 USER_BCM_DBG_STATE sUserDebugState;
1237
1238 //                              BCM_DEBUG_PRINT (Adapter, DBG_TYPE_PRINTK, 0, 0, "Entered the ioctl %x \n", IOCTL_BCM_SET_DEBUG );
1239
1240                                 BCM_DEBUG_PRINT (Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "In SET_DEBUG ioctl\n");
1241                                 if (copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1242                                         return -EFAULT;
1243
1244                                 if (copy_from_user(&sUserDebugState, IoBuffer.InputBuffer, sizeof(USER_BCM_DBG_STATE)))
1245                                         return -EFAULT;
1246
1247
1248                                 BCM_DEBUG_PRINT (Adapter, DBG_TYPE_PRINTK, 0, 0, "IOCTL_BCM_SET_DEBUG: OnOff=%d Type = 0x%x ",
1249                                 sUserDebugState.OnOff, sUserDebugState.Type);
1250                                 //sUserDebugState.Subtype <<= 1;
1251                                 sUserDebugState.Subtype = 1 << sUserDebugState.Subtype;
1252                                 BCM_DEBUG_PRINT (Adapter, DBG_TYPE_PRINTK, 0, 0, "actual Subtype=0x%x\n", sUserDebugState.Subtype);
1253
1254                                 // Update new 'DebugState' in the Adapter
1255                                 Adapter->stDebugState.type |= sUserDebugState.Type;
1256                                 /* Subtype: A bitmap of 32 bits for Subtype per Type.
1257                                  * Valid indexes in 'subtype' array: 1,2,4,8
1258                                  * corresponding to valid Type values. Hence we can use the 'Type' field
1259                                  * as the index value, ignoring the array entries 0,3,5,6,7 !
1260                                  */
1261                                 if (sUserDebugState.OnOff)
1262                                         Adapter->stDebugState.subtype[sUserDebugState.Type] |= sUserDebugState.Subtype;
1263                                 else
1264                                         Adapter->stDebugState.subtype[sUserDebugState.Type] &= ~sUserDebugState.Subtype;
1265
1266                 BCM_SHOW_DEBUG_BITMAP(Adapter);
1267
1268                         }
1269 #endif
1270                         break;
1271                 case IOCTL_BCM_NVM_READ:
1272                 case IOCTL_BCM_NVM_WRITE:
1273                         {
1274                                 NVM_READWRITE  stNVMReadWrite;
1275                                 PUCHAR pReadData = NULL;
1276                                 ULONG ulDSDMagicNumInUsrBuff = 0;
1277                                 struct timeval tv0, tv1;
1278                                 memset(&tv0,0,sizeof(struct timeval));
1279                                 memset(&tv1,0,sizeof(struct timeval));
1280                                 if((Adapter->eNVMType == NVM_FLASH) && (Adapter->uiFlashLayoutMajorVersion == 0))
1281                                 {
1282                                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0,"The Flash Control Section is Corrupted. Hence Rejection on NVM Read/Write\n");
1283                                         Status = -EFAULT;
1284                                         break;
1285                                 }
1286
1287                                 if(IsFlash2x(Adapter))
1288                                 {
1289                                         if((Adapter->eActiveDSD != DSD0) &&
1290                                                 (Adapter->eActiveDSD != DSD1) &&
1291                                                 (Adapter->eActiveDSD != DSD2))
1292                                         {
1293                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"No DSD is active..hence NVM Command is blocked");
1294                                                 return STATUS_FAILURE ;
1295                                         }
1296                                 }
1297
1298                         /* Copy Ioctl Buffer structure */
1299
1300                                 if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1301                                         return -EFAULT;
1302
1303                                 if(copy_from_user(&stNVMReadWrite,
1304                                                   (IOCTL_BCM_NVM_READ == cmd) ? IoBuffer.OutputBuffer : IoBuffer.InputBuffer,
1305                                                   sizeof(NVM_READWRITE)))
1306                                         return -EFAULT;
1307
1308                                 //
1309                                 // Deny the access if the offset crosses the cal area limit.
1310                                 //
1311                                 if((stNVMReadWrite.uiOffset + stNVMReadWrite.uiNumBytes) > Adapter->uiNVMDSDSize)
1312                                 {
1313                                 //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Can't allow access beyond NVM Size: 0x%x 0x%x\n", stNVMReadWrite.uiOffset ,
1314 //                                                      stNVMReadWrite.uiNumBytes);
1315                                         Status = STATUS_FAILURE;
1316                                         break;
1317                                 }
1318
1319                                 pReadData = kzalloc(stNVMReadWrite.uiNumBytes, GFP_KERNEL);
1320                                 if(!pReadData)
1321                                         return -ENOMEM;
1322
1323                                 if(copy_from_user(pReadData, stNVMReadWrite.pBuffer,
1324                                                         stNVMReadWrite.uiNumBytes))
1325                                 {
1326                                         Status = -EFAULT;
1327                                         kfree(pReadData);
1328                                         break;
1329                                 }
1330
1331                                 do_gettimeofday(&tv0);
1332                                 if(IOCTL_BCM_NVM_READ == cmd)
1333                                 {
1334                                         down(&Adapter->NVMRdmWrmLock);
1335
1336                                         if((Adapter->IdleMode == TRUE) ||
1337                                                 (Adapter->bShutStatus ==TRUE) ||
1338                                                 (Adapter->bPreparingForLowPowerMode ==TRUE))
1339                                         {
1340                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Device is in Idle/Shutdown Mode\n");
1341                                                 up(&Adapter->NVMRdmWrmLock);
1342                                                 kfree(pReadData);
1343                                                 return -EACCES;
1344                                         }
1345
1346                                         Status = BeceemNVMRead(Adapter, (PUINT)pReadData,
1347                                                 stNVMReadWrite.uiOffset, stNVMReadWrite.uiNumBytes);
1348
1349                                         up(&Adapter->NVMRdmWrmLock);
1350
1351                                         if(Status != STATUS_SUCCESS)
1352                                                 {
1353                                                         kfree(pReadData);
1354                                                         return Status;
1355                                                 }
1356                                         if(copy_to_user(stNVMReadWrite.pBuffer,pReadData, stNVMReadWrite.uiNumBytes))
1357                                                 {
1358                                                         kfree(pReadData);
1359                                                         Status = -EFAULT;
1360                                                 }
1361                                 }
1362                                 else
1363                                 {
1364
1365                                         down(&Adapter->NVMRdmWrmLock);
1366
1367                                         if((Adapter->IdleMode == TRUE) ||
1368                                                 (Adapter->bShutStatus ==TRUE) ||
1369                                                 (Adapter->bPreparingForLowPowerMode ==TRUE))
1370                                         {
1371                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Device is in Idle/Shutdown Mode\n");
1372                                                 up(&Adapter->NVMRdmWrmLock);
1373                                                 kfree(pReadData);
1374                                                 return -EACCES;
1375                                         }
1376
1377                                         Adapter->bHeaderChangeAllowed = TRUE ;
1378                                         if(IsFlash2x(Adapter))
1379                                         {
1380                                                 /*
1381                                                         New Requirement:-
1382                                                         DSD section updation will be allowed in two case:-
1383                                                         1.  if DSD sig is present in DSD header means dongle is ok and updation is fruitfull
1384                                                         2.  if point 1 failes then user buff should have DSD sig. this point ensures that if dongle is
1385                                                               corrupted then user space program first modify the DSD header with valid DSD sig so
1386                                                               that this as well as further write may be worthwhile.
1387
1388                                                          This restriction has been put assuming that if DSD sig is corrupted, DSD
1389                                                          data won't be considered valid.
1390
1391
1392                                                 */
1393                                                 Status = BcmFlash2xCorruptSig(Adapter,Adapter->eActiveDSD);
1394                                                 if(Status != STATUS_SUCCESS)
1395                                                 {
1396                                                         if(( (stNVMReadWrite.uiOffset + stNVMReadWrite.uiNumBytes) != Adapter->uiNVMDSDSize ) ||
1397                                                                 (stNVMReadWrite.uiNumBytes < SIGNATURE_SIZE))
1398                                                         {
1399                                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"DSD Sig is present neither in Flash nor User provided Input..");
1400                                                                 up(&Adapter->NVMRdmWrmLock);
1401                                                                 kfree(pReadData);
1402                                                                 return Status;
1403                                                         }
1404
1405                                                         ulDSDMagicNumInUsrBuff = ntohl(*(PUINT)(pReadData + stNVMReadWrite.uiNumBytes - SIGNATURE_SIZE));
1406                                                         if(ulDSDMagicNumInUsrBuff != DSD_IMAGE_MAGIC_NUMBER)
1407                                                         {
1408                                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"DSD Sig is present neither in Flash nor User provided Input..");
1409                                                                 up(&Adapter->NVMRdmWrmLock);
1410                                                                 kfree(pReadData);
1411                                                                 return Status;
1412                                                         }
1413                                                 }
1414                                         }
1415                                         Status = BeceemNVMWrite(Adapter, (PUINT )pReadData,
1416                                                                         stNVMReadWrite.uiOffset, stNVMReadWrite.uiNumBytes, stNVMReadWrite.bVerify);
1417                                         if(IsFlash2x(Adapter))
1418                                                 BcmFlash2xWriteSig(Adapter,Adapter->eActiveDSD);
1419
1420                                         Adapter->bHeaderChangeAllowed = FALSE ;
1421
1422                                         up(&Adapter->NVMRdmWrmLock);
1423
1424
1425                                         if(Status != STATUS_SUCCESS)
1426                                         {
1427                                                 kfree(pReadData);
1428                                                 return Status;
1429                                         }
1430                                 }
1431                                 do_gettimeofday(&tv1);
1432                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, " timetaken by Write/read :%ld msec\n",(tv1.tv_sec - tv0.tv_sec)*1000 +(tv1.tv_usec - tv0.tv_usec)/1000);
1433
1434
1435                                 kfree(pReadData);
1436                                 Status = STATUS_SUCCESS;
1437                         }
1438                         break;
1439                 case IOCTL_BCM_FLASH2X_SECTION_READ :
1440                          {
1441
1442                                 FLASH2X_READWRITE sFlash2xRead = {0};
1443                                 PUCHAR pReadBuff = NULL ;
1444                                 UINT NOB = 0;
1445                                 UINT BuffSize = 0;
1446                                 UINT ReadBytes = 0;
1447                                 UINT ReadOffset = 0;
1448                                 void __user *OutPutBuff;
1449
1450                                 if(IsFlash2x(Adapter) != TRUE)
1451                                 {
1452                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Flash Does not have 2.x map");
1453                                         return -EINVAL;
1454                                 }
1455
1456                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_FLASH2X_SECTION_READ Called");
1457                                 if (copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1458                                         return -EFAULT;
1459
1460                                 //Reading FLASH 2.x READ structure
1461                                 if (copy_from_user(&sFlash2xRead, IoBuffer.InputBuffer,sizeof(FLASH2X_READWRITE)))
1462                                         return -EFAULT;
1463
1464
1465                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"\nsFlash2xRead.Section :%x" ,sFlash2xRead.Section);
1466                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"\nsFlash2xRead.offset :%x" ,sFlash2xRead.offset);
1467                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"\nsFlash2xRead.numOfBytes :%x" ,sFlash2xRead.numOfBytes);
1468                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"\nsFlash2xRead.bVerify :%x\n" ,sFlash2xRead.bVerify);
1469
1470                                 //This was internal to driver for raw read. now it has ben exposed to user space app.
1471                                 if(validateFlash2xReadWrite(Adapter,&sFlash2xRead) == FALSE)
1472                                         return STATUS_FAILURE ;
1473
1474                                 NOB = sFlash2xRead.numOfBytes;
1475                                 if(NOB > Adapter->uiSectorSize )
1476                                         BuffSize = Adapter->uiSectorSize;
1477                                 else
1478                                         BuffSize = NOB ;
1479
1480                                 ReadOffset = sFlash2xRead.offset ;
1481                                 OutPutBuff = IoBuffer.OutputBuffer;
1482
1483
1484                                 pReadBuff = (PCHAR)kzalloc(BuffSize , GFP_KERNEL);
1485                                 if(pReadBuff == NULL)
1486                                 {
1487                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Memory allocation failed for Flash 2.x Read Structure");
1488                                         return -ENOMEM;
1489                                 }
1490                                 down(&Adapter->NVMRdmWrmLock);
1491
1492                                 if((Adapter->IdleMode == TRUE) ||
1493                                         (Adapter->bShutStatus ==TRUE) ||
1494                                         (Adapter->bPreparingForLowPowerMode ==TRUE))
1495                                 {
1496                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Device is in Idle/Shutdown Mode\n");
1497                                         up(&Adapter->NVMRdmWrmLock);
1498                                         kfree(pReadBuff);
1499                                         return -EACCES;
1500                                 }
1501
1502                                 while(NOB)
1503                                 {
1504
1505                                         if(NOB > Adapter->uiSectorSize )
1506                                                 ReadBytes = Adapter->uiSectorSize;
1507                                         else
1508                                                 ReadBytes = NOB;
1509
1510
1511                                         //Reading the data from Flash 2.x
1512
1513                                         Status = BcmFlash2xBulkRead(Adapter,(PUINT)pReadBuff,sFlash2xRead.Section,ReadOffset,ReadBytes);
1514                                         if(Status)
1515                                         {
1516                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Flash 2x read err with Status :%d", Status);
1517                                                 break ;
1518                                         }
1519
1520                                         BCM_DEBUG_PRINT_BUFFER(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,pReadBuff, ReadBytes);
1521
1522                                         Status = copy_to_user(OutPutBuff, pReadBuff,ReadBytes);
1523                                         if(Status)
1524                                         {
1525                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Copy to use failed with status :%d", Status);
1526                                                 break;
1527                                         }
1528                                         NOB = NOB - ReadBytes;
1529                                         if(NOB)
1530                                         {
1531                                                 ReadOffset = ReadOffset + ReadBytes ;
1532                                                 OutPutBuff = OutPutBuff + ReadBytes ;
1533                                         }
1534
1535                                 }
1536                                 up(&Adapter->NVMRdmWrmLock);
1537                                 kfree(pReadBuff);
1538
1539                          }
1540                          break ;
1541                 case IOCTL_BCM_FLASH2X_SECTION_WRITE :
1542                          {
1543                                 FLASH2X_READWRITE sFlash2xWrite = {0};
1544                                 PUCHAR pWriteBuff;
1545                                 void __user *InputAddr;
1546                                 UINT NOB = 0;
1547                                 UINT BuffSize = 0;
1548                                 UINT WriteOffset = 0;
1549                                 UINT WriteBytes = 0;
1550
1551                                 if(IsFlash2x(Adapter) != TRUE)
1552                                 {
1553                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Flash Does not have 2.x map");
1554                                         return -EINVAL;
1555                                 }
1556
1557                                 //First make this False so that we can enable the Sector Permission Check in BeceemFlashBulkWrite
1558                                 Adapter->bAllDSDWriteAllow = FALSE;
1559
1560
1561                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, " IOCTL_BCM_FLASH2X_SECTION_WRITE Called");
1562                                 if (copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1563                                         return -EFAULT;
1564
1565                                 //Reading FLASH 2.x READ structure
1566                                 if (copy_from_user(&sFlash2xWrite, IoBuffer.InputBuffer, sizeof(FLASH2X_READWRITE)))
1567                                         return -EFAULT;
1568
1569                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"\nsFlash2xRead.Section :%x" ,sFlash2xWrite.Section);
1570                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"\nsFlash2xRead.offset :%d" ,sFlash2xWrite.offset);
1571                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"\nsFlash2xRead.numOfBytes :%x" ,sFlash2xWrite.numOfBytes);
1572                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"\nsFlash2xRead.bVerify :%x\n" ,sFlash2xWrite.bVerify);
1573                                 if((sFlash2xWrite.Section != VSA0) && (sFlash2xWrite.Section != VSA1) &&
1574                                         (sFlash2xWrite.Section != VSA2) )
1575                                 {
1576                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Only VSA write is allowed");
1577                                         return -EINVAL;
1578                                 }
1579
1580                                 if(validateFlash2xReadWrite(Adapter,&sFlash2xWrite) == FALSE)
1581                                         return STATUS_FAILURE ;
1582
1583                                 InputAddr = sFlash2xWrite.pDataBuff;
1584                                 WriteOffset = sFlash2xWrite.offset ;
1585                                 NOB = sFlash2xWrite.numOfBytes;
1586
1587                                 if(NOB > Adapter->uiSectorSize )
1588                                         BuffSize = Adapter->uiSectorSize;
1589                                 else
1590                                         BuffSize = NOB ;
1591
1592                                 pWriteBuff = kmalloc(BuffSize, GFP_KERNEL);
1593                                 if(pWriteBuff == NULL)
1594                                         return -ENOMEM;
1595
1596
1597                                 //extracting the remainder of the given offset.
1598                                 WriteBytes = Adapter->uiSectorSize ;
1599                                 if(WriteOffset % Adapter->uiSectorSize)
1600                                         WriteBytes =Adapter->uiSectorSize - (WriteOffset % Adapter->uiSectorSize);
1601                                 if(NOB < WriteBytes)
1602                                         WriteBytes = NOB;
1603
1604                                 down(&Adapter->NVMRdmWrmLock);
1605
1606                                 if((Adapter->IdleMode == TRUE) ||
1607                                         (Adapter->bShutStatus ==TRUE) ||
1608                                         (Adapter->bPreparingForLowPowerMode ==TRUE))
1609                                 {
1610                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Device is in Idle/Shutdown Mode\n");
1611                                         up(&Adapter->NVMRdmWrmLock);
1612                                         kfree(pWriteBuff);
1613                                         return -EACCES;
1614                                 }
1615
1616                                 BcmFlash2xCorruptSig(Adapter,sFlash2xWrite.Section);
1617                                 do
1618                                 {
1619                                         Status = copy_from_user(pWriteBuff,InputAddr,WriteBytes);
1620                                         if(Status)
1621                                         {
1622                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Copy to user failed with status :%d", Status);
1623                                                 break ;
1624                                         }
1625                                         BCM_DEBUG_PRINT_BUFFER(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,pWriteBuff,WriteBytes);
1626                                         //Writing the data from Flash 2.x
1627                                         Status = BcmFlash2xBulkWrite(Adapter,(PUINT)pWriteBuff,sFlash2xWrite.Section,WriteOffset,WriteBytes,sFlash2xWrite.bVerify);
1628
1629                                         if(Status)
1630                                         {
1631                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Flash 2x read err with Status :%d", Status);
1632                                                 break ;
1633                                         }
1634
1635                                         NOB = NOB - WriteBytes;
1636                                         if(NOB)
1637                                         {
1638                                                 WriteOffset = WriteOffset + WriteBytes ;
1639                                                 InputAddr = InputAddr + WriteBytes ;
1640                                                 if(NOB > Adapter->uiSectorSize )
1641                                                         WriteBytes = Adapter->uiSectorSize;
1642                                                 else
1643                                                         WriteBytes = NOB;
1644                                         }
1645
1646
1647                                 }       while(NOB > 0);
1648                                 BcmFlash2xWriteSig(Adapter,sFlash2xWrite.Section);
1649                                 up(&Adapter->NVMRdmWrmLock);
1650                                 kfree(pWriteBuff);
1651                          }
1652                          break ;
1653                 case IOCTL_BCM_GET_FLASH2X_SECTION_BITMAP :
1654                          {
1655
1656                                  PFLASH2X_BITMAP psFlash2xBitMap;
1657                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_GET_FLASH2X_SECTION_BITMAP Called");
1658
1659                                 if (copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1660                                         return -EFAULT;
1661
1662                                 if(IoBuffer.OutputLength != sizeof(FLASH2X_BITMAP))
1663                                         return -EINVAL;
1664
1665                                 psFlash2xBitMap = kzalloc(sizeof(FLASH2X_BITMAP), GFP_KERNEL);
1666                                 if(psFlash2xBitMap == NULL)
1667                                 {
1668                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Memory is not available");
1669                                         return -ENOMEM ;
1670                                 }
1671                                 //Reading the Flash Sectio Bit map
1672                                 down(&Adapter->NVMRdmWrmLock);
1673
1674                                 if((Adapter->IdleMode == TRUE) ||
1675                                         (Adapter->bShutStatus ==TRUE) ||
1676                                         (Adapter->bPreparingForLowPowerMode ==TRUE))
1677                                 {
1678                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Device is in Idle/Shutdown Mode\n");
1679                                         up(&Adapter->NVMRdmWrmLock);
1680                                         kfree(psFlash2xBitMap);
1681                                         return -EACCES;
1682                                 }
1683
1684                                 BcmGetFlash2xSectionalBitMap(Adapter, psFlash2xBitMap);
1685                                 up(&Adapter->NVMRdmWrmLock);
1686                                 if (copy_to_user(IoBuffer.OutputBuffer, psFlash2xBitMap, sizeof(FLASH2X_BITMAP)))
1687                                         Status = -EFAULT;
1688
1689                                 kfree(psFlash2xBitMap);
1690                          }
1691                          break ;
1692                 case IOCTL_BCM_SET_ACTIVE_SECTION :
1693                          {
1694                                 FLASH2X_SECTION_VAL eFlash2xSectionVal = 0;
1695                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_SET_ACTIVE_SECTION Called");
1696
1697                                 if(IsFlash2x(Adapter) != TRUE)
1698                                 {
1699                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Flash Does not have 2.x map");
1700                                         return -EINVAL;
1701                                 }
1702
1703                                 Status = copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER));
1704                                 if(Status)
1705                                 {
1706                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Copy of IOCTL BUFFER failed");
1707                                         return Status;
1708                                 }
1709
1710                                 Status = copy_from_user(&eFlash2xSectionVal,IoBuffer.InputBuffer, sizeof(INT));
1711                                 if(Status)
1712                                 {
1713                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Copy of flash section val failed");
1714                                         return Status;
1715                                 }
1716
1717                                 down(&Adapter->NVMRdmWrmLock);
1718
1719                                 if((Adapter->IdleMode == TRUE) ||
1720                                         (Adapter->bShutStatus ==TRUE) ||
1721                                         (Adapter->bPreparingForLowPowerMode ==TRUE))
1722                                 {
1723                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Device is in Idle/Shutdown Mode\n");
1724                                         up(&Adapter->NVMRdmWrmLock);
1725                                         return -EACCES;
1726                                 }
1727
1728                                 Status = BcmSetActiveSection(Adapter,eFlash2xSectionVal);
1729                                 if(Status)
1730                                 {
1731                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Failed to make it's priority Highest. Status %d", Status);
1732                                 }
1733                                 up(&Adapter->NVMRdmWrmLock);
1734                         }
1735                         break ;
1736                 case IOCTL_BCM_IDENTIFY_ACTIVE_SECTION :
1737                          {
1738                                 //Right Now we are taking care of only DSD
1739                                 Adapter->bAllDSDWriteAllow = FALSE ;
1740                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"IOCTL_BCM_IDENTIFY_ACTIVE_SECTION called");
1741
1742                                 Status = STATUS_SUCCESS ;
1743                          }
1744                          break ;
1745                 case IOCTL_BCM_COPY_SECTION :
1746                          {
1747                                 FLASH2X_COPY_SECTION sCopySectStrut = {0};
1748                                 Status = STATUS_SUCCESS;
1749                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_COPY_SECTION  Called");
1750
1751                                 Adapter->bAllDSDWriteAllow = FALSE ;
1752                                 if(IsFlash2x(Adapter) != TRUE)
1753                                 {
1754                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Flash Does not have 2.x map");
1755                                         return -EINVAL;
1756                                 }
1757
1758                                 Status = copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER));
1759                                 if(Status)
1760                                 {
1761                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Copy of IOCTL BUFFER failed Status :%d", Status);
1762                                         return Status;
1763                                 }
1764
1765                                 Status = copy_from_user(&sCopySectStrut, IoBuffer.InputBuffer, sizeof(FLASH2X_COPY_SECTION));
1766                                 if(Status)
1767                                 {
1768                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Copy of Copy_Section_Struct failed with Status :%d", Status);
1769                                         return Status;
1770                                 }
1771                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Source SEction :%x", sCopySectStrut.SrcSection);
1772                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Destination SEction :%x", sCopySectStrut.DstSection);
1773                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "offset :%x", sCopySectStrut.offset);
1774                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "NOB :%x", sCopySectStrut.numOfBytes);
1775
1776
1777                                 if(IsSectionExistInFlash(Adapter,sCopySectStrut.SrcSection) == FALSE)
1778                                 {
1779                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Source Section<%x> does not exixt in Flash ", sCopySectStrut.SrcSection);
1780                                         return -EINVAL;
1781                                 }
1782
1783                                 if(IsSectionExistInFlash(Adapter,sCopySectStrut.DstSection) == FALSE)
1784                                 {
1785                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Destinatio Section<%x> does not exixt in Flash ", sCopySectStrut.DstSection);
1786                                         return -EINVAL;
1787                                 }
1788
1789                                 if(sCopySectStrut.SrcSection == sCopySectStrut.DstSection)
1790                                 {
1791                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Source and Destination section should be different");
1792                                         return -EINVAL;
1793                                 }
1794
1795                                 down(&Adapter->NVMRdmWrmLock);
1796
1797                                 if((Adapter->IdleMode == TRUE) ||
1798                                         (Adapter->bShutStatus ==TRUE) ||
1799                                         (Adapter->bPreparingForLowPowerMode ==TRUE))
1800                                 {
1801                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Device is in Idle/Shutdown Mode\n");
1802                                         up(&Adapter->NVMRdmWrmLock);
1803                                         return -EACCES;
1804                                 }
1805
1806                                 if(sCopySectStrut.SrcSection == ISO_IMAGE1 || sCopySectStrut.SrcSection == ISO_IMAGE2)
1807                                 {
1808                                         if(IsNonCDLessDevice(Adapter))
1809                                         {
1810                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Device is Non-CDLess hence won't have ISO !!");
1811                                                 Status = -EINVAL ;
1812                                         }
1813                                         else if(sCopySectStrut.numOfBytes == 0)
1814                                         {
1815                                                 Status = BcmCopyISO(Adapter,sCopySectStrut);
1816                                         }
1817                                         else
1818                                         {
1819                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Partial Copy of ISO section is not Allowed..");
1820                                                 Status = STATUS_FAILURE ;
1821                                         }
1822                                         up(&Adapter->NVMRdmWrmLock);
1823                                         return Status;
1824                                 }
1825
1826                                 Status = BcmCopySection(Adapter, sCopySectStrut.SrcSection,
1827                                                         sCopySectStrut.DstSection,sCopySectStrut.offset,sCopySectStrut.numOfBytes);
1828                                 up(&Adapter->NVMRdmWrmLock);
1829                          }
1830                          break ;
1831                 case IOCTL_BCM_GET_FLASH_CS_INFO :
1832                          {
1833                                 Status = STATUS_SUCCESS;
1834                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, " IOCTL_BCM_GET_FLASH_CS_INFO Called");
1835
1836                                 Status = copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER));
1837                                 if(Status)
1838                                 {
1839                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Copy of IOCTL BUFFER failed");
1840                                         break;
1841                                 }
1842                                 if(Adapter->eNVMType != NVM_FLASH)
1843                                 {
1844                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Connected device does not have flash");
1845                                         Status = -EINVAL;
1846                                         break;
1847                                 }
1848                                 if(IsFlash2x(Adapter) == TRUE)
1849                                 {
1850
1851                                         if(IoBuffer.OutputLength < sizeof(FLASH2X_CS_INFO))
1852                                                 return -EINVAL;
1853
1854                                         if (copy_to_user(IoBuffer.OutputBuffer, Adapter->psFlash2xCSInfo, sizeof(FLASH2X_CS_INFO)))
1855                                                 return -EFAULT;
1856                                 }
1857                                 else
1858                                 {
1859                                         if(IoBuffer.OutputLength < sizeof(FLASH_CS_INFO))
1860                                                 return -EINVAL;
1861
1862                                         if (copy_to_user(IoBuffer.OutputBuffer, Adapter->psFlashCSInfo, sizeof(FLASH_CS_INFO)))
1863                                                 return -EFAULT;
1864
1865                                  }
1866                           }
1867                           break ;
1868                 case IOCTL_BCM_SELECT_DSD :
1869                          {
1870                                 UINT SectOfset = 0;
1871                                 FLASH2X_SECTION_VAL eFlash2xSectionVal;
1872                                 eFlash2xSectionVal = NO_SECTION_VAL ;
1873                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, " IOCTL_BCM_SELECT_DSD Called");
1874
1875                                 if(IsFlash2x(Adapter) != TRUE)
1876                                 {
1877                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Flash Does not have 2.x map");
1878                                         return -EINVAL;
1879                                 }
1880
1881                                 Status = copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER));
1882                                 if(Status)
1883                                 {
1884                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Copy of IOCTL BUFFER failed");
1885                                         return Status;
1886                                 }
1887                                 Status = copy_from_user(&eFlash2xSectionVal, IoBuffer.InputBuffer, sizeof(INT));
1888                                 if(Status)
1889                                 {
1890                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Copy of flash section val failed");
1891                                         return Status;
1892                                 }
1893
1894                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Read Section :%d", eFlash2xSectionVal);
1895                                 if((eFlash2xSectionVal != DSD0) &&
1896                                         (eFlash2xSectionVal != DSD1) &&
1897                                         (eFlash2xSectionVal != DSD2) )
1898                                 {
1899                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Passed section<%x> is not DSD section", eFlash2xSectionVal);
1900                                         return STATUS_FAILURE ;
1901                                 }
1902
1903                                 SectOfset= BcmGetSectionValStartOffset(Adapter,eFlash2xSectionVal);
1904                                 if(SectOfset == INVALID_OFFSET)
1905                                 {
1906                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Provided Section val <%d> does not exixt in Flash 2.x", eFlash2xSectionVal);
1907                                         return -EINVAL;
1908                                 }
1909
1910                                 Adapter->bAllDSDWriteAllow = TRUE ;
1911
1912                                 Adapter->ulFlashCalStart = SectOfset ;
1913                                 Adapter->eActiveDSD = eFlash2xSectionVal;
1914                          }
1915                          Status = STATUS_SUCCESS ;
1916                          break;
1917
1918                 case IOCTL_BCM_NVM_RAW_READ :
1919                          {
1920
1921                                  NVM_READWRITE stNVMRead;
1922                                 INT NOB ;
1923                                 INT BuffSize ;
1924                                 INT ReadOffset = 0;
1925                                 UINT ReadBytes = 0 ;
1926                                 PUCHAR pReadBuff;
1927                                 void __user *OutPutBuff;
1928
1929                                 if(Adapter->eNVMType != NVM_FLASH)
1930                                 {
1931                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"NVM TYPE is not Flash ");
1932                                         return -EINVAL ;
1933                                 }
1934
1935                                 /* Copy Ioctl Buffer structure */
1936                                 if(copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
1937                                 {
1938                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "copy_from_user 1 failed\n");
1939                                         Status = -EFAULT;
1940                                         break;
1941                                 }
1942
1943                                 if(copy_from_user(&stNVMRead, IoBuffer.OutputBuffer,sizeof(NVM_READWRITE)))
1944                                         return -EFAULT;
1945
1946                                 NOB = stNVMRead.uiNumBytes;
1947                                 //In Raw-Read max Buff size : 64MB
1948
1949                                 if(NOB > DEFAULT_BUFF_SIZE)
1950                                         BuffSize = DEFAULT_BUFF_SIZE;
1951                                 else
1952                                         BuffSize = NOB ;
1953
1954                                 ReadOffset = stNVMRead.uiOffset;
1955                                 OutPutBuff = stNVMRead.pBuffer;
1956
1957                                 pReadBuff = kzalloc(BuffSize , GFP_KERNEL);
1958                                 if(pReadBuff == NULL)
1959                                 {
1960                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Memory allocation failed for Flash 2.x Read Structure");
1961                                         Status = -ENOMEM;
1962                                         break;
1963                                 }
1964                                 down(&Adapter->NVMRdmWrmLock);
1965
1966                                 if((Adapter->IdleMode == TRUE) ||
1967                                         (Adapter->bShutStatus ==TRUE) ||
1968                                         (Adapter->bPreparingForLowPowerMode ==TRUE))
1969                                 {
1970                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Device is in Idle/Shutdown Mode\n");
1971                                         kfree(pReadBuff);
1972                                         up(&Adapter->NVMRdmWrmLock);
1973                                         return -EACCES;
1974                                 }
1975
1976                                 Adapter->bFlashRawRead = TRUE ;
1977                                 while(NOB)
1978                                 {
1979                                         if(NOB > DEFAULT_BUFF_SIZE )
1980                                                 ReadBytes = DEFAULT_BUFF_SIZE;
1981                                         else
1982                                                 ReadBytes = NOB;
1983
1984                                         //Reading the data from Flash 2.x
1985                                         Status = BeceemNVMRead(Adapter,(PUINT)pReadBuff,ReadOffset,ReadBytes);
1986                                         if(Status)
1987                                         {
1988                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Flash 2x read err with Status :%d", Status);
1989                                                 break;
1990                                         }
1991
1992                                         BCM_DEBUG_PRINT_BUFFER(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,pReadBuff,ReadBytes);
1993
1994                                         Status = copy_to_user(OutPutBuff, pReadBuff,ReadBytes);
1995                                         if(Status)
1996                                         {
1997                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Copy to use failed with status :%d", Status);
1998                                                 break;
1999                                         }
2000                                         NOB = NOB - ReadBytes;
2001                                         if(NOB)
2002                                         {
2003                                                 ReadOffset = ReadOffset + ReadBytes ;
2004                                                 OutPutBuff = OutPutBuff + ReadBytes ;
2005                                         }
2006
2007                                 }
2008                                 Adapter->bFlashRawRead = FALSE ;
2009                                 up(&Adapter->NVMRdmWrmLock);
2010                                 kfree(pReadBuff);
2011                                 break ;
2012                          }
2013
2014                 case IOCTL_BCM_CNTRLMSG_MASK:
2015                          {
2016                                 ULONG RxCntrlMsgBitMask = 0 ;
2017
2018                                 /* Copy Ioctl Buffer structure */
2019                                 Status = copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER));
2020                                 if(Status)
2021                                 {
2022                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"copy of Ioctl buffer is failed from user space");
2023                                         Status = -EFAULT;
2024                                         break;
2025                                 }
2026
2027                                 if (IoBuffer.InputLength != sizeof(unsigned long)) {
2028                                         Status = -EINVAL;
2029                                         break;
2030                                 }
2031
2032                                 Status = copy_from_user(&RxCntrlMsgBitMask, IoBuffer.InputBuffer, IoBuffer.InputLength);
2033                                 if(Status)
2034                                 {
2035                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"copy of control bit mask failed from user space");
2036                                         Status = -EFAULT;
2037                                         break;
2038                                 }
2039                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"\n Got user defined cntrl msg bit mask :%lx", RxCntrlMsgBitMask);
2040                                 pTarang->RxCntrlMsgBitMask = RxCntrlMsgBitMask ;
2041                          }
2042                          break;
2043                         case IOCTL_BCM_GET_DEVICE_DRIVER_INFO:
2044                         {
2045                                 DEVICE_DRIVER_INFO DevInfo;
2046
2047                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"Called IOCTL_BCM_GET_DEVICE_DRIVER_INFO\n");
2048
2049                                 DevInfo.MaxRDMBufferSize = BUFFER_4K;
2050                                 DevInfo.u32DSDStartOffset = EEPROM_CALPARAM_START;
2051                                 DevInfo.u32RxAlignmentCorrection = 0;
2052                                 DevInfo.u32NVMType = Adapter->eNVMType;
2053                                 DevInfo.u32InterfaceType = BCM_USB;
2054
2055                                 if (copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
2056                                         return -EFAULT;
2057
2058                                 if(IoBuffer.OutputLength < sizeof(DevInfo))
2059                                         return -EINVAL;
2060
2061                                 if (copy_to_user(IoBuffer.OutputBuffer, &DevInfo, sizeof(DevInfo)))
2062                                         return -EFAULT;
2063                         }
2064                         break ;
2065
2066                         case IOCTL_BCM_TIME_SINCE_NET_ENTRY:
2067                         {
2068                                 ST_TIME_ELAPSED stTimeElapsedSinceNetEntry = {0};
2069
2070                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"IOCTL_BCM_TIME_SINCE_NET_ENTRY called");
2071
2072                                 if (copy_from_user(&IoBuffer, argp, sizeof(IOCTL_BUFFER)))
2073                                         return -EFAULT;
2074
2075                                 if(IoBuffer.OutputLength < sizeof(ST_TIME_ELAPSED))
2076                                         return -EINVAL;
2077
2078                                 stTimeElapsedSinceNetEntry.ul64TimeElapsedSinceNetEntry = get_seconds() - Adapter->liTimeSinceLastNetEntry;
2079
2080                                 if (copy_to_user(IoBuffer.OutputBuffer, &stTimeElapsedSinceNetEntry, sizeof(ST_TIME_ELAPSED)))
2081                                         return -EFAULT;
2082
2083                         }
2084                         break;
2085
2086                 case IOCTL_CLOSE_NOTIFICATION:
2087                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,"IOCTL_CLOSE_NOTIFICATION");
2088                         break;
2089
2090                 default:
2091                         pr_info(DRV_NAME ": unknown ioctl cmd=%#x\n", cmd);
2092                         Status = STATUS_FAILURE;
2093                         break;
2094         }
2095         return Status;
2096 }
2097
2098
2099 static struct file_operations bcm_fops = {
2100         .owner    = THIS_MODULE,
2101         .open     = bcm_char_open,
2102         .release  = bcm_char_release,
2103         .read     = bcm_char_read,
2104         .unlocked_ioctl    = bcm_char_ioctl,
2105         .llseek = no_llseek,
2106 };
2107
2108 extern struct class *bcm_class;
2109
2110 int register_control_device_interface(PMINI_ADAPTER Adapter)
2111 {
2112
2113         if(Adapter->major>0)
2114                 return Adapter->major;
2115
2116         Adapter->major = register_chrdev(0, DEV_NAME, &bcm_fops);
2117         if(Adapter->major < 0) {
2118                 pr_err(DRV_NAME ": could not created character device\n");
2119                 return Adapter->major;
2120         }
2121
2122         Adapter->pstCreatedClassDevice = device_create (bcm_class, NULL,
2123                                                         MKDEV(Adapter->major, 0), Adapter,
2124                                                         DEV_NAME);
2125
2126         if(IS_ERR(Adapter->pstCreatedClassDevice)) {
2127                 pr_err(DRV_NAME ": class device create failed\n");
2128                 unregister_chrdev(Adapter->major, DEV_NAME);
2129                 return PTR_ERR(Adapter->pstCreatedClassDevice);
2130         }
2131                         
2132         return 0;
2133 }
2134
2135 void unregister_control_device_interface(PMINI_ADAPTER Adapter)
2136 {
2137         if(Adapter->major > 0) {
2138                 device_destroy (bcm_class, MKDEV(Adapter->major, 0));
2139                 unregister_chrdev(Adapter->major, DEV_NAME);
2140         }
2141 }