staging/ft1000-usb: fix problems found by sparse
[pandora-kernel.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 // 20090926; aelias; removed all compiler warnings; ubuntu 9.04; 2.6.28-15-generic
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 "ft1000_usb.h"
17
18 //#include <linux/sched.h>
19 //#include <linux/ptrace.h>
20 //#include <linux/slab.h>
21 //#include <linux/string.h>
22 //#include <linux/timer.h>
23 //#include <linux/netdevice.h>
24 //#include <linux/ioport.h>
25 //#include <linux/delay.h>
26 //#include <asm/io.h>
27 //#include <asm/system.h>
28 #include <linux/kthread.h>
29
30 MODULE_DESCRIPTION("FT1000 EXPRESS CARD DRIVER");
31 MODULE_LICENSE("Dual MPL/GPL");
32 MODULE_SUPPORTED_DEVICE("QFT FT1000 Express Cards");
33
34
35 void *pFileStart;
36 size_t FileLength;
37
38 #define VENDOR_ID 0x1291   /* Qualcomm vendor id */
39 #define PRODUCT_ID 0x11    /* fake product id */
40
41 /* table of devices that work with this driver */
42 static struct usb_device_id id_table[] = {
43     {USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
44     { },
45 };
46
47 MODULE_DEVICE_TABLE (usb, id_table);
48
49 static BOOLEAN gPollingfailed = FALSE;
50 int ft1000_poll_thread(void *arg)
51 {
52     int ret = STATUS_SUCCESS;
53
54     while(!kthread_should_stop() )
55     {
56         msleep(10);
57         if ( ! gPollingfailed )
58         {
59             ret = ft1000_poll(arg);
60             if ( ret != STATUS_SUCCESS )
61             {
62                 DEBUG("ft1000_poll_thread: polling failed\n");
63                 gPollingfailed = TRUE;
64             }
65         }
66     }
67     //DEBUG("returned from polling thread\n");
68     return STATUS_SUCCESS;
69 }
70
71
72
73 //---------------------------------------------------------------------------
74 // Function:    ft1000_probe
75 //
76 // Parameters:  struct usb_interface *interface  - passed by USB core
77 //              struct usb_device_id *id         - passed by USB core
78 // Returns:     0 - success
79 //
80 // Description: This function is invoked when the express card is plugged in
81 //
82 // Notes:
83 //
84 //---------------------------------------------------------------------------
85 static int ft1000_probe(struct usb_interface *interface, const struct usb_device_id *id)
86 {
87     struct usb_host_interface *iface_desc;
88     struct usb_endpoint_descriptor *endpoint;
89     struct usb_device *dev;
90     unsigned numaltsetting;
91     int i;
92
93     struct ft1000_device *ft1000dev;
94     FT1000_INFO *pft1000info;
95
96     if(!(ft1000dev = kmalloc(sizeof(struct ft1000_device), GFP_KERNEL)))
97     {
98         printk("out of memory allocating device structure\n");
99         return 0;
100     }
101
102     memset(ft1000dev, 0, sizeof(*ft1000dev));
103
104         //get usb device
105     dev = interface_to_usbdev(interface);
106     DEBUG("ft1000_probe: usb device descriptor info:\n");
107     DEBUG("ft1000_probe: number of configuration is %d\n", dev->descriptor.bNumConfigurations);
108
109         ft1000dev->dev = dev;
110         ft1000dev->status = 0;
111         ft1000dev->net = NULL;
112         //ft1000dev->device_lock = SPIN_LOCK_UNLOCKED;
113         spin_lock_init(&ft1000dev->device_lock);
114         ft1000dev->tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
115         ft1000dev->rx_urb = usb_alloc_urb(0, GFP_ATOMIC);
116
117
118     DEBUG("ft1000_probe is called\n");
119     numaltsetting = interface->num_altsetting;
120     DEBUG("ft1000_probe: number of alt settings is :%d\n",numaltsetting);
121     iface_desc = interface->cur_altsetting;
122     DEBUG("ft1000_probe: number of endpoints is %d\n", iface_desc->desc.bNumEndpoints);
123     DEBUG("ft1000_probe: descriptor type is %d\n", iface_desc->desc.bDescriptorType);
124     DEBUG("ft1000_probe: interface number is %d\n", iface_desc->desc.bInterfaceNumber);
125     DEBUG("ft1000_probe: alternatesetting is %d\n", iface_desc->desc.bAlternateSetting);
126     DEBUG("ft1000_probe: interface class is %d\n", iface_desc->desc.bInterfaceClass);
127     DEBUG("ft1000_probe: control endpoint info:\n");
128     DEBUG("ft1000_probe: descriptor0 type -- %d\n", iface_desc->endpoint[0].desc.bmAttributes);
129     DEBUG("ft1000_probe: descriptor1 type -- %d\n", iface_desc->endpoint[1].desc.bmAttributes);
130     DEBUG("ft1000_probe: descriptor2 type -- %d\n", iface_desc->endpoint[2].desc.bmAttributes);
131
132     for (i=0; i< iface_desc->desc.bNumEndpoints;i++ )
133     {
134                 endpoint = (struct usb_endpoint_descriptor *)&iface_desc->endpoint[i].desc;
135                 DEBUG("endpoint %d\n", i);
136                 DEBUG("bEndpointAddress=%x, bmAttributes=%x\n", endpoint->bEndpointAddress, endpoint->bmAttributes);
137                 if ( (endpoint->bEndpointAddress & USB_DIR_IN) && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK))
138                 {
139                         ft1000dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
140                         DEBUG("ft1000_probe: in: %d\n", endpoint->bEndpointAddress);
141                 }
142
143                 if (!(endpoint->bEndpointAddress & USB_DIR_IN) && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK))
144                 {
145                         ft1000dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
146                         DEBUG("ft1000_probe: out: %d\n", endpoint->bEndpointAddress);
147                 }
148     }
149
150     DEBUG("bulk_in=%d, bulk_out=%d\n", ft1000dev->bulk_in_endpointAddr, ft1000dev->bulk_out_endpointAddr);
151
152     //read DSP image
153     pFileStart = (void*)getfw("/etc/flarion/ft3000.img", &FileLength);
154
155     if (pFileStart == NULL )
156     {
157         DEBUG ("ft1000_probe: Read DSP image failed\n");
158         return 0;
159     }
160
161     //for ( i=0; i< MAX_NUM_CARDS+2; i++)
162     //    pdevobj[i] = NULL;
163
164     //download dsp image
165     DEBUG("ft1000_probe: start downloading dsp image...\n");
166     init_ft1000_netdev(ft1000dev);
167     pft1000info = (FT1000_INFO *) netdev_priv (ft1000dev->net);
168
169 //    DEBUG("In probe: pft1000info=%x\n", pft1000info);                         // aelias [-] reason: warning: format ???%x??? expects type ???unsigned int???, but argument 2 has type ???struct FT1000_INFO *???
170     DEBUG("In probe: pft1000info=%p\n", pft1000info);           // aelias [+] reason: up
171
172     dsp_reload(ft1000dev);
173     gPollingfailed = FALSE;  //mbelian
174     pft1000info->pPollThread = kthread_run(ft1000_poll_thread, ft1000dev, "ft1000_poll");
175         msleep(500); //mbelian
176
177
178     if ( pft1000info->DSP_loading )
179     {
180         DEBUG("ERROR!!!! RETURN FROM ft1000_probe **********************\n");
181         return 0;
182     }
183
184     while (!pft1000info->CardReady)
185     {
186         if ( gPollingfailed )
187         {
188             if ( pft1000info->pPollThread )
189             {
190                 kthread_stop(pft1000info->pPollThread );
191             }
192             return 0;
193         }
194         msleep(100);
195         DEBUG("ft1000_probe::Waiting for Card Ready\n");
196     }
197
198
199     //initialize network device
200     DEBUG("ft1000_probe::Card Ready!!!! Registering network device\n");
201
202     reg_ft1000_netdev(ft1000dev, interface);
203
204     pft1000info->NetDevRegDone = 1;
205
206                 ft1000InitProc(ft1000dev->net);// +mbelian
207
208        return 0;
209 }
210
211 //---------------------------------------------------------------------------
212 // Function:    ft1000_disconnect
213 //
214 // Parameters:  struct usb_interface *interface  - passed by USB core
215 //
216 // Returns:     0 - success
217 //
218 // Description: This function is invoked when the express card is plugged out
219 //
220 // Notes:
221 //
222 //---------------------------------------------------------------------------
223 static void ft1000_disconnect(struct usb_interface *interface)
224 {
225     FT1000_INFO *pft1000info;
226
227     DEBUG("ft1000_disconnect is called\n");
228
229     pft1000info = (PFT1000_INFO)usb_get_intfdata(interface);
230 //    DEBUG("In disconnect pft1000info=%x\n", pft1000info);     // aelias [-] reason: warning: format ???%x??? expects type ???unsigned int???, but argument 2 has type ???struct FT1000_INFO *???
231     DEBUG("In disconnect pft1000info=%p\n", pft1000info);       // aelias [+] reason: up
232
233
234
235     if (pft1000info)
236     {
237                 ft1000CleanupProc(pft1000info); //+mbelian
238         if ( pft1000info->pPollThread )
239         {
240             kthread_stop(pft1000info->pPollThread );
241         }
242
243         DEBUG("ft1000_disconnect: threads are terminated\n");
244
245         if (pft1000info->pFt1000Dev->net)
246         {
247             DEBUG("ft1000_disconnect: destroy char driver\n");
248             ft1000_DestroyDevice(pft1000info->pFt1000Dev->net);
249             //DEBUG("ft1000_disconnect: calling ft1000_close\n");
250             //ft1000_close(pft1000info->pFt1000Dev->net);
251             //DEBUG("ft1000_disconnect: ft1000_close is called\n");
252             unregister_netdev(pft1000info->pFt1000Dev->net);
253             DEBUG("ft1000_disconnect: network device unregisterd\n");
254             free_netdev(pft1000info->pFt1000Dev->net);
255
256         }
257
258         usb_free_urb(pft1000info->pFt1000Dev->rx_urb);
259         usb_free_urb(pft1000info->pFt1000Dev->tx_urb);
260
261         DEBUG("ft1000_disconnect: urb freed\n");
262
263                 kfree(pft1000info->pFt1000Dev); //+mbelian
264     }
265
266     //terminate other kernel threads
267     //in multiple instances case, first find the device
268     //in the link list
269     /**if (pPollThread)
270     {
271         kthread_stop(pPollThread);
272         DEBUG("Polling thread is killed \n");
273     }**/
274
275     return;
276 }
277
278 static struct usb_driver ft1000_usb_driver = {
279     //.owner =    THIS_MODULE,
280     .name  =    "ft1000usb",
281     .probe =    ft1000_probe,
282     .disconnect = ft1000_disconnect,
283     .id_table = id_table,
284 };
285
286 //---------------------------------------------------------------------------
287 // Function:    usb_ft1000_init
288 //
289 // Parameters:  none
290 //
291 // Returns:     0 - success
292 //
293 // Description: The entry point of the module, register the usb driver
294 //
295 // Notes:
296 //
297 //---------------------------------------------------------------------------
298 static int __init usb_ft1000_init(void)
299 {
300     int ret = 0;
301
302     DEBUG("Initialize and register the driver\n");
303
304     ret = usb_register(&ft1000_usb_driver);
305     if (ret)
306         err("usb_register failed. Error number %d", ret);
307
308     return ret;
309 }
310
311 //---------------------------------------------------------------------------
312 // Function:    usb_ft1000_exit
313 //
314 // Parameters:
315 //
316 // Returns:
317 //
318 // Description: Moudle unload function, deregister usb driver
319 //
320 // Notes:
321 //
322 //---------------------------------------------------------------------------
323 static void __exit usb_ft1000_exit(void)
324 {
325     DEBUG("Deregister the driver\n");
326     usb_deregister(&ft1000_usb_driver);
327 }
328
329 module_init (usb_ft1000_init);
330 module_exit (usb_ft1000_exit);
331
332