Linux Audio

Check our new training course

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