blob: e667933c3d703b7fb7955caee554a461f4f0171b [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 *
4 * Broadcom Blutonium firmware driver
5 *
6 * Copyright (C) 2003 Maxim Krasnyansky <maxk@qualcomm.com>
7 * Copyright (C) 2003 Marcel Holtmann <marcel@holtmann.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11
David Herrmannfc501ad2011-10-26 11:13:13 +020012#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/types.h>
17#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/device.h>
20#include <linux/firmware.h>
21
22#include <linux/usb.h>
23
24#include <net/bluetooth/bluetooth.h>
25
Marcel Holtmann943d56b2008-08-07 22:26:55 +020026#define VERSION "1.2"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Márton Németh89781112010-01-10 13:39:15 +010028static const struct usb_device_id bcm203x_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 /* Broadcom Blutonium (BCM2033) */
30 { USB_DEVICE(0x0a5c, 0x2033) },
31
32 { } /* Terminating entry */
33};
34
35MODULE_DEVICE_TABLE(usb, bcm203x_table);
36
37#define BCM203X_ERROR 0
38#define BCM203X_RESET 1
39#define BCM203X_LOAD_MINIDRV 2
40#define BCM203X_SELECT_MEMORY 3
41#define BCM203X_CHECK_MEMORY 4
42#define BCM203X_LOAD_FIRMWARE 5
43#define BCM203X_CHECK_FIRMWARE 6
44
45#define BCM203X_IN_EP 0x81
46#define BCM203X_OUT_EP 0x02
47
48struct bcm203x_data {
49 struct usb_device *udev;
50
51 unsigned long state;
52
Marcel Holtmann3f530692006-10-15 17:31:19 +020053 struct work_struct work;
David Herrmannfc501ad2011-10-26 11:13:13 +020054 atomic_t shutdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 struct urb *urb;
57 unsigned char *buffer;
58
59 unsigned char *fw_data;
60 unsigned int fw_size;
61 unsigned int fw_sent;
62};
63
David Howells7d12e782006-10-05 14:55:46 +010064static void bcm203x_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 struct bcm203x_data *data = urb->context;
67 struct usb_device *udev = urb->dev;
68 int len;
69
70 BT_DBG("udev %p urb %p", udev, urb);
71
72 if (urb->status) {
73 BT_ERR("URB failed with status %d", urb->status);
74 data->state = BCM203X_ERROR;
75 return;
76 }
77
78 switch (data->state) {
79 case BCM203X_LOAD_MINIDRV:
80 memcpy(data->buffer, "#", 1);
81
82 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
83 data->buffer, 1, bcm203x_complete, data);
84
85 data->state = BCM203X_SELECT_MEMORY;
86
David Herrmannfc501ad2011-10-26 11:13:13 +020087 /* use workqueue to have a small delay */
Marcel Holtmann3f530692006-10-15 17:31:19 +020088 schedule_work(&data->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 break;
90
91 case BCM203X_SELECT_MEMORY:
92 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, BCM203X_IN_EP),
93 data->buffer, 32, bcm203x_complete, data, 1);
94
95 data->state = BCM203X_CHECK_MEMORY;
96
97 if (usb_submit_urb(data->urb, GFP_ATOMIC) < 0)
98 BT_ERR("Can't submit URB");
99 break;
100
101 case BCM203X_CHECK_MEMORY:
102 if (data->buffer[0] != '#') {
103 BT_ERR("Memory select failed");
104 data->state = BCM203X_ERROR;
105 break;
106 }
107
108 data->state = BCM203X_LOAD_FIRMWARE;
Gustavo A. R. Silvaa3b4cbf2020-07-08 15:26:50 -0500109 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 case BCM203X_LOAD_FIRMWARE:
111 if (data->fw_sent == data->fw_size) {
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_FIRMWARE;
116 } else {
117 len = min_t(uint, data->fw_size - data->fw_sent, 4096);
118
119 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
120 data->fw_data + data->fw_sent, len, bcm203x_complete, data);
121
122 data->fw_sent += len;
123 }
124
125 if (usb_submit_urb(data->urb, GFP_ATOMIC) < 0)
126 BT_ERR("Can't submit URB");
127 break;
128
129 case BCM203X_CHECK_FIRMWARE:
130 if (data->buffer[0] != '.') {
131 BT_ERR("Firmware loading failed");
132 data->state = BCM203X_ERROR;
133 break;
134 }
135
136 data->state = BCM203X_RESET;
137 break;
138 }
139}
140
David Howellsc4028952006-11-22 14:57:56 +0000141static void bcm203x_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
David Howellsc4028952006-11-22 14:57:56 +0000143 struct bcm203x_data *data =
144 container_of(work, struct bcm203x_data, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
David Herrmannfc501ad2011-10-26 11:13:13 +0200146 if (atomic_read(&data->shutdown))
147 return;
148
David Herrmannb91a4e32011-10-25 21:13:36 +0200149 if (usb_submit_urb(data->urb, GFP_KERNEL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 BT_ERR("Can't submit URB");
151}
152
153static int bcm203x_probe(struct usb_interface *intf, const struct usb_device_id *id)
154{
155 const struct firmware *firmware;
156 struct usb_device *udev = interface_to_usbdev(intf);
157 struct bcm203x_data *data;
158 int size;
159
160 BT_DBG("intf %p id %p", intf, id);
161
Marcel Holtmann943d56b2008-08-07 22:26:55 +0200162 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return -ENODEV;
164
Sachin Kamat9357cc62012-07-27 12:38:31 +0530165 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Syam Sidhardhan17722e22015-12-22 19:30:18 +0530166 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 data->udev = udev;
170 data->state = BCM203X_LOAD_MINIDRV;
171
172 data->urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sanga2f195a2016-08-11 23:00:31 +0200173 if (!data->urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (request_firmware(&firmware, "BCM2033-MD.hex", &udev->dev) < 0) {
177 BT_ERR("Mini driver request failed");
178 usb_free_urb(data->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -EIO;
180 }
181
Marcel Holtmanna418b892008-11-30 12:17:28 +0100182 BT_DBG("minidrv data %p size %zu", firmware->data, firmware->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 size = max_t(uint, firmware->size, 4096);
185
186 data->buffer = kmalloc(size, GFP_KERNEL);
187 if (!data->buffer) {
188 BT_ERR("Can't allocate memory for mini driver");
189 release_firmware(firmware);
190 usb_free_urb(data->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -ENOMEM;
192 }
193
194 memcpy(data->buffer, firmware->data, firmware->size);
195
196 usb_fill_bulk_urb(data->urb, udev, usb_sndbulkpipe(udev, BCM203X_OUT_EP),
197 data->buffer, firmware->size, bcm203x_complete, data);
198
199 release_firmware(firmware);
200
201 if (request_firmware(&firmware, "BCM2033-FW.bin", &udev->dev) < 0) {
202 BT_ERR("Firmware request failed");
203 usb_free_urb(data->urb);
204 kfree(data->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return -EIO;
206 }
207
Marcel Holtmanna418b892008-11-30 12:17:28 +0100208 BT_DBG("firmware data %p size %zu", firmware->data, firmware->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Julia Lawall5ee283c2010-05-15 23:19:15 +0200210 data->fw_data = kmemdup(firmware->data, firmware->size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!data->fw_data) {
212 BT_ERR("Can't allocate memory for firmware image");
Magnus Damm73ca66b2006-07-10 04:44:09 -0700213 release_firmware(firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 usb_free_urb(data->urb);
215 kfree(data->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -ENOMEM;
217 }
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 data->fw_size = firmware->size;
220 data->fw_sent = 0;
221
222 release_firmware(firmware);
223
David Howellsc4028952006-11-22 14:57:56 +0000224 INIT_WORK(&data->work, bcm203x_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 usb_set_intfdata(intf, data);
227
David Herrmannfc501ad2011-10-26 11:13:13 +0200228 /* use workqueue to have a small delay */
Marcel Holtmann3f530692006-10-15 17:31:19 +0200229 schedule_work(&data->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 return 0;
232}
233
234static void bcm203x_disconnect(struct usb_interface *intf)
235{
236 struct bcm203x_data *data = usb_get_intfdata(intf);
237
238 BT_DBG("intf %p", intf);
239
David Herrmannfc501ad2011-10-26 11:13:13 +0200240 atomic_inc(&data->shutdown);
241 cancel_work_sync(&data->work);
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 usb_kill_urb(data->urb);
244
245 usb_set_intfdata(intf, NULL);
246
247 usb_free_urb(data->urb);
248 kfree(data->fw_data);
249 kfree(data->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252static struct usb_driver bcm203x_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .name = "bcm203x",
254 .probe = bcm203x_probe,
255 .disconnect = bcm203x_disconnect,
256 .id_table = bcm203x_table,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700257 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -0800260module_usb_driver(bcm203x_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
263MODULE_DESCRIPTION("Broadcom Blutonium firmware driver ver " VERSION);
264MODULE_VERSION(VERSION);
265MODULE_LICENSE("GPL");
Marcel Holtmann23121192007-02-17 23:59:02 +0100266MODULE_FIRMWARE("BCM2033-MD.hex");
267MODULE_FIRMWARE("BCM2033-FW.bin");