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