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