blob: 6e7bd4e4adbb29f5b3d43ea0f4fcacf2a67a3c3f [file] [log] [blame]
Bing Zhao132ff4e2009-06-02 14:29:35 -07001/**
2 * Marvell Bluetooth driver
3 *
4 * Copyright (C) 2009, Marvell International Ltd.
5 *
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 *
14 *
15 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
17 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
18 * this warranty disclaimer.
19 **/
20
Paul Gortmakera59b9682011-08-29 16:44:23 -040021#include <linux/module.h>
22
Bing Zhao132ff4e2009-06-02 14:29:35 -070023#include <net/bluetooth/bluetooth.h>
24#include <net/bluetooth/hci_core.h>
25
26#include "btmrvl_drv.h"
27
28#define VERSION "1.0"
29
30/*
31 * This function is called by interface specific interrupt handler.
32 * It updates Power Save & Host Sleep states, and wakes up the main
33 * thread.
34 */
35void btmrvl_interrupt(struct btmrvl_private *priv)
36{
Bing Zhao132ff4e2009-06-02 14:29:35 -070037 priv->adapter->ps_state = PS_AWAKE;
38
39 priv->adapter->wakeup_tries = 0;
40
41 priv->adapter->int_count++;
42
43 wake_up_interruptible(&priv->main_thread.wait_q);
Bing Zhao132ff4e2009-06-02 14:29:35 -070044}
45EXPORT_SYMBOL_GPL(btmrvl_interrupt);
46
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +030047bool btmrvl_check_evtpkt(struct btmrvl_private *priv, struct sk_buff *skb)
Bing Zhao132ff4e2009-06-02 14:29:35 -070048{
Marcel Holtmann4271e082009-06-09 16:00:22 +020049 struct hci_event_hdr *hdr = (void *) skb->data;
Bing Zhao132ff4e2009-06-02 14:29:35 -070050
Bing Zhao132ff4e2009-06-02 14:29:35 -070051 if (hdr->evt == HCI_EV_CMD_COMPLETE) {
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +030052 struct hci_ev_cmd_complete *ec;
53 u16 opcode, ocf, ogf;
54
Marcel Holtmann4271e082009-06-09 16:00:22 +020055 ec = (void *) (skb->data + HCI_EVENT_HDR_SIZE);
Bing Zhao132ff4e2009-06-02 14:29:35 -070056 opcode = __le16_to_cpu(ec->opcode);
57 ocf = hci_opcode_ocf(opcode);
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +030058 ogf = hci_opcode_ogf(opcode);
59
Amitkumar Karwarc0378742013-10-01 12:19:12 -070060 if (priv->btmrvl_dev.sendcmdflag) {
Bing Zhao132ff4e2009-06-02 14:29:35 -070061 priv->btmrvl_dev.sendcmdflag = false;
62 priv->adapter->cmd_complete = true;
63 wake_up_interruptible(&priv->adapter->cmd_wait_q);
64 }
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +030065
66 if (ogf == OGF) {
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +030067 BT_DBG("vendor event skipped: ogf 0x%4.4x ocf 0x%4.4x",
68 ogf, ocf);
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +030069 kfree_skb(skb);
70 return false;
71 }
Bing Zhao132ff4e2009-06-02 14:29:35 -070072 }
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +030073
74 return true;
Bing Zhao132ff4e2009-06-02 14:29:35 -070075}
76EXPORT_SYMBOL_GPL(btmrvl_check_evtpkt);
77
78int btmrvl_process_event(struct btmrvl_private *priv, struct sk_buff *skb)
79{
80 struct btmrvl_adapter *adapter = priv->adapter;
Bing Zhao132ff4e2009-06-02 14:29:35 -070081 struct btmrvl_event *event;
Dan Carpenter31f085f2010-03-08 15:15:59 +030082 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -070083
Bing Zhao132ff4e2009-06-02 14:29:35 -070084 event = (struct btmrvl_event *) skb->data;
85 if (event->ec != 0xff) {
86 BT_DBG("Not Marvell Event=%x", event->ec);
87 ret = -EINVAL;
88 goto exit;
89 }
90
91 switch (event->data[0]) {
92 case BT_CMD_AUTO_SLEEP_MODE:
93 if (!event->data[2]) {
94 if (event->data[1] == BT_PS_ENABLE)
95 adapter->psmode = 1;
96 else
97 adapter->psmode = 0;
98 BT_DBG("PS Mode:%s",
99 (adapter->psmode) ? "Enable" : "Disable");
100 } else {
101 BT_DBG("PS Mode command failed");
102 }
103 break;
104
105 case BT_CMD_HOST_SLEEP_CONFIG:
106 if (!event->data[3])
107 BT_DBG("gpio=%x, gap=%x", event->data[1],
Marcel Holtmann4271e082009-06-09 16:00:22 +0200108 event->data[2]);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700109 else
110 BT_DBG("HSCFG command failed");
111 break;
112
113 case BT_CMD_HOST_SLEEP_ENABLE:
114 if (!event->data[1]) {
115 adapter->hs_state = HS_ACTIVATED;
116 if (adapter->psmode)
117 adapter->ps_state = PS_SLEEP;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700118 BT_DBG("HS ACTIVATED!");
119 } else {
120 BT_DBG("HS Enable failed");
121 }
122 break;
123
124 case BT_CMD_MODULE_CFG_REQ:
Marcel Holtmann4271e082009-06-09 16:00:22 +0200125 if (priv->btmrvl_dev.sendcmdflag &&
126 event->data[1] == MODULE_BRINGUP_REQ) {
Bing Zhao903c8432010-03-03 14:37:35 -0800127 BT_DBG("EVENT:%s",
128 ((event->data[2] == MODULE_BROUGHT_UP) ||
129 (event->data[2] == MODULE_ALREADY_UP)) ?
130 "Bring-up succeed" : "Bring-up failed");
Bing Zhaof120c6b2010-03-03 14:37:37 -0800131
David Vrabel8f1e1742010-08-09 17:38:10 -0400132 if (event->length > 3 && event->data[3])
133 priv->btmrvl_dev.dev_type = HCI_AMP;
Bing Zhaof120c6b2010-03-03 14:37:37 -0800134 else
135 priv->btmrvl_dev.dev_type = HCI_BREDR;
136
137 BT_DBG("dev_type: %d", priv->btmrvl_dev.dev_type);
Marcel Holtmann4271e082009-06-09 16:00:22 +0200138 } else if (priv->btmrvl_dev.sendcmdflag &&
139 event->data[1] == MODULE_SHUTDOWN_REQ) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700140 BT_DBG("EVENT:%s", (event->data[2]) ?
141 "Shutdown failed" : "Shutdown succeed");
142 } else {
143 BT_DBG("BT_CMD_MODULE_CFG_REQ resp for APP");
144 ret = -EINVAL;
145 }
146 break;
147
148 case BT_EVENT_POWER_STATE:
149 if (event->data[1] == BT_PS_SLEEP)
150 adapter->ps_state = PS_SLEEP;
151 BT_DBG("EVENT:%s",
152 (adapter->ps_state) ? "PS_SLEEP" : "PS_AWAKE");
153 break;
154
155 default:
156 BT_DBG("Unknown Event=%d", event->data[0]);
157 ret = -EINVAL;
158 break;
159 }
160
161exit:
162 if (!ret)
163 kfree_skb(skb);
164
Bing Zhao132ff4e2009-06-02 14:29:35 -0700165 return ret;
166}
167EXPORT_SYMBOL_GPL(btmrvl_process_event);
168
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700169static int btmrvl_send_sync_cmd(struct btmrvl_private *priv, u16 cmd_no,
170 const void *param, u8 len)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700171{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200172 struct sk_buff *skb;
Amitkumar Karwar7d5b4002013-10-01 12:19:13 -0700173 struct hci_command_hdr *hdr;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700174
Amitkumar Karwar7d5b4002013-10-01 12:19:13 -0700175 skb = bt_skb_alloc(HCI_COMMAND_HDR_SIZE + len, GFP_ATOMIC);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700176 if (skb == NULL) {
177 BT_ERR("No free skb");
Marcel Holtmann93742532009-06-13 07:40:18 +0200178 return -ENOMEM;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700179 }
180
Amitkumar Karwar7d5b4002013-10-01 12:19:13 -0700181 hdr = (struct hci_command_hdr *)skb_put(skb, HCI_COMMAND_HDR_SIZE);
182 hdr->opcode = cpu_to_le16(hci_opcode_pack(OGF, cmd_no));
183 hdr->plen = len;
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700184
185 if (len)
Amitkumar Karwar7d5b4002013-10-01 12:19:13 -0700186 memcpy(skb_put(skb, len), param, len);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700187
188 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
189
Marcel Holtmann4271e082009-06-09 16:00:22 +0200190 skb->dev = (void *) priv->btmrvl_dev.hcidev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700191 skb_queue_head(&priv->adapter->tx_queue, skb);
192
193 priv->btmrvl_dev.sendcmdflag = true;
194
195 priv->adapter->cmd_complete = false;
196
Bing Zhao132ff4e2009-06-02 14:29:35 -0700197 wake_up_interruptible(&priv->main_thread.wait_q);
198
Marcel Holtmann4271e082009-06-09 16:00:22 +0200199 if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
200 priv->adapter->cmd_complete,
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700201 msecs_to_jiffies(WAIT_UNTIL_CMD_RESP)))
202 return -ETIMEDOUT;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700203
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700204 return 0;
205}
206
207int btmrvl_send_module_cfg_cmd(struct btmrvl_private *priv, int subcmd)
208{
209 int ret;
210
211 ret = btmrvl_send_sync_cmd(priv, BT_CMD_MODULE_CFG_REQ, &subcmd, 1);
212 if (ret)
213 BT_ERR("module_cfg_cmd(%x) failed\n", subcmd);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700214
Bing Zhao132ff4e2009-06-02 14:29:35 -0700215 return ret;
216}
217EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd);
218
Amitkumar Karwar2a8ff612012-04-24 16:31:40 -0700219int btmrvl_send_hscfg_cmd(struct btmrvl_private *priv)
220{
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700221 int ret;
222 u8 param[2];
Amitkumar Karwar2a8ff612012-04-24 16:31:40 -0700223
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700224 param[0] = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8;
225 param[1] = (u8) (priv->btmrvl_dev.gpio_gap & 0x00ff);
Amitkumar Karwar2a8ff612012-04-24 16:31:40 -0700226
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700227 BT_DBG("Sending HSCFG Command, gpio=0x%x, gap=0x%x",
228 param[0], param[1]);
Amitkumar Karwar2a8ff612012-04-24 16:31:40 -0700229
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700230 ret = btmrvl_send_sync_cmd(priv, BT_CMD_HOST_SLEEP_CONFIG, param, 2);
231 if (ret)
232 BT_ERR("HSCFG command failed\n");
Amitkumar Karwar2a8ff612012-04-24 16:31:40 -0700233
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700234 return ret;
Amitkumar Karwar2a8ff612012-04-24 16:31:40 -0700235}
236EXPORT_SYMBOL_GPL(btmrvl_send_hscfg_cmd);
237
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700238int btmrvl_enable_ps(struct btmrvl_private *priv)
239{
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700240 int ret;
241 u8 param;
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700242
243 if (priv->btmrvl_dev.psmode)
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700244 param = BT_PS_ENABLE;
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700245 else
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700246 param = BT_PS_DISABLE;
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700247
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700248 ret = btmrvl_send_sync_cmd(priv, BT_CMD_AUTO_SLEEP_MODE, &param, 1);
249 if (ret)
250 BT_ERR("PSMODE command failed\n");
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700251
252 return 0;
253}
254EXPORT_SYMBOL_GPL(btmrvl_enable_ps);
255
Amitkumar Karwarba54a162012-04-25 11:43:54 -0700256int btmrvl_enable_hs(struct btmrvl_private *priv)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700257{
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700258 int ret;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700259
Amitkumar Karwarc0378742013-10-01 12:19:12 -0700260 ret = btmrvl_send_sync_cmd(priv, BT_CMD_HOST_SLEEP_ENABLE, NULL, 0);
261 if (ret)
262 BT_ERR("Host sleep enable command failed\n");
Bing Zhao132ff4e2009-06-02 14:29:35 -0700263
Bing Zhao132ff4e2009-06-02 14:29:35 -0700264 return ret;
265}
Amitkumar Karwarba54a162012-04-25 11:43:54 -0700266EXPORT_SYMBOL_GPL(btmrvl_enable_hs);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700267
268int btmrvl_prepare_command(struct btmrvl_private *priv)
269{
Marcel Holtmann93742532009-06-13 07:40:18 +0200270 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700271
272 if (priv->btmrvl_dev.hscfgcmd) {
273 priv->btmrvl_dev.hscfgcmd = 0;
Amitkumar Karwar2a8ff612012-04-24 16:31:40 -0700274 btmrvl_send_hscfg_cmd(priv);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700275 }
276
277 if (priv->btmrvl_dev.pscmd) {
278 priv->btmrvl_dev.pscmd = 0;
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700279 btmrvl_enable_ps(priv);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700280 }
281
282 if (priv->btmrvl_dev.hscmd) {
283 priv->btmrvl_dev.hscmd = 0;
284
285 if (priv->btmrvl_dev.hsmode) {
286 ret = btmrvl_enable_hs(priv);
287 } else {
288 ret = priv->hw_wakeup_firmware(priv);
289 priv->adapter->hs_state = HS_DEACTIVATED;
290 }
291 }
292
Bing Zhao132ff4e2009-06-02 14:29:35 -0700293 return ret;
294}
295
296static int btmrvl_tx_pkt(struct btmrvl_private *priv, struct sk_buff *skb)
297{
Marcel Holtmann93742532009-06-13 07:40:18 +0200298 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700299
Marcel Holtmann93742532009-06-13 07:40:18 +0200300 if (!skb || !skb->data)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700301 return -EINVAL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700302
303 if (!skb->len || ((skb->len + BTM_HEADER_LEN) > BTM_UPLD_SIZE)) {
304 BT_ERR("Tx Error: Bad skb length %d : %d",
Marcel Holtmann4271e082009-06-09 16:00:22 +0200305 skb->len, BTM_UPLD_SIZE);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700306 return -EINVAL;
307 }
308
309 if (skb_headroom(skb) < BTM_HEADER_LEN) {
310 struct sk_buff *tmp = skb;
311
312 skb = skb_realloc_headroom(skb, BTM_HEADER_LEN);
313 if (!skb) {
314 BT_ERR("Tx Error: realloc_headroom failed %d",
315 BTM_HEADER_LEN);
316 skb = tmp;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700317 return -EINVAL;
318 }
319
320 kfree_skb(tmp);
321 }
322
323 skb_push(skb, BTM_HEADER_LEN);
324
325 /* header type: byte[3]
326 * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor
327 * header length: byte[2][1][0]
328 */
329
330 skb->data[0] = (skb->len & 0x0000ff);
331 skb->data[1] = (skb->len & 0x00ff00) >> 8;
332 skb->data[2] = (skb->len & 0xff0000) >> 16;
333 skb->data[3] = bt_cb(skb)->pkt_type;
334
335 if (priv->hw_host_to_card)
336 ret = priv->hw_host_to_card(priv, skb->data, skb->len);
337
Bing Zhao132ff4e2009-06-02 14:29:35 -0700338 return ret;
339}
340
341static void btmrvl_init_adapter(struct btmrvl_private *priv)
342{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700343 skb_queue_head_init(&priv->adapter->tx_queue);
344
345 priv->adapter->ps_state = PS_AWAKE;
346
347 init_waitqueue_head(&priv->adapter->cmd_wait_q);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700348}
349
350static void btmrvl_free_adapter(struct btmrvl_private *priv)
351{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700352 skb_queue_purge(&priv->adapter->tx_queue);
353
354 kfree(priv->adapter);
355
356 priv->adapter = NULL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700357}
358
Marcel Holtmann4271e082009-06-09 16:00:22 +0200359static int btmrvl_ioctl(struct hci_dev *hdev,
360 unsigned int cmd, unsigned long arg)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700361{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700362 return -ENOIOCTLCMD;
363}
364
Bing Zhao132ff4e2009-06-02 14:29:35 -0700365static int btmrvl_send_frame(struct sk_buff *skb)
366{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200367 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700368 struct btmrvl_private *priv = NULL;
369
Marcel Holtmann93742532009-06-13 07:40:18 +0200370 BT_DBG("type=%d, len=%d", skb->pkt_type, skb->len);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700371
David Herrmann155961e2012-02-09 21:58:32 +0100372 if (!hdev) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700373 BT_ERR("Frame for unknown HCI device");
Bing Zhao132ff4e2009-06-02 14:29:35 -0700374 return -ENODEV;
375 }
376
David Herrmann155961e2012-02-09 21:58:32 +0100377 priv = hci_get_drvdata(hdev);
378
Bing Zhao132ff4e2009-06-02 14:29:35 -0700379 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
380 BT_ERR("Failed testing HCI_RUNING, flags=%lx", hdev->flags);
381 print_hex_dump_bytes("data: ", DUMP_PREFIX_OFFSET,
Marcel Holtmann4271e082009-06-09 16:00:22 +0200382 skb->data, skb->len);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700383 return -EBUSY;
384 }
385
386 switch (bt_cb(skb)->pkt_type) {
387 case HCI_COMMAND_PKT:
388 hdev->stat.cmd_tx++;
389 break;
390
391 case HCI_ACLDATA_PKT:
392 hdev->stat.acl_tx++;
393 break;
394
395 case HCI_SCODATA_PKT:
396 hdev->stat.sco_tx++;
397 break;
398 }
399
400 skb_queue_tail(&priv->adapter->tx_queue, skb);
401
402 wake_up_interruptible(&priv->main_thread.wait_q);
403
Bing Zhao132ff4e2009-06-02 14:29:35 -0700404 return 0;
405}
406
407static int btmrvl_flush(struct hci_dev *hdev)
408{
David Herrmann155961e2012-02-09 21:58:32 +0100409 struct btmrvl_private *priv = hci_get_drvdata(hdev);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700410
Bing Zhao132ff4e2009-06-02 14:29:35 -0700411 skb_queue_purge(&priv->adapter->tx_queue);
412
Bing Zhao132ff4e2009-06-02 14:29:35 -0700413 return 0;
414}
415
416static int btmrvl_close(struct hci_dev *hdev)
417{
David Herrmann155961e2012-02-09 21:58:32 +0100418 struct btmrvl_private *priv = hci_get_drvdata(hdev);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700419
Marcel Holtmann93742532009-06-13 07:40:18 +0200420 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
Bing Zhao132ff4e2009-06-02 14:29:35 -0700421 return 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700422
423 skb_queue_purge(&priv->adapter->tx_queue);
424
Bing Zhao132ff4e2009-06-02 14:29:35 -0700425 return 0;
426}
427
428static int btmrvl_open(struct hci_dev *hdev)
429{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700430 set_bit(HCI_RUNNING, &hdev->flags);
431
Bing Zhao132ff4e2009-06-02 14:29:35 -0700432 return 0;
433}
434
Amitkumar Karwar2cc86892013-10-01 12:19:15 -0700435/*
436 * This function parses provided calibration data input. It should contain
437 * hex bytes separated by space or new line character. Here is an example.
438 * 00 1C 01 37 FF FF FF FF 02 04 7F 01
439 * CE BA 00 00 00 2D C6 C0 00 00 00 00
440 * 00 F0 00 00
441 */
442static int btmrvl_parse_cal_cfg(const u8 *src, u32 len, u8 *dst, u32 dst_size)
443{
444 const u8 *s = src;
445 u8 *d = dst;
446 int ret;
447 u8 tmp[3];
448
449 tmp[2] = '\0';
450 while ((s - src) <= len - 2) {
451 if (isspace(*s)) {
452 s++;
453 continue;
454 }
455
456 if (isxdigit(*s)) {
457 if ((d - dst) >= dst_size) {
458 BT_ERR("calibration data file too big!!!");
459 return -EINVAL;
460 }
461
462 memcpy(tmp, s, 2);
463
464 ret = kstrtou8(tmp, 16, d++);
465 if (ret < 0)
466 return ret;
467
468 s += 2;
469 } else {
470 return -EINVAL;
471 }
472 }
473 if (d == dst)
474 return -EINVAL;
475
476 return 0;
477}
478
479static int btmrvl_load_cal_data(struct btmrvl_private *priv,
480 u8 *config_data)
481{
482 int i, ret;
483 u8 data[BT_CMD_DATA_SIZE];
484
485 data[0] = 0x00;
486 data[1] = 0x00;
487 data[2] = 0x00;
488 data[3] = BT_CMD_DATA_SIZE - 4;
489
490 /* Swap cal-data bytes. Each four bytes are swapped. Considering 4
491 * byte SDIO header offset, mapping of input and output bytes will be
492 * {3, 2, 1, 0} -> {0+4, 1+4, 2+4, 3+4},
493 * {7, 6, 5, 4} -> {4+4, 5+4, 6+4, 7+4} */
494 for (i = 4; i < BT_CMD_DATA_SIZE; i++)
495 data[i] = config_data[(i / 4) * 8 - 1 - i];
496
497 print_hex_dump_bytes("Calibration data: ",
498 DUMP_PREFIX_OFFSET, data, BT_CMD_DATA_SIZE);
499
500 ret = btmrvl_send_sync_cmd(priv, BT_CMD_LOAD_CONFIG_DATA, data,
501 BT_CMD_DATA_SIZE);
502 if (ret)
503 BT_ERR("Failed to download caibration data\n");
504
505 return 0;
506}
507
508static int
509btmrvl_process_cal_cfg(struct btmrvl_private *priv, u8 *data, u32 size)
510{
511 u8 cal_data[BT_CAL_DATA_SIZE];
512 int ret;
513
514 ret = btmrvl_parse_cal_cfg(data, size, cal_data, sizeof(cal_data));
515 if (ret)
516 return ret;
517
518 ret = btmrvl_load_cal_data(priv, cal_data);
519 if (ret) {
520 BT_ERR("Fail to load calibrate data");
521 return ret;
522 }
523
524 return 0;
525}
526
527static int btmrvl_cal_data_config(struct btmrvl_private *priv)
528{
529 const struct firmware *cfg;
530 int ret;
531 const char *cal_data = priv->btmrvl_dev.cal_data;
532
533 if (!cal_data)
534 return 0;
535
536 ret = request_firmware(&cfg, cal_data, priv->btmrvl_dev.dev);
537 if (ret < 0) {
538 BT_DBG("Failed to get %s file, skipping cal data download",
539 cal_data);
540 return 0;
541 }
542
543 ret = btmrvl_process_cal_cfg(priv, (u8 *)cfg->data, cfg->size);
544 release_firmware(cfg);
545 return ret;
546}
547
Amitkumar Karwar4b245722013-10-01 12:19:14 -0700548static int btmrvl_setup(struct hci_dev *hdev)
549{
550 struct btmrvl_private *priv = hci_get_drvdata(hdev);
551
552 btmrvl_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ);
553
Amitkumar Karwar2cc86892013-10-01 12:19:15 -0700554 if (btmrvl_cal_data_config(priv))
555 BT_ERR("Set cal data failed");
556
Amitkumar Karwar4b245722013-10-01 12:19:14 -0700557 priv->btmrvl_dev.psmode = 1;
558 btmrvl_enable_ps(priv);
559
560 priv->btmrvl_dev.gpio_gap = 0xffff;
561 btmrvl_send_hscfg_cmd(priv);
562
563 return 0;
564}
565
Bing Zhao132ff4e2009-06-02 14:29:35 -0700566/*
567 * This function handles the event generated by firmware, rx data
568 * received from firmware, and tx data sent from kernel.
569 */
570static int btmrvl_service_main_thread(void *data)
571{
572 struct btmrvl_thread *thread = data;
573 struct btmrvl_private *priv = thread->priv;
574 struct btmrvl_adapter *adapter = priv->adapter;
575 wait_queue_t wait;
576 struct sk_buff *skb;
577 ulong flags;
578
Bing Zhao132ff4e2009-06-02 14:29:35 -0700579 init_waitqueue_entry(&wait, current);
580
Bing Zhao132ff4e2009-06-02 14:29:35 -0700581 for (;;) {
582 add_wait_queue(&thread->wait_q, &wait);
583
584 set_current_state(TASK_INTERRUPTIBLE);
Daniel Drakeea05fea2013-06-11 15:40:20 -0400585 if (kthread_should_stop()) {
586 BT_DBG("main_thread: break from main thread");
587 break;
588 }
Bing Zhao132ff4e2009-06-02 14:29:35 -0700589
590 if (adapter->wakeup_tries ||
Marcel Holtmann4271e082009-06-09 16:00:22 +0200591 ((!adapter->int_count) &&
592 (!priv->btmrvl_dev.tx_dnld_rdy ||
593 skb_queue_empty(&adapter->tx_queue)))) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700594 BT_DBG("main_thread is sleeping...");
595 schedule();
596 }
597
598 set_current_state(TASK_RUNNING);
599
600 remove_wait_queue(&thread->wait_q, &wait);
601
602 BT_DBG("main_thread woke up");
603
Bing Zhao132ff4e2009-06-02 14:29:35 -0700604 spin_lock_irqsave(&priv->driver_lock, flags);
605 if (adapter->int_count) {
606 adapter->int_count = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700607 spin_unlock_irqrestore(&priv->driver_lock, flags);
608 priv->hw_process_int_status(priv);
Marcel Holtmann4271e082009-06-09 16:00:22 +0200609 } else if (adapter->ps_state == PS_SLEEP &&
610 !skb_queue_empty(&adapter->tx_queue)) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700611 spin_unlock_irqrestore(&priv->driver_lock, flags);
612 adapter->wakeup_tries++;
613 priv->hw_wakeup_firmware(priv);
614 continue;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700615 } else {
616 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700617 }
Bing Zhao132ff4e2009-06-02 14:29:35 -0700618
619 if (adapter->ps_state == PS_SLEEP)
620 continue;
621
622 if (!priv->btmrvl_dev.tx_dnld_rdy)
623 continue;
624
625 skb = skb_dequeue(&adapter->tx_queue);
626 if (skb) {
627 if (btmrvl_tx_pkt(priv, skb))
628 priv->btmrvl_dev.hcidev->stat.err_tx++;
629 else
Marcel Holtmann4271e082009-06-09 16:00:22 +0200630 priv->btmrvl_dev.hcidev->stat.byte_tx += skb->len;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700631
632 kfree_skb(skb);
633 }
634 }
635
Bing Zhao132ff4e2009-06-02 14:29:35 -0700636 return 0;
637}
638
Bing Zhao64061602010-03-03 14:37:36 -0800639int btmrvl_register_hdev(struct btmrvl_private *priv)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700640{
641 struct hci_dev *hdev = NULL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700642 int ret;
643
Bing Zhao64061602010-03-03 14:37:36 -0800644 hdev = hci_alloc_dev();
645 if (!hdev) {
646 BT_ERR("Can not allocate HCI device");
647 goto err_hdev;
648 }
649
650 priv->btmrvl_dev.hcidev = hdev;
David Herrmann155961e2012-02-09 21:58:32 +0100651 hci_set_drvdata(hdev, priv);
Bing Zhao64061602010-03-03 14:37:36 -0800652
653 hdev->bus = HCI_SDIO;
654 hdev->open = btmrvl_open;
655 hdev->close = btmrvl_close;
656 hdev->flush = btmrvl_flush;
657 hdev->send = btmrvl_send_frame;
Bing Zhao64061602010-03-03 14:37:36 -0800658 hdev->ioctl = btmrvl_ioctl;
Amitkumar Karwar4b245722013-10-01 12:19:14 -0700659 hdev->setup = btmrvl_setup;
Bing Zhao64061602010-03-03 14:37:36 -0800660
Bing Zhaof120c6b2010-03-03 14:37:37 -0800661 hdev->dev_type = priv->btmrvl_dev.dev_type;
662
Bing Zhao64061602010-03-03 14:37:36 -0800663 ret = hci_register_dev(hdev);
664 if (ret < 0) {
665 BT_ERR("Can not register HCI device");
666 goto err_hci_register_dev;
667 }
668
669#ifdef CONFIG_DEBUG_FS
670 btmrvl_debugfs_init(hdev);
671#endif
672
673 return 0;
674
675err_hci_register_dev:
676 hci_free_dev(hdev);
677
678err_hdev:
679 /* Stop the thread servicing the interrupts */
680 kthread_stop(priv->main_thread.task);
681
682 btmrvl_free_adapter(priv);
683 kfree(priv);
684
685 return -ENOMEM;
686}
687EXPORT_SYMBOL_GPL(btmrvl_register_hdev);
688
689struct btmrvl_private *btmrvl_add_card(void *card)
690{
691 struct btmrvl_private *priv;
692
Bing Zhao132ff4e2009-06-02 14:29:35 -0700693 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
694 if (!priv) {
695 BT_ERR("Can not allocate priv");
696 goto err_priv;
697 }
698
699 priv->adapter = kzalloc(sizeof(*priv->adapter), GFP_KERNEL);
700 if (!priv->adapter) {
701 BT_ERR("Allocate buffer for btmrvl_adapter failed!");
702 goto err_adapter;
703 }
704
705 btmrvl_init_adapter(priv);
706
Bing Zhao132ff4e2009-06-02 14:29:35 -0700707 BT_DBG("Starting kthread...");
708 priv->main_thread.priv = priv;
709 spin_lock_init(&priv->driver_lock);
710
711 init_waitqueue_head(&priv->main_thread.wait_q);
712 priv->main_thread.task = kthread_run(btmrvl_service_main_thread,
Marcel Holtmann4271e082009-06-09 16:00:22 +0200713 &priv->main_thread, "btmrvl_main_service");
Bing Zhao132ff4e2009-06-02 14:29:35 -0700714
Bing Zhao132ff4e2009-06-02 14:29:35 -0700715 priv->btmrvl_dev.card = card;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700716 priv->btmrvl_dev.tx_dnld_rdy = true;
717
Bing Zhao132ff4e2009-06-02 14:29:35 -0700718 return priv;
719
Bing Zhao132ff4e2009-06-02 14:29:35 -0700720err_adapter:
721 kfree(priv);
722
723err_priv:
Bing Zhao132ff4e2009-06-02 14:29:35 -0700724 return NULL;
725}
726EXPORT_SYMBOL_GPL(btmrvl_add_card);
727
728int btmrvl_remove_card(struct btmrvl_private *priv)
729{
730 struct hci_dev *hdev;
731
Bing Zhao132ff4e2009-06-02 14:29:35 -0700732 hdev = priv->btmrvl_dev.hcidev;
733
734 wake_up_interruptible(&priv->adapter->cmd_wait_q);
735
736 kthread_stop(priv->main_thread.task);
737
Bing Zhaofb784f02009-06-02 14:29:37 -0700738#ifdef CONFIG_DEBUG_FS
739 btmrvl_debugfs_remove(hdev);
740#endif
741
Bing Zhao132ff4e2009-06-02 14:29:35 -0700742 hci_unregister_dev(hdev);
743
744 hci_free_dev(hdev);
745
746 priv->btmrvl_dev.hcidev = NULL;
747
748 btmrvl_free_adapter(priv);
749
750 kfree(priv);
751
Bing Zhao132ff4e2009-06-02 14:29:35 -0700752 return 0;
753}
754EXPORT_SYMBOL_GPL(btmrvl_remove_card);
755
756MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +0200757MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700758MODULE_VERSION(VERSION);
759MODULE_LICENSE("GPL v2");