staging/bcm: move several request ioctl cases out to its own function.
[sfrench/cifs-2.6.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 int bcm_char_ioctl_misc_request(void __user *argp, struct bcm_mini_adapter *Adapter)
743 {
744         struct bcm_ioctl_buffer IoBuffer;
745         PVOID pvBuffer = NULL;
746         INT Status;
747
748         /* Copy Ioctl Buffer structure */
749         if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
750                 return -EFAULT;
751
752         if (IoBuffer.InputLength < sizeof(struct bcm_link_request))
753                 return -EINVAL;
754
755         if (IoBuffer.InputLength > MAX_CNTL_PKT_SIZE)
756                 return -EINVAL;
757
758         pvBuffer = memdup_user(IoBuffer.InputBuffer,
759                                IoBuffer.InputLength);
760         if (IS_ERR(pvBuffer))
761                 return PTR_ERR(pvBuffer);
762
763         down(&Adapter->LowPowerModeSync);
764         Status = wait_event_interruptible_timeout(Adapter->lowpower_mode_wait_queue,
765                                                 !Adapter->bPreparingForLowPowerMode,
766                                                 (1 * HZ));
767         if (Status == -ERESTARTSYS)
768                 goto cntrlEnd;
769
770         if (Adapter->bPreparingForLowPowerMode) {
771                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,
772                                 "Preparing Idle Mode is still True - Hence Rejecting control message\n");
773                 Status = STATUS_FAILURE;
774                 goto cntrlEnd;
775         }
776         Status = CopyBufferToControlPacket(Adapter, (PVOID)pvBuffer);
777
778 cntrlEnd:
779         up(&Adapter->LowPowerModeSync);
780         kfree(pvBuffer);
781         return Status;
782 }
783
784
785 static long bcm_char_ioctl(struct file *filp, UINT cmd, ULONG arg)
786 {
787         struct bcm_tarang_data *pTarang = filp->private_data;
788         void __user *argp = (void __user *)arg;
789         struct bcm_mini_adapter *Adapter = pTarang->Adapter;
790         INT Status = STATUS_FAILURE;
791         int timeout = 0;
792         struct bcm_ioctl_buffer IoBuffer;
793
794         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,
795                         "Parameters Passed to control IOCTL cmd=0x%X arg=0x%lX",
796                         cmd, arg);
797
798         if (_IOC_TYPE(cmd) != BCM_IOCTL)
799                 return -EFAULT;
800         if (_IOC_DIR(cmd) & _IOC_READ)
801                 Status = !access_ok(VERIFY_WRITE, argp, _IOC_SIZE(cmd));
802         else if (_IOC_DIR(cmd) & _IOC_WRITE)
803                 Status = !access_ok(VERIFY_READ, argp, _IOC_SIZE(cmd));
804         else if (_IOC_NONE == (_IOC_DIR(cmd) & _IOC_NONE))
805                 Status = STATUS_SUCCESS;
806
807         if (Status)
808                 return -EFAULT;
809
810         if (Adapter->device_removed)
811                 return -EFAULT;
812
813         if (false == Adapter->fw_download_done) {
814                 switch (cmd) {
815                 case IOCTL_MAC_ADDR_REQ:
816                 case IOCTL_LINK_REQ:
817                 case IOCTL_CM_REQUEST:
818                 case IOCTL_SS_INFO_REQ:
819                 case IOCTL_SEND_CONTROL_MESSAGE:
820                 case IOCTL_IDLE_REQ:
821                 case IOCTL_BCM_GPIO_SET_REQUEST:
822                 case IOCTL_BCM_GPIO_STATUS_REQUEST:
823                         return -EACCES;
824                 default:
825                         break;
826                 }
827         }
828
829         Status = vendorextnIoctl(Adapter, cmd, arg);
830         if (Status != CONTINUE_COMMON_PATH)
831                 return Status;
832
833         switch (cmd) {
834         /* Rdms for Swin Idle... */
835         case IOCTL_BCM_REGISTER_READ_PRIVATE:
836                 Status = bcm_char_ioctl_reg_read_private(argp, Adapter);
837                 return Status;
838
839         case IOCTL_BCM_REGISTER_WRITE_PRIVATE:
840                 Status = bcm_char_ioctl_reg_write_private(argp, Adapter);
841                 return Status;
842
843         case IOCTL_BCM_REGISTER_READ:
844         case IOCTL_BCM_EEPROM_REGISTER_READ:
845                 Status = bcm_char_ioctl_eeprom_reg_read(argp, Adapter);
846                 return Status;
847
848         case IOCTL_BCM_REGISTER_WRITE:
849         case IOCTL_BCM_EEPROM_REGISTER_WRITE:
850                 Status = bcm_char_ioctl_eeprom_reg_write(argp, Adapter, cmd);
851                 return Status;
852
853         case IOCTL_BCM_GPIO_SET_REQUEST:
854                 Status = bcm_char_ioctl_gpio_set_request(argp, Adapter);
855                 return Status;
856
857         case BCM_LED_THREAD_STATE_CHANGE_REQ:
858                 Status = bcm_char_ioctl_led_thread_state_change_req(argp, Adapter);
859                 return Status;
860
861         case IOCTL_BCM_GPIO_STATUS_REQUEST:
862                 Status = bcm_char_ioctl_gpio_status_request(argp, Adapter);
863                 return Status;
864
865         case IOCTL_BCM_GPIO_MULTI_REQUEST:
866                 Status = bcm_char_ioctl_gpio_multi_request(argp, Adapter);
867                 return Status;
868
869         case IOCTL_BCM_GPIO_MODE_REQUEST:
870                 Status = bcm_char_ioctl_gpio_mode_request(argp, Adapter);
871                 return Status;
872
873         case IOCTL_MAC_ADDR_REQ:
874         case IOCTL_LINK_REQ:
875         case IOCTL_CM_REQUEST:
876         case IOCTL_SS_INFO_REQ:
877         case IOCTL_SEND_CONTROL_MESSAGE:
878         case IOCTL_IDLE_REQ:
879                 Status = bcm_char_ioctl_misc_request(argp, Adapter);
880                 return Status;
881
882         case IOCTL_BCM_BUFFER_DOWNLOAD_START: {
883                 if (down_trylock(&Adapter->NVMRdmWrmLock)) {
884                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL,
885                                         "IOCTL_BCM_CHIP_RESET not allowed as EEPROM Read/Write is in progress\n");
886                         return -EACCES;
887                 }
888
889                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0,
890                                 "Starting the firmware download PID =0x%x!!!!\n", current->pid);
891
892                 if (down_trylock(&Adapter->fw_download_sema))
893                         return -EBUSY;
894
895                 Adapter->bBinDownloaded = false;
896                 Adapter->fw_download_process_pid = current->pid;
897                 Adapter->bCfgDownloaded = false;
898                 Adapter->fw_download_done = false;
899                 netif_carrier_off(Adapter->dev);
900                 netif_stop_queue(Adapter->dev);
901                 Status = reset_card_proc(Adapter);
902                 if (Status) {
903                         pr_err(PFX "%s: reset_card_proc Failed!\n", Adapter->dev->name);
904                         up(&Adapter->fw_download_sema);
905                         up(&Adapter->NVMRdmWrmLock);
906                         return Status;
907                 }
908                 mdelay(10);
909
910                 up(&Adapter->NVMRdmWrmLock);
911                 return Status;
912         }
913
914         case IOCTL_BCM_BUFFER_DOWNLOAD: {
915                 struct bcm_firmware_info *psFwInfo = NULL;
916                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Starting the firmware download PID =0x%x!!!!\n", current->pid);
917
918                 if (!down_trylock(&Adapter->fw_download_sema)) {
919                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0,
920                                         "Invalid way to download buffer. Use Start and then call this!!!\n");
921                         up(&Adapter->fw_download_sema);
922                         Status = -EINVAL;
923                         return Status;
924                 }
925
926                 /* Copy Ioctl Buffer structure */
927                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer))) {
928                         up(&Adapter->fw_download_sema);
929                         return -EFAULT;
930                 }
931
932                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0,
933                                 "Length for FW DLD is : %lx\n", IoBuffer.InputLength);
934
935                 if (IoBuffer.InputLength > sizeof(struct bcm_firmware_info)) {
936                         up(&Adapter->fw_download_sema);
937                         return -EINVAL;
938                 }
939
940                 psFwInfo = kmalloc(sizeof(*psFwInfo), GFP_KERNEL);
941                 if (!psFwInfo) {
942                         up(&Adapter->fw_download_sema);
943                         return -ENOMEM;
944                 }
945
946                 if (copy_from_user(psFwInfo, IoBuffer.InputBuffer, IoBuffer.InputLength)) {
947                         up(&Adapter->fw_download_sema);
948                         kfree(psFwInfo);
949                         return -EFAULT;
950                 }
951
952                 if (!psFwInfo->pvMappedFirmwareAddress ||
953                         (psFwInfo->u32FirmwareLength == 0)) {
954
955                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Something else is wrong %lu\n",
956                                         psFwInfo->u32FirmwareLength);
957                         up(&Adapter->fw_download_sema);
958                         kfree(psFwInfo);
959                         Status = -EINVAL;
960                         return Status;
961                 }
962
963                 Status = bcm_ioctl_fw_download(Adapter, psFwInfo);
964
965                 if (Status != STATUS_SUCCESS) {
966                         if (psFwInfo->u32StartingAddress == CONFIG_BEGIN_ADDR)
967                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "IOCTL: Configuration File Upload Failed\n");
968                         else
969                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "IOCTL: Firmware File Upload Failed\n");
970
971                         /* up(&Adapter->fw_download_sema); */
972
973                         if (Adapter->LEDInfo.led_thread_running & BCM_LED_THREAD_RUNNING_ACTIVELY) {
974                                 Adapter->DriverState = DRIVER_INIT;
975                                 Adapter->LEDInfo.bLedInitDone = false;
976                                 wake_up(&Adapter->LEDInfo.notify_led_event);
977                         }
978                 }
979
980                 if (Status != STATUS_SUCCESS)
981                         up(&Adapter->fw_download_sema);
982
983                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, OSAL_DBG, DBG_LVL_ALL, "IOCTL: Firmware File Uploaded\n");
984                 kfree(psFwInfo);
985                 return Status;
986         }
987
988         case IOCTL_BCM_BUFFER_DOWNLOAD_STOP: {
989                 if (!down_trylock(&Adapter->fw_download_sema)) {
990                         up(&Adapter->fw_download_sema);
991                         return -EINVAL;
992                 }
993
994                 if (down_trylock(&Adapter->NVMRdmWrmLock)) {
995                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0,
996                                         "FW download blocked as EEPROM Read/Write is in progress\n");
997                         up(&Adapter->fw_download_sema);
998                         return -EACCES;
999                 }
1000
1001                 Adapter->bBinDownloaded = TRUE;
1002                 Adapter->bCfgDownloaded = TRUE;
1003                 atomic_set(&Adapter->CurrNumFreeTxDesc, 0);
1004                 Adapter->CurrNumRecvDescs = 0;
1005                 Adapter->downloadDDR = 0;
1006
1007                 /* setting the Mips to Run */
1008                 Status = run_card_proc(Adapter);
1009
1010                 if (Status) {
1011                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Firm Download Failed\n");
1012                         up(&Adapter->fw_download_sema);
1013                         up(&Adapter->NVMRdmWrmLock);
1014                         return Status;
1015                 } else {
1016                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG,
1017                                         DBG_LVL_ALL, "Firm Download Over...\n");
1018                 }
1019
1020                 mdelay(10);
1021
1022                 /* Wait for MailBox Interrupt */
1023                 if (StartInterruptUrb((struct bcm_interface_adapter *)Adapter->pvInterfaceAdapter))
1024                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Unable to send interrupt...\n");
1025
1026                 timeout = 5*HZ;
1027                 Adapter->waiting_to_fw_download_done = false;
1028                 wait_event_timeout(Adapter->ioctl_fw_dnld_wait_queue,
1029                                 Adapter->waiting_to_fw_download_done, timeout);
1030                 Adapter->fw_download_process_pid = INVALID_PID;
1031                 Adapter->fw_download_done = TRUE;
1032                 atomic_set(&Adapter->CurrNumFreeTxDesc, 0);
1033                 Adapter->CurrNumRecvDescs = 0;
1034                 Adapter->PrevNumRecvDescs = 0;
1035                 atomic_set(&Adapter->cntrlpktCnt, 0);
1036                 Adapter->LinkUpStatus = 0;
1037                 Adapter->LinkStatus = 0;
1038
1039                 if (Adapter->LEDInfo.led_thread_running & BCM_LED_THREAD_RUNNING_ACTIVELY) {
1040                         Adapter->DriverState = FW_DOWNLOAD_DONE;
1041                         wake_up(&Adapter->LEDInfo.notify_led_event);
1042                 }
1043
1044                 if (!timeout)
1045                         Status = -ENODEV;
1046
1047                 up(&Adapter->fw_download_sema);
1048                 up(&Adapter->NVMRdmWrmLock);
1049                 return Status;
1050         }
1051
1052         case IOCTL_BE_BUCKET_SIZE:
1053                 Status = 0;
1054                 if (get_user(Adapter->BEBucketSize, (unsigned long __user *)arg))
1055                         Status = -EFAULT;
1056                 break;
1057
1058         case IOCTL_RTPS_BUCKET_SIZE:
1059                 Status = 0;
1060                 if (get_user(Adapter->rtPSBucketSize, (unsigned long __user *)arg))
1061                         Status = -EFAULT;
1062                 break;
1063
1064         case IOCTL_CHIP_RESET: {
1065                 INT NVMAccess = down_trylock(&Adapter->NVMRdmWrmLock);
1066                 if (NVMAccess) {
1067                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, " IOCTL_BCM_CHIP_RESET not allowed as EEPROM Read/Write is in progress\n");
1068                         return -EACCES;
1069                 }
1070
1071                 down(&Adapter->RxAppControlQueuelock);
1072                 Status = reset_card_proc(Adapter);
1073                 flushAllAppQ();
1074                 up(&Adapter->RxAppControlQueuelock);
1075                 up(&Adapter->NVMRdmWrmLock);
1076                 ResetCounters(Adapter);
1077                 break;
1078         }
1079
1080         case IOCTL_QOS_THRESHOLD: {
1081                 USHORT uiLoopIndex;
1082
1083                 Status = 0;
1084                 for (uiLoopIndex = 0; uiLoopIndex < NO_OF_QUEUES; uiLoopIndex++) {
1085                         if (get_user(Adapter->PackInfo[uiLoopIndex].uiThreshold,
1086                                         (unsigned long __user *)arg)) {
1087                                 Status = -EFAULT;
1088                                 break;
1089                         }
1090                 }
1091                 break;
1092         }
1093
1094         case IOCTL_DUMP_PACKET_INFO:
1095                 DumpPackInfo(Adapter);
1096                 DumpPhsRules(&Adapter->stBCMPhsContext);
1097                 Status = STATUS_SUCCESS;
1098                 break;
1099
1100         case IOCTL_GET_PACK_INFO:
1101                 if (copy_to_user(argp, &Adapter->PackInfo, sizeof(struct bcm_packet_info)*NO_OF_QUEUES))
1102                         return -EFAULT;
1103                 Status = STATUS_SUCCESS;
1104                 break;
1105
1106         case IOCTL_BCM_SWITCH_TRANSFER_MODE: {
1107                 UINT uiData = 0;
1108                 if (copy_from_user(&uiData, argp, sizeof(UINT)))
1109                         return -EFAULT;
1110
1111                 if (uiData) {
1112                         /* Allow All Packets */
1113                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_SWITCH_TRANSFER_MODE: ETH_PACKET_TUNNELING_MODE\n");
1114                                 Adapter->TransferMode = ETH_PACKET_TUNNELING_MODE;
1115                 } else {
1116                         /* Allow IP only Packets */
1117                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_SWITCH_TRANSFER_MODE: IP_PACKET_ONLY_MODE\n");
1118                         Adapter->TransferMode = IP_PACKET_ONLY_MODE;
1119                 }
1120                 Status = STATUS_SUCCESS;
1121                 break;
1122         }
1123
1124         case IOCTL_BCM_GET_DRIVER_VERSION: {
1125                 ulong len;
1126
1127                 /* Copy Ioctl Buffer structure */
1128                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1129                         return -EFAULT;
1130
1131                 len = min_t(ulong, IoBuffer.OutputLength, strlen(DRV_VERSION) + 1);
1132
1133                 if (copy_to_user(IoBuffer.OutputBuffer, DRV_VERSION, len))
1134                         return -EFAULT;
1135                 Status = STATUS_SUCCESS;
1136                 break;
1137         }
1138
1139         case IOCTL_BCM_GET_CURRENT_STATUS: {
1140                 struct bcm_link_state link_state;
1141
1142                 /* Copy Ioctl Buffer structure */
1143                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer))) {
1144                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "copy_from_user failed..\n");
1145                         return -EFAULT;
1146                 }
1147
1148                 if (IoBuffer.OutputLength != sizeof(link_state)) {
1149                         Status = -EINVAL;
1150                         break;
1151                 }
1152
1153                 memset(&link_state, 0, sizeof(link_state));
1154                 link_state.bIdleMode = Adapter->IdleMode;
1155                 link_state.bShutdownMode = Adapter->bShutStatus;
1156                 link_state.ucLinkStatus = Adapter->LinkStatus;
1157
1158                 if (copy_to_user(IoBuffer.OutputBuffer, &link_state, min_t(size_t, sizeof(link_state), IoBuffer.OutputLength))) {
1159                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Copy_to_user Failed..\n");
1160                         return -EFAULT;
1161                 }
1162                 Status = STATUS_SUCCESS;
1163                 break;
1164         }
1165
1166         case IOCTL_BCM_SET_MAC_TRACING: {
1167                 UINT  tracing_flag;
1168
1169                 /* copy ioctl Buffer structure */
1170                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1171                         return -EFAULT;
1172
1173                 if (copy_from_user(&tracing_flag, IoBuffer.InputBuffer, sizeof(UINT)))
1174                         return -EFAULT;
1175
1176                 if (tracing_flag)
1177                         Adapter->pTarangs->MacTracingEnabled = TRUE;
1178                 else
1179                         Adapter->pTarangs->MacTracingEnabled = false;
1180                 break;
1181         }
1182
1183         case IOCTL_BCM_GET_DSX_INDICATION: {
1184                 ULONG ulSFId = 0;
1185                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1186                         return -EFAULT;
1187
1188                 if (IoBuffer.OutputLength < sizeof(struct bcm_add_indication_alt)) {
1189                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0,
1190                                         "Mismatch req: %lx needed is =0x%zx!!!",
1191                                         IoBuffer.OutputLength, sizeof(struct bcm_add_indication_alt));
1192                         return -EINVAL;
1193                 }
1194
1195                 if (copy_from_user(&ulSFId, IoBuffer.InputBuffer, sizeof(ulSFId)))
1196                         return -EFAULT;
1197
1198                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Get DSX Data SF ID is =%lx\n", ulSFId);
1199                 get_dsx_sf_data_to_application(Adapter, ulSFId, IoBuffer.OutputBuffer);
1200                 Status = STATUS_SUCCESS;
1201         }
1202         break;
1203
1204         case IOCTL_BCM_GET_HOST_MIBS: {
1205                 PVOID temp_buff;
1206
1207                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1208                         return -EFAULT;
1209
1210                 if (IoBuffer.OutputLength != sizeof(struct bcm_host_stats_mibs)) {
1211                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0,
1212                                         "Length Check failed %lu %zd\n",
1213                                         IoBuffer.OutputLength, sizeof(struct bcm_host_stats_mibs));
1214                         return -EINVAL;
1215                 }
1216
1217                 /* FIXME: HOST_STATS are too big for kmalloc (122048)! */
1218                 temp_buff = kzalloc(sizeof(struct bcm_host_stats_mibs), GFP_KERNEL);
1219                 if (!temp_buff)
1220                         return STATUS_FAILURE;
1221
1222                 Status = ProcessGetHostMibs(Adapter, temp_buff);
1223                 GetDroppedAppCntrlPktMibs(temp_buff, pTarang);
1224
1225                 if (Status != STATUS_FAILURE)
1226                         if (copy_to_user(IoBuffer.OutputBuffer, temp_buff, sizeof(struct bcm_host_stats_mibs))) {
1227                                 kfree(temp_buff);
1228                                 return -EFAULT;
1229                         }
1230
1231                 kfree(temp_buff);
1232                 break;
1233         }
1234
1235         case IOCTL_BCM_WAKE_UP_DEVICE_FROM_IDLE:
1236                 if ((false == Adapter->bTriedToWakeUpFromlowPowerMode) && (TRUE == Adapter->IdleMode)) {
1237                         Adapter->usIdleModePattern = ABORT_IDLE_MODE;
1238                         Adapter->bWakeUpDevice = TRUE;
1239                         wake_up(&Adapter->process_rx_cntrlpkt);
1240                 }
1241
1242                 Status = STATUS_SUCCESS;
1243                 break;
1244
1245         case IOCTL_BCM_BULK_WRM: {
1246                 struct bcm_bulk_wrm_buffer *pBulkBuffer;
1247                 UINT uiTempVar = 0;
1248                 PCHAR pvBuffer = NULL;
1249
1250                 if ((Adapter->IdleMode == TRUE) ||
1251                         (Adapter->bShutStatus == TRUE) ||
1252                         (Adapter->bPreparingForLowPowerMode == TRUE)) {
1253
1254                         BCM_DEBUG_PRINT (Adapter, DBG_TYPE_PRINTK, 0, 0, "Device in Idle/Shutdown Mode, Blocking Wrms\n");
1255                         Status = -EACCES;
1256                         break;
1257                 }
1258
1259                 /* Copy Ioctl Buffer structure */
1260                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1261                         return -EFAULT;
1262
1263                 if (IoBuffer.InputLength < sizeof(ULONG) * 2)
1264                         return -EINVAL;
1265
1266                 pvBuffer = memdup_user(IoBuffer.InputBuffer,
1267                                        IoBuffer.InputLength);
1268                 if (IS_ERR(pvBuffer))
1269                         return PTR_ERR(pvBuffer);
1270
1271                 pBulkBuffer = (struct bcm_bulk_wrm_buffer *)pvBuffer;
1272
1273                 if (((ULONG)pBulkBuffer->Register & 0x0F000000) != 0x0F000000 ||
1274                         ((ULONG)pBulkBuffer->Register & 0x3)) {
1275                         BCM_DEBUG_PRINT (Adapter, DBG_TYPE_PRINTK, 0, 0, "WRM Done On invalid Address : %x Access Denied.\n", (int)pBulkBuffer->Register);
1276                         kfree(pvBuffer);
1277                         Status = -EINVAL;
1278                         break;
1279                 }
1280
1281                 uiTempVar = pBulkBuffer->Register & EEPROM_REJECT_MASK;
1282                 if (!((Adapter->pstargetparams->m_u32Customize)&VSG_MODE) &&
1283                         ((uiTempVar == EEPROM_REJECT_REG_1) ||
1284                                 (uiTempVar == EEPROM_REJECT_REG_2) ||
1285                                 (uiTempVar == EEPROM_REJECT_REG_3) ||
1286                                 (uiTempVar == EEPROM_REJECT_REG_4)) &&
1287                         (cmd == IOCTL_BCM_REGISTER_WRITE)) {
1288
1289                         kfree(pvBuffer);
1290                         BCM_DEBUG_PRINT (Adapter, DBG_TYPE_PRINTK, 0, 0, "EEPROM Access Denied, not in VSG Mode\n");
1291                         Status = -EFAULT;
1292                         break;
1293                 }
1294
1295                 if (pBulkBuffer->SwapEndian == false)
1296                         Status = wrmWithLock(Adapter, (UINT)pBulkBuffer->Register, (PCHAR)pBulkBuffer->Values, IoBuffer.InputLength - 2*sizeof(ULONG));
1297                 else
1298                         Status = wrmaltWithLock(Adapter, (UINT)pBulkBuffer->Register, (PUINT)pBulkBuffer->Values, IoBuffer.InputLength - 2*sizeof(ULONG));
1299
1300                 if (Status != STATUS_SUCCESS)
1301                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "WRM Failed\n");
1302
1303                 kfree(pvBuffer);
1304                 break;
1305         }
1306
1307         case IOCTL_BCM_GET_NVM_SIZE:
1308                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1309                         return -EFAULT;
1310
1311                 if (Adapter->eNVMType == NVM_EEPROM || Adapter->eNVMType == NVM_FLASH) {
1312                         if (copy_to_user(IoBuffer.OutputBuffer, &Adapter->uiNVMDSDSize, sizeof(UINT)))
1313                                 return -EFAULT;
1314                 }
1315
1316                 Status = STATUS_SUCCESS;
1317                 break;
1318
1319         case IOCTL_BCM_CAL_INIT: {
1320                 UINT uiSectorSize = 0;
1321                 if (Adapter->eNVMType == NVM_FLASH) {
1322                         if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1323                                 return -EFAULT;
1324
1325                         if (copy_from_user(&uiSectorSize, IoBuffer.InputBuffer, sizeof(UINT)))
1326                                 return -EFAULT;
1327
1328                         if ((uiSectorSize < MIN_SECTOR_SIZE) || (uiSectorSize > MAX_SECTOR_SIZE)) {
1329                                 if (copy_to_user(IoBuffer.OutputBuffer, &Adapter->uiSectorSize,
1330                                                         sizeof(UINT)))
1331                                         return -EFAULT;
1332                         } else {
1333                                 if (IsFlash2x(Adapter)) {
1334                                         if (copy_to_user(IoBuffer.OutputBuffer, &Adapter->uiSectorSize, sizeof(UINT)))
1335                                                 return -EFAULT;
1336                                 } else {
1337                                         if ((TRUE == Adapter->bShutStatus) || (TRUE == Adapter->IdleMode)) {
1338                                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Device is in Idle/Shutdown Mode\n");
1339                                                 return -EACCES;
1340                                         }
1341
1342                                         Adapter->uiSectorSize = uiSectorSize;
1343                                         BcmUpdateSectorSize(Adapter, Adapter->uiSectorSize);
1344                                 }
1345                         }
1346                         Status = STATUS_SUCCESS;
1347                 } else {
1348                         Status = STATUS_FAILURE;
1349                 }
1350         }
1351         break;
1352
1353         case IOCTL_BCM_SET_DEBUG:
1354 #ifdef DEBUG
1355         {
1356                 struct bcm_user_debug_state sUserDebugState;
1357
1358                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "In SET_DEBUG ioctl\n");
1359                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1360                         return -EFAULT;
1361
1362                 if (copy_from_user(&sUserDebugState, IoBuffer.InputBuffer, sizeof(struct bcm_user_debug_state)))
1363                         return -EFAULT;
1364
1365                 BCM_DEBUG_PRINT (Adapter, DBG_TYPE_PRINTK, 0, 0, "IOCTL_BCM_SET_DEBUG: OnOff=%d Type = 0x%x ",
1366                                 sUserDebugState.OnOff, sUserDebugState.Type);
1367                 /* sUserDebugState.Subtype <<= 1; */
1368                 sUserDebugState.Subtype = 1 << sUserDebugState.Subtype;
1369                 BCM_DEBUG_PRINT (Adapter, DBG_TYPE_PRINTK, 0, 0, "actual Subtype=0x%x\n", sUserDebugState.Subtype);
1370
1371                 /* Update new 'DebugState' in the Adapter */
1372                 Adapter->stDebugState.type |= sUserDebugState.Type;
1373                 /* Subtype: A bitmap of 32 bits for Subtype per Type.
1374                  * Valid indexes in 'subtype' array: 1,2,4,8
1375                  * corresponding to valid Type values. Hence we can use the 'Type' field
1376                  * as the index value, ignoring the array entries 0,3,5,6,7 !
1377                  */
1378                 if (sUserDebugState.OnOff)
1379                         Adapter->stDebugState.subtype[sUserDebugState.Type] |= sUserDebugState.Subtype;
1380                 else
1381                         Adapter->stDebugState.subtype[sUserDebugState.Type] &= ~sUserDebugState.Subtype;
1382
1383                 BCM_SHOW_DEBUG_BITMAP(Adapter);
1384         }
1385 #endif
1386         break;
1387
1388         case IOCTL_BCM_NVM_READ:
1389         case IOCTL_BCM_NVM_WRITE: {
1390                 struct bcm_nvm_readwrite stNVMReadWrite;
1391                 PUCHAR pReadData = NULL;
1392                 ULONG ulDSDMagicNumInUsrBuff = 0;
1393                 struct timeval tv0, tv1;
1394                 memset(&tv0, 0, sizeof(struct timeval));
1395                 memset(&tv1, 0, sizeof(struct timeval));
1396                 if ((Adapter->eNVMType == NVM_FLASH) && (Adapter->uiFlashLayoutMajorVersion == 0)) {
1397                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "The Flash Control Section is Corrupted. Hence Rejection on NVM Read/Write\n");
1398                         return -EFAULT;
1399                 }
1400
1401                 if (IsFlash2x(Adapter)) {
1402                         if ((Adapter->eActiveDSD != DSD0) &&
1403                                 (Adapter->eActiveDSD != DSD1) &&
1404                                 (Adapter->eActiveDSD != DSD2)) {
1405
1406                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "No DSD is active..hence NVM Command is blocked");
1407                                 return STATUS_FAILURE;
1408                         }
1409                 }
1410
1411                 /* Copy Ioctl Buffer structure */
1412                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1413                         return -EFAULT;
1414
1415                 if (copy_from_user(&stNVMReadWrite,
1416                                         (IOCTL_BCM_NVM_READ == cmd) ? IoBuffer.OutputBuffer : IoBuffer.InputBuffer,
1417                                         sizeof(struct bcm_nvm_readwrite)))
1418                         return -EFAULT;
1419
1420                 /*
1421                  * Deny the access if the offset crosses the cal area limit.
1422                  */
1423                 if (stNVMReadWrite.uiNumBytes > Adapter->uiNVMDSDSize)
1424                         return STATUS_FAILURE;
1425
1426                 if (stNVMReadWrite.uiOffset > Adapter->uiNVMDSDSize - stNVMReadWrite.uiNumBytes) {
1427                         /* 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); */
1428                         return STATUS_FAILURE;
1429                 }
1430
1431                 pReadData = memdup_user(stNVMReadWrite.pBuffer,
1432                                         stNVMReadWrite.uiNumBytes);
1433                 if (IS_ERR(pReadData))
1434                         return PTR_ERR(pReadData);
1435
1436                 do_gettimeofday(&tv0);
1437                 if (IOCTL_BCM_NVM_READ == cmd) {
1438                         down(&Adapter->NVMRdmWrmLock);
1439
1440                         if ((Adapter->IdleMode == TRUE) ||
1441                                 (Adapter->bShutStatus == TRUE) ||
1442                                 (Adapter->bPreparingForLowPowerMode == TRUE)) {
1443
1444                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Device is in Idle/Shutdown Mode\n");
1445                                 up(&Adapter->NVMRdmWrmLock);
1446                                 kfree(pReadData);
1447                                 return -EACCES;
1448                         }
1449
1450                         Status = BeceemNVMRead(Adapter, (PUINT)pReadData, stNVMReadWrite.uiOffset, stNVMReadWrite.uiNumBytes);
1451                         up(&Adapter->NVMRdmWrmLock);
1452
1453                         if (Status != STATUS_SUCCESS) {
1454                                 kfree(pReadData);
1455                                 return Status;
1456                         }
1457
1458                         if (copy_to_user(stNVMReadWrite.pBuffer, pReadData, stNVMReadWrite.uiNumBytes)) {
1459                                 kfree(pReadData);
1460                                 return -EFAULT;
1461                         }
1462                 } else {
1463                         down(&Adapter->NVMRdmWrmLock);
1464
1465                         if ((Adapter->IdleMode == TRUE) ||
1466                                 (Adapter->bShutStatus == TRUE) ||
1467                                 (Adapter->bPreparingForLowPowerMode == TRUE)) {
1468
1469                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Device is in Idle/Shutdown Mode\n");
1470                                 up(&Adapter->NVMRdmWrmLock);
1471                                 kfree(pReadData);
1472                                 return -EACCES;
1473                         }
1474
1475                         Adapter->bHeaderChangeAllowed = TRUE;
1476                         if (IsFlash2x(Adapter)) {
1477                                 /*
1478                                  *                      New Requirement:-
1479                                  *                      DSD section updation will be allowed in two case:-
1480                                  *                      1.  if DSD sig is present in DSD header means dongle is ok and updation is fruitfull
1481                                  *                      2.  if point 1 failes then user buff should have DSD sig. this point ensures that if dongle is
1482                                  *                            corrupted then user space program first modify the DSD header with valid DSD sig so
1483                                  *                            that this as well as further write may be worthwhile.
1484                                  *
1485                                  *                       This restriction has been put assuming that if DSD sig is corrupted, DSD
1486                                  *                       data won't be considered valid.
1487                                  */
1488
1489                                 Status = BcmFlash2xCorruptSig(Adapter, Adapter->eActiveDSD);
1490                                 if (Status != STATUS_SUCCESS) {
1491                                         if (((stNVMReadWrite.uiOffset + stNVMReadWrite.uiNumBytes) != Adapter->uiNVMDSDSize) ||
1492                                                 (stNVMReadWrite.uiNumBytes < SIGNATURE_SIZE)) {
1493
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                                         ulDSDMagicNumInUsrBuff = ntohl(*(PUINT)(pReadData + stNVMReadWrite.uiNumBytes - SIGNATURE_SIZE));
1501                                         if (ulDSDMagicNumInUsrBuff != DSD_IMAGE_MAGIC_NUMBER) {
1502                                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "DSD Sig is present neither in Flash nor User provided Input..");
1503                                                 up(&Adapter->NVMRdmWrmLock);
1504                                                 kfree(pReadData);
1505                                                 return Status;
1506                                         }
1507                                 }
1508                         }
1509
1510                         Status = BeceemNVMWrite(Adapter, (PUINT)pReadData, stNVMReadWrite.uiOffset, stNVMReadWrite.uiNumBytes, stNVMReadWrite.bVerify);
1511                         if (IsFlash2x(Adapter))
1512                                 BcmFlash2xWriteSig(Adapter, Adapter->eActiveDSD);
1513
1514                         Adapter->bHeaderChangeAllowed = false;
1515
1516                         up(&Adapter->NVMRdmWrmLock);
1517
1518                         if (Status != STATUS_SUCCESS) {
1519                                 kfree(pReadData);
1520                                 return Status;
1521                         }
1522                 }
1523
1524                 do_gettimeofday(&tv1);
1525                 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);
1526
1527                 kfree(pReadData);
1528                 return STATUS_SUCCESS;
1529         }
1530
1531         case IOCTL_BCM_FLASH2X_SECTION_READ: {
1532                 struct bcm_flash2x_readwrite sFlash2xRead = {0};
1533                 PUCHAR pReadBuff = NULL;
1534                 UINT NOB = 0;
1535                 UINT BuffSize = 0;
1536                 UINT ReadBytes = 0;
1537                 UINT ReadOffset = 0;
1538                 void __user *OutPutBuff;
1539
1540                 if (IsFlash2x(Adapter) != TRUE) {
1541                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Flash Does not have 2.x map");
1542                         return -EINVAL;
1543                 }
1544
1545                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_FLASH2X_SECTION_READ Called");
1546                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1547                         return -EFAULT;
1548
1549                 /* Reading FLASH 2.x READ structure */
1550                 if (copy_from_user(&sFlash2xRead, IoBuffer.InputBuffer, sizeof(struct bcm_flash2x_readwrite)))
1551                         return -EFAULT;
1552
1553                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "\nsFlash2xRead.Section :%x", sFlash2xRead.Section);
1554                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "\nsFlash2xRead.offset :%x", sFlash2xRead.offset);
1555                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "\nsFlash2xRead.numOfBytes :%x", sFlash2xRead.numOfBytes);
1556                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "\nsFlash2xRead.bVerify :%x\n", sFlash2xRead.bVerify);
1557
1558                 /* This was internal to driver for raw read. now it has ben exposed to user space app. */
1559                 if (validateFlash2xReadWrite(Adapter, &sFlash2xRead) == false)
1560                         return STATUS_FAILURE;
1561
1562                 NOB = sFlash2xRead.numOfBytes;
1563                 if (NOB > Adapter->uiSectorSize)
1564                         BuffSize = Adapter->uiSectorSize;
1565                 else
1566                         BuffSize = NOB;
1567
1568                 ReadOffset = sFlash2xRead.offset;
1569                 OutPutBuff = IoBuffer.OutputBuffer;
1570                 pReadBuff = (PCHAR)kzalloc(BuffSize , GFP_KERNEL);
1571
1572                 if (pReadBuff == NULL) {
1573                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Memory allocation failed for Flash 2.x Read Structure");
1574                         return -ENOMEM;
1575                 }
1576                 down(&Adapter->NVMRdmWrmLock);
1577
1578                 if ((Adapter->IdleMode == TRUE) ||
1579                         (Adapter->bShutStatus == TRUE) ||
1580                         (Adapter->bPreparingForLowPowerMode == TRUE)) {
1581
1582                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Device is in Idle/Shutdown Mode\n");
1583                         up(&Adapter->NVMRdmWrmLock);
1584                         kfree(pReadBuff);
1585                         return -EACCES;
1586                 }
1587
1588                 while (NOB) {
1589                         if (NOB > Adapter->uiSectorSize)
1590                                 ReadBytes = Adapter->uiSectorSize;
1591                         else
1592                                 ReadBytes = NOB;
1593
1594                         /* Reading the data from Flash 2.x */
1595                         Status = BcmFlash2xBulkRead(Adapter, (PUINT)pReadBuff, sFlash2xRead.Section, ReadOffset, ReadBytes);
1596                         if (Status) {
1597                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Flash 2x read err with Status :%d", Status);
1598                                 break;
1599                         }
1600
1601                         BCM_DEBUG_PRINT_BUFFER(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, pReadBuff, ReadBytes);
1602
1603                         Status = copy_to_user(OutPutBuff, pReadBuff, ReadBytes);
1604                         if (Status) {
1605                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Copy to use failed with status :%d", Status);
1606                                 up(&Adapter->NVMRdmWrmLock);
1607                                 kfree(pReadBuff);
1608                                 return -EFAULT;
1609                         }
1610                         NOB = NOB - ReadBytes;
1611                         if (NOB) {
1612                                 ReadOffset = ReadOffset + ReadBytes;
1613                                 OutPutBuff = OutPutBuff + ReadBytes;
1614                         }
1615                 }
1616
1617                 up(&Adapter->NVMRdmWrmLock);
1618                 kfree(pReadBuff);
1619         }
1620         break;
1621
1622         case IOCTL_BCM_FLASH2X_SECTION_WRITE: {
1623                 struct bcm_flash2x_readwrite sFlash2xWrite = {0};
1624                 PUCHAR pWriteBuff;
1625                 void __user *InputAddr;
1626                 UINT NOB = 0;
1627                 UINT BuffSize = 0;
1628                 UINT WriteOffset = 0;
1629                 UINT WriteBytes = 0;
1630
1631                 if (IsFlash2x(Adapter) != TRUE) {
1632                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Flash Does not have 2.x map");
1633                         return -EINVAL;
1634                 }
1635
1636                 /* First make this False so that we can enable the Sector Permission Check in BeceemFlashBulkWrite */
1637                 Adapter->bAllDSDWriteAllow = false;
1638
1639                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_FLASH2X_SECTION_WRITE Called");
1640
1641                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1642                         return -EFAULT;
1643
1644                 /* Reading FLASH 2.x READ structure */
1645                 if (copy_from_user(&sFlash2xWrite, IoBuffer.InputBuffer, sizeof(struct bcm_flash2x_readwrite)))
1646                         return -EFAULT;
1647
1648                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "\nsFlash2xRead.Section :%x", sFlash2xWrite.Section);
1649                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "\nsFlash2xRead.offset :%d", sFlash2xWrite.offset);
1650                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "\nsFlash2xRead.numOfBytes :%x", sFlash2xWrite.numOfBytes);
1651                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "\nsFlash2xRead.bVerify :%x\n", sFlash2xWrite.bVerify);
1652
1653                 if ((sFlash2xWrite.Section != VSA0) && (sFlash2xWrite.Section != VSA1) && (sFlash2xWrite.Section != VSA2)) {
1654                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Only VSA write is allowed");
1655                         return -EINVAL;
1656                 }
1657
1658                 if (validateFlash2xReadWrite(Adapter, &sFlash2xWrite) == false)
1659                         return STATUS_FAILURE;
1660
1661                 InputAddr = sFlash2xWrite.pDataBuff;
1662                 WriteOffset = sFlash2xWrite.offset;
1663                 NOB = sFlash2xWrite.numOfBytes;
1664
1665                 if (NOB > Adapter->uiSectorSize)
1666                         BuffSize = Adapter->uiSectorSize;
1667                 else
1668                         BuffSize = NOB;
1669
1670                 pWriteBuff = kmalloc(BuffSize, GFP_KERNEL);
1671
1672                 if (pWriteBuff == NULL)
1673                         return -ENOMEM;
1674
1675                 /* extracting the remainder of the given offset. */
1676                 WriteBytes = Adapter->uiSectorSize;
1677                 if (WriteOffset % Adapter->uiSectorSize)
1678                         WriteBytes = Adapter->uiSectorSize - (WriteOffset % Adapter->uiSectorSize);
1679
1680                 if (NOB < WriteBytes)
1681                         WriteBytes = NOB;
1682
1683                 down(&Adapter->NVMRdmWrmLock);
1684
1685                 if ((Adapter->IdleMode == TRUE) ||
1686                         (Adapter->bShutStatus == TRUE) ||
1687                         (Adapter->bPreparingForLowPowerMode == TRUE)) {
1688
1689                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Device is in Idle/Shutdown Mode\n");
1690                         up(&Adapter->NVMRdmWrmLock);
1691                         kfree(pWriteBuff);
1692                         return -EACCES;
1693                 }
1694
1695                 BcmFlash2xCorruptSig(Adapter, sFlash2xWrite.Section);
1696                 do {
1697                         Status = copy_from_user(pWriteBuff, InputAddr, WriteBytes);
1698                         if (Status) {
1699                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Copy to user failed with status :%d", Status);
1700                                 up(&Adapter->NVMRdmWrmLock);
1701                                 kfree(pWriteBuff);
1702                                 return -EFAULT;
1703                         }
1704                         BCM_DEBUG_PRINT_BUFFER(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, pWriteBuff, WriteBytes);
1705
1706                         /* Writing the data from Flash 2.x */
1707                         Status = BcmFlash2xBulkWrite(Adapter, (PUINT)pWriteBuff, sFlash2xWrite.Section, WriteOffset, WriteBytes, sFlash2xWrite.bVerify);
1708
1709                         if (Status) {
1710                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Flash 2x read err with Status :%d", Status);
1711                                 break;
1712                         }
1713
1714                         NOB = NOB - WriteBytes;
1715                         if (NOB) {
1716                                 WriteOffset = WriteOffset + WriteBytes;
1717                                 InputAddr = InputAddr + WriteBytes;
1718                                 if (NOB > Adapter->uiSectorSize)
1719                                         WriteBytes = Adapter->uiSectorSize;
1720                                 else
1721                                         WriteBytes = NOB;
1722                         }
1723                 } while (NOB > 0);
1724
1725                 BcmFlash2xWriteSig(Adapter, sFlash2xWrite.Section);
1726                 up(&Adapter->NVMRdmWrmLock);
1727                 kfree(pWriteBuff);
1728         }
1729         break;
1730
1731         case IOCTL_BCM_GET_FLASH2X_SECTION_BITMAP: {
1732                 struct bcm_flash2x_bitmap *psFlash2xBitMap;
1733                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_GET_FLASH2X_SECTION_BITMAP Called");
1734
1735                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
1736                         return -EFAULT;
1737
1738                 if (IoBuffer.OutputLength != sizeof(struct bcm_flash2x_bitmap))
1739                         return -EINVAL;
1740
1741                 psFlash2xBitMap = kzalloc(sizeof(struct bcm_flash2x_bitmap), GFP_KERNEL);
1742                 if (psFlash2xBitMap == NULL) {
1743                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Memory is not available");
1744                         return -ENOMEM;
1745                 }
1746
1747                 /* Reading the Flash Sectio Bit map */
1748                 down(&Adapter->NVMRdmWrmLock);
1749
1750                 if ((Adapter->IdleMode == TRUE) ||
1751                         (Adapter->bShutStatus == TRUE) ||
1752                         (Adapter->bPreparingForLowPowerMode == TRUE)) {
1753
1754                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Device is in Idle/Shutdown Mode\n");
1755                         up(&Adapter->NVMRdmWrmLock);
1756                         kfree(psFlash2xBitMap);
1757                         return -EACCES;
1758                 }
1759
1760                 BcmGetFlash2xSectionalBitMap(Adapter, psFlash2xBitMap);
1761                 up(&Adapter->NVMRdmWrmLock);
1762                 if (copy_to_user(IoBuffer.OutputBuffer, psFlash2xBitMap, sizeof(struct bcm_flash2x_bitmap))) {
1763                         kfree(psFlash2xBitMap);
1764                         return -EFAULT;
1765                 }
1766
1767                 kfree(psFlash2xBitMap);
1768         }
1769         break;
1770
1771         case IOCTL_BCM_SET_ACTIVE_SECTION: {
1772                 enum bcm_flash2x_section_val eFlash2xSectionVal = 0;
1773                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_SET_ACTIVE_SECTION Called");
1774
1775                 if (IsFlash2x(Adapter) != TRUE) {
1776                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Flash Does not have 2.x map");
1777                         return -EINVAL;
1778                 }
1779
1780                 Status = copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer));
1781                 if (Status) {
1782                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Copy of IOCTL BUFFER failed");
1783                         return -EFAULT;
1784                 }
1785
1786                 Status = copy_from_user(&eFlash2xSectionVal, IoBuffer.InputBuffer, sizeof(INT));
1787                 if (Status) {
1788                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Copy of flash section val failed");
1789                         return -EFAULT;
1790                 }
1791
1792                 down(&Adapter->NVMRdmWrmLock);
1793
1794                 if ((Adapter->IdleMode == TRUE) ||
1795                         (Adapter->bShutStatus == TRUE) ||
1796                         (Adapter->bPreparingForLowPowerMode == TRUE)) {
1797
1798                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Device is in Idle/Shutdown Mode\n");
1799                         up(&Adapter->NVMRdmWrmLock);
1800                         return -EACCES;
1801                 }
1802
1803                 Status = BcmSetActiveSection(Adapter, eFlash2xSectionVal);
1804                 if (Status)
1805                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Failed to make it's priority Highest. Status %d", Status);
1806
1807                 up(&Adapter->NVMRdmWrmLock);
1808         }
1809         break;
1810
1811         case IOCTL_BCM_IDENTIFY_ACTIVE_SECTION: {
1812                 /* Right Now we are taking care of only DSD */
1813                 Adapter->bAllDSDWriteAllow = false;
1814                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_IDENTIFY_ACTIVE_SECTION called");
1815                 Status = STATUS_SUCCESS;
1816         }
1817         break;
1818
1819         case IOCTL_BCM_COPY_SECTION: {
1820                 struct bcm_flash2x_copy_section sCopySectStrut = {0};
1821                 Status = STATUS_SUCCESS;
1822                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_COPY_SECTION  Called");
1823
1824                 Adapter->bAllDSDWriteAllow = false;
1825                 if (IsFlash2x(Adapter) != TRUE) {
1826                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Flash Does not have 2.x map");
1827                         return -EINVAL;
1828                 }
1829
1830                 Status = copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer));
1831                 if (Status) {
1832                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Copy of IOCTL BUFFER failed Status :%d", Status);
1833                         return -EFAULT;
1834                 }
1835
1836                 Status = copy_from_user(&sCopySectStrut, IoBuffer.InputBuffer, sizeof(struct bcm_flash2x_copy_section));
1837                 if (Status) {
1838                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Copy of Copy_Section_Struct failed with Status :%d", Status);
1839                         return -EFAULT;
1840                 }
1841
1842                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Source SEction :%x", sCopySectStrut.SrcSection);
1843                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Destination SEction :%x", sCopySectStrut.DstSection);
1844                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "offset :%x", sCopySectStrut.offset);
1845                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "NOB :%x", sCopySectStrut.numOfBytes);
1846
1847                 if (IsSectionExistInFlash(Adapter, sCopySectStrut.SrcSection) == false) {
1848                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Source Section<%x> does not exist in Flash ", sCopySectStrut.SrcSection);
1849                         return -EINVAL;
1850                 }
1851
1852                 if (IsSectionExistInFlash(Adapter, sCopySectStrut.DstSection) == false) {
1853                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Destinatio Section<%x> does not exist in Flash ", sCopySectStrut.DstSection);
1854                         return -EINVAL;
1855                 }
1856
1857                 if (sCopySectStrut.SrcSection == sCopySectStrut.DstSection) {
1858                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Source and Destination section should be different");
1859                         return -EINVAL;
1860                 }
1861
1862                 down(&Adapter->NVMRdmWrmLock);
1863
1864                 if ((Adapter->IdleMode == TRUE) ||
1865                         (Adapter->bShutStatus == TRUE) ||
1866                         (Adapter->bPreparingForLowPowerMode == TRUE)) {
1867
1868                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Device is in Idle/Shutdown Mode\n");
1869                         up(&Adapter->NVMRdmWrmLock);
1870                         return -EACCES;
1871                 }
1872
1873                 if (sCopySectStrut.SrcSection == ISO_IMAGE1 || sCopySectStrut.SrcSection == ISO_IMAGE2) {
1874                         if (IsNonCDLessDevice(Adapter)) {
1875                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Device is Non-CDLess hence won't have ISO !!");
1876                                 Status = -EINVAL;
1877                         } else if (sCopySectStrut.numOfBytes == 0) {
1878                                 Status = BcmCopyISO(Adapter, sCopySectStrut);
1879                         } else {
1880                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Partial Copy of ISO section is not Allowed..");
1881                                 Status = STATUS_FAILURE;
1882                         }
1883                         up(&Adapter->NVMRdmWrmLock);
1884                         return Status;
1885                 }
1886
1887                 Status = BcmCopySection(Adapter, sCopySectStrut.SrcSection,
1888                                         sCopySectStrut.DstSection, sCopySectStrut.offset, sCopySectStrut.numOfBytes);
1889                 up(&Adapter->NVMRdmWrmLock);
1890         }
1891         break;
1892
1893         case IOCTL_BCM_GET_FLASH_CS_INFO: {
1894                 Status = STATUS_SUCCESS;
1895                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, " IOCTL_BCM_GET_FLASH_CS_INFO Called");
1896
1897                 Status = copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer));
1898                 if (Status) {
1899                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Copy of IOCTL BUFFER failed");
1900                         return -EFAULT;
1901                 }
1902
1903                 if (Adapter->eNVMType != NVM_FLASH) {
1904                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Connected device does not have flash");
1905                         Status = -EINVAL;
1906                         break;
1907                 }
1908
1909                 if (IsFlash2x(Adapter) == TRUE) {
1910                         if (IoBuffer.OutputLength < sizeof(struct bcm_flash2x_cs_info))
1911                                 return -EINVAL;
1912
1913                         if (copy_to_user(IoBuffer.OutputBuffer, Adapter->psFlash2xCSInfo, sizeof(struct bcm_flash2x_cs_info)))
1914                                 return -EFAULT;
1915                 } else {
1916                         if (IoBuffer.OutputLength < sizeof(struct bcm_flash_cs_info))
1917                                 return -EINVAL;
1918
1919                         if (copy_to_user(IoBuffer.OutputBuffer, Adapter->psFlashCSInfo, sizeof(struct bcm_flash_cs_info)))
1920                                 return -EFAULT;
1921                 }
1922         }
1923         break;
1924
1925         case IOCTL_BCM_SELECT_DSD: {
1926                 UINT SectOfset = 0;
1927                 enum bcm_flash2x_section_val eFlash2xSectionVal;
1928                 eFlash2xSectionVal = NO_SECTION_VAL;
1929                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_SELECT_DSD Called");
1930
1931                 if (IsFlash2x(Adapter) != TRUE) {
1932                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Flash Does not have 2.x map");
1933                         return -EINVAL;
1934                 }
1935
1936                 Status = copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer));
1937                 if (Status) {
1938                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Copy of IOCTL BUFFER failed");
1939                         return -EFAULT;
1940                 }
1941                 Status = copy_from_user(&eFlash2xSectionVal, IoBuffer.InputBuffer, sizeof(INT));
1942                 if (Status) {
1943                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Copy of flash section val failed");
1944                         return -EFAULT;
1945                 }
1946
1947                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Read Section :%d", eFlash2xSectionVal);
1948                 if ((eFlash2xSectionVal != DSD0) &&
1949                         (eFlash2xSectionVal != DSD1) &&
1950                         (eFlash2xSectionVal != DSD2)) {
1951
1952                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Passed section<%x> is not DSD section", eFlash2xSectionVal);
1953                         return STATUS_FAILURE;
1954                 }
1955
1956                 SectOfset = BcmGetSectionValStartOffset(Adapter, eFlash2xSectionVal);
1957                 if (SectOfset == INVALID_OFFSET) {
1958                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Provided Section val <%d> does not exist in Flash 2.x", eFlash2xSectionVal);
1959                         return -EINVAL;
1960                 }
1961
1962                 Adapter->bAllDSDWriteAllow = TRUE;
1963                 Adapter->ulFlashCalStart = SectOfset;
1964                 Adapter->eActiveDSD = eFlash2xSectionVal;
1965         }
1966         Status = STATUS_SUCCESS;
1967         break;
1968
1969         case IOCTL_BCM_NVM_RAW_READ: {
1970                 struct bcm_nvm_readwrite stNVMRead;
1971                 INT NOB;
1972                 INT BuffSize;
1973                 INT ReadOffset = 0;
1974                 UINT ReadBytes = 0;
1975                 PUCHAR pReadBuff;
1976                 void __user *OutPutBuff;
1977
1978                 if (Adapter->eNVMType != NVM_FLASH) {
1979                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "NVM TYPE is not Flash");
1980                         return -EINVAL;
1981                 }
1982
1983                 /* Copy Ioctl Buffer structure */
1984                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer))) {
1985                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "copy_from_user 1 failed\n");
1986                         return -EFAULT;
1987                 }
1988
1989                 if (copy_from_user(&stNVMRead, IoBuffer.OutputBuffer, sizeof(struct bcm_nvm_readwrite)))
1990                         return -EFAULT;
1991
1992                 NOB = stNVMRead.uiNumBytes;
1993                 /* In Raw-Read max Buff size : 64MB */
1994
1995                 if (NOB > DEFAULT_BUFF_SIZE)
1996                         BuffSize = DEFAULT_BUFF_SIZE;
1997                 else
1998                         BuffSize = NOB;
1999
2000                 ReadOffset = stNVMRead.uiOffset;
2001                 OutPutBuff = stNVMRead.pBuffer;
2002
2003                 pReadBuff = kzalloc(BuffSize , GFP_KERNEL);
2004                 if (pReadBuff == NULL) {
2005                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Memory allocation failed for Flash 2.x Read Structure");
2006                         Status = -ENOMEM;
2007                         break;
2008                 }
2009                 down(&Adapter->NVMRdmWrmLock);
2010
2011                 if ((Adapter->IdleMode == TRUE) ||
2012                         (Adapter->bShutStatus == TRUE) ||
2013                         (Adapter->bPreparingForLowPowerMode == TRUE)) {
2014
2015                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Device is in Idle/Shutdown Mode\n");
2016                         kfree(pReadBuff);
2017                         up(&Adapter->NVMRdmWrmLock);
2018                         return -EACCES;
2019                 }
2020
2021                 Adapter->bFlashRawRead = TRUE;
2022
2023                 while (NOB) {
2024                         if (NOB > DEFAULT_BUFF_SIZE)
2025                                 ReadBytes = DEFAULT_BUFF_SIZE;
2026                         else
2027                                 ReadBytes = NOB;
2028
2029                         /* Reading the data from Flash 2.x */
2030                         Status = BeceemNVMRead(Adapter, (PUINT)pReadBuff, ReadOffset, ReadBytes);
2031                         if (Status) {
2032                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Flash 2x read err with Status :%d", Status);
2033                                 break;
2034                         }
2035
2036                         BCM_DEBUG_PRINT_BUFFER(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, pReadBuff, ReadBytes);
2037
2038                         Status = copy_to_user(OutPutBuff, pReadBuff, ReadBytes);
2039                         if (Status) {
2040                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_PRINTK, 0, 0, "Copy to use failed with status :%d", Status);
2041                                 up(&Adapter->NVMRdmWrmLock);
2042                                 kfree(pReadBuff);
2043                                 return -EFAULT;
2044                         }
2045                         NOB = NOB - ReadBytes;
2046                         if (NOB) {
2047                                 ReadOffset = ReadOffset + ReadBytes;
2048                                 OutPutBuff = OutPutBuff + ReadBytes;
2049                         }
2050                 }
2051                 Adapter->bFlashRawRead = false;
2052                 up(&Adapter->NVMRdmWrmLock);
2053                 kfree(pReadBuff);
2054                 break;
2055         }
2056
2057         case IOCTL_BCM_CNTRLMSG_MASK: {
2058                 ULONG RxCntrlMsgBitMask = 0;
2059
2060                 /* Copy Ioctl Buffer structure */
2061                 Status = copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer));
2062                 if (Status) {
2063                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "copy of Ioctl buffer is failed from user space");
2064                         return -EFAULT;
2065                 }
2066
2067                 if (IoBuffer.InputLength != sizeof(unsigned long)) {
2068                         Status = -EINVAL;
2069                         break;
2070                 }
2071
2072                 Status = copy_from_user(&RxCntrlMsgBitMask, IoBuffer.InputBuffer, IoBuffer.InputLength);
2073                 if (Status) {
2074                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "copy of control bit mask failed from user space");
2075                         return -EFAULT;
2076                 }
2077                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "\n Got user defined cntrl msg bit mask :%lx", RxCntrlMsgBitMask);
2078                 pTarang->RxCntrlMsgBitMask = RxCntrlMsgBitMask;
2079         }
2080         break;
2081
2082         case IOCTL_BCM_GET_DEVICE_DRIVER_INFO: {
2083                 struct bcm_driver_info DevInfo;
2084
2085                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "Called IOCTL_BCM_GET_DEVICE_DRIVER_INFO\n");
2086
2087                 memset(&DevInfo, 0, sizeof(DevInfo));
2088                 DevInfo.MaxRDMBufferSize = BUFFER_4K;
2089                 DevInfo.u32DSDStartOffset = EEPROM_CALPARAM_START;
2090                 DevInfo.u32RxAlignmentCorrection = 0;
2091                 DevInfo.u32NVMType = Adapter->eNVMType;
2092                 DevInfo.u32InterfaceType = BCM_USB;
2093
2094                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
2095                         return -EFAULT;
2096
2097                 if (IoBuffer.OutputLength < sizeof(DevInfo))
2098                         return -EINVAL;
2099
2100                 if (copy_to_user(IoBuffer.OutputBuffer, &DevInfo, sizeof(DevInfo)))
2101                         return -EFAULT;
2102         }
2103         break;
2104
2105         case IOCTL_BCM_TIME_SINCE_NET_ENTRY: {
2106                 struct bcm_time_elapsed stTimeElapsedSinceNetEntry = {0};
2107
2108                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_BCM_TIME_SINCE_NET_ENTRY called");
2109
2110                 if (copy_from_user(&IoBuffer, argp, sizeof(struct bcm_ioctl_buffer)))
2111                         return -EFAULT;
2112
2113                 if (IoBuffer.OutputLength < sizeof(struct bcm_time_elapsed))
2114                         return -EINVAL;
2115
2116                 stTimeElapsedSinceNetEntry.ul64TimeElapsedSinceNetEntry = get_seconds() - Adapter->liTimeSinceLastNetEntry;
2117
2118                 if (copy_to_user(IoBuffer.OutputBuffer, &stTimeElapsedSinceNetEntry, sizeof(struct bcm_time_elapsed)))
2119                         return -EFAULT;
2120         }
2121         break;
2122
2123         case IOCTL_CLOSE_NOTIFICATION:
2124                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, OSAL_DBG, DBG_LVL_ALL, "IOCTL_CLOSE_NOTIFICATION");
2125                 break;
2126
2127         default:
2128                 pr_info(DRV_NAME ": unknown ioctl cmd=%#x\n", cmd);
2129                 Status = STATUS_FAILURE;
2130                 break;
2131         }
2132         return Status;
2133 }
2134
2135
2136 static const struct file_operations bcm_fops = {
2137         .owner    = THIS_MODULE,
2138         .open     = bcm_char_open,
2139         .release  = bcm_char_release,
2140         .read     = bcm_char_read,
2141         .unlocked_ioctl    = bcm_char_ioctl,
2142         .llseek = no_llseek,
2143 };
2144
2145 int register_control_device_interface(struct bcm_mini_adapter *Adapter)
2146 {
2147
2148         if (Adapter->major > 0)
2149                 return Adapter->major;
2150
2151         Adapter->major = register_chrdev(0, DEV_NAME, &bcm_fops);
2152         if (Adapter->major < 0) {
2153                 pr_err(DRV_NAME ": could not created character device\n");
2154                 return Adapter->major;
2155         }
2156
2157         Adapter->pstCreatedClassDevice = device_create(bcm_class, NULL,
2158                                                 MKDEV(Adapter->major, 0),
2159                                                 Adapter, DEV_NAME);
2160
2161         if (IS_ERR(Adapter->pstCreatedClassDevice)) {
2162                 pr_err(DRV_NAME ": class device create failed\n");
2163                 unregister_chrdev(Adapter->major, DEV_NAME);
2164                 return PTR_ERR(Adapter->pstCreatedClassDevice);
2165         }
2166
2167         return 0;
2168 }
2169
2170 void unregister_control_device_interface(struct bcm_mini_adapter *Adapter)
2171 {
2172         if (Adapter->major > 0) {
2173                 device_destroy(bcm_class, MKDEV(Adapter->major, 0));
2174                 unregister_chrdev(Adapter->major, DEV_NAME);
2175         }
2176 }
2177