Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 int ignore = 0; |
| 49 | |
| 50 | static struct usb_device_id bcm203x_table[] = { |
| 51 | /* Broadcom Blutonium (BCM2033) */ |
| 52 | { USB_DEVICE(0x0a5c, 0x2033) }, |
| 53 | |
| 54 | { } /* Terminating entry */ |
| 55 | }; |
| 56 | |
| 57 | MODULE_DEVICE_TABLE(usb, bcm203x_table); |
| 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 (ignore || (intf->cur_altsetting->desc.bInterfaceNumber != 0)) |
| 179 | return -ENODEV; |
| 180 | |
Deepak Saxena | 089b1db | 2005-11-07 01:01:26 -0800 | [diff] [blame] | 181 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | if (!data) { |
| 183 | BT_ERR("Can't allocate memory for data structure"); |
| 184 | return -ENOMEM; |
| 185 | } |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | 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"); |
| 237 | usb_free_urb(data->urb); |
| 238 | kfree(data->buffer); |
| 239 | kfree(data); |
| 240 | return -ENOMEM; |
| 241 | } |
| 242 | |
| 243 | memcpy(data->fw_data, firmware->data, firmware->size); |
| 244 | data->fw_size = firmware->size; |
| 245 | data->fw_sent = 0; |
| 246 | |
| 247 | release_firmware(firmware); |
| 248 | |
| 249 | init_timer(&data->timer); |
| 250 | data->timer.function = bcm203x_timer; |
| 251 | data->timer.data = (unsigned long) data; |
| 252 | |
| 253 | usb_set_intfdata(intf, data); |
| 254 | |
| 255 | mod_timer(&data->timer, jiffies + HZ); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static void bcm203x_disconnect(struct usb_interface *intf) |
| 261 | { |
| 262 | struct bcm203x_data *data = usb_get_intfdata(intf); |
| 263 | |
| 264 | BT_DBG("intf %p", intf); |
| 265 | |
| 266 | usb_kill_urb(data->urb); |
| 267 | |
| 268 | usb_set_intfdata(intf, NULL); |
| 269 | |
| 270 | usb_free_urb(data->urb); |
| 271 | kfree(data->fw_data); |
| 272 | kfree(data->buffer); |
| 273 | kfree(data); |
| 274 | } |
| 275 | |
| 276 | static struct usb_driver bcm203x_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | .name = "bcm203x", |
| 278 | .probe = bcm203x_probe, |
| 279 | .disconnect = bcm203x_disconnect, |
| 280 | .id_table = bcm203x_table, |
| 281 | }; |
| 282 | |
| 283 | static int __init bcm203x_init(void) |
| 284 | { |
| 285 | int err; |
| 286 | |
| 287 | BT_INFO("Broadcom Blutonium firmware driver ver %s", VERSION); |
| 288 | |
| 289 | err = usb_register(&bcm203x_driver); |
| 290 | if (err < 0) |
| 291 | BT_ERR("Failed to register USB driver"); |
| 292 | |
| 293 | return err; |
| 294 | } |
| 295 | |
| 296 | static void __exit bcm203x_exit(void) |
| 297 | { |
| 298 | usb_deregister(&bcm203x_driver); |
| 299 | } |
| 300 | |
| 301 | module_init(bcm203x_init); |
| 302 | module_exit(bcm203x_exit); |
| 303 | |
| 304 | module_param(ignore, bool, 0644); |
| 305 | MODULE_PARM_DESC(ignore, "Ignore devices from the matching table"); |
| 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"); |