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