upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / bluetooth / bcm203x.c
1 /*
2  *
3  *  Broadcom Blutonium firmware driver
4  *
5  *  Copyright (C) 2003  Maxim Krasnyansky <maxk@qualcomm.com>
6  *  Copyright (C) 2003  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
32 #include <linux/errno.h>
33 #include <linux/timer.h>
34
35 #include <linux/device.h>
36 #include <linux/firmware.h>
37
38 #include <linux/usb.h>
39
40 #include <net/bluetooth/bluetooth.h>
41
42 #ifndef CONFIG_BT_HCIBCM203X_DEBUG
43 #undef  BT_DBG
44 #define BT_DBG(D...)
45 #endif
46
47 #define VERSION "1.0"
48
49 static struct usb_device_id bcm203x_table[] = {
50         /* Broadcom Blutonium (BCM2033) */
51         { USB_DEVICE(0x0a5c, 0x2033) },
52
53         { }     /* Terminating entry */
54 };
55
56 MODULE_DEVICE_TABLE(usb, bcm203x_table);
57
58
59 #define BCM203X_ERROR           0
60 #define BCM203X_RESET           1
61 #define BCM203X_LOAD_MINIDRV    2
62 #define BCM203X_SELECT_MEMORY   3
63 #define BCM203X_CHECK_MEMORY    4
64 #define BCM203X_LOAD_FIRMWARE   5
65 #define BCM203X_CHECK_FIRMWARE  6
66
67 #define BCM203X_IN_EP           0x81
68 #define BCM203X_OUT_EP          0x02
69
70 struct bcm203x_data {
71         struct usb_device       *udev;
72
73         unsigned long           state;
74
75         struct timer_list       timer;
76
77         struct urb              *urb;
78         unsigned char           *buffer;
79
80         unsigned char           *fw_data;
81         unsigned int            fw_size;
82         unsigned int            fw_sent;
83 };
84
85 static void bcm203x_complete(struct urb *urb, struct pt_regs *regs)
86 {
87         struct bcm203x_data *data = urb->context;
88         struct usb_device *udev = urb->dev;
89         int len;
90
91         BT_DBG("udev %p urb %p", udev, urb);
92
93         if (urb->status) {
94                 BT_ERR("URB failed with status %d", urb->status);
95                 data->state = BCM203X_ERROR;
96                 return;
97         }
98
99         switch (data->state) {
100         case BCM203X_LOAD_MINIDRV:
101                 memcpy(data->buffer, "#", 1);
102
103                 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
104                                 data->buffer, 1, bcm203x_complete, data);
105
106                 data->state = BCM203X_SELECT_MEMORY;
107
108                 mod_timer(&data->timer, jiffies + (HZ / 10));
109                 break;
110
111         case BCM203X_SELECT_MEMORY:
112                 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, BCM203X_IN_EP),
113                                 data->buffer, 32, bcm203x_complete, data, 1);
114
115                 data->state = BCM203X_CHECK_MEMORY;
116
117                 if (usb_submit_urb(data->urb, GFP_ATOMIC) < 0)
118                         BT_ERR("Can't submit URB");
119                 break;
120
121         case BCM203X_CHECK_MEMORY:
122                 if (data->buffer[0] != '#') {
123                         BT_ERR("Memory select failed");
124                         data->state = BCM203X_ERROR;
125                         break;
126                 }
127
128                 data->state = BCM203X_LOAD_FIRMWARE;
129
130         case BCM203X_LOAD_FIRMWARE:
131                 if (data->fw_sent == data->fw_size) {
132                         usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, BCM203X_IN_EP),
133                                 data->buffer, 32, bcm203x_complete, data, 1);
134
135                         data->state = BCM203X_CHECK_FIRMWARE;
136                 } else {
137                         len = min_t(uint, data->fw_size - data->fw_sent, 4096);
138
139                         usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
140                                 data->fw_data + data->fw_sent, len, bcm203x_complete, data);
141
142                         data->fw_sent += len;
143                 }
144
145                 if (usb_submit_urb(data->urb, GFP_ATOMIC) < 0)
146                         BT_ERR("Can't submit URB");
147                 break;
148
149         case BCM203X_CHECK_FIRMWARE:
150                 if (data->buffer[0] != '.') {
151                         BT_ERR("Firmware loading failed");
152                         data->state = BCM203X_ERROR;
153                         break;
154                 }
155
156                 data->state = BCM203X_RESET;
157                 break;
158         }
159 }
160
161 static void bcm203x_timer(unsigned long user_data)
162 {
163         struct bcm203x_data *data = (struct bcm203x_data *) user_data;
164
165         if (usb_submit_urb(data->urb, GFP_ATOMIC) < 0)
166                 BT_ERR("Can't submit URB");
167 }
168
169 static int bcm203x_probe(struct usb_interface *intf, const struct usb_device_id *id)
170 {
171         const struct firmware *firmware;
172         struct usb_device *udev = interface_to_usbdev(intf);
173         struct bcm203x_data *data;
174         int size;
175
176         BT_DBG("intf %p id %p", intf, id);
177
178         if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
179                 return -ENODEV;
180
181         data = kmalloc(sizeof(*data), GFP_KERNEL);
182         if (!data) {
183                 BT_ERR("Can't allocate memory for data structure");
184                 return -ENOMEM;
185         }
186
187         memset(data, 0, sizeof(*data));
188
189         data->udev  = udev;
190         data->state = BCM203X_LOAD_MINIDRV;
191
192         data->urb = usb_alloc_urb(0, GFP_KERNEL);
193         if (!data->urb) {
194                 BT_ERR("Can't allocate URB");
195                 kfree(data);
196                 return -ENOMEM;
197         }
198
199         if (request_firmware(&firmware, "BCM2033-MD.hex", &udev->dev) < 0) {
200                 BT_ERR("Mini driver request failed");
201                 usb_free_urb(data->urb);
202                 kfree(data);
203                 return -EIO;
204         }
205
206         BT_DBG("minidrv data %p size %d", firmware->data, firmware->size);
207
208         size = max_t(uint, firmware->size, 4096);
209
210         data->buffer = kmalloc(size, GFP_KERNEL);
211         if (!data->buffer) {
212                 BT_ERR("Can't allocate memory for mini driver");
213                 release_firmware(firmware);
214                 usb_free_urb(data->urb);
215                 kfree(data);
216                 return -ENOMEM;
217         }
218
219         memcpy(data->buffer, firmware->data, firmware->size);
220
221         usb_fill_bulk_urb(data->urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
222                         data->buffer, firmware->size, bcm203x_complete, data);
223
224         release_firmware(firmware);
225
226         if (request_firmware(&firmware, "BCM2033-FW.bin", &udev->dev) < 0) {
227                 BT_ERR("Firmware request failed");
228                 usb_free_urb(data->urb);
229                 kfree(data->buffer);
230                 kfree(data);
231                 return -EIO;
232         }
233
234         BT_DBG("firmware data %p size %d", firmware->data, firmware->size);
235
236         data->fw_data = kmalloc(firmware->size, GFP_KERNEL);
237         if (!data->fw_data) {
238                 BT_ERR("Can't allocate memory for firmware image");
239                 usb_free_urb(data->urb);
240                 kfree(data->buffer);
241                 kfree(data);
242                 return -ENOMEM;
243         }
244
245         memcpy(data->fw_data, firmware->data, firmware->size);
246         data->fw_size = firmware->size;
247         data->fw_sent = 0;
248
249         release_firmware(firmware);
250
251         init_timer(&data->timer);
252         data->timer.function = bcm203x_timer;
253         data->timer.data = (unsigned long) data;
254
255         usb_set_intfdata(intf, data);
256
257         mod_timer(&data->timer, jiffies + HZ);
258
259         return 0;
260 }
261
262 static void bcm203x_disconnect(struct usb_interface *intf)
263 {
264         struct bcm203x_data *data = usb_get_intfdata(intf);
265
266         BT_DBG("intf %p", intf);
267
268         usb_kill_urb(data->urb);
269
270         usb_set_intfdata(intf, NULL);
271
272         usb_free_urb(data->urb);
273         kfree(data->fw_data);
274         kfree(data->buffer);
275         kfree(data);
276 }
277
278 static struct usb_driver bcm203x_driver = {
279         .owner          = THIS_MODULE,
280         .name           = "bcm203x",
281         .probe          = bcm203x_probe,
282         .disconnect     = bcm203x_disconnect,
283         .id_table       = bcm203x_table,
284 };
285
286 static int __init bcm203x_init(void)
287 {
288         int err;
289
290         BT_INFO("Broadcom Blutonium firmware driver ver %s", VERSION);
291
292         err = usb_register(&bcm203x_driver);
293         if (err < 0)
294                 BT_ERR("Failed to register USB driver");
295
296         return err;
297 }
298
299 static void __exit bcm203x_exit(void)
300 {
301         usb_deregister(&bcm203x_driver);
302 }
303
304 module_init(bcm203x_init);
305 module_exit(bcm203x_exit);
306
307 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
308 MODULE_DESCRIPTION("Broadcom Blutonium firmware driver ver " VERSION);
309 MODULE_VERSION(VERSION);
310 MODULE_LICENSE("GPL");