Merge branch 'master' of git://git.infradead.org/users/pcmoore/selinux into next
[firefly-linux-kernel-4.4.55.git] / drivers / staging / ft1000 / ft1000-usb / ft1000_usb.c
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
21 MODULE_DESCRIPTION("FT1000 EXPRESS CARD DRIVER");
22 MODULE_LICENSE("Dual MPL/GPL");
23 MODULE_SUPPORTED_DEVICE("QFT FT1000 Express Cards");
24
25 void *pFileStart;
26 size_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 */
32 static struct usb_device_id id_table[] = {
33         {USB_DEVICE(VENDOR_ID, PRODUCT_ID)},
34         {},
35 };
36
37 MODULE_DEVICE_TABLE(usb, id_table);
38
39 static bool gPollingfailed = false;
40 static int ft1000_poll_thread(void *arg)
41 {
42         int ret;
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
57 static 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_usb *ft1000dev;
67         struct ft1000_info *pft1000info = NULL;
68         const struct firmware *dsp_fw;
69
70         ft1000dev = kzalloc(sizeof(struct ft1000_usb), GFP_KERNEL);
71         if (!ft1000dev)
72                 return -ENOMEM;
73
74         dev = interface_to_usbdev(interface);
75         DEBUG("ft1000_probe: usb device descriptor info:\n");
76         DEBUG("ft1000_probe: number of configuration is %d\n",
77               dev->descriptor.bNumConfigurations);
78
79         ft1000dev->dev = dev;
80         ft1000dev->status = 0;
81         ft1000dev->net = NULL;
82         ft1000dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
83         ft1000dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
84         if (!ft1000dev->tx_urb || !ft1000dev->rx_urb) {
85                 ret = -ENOMEM;
86                 goto err_fw;
87         }
88
89         DEBUG("ft1000_probe is called\n");
90         numaltsetting = interface->num_altsetting;
91         DEBUG("ft1000_probe: number of alt settings is :%d\n", numaltsetting);
92         iface_desc = interface->cur_altsetting;
93         DEBUG("ft1000_probe: number of endpoints is %d\n",
94               iface_desc->desc.bNumEndpoints);
95         DEBUG("ft1000_probe: descriptor type is %d\n",
96               iface_desc->desc.bDescriptorType);
97         DEBUG("ft1000_probe: interface number is %d\n",
98               iface_desc->desc.bInterfaceNumber);
99         DEBUG("ft1000_probe: alternatesetting is %d\n",
100               iface_desc->desc.bAlternateSetting);
101         DEBUG("ft1000_probe: interface class is %d\n",
102               iface_desc->desc.bInterfaceClass);
103         DEBUG("ft1000_probe: control endpoint info:\n");
104         DEBUG("ft1000_probe: descriptor0 type -- %d\n",
105               iface_desc->endpoint[0].desc.bmAttributes);
106         DEBUG("ft1000_probe: descriptor1 type -- %d\n",
107               iface_desc->endpoint[1].desc.bmAttributes);
108         DEBUG("ft1000_probe: descriptor2 type -- %d\n",
109               iface_desc->endpoint[2].desc.bmAttributes);
110
111         for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
112                 endpoint =
113                     (struct usb_endpoint_descriptor *)&iface_desc->
114                     endpoint[i].desc;
115                 DEBUG("endpoint %d\n", i);
116                 DEBUG("bEndpointAddress=%x, bmAttributes=%x\n",
117                       endpoint->bEndpointAddress, endpoint->bmAttributes);
118                 if ((endpoint->bEndpointAddress & USB_DIR_IN)
119                     && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
120                         USB_ENDPOINT_XFER_BULK)) {
121                         ft1000dev->bulk_in_endpointAddr =
122                             endpoint->bEndpointAddress;
123                         DEBUG("ft1000_probe: in: %d\n",
124                               endpoint->bEndpointAddress);
125                 }
126
127                 if (!(endpoint->bEndpointAddress & USB_DIR_IN)
128                     && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
129                         USB_ENDPOINT_XFER_BULK)) {
130                         ft1000dev->bulk_out_endpointAddr =
131                             endpoint->bEndpointAddress;
132                         DEBUG("ft1000_probe: out: %d\n",
133                               endpoint->bEndpointAddress);
134                 }
135         }
136
137         DEBUG("bulk_in=%d, bulk_out=%d\n", ft1000dev->bulk_in_endpointAddr,
138               ft1000dev->bulk_out_endpointAddr);
139
140         ret = request_firmware(&dsp_fw, "ft3000.img", &dev->dev);
141         if (ret < 0) {
142                 pr_err("Error request_firmware().\n");
143                 goto err_fw;
144         }
145
146         size = max_t(uint, dsp_fw->size, 4096);
147         pFileStart = kmalloc(size, GFP_KERNEL);
148
149         if (!pFileStart) {
150                 release_firmware(dsp_fw);
151                 ret = -ENOMEM;
152                 goto err_fw;
153         }
154
155         memcpy(pFileStart, dsp_fw->data, dsp_fw->size);
156         FileLength = dsp_fw->size;
157         release_firmware(dsp_fw);
158
159         DEBUG("ft1000_probe: start downloading dsp image...\n");
160
161         ret = init_ft1000_netdev(ft1000dev);
162         if (ret)
163                 goto err_load;
164
165         pft1000info = netdev_priv(ft1000dev->net);
166
167         DEBUG("In probe: pft1000info=%p\n", pft1000info);
168         ret = dsp_reload(ft1000dev);
169         if (ret) {
170                 pr_err("Problem with DSP image loading\n");
171                 goto err_load;
172         }
173
174         gPollingfailed = false;
175         ft1000dev->pPollThread =
176             kthread_run(ft1000_poll_thread, ft1000dev, "ft1000_poll");
177
178         if (IS_ERR(ft1000dev->pPollThread)) {
179                 ret = PTR_ERR(ft1000dev->pPollThread);
180                 goto err_load;
181         }
182
183         msleep(500);
184
185         while (!pft1000info->CardReady) {
186                 if (gPollingfailed) {
187                         ret = -EIO;
188                         goto err_thread;
189                 }
190                 msleep(100);
191                 DEBUG("ft1000_probe::Waiting for Card Ready\n");
192         }
193
194         DEBUG("ft1000_probe::Card Ready!!!! Registering network device\n");
195
196         ret = reg_ft1000_netdev(ft1000dev, interface);
197         if (ret)
198                 goto err_thread;
199
200         ret = ft1000_init_proc(ft1000dev->net);
201         if (ret)
202                 goto err_proc;
203
204         ft1000dev->NetDevRegDone = 1;
205
206         return 0;
207
208 err_proc:
209         unregister_netdev(ft1000dev->net);
210         free_netdev(ft1000dev->net);
211 err_thread:
212         kthread_stop(ft1000dev->pPollThread);
213 err_load:
214         kfree(pFileStart);
215 err_fw:
216         usb_free_urb(ft1000dev->rx_urb);
217         usb_free_urb(ft1000dev->tx_urb);
218         kfree(ft1000dev);
219         return ret;
220 }
221
222 static void ft1000_disconnect(struct usb_interface *interface)
223 {
224         struct ft1000_info *pft1000info;
225         struct ft1000_usb *ft1000dev;
226
227         DEBUG("ft1000_disconnect is called\n");
228
229         pft1000info = (struct ft1000_info *) usb_get_intfdata(interface);
230         DEBUG("In disconnect pft1000info=%p\n", pft1000info);
231
232         if (pft1000info) {
233                 ft1000dev = pft1000info->priv;
234                 ft1000_cleanup_proc(pft1000info);
235                 if (ft1000dev->pPollThread)
236                         kthread_stop(ft1000dev->pPollThread);
237
238                 DEBUG("ft1000_disconnect: threads are terminated\n");
239
240                 if (ft1000dev->net) {
241                         DEBUG("ft1000_disconnect: destroy char driver\n");
242                         ft1000_destroy_dev(ft1000dev->net);
243                         unregister_netdev(ft1000dev->net);
244                         DEBUG
245                             ("ft1000_disconnect: network device unregistered\n");
246                         free_netdev(ft1000dev->net);
247
248                 }
249
250                 usb_free_urb(ft1000dev->rx_urb);
251                 usb_free_urb(ft1000dev->tx_urb);
252
253                 DEBUG("ft1000_disconnect: urb freed\n");
254
255                 kfree(ft1000dev);
256         }
257         kfree(pFileStart);
258
259         return;
260 }
261
262 static struct usb_driver ft1000_usb_driver = {
263         .name = "ft1000usb",
264         .probe = ft1000_probe,
265         .disconnect = ft1000_disconnect,
266         .id_table = id_table,
267 };
268
269 module_usb_driver(ft1000_usb_driver);