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