blob: 681ca9d18e125e39cbec92071560ce3d0da4269a [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
47void btmrvl_check_evtpkt(struct btmrvl_private *priv, struct sk_buff *skb)
48{
Marcel Holtmann4271e082009-06-09 16:00:22 +020049 struct hci_event_hdr *hdr = (void *) skb->data;
Bing Zhao132ff4e2009-06-02 14:29:35 -070050 struct hci_ev_cmd_complete *ec;
51 u16 opcode, ocf;
52
Bing Zhao132ff4e2009-06-02 14:29:35 -070053 if (hdr->evt == HCI_EV_CMD_COMPLETE) {
Marcel Holtmann4271e082009-06-09 16:00:22 +020054 ec = (void *) (skb->data + HCI_EVENT_HDR_SIZE);
Bing Zhao132ff4e2009-06-02 14:29:35 -070055 opcode = __le16_to_cpu(ec->opcode);
56 ocf = hci_opcode_ocf(opcode);
Marcel Holtmann4271e082009-06-09 16:00:22 +020057 if (ocf == BT_CMD_MODULE_CFG_REQ &&
58 priv->btmrvl_dev.sendcmdflag) {
Bing Zhao132ff4e2009-06-02 14:29:35 -070059 priv->btmrvl_dev.sendcmdflag = false;
60 priv->adapter->cmd_complete = true;
61 wake_up_interruptible(&priv->adapter->cmd_wait_q);
62 }
63 }
Bing Zhao132ff4e2009-06-02 14:29:35 -070064}
65EXPORT_SYMBOL_GPL(btmrvl_check_evtpkt);
66
67int btmrvl_process_event(struct btmrvl_private *priv, struct sk_buff *skb)
68{
69 struct btmrvl_adapter *adapter = priv->adapter;
Bing Zhao132ff4e2009-06-02 14:29:35 -070070 struct btmrvl_event *event;
Dan Carpenter31f085f2010-03-08 15:15:59 +030071 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -070072
Bing Zhao132ff4e2009-06-02 14:29:35 -070073 event = (struct btmrvl_event *) skb->data;
74 if (event->ec != 0xff) {
75 BT_DBG("Not Marvell Event=%x", event->ec);
76 ret = -EINVAL;
77 goto exit;
78 }
79
80 switch (event->data[0]) {
81 case BT_CMD_AUTO_SLEEP_MODE:
82 if (!event->data[2]) {
83 if (event->data[1] == BT_PS_ENABLE)
84 adapter->psmode = 1;
85 else
86 adapter->psmode = 0;
87 BT_DBG("PS Mode:%s",
88 (adapter->psmode) ? "Enable" : "Disable");
89 } else {
90 BT_DBG("PS Mode command failed");
91 }
92 break;
93
94 case BT_CMD_HOST_SLEEP_CONFIG:
95 if (!event->data[3])
96 BT_DBG("gpio=%x, gap=%x", event->data[1],
Marcel Holtmann4271e082009-06-09 16:00:22 +020097 event->data[2]);
Bing Zhao132ff4e2009-06-02 14:29:35 -070098 else
99 BT_DBG("HSCFG command failed");
100 break;
101
102 case BT_CMD_HOST_SLEEP_ENABLE:
103 if (!event->data[1]) {
104 adapter->hs_state = HS_ACTIVATED;
105 if (adapter->psmode)
106 adapter->ps_state = PS_SLEEP;
107 wake_up_interruptible(&adapter->cmd_wait_q);
108 BT_DBG("HS ACTIVATED!");
109 } else {
110 BT_DBG("HS Enable failed");
111 }
112 break;
113
114 case BT_CMD_MODULE_CFG_REQ:
Marcel Holtmann4271e082009-06-09 16:00:22 +0200115 if (priv->btmrvl_dev.sendcmdflag &&
116 event->data[1] == MODULE_BRINGUP_REQ) {
Bing Zhao903c8432010-03-03 14:37:35 -0800117 BT_DBG("EVENT:%s",
118 ((event->data[2] == MODULE_BROUGHT_UP) ||
119 (event->data[2] == MODULE_ALREADY_UP)) ?
120 "Bring-up succeed" : "Bring-up failed");
Bing Zhaof120c6b2010-03-03 14:37:37 -0800121
David Vrabel8f1e1742010-08-09 17:38:10 -0400122 if (event->length > 3 && event->data[3])
123 priv->btmrvl_dev.dev_type = HCI_AMP;
Bing Zhaof120c6b2010-03-03 14:37:37 -0800124 else
125 priv->btmrvl_dev.dev_type = HCI_BREDR;
126
127 BT_DBG("dev_type: %d", priv->btmrvl_dev.dev_type);
Marcel Holtmann4271e082009-06-09 16:00:22 +0200128 } else if (priv->btmrvl_dev.sendcmdflag &&
129 event->data[1] == MODULE_SHUTDOWN_REQ) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700130 BT_DBG("EVENT:%s", (event->data[2]) ?
131 "Shutdown failed" : "Shutdown succeed");
132 } else {
133 BT_DBG("BT_CMD_MODULE_CFG_REQ resp for APP");
134 ret = -EINVAL;
135 }
136 break;
137
138 case BT_EVENT_POWER_STATE:
139 if (event->data[1] == BT_PS_SLEEP)
140 adapter->ps_state = PS_SLEEP;
141 BT_DBG("EVENT:%s",
142 (adapter->ps_state) ? "PS_SLEEP" : "PS_AWAKE");
143 break;
144
145 default:
146 BT_DBG("Unknown Event=%d", event->data[0]);
147 ret = -EINVAL;
148 break;
149 }
150
151exit:
152 if (!ret)
153 kfree_skb(skb);
154
Bing Zhao132ff4e2009-06-02 14:29:35 -0700155 return ret;
156}
157EXPORT_SYMBOL_GPL(btmrvl_process_event);
158
159int btmrvl_send_module_cfg_cmd(struct btmrvl_private *priv, int subcmd)
160{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200161 struct sk_buff *skb;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700162 struct btmrvl_cmd *cmd;
Marcel Holtmann93742532009-06-13 07:40:18 +0200163 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700164
165 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
166 if (skb == NULL) {
167 BT_ERR("No free skb");
Marcel Holtmann93742532009-06-13 07:40:18 +0200168 return -ENOMEM;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700169 }
170
Marcel Holtmanne0721f92009-06-13 07:27:19 +0200171 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
Marcel Holtmann91d69732009-06-10 12:18:50 +0200172 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_MODULE_CFG_REQ));
Bing Zhao132ff4e2009-06-02 14:29:35 -0700173 cmd->length = 1;
174 cmd->data[0] = subcmd;
175
176 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
177
Marcel Holtmann4271e082009-06-09 16:00:22 +0200178 skb->dev = (void *) priv->btmrvl_dev.hcidev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700179 skb_queue_head(&priv->adapter->tx_queue, skb);
180
181 priv->btmrvl_dev.sendcmdflag = true;
182
183 priv->adapter->cmd_complete = false;
184
185 BT_DBG("Queue module cfg Command");
186
187 wake_up_interruptible(&priv->main_thread.wait_q);
188
Marcel Holtmann4271e082009-06-09 16:00:22 +0200189 if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
190 priv->adapter->cmd_complete,
191 msecs_to_jiffies(WAIT_UNTIL_CMD_RESP))) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700192 ret = -ETIMEDOUT;
193 BT_ERR("module_cfg_cmd(%x): timeout: %d",
Marcel Holtmann4271e082009-06-09 16:00:22 +0200194 subcmd, priv->btmrvl_dev.sendcmdflag);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700195 }
196
197 BT_DBG("module cfg Command done");
198
Bing Zhao132ff4e2009-06-02 14:29:35 -0700199 return ret;
200}
201EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd);
202
Amitkumar Karwar2a8ff612012-04-24 16:31:40 -0700203int btmrvl_send_hscfg_cmd(struct btmrvl_private *priv)
204{
205 struct sk_buff *skb;
206 struct btmrvl_cmd *cmd;
207
208 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
209 if (!skb) {
210 BT_ERR("No free skb");
211 return -ENOMEM;
212 }
213
214 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
215 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF,
216 BT_CMD_HOST_SLEEP_CONFIG));
217 cmd->length = 2;
218 cmd->data[0] = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8;
219 cmd->data[1] = (u8) (priv->btmrvl_dev.gpio_gap & 0x00ff);
220
221 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
222
223 skb->dev = (void *) priv->btmrvl_dev.hcidev;
224 skb_queue_head(&priv->adapter->tx_queue, skb);
225
226 BT_DBG("Queue HSCFG Command, gpio=0x%x, gap=0x%x", cmd->data[0],
227 cmd->data[1]);
228
229 return 0;
230}
231EXPORT_SYMBOL_GPL(btmrvl_send_hscfg_cmd);
232
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700233int btmrvl_enable_ps(struct btmrvl_private *priv)
234{
235 struct sk_buff *skb;
236 struct btmrvl_cmd *cmd;
237
238 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
239 if (skb == NULL) {
240 BT_ERR("No free skb");
241 return -ENOMEM;
242 }
243
244 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
245 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF,
246 BT_CMD_AUTO_SLEEP_MODE));
247 cmd->length = 1;
248
249 if (priv->btmrvl_dev.psmode)
250 cmd->data[0] = BT_PS_ENABLE;
251 else
252 cmd->data[0] = BT_PS_DISABLE;
253
254 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
255
256 skb->dev = (void *) priv->btmrvl_dev.hcidev;
257 skb_queue_head(&priv->adapter->tx_queue, skb);
258
259 BT_DBG("Queue PSMODE Command:%d", cmd->data[0]);
260
261 return 0;
262}
263EXPORT_SYMBOL_GPL(btmrvl_enable_ps);
264
Amitkumar Karwarba54a162012-04-25 11:43:54 -0700265int btmrvl_enable_hs(struct btmrvl_private *priv)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700266{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200267 struct sk_buff *skb;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700268 struct btmrvl_cmd *cmd;
Marcel Holtmann93742532009-06-13 07:40:18 +0200269 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700270
271 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
272 if (skb == NULL) {
273 BT_ERR("No free skb");
Marcel Holtmann93742532009-06-13 07:40:18 +0200274 return -ENOMEM;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700275 }
276
Marcel Holtmanne0721f92009-06-13 07:27:19 +0200277 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
Marcel Holtmann91d69732009-06-10 12:18:50 +0200278 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_HOST_SLEEP_ENABLE));
Bing Zhao132ff4e2009-06-02 14:29:35 -0700279 cmd->length = 0;
280
281 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
282
Marcel Holtmann4271e082009-06-09 16:00:22 +0200283 skb->dev = (void *) priv->btmrvl_dev.hcidev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700284 skb_queue_head(&priv->adapter->tx_queue, skb);
285
286 BT_DBG("Queue hs enable Command");
287
288 wake_up_interruptible(&priv->main_thread.wait_q);
289
Marcel Holtmann4271e082009-06-09 16:00:22 +0200290 if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
Bing Zhao132ff4e2009-06-02 14:29:35 -0700291 priv->adapter->hs_state,
292 msecs_to_jiffies(WAIT_UNTIL_HS_STATE_CHANGED))) {
293 ret = -ETIMEDOUT;
Marcel Holtmann4271e082009-06-09 16:00:22 +0200294 BT_ERR("timeout: %d, %d,%d", priv->adapter->hs_state,
295 priv->adapter->ps_state,
296 priv->adapter->wakeup_tries);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700297 }
298
Bing Zhao132ff4e2009-06-02 14:29:35 -0700299 return ret;
300}
Amitkumar Karwarba54a162012-04-25 11:43:54 -0700301EXPORT_SYMBOL_GPL(btmrvl_enable_hs);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700302
303int btmrvl_prepare_command(struct btmrvl_private *priv)
304{
Marcel Holtmann93742532009-06-13 07:40:18 +0200305 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700306
307 if (priv->btmrvl_dev.hscfgcmd) {
308 priv->btmrvl_dev.hscfgcmd = 0;
Amitkumar Karwar2a8ff612012-04-24 16:31:40 -0700309 btmrvl_send_hscfg_cmd(priv);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700310 }
311
312 if (priv->btmrvl_dev.pscmd) {
313 priv->btmrvl_dev.pscmd = 0;
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700314 btmrvl_enable_ps(priv);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700315 }
316
317 if (priv->btmrvl_dev.hscmd) {
318 priv->btmrvl_dev.hscmd = 0;
319
320 if (priv->btmrvl_dev.hsmode) {
321 ret = btmrvl_enable_hs(priv);
322 } else {
323 ret = priv->hw_wakeup_firmware(priv);
324 priv->adapter->hs_state = HS_DEACTIVATED;
325 }
326 }
327
Bing Zhao132ff4e2009-06-02 14:29:35 -0700328 return ret;
329}
330
331static int btmrvl_tx_pkt(struct btmrvl_private *priv, struct sk_buff *skb)
332{
Marcel Holtmann93742532009-06-13 07:40:18 +0200333 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700334
Marcel Holtmann93742532009-06-13 07:40:18 +0200335 if (!skb || !skb->data)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700336 return -EINVAL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700337
338 if (!skb->len || ((skb->len + BTM_HEADER_LEN) > BTM_UPLD_SIZE)) {
339 BT_ERR("Tx Error: Bad skb length %d : %d",
Marcel Holtmann4271e082009-06-09 16:00:22 +0200340 skb->len, BTM_UPLD_SIZE);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700341 return -EINVAL;
342 }
343
344 if (skb_headroom(skb) < BTM_HEADER_LEN) {
345 struct sk_buff *tmp = skb;
346
347 skb = skb_realloc_headroom(skb, BTM_HEADER_LEN);
348 if (!skb) {
349 BT_ERR("Tx Error: realloc_headroom failed %d",
350 BTM_HEADER_LEN);
351 skb = tmp;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700352 return -EINVAL;
353 }
354
355 kfree_skb(tmp);
356 }
357
358 skb_push(skb, BTM_HEADER_LEN);
359
360 /* header type: byte[3]
361 * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor
362 * header length: byte[2][1][0]
363 */
364
365 skb->data[0] = (skb->len & 0x0000ff);
366 skb->data[1] = (skb->len & 0x00ff00) >> 8;
367 skb->data[2] = (skb->len & 0xff0000) >> 16;
368 skb->data[3] = bt_cb(skb)->pkt_type;
369
370 if (priv->hw_host_to_card)
371 ret = priv->hw_host_to_card(priv, skb->data, skb->len);
372
Bing Zhao132ff4e2009-06-02 14:29:35 -0700373 return ret;
374}
375
376static void btmrvl_init_adapter(struct btmrvl_private *priv)
377{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700378 skb_queue_head_init(&priv->adapter->tx_queue);
379
380 priv->adapter->ps_state = PS_AWAKE;
381
382 init_waitqueue_head(&priv->adapter->cmd_wait_q);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700383}
384
385static void btmrvl_free_adapter(struct btmrvl_private *priv)
386{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700387 skb_queue_purge(&priv->adapter->tx_queue);
388
389 kfree(priv->adapter);
390
391 priv->adapter = NULL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700392}
393
Marcel Holtmann4271e082009-06-09 16:00:22 +0200394static int btmrvl_ioctl(struct hci_dev *hdev,
395 unsigned int cmd, unsigned long arg)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700396{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700397 return -ENOIOCTLCMD;
398}
399
Bing Zhao132ff4e2009-06-02 14:29:35 -0700400static int btmrvl_send_frame(struct sk_buff *skb)
401{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200402 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700403 struct btmrvl_private *priv = NULL;
404
Marcel Holtmann93742532009-06-13 07:40:18 +0200405 BT_DBG("type=%d, len=%d", skb->pkt_type, skb->len);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700406
David Herrmann155961e2012-02-09 21:58:32 +0100407 if (!hdev) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700408 BT_ERR("Frame for unknown HCI device");
Bing Zhao132ff4e2009-06-02 14:29:35 -0700409 return -ENODEV;
410 }
411
David Herrmann155961e2012-02-09 21:58:32 +0100412 priv = hci_get_drvdata(hdev);
413
Bing Zhao132ff4e2009-06-02 14:29:35 -0700414 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
415 BT_ERR("Failed testing HCI_RUNING, flags=%lx", hdev->flags);
416 print_hex_dump_bytes("data: ", DUMP_PREFIX_OFFSET,
Marcel Holtmann4271e082009-06-09 16:00:22 +0200417 skb->data, skb->len);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700418 return -EBUSY;
419 }
420
421 switch (bt_cb(skb)->pkt_type) {
422 case HCI_COMMAND_PKT:
423 hdev->stat.cmd_tx++;
424 break;
425
426 case HCI_ACLDATA_PKT:
427 hdev->stat.acl_tx++;
428 break;
429
430 case HCI_SCODATA_PKT:
431 hdev->stat.sco_tx++;
432 break;
433 }
434
435 skb_queue_tail(&priv->adapter->tx_queue, skb);
436
437 wake_up_interruptible(&priv->main_thread.wait_q);
438
Bing Zhao132ff4e2009-06-02 14:29:35 -0700439 return 0;
440}
441
442static int btmrvl_flush(struct hci_dev *hdev)
443{
David Herrmann155961e2012-02-09 21:58:32 +0100444 struct btmrvl_private *priv = hci_get_drvdata(hdev);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700445
Bing Zhao132ff4e2009-06-02 14:29:35 -0700446 skb_queue_purge(&priv->adapter->tx_queue);
447
Bing Zhao132ff4e2009-06-02 14:29:35 -0700448 return 0;
449}
450
451static int btmrvl_close(struct hci_dev *hdev)
452{
David Herrmann155961e2012-02-09 21:58:32 +0100453 struct btmrvl_private *priv = hci_get_drvdata(hdev);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700454
Marcel Holtmann93742532009-06-13 07:40:18 +0200455 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
Bing Zhao132ff4e2009-06-02 14:29:35 -0700456 return 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700457
458 skb_queue_purge(&priv->adapter->tx_queue);
459
Bing Zhao132ff4e2009-06-02 14:29:35 -0700460 return 0;
461}
462
463static int btmrvl_open(struct hci_dev *hdev)
464{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700465 set_bit(HCI_RUNNING, &hdev->flags);
466
Bing Zhao132ff4e2009-06-02 14:29:35 -0700467 return 0;
468}
469
470/*
471 * This function handles the event generated by firmware, rx data
472 * received from firmware, and tx data sent from kernel.
473 */
474static int btmrvl_service_main_thread(void *data)
475{
476 struct btmrvl_thread *thread = data;
477 struct btmrvl_private *priv = thread->priv;
478 struct btmrvl_adapter *adapter = priv->adapter;
479 wait_queue_t wait;
480 struct sk_buff *skb;
481 ulong flags;
482
Bing Zhao132ff4e2009-06-02 14:29:35 -0700483 init_waitqueue_entry(&wait, current);
484
Bing Zhao132ff4e2009-06-02 14:29:35 -0700485 for (;;) {
486 add_wait_queue(&thread->wait_q, &wait);
487
488 set_current_state(TASK_INTERRUPTIBLE);
489
490 if (adapter->wakeup_tries ||
Marcel Holtmann4271e082009-06-09 16:00:22 +0200491 ((!adapter->int_count) &&
492 (!priv->btmrvl_dev.tx_dnld_rdy ||
493 skb_queue_empty(&adapter->tx_queue)))) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700494 BT_DBG("main_thread is sleeping...");
495 schedule();
496 }
497
498 set_current_state(TASK_RUNNING);
499
500 remove_wait_queue(&thread->wait_q, &wait);
501
502 BT_DBG("main_thread woke up");
503
504 if (kthread_should_stop()) {
505 BT_DBG("main_thread: break from main thread");
506 break;
507 }
508
509 spin_lock_irqsave(&priv->driver_lock, flags);
510 if (adapter->int_count) {
511 adapter->int_count = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700512 spin_unlock_irqrestore(&priv->driver_lock, flags);
513 priv->hw_process_int_status(priv);
Marcel Holtmann4271e082009-06-09 16:00:22 +0200514 } else if (adapter->ps_state == PS_SLEEP &&
515 !skb_queue_empty(&adapter->tx_queue)) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700516 spin_unlock_irqrestore(&priv->driver_lock, flags);
517 adapter->wakeup_tries++;
518 priv->hw_wakeup_firmware(priv);
519 continue;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700520 } else {
521 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700522 }
Bing Zhao132ff4e2009-06-02 14:29:35 -0700523
524 if (adapter->ps_state == PS_SLEEP)
525 continue;
526
527 if (!priv->btmrvl_dev.tx_dnld_rdy)
528 continue;
529
530 skb = skb_dequeue(&adapter->tx_queue);
531 if (skb) {
532 if (btmrvl_tx_pkt(priv, skb))
533 priv->btmrvl_dev.hcidev->stat.err_tx++;
534 else
Marcel Holtmann4271e082009-06-09 16:00:22 +0200535 priv->btmrvl_dev.hcidev->stat.byte_tx += skb->len;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700536
537 kfree_skb(skb);
538 }
539 }
540
Bing Zhao132ff4e2009-06-02 14:29:35 -0700541 return 0;
542}
543
Bing Zhao64061602010-03-03 14:37:36 -0800544int btmrvl_register_hdev(struct btmrvl_private *priv)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700545{
546 struct hci_dev *hdev = NULL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700547 int ret;
548
Bing Zhao64061602010-03-03 14:37:36 -0800549 hdev = hci_alloc_dev();
550 if (!hdev) {
551 BT_ERR("Can not allocate HCI device");
552 goto err_hdev;
553 }
554
555 priv->btmrvl_dev.hcidev = hdev;
David Herrmann155961e2012-02-09 21:58:32 +0100556 hci_set_drvdata(hdev, priv);
Bing Zhao64061602010-03-03 14:37:36 -0800557
558 hdev->bus = HCI_SDIO;
559 hdev->open = btmrvl_open;
560 hdev->close = btmrvl_close;
561 hdev->flush = btmrvl_flush;
562 hdev->send = btmrvl_send_frame;
Bing Zhao64061602010-03-03 14:37:36 -0800563 hdev->ioctl = btmrvl_ioctl;
Bing Zhao64061602010-03-03 14:37:36 -0800564
565 btmrvl_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ);
566
Bing Zhaof120c6b2010-03-03 14:37:37 -0800567 hdev->dev_type = priv->btmrvl_dev.dev_type;
568
Bing Zhao64061602010-03-03 14:37:36 -0800569 ret = hci_register_dev(hdev);
570 if (ret < 0) {
571 BT_ERR("Can not register HCI device");
572 goto err_hci_register_dev;
573 }
574
575#ifdef CONFIG_DEBUG_FS
576 btmrvl_debugfs_init(hdev);
577#endif
578
579 return 0;
580
581err_hci_register_dev:
582 hci_free_dev(hdev);
583
584err_hdev:
585 /* Stop the thread servicing the interrupts */
586 kthread_stop(priv->main_thread.task);
587
588 btmrvl_free_adapter(priv);
589 kfree(priv);
590
591 return -ENOMEM;
592}
593EXPORT_SYMBOL_GPL(btmrvl_register_hdev);
594
595struct btmrvl_private *btmrvl_add_card(void *card)
596{
597 struct btmrvl_private *priv;
598
Bing Zhao132ff4e2009-06-02 14:29:35 -0700599 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
600 if (!priv) {
601 BT_ERR("Can not allocate priv");
602 goto err_priv;
603 }
604
605 priv->adapter = kzalloc(sizeof(*priv->adapter), GFP_KERNEL);
606 if (!priv->adapter) {
607 BT_ERR("Allocate buffer for btmrvl_adapter failed!");
608 goto err_adapter;
609 }
610
611 btmrvl_init_adapter(priv);
612
Bing Zhao132ff4e2009-06-02 14:29:35 -0700613 BT_DBG("Starting kthread...");
614 priv->main_thread.priv = priv;
615 spin_lock_init(&priv->driver_lock);
616
617 init_waitqueue_head(&priv->main_thread.wait_q);
618 priv->main_thread.task = kthread_run(btmrvl_service_main_thread,
Marcel Holtmann4271e082009-06-09 16:00:22 +0200619 &priv->main_thread, "btmrvl_main_service");
Bing Zhao132ff4e2009-06-02 14:29:35 -0700620
Bing Zhao132ff4e2009-06-02 14:29:35 -0700621 priv->btmrvl_dev.card = card;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700622 priv->btmrvl_dev.tx_dnld_rdy = true;
623
Bing Zhao132ff4e2009-06-02 14:29:35 -0700624 return priv;
625
Bing Zhao132ff4e2009-06-02 14:29:35 -0700626err_adapter:
627 kfree(priv);
628
629err_priv:
Bing Zhao132ff4e2009-06-02 14:29:35 -0700630 return NULL;
631}
632EXPORT_SYMBOL_GPL(btmrvl_add_card);
633
634int btmrvl_remove_card(struct btmrvl_private *priv)
635{
636 struct hci_dev *hdev;
637
Bing Zhao132ff4e2009-06-02 14:29:35 -0700638 hdev = priv->btmrvl_dev.hcidev;
639
640 wake_up_interruptible(&priv->adapter->cmd_wait_q);
641
642 kthread_stop(priv->main_thread.task);
643
Bing Zhaofb784f02009-06-02 14:29:37 -0700644#ifdef CONFIG_DEBUG_FS
645 btmrvl_debugfs_remove(hdev);
646#endif
647
Bing Zhao132ff4e2009-06-02 14:29:35 -0700648 hci_unregister_dev(hdev);
649
650 hci_free_dev(hdev);
651
652 priv->btmrvl_dev.hcidev = NULL;
653
654 btmrvl_free_adapter(priv);
655
656 kfree(priv);
657
Bing Zhao132ff4e2009-06-02 14:29:35 -0700658 return 0;
659}
660EXPORT_SYMBOL_GPL(btmrvl_remove_card);
661
662MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +0200663MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700664MODULE_VERSION(VERSION);
665MODULE_LICENSE("GPL v2");