1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Broadcom Blutonium firmware driver
6 * Copyright (C) 2003 Maxim Krasnyansky <maxk@qualcomm.com>
7 * Copyright (C) 2003 Marcel Holtmann <marcel@holtmann.org>
10 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
18 #include <linux/device.h>
19 #include <linux/firmware.h>
21 #include <linux/usb.h>
23 #include <net/bluetooth/bluetooth.h>
27 static const struct usb_device_id bcm203x_table
[] = {
28 /* Broadcom Blutonium (BCM2033) */
29 { USB_DEVICE(0x0a5c, 0x2033) },
31 { } /* Terminating entry */
34 MODULE_DEVICE_TABLE(usb
, bcm203x_table
);
36 #define BCM203X_ERROR 0
37 #define BCM203X_RESET 1
38 #define BCM203X_LOAD_MINIDRV 2
39 #define BCM203X_SELECT_MEMORY 3
40 #define BCM203X_CHECK_MEMORY 4
41 #define BCM203X_LOAD_FIRMWARE 5
42 #define BCM203X_CHECK_FIRMWARE 6
44 #define BCM203X_IN_EP 0x81
45 #define BCM203X_OUT_EP 0x02
48 struct usb_device
*udev
;
52 struct work_struct work
;
56 unsigned char *buffer
;
58 unsigned char *fw_data
;
63 static void bcm203x_complete(struct urb
*urb
)
65 struct bcm203x_data
*data
= urb
->context
;
66 struct usb_device
*udev
= urb
->dev
;
69 BT_DBG("udev %p urb %p", udev
, urb
);
72 BT_ERR("URB failed with status %d", urb
->status
);
73 data
->state
= BCM203X_ERROR
;
77 switch (data
->state
) {
78 case BCM203X_LOAD_MINIDRV
:
79 memcpy(data
->buffer
, "#", 1);
81 usb_fill_bulk_urb(urb
, udev
, usb_sndbulkpipe(udev
, BCM203X_OUT_EP
),
82 data
->buffer
, 1, bcm203x_complete
, data
);
84 data
->state
= BCM203X_SELECT_MEMORY
;
86 /* use workqueue to have a small delay */
87 schedule_work(&data
->work
);
90 case BCM203X_SELECT_MEMORY
:
91 usb_fill_int_urb(urb
, udev
, usb_rcvintpipe(udev
, BCM203X_IN_EP
),
92 data
->buffer
, 32, bcm203x_complete
, data
, 1);
94 data
->state
= BCM203X_CHECK_MEMORY
;
96 if (usb_submit_urb(data
->urb
, GFP_ATOMIC
) < 0)
97 BT_ERR("Can't submit URB");
100 case BCM203X_CHECK_MEMORY
:
101 if (data
->buffer
[0] != '#') {
102 BT_ERR("Memory select failed");
103 data
->state
= BCM203X_ERROR
;
107 data
->state
= BCM203X_LOAD_FIRMWARE
;
109 case BCM203X_LOAD_FIRMWARE
:
110 if (data
->fw_sent
== data
->fw_size
) {
111 usb_fill_int_urb(urb
, udev
, usb_rcvintpipe(udev
, BCM203X_IN_EP
),
112 data
->buffer
, 32, bcm203x_complete
, data
, 1);
114 data
->state
= BCM203X_CHECK_FIRMWARE
;
116 len
= min_t(uint
, data
->fw_size
- data
->fw_sent
, 4096);
118 usb_fill_bulk_urb(urb
, udev
, usb_sndbulkpipe(udev
, BCM203X_OUT_EP
),
119 data
->fw_data
+ data
->fw_sent
, len
, bcm203x_complete
, data
);
121 data
->fw_sent
+= len
;
124 if (usb_submit_urb(data
->urb
, GFP_ATOMIC
) < 0)
125 BT_ERR("Can't submit URB");
128 case BCM203X_CHECK_FIRMWARE
:
129 if (data
->buffer
[0] != '.') {
130 BT_ERR("Firmware loading failed");
131 data
->state
= BCM203X_ERROR
;
135 data
->state
= BCM203X_RESET
;
140 static void bcm203x_work(struct work_struct
*work
)
142 struct bcm203x_data
*data
=
143 container_of(work
, struct bcm203x_data
, work
);
145 if (atomic_read(&data
->shutdown
))
148 if (usb_submit_urb(data
->urb
, GFP_KERNEL
) < 0)
149 BT_ERR("Can't submit URB");
152 static int bcm203x_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
154 const struct firmware
*firmware
;
155 struct usb_device
*udev
= interface_to_usbdev(intf
);
156 struct bcm203x_data
*data
;
159 BT_DBG("intf %p id %p", intf
, id
);
161 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 0)
164 data
= devm_kzalloc(&intf
->dev
, sizeof(*data
), GFP_KERNEL
);
169 data
->state
= BCM203X_LOAD_MINIDRV
;
171 data
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
175 if (request_firmware(&firmware
, "BCM2033-MD.hex", &udev
->dev
) < 0) {
176 BT_ERR("Mini driver request failed");
177 usb_free_urb(data
->urb
);
181 BT_DBG("minidrv data %p size %zu", firmware
->data
, firmware
->size
);
183 size
= max_t(uint
, firmware
->size
, 4096);
185 data
->buffer
= kmalloc(size
, GFP_KERNEL
);
187 BT_ERR("Can't allocate memory for mini driver");
188 release_firmware(firmware
);
189 usb_free_urb(data
->urb
);
193 memcpy(data
->buffer
, firmware
->data
, firmware
->size
);
195 usb_fill_bulk_urb(data
->urb
, udev
, usb_sndbulkpipe(udev
, BCM203X_OUT_EP
),
196 data
->buffer
, firmware
->size
, bcm203x_complete
, data
);
198 release_firmware(firmware
);
200 if (request_firmware(&firmware
, "BCM2033-FW.bin", &udev
->dev
) < 0) {
201 BT_ERR("Firmware request failed");
202 usb_free_urb(data
->urb
);
207 BT_DBG("firmware data %p size %zu", firmware
->data
, firmware
->size
);
209 data
->fw_data
= kmemdup(firmware
->data
, firmware
->size
, GFP_KERNEL
);
210 if (!data
->fw_data
) {
211 BT_ERR("Can't allocate memory for firmware image");
212 release_firmware(firmware
);
213 usb_free_urb(data
->urb
);
218 data
->fw_size
= firmware
->size
;
221 release_firmware(firmware
);
223 INIT_WORK(&data
->work
, bcm203x_work
);
225 usb_set_intfdata(intf
, data
);
227 /* use workqueue to have a small delay */
228 schedule_work(&data
->work
);
233 static void bcm203x_disconnect(struct usb_interface
*intf
)
235 struct bcm203x_data
*data
= usb_get_intfdata(intf
);
237 BT_DBG("intf %p", intf
);
239 atomic_inc(&data
->shutdown
);
240 cancel_work_sync(&data
->work
);
242 usb_kill_urb(data
->urb
);
244 usb_set_intfdata(intf
, NULL
);
246 usb_free_urb(data
->urb
);
247 kfree(data
->fw_data
);
251 static struct usb_driver bcm203x_driver
= {
253 .probe
= bcm203x_probe
,
254 .disconnect
= bcm203x_disconnect
,
255 .id_table
= bcm203x_table
,
256 .disable_hub_initiated_lpm
= 1,
259 module_usb_driver(bcm203x_driver
);
261 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
262 MODULE_DESCRIPTION("Broadcom Blutonium firmware driver ver " VERSION
);
263 MODULE_VERSION(VERSION
);
264 MODULE_LICENSE("GPL");
265 MODULE_FIRMWARE("BCM2033-MD.hex");
266 MODULE_FIRMWARE("BCM2033-FW.bin");