1/*=====================================================
2 * CopyRight (C) 2007 Qualcomm Inc. All Rights Reserved.
3 *
4 *
5 * This file is part of Express Card USB Driver
6 *
7 * $Id:
8 *====================================================
9 */
10#include <linux/init.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/usb.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/firmware.h>
17#include "ft1000_usb.h"
18
19#include <linux/kthread.h>
20
21MODULE_DESCRIPTION("FT1000 EXPRESS CARD DRIVER");
22MODULE_LICENSE("Dual MPL/GPL");
23MODULE_SUPPORTED_DEVICE("QFT FT1000 Express Cards");
24
25void *pFileStart;
26size_t FileLength;
27
28#define VENDOR_ID 0x1291	/* Qualcomm vendor id */
29#define PRODUCT_ID 0x11		/* fake product id */
30
31/* table of devices that work with this driver */
32static struct usb_device_id id_table[] = {
33	{USB_DEVICE(VENDOR_ID, PRODUCT_ID)},
34	{},
35};
36
37MODULE_DEVICE_TABLE(usb, id_table);
38
39static bool gPollingfailed = FALSE;
40int ft1000_poll_thread(void *arg)
41{
42	int ret = STATUS_SUCCESS;
43
44	while (!kthread_should_stop()) {
45		msleep(10);
46		if (!gPollingfailed) {
47			ret = ft1000_poll(arg);
48			if (ret != STATUS_SUCCESS) {
49				DEBUG("ft1000_poll_thread: polling failed\n");
50				gPollingfailed = TRUE;
51			}
52		}
53	}
54	return STATUS_SUCCESS;
55}
56
57static int ft1000_probe(struct usb_interface *interface,
58			const struct usb_device_id *id)
59{
60	struct usb_host_interface *iface_desc;
61	struct usb_endpoint_descriptor *endpoint;
62	struct usb_device *dev;
63	unsigned numaltsetting;
64	int i, ret = 0, size;
65
66	struct ft1000_device *ft1000dev;
67	struct ft1000_info *pft1000info = NULL;
68	const struct firmware *dsp_fw;
69
70	ft1000dev = kmalloc(sizeof(struct ft1000_device), GFP_KERNEL);
71
72	if (!ft1000dev) {
73		printk(KERN_ERR "out of memory allocating device structure\n");
74		return 0;
75	}
76
77	memset(ft1000dev, 0, sizeof(*ft1000dev));
78
79	dev = interface_to_usbdev(interface);
80	DEBUG("ft1000_probe: usb device descriptor info:\n");
81	DEBUG("ft1000_probe: number of configuration is %d\n",
82	      dev->descriptor.bNumConfigurations);
83
84	ft1000dev->dev = dev;
85	ft1000dev->status = 0;
86	ft1000dev->net = NULL;
87	ft1000dev->tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
88	ft1000dev->rx_urb = usb_alloc_urb(0, GFP_ATOMIC);
89
90	DEBUG("ft1000_probe is called\n");
91	numaltsetting = interface->num_altsetting;
92	DEBUG("ft1000_probe: number of alt settings is :%d\n", numaltsetting);
93	iface_desc = interface->cur_altsetting;
94	DEBUG("ft1000_probe: number of endpoints is %d\n",
95	      iface_desc->desc.bNumEndpoints);
96	DEBUG("ft1000_probe: descriptor type is %d\n",
97	      iface_desc->desc.bDescriptorType);
98	DEBUG("ft1000_probe: interface number is %d\n",
99	      iface_desc->desc.bInterfaceNumber);
100	DEBUG("ft1000_probe: alternatesetting is %d\n",
101	      iface_desc->desc.bAlternateSetting);
102	DEBUG("ft1000_probe: interface class is %d\n",
103	      iface_desc->desc.bInterfaceClass);
104	DEBUG("ft1000_probe: control endpoint info:\n");
105	DEBUG("ft1000_probe: descriptor0 type -- %d\n",
106	      iface_desc->endpoint[0].desc.bmAttributes);
107	DEBUG("ft1000_probe: descriptor1 type -- %d\n",
108	      iface_desc->endpoint[1].desc.bmAttributes);
109	DEBUG("ft1000_probe: descriptor2 type -- %d\n",
110	      iface_desc->endpoint[2].desc.bmAttributes);
111
112	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
113		endpoint =
114		    (struct usb_endpoint_descriptor *)&iface_desc->
115		    endpoint[i].desc;
116		DEBUG("endpoint %d\n", i);
117		DEBUG("bEndpointAddress=%x, bmAttributes=%x\n",
118		      endpoint->bEndpointAddress, endpoint->bmAttributes);
119		if ((endpoint->bEndpointAddress & USB_DIR_IN)
120		    && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
121			USB_ENDPOINT_XFER_BULK)) {
122			ft1000dev->bulk_in_endpointAddr =
123			    endpoint->bEndpointAddress;
124			DEBUG("ft1000_probe: in: %d\n",
125			      endpoint->bEndpointAddress);
126		}
127
128		if (!(endpoint->bEndpointAddress & USB_DIR_IN)
129		    && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
130			USB_ENDPOINT_XFER_BULK)) {
131			ft1000dev->bulk_out_endpointAddr =
132			    endpoint->bEndpointAddress;
133			DEBUG("ft1000_probe: out: %d\n",
134			      endpoint->bEndpointAddress);
135		}
136	}
137
138	DEBUG("bulk_in=%d, bulk_out=%d\n", ft1000dev->bulk_in_endpointAddr,
139	      ft1000dev->bulk_out_endpointAddr);
140
141	ret = request_firmware(&dsp_fw, "ft3000.img", &dev->dev);
142	if (ret < 0) {
143		printk(KERN_ERR "Error request_firmware().\n");
144		goto err_fw;
145	}
146
147	size = max_t(uint, dsp_fw->size, 4096);
148	pFileStart = kmalloc(size, GFP_KERNEL);
149
150	if (!pFileStart) {
151		release_firmware(dsp_fw);
152		ret = -ENOMEM;
153		goto err_fw;
154	}
155
156	memcpy(pFileStart, dsp_fw->data, dsp_fw->size);
157	FileLength = dsp_fw->size;
158	release_firmware(dsp_fw);
159
160	DEBUG("ft1000_probe: start downloading dsp image...\n");
161
162	ret = init_ft1000_netdev(ft1000dev);
163	if (ret)
164		goto err_load;
165
166	pft1000info = netdev_priv(ft1000dev->net);
167
168	DEBUG("In probe: pft1000info=%p\n", pft1000info);
169	ret = dsp_reload(ft1000dev);
170	if (ret) {
171		printk(KERN_ERR "Problem with DSP image loading\n");
172		goto err_load;
173	}
174
175	gPollingfailed = FALSE;
176	pft1000info->pPollThread =
177	    kthread_run(ft1000_poll_thread, ft1000dev, "ft1000_poll");
178
179	if (IS_ERR(pft1000info->pPollThread)) {
180		ret = PTR_ERR(pft1000info->pPollThread);
181		goto err_load;
182	}
183
184	msleep(500);
185
186	while (!pft1000info->CardReady) {
187		if (gPollingfailed) {
188			ret = -EIO;
189			goto err_thread;
190		}
191		msleep(100);
192		DEBUG("ft1000_probe::Waiting for Card Ready\n");
193	}
194
195	DEBUG("ft1000_probe::Card Ready!!!! Registering network device\n");
196
197	ret = reg_ft1000_netdev(ft1000dev, interface);
198	if (ret)
199		goto err_thread;
200
201	ret = ft1000_init_proc(ft1000dev->net);
202	if (ret)
203		goto err_proc;
204
205	pft1000info->NetDevRegDone = 1;
206
207	return 0;
208
209err_proc:
210	unregister_netdev(ft1000dev->net);
211	free_netdev(ft1000dev->net);
212err_thread:
213	kthread_stop(pft1000info->pPollThread);
214err_load:
215	kfree(pFileStart);
216err_fw:
217	kfree(ft1000dev);
218	return ret;
219}
220
221static void ft1000_disconnect(struct usb_interface *interface)
222{
223	struct ft1000_info *pft1000info;
224
225	DEBUG("ft1000_disconnect is called\n");
226
227	pft1000info = (struct ft1000_info *) usb_get_intfdata(interface);
228	DEBUG("In disconnect pft1000info=%p\n", pft1000info);
229
230	if (pft1000info) {
231		ft1000_cleanup_proc(pft1000info);
232		if (pft1000info->pPollThread)
233			kthread_stop(pft1000info->pPollThread);
234
235		DEBUG("ft1000_disconnect: threads are terminated\n");
236
237		if (pft1000info->pFt1000Dev->net) {
238			DEBUG("ft1000_disconnect: destroy char driver\n");
239			ft1000_destroy_dev(pft1000info->pFt1000Dev->net);
240			unregister_netdev(pft1000info->pFt1000Dev->net);
241			DEBUG
242			    ("ft1000_disconnect: network device unregisterd\n");
243			free_netdev(pft1000info->pFt1000Dev->net);
244
245		}
246
247		usb_free_urb(pft1000info->pFt1000Dev->rx_urb);
248		usb_free_urb(pft1000info->pFt1000Dev->tx_urb);
249
250		DEBUG("ft1000_disconnect: urb freed\n");
251
252		kfree(pft1000info->pFt1000Dev);
253	}
254	kfree(pFileStart);
255
256	return;
257}
258
259static struct usb_driver ft1000_usb_driver = {
260	.name = "ft1000usb",
261	.probe = ft1000_probe,
262	.disconnect = ft1000_disconnect,
263	.id_table = id_table,
264};
265
266module_usb_driver(ft1000_usb_driver);
267