xref: /openbmc/linux/drivers/bluetooth/ath3k.c (revision baa7eb025ab14f3cba2e35c0a8648f9c9f01d24f)
1 /*
2  * Copyright (c) 2008-2009 Atheros Communications Inc.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19 
20 
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/device.h>
28 #include <linux/firmware.h>
29 #include <linux/usb.h>
30 #include <net/bluetooth/bluetooth.h>
31 
32 #define VERSION "1.0"
33 
34 
35 static struct usb_device_id ath3k_table[] = {
36 	/* Atheros AR3011 */
37 	{ USB_DEVICE(0x0CF3, 0x3000) },
38 
39 	/* Atheros AR3011 with sflash firmware*/
40 	{ USB_DEVICE(0x0CF3, 0x3002) },
41 
42 	{ }	/* Terminating entry */
43 };
44 
45 MODULE_DEVICE_TABLE(usb, ath3k_table);
46 
47 #define USB_REQ_DFU_DNLOAD	1
48 #define BULK_SIZE		4096
49 
50 struct ath3k_data {
51 	struct usb_device *udev;
52 	u8 *fw_data;
53 	u32 fw_size;
54 	u32 fw_sent;
55 };
56 
57 static int ath3k_load_firmware(struct ath3k_data *data,
58 				unsigned char *firmware,
59 				int count)
60 {
61 	u8 *send_buf;
62 	int err, pipe, len, size, sent = 0;
63 
64 	BT_DBG("ath3k %p udev %p", data, data->udev);
65 
66 	pipe = usb_sndctrlpipe(data->udev, 0);
67 
68 	if ((usb_control_msg(data->udev, pipe,
69 				USB_REQ_DFU_DNLOAD,
70 				USB_TYPE_VENDOR, 0, 0,
71 				firmware, 20, USB_CTRL_SET_TIMEOUT)) < 0) {
72 		BT_ERR("Can't change to loading configuration err");
73 		return -EBUSY;
74 	}
75 	sent += 20;
76 	count -= 20;
77 
78 	send_buf = kmalloc(BULK_SIZE, GFP_ATOMIC);
79 	if (!send_buf) {
80 		BT_ERR("Can't allocate memory chunk for firmware");
81 		return -ENOMEM;
82 	}
83 
84 	while (count) {
85 		size = min_t(uint, count, BULK_SIZE);
86 		pipe = usb_sndbulkpipe(data->udev, 0x02);
87 		memcpy(send_buf, firmware + sent, size);
88 
89 		err = usb_bulk_msg(data->udev, pipe, send_buf, size,
90 					&len, 3000);
91 
92 		if (err || (len != size)) {
93 			BT_ERR("Error in firmware loading err = %d,"
94 				"len = %d, size = %d", err, len, size);
95 			goto error;
96 		}
97 
98 		sent  += size;
99 		count -= size;
100 	}
101 
102 	kfree(send_buf);
103 	return 0;
104 
105 error:
106 	kfree(send_buf);
107 	return err;
108 }
109 
110 static int ath3k_probe(struct usb_interface *intf,
111 			const struct usb_device_id *id)
112 {
113 	const struct firmware *firmware;
114 	struct usb_device *udev = interface_to_usbdev(intf);
115 	struct ath3k_data *data;
116 	int size;
117 
118 	BT_DBG("intf %p id %p", intf, id);
119 
120 	if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
121 		return -ENODEV;
122 
123 	data = kzalloc(sizeof(*data), GFP_KERNEL);
124 	if (!data)
125 		return -ENOMEM;
126 
127 	data->udev = udev;
128 
129 	if (request_firmware(&firmware, "ath3k-1.fw", &udev->dev) < 0) {
130 		kfree(data);
131 		return -EIO;
132 	}
133 
134 	size = max_t(uint, firmware->size, 4096);
135 	data->fw_data = kmalloc(size, GFP_KERNEL);
136 	if (!data->fw_data) {
137 		release_firmware(firmware);
138 		kfree(data);
139 		return -ENOMEM;
140 	}
141 
142 	memcpy(data->fw_data, firmware->data, firmware->size);
143 	data->fw_size = firmware->size;
144 	data->fw_sent = 0;
145 	release_firmware(firmware);
146 
147 	usb_set_intfdata(intf, data);
148 	if (ath3k_load_firmware(data, data->fw_data, data->fw_size)) {
149 		usb_set_intfdata(intf, NULL);
150 		kfree(data->fw_data);
151 		kfree(data);
152 		return -EIO;
153 	}
154 
155 	return 0;
156 }
157 
158 static void ath3k_disconnect(struct usb_interface *intf)
159 {
160 	struct ath3k_data *data = usb_get_intfdata(intf);
161 
162 	BT_DBG("ath3k_disconnect intf %p", intf);
163 
164 	kfree(data->fw_data);
165 	kfree(data);
166 }
167 
168 static struct usb_driver ath3k_driver = {
169 	.name		= "ath3k",
170 	.probe		= ath3k_probe,
171 	.disconnect	= ath3k_disconnect,
172 	.id_table	= ath3k_table,
173 };
174 
175 static int __init ath3k_init(void)
176 {
177 	BT_INFO("Atheros AR30xx firmware driver ver %s", VERSION);
178 	return usb_register(&ath3k_driver);
179 }
180 
181 static void __exit ath3k_exit(void)
182 {
183 	usb_deregister(&ath3k_driver);
184 }
185 
186 module_init(ath3k_init);
187 module_exit(ath3k_exit);
188 
189 MODULE_AUTHOR("Atheros Communications");
190 MODULE_DESCRIPTION("Atheros AR30xx firmware driver");
191 MODULE_VERSION(VERSION);
192 MODULE_LICENSE("GPL");
193 MODULE_FIRMWARE("ath3k-1.fw");
194