blob: 87d0d3d5ae548f2f49c7060268a206a546173e77 [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");
Marcel Holtmann4271e082009-06-09 16:00:22 +0200119 } else if (priv->btmrvl_dev.sendcmdflag &&
120 event->data[1] == MODULE_SHUTDOWN_REQ) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700121 BT_DBG("EVENT:%s", (event->data[2]) ?
122 "Shutdown failed" : "Shutdown succeed");
123 } else {
124 BT_DBG("BT_CMD_MODULE_CFG_REQ resp for APP");
125 ret = -EINVAL;
126 }
127 break;
128
129 case BT_EVENT_POWER_STATE:
130 if (event->data[1] == BT_PS_SLEEP)
131 adapter->ps_state = PS_SLEEP;
132 BT_DBG("EVENT:%s",
133 (adapter->ps_state) ? "PS_SLEEP" : "PS_AWAKE");
134 break;
135
136 default:
137 BT_DBG("Unknown Event=%d", event->data[0]);
138 ret = -EINVAL;
139 break;
140 }
141
142exit:
143 if (!ret)
144 kfree_skb(skb);
145
Bing Zhao132ff4e2009-06-02 14:29:35 -0700146 return ret;
147}
148EXPORT_SYMBOL_GPL(btmrvl_process_event);
149
150int btmrvl_send_module_cfg_cmd(struct btmrvl_private *priv, int subcmd)
151{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200152 struct sk_buff *skb;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700153 struct btmrvl_cmd *cmd;
Marcel Holtmann93742532009-06-13 07:40:18 +0200154 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700155
156 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
157 if (skb == NULL) {
158 BT_ERR("No free skb");
Marcel Holtmann93742532009-06-13 07:40:18 +0200159 return -ENOMEM;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700160 }
161
Marcel Holtmanne0721f92009-06-13 07:27:19 +0200162 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
Marcel Holtmann91d69732009-06-10 12:18:50 +0200163 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_MODULE_CFG_REQ));
Bing Zhao132ff4e2009-06-02 14:29:35 -0700164 cmd->length = 1;
165 cmd->data[0] = subcmd;
166
167 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
168
Marcel Holtmann4271e082009-06-09 16:00:22 +0200169 skb->dev = (void *) priv->btmrvl_dev.hcidev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700170 skb_queue_head(&priv->adapter->tx_queue, skb);
171
172 priv->btmrvl_dev.sendcmdflag = true;
173
174 priv->adapter->cmd_complete = false;
175
176 BT_DBG("Queue module cfg Command");
177
178 wake_up_interruptible(&priv->main_thread.wait_q);
179
Marcel Holtmann4271e082009-06-09 16:00:22 +0200180 if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
181 priv->adapter->cmd_complete,
182 msecs_to_jiffies(WAIT_UNTIL_CMD_RESP))) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700183 ret = -ETIMEDOUT;
184 BT_ERR("module_cfg_cmd(%x): timeout: %d",
Marcel Holtmann4271e082009-06-09 16:00:22 +0200185 subcmd, priv->btmrvl_dev.sendcmdflag);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700186 }
187
188 BT_DBG("module cfg Command done");
189
Bing Zhao132ff4e2009-06-02 14:29:35 -0700190 return ret;
191}
192EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd);
193
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700194int btmrvl_enable_ps(struct btmrvl_private *priv)
195{
196 struct sk_buff *skb;
197 struct btmrvl_cmd *cmd;
198
199 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
200 if (skb == NULL) {
201 BT_ERR("No free skb");
202 return -ENOMEM;
203 }
204
205 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
206 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF,
207 BT_CMD_AUTO_SLEEP_MODE));
208 cmd->length = 1;
209
210 if (priv->btmrvl_dev.psmode)
211 cmd->data[0] = BT_PS_ENABLE;
212 else
213 cmd->data[0] = BT_PS_DISABLE;
214
215 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
216
217 skb->dev = (void *) priv->btmrvl_dev.hcidev;
218 skb_queue_head(&priv->adapter->tx_queue, skb);
219
220 BT_DBG("Queue PSMODE Command:%d", cmd->data[0]);
221
222 return 0;
223}
224EXPORT_SYMBOL_GPL(btmrvl_enable_ps);
225
Bing Zhao132ff4e2009-06-02 14:29:35 -0700226static int btmrvl_enable_hs(struct btmrvl_private *priv)
227{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200228 struct sk_buff *skb;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700229 struct btmrvl_cmd *cmd;
Marcel Holtmann93742532009-06-13 07:40:18 +0200230 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700231
232 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
233 if (skb == NULL) {
234 BT_ERR("No free skb");
Marcel Holtmann93742532009-06-13 07:40:18 +0200235 return -ENOMEM;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700236 }
237
Marcel Holtmanne0721f92009-06-13 07:27:19 +0200238 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
Marcel Holtmann91d69732009-06-10 12:18:50 +0200239 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_HOST_SLEEP_ENABLE));
Bing Zhao132ff4e2009-06-02 14:29:35 -0700240 cmd->length = 0;
241
242 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
243
Marcel Holtmann4271e082009-06-09 16:00:22 +0200244 skb->dev = (void *) priv->btmrvl_dev.hcidev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700245 skb_queue_head(&priv->adapter->tx_queue, skb);
246
247 BT_DBG("Queue hs enable Command");
248
249 wake_up_interruptible(&priv->main_thread.wait_q);
250
Marcel Holtmann4271e082009-06-09 16:00:22 +0200251 if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
Bing Zhao132ff4e2009-06-02 14:29:35 -0700252 priv->adapter->hs_state,
253 msecs_to_jiffies(WAIT_UNTIL_HS_STATE_CHANGED))) {
254 ret = -ETIMEDOUT;
Marcel Holtmann4271e082009-06-09 16:00:22 +0200255 BT_ERR("timeout: %d, %d,%d", priv->adapter->hs_state,
256 priv->adapter->ps_state,
257 priv->adapter->wakeup_tries);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700258 }
259
Bing Zhao132ff4e2009-06-02 14:29:35 -0700260 return ret;
261}
262
263int btmrvl_prepare_command(struct btmrvl_private *priv)
264{
265 struct sk_buff *skb = NULL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700266 struct btmrvl_cmd *cmd;
Marcel Holtmann93742532009-06-13 07:40:18 +0200267 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700268
269 if (priv->btmrvl_dev.hscfgcmd) {
270 priv->btmrvl_dev.hscfgcmd = 0;
271
272 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
273 if (skb == NULL) {
274 BT_ERR("No free skb");
Marcel Holtmann93742532009-06-13 07:40:18 +0200275 return -ENOMEM;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700276 }
277
Marcel Holtmanne0721f92009-06-13 07:27:19 +0200278 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
Marcel Holtmann91d69732009-06-10 12:18:50 +0200279 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_HOST_SLEEP_CONFIG));
Bing Zhao132ff4e2009-06-02 14:29:35 -0700280 cmd->length = 2;
281 cmd->data[0] = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8;
282 cmd->data[1] = (u8) (priv->btmrvl_dev.gpio_gap & 0x00ff);
283
284 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
285
Marcel Holtmann4271e082009-06-09 16:00:22 +0200286 skb->dev = (void *) priv->btmrvl_dev.hcidev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700287 skb_queue_head(&priv->adapter->tx_queue, skb);
288
289 BT_DBG("Queue HSCFG Command, gpio=0x%x, gap=0x%x",
Marcel Holtmann4271e082009-06-09 16:00:22 +0200290 cmd->data[0], cmd->data[1]);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700291 }
292
293 if (priv->btmrvl_dev.pscmd) {
294 priv->btmrvl_dev.pscmd = 0;
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700295 btmrvl_enable_ps(priv);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700296 }
297
298 if (priv->btmrvl_dev.hscmd) {
299 priv->btmrvl_dev.hscmd = 0;
300
301 if (priv->btmrvl_dev.hsmode) {
302 ret = btmrvl_enable_hs(priv);
303 } else {
304 ret = priv->hw_wakeup_firmware(priv);
305 priv->adapter->hs_state = HS_DEACTIVATED;
306 }
307 }
308
Bing Zhao132ff4e2009-06-02 14:29:35 -0700309 return ret;
310}
311
312static int btmrvl_tx_pkt(struct btmrvl_private *priv, struct sk_buff *skb)
313{
Marcel Holtmann93742532009-06-13 07:40:18 +0200314 int ret = 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700315
Marcel Holtmann93742532009-06-13 07:40:18 +0200316 if (!skb || !skb->data)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700317 return -EINVAL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700318
319 if (!skb->len || ((skb->len + BTM_HEADER_LEN) > BTM_UPLD_SIZE)) {
320 BT_ERR("Tx Error: Bad skb length %d : %d",
Marcel Holtmann4271e082009-06-09 16:00:22 +0200321 skb->len, BTM_UPLD_SIZE);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700322 return -EINVAL;
323 }
324
325 if (skb_headroom(skb) < BTM_HEADER_LEN) {
326 struct sk_buff *tmp = skb;
327
328 skb = skb_realloc_headroom(skb, BTM_HEADER_LEN);
329 if (!skb) {
330 BT_ERR("Tx Error: realloc_headroom failed %d",
331 BTM_HEADER_LEN);
332 skb = tmp;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700333 return -EINVAL;
334 }
335
336 kfree_skb(tmp);
337 }
338
339 skb_push(skb, BTM_HEADER_LEN);
340
341 /* header type: byte[3]
342 * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor
343 * header length: byte[2][1][0]
344 */
345
346 skb->data[0] = (skb->len & 0x0000ff);
347 skb->data[1] = (skb->len & 0x00ff00) >> 8;
348 skb->data[2] = (skb->len & 0xff0000) >> 16;
349 skb->data[3] = bt_cb(skb)->pkt_type;
350
351 if (priv->hw_host_to_card)
352 ret = priv->hw_host_to_card(priv, skb->data, skb->len);
353
Bing Zhao132ff4e2009-06-02 14:29:35 -0700354 return ret;
355}
356
357static void btmrvl_init_adapter(struct btmrvl_private *priv)
358{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700359 skb_queue_head_init(&priv->adapter->tx_queue);
360
361 priv->adapter->ps_state = PS_AWAKE;
362
363 init_waitqueue_head(&priv->adapter->cmd_wait_q);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700364}
365
366static void btmrvl_free_adapter(struct btmrvl_private *priv)
367{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700368 skb_queue_purge(&priv->adapter->tx_queue);
369
370 kfree(priv->adapter);
371
372 priv->adapter = NULL;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700373}
374
Marcel Holtmann4271e082009-06-09 16:00:22 +0200375static int btmrvl_ioctl(struct hci_dev *hdev,
376 unsigned int cmd, unsigned long arg)
Bing Zhao132ff4e2009-06-02 14:29:35 -0700377{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700378 return -ENOIOCTLCMD;
379}
380
381static void btmrvl_destruct(struct hci_dev *hdev)
382{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700383}
384
385static int btmrvl_send_frame(struct sk_buff *skb)
386{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200387 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700388 struct btmrvl_private *priv = NULL;
389
Marcel Holtmann93742532009-06-13 07:40:18 +0200390 BT_DBG("type=%d, len=%d", skb->pkt_type, skb->len);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700391
392 if (!hdev || !hdev->driver_data) {
393 BT_ERR("Frame for unknown HCI device");
Bing Zhao132ff4e2009-06-02 14:29:35 -0700394 return -ENODEV;
395 }
396
Marcel Holtmann4271e082009-06-09 16:00:22 +0200397 priv = (struct btmrvl_private *) hdev->driver_data;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700398 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
399 BT_ERR("Failed testing HCI_RUNING, flags=%lx", hdev->flags);
400 print_hex_dump_bytes("data: ", DUMP_PREFIX_OFFSET,
Marcel Holtmann4271e082009-06-09 16:00:22 +0200401 skb->data, skb->len);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700402 return -EBUSY;
403 }
404
405 switch (bt_cb(skb)->pkt_type) {
406 case HCI_COMMAND_PKT:
407 hdev->stat.cmd_tx++;
408 break;
409
410 case HCI_ACLDATA_PKT:
411 hdev->stat.acl_tx++;
412 break;
413
414 case HCI_SCODATA_PKT:
415 hdev->stat.sco_tx++;
416 break;
417 }
418
419 skb_queue_tail(&priv->adapter->tx_queue, skb);
420
421 wake_up_interruptible(&priv->main_thread.wait_q);
422
Bing Zhao132ff4e2009-06-02 14:29:35 -0700423 return 0;
424}
425
426static int btmrvl_flush(struct hci_dev *hdev)
427{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200428 struct btmrvl_private *priv = hdev->driver_data;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700429
Bing Zhao132ff4e2009-06-02 14:29:35 -0700430 skb_queue_purge(&priv->adapter->tx_queue);
431
Bing Zhao132ff4e2009-06-02 14:29:35 -0700432 return 0;
433}
434
435static int btmrvl_close(struct hci_dev *hdev)
436{
Marcel Holtmann4271e082009-06-09 16:00:22 +0200437 struct btmrvl_private *priv = hdev->driver_data;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700438
Marcel Holtmann93742532009-06-13 07:40:18 +0200439 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
Bing Zhao132ff4e2009-06-02 14:29:35 -0700440 return 0;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700441
442 skb_queue_purge(&priv->adapter->tx_queue);
443
Bing Zhao132ff4e2009-06-02 14:29:35 -0700444 return 0;
445}
446
447static int btmrvl_open(struct hci_dev *hdev)
448{
Bing Zhao132ff4e2009-06-02 14:29:35 -0700449 set_bit(HCI_RUNNING, &hdev->flags);
450
Bing Zhao132ff4e2009-06-02 14:29:35 -0700451 return 0;
452}
453
454/*
455 * This function handles the event generated by firmware, rx data
456 * received from firmware, and tx data sent from kernel.
457 */
458static int btmrvl_service_main_thread(void *data)
459{
460 struct btmrvl_thread *thread = data;
461 struct btmrvl_private *priv = thread->priv;
462 struct btmrvl_adapter *adapter = priv->adapter;
463 wait_queue_t wait;
464 struct sk_buff *skb;
465 ulong flags;
466
Bing Zhao132ff4e2009-06-02 14:29:35 -0700467 init_waitqueue_entry(&wait, current);
468
469 current->flags |= PF_NOFREEZE;
470
471 for (;;) {
472 add_wait_queue(&thread->wait_q, &wait);
473
474 set_current_state(TASK_INTERRUPTIBLE);
475
476 if (adapter->wakeup_tries ||
Marcel Holtmann4271e082009-06-09 16:00:22 +0200477 ((!adapter->int_count) &&
478 (!priv->btmrvl_dev.tx_dnld_rdy ||
479 skb_queue_empty(&adapter->tx_queue)))) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700480 BT_DBG("main_thread is sleeping...");
481 schedule();
482 }
483
484 set_current_state(TASK_RUNNING);
485
486 remove_wait_queue(&thread->wait_q, &wait);
487
488 BT_DBG("main_thread woke up");
489
490 if (kthread_should_stop()) {
491 BT_DBG("main_thread: break from main thread");
492 break;
493 }
494
495 spin_lock_irqsave(&priv->driver_lock, flags);
496 if (adapter->int_count) {
497 adapter->int_count = 0;
Marcel Holtmann4271e082009-06-09 16:00:22 +0200498 } else if (adapter->ps_state == PS_SLEEP &&
499 !skb_queue_empty(&adapter->tx_queue)) {
Bing Zhao132ff4e2009-06-02 14:29:35 -0700500 spin_unlock_irqrestore(&priv->driver_lock, flags);
501 adapter->wakeup_tries++;
502 priv->hw_wakeup_firmware(priv);
503 continue;
504 }
505 spin_unlock_irqrestore(&priv->driver_lock, flags);
506
507 if (adapter->ps_state == PS_SLEEP)
508 continue;
509
510 if (!priv->btmrvl_dev.tx_dnld_rdy)
511 continue;
512
513 skb = skb_dequeue(&adapter->tx_queue);
514 if (skb) {
515 if (btmrvl_tx_pkt(priv, skb))
516 priv->btmrvl_dev.hcidev->stat.err_tx++;
517 else
Marcel Holtmann4271e082009-06-09 16:00:22 +0200518 priv->btmrvl_dev.hcidev->stat.byte_tx += skb->len;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700519
520 kfree_skb(skb);
521 }
522 }
523
Bing Zhao132ff4e2009-06-02 14:29:35 -0700524 return 0;
525}
526
527struct btmrvl_private *btmrvl_add_card(void *card)
528{
529 struct hci_dev *hdev = NULL;
Marcel Holtmann4271e082009-06-09 16:00:22 +0200530 struct btmrvl_private *priv;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700531 int ret;
532
Bing Zhao132ff4e2009-06-02 14:29:35 -0700533 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
534 if (!priv) {
535 BT_ERR("Can not allocate priv");
536 goto err_priv;
537 }
538
539 priv->adapter = kzalloc(sizeof(*priv->adapter), GFP_KERNEL);
540 if (!priv->adapter) {
541 BT_ERR("Allocate buffer for btmrvl_adapter failed!");
542 goto err_adapter;
543 }
544
545 btmrvl_init_adapter(priv);
546
547 hdev = hci_alloc_dev();
548 if (!hdev) {
549 BT_ERR("Can not allocate HCI device");
550 goto err_hdev;
551 }
552
553 BT_DBG("Starting kthread...");
554 priv->main_thread.priv = priv;
555 spin_lock_init(&priv->driver_lock);
556
557 init_waitqueue_head(&priv->main_thread.wait_q);
558 priv->main_thread.task = kthread_run(btmrvl_service_main_thread,
Marcel Holtmann4271e082009-06-09 16:00:22 +0200559 &priv->main_thread, "btmrvl_main_service");
Bing Zhao132ff4e2009-06-02 14:29:35 -0700560
561 priv->btmrvl_dev.hcidev = hdev;
562 priv->btmrvl_dev.card = card;
563
564 hdev->driver_data = priv;
565
566 priv->btmrvl_dev.tx_dnld_rdy = true;
567
Marcel Holtmannc13854ce2010-02-08 15:27:07 +0100568 hdev->bus = HCI_SDIO;
Bing Zhao132ff4e2009-06-02 14:29:35 -0700569 hdev->open = btmrvl_open;
570 hdev->close = btmrvl_close;
571 hdev->flush = btmrvl_flush;
572 hdev->send = btmrvl_send_frame;
573 hdev->destruct = btmrvl_destruct;
574 hdev->ioctl = btmrvl_ioctl;
575 hdev->owner = THIS_MODULE;
576
577 ret = hci_register_dev(hdev);
578 if (ret < 0) {
579 BT_ERR("Can not register HCI device");
580 goto err_hci_register_dev;
581 }
582
Bing Zhaofb784f02009-06-02 14:29:37 -0700583#ifdef CONFIG_DEBUG_FS
584 btmrvl_debugfs_init(hdev);
585#endif
586
Bing Zhao132ff4e2009-06-02 14:29:35 -0700587 return priv;
588
589err_hci_register_dev:
590 /* Stop the thread servicing the interrupts */
591 kthread_stop(priv->main_thread.task);
592
593 hci_free_dev(hdev);
594
595err_hdev:
596 btmrvl_free_adapter(priv);
597
598err_adapter:
599 kfree(priv);
600
601err_priv:
Bing Zhao132ff4e2009-06-02 14:29:35 -0700602 return NULL;
603}
604EXPORT_SYMBOL_GPL(btmrvl_add_card);
605
606int btmrvl_remove_card(struct btmrvl_private *priv)
607{
608 struct hci_dev *hdev;
609
Bing Zhao132ff4e2009-06-02 14:29:35 -0700610 hdev = priv->btmrvl_dev.hcidev;
611
612 wake_up_interruptible(&priv->adapter->cmd_wait_q);
613
614 kthread_stop(priv->main_thread.task);
615
Bing Zhaofb784f02009-06-02 14:29:37 -0700616#ifdef CONFIG_DEBUG_FS
617 btmrvl_debugfs_remove(hdev);
618#endif
619
Bing Zhao132ff4e2009-06-02 14:29:35 -0700620 hci_unregister_dev(hdev);
621
622 hci_free_dev(hdev);
623
624 priv->btmrvl_dev.hcidev = NULL;
625
626 btmrvl_free_adapter(priv);
627
628 kfree(priv);
629
Bing Zhao132ff4e2009-06-02 14:29:35 -0700630 return 0;
631}
632EXPORT_SYMBOL_GPL(btmrvl_remove_card);
633
634MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +0200635MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION);
Bing Zhao132ff4e2009-06-02 14:29:35 -0700636MODULE_VERSION(VERSION);
637MODULE_LICENSE("GPL v2");