blob: ee37ef0caee269c0383fd9336ea9cf0d665ddba0 [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
21#include <net/bluetooth/bluetooth.h>
22#include <net/bluetooth/hci_core.h>
23
24#include "btmrvl_drv.h"
25
26#define VERSION "1.0"
27
28/*
29 * This function is called by interface specific interrupt handler.
30 * It updates Power Save & Host Sleep states, and wakes up the main
31 * thread.
32 */
33void btmrvl_interrupt(struct btmrvl_private *priv)
34{
Bing Zhao132ff4e2009-06-02 14:29:35 -070035 priv->adapter->ps_state = PS_AWAKE;
36
37 priv->adapter->wakeup_tries = 0;
38
39 priv->adapter->int_count++;
40
41 wake_up_interruptible(&priv->main_thread.wait_q);
Bing Zhao132ff4e2009-06-02 14:29:35 -070042}
43EXPORT_SYMBOL_GPL(btmrvl_interrupt);
44
45void btmrvl_check_evtpkt(struct btmrvl_private *priv, struct sk_buff *skb)
46{
Marcel Holtmann4271e082009-06-09 16:00:22 +020047 struct hci_event_hdr *hdr = (void *) skb->data;
Bing Zhao132ff4e2009-06-02 14:29:35 -070048 struct hci_ev_cmd_complete *ec;
49 u16 opcode, ocf;
50
Bing Zhao132ff4e2009-06-02 14:29:35 -070051 if (hdr->evt == HCI_EV_CMD_COMPLETE) {
Marcel Holtmann4271e082009-06-09 16:00:22 +020052 ec = (void *) (skb->data + HCI_EVENT_HDR_SIZE);
Bing Zhao132ff4e2009-06-02 14:29:35 -070053 opcode = __le16_to_cpu(ec->opcode);
54 ocf = hci_opcode_ocf(opcode);
Marcel Holtmann4271e082009-06-09 16:00:22 +020055 if (ocf == BT_CMD_MODULE_CFG_REQ &&
56 priv->btmrvl_dev.sendcmdflag) {
Bing Zhao132ff4e2009-06-02 14:29:35 -070057 priv->btmrvl_dev.sendcmdflag = false;
58 priv->adapter->cmd_complete = true;
59 wake_up_interruptible(&priv->adapter->cmd_wait_q);
60 }
61 }
Bing Zhao132ff4e2009-06-02 14:29:35 -070062}
63EXPORT_SYMBOL_GPL(btmrvl_check_evtpkt);
64
65int btmrvl_process_event(struct btmrvl_private *priv, struct sk_buff *skb)
66{
67 struct btmrvl_adapter *adapter = priv->adapter;
Bing Zhao132ff4e2009-06-02 14:29:35 -070068 struct btmrvl_event *event;
Dan Carpenter31f085f2010-03-08 15:15:59 +030069 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -070070
Bing Zhao132ff4e2009-06-02 14:29:35 -070071 event = (struct btmrvl_event *) skb->data;
72 if (event->ec != 0xff) {
73 BT_DBG("Not Marvell Event=%x", event->ec);
74 ret = -EINVAL;
75 goto exit;
76 }
77
78 switch (event->data[0]) {
79 case BT_CMD_AUTO_SLEEP_MODE:
80 if (!event->data[2]) {
81 if (event->data[1] == BT_PS_ENABLE)
82 adapter->psmode = 1;
83 else
84 adapter->psmode = 0;
85 BT_DBG("PS Mode:%s",
86 (adapter->psmode) ? "Enable" : "Disable");
87 } else {
88 BT_DBG("PS Mode command failed");
89 }
90 break;
91
92 case BT_CMD_HOST_SLEEP_CONFIG:
93 if (!event->data[3])
94 BT_DBG("gpio=%x, gap=%x", event->data[1],
Marcel Holtmann4271e082009-06-09 16:00:22 +020095 event->data[2]);
Bing Zhao132ff4e2009-06-02 14:29:35 -070096 else
97 BT_DBG("HSCFG command failed");
98 break;
99
100 case BT_CMD_HOST_SLEEP_ENABLE:
101 if (!event->data[1]) {
102 adapter->hs_state = HS_ACTIVATED;
103 if (adapter->psmode)
104 adapter->ps_state = PS_SLEEP;
105 wake_up_interruptible(&adapter->cmd_wait_q);
106 BT_DBG("HS ACTIVATED!");
107 } else {
108 BT_DBG("HS Enable failed");
109 }
110 break;
111
112 case BT_CMD_MODULE_CFG_REQ:
Marcel Holtmann4271e082009-06-09 16:00:22 +0200113 if (priv->btmrvl_dev.sendcmdflag &&
114 event->data[1] == MODULE_BRINGUP_REQ) {
Bing Zhao903c8432010-03-03 14:37:35 -0800115 BT_DBG("EVENT:%s",
116 ((event->data[2] == MODULE_BROUGHT_UP) ||
117 (event->data[2] == MODULE_ALREADY_UP)) ?
118 "Bring-up succeed" : "Bring-up failed");
Bing Zhaof120c6b2010-03-03 14:37:37 -0800119
120 if (event->length > 3)
121 priv->btmrvl_dev.dev_type = event->data[3];
122 else
123 priv->btmrvl_dev.dev_type = HCI_BREDR;
124
125 BT_DBG("dev_type: %d", priv->btmrvl_dev.dev_type);
Marcel Holtmann4271e082009-06-09 16:00:22 +0200126 } else if (priv->btmrvl_dev.sendcmdflag &&
127 event->data[1] == MODULE_SHUTDOWN_REQ) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700128 BT_DBG("EVENT:%s", (event->data[2]) ?
129 "Shutdown failed" : "Shutdown succeed");
130 } else {
131 BT_DBG("BT_CMD_MODULE_CFG_REQ resp for APP");
132 ret = -EINVAL;
133 }
134 break;
135
136 case BT_EVENT_POWER_STATE:
137 if (event->data[1] == BT_PS_SLEEP)
138 adapter->ps_state = PS_SLEEP;
139 BT_DBG("EVENT:%s",
140 (adapter->ps_state) ? "PS_SLEEP" : "PS_AWAKE");
141 break;
142
143 default:
144 BT_DBG("Unknown Event=%d", event->data[0]);
145 ret = -EINVAL;
146 break;
147 }
148
149exit:
150 if (!ret)
151 kfree_skb(skb);
152
Bing Zhao132ff4e2009-06-02 14:29:35 -0700153 return ret;
154}
155EXPORT_SYMBOL_GPL(btmrvl_process_event);
156
157int btmrvl_send_module_cfg_cmd(struct btmrvl_private *priv, int subcmd)
158{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200159 struct sk_buff *skb;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700160 struct btmrvl_cmd *cmd;
Marcel Holtmann93742532009-06-13 07:40:18 +0200161 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700162
163 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
164 if (skb == NULL) {
165 BT_ERR("No free skb");
Marcel Holtmann93742532009-06-13 07:40:18 +0200166 return -ENOMEM;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700167 }
168
Marcel Holtmanne0721f92009-06-13 07:27:19 +0200169 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
Marcel Holtmann91d69732009-06-10 12:18:50 +0200170 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_MODULE_CFG_REQ));
Bing Zhao132ff4e2009-06-02 14:29:35 -0700171 cmd->length = 1;
172 cmd->data[0] = subcmd;
173
174 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
175
Marcel Holtmann4271e082009-06-09 16:00:22 +0200176 skb->dev = (void *) priv->btmrvl_dev.hcidev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700177 skb_queue_head(&priv->adapter->tx_queue, skb);
178
179 priv->btmrvl_dev.sendcmdflag = true;
180
181 priv->adapter->cmd_complete = false;
182
183 BT_DBG("Queue module cfg Command");
184
185 wake_up_interruptible(&priv->main_thread.wait_q);
186
Marcel Holtmann4271e082009-06-09 16:00:22 +0200187 if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
188 priv->adapter->cmd_complete,
189 msecs_to_jiffies(WAIT_UNTIL_CMD_RESP))) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700190 ret = -ETIMEDOUT;
191 BT_ERR("module_cfg_cmd(%x): timeout: %d",
Marcel Holtmann4271e082009-06-09 16:00:22 +0200192 subcmd, priv->btmrvl_dev.sendcmdflag);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700193 }
194
195 BT_DBG("module cfg Command done");
196
Bing Zhao132ff4e2009-06-02 14:29:35 -0700197 return ret;
198}
199EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd);
200
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700201int btmrvl_enable_ps(struct btmrvl_private *priv)
202{
203 struct sk_buff *skb;
204 struct btmrvl_cmd *cmd;
205
206 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
207 if (skb == NULL) {
208 BT_ERR("No free skb");
209 return -ENOMEM;
210 }
211
212 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
213 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF,
214 BT_CMD_AUTO_SLEEP_MODE));
215 cmd->length = 1;
216
217 if (priv->btmrvl_dev.psmode)
218 cmd->data[0] = BT_PS_ENABLE;
219 else
220 cmd->data[0] = BT_PS_DISABLE;
221
222 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
223
224 skb->dev = (void *) priv->btmrvl_dev.hcidev;
225 skb_queue_head(&priv->adapter->tx_queue, skb);
226
227 BT_DBG("Queue PSMODE Command:%d", cmd->data[0]);
228
229 return 0;
230}
231EXPORT_SYMBOL_GPL(btmrvl_enable_ps);
232
Bing Zhao132ff4e2009-06-02 14:29:35 -0700233static int btmrvl_enable_hs(struct btmrvl_private *priv)
234{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200235 struct sk_buff *skb;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700236 struct btmrvl_cmd *cmd;
Marcel Holtmann93742532009-06-13 07:40:18 +0200237 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700238
239 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
240 if (skb == NULL) {
241 BT_ERR("No free skb");
Marcel Holtmann93742532009-06-13 07:40:18 +0200242 return -ENOMEM;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700243 }
244
Marcel Holtmanne0721f92009-06-13 07:27:19 +0200245 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
Marcel Holtmann91d69732009-06-10 12:18:50 +0200246 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_HOST_SLEEP_ENABLE));
Bing Zhao132ff4e2009-06-02 14:29:35 -0700247 cmd->length = 0;
248
249 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
250
Marcel Holtmann4271e082009-06-09 16:00:22 +0200251 skb->dev = (void *) priv->btmrvl_dev.hcidev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700252 skb_queue_head(&priv->adapter->tx_queue, skb);
253
254 BT_DBG("Queue hs enable Command");
255
256 wake_up_interruptible(&priv->main_thread.wait_q);
257
Marcel Holtmann4271e082009-06-09 16:00:22 +0200258 if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
Bing Zhao132ff4e2009-06-02 14:29:35 -0700259 priv->adapter->hs_state,
260 msecs_to_jiffies(WAIT_UNTIL_HS_STATE_CHANGED))) {
261 ret = -ETIMEDOUT;
Marcel Holtmann4271e082009-06-09 16:00:22 +0200262 BT_ERR("timeout: %d, %d,%d", priv->adapter->hs_state,
263 priv->adapter->ps_state,
264 priv->adapter->wakeup_tries);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700265 }
266
Bing Zhao132ff4e2009-06-02 14:29:35 -0700267 return ret;
268}
269
270int btmrvl_prepare_command(struct btmrvl_private *priv)
271{
272 struct sk_buff *skb = NULL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700273 struct btmrvl_cmd *cmd;
Marcel Holtmann93742532009-06-13 07:40:18 +0200274 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700275
276 if (priv->btmrvl_dev.hscfgcmd) {
277 priv->btmrvl_dev.hscfgcmd = 0;
278
279 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
280 if (skb == NULL) {
281 BT_ERR("No free skb");
Marcel Holtmann93742532009-06-13 07:40:18 +0200282 return -ENOMEM;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700283 }
284
Marcel Holtmanne0721f92009-06-13 07:27:19 +0200285 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
Marcel Holtmann91d69732009-06-10 12:18:50 +0200286 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_HOST_SLEEP_CONFIG));
Bing Zhao132ff4e2009-06-02 14:29:35 -0700287 cmd->length = 2;
288 cmd->data[0] = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8;
289 cmd->data[1] = (u8) (priv->btmrvl_dev.gpio_gap & 0x00ff);
290
291 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
292
Marcel Holtmann4271e082009-06-09 16:00:22 +0200293 skb->dev = (void *) priv->btmrvl_dev.hcidev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700294 skb_queue_head(&priv->adapter->tx_queue, skb);
295
296 BT_DBG("Queue HSCFG Command, gpio=0x%x, gap=0x%x",
Marcel Holtmann4271e082009-06-09 16:00:22 +0200297 cmd->data[0], cmd->data[1]);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700298 }
299
300 if (priv->btmrvl_dev.pscmd) {
301 priv->btmrvl_dev.pscmd = 0;
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700302 btmrvl_enable_ps(priv);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700303 }
304
305 if (priv->btmrvl_dev.hscmd) {
306 priv->btmrvl_dev.hscmd = 0;
307
308 if (priv->btmrvl_dev.hsmode) {
309 ret = btmrvl_enable_hs(priv);
310 } else {
311 ret = priv->hw_wakeup_firmware(priv);
312 priv->adapter->hs_state = HS_DEACTIVATED;
313 }
314 }
315
Bing Zhao132ff4e2009-06-02 14:29:35 -0700316 return ret;
317}
318
319static int btmrvl_tx_pkt(struct btmrvl_private *priv, struct sk_buff *skb)
320{
Marcel Holtmann93742532009-06-13 07:40:18 +0200321 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700322
Marcel Holtmann93742532009-06-13 07:40:18 +0200323 if (!skb || !skb->data)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700324 return -EINVAL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700325
326 if (!skb->len || ((skb->len + BTM_HEADER_LEN) > BTM_UPLD_SIZE)) {
327 BT_ERR("Tx Error: Bad skb length %d : %d",
Marcel Holtmann4271e082009-06-09 16:00:22 +0200328 skb->len, BTM_UPLD_SIZE);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700329 return -EINVAL;
330 }
331
332 if (skb_headroom(skb) < BTM_HEADER_LEN) {
333 struct sk_buff *tmp = skb;
334
335 skb = skb_realloc_headroom(skb, BTM_HEADER_LEN);
336 if (!skb) {
337 BT_ERR("Tx Error: realloc_headroom failed %d",
338 BTM_HEADER_LEN);
339 skb = tmp;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700340 return -EINVAL;
341 }
342
343 kfree_skb(tmp);
344 }
345
346 skb_push(skb, BTM_HEADER_LEN);
347
348 /* header type: byte[3]
349 * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor
350 * header length: byte[2][1][0]
351 */
352
353 skb->data[0] = (skb->len & 0x0000ff);
354 skb->data[1] = (skb->len & 0x00ff00) >> 8;
355 skb->data[2] = (skb->len & 0xff0000) >> 16;
356 skb->data[3] = bt_cb(skb)->pkt_type;
357
358 if (priv->hw_host_to_card)
359 ret = priv->hw_host_to_card(priv, skb->data, skb->len);
360
Bing Zhao132ff4e2009-06-02 14:29:35 -0700361 return ret;
362}
363
364static void btmrvl_init_adapter(struct btmrvl_private *priv)
365{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700366 skb_queue_head_init(&priv->adapter->tx_queue);
367
368 priv->adapter->ps_state = PS_AWAKE;
369
370 init_waitqueue_head(&priv->adapter->cmd_wait_q);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700371}
372
373static void btmrvl_free_adapter(struct btmrvl_private *priv)
374{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700375 skb_queue_purge(&priv->adapter->tx_queue);
376
377 kfree(priv->adapter);
378
379 priv->adapter = NULL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700380}
381
Marcel Holtmann4271e082009-06-09 16:00:22 +0200382static int btmrvl_ioctl(struct hci_dev *hdev,
383 unsigned int cmd, unsigned long arg)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700384{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700385 return -ENOIOCTLCMD;
386}
387
388static void btmrvl_destruct(struct hci_dev *hdev)
389{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700390}
391
392static int btmrvl_send_frame(struct sk_buff *skb)
393{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200394 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700395 struct btmrvl_private *priv = NULL;
396
Marcel Holtmann93742532009-06-13 07:40:18 +0200397 BT_DBG("type=%d, len=%d", skb->pkt_type, skb->len);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700398
399 if (!hdev || !hdev->driver_data) {
400 BT_ERR("Frame for unknown HCI device");
Bing Zhao132ff4e2009-06-02 14:29:35 -0700401 return -ENODEV;
402 }
403
Marcel Holtmann4271e082009-06-09 16:00:22 +0200404 priv = (struct btmrvl_private *) hdev->driver_data;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700405 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
406 BT_ERR("Failed testing HCI_RUNING, flags=%lx", hdev->flags);
407 print_hex_dump_bytes("data: ", DUMP_PREFIX_OFFSET,
Marcel Holtmann4271e082009-06-09 16:00:22 +0200408 skb->data, skb->len);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700409 return -EBUSY;
410 }
411
412 switch (bt_cb(skb)->pkt_type) {
413 case HCI_COMMAND_PKT:
414 hdev->stat.cmd_tx++;
415 break;
416
417 case HCI_ACLDATA_PKT:
418 hdev->stat.acl_tx++;
419 break;
420
421 case HCI_SCODATA_PKT:
422 hdev->stat.sco_tx++;
423 break;
424 }
425
426 skb_queue_tail(&priv->adapter->tx_queue, skb);
427
428 wake_up_interruptible(&priv->main_thread.wait_q);
429
Bing Zhao132ff4e2009-06-02 14:29:35 -0700430 return 0;
431}
432
433static int btmrvl_flush(struct hci_dev *hdev)
434{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200435 struct btmrvl_private *priv = hdev->driver_data;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700436
Bing Zhao132ff4e2009-06-02 14:29:35 -0700437 skb_queue_purge(&priv->adapter->tx_queue);
438
Bing Zhao132ff4e2009-06-02 14:29:35 -0700439 return 0;
440}
441
442static int btmrvl_close(struct hci_dev *hdev)
443{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200444 struct btmrvl_private *priv = hdev->driver_data;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700445
Marcel Holtmann93742532009-06-13 07:40:18 +0200446 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
Bing Zhao132ff4e2009-06-02 14:29:35 -0700447 return 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700448
449 skb_queue_purge(&priv->adapter->tx_queue);
450
Bing Zhao132ff4e2009-06-02 14:29:35 -0700451 return 0;
452}
453
454static int btmrvl_open(struct hci_dev *hdev)
455{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700456 set_bit(HCI_RUNNING, &hdev->flags);
457
Bing Zhao132ff4e2009-06-02 14:29:35 -0700458 return 0;
459}
460
461/*
462 * This function handles the event generated by firmware, rx data
463 * received from firmware, and tx data sent from kernel.
464 */
465static int btmrvl_service_main_thread(void *data)
466{
467 struct btmrvl_thread *thread = data;
468 struct btmrvl_private *priv = thread->priv;
469 struct btmrvl_adapter *adapter = priv->adapter;
470 wait_queue_t wait;
471 struct sk_buff *skb;
472 ulong flags;
473
Bing Zhao132ff4e2009-06-02 14:29:35 -0700474 init_waitqueue_entry(&wait, current);
475
476 current->flags |= PF_NOFREEZE;
477
478 for (;;) {
479 add_wait_queue(&thread->wait_q, &wait);
480
481 set_current_state(TASK_INTERRUPTIBLE);
482
483 if (adapter->wakeup_tries ||
Marcel Holtmann4271e082009-06-09 16:00:22 +0200484 ((!adapter->int_count) &&
485 (!priv->btmrvl_dev.tx_dnld_rdy ||
486 skb_queue_empty(&adapter->tx_queue)))) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700487 BT_DBG("main_thread is sleeping...");
488 schedule();
489 }
490
491 set_current_state(TASK_RUNNING);
492
493 remove_wait_queue(&thread->wait_q, &wait);
494
495 BT_DBG("main_thread woke up");
496
497 if (kthread_should_stop()) {
498 BT_DBG("main_thread: break from main thread");
499 break;
500 }
501
502 spin_lock_irqsave(&priv->driver_lock, flags);
503 if (adapter->int_count) {
504 adapter->int_count = 0;
Marcel Holtmann4271e082009-06-09 16:00:22 +0200505 } else if (adapter->ps_state == PS_SLEEP &&
506 !skb_queue_empty(&adapter->tx_queue)) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700507 spin_unlock_irqrestore(&priv->driver_lock, flags);
508 adapter->wakeup_tries++;
509 priv->hw_wakeup_firmware(priv);
510 continue;
511 }
512 spin_unlock_irqrestore(&priv->driver_lock, flags);
513
514 if (adapter->ps_state == PS_SLEEP)
515 continue;
516
517 if (!priv->btmrvl_dev.tx_dnld_rdy)
518 continue;
519
520 skb = skb_dequeue(&adapter->tx_queue);
521 if (skb) {
522 if (btmrvl_tx_pkt(priv, skb))
523 priv->btmrvl_dev.hcidev->stat.err_tx++;
524 else
Marcel Holtmann4271e082009-06-09 16:00:22 +0200525 priv->btmrvl_dev.hcidev->stat.byte_tx += skb->len;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700526
527 kfree_skb(skb);
528 }
529 }
530
Bing Zhao132ff4e2009-06-02 14:29:35 -0700531 return 0;
532}
533
Bing Zhao64061602010-03-03 14:37:36 -0800534int btmrvl_register_hdev(struct btmrvl_private *priv)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700535{
536 struct hci_dev *hdev = NULL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700537 int ret;
538
Bing Zhao64061602010-03-03 14:37:36 -0800539 hdev = hci_alloc_dev();
540 if (!hdev) {
541 BT_ERR("Can not allocate HCI device");
542 goto err_hdev;
543 }
544
545 priv->btmrvl_dev.hcidev = hdev;
546 hdev->driver_data = priv;
547
548 hdev->bus = HCI_SDIO;
549 hdev->open = btmrvl_open;
550 hdev->close = btmrvl_close;
551 hdev->flush = btmrvl_flush;
552 hdev->send = btmrvl_send_frame;
553 hdev->destruct = btmrvl_destruct;
554 hdev->ioctl = btmrvl_ioctl;
555 hdev->owner = THIS_MODULE;
556
557 btmrvl_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ);
558
Bing Zhaof120c6b2010-03-03 14:37:37 -0800559 hdev->dev_type = priv->btmrvl_dev.dev_type;
560
Bing Zhao64061602010-03-03 14:37:36 -0800561 ret = hci_register_dev(hdev);
562 if (ret < 0) {
563 BT_ERR("Can not register HCI device");
564 goto err_hci_register_dev;
565 }
566
567#ifdef CONFIG_DEBUG_FS
568 btmrvl_debugfs_init(hdev);
569#endif
570
571 return 0;
572
573err_hci_register_dev:
574 hci_free_dev(hdev);
575
576err_hdev:
577 /* Stop the thread servicing the interrupts */
578 kthread_stop(priv->main_thread.task);
579
580 btmrvl_free_adapter(priv);
581 kfree(priv);
582
583 return -ENOMEM;
584}
585EXPORT_SYMBOL_GPL(btmrvl_register_hdev);
586
587struct btmrvl_private *btmrvl_add_card(void *card)
588{
589 struct btmrvl_private *priv;
590
Bing Zhao132ff4e2009-06-02 14:29:35 -0700591 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
592 if (!priv) {
593 BT_ERR("Can not allocate priv");
594 goto err_priv;
595 }
596
597 priv->adapter = kzalloc(sizeof(*priv->adapter), GFP_KERNEL);
598 if (!priv->adapter) {
599 BT_ERR("Allocate buffer for btmrvl_adapter failed!");
600 goto err_adapter;
601 }
602
603 btmrvl_init_adapter(priv);
604
Bing Zhao132ff4e2009-06-02 14:29:35 -0700605 BT_DBG("Starting kthread...");
606 priv->main_thread.priv = priv;
607 spin_lock_init(&priv->driver_lock);
608
609 init_waitqueue_head(&priv->main_thread.wait_q);
610 priv->main_thread.task = kthread_run(btmrvl_service_main_thread,
Marcel Holtmann4271e082009-06-09 16:00:22 +0200611 &priv->main_thread, "btmrvl_main_service");
Bing Zhao132ff4e2009-06-02 14:29:35 -0700612
Bing Zhao132ff4e2009-06-02 14:29:35 -0700613 priv->btmrvl_dev.card = card;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700614 priv->btmrvl_dev.tx_dnld_rdy = true;
615
Bing Zhao132ff4e2009-06-02 14:29:35 -0700616 return priv;
617
Bing Zhao132ff4e2009-06-02 14:29:35 -0700618err_adapter:
619 kfree(priv);
620
621err_priv:
Bing Zhao132ff4e2009-06-02 14:29:35 -0700622 return NULL;
623}
624EXPORT_SYMBOL_GPL(btmrvl_add_card);
625
626int btmrvl_remove_card(struct btmrvl_private *priv)
627{
628 struct hci_dev *hdev;
629
Bing Zhao132ff4e2009-06-02 14:29:35 -0700630 hdev = priv->btmrvl_dev.hcidev;
631
632 wake_up_interruptible(&priv->adapter->cmd_wait_q);
633
634 kthread_stop(priv->main_thread.task);
635
Bing Zhaofb784f02009-06-02 14:29:37 -0700636#ifdef CONFIG_DEBUG_FS
637 btmrvl_debugfs_remove(hdev);
638#endif
639
Bing Zhao132ff4e2009-06-02 14:29:35 -0700640 hci_unregister_dev(hdev);
641
642 hci_free_dev(hdev);
643
644 priv->btmrvl_dev.hcidev = NULL;
645
646 btmrvl_free_adapter(priv);
647
648 kfree(priv);
649
Bing Zhao132ff4e2009-06-02 14:29:35 -0700650 return 0;
651}
652EXPORT_SYMBOL_GPL(btmrvl_remove_card);
653
654MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +0200655MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700656MODULE_VERSION(VERSION);
657MODULE_LICENSE("GPL v2");