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