blob: 8919ccf8274b3141c4fc240f7b83fc12b7203708 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/device.h>
34#include <linux/firmware.h>
35
36#include <linux/usb.h>
37
38#include <net/bluetooth/bluetooth.h>
39
40#ifndef CONFIG_BT_HCIBCM203X_DEBUG
41#undef BT_DBG
42#define BT_DBG(D...)
43#endif
44
Marcel Holtmann3f530692006-10-15 17:31:19 +020045#define VERSION "1.1"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static int ignore = 0;
48
49static struct usb_device_id bcm203x_table[] = {
50 /* Broadcom Blutonium (BCM2033) */
51 { USB_DEVICE(0x0a5c, 0x2033) },
52
53 { } /* Terminating entry */
54};
55
56MODULE_DEVICE_TABLE(usb, bcm203x_table);
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
69struct bcm203x_data {
70 struct usb_device *udev;
71
72 unsigned long state;
73
Marcel Holtmann3f530692006-10-15 17:31:19 +020074 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
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
David Howells7d12e782006-10-05 14:55:46 +010084static void bcm203x_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
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
Marcel Holtmann3f530692006-10-15 17:31:19 +0200107 schedule_work(&data->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 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
David Howellsc4028952006-11-22 14:57:56 +0000160static void bcm203x_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
David Howellsc4028952006-11-22 14:57:56 +0000162 struct bcm203x_data *data =
163 container_of(work, struct bcm203x_data, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 if (usb_submit_urb(data->urb, GFP_ATOMIC) < 0)
166 BT_ERR("Can't submit URB");
167}
168
169static 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 (ignore || (intf->cur_altsetting->desc.bInterfaceNumber != 0))
179 return -ENODEV;
180
Deepak Saxena089b1db2005-11-07 01:01:26 -0800181 data = kzalloc(sizeof(*data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (!data) {
183 BT_ERR("Can't allocate memory for data structure");
184 return -ENOMEM;
185 }
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 data->udev = udev;
188 data->state = BCM203X_LOAD_MINIDRV;
189
190 data->urb = usb_alloc_urb(0, GFP_KERNEL);
191 if (!data->urb) {
192 BT_ERR("Can't allocate URB");
193 kfree(data);
194 return -ENOMEM;
195 }
196
197 if (request_firmware(&firmware, "BCM2033-MD.hex", &udev->dev) < 0) {
198 BT_ERR("Mini driver request failed");
199 usb_free_urb(data->urb);
200 kfree(data);
201 return -EIO;
202 }
203
204 BT_DBG("minidrv data %p size %d", firmware->data, firmware->size);
205
206 size = max_t(uint, firmware->size, 4096);
207
208 data->buffer = kmalloc(size, GFP_KERNEL);
209 if (!data->buffer) {
210 BT_ERR("Can't allocate memory for mini driver");
211 release_firmware(firmware);
212 usb_free_urb(data->urb);
213 kfree(data);
214 return -ENOMEM;
215 }
216
217 memcpy(data->buffer, firmware->data, firmware->size);
218
219 usb_fill_bulk_urb(data->urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
220 data->buffer, firmware->size, bcm203x_complete, data);
221
222 release_firmware(firmware);
223
224 if (request_firmware(&firmware, "BCM2033-FW.bin", &udev->dev) < 0) {
225 BT_ERR("Firmware request failed");
226 usb_free_urb(data->urb);
227 kfree(data->buffer);
228 kfree(data);
229 return -EIO;
230 }
231
232 BT_DBG("firmware data %p size %d", firmware->data, firmware->size);
233
234 data->fw_data = kmalloc(firmware->size, GFP_KERNEL);
235 if (!data->fw_data) {
236 BT_ERR("Can't allocate memory for firmware image");
Magnus Damm73ca66b2006-07-10 04:44:09 -0700237 release_firmware(firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 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
David Howellsc4028952006-11-22 14:57:56 +0000250 INIT_WORK(&data->work, bcm203x_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 usb_set_intfdata(intf, data);
253
Marcel Holtmann3f530692006-10-15 17:31:19 +0200254 schedule_work(&data->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 return 0;
257}
258
259static void bcm203x_disconnect(struct usb_interface *intf)
260{
261 struct bcm203x_data *data = usb_get_intfdata(intf);
262
263 BT_DBG("intf %p", intf);
264
265 usb_kill_urb(data->urb);
266
267 usb_set_intfdata(intf, NULL);
268
269 usb_free_urb(data->urb);
270 kfree(data->fw_data);
271 kfree(data->buffer);
272 kfree(data);
273}
274
275static struct usb_driver bcm203x_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 .name = "bcm203x",
277 .probe = bcm203x_probe,
278 .disconnect = bcm203x_disconnect,
279 .id_table = bcm203x_table,
280};
281
282static int __init bcm203x_init(void)
283{
284 int err;
285
286 BT_INFO("Broadcom Blutonium firmware driver ver %s", VERSION);
287
288 err = usb_register(&bcm203x_driver);
289 if (err < 0)
290 BT_ERR("Failed to register USB driver");
291
292 return err;
293}
294
295static void __exit bcm203x_exit(void)
296{
297 usb_deregister(&bcm203x_driver);
298}
299
300module_init(bcm203x_init);
301module_exit(bcm203x_exit);
302
303module_param(ignore, bool, 0644);
304MODULE_PARM_DESC(ignore, "Ignore devices from the matching table");
305
306MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
307MODULE_DESCRIPTION("Broadcom Blutonium firmware driver ver " VERSION);
308MODULE_VERSION(VERSION);
309MODULE_LICENSE("GPL");
Marcel Holtmann23121192007-02-17 23:59:02 +0100310MODULE_FIRMWARE("BCM2033-MD.hex");
311MODULE_FIRMWARE("BCM2033-FW.bin");