Staging: go7007: Move a dereference below a NULL test
[linux-2.6] / drivers / staging / go7007 / s2250-loader.c
1 /*
2  * Copyright (C) 2008 Sensoray Company Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License (Version 2) as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
16  */
17
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/usb.h>
21 #include <dvb-usb.h>
22
23 #define S2250_LOADER_FIRMWARE   "s2250_loader.fw"
24 #define S2250_FIRMWARE          "s2250.fw"
25
26 typedef struct device_extension_s {
27     struct kref     kref;
28     int minor;
29     struct usb_device *usbdev;
30 } device_extension_t, *pdevice_extension_t;
31
32 #define USB_s2250loader_MAJOR 240
33 #define USB_s2250loader_MINOR_BASE 0
34 #define MAX_DEVICES 256
35
36 static pdevice_extension_t s2250_dev_table[MAX_DEVICES];
37 static DECLARE_MUTEX(s2250_dev_table_mutex);
38
39 #define to_s2250loader_dev_common(d) container_of(d, device_extension_t, kref)
40 static void s2250loader_delete(struct kref *kref)
41 {
42         pdevice_extension_t s = to_s2250loader_dev_common(kref);
43         s2250_dev_table[s->minor] = NULL;
44         kfree(s);
45 }
46
47 static int s2250loader_probe(struct usb_interface *interface,
48                                 const struct usb_device_id *id)
49 {
50         struct usb_device *usbdev;
51         int minor, ret;
52         pdevice_extension_t s = NULL;
53         const struct firmware *fw;
54
55         usbdev = usb_get_dev(interface_to_usbdev(interface));
56         if (!usbdev) {
57                 printk(KERN_ERR "Enter s2250loader_probe failed\n");
58                 return -1;
59         }
60         printk(KERN_INFO "Enter s2250loader_probe 2.6 kernel\n");
61         printk(KERN_INFO "vendor id 0x%x, device id 0x%x devnum:%d\n",
62            usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
63            usbdev->devnum);
64
65         if (usbdev->descriptor.bNumConfigurations != 1) {
66                 printk(KERN_ERR "can't handle multiple config\n");
67                 return -1;
68         }
69         down(&s2250_dev_table_mutex);
70
71         for (minor = 0; minor < MAX_DEVICES; minor++) {
72                 if (s2250_dev_table[minor] == NULL)
73                         break;
74         }
75
76         if (minor < 0 || minor >= MAX_DEVICES) {
77                 printk(KERN_ERR "Invalid minor: %d\n", minor);
78                 goto failed;
79         }
80
81         /* Allocate dev data structure */
82         s = kmalloc(sizeof(device_extension_t), GFP_KERNEL);
83         if (s == NULL) {
84                 printk(KERN_ERR "Out of memory\n");
85                 goto failed;
86         }
87         s2250_dev_table[minor] = s;
88
89         printk(KERN_INFO "s2250loader_probe: Device %d on Bus %d Minor %d\n",
90                 usbdev->devnum, usbdev->bus->busnum, minor);
91
92         memset(s, 0, sizeof(device_extension_t));
93         s->usbdev = usbdev;
94         printk(KERN_INFO "loading 2250 loader\n");
95
96         kref_init(&(s->kref));
97
98         up(&s2250_dev_table_mutex);
99
100         if (request_firmware(&fw, S2250_LOADER_FIRMWARE, &usbdev->dev)) {
101                 printk(KERN_ERR
102                         "s2250: unable to load firmware from file \"%s\"\n",
103                         S2250_LOADER_FIRMWARE);
104                 goto failed2;
105         }
106         ret = usb_cypress_load_firmware(usbdev, fw, CYPRESS_FX2);
107         release_firmware(fw);
108         if (0 != ret) {
109                 printk(KERN_ERR "loader download failed\n");
110                 goto failed2;
111         }
112
113         if (request_firmware(&fw, S2250_FIRMWARE, &usbdev->dev)) {
114                 printk(KERN_ERR
115                         "s2250: unable to load firmware from file \"%s\"\n",
116                         S2250_FIRMWARE);
117                 goto failed2;
118         }
119         ret = usb_cypress_load_firmware(usbdev, fw, CYPRESS_FX2);
120         release_firmware(fw);
121         if (0 != ret) {
122                 printk(KERN_ERR "firmware_s2250 download failed\n");
123                 goto failed2;
124         }
125
126         usb_set_intfdata(interface, s);
127         return 0;
128
129 failed:
130         up(&s2250_dev_table_mutex);
131 failed2:
132         if (s)
133                 kref_put(&(s->kref), s2250loader_delete);
134
135         printk(KERN_ERR "probe failed\n");
136         return -1;
137 }
138
139 static void s2250loader_disconnect(struct usb_interface *interface)
140 {
141         pdevice_extension_t s = usb_get_intfdata(interface);
142         printk(KERN_INFO "s2250: disconnect\n");
143         lock_kernel();
144         s = usb_get_intfdata(interface);
145         usb_set_intfdata(interface, NULL);
146         kref_put(&(s->kref), s2250loader_delete);
147         unlock_kernel();
148 }
149
150 static struct usb_device_id s2250loader_ids[] = {
151         {USB_DEVICE(0x1943, 0xa250)},
152         {}                          /* Terminating entry */
153 };
154
155 MODULE_DEVICE_TABLE(usb, s2250loader_ids);
156
157 static struct usb_driver s2250loader_driver = {
158         .name           = "s2250-loader",
159         .probe          = s2250loader_probe,
160         .disconnect     = s2250loader_disconnect,
161         .id_table       = s2250loader_ids,
162 };
163
164 int s2250loader_init(void)
165 {
166         int r;
167         unsigned i = 0;
168
169         for (i = 0; i < MAX_DEVICES; i++)
170                 s2250_dev_table[i] = NULL;
171
172         r = usb_register(&s2250loader_driver);
173         if (r) {
174                 printk(KERN_ERR "usb_register failed. Error number %d\n", r);
175                 return -1;
176         }
177
178         printk(KERN_INFO "s2250loader_init: driver registered\n");
179         return 0;
180 }
181 EXPORT_SYMBOL(s2250loader_init);
182
183 void s2250loader_cleanup(void)
184 {
185         printk(KERN_INFO "s2250loader_cleanup\n");
186         usb_deregister(&s2250loader_driver);
187 }
188 EXPORT_SYMBOL(s2250loader_cleanup);