]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/bcm/InterfaceInit.c
801ba65e978571af3b5d20d7b60e9ce5637fb476
[mv-sheeva.git] / drivers / staging / bcm / InterfaceInit.c
1 #include "headers.h"
2
3 static struct usb_device_id InterfaceUsbtable[] = {
4     { USB_DEVICE(BCM_USB_VENDOR_ID_T3, BCM_USB_PRODUCT_ID_T3) },
5     { USB_DEVICE(BCM_USB_VENDOR_ID_T3, BCM_USB_PRODUCT_ID_T3B) },
6     { USB_DEVICE(BCM_USB_VENDOR_ID_T3, BCM_USB_PRODUCT_ID_T3L) },
7     { USB_DEVICE(BCM_USB_VENDOR_ID_ZTE, BCM_USB_PRODUCT_ID_226) },
8     { USB_DEVICE(BCM_USB_VENDOR_ID_FOXCONN, BCM_USB_PRODUCT_ID_1901) },
9
10     { }
11 };
12 MODULE_DEVICE_TABLE(usb, InterfaceUsbtable);
13
14 static unsigned int debug_level = DBG_LVL_CURR;
15 module_param(debug_level, uint, 0644);
16 MODULE_PARM_DESC(debug_level, "Debug level (0=none,...,7=all)");
17
18
19 VOID InterfaceAdapterFree(PS_INTERFACE_ADAPTER psIntfAdapter)
20 {
21         INT i = 0;
22         // Wake up the wait_queue...
23         if(psIntfAdapter->psAdapter->LEDInfo.led_thread_running & BCM_LED_THREAD_RUNNING_ACTIVELY)
24         {
25                 psIntfAdapter->psAdapter->DriverState = DRIVER_HALT;
26                 wake_up(&psIntfAdapter->psAdapter->LEDInfo.notify_led_event);
27         }
28         reset_card_proc(psIntfAdapter->psAdapter);
29
30         //worst case time taken by the RDM/WRM will be 5 sec. will check after every 100 ms
31         //to accertain the device is not being accessed. After this No RDM/WRM should be made.
32         while(psIntfAdapter->psAdapter->DeviceAccess)
33         {
34                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL,"Device is being Accessed \n");
35                 msleep(100);
36         }
37         /* Free interrupt URB */
38         //psIntfAdapter->psAdapter->device_removed = TRUE;
39         if(psIntfAdapter->psInterruptUrb)
40         {
41                 usb_free_urb(psIntfAdapter->psInterruptUrb);
42         }
43
44         /* Free transmit URBs */
45         for(i = 0; i < MAXIMUM_USB_TCB; i++)
46         {
47                 if(psIntfAdapter->asUsbTcb[i].urb  != NULL)
48                 {
49                         usb_free_urb(psIntfAdapter->asUsbTcb[i].urb);
50                         psIntfAdapter->asUsbTcb[i].urb = NULL;
51                 }
52         }
53         /* Free receive URB and buffers */
54         for(i = 0; i < MAXIMUM_USB_RCB; i++)
55         {
56                 if (psIntfAdapter->asUsbRcb[i].urb != NULL)
57                 {
58                         kfree(psIntfAdapter->asUsbRcb[i].urb->transfer_buffer);
59                         usb_free_urb(psIntfAdapter->asUsbRcb[i].urb);
60                         psIntfAdapter->asUsbRcb[i].urb = NULL;
61                 }
62         }
63         AdapterFree(psIntfAdapter->psAdapter);
64 }
65
66
67
68 static int usbbcm_open(struct inode *inode, struct file *file)
69 {
70         return 0;
71 }
72
73 static int usbbcm_release(struct inode *inode, struct file *file)
74 {
75         return 0;
76 }
77
78 static ssize_t usbbcm_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
79 {
80         return 0;
81 }
82
83 static ssize_t usbbcm_write(struct file *file, const char __user *user_buffer, size_t count, loff_t *ppos)
84 {
85         return 0;
86 }
87
88
89 VOID ConfigureEndPointTypesThroughEEPROM(PMINI_ADAPTER Adapter)
90 {
91         ULONG ulReg = 0;
92
93 // Program EP2 MAX_PKT_SIZE
94         ulReg = ntohl(EP2_MPS_REG);
95         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x128,4,TRUE);
96         ulReg = ntohl(EP2_MPS);
97         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x12C,4,TRUE);
98
99         ulReg = ntohl(EP2_CFG_REG);
100         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x132,4,TRUE);
101         if(((PS_INTERFACE_ADAPTER)(Adapter->pvInterfaceAdapter))->bHighSpeedDevice == TRUE)
102         {
103                 ulReg = ntohl(EP2_CFG_INT);
104                 BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x136,4,TRUE);
105         }
106         else
107         {
108 // USE BULK EP as TX in FS mode.
109                 ulReg = ntohl(EP2_CFG_BULK);
110                 BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x136,4,TRUE);
111         }
112
113
114 // Program EP4 MAX_PKT_SIZE.
115         ulReg = ntohl(EP4_MPS_REG);
116         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x13C,4,TRUE);
117         ulReg = ntohl(EP4_MPS);
118         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x140,4,TRUE);
119
120 //      Program TX EP as interrupt (Alternate Setting)
121         if( rdmalt(Adapter,0x0F0110F8, (PUINT)&ulReg,4))
122         {
123                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "reading of Tx EP is failing");
124                 return ;
125         }
126         ulReg |= 0x6;
127
128         ulReg = ntohl(ulReg);
129         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x1CC,4,TRUE);
130
131         ulReg = ntohl(EP4_CFG_REG);
132         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x1C8,4,TRUE);
133 // Program ISOCHRONOUS EP size to zero.
134         ulReg = ntohl(ISO_MPS_REG);
135         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x1D2,4,TRUE);
136         ulReg = ntohl(ISO_MPS);
137         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x1D6,4,TRUE);
138
139 // Update EEPROM Version.
140 // Read 4 bytes from 508 and modify 511 and 510.
141 //
142         ReadBeceemEEPROM(Adapter,0x1FC,(PUINT)&ulReg);
143         ulReg &= 0x0101FFFF;
144         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x1FC,4,TRUE);
145 //
146 //Update length field if required. Also make the string NULL terminated.
147 //
148         ReadBeceemEEPROM(Adapter,0xA8,(PUINT)&ulReg);
149         if((ulReg&0x00FF0000)>>16 > 0x30)
150         {
151                 ulReg = (ulReg&0xFF00FFFF)|(0x30<<16);
152                 BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0xA8,4,TRUE);
153         }
154         ReadBeceemEEPROM(Adapter,0x148,(PUINT)&ulReg);
155         if((ulReg&0x00FF0000)>>16 > 0x30)
156         {
157                 ulReg = (ulReg&0xFF00FFFF)|(0x30<<16);
158                 BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x148,4,TRUE);
159         }
160         ulReg = 0;
161         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x122,4,TRUE);
162         ulReg = 0;
163         BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&ulReg,0x1C2,4,TRUE);
164
165 }
166
167 static struct file_operations usbbcm_fops = {
168     .open    =  usbbcm_open,
169     .release =  usbbcm_release,
170     .read    =  usbbcm_read,
171     .write   =  usbbcm_write,
172     .owner   =  THIS_MODULE,
173         .llseek = no_llseek,
174 };
175
176 static struct usb_class_driver usbbcm_class = {
177     .name =             "usbbcm",
178     .fops =             &usbbcm_fops,
179     .minor_base =   BCM_USB_MINOR_BASE,
180 };
181
182 static int
183 usbbcm_device_probe(struct usb_interface *intf, const struct usb_device_id *id)
184 {
185         struct usb_device *udev = interface_to_usbdev (intf);
186         int retval;
187         PMINI_ADAPTER psAdapter;
188         PS_INTERFACE_ADAPTER psIntfAdapter;
189         struct net_device *ndev;
190
191         /* Reserve one extra queue for the bit-bucket */
192         ndev = alloc_etherdev_mq(sizeof(MINI_ADAPTER), NO_OF_QUEUES+1);
193         if(ndev == NULL) {
194                 dev_err(&udev->dev, DRV_NAME ": no memory for device\n");
195                 return -ENOMEM;
196         }
197
198         SET_NETDEV_DEV(ndev, &intf->dev);
199
200         psAdapter = netdev_priv(ndev);
201         psAdapter->dev = ndev;
202
203     /* Init default driver debug state */
204
205         psAdapter->stDebugState.debug_level = debug_level;
206         psAdapter->stDebugState.type = DBG_TYPE_INITEXIT;
207
208     /* Technically, one can start using BCM_DEBUG_PRINT after this point.
209          * However, realize that by default the Type/Subtype bitmaps are all zero now;
210          * so no prints will actually appear until the TestApp turns on debug paths via
211          * the ioctl(); so practically speaking, in early init, no logging happens.
212          *
213          * A solution (used below): we explicitly set the bitmaps to 1 for Type=DBG_TYPE_INITEXIT
214          * and ALL subtype's of the same. Now all bcm debug statements get logged, enabling debug
215          * during early init.
216          * Further, we turn this OFF once init_module() completes.
217          */
218
219     psAdapter->stDebugState.subtype[DBG_TYPE_INITEXIT] = 0xff;
220         BCM_SHOW_DEBUG_BITMAP(psAdapter);
221
222         retval = InitAdapter(psAdapter);
223         if(retval)
224         {
225                 dev_err(&udev->dev, DRV_NAME ": InitAdapter Failed\n");
226                 AdapterFree(psAdapter);
227                 return retval;
228         }
229
230         /* Allocate interface adapter structure */
231         psIntfAdapter = kzalloc(sizeof(S_INTERFACE_ADAPTER), GFP_KERNEL);
232         if (psIntfAdapter == NULL)
233         {
234                 dev_err(&udev->dev, DRV_NAME ": no memory for Interface adapter\n");
235                 AdapterFree (psAdapter);
236                 return -ENOMEM;
237         }
238
239         psAdapter->pvInterfaceAdapter = psIntfAdapter;
240         psIntfAdapter->psAdapter = psAdapter;
241
242         /* Store usb interface in Interface Adapter */
243         psIntfAdapter->interface = intf;
244         usb_set_intfdata(intf, psIntfAdapter);
245
246         BCM_DEBUG_PRINT(psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "psIntfAdapter 0x%p",psIntfAdapter);
247         retval = InterfaceAdapterInit(psIntfAdapter);
248         if(retval)
249         {
250                 /* If the Firmware/Cfg File is not present
251                  * then return success, let the application
252                  * download the files.
253                  */
254                 if(-ENOENT == retval){
255                         BCM_DEBUG_PRINT(psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "File Not Found, Use App to Download\n");
256                         return STATUS_SUCCESS;
257                 }
258                 BCM_DEBUG_PRINT(psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "InterfaceAdapterInit Failed \n");
259                 usb_set_intfdata(intf, NULL);
260                 udev = interface_to_usbdev (intf);
261                 usb_put_dev(udev);
262                 if(psAdapter->bUsbClassDriverRegistered == TRUE)
263                                 usb_deregister_dev (intf, &usbbcm_class);
264                 InterfaceAdapterFree(psIntfAdapter);
265                 return retval ;
266         }
267         if(psAdapter->chip_id > T3)
268         {
269                 uint32_t uiNackZeroLengthInt=4;
270                 if(wrmalt(psAdapter, DISABLE_USB_ZERO_LEN_INT, &uiNackZeroLengthInt, sizeof(uiNackZeroLengthInt)))
271                 {
272                         return -EIO;;
273                 }
274         }
275
276         /* Check whether the USB-Device Supports remote Wake-Up */
277         if(USB_CONFIG_ATT_WAKEUP & udev->actconfig->desc.bmAttributes)
278         {
279                 /* If Suspend then only support dynamic suspend */
280                 if(psAdapter->bDoSuspend)
281                 {
282 #ifdef CONFIG_PM
283                         udev->autosuspend_delay = 0;
284                         intf->needs_remote_wakeup = 1;
285 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35)
286                         udev->autosuspend_disabled = 0;
287 #else
288                         usb_enable_autosuspend(udev);
289 #endif
290                         device_init_wakeup(&intf->dev,1);
291 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 32)
292                         usb_autopm_disable(intf);
293 #endif
294                         INIT_WORK(&psIntfAdapter->usbSuspendWork, putUsbSuspend);
295                         BCM_DEBUG_PRINT(psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Enabling USB Auto-Suspend\n");
296 #endif
297                 }
298                 else
299                 {
300                         intf->needs_remote_wakeup = 0;
301 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35)
302                         udev->autosuspend_disabled = 1;
303 #else
304                         usb_disable_autosuspend(udev);
305 #endif
306                 }
307         }
308
309     psAdapter->stDebugState.subtype[DBG_TYPE_INITEXIT] = 0x0;
310     return retval;
311 }
312
313 static void usbbcm_disconnect (struct usb_interface *intf)
314 {
315         PS_INTERFACE_ADAPTER psIntfAdapter = NULL;
316         PMINI_ADAPTER psAdapter = NULL;
317         struct usb_device       *udev = NULL;
318     PMINI_ADAPTER Adapter = GET_BCM_ADAPTER(gblpnetdev);
319
320         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Usb disconnected");
321         if(intf == NULL)
322         {
323                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "intf pointer is NULL");
324                 return;
325         }
326         psIntfAdapter = usb_get_intfdata(intf);
327         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "psIntfAdapter 0x%p",psIntfAdapter);
328         if(psIntfAdapter == NULL)
329         {
330                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "InterfaceAdapter pointer is NULL");
331                 return;
332         }
333         psAdapter = psIntfAdapter->psAdapter;
334         if(psAdapter->bDoSuspend)
335                 intf->needs_remote_wakeup = 0;
336
337         psAdapter->device_removed = TRUE ;
338         usb_set_intfdata(intf, NULL);
339         InterfaceAdapterFree(psIntfAdapter);
340         udev = interface_to_usbdev (intf);
341         usb_put_dev(udev);
342         usb_deregister_dev (intf, &usbbcm_class);
343 }
344
345
346 static int AllocUsbCb(PS_INTERFACE_ADAPTER psIntfAdapter)
347 {
348         int i = 0;
349         for(i = 0; i < MAXIMUM_USB_TCB; i++)
350         {
351                 if((psIntfAdapter->asUsbTcb[i].urb =
352                                 usb_alloc_urb(0, GFP_KERNEL)) == NULL)
353                 {
354                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_PRINTK, 0, 0, "Cant allocate Tx urb for index %d", i);
355                         return -ENOMEM;
356                 }
357         }
358
359         for(i = 0; i < MAXIMUM_USB_RCB; i++)
360         {
361                 if ((psIntfAdapter->asUsbRcb[i].urb =
362                                 usb_alloc_urb(0, GFP_KERNEL)) == NULL)
363                 {
364                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_PRINTK, 0, 0, "Cant allocate Rx urb for index %d", i);
365                         return -ENOMEM;
366                 }
367                 if((psIntfAdapter->asUsbRcb[i].urb->transfer_buffer =
368                         kmalloc(MAX_DATA_BUFFER_SIZE, GFP_KERNEL)) == NULL)
369                 {
370                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_PRINTK, 0, 0, "Cant allocate Rx buffer for index %d", i);
371                         return -ENOMEM;
372                 }
373                 psIntfAdapter->asUsbRcb[i].urb->transfer_buffer_length = MAX_DATA_BUFFER_SIZE;
374         }
375         return 0;
376 }
377
378
379
380 static int device_run(PS_INTERFACE_ADAPTER psIntfAdapter)
381 {
382         INT value = 0;
383         UINT status = STATUS_SUCCESS;
384
385         status = InitCardAndDownloadFirmware(psIntfAdapter->psAdapter);
386         if(status != STATUS_SUCCESS)
387         {
388                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_PRINTK, 0, 0, "InitCardAndDownloadFirmware failed.\n");
389                 return status;
390         }
391         if(TRUE == psIntfAdapter->psAdapter->fw_download_done)
392         {
393
394                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Sending first interrupt URB down......");
395                 if(StartInterruptUrb(psIntfAdapter))
396                 {
397                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Cannot send interrupt in URB");
398                 }
399                 //now register the cntrl interface.
400                 //after downloading the f/w waiting for 5 sec to get the mailbox interrupt.
401
402                 psIntfAdapter->psAdapter->waiting_to_fw_download_done = FALSE;
403                 value = wait_event_timeout(psIntfAdapter->psAdapter->ioctl_fw_dnld_wait_queue,
404                                         psIntfAdapter->psAdapter->waiting_to_fw_download_done, 5*HZ);
405
406                 if(value == 0)
407                 {
408                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL,"Mailbox Interrupt has not reached to Driver..");
409                 }
410                 else
411                 {
412                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL,"Got the mailbox interrupt ...Registering control interface...\n ");
413                 }
414                 if(register_control_device_interface(psIntfAdapter->psAdapter) < 0)
415                 {
416                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_PRINTK, 0, 0, "Register Control Device failed...");
417                         return -EIO;
418                 }
419         }
420         return 0;
421 }
422
423
424 static inline int bcm_usb_endpoint_num(const struct usb_endpoint_descriptor *epd)
425 {
426         return epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
427 }
428
429 static inline int bcm_usb_endpoint_type(const struct usb_endpoint_descriptor *epd)
430 {
431         return epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
432 }
433
434 static inline int bcm_usb_endpoint_dir_in(const struct usb_endpoint_descriptor *epd)
435 {
436         return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN);
437 }
438
439 static inline int bcm_usb_endpoint_dir_out(const struct usb_endpoint_descriptor *epd)
440 {
441         return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
442 }
443
444 static inline int bcm_usb_endpoint_xfer_bulk(const struct usb_endpoint_descriptor *epd)
445 {
446         return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
447                 USB_ENDPOINT_XFER_BULK);
448 }
449
450 static inline int bcm_usb_endpoint_xfer_control(const struct usb_endpoint_descriptor *epd)
451 {
452         return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
453                 USB_ENDPOINT_XFER_CONTROL);
454 }
455
456 static inline int bcm_usb_endpoint_xfer_int(const struct usb_endpoint_descriptor *epd)
457 {
458         return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
459                 USB_ENDPOINT_XFER_INT);
460 }
461
462 static inline int bcm_usb_endpoint_xfer_isoc(const struct usb_endpoint_descriptor *epd)
463 {
464         return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
465                 USB_ENDPOINT_XFER_ISOC);
466 }
467
468 static inline int bcm_usb_endpoint_is_bulk_in(const struct usb_endpoint_descriptor *epd)
469 {
470         return (bcm_usb_endpoint_xfer_bulk(epd) && bcm_usb_endpoint_dir_in(epd));
471 }
472
473 static inline int bcm_usb_endpoint_is_bulk_out(const struct usb_endpoint_descriptor *epd)
474 {
475         return (bcm_usb_endpoint_xfer_bulk(epd) && bcm_usb_endpoint_dir_out(epd));
476 }
477
478 static inline int bcm_usb_endpoint_is_int_in(const struct usb_endpoint_descriptor *epd)
479 {
480         return (bcm_usb_endpoint_xfer_int(epd) && bcm_usb_endpoint_dir_in(epd));
481 }
482
483 static inline int bcm_usb_endpoint_is_int_out(const struct usb_endpoint_descriptor *epd)
484 {
485         return (bcm_usb_endpoint_xfer_int(epd) && bcm_usb_endpoint_dir_out(epd));
486 }
487
488 static inline int bcm_usb_endpoint_is_isoc_in(const struct usb_endpoint_descriptor *epd)
489 {
490         return (bcm_usb_endpoint_xfer_isoc(epd) && bcm_usb_endpoint_dir_in(epd));
491 }
492
493 static inline int bcm_usb_endpoint_is_isoc_out(const struct usb_endpoint_descriptor *epd)
494 {
495         return (bcm_usb_endpoint_xfer_isoc(epd) && bcm_usb_endpoint_dir_out(epd));
496 }
497
498 INT InterfaceAdapterInit(PS_INTERFACE_ADAPTER psIntfAdapter)
499 {
500         struct usb_host_interface *iface_desc;
501         struct usb_endpoint_descriptor *endpoint;
502         size_t buffer_size;
503         ULONG value;
504         INT retval = 0;
505         INT usedIntOutForBulkTransfer = 0 ;
506         BOOLEAN bBcm16 = FALSE;
507         UINT uiData = 0;
508
509         /* Store the usb dev into interface adapter */
510         psIntfAdapter->udev = usb_get_dev(interface_to_usbdev(
511                                                                 psIntfAdapter->interface));
512
513         if((psIntfAdapter->udev->speed == USB_SPEED_HIGH))
514         {
515                 psIntfAdapter->bHighSpeedDevice = TRUE ;
516                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "MODEM IS CONFIGURED TO HIGH_SPEED ");
517         }
518         else
519         {
520                 psIntfAdapter->bHighSpeedDevice = FALSE ;
521                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "MODEM IS CONFIGURED TO FULL_SPEED ");
522         }
523
524         psIntfAdapter->psAdapter->interface_rdm = BcmRDM;
525         psIntfAdapter->psAdapter->interface_wrm = BcmWRM;
526
527         if(rdmalt(psIntfAdapter->psAdapter, CHIP_ID_REG, (PUINT)&(psIntfAdapter->psAdapter->chip_id), sizeof(UINT)) < 0)
528         {
529                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_PRINTK, 0, 0, "CHIP ID Read Failed\n");
530                 return STATUS_FAILURE;
531         }
532     if(0xbece3200==(psIntfAdapter->psAdapter->chip_id&~(0xF0)))
533         {
534                 psIntfAdapter->psAdapter->chip_id=(psIntfAdapter->psAdapter->chip_id&~(0xF0));
535         }
536
537         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "First RDM Chip ID 0x%lx\n", psIntfAdapter->psAdapter->chip_id);
538
539     iface_desc = psIntfAdapter->interface->cur_altsetting;
540         //print_usb_interface_desc(&(iface_desc->desc));
541
542         if(psIntfAdapter->psAdapter->chip_id == T3B)
543         {
544
545                 //
546                 //T3B device will have EEPROM,check if EEPROM is proper and BCM16 can be done or not.
547                 //
548                 BeceemEEPROMBulkRead(psIntfAdapter->psAdapter,&uiData,0x0,4);
549                 if(uiData == BECM)
550                 {
551                         bBcm16 = TRUE;
552                 }
553                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Number of Altsetting aviailable for This Modem 0x%x\n", psIntfAdapter->interface->num_altsetting);
554                 if(bBcm16 == TRUE)
555                 {
556                         //selecting alternate setting one as a default setting for High Speed  modem.
557                         if(psIntfAdapter->bHighSpeedDevice)
558                                 retval= usb_set_interface(psIntfAdapter->udev,DEFAULT_SETTING_0,ALTERNATE_SETTING_1);
559                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "BCM16 is Applicable on this dongle");
560                         if(retval || (psIntfAdapter->bHighSpeedDevice == FALSE))
561                         {
562                                 usedIntOutForBulkTransfer = EP2 ;
563                                 endpoint = &iface_desc->endpoint[EP2].desc;
564                                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Interface altsetting  got failed or Moemd is configured to FS.hence will work on default setting 0 \n");
565                                 /*
566                                 If Modem is high speed device EP2 should be INT OUT End point
567                                 If Mode is FS then EP2 should be bulk end point
568                                 */
569                                 if(((psIntfAdapter->bHighSpeedDevice ==TRUE ) && (bcm_usb_endpoint_is_int_out(endpoint)== FALSE))
570                                         ||((psIntfAdapter->bHighSpeedDevice == FALSE)&& (bcm_usb_endpoint_is_bulk_out(endpoint)== FALSE)))
571                                 {
572                                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL,"Configuring the EEPROM ");
573                                         //change the EP2, EP4 to INT OUT end point
574                                         ConfigureEndPointTypesThroughEEPROM(psIntfAdapter->psAdapter);
575
576                                         /*
577                                         It resets the device and if any thing gets changed in USB descriptor it will show fail and
578                                         re-enumerate the device
579                                         */
580                                         retval = usb_reset_device(psIntfAdapter->udev);
581                                         if(retval)
582                                         {
583                                                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "reset got failed. hence Re-enumerating the device \n");
584                                                 return retval ;
585                                         }
586
587                                 }
588                                 if((psIntfAdapter->bHighSpeedDevice == FALSE) && bcm_usb_endpoint_is_bulk_out(endpoint))
589                                 {
590                                         // Once BULK is selected in FS mode. Revert it back to INT. Else USB_IF will fail.
591                                         UINT _uiData = ntohl(EP2_CFG_INT);
592                                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL,"Reverting Bulk to INT as it is FS MODE");
593                                         BeceemEEPROMBulkWrite(psIntfAdapter->psAdapter,(PUCHAR)&_uiData,0x136,4,TRUE);
594                                 }
595                         }
596                         else
597                         {
598                                 usedIntOutForBulkTransfer = EP4 ;
599                                 endpoint = &iface_desc->endpoint[EP4].desc;
600                                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Choosing AltSetting as a default setting");
601                                 if( bcm_usb_endpoint_is_int_out(endpoint) == FALSE)
602                                 {
603                                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, " Dongle does not have BCM16 Fix");
604                                         //change the EP2, EP4 to INT OUT end point and use EP4 in altsetting
605                                         ConfigureEndPointTypesThroughEEPROM(psIntfAdapter->psAdapter);
606
607                                         /*
608                                         It resets the device and if any thing gets changed in USB descriptor it will show fail and
609                                         re-enumerate the device
610                                         */
611                                         retval = usb_reset_device(psIntfAdapter->udev);
612                                         if(retval)
613                                         {
614                                                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "reset got failed. hence Re-enumerating the device \n");
615                                                 return retval ;
616                                         }
617
618                                 }
619                         }
620                 }
621         }
622
623         iface_desc = psIntfAdapter->interface->cur_altsetting;
624         //print_usb_interface_desc(&(iface_desc->desc));
625         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_PRINTK, 0, 0, "Current number of endpoints :%x \n", iface_desc->desc.bNumEndpoints);
626     for (value = 0; value < iface_desc->desc.bNumEndpoints; ++value)
627         {
628         endpoint = &iface_desc->endpoint[value].desc;
629                 //print_usb_endpoint_descriptor(endpoint);
630
631         if (!psIntfAdapter->sBulkIn.bulk_in_endpointAddr && bcm_usb_endpoint_is_bulk_in(endpoint))
632         {
633             buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
634             psIntfAdapter->sBulkIn.bulk_in_size = buffer_size;
635             psIntfAdapter->sBulkIn.bulk_in_endpointAddr =
636                                                                 endpoint->bEndpointAddress;
637                 psIntfAdapter->sBulkIn.bulk_in_pipe =
638                                         usb_rcvbulkpipe(psIntfAdapter->udev,
639                                                                 psIntfAdapter->sBulkIn.bulk_in_endpointAddr);
640         }
641
642         if (!psIntfAdapter->sBulkOut.bulk_out_endpointAddr && bcm_usb_endpoint_is_bulk_out(endpoint))
643         {
644
645                         psIntfAdapter->sBulkOut.bulk_out_endpointAddr =
646                                                                                 endpoint->bEndpointAddress;
647                 psIntfAdapter->sBulkOut.bulk_out_pipe =
648                         usb_sndbulkpipe(psIntfAdapter->udev,
649                                         psIntfAdapter->sBulkOut.bulk_out_endpointAddr);
650         }
651
652         if (!psIntfAdapter->sIntrIn.int_in_endpointAddr && bcm_usb_endpoint_is_int_in(endpoint))
653         {
654             buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
655             psIntfAdapter->sIntrIn.int_in_size = buffer_size;
656             psIntfAdapter->sIntrIn.int_in_endpointAddr =
657                                                                 endpoint->bEndpointAddress;
658             psIntfAdapter->sIntrIn.int_in_interval = endpoint->bInterval;
659             psIntfAdapter->sIntrIn.int_in_buffer =
660                                                 kmalloc(buffer_size, GFP_KERNEL);
661             if (!psIntfAdapter->sIntrIn.int_in_buffer) {
662                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Could not allocate interrupt_in_buffer");
663                 return -EINVAL;
664             }
665                         //psIntfAdapter->sIntrIn.int_in_pipe =
666         }
667
668         if (!psIntfAdapter->sIntrOut.int_out_endpointAddr && bcm_usb_endpoint_is_int_out(endpoint))
669         {
670
671                         if( !psIntfAdapter->sBulkOut.bulk_out_endpointAddr &&
672                                 (psIntfAdapter->psAdapter->chip_id == T3B) && (value == usedIntOutForBulkTransfer))
673                         {
674                                 //use first intout end point as a bulk out end point
675                 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
676                 psIntfAdapter->sBulkOut.bulk_out_size = buffer_size;
677                                 //printk("\nINT OUT Endpoing buffer size :%x endpoint :%x\n", buffer_size, value +1);
678                                 psIntfAdapter->sBulkOut.bulk_out_endpointAddr =
679                                                                                 endpoint->bEndpointAddress;
680                         psIntfAdapter->sBulkOut.bulk_out_pipe =
681                                 usb_sndintpipe(psIntfAdapter->udev,
682                                         psIntfAdapter->sBulkOut.bulk_out_endpointAddr);
683                         psIntfAdapter->sBulkOut.int_out_interval = endpoint->bInterval;
684
685                         }
686                         else if(value == EP6)
687                         {
688                     buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
689                     psIntfAdapter->sIntrOut.int_out_size = buffer_size;
690                     psIntfAdapter->sIntrOut.int_out_endpointAddr =
691                                                                                 endpoint->bEndpointAddress;
692                     psIntfAdapter->sIntrOut.int_out_interval = endpoint->bInterval;
693                     psIntfAdapter->sIntrOut.int_out_buffer= kmalloc(buffer_size,
694                                                                                                                 GFP_KERNEL);
695                         if (!psIntfAdapter->sIntrOut.int_out_buffer)
696                                         {
697                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Could not allocate interrupt_out_buffer");
698                         return -EINVAL;
699             }
700         }
701     }
702         }
703     usb_set_intfdata(psIntfAdapter->interface, psIntfAdapter);
704     retval = usb_register_dev(psIntfAdapter->interface, &usbbcm_class);
705         if(retval)
706         {
707                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_PRINTK, 0, 0, "usb register dev failed = %d", retval);
708                 psIntfAdapter->psAdapter->bUsbClassDriverRegistered = FALSE;
709                 return retval;
710         }
711         else
712         {
713                 psIntfAdapter->psAdapter->bUsbClassDriverRegistered = TRUE;
714                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_PRINTK, 0, 0, "usb dev registered");
715         }
716
717         psIntfAdapter->psAdapter->bcm_file_download = InterfaceFileDownload;
718         psIntfAdapter->psAdapter->bcm_file_readback_from_chip =
719                                 InterfaceFileReadbackFromChip;
720         psIntfAdapter->psAdapter->interface_transmit = InterfaceTransmitPacket;
721
722         retval = CreateInterruptUrb(psIntfAdapter);
723
724         if(retval)
725         {
726                 BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_PRINTK, 0, 0, "Cannot create interrupt urb");
727                 return retval;
728         }
729
730         retval = AllocUsbCb(psIntfAdapter);
731         if(retval)
732         {
733                 return retval;
734         }
735
736
737         retval = device_run(psIntfAdapter);
738         if(retval)
739         {
740                 return retval;
741         }
742
743
744         return 0;
745 }
746
747 static int InterfaceSuspend (struct usb_interface *intf, pm_message_t message)
748 {
749         PS_INTERFACE_ADAPTER  psIntfAdapter = usb_get_intfdata(intf);
750         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "=================================\n");
751         //Bcm_kill_all_URBs(psIntfAdapter);
752         psIntfAdapter->bSuspended = TRUE;
753
754         if(TRUE == psIntfAdapter->bPreparingForBusSuspend)
755         {
756                 psIntfAdapter->bPreparingForBusSuspend = FALSE;
757
758                 if(psIntfAdapter->psAdapter->LinkStatus == LINKUP_DONE)
759                 {
760                         psIntfAdapter->psAdapter->IdleMode = TRUE ;
761                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Host Entered in PMU Idle Mode..");
762                 }
763                 else
764                 {
765                         psIntfAdapter->psAdapter->bShutStatus = TRUE;
766                         BCM_DEBUG_PRINT(psIntfAdapter->psAdapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Host Entered in PMU Shutdown Mode..");
767                 }
768         }
769         psIntfAdapter->psAdapter->bPreparingForLowPowerMode = FALSE;
770
771         //Signaling the control pkt path
772         wake_up(&psIntfAdapter->psAdapter->lowpower_mode_wait_queue);
773
774         return 0;
775 }
776
777 static int InterfaceResume (struct usb_interface *intf)
778 {
779     PS_INTERFACE_ADAPTER  psIntfAdapter = usb_get_intfdata(intf);
780         printk("=================================\n");
781         mdelay(100);
782 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 32)
783         intf->pm_usage_cnt =1 ;
784 #endif
785         psIntfAdapter->bSuspended = FALSE;
786
787         StartInterruptUrb(psIntfAdapter);
788         InterfaceRx(psIntfAdapter);
789         return 0;
790 }
791
792 static struct usb_driver usbbcm_driver = {
793     .name = "usbbcm",
794     .probe = usbbcm_device_probe,
795     .disconnect = usbbcm_disconnect,
796     .suspend = InterfaceSuspend,
797     .resume = InterfaceResume,
798     .id_table = InterfaceUsbtable,
799     .supports_autosuspend = 1,
800 };
801
802
803 /*
804 Function:                               InterfaceInitialize
805
806 Description:                    This is the hardware specific initialization Function.
807                                                 Registering the driver with NDIS , other device specific NDIS
808                                                 and hardware initializations are done here.
809
810 Input parameters:               IN PMINI_ADAPTER Adapter   - Miniport Adapter Context
811
812
813 Return:                                 BCM_STATUS_SUCCESS - If Initialization of the
814                                                 HW Interface was successful.
815                                                 Other           - If an error occured.
816 */
817 INT InterfaceInitialize(void)
818 {
819 //      BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Registering Usb driver!!");
820         return usb_register(&usbbcm_driver);
821 }
822
823 INT InterfaceExit(void)
824 {
825         //PMINI_ADAPTER psAdapter = NULL;
826         int status = 0;
827
828         //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_INITEXIT, DRV_ENTRY, DBG_LVL_ALL, "Deregistering Usb driver!!");
829         usb_deregister(&usbbcm_driver);
830         return status;
831 }