Commit | Line | Data |
---|---|---|
132ff4e5 BZ |
1 | /** |
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 | ||
a59b968e PG |
21 | #include <linux/module.h> |
22 | ||
132ff4e5 BZ |
23 | #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 | */ | |
35 | void btmrvl_interrupt(struct btmrvl_private *priv) | |
36 | { | |
132ff4e5 BZ |
37 | 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); | |
132ff4e5 BZ |
44 | } |
45 | EXPORT_SYMBOL_GPL(btmrvl_interrupt); | |
46 | ||
ea1e76a3 | 47 | bool btmrvl_check_evtpkt(struct btmrvl_private *priv, struct sk_buff *skb) |
132ff4e5 | 48 | { |
4271e08d | 49 | struct hci_event_hdr *hdr = (void *) skb->data; |
132ff4e5 | 50 | |
132ff4e5 | 51 | if (hdr->evt == HCI_EV_CMD_COMPLETE) { |
2c2453f3 AE |
52 | struct hci_ev_cmd_complete *ec; |
53 | u16 opcode, ocf, ogf; | |
54 | ||
4271e08d | 55 | ec = (void *) (skb->data + HCI_EVENT_HDR_SIZE); |
132ff4e5 BZ |
56 | opcode = __le16_to_cpu(ec->opcode); |
57 | ocf = hci_opcode_ocf(opcode); | |
ea1e76a3 AE |
58 | ogf = hci_opcode_ogf(opcode); |
59 | ||
c037874c | 60 | if (priv->btmrvl_dev.sendcmdflag) { |
132ff4e5 BZ |
61 | priv->btmrvl_dev.sendcmdflag = false; |
62 | priv->adapter->cmd_complete = true; | |
63 | wake_up_interruptible(&priv->adapter->cmd_wait_q); | |
64 | } | |
ea1e76a3 AE |
65 | |
66 | if (ogf == OGF) { | |
2c2453f3 AE |
67 | BT_DBG("vendor event skipped: ogf 0x%4.4x ocf 0x%4.4x", |
68 | ogf, ocf); | |
ea1e76a3 AE |
69 | kfree_skb(skb); |
70 | return false; | |
71 | } | |
132ff4e5 | 72 | } |
ea1e76a3 AE |
73 | |
74 | return true; | |
132ff4e5 BZ |
75 | } |
76 | EXPORT_SYMBOL_GPL(btmrvl_check_evtpkt); | |
77 | ||
78 | int btmrvl_process_event(struct btmrvl_private *priv, struct sk_buff *skb) | |
79 | { | |
80 | struct btmrvl_adapter *adapter = priv->adapter; | |
132ff4e5 | 81 | struct btmrvl_event *event; |
31f085fe | 82 | int ret = 0; |
132ff4e5 | 83 | |
132ff4e5 BZ |
84 | 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], | |
4271e08d | 108 | event->data[2]); |
132ff4e5 BZ |
109 | 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; | |
132ff4e5 BZ |
118 | BT_DBG("HS ACTIVATED!"); |
119 | } else { | |
120 | BT_DBG("HS Enable failed"); | |
121 | } | |
122 | break; | |
123 | ||
124 | case BT_CMD_MODULE_CFG_REQ: | |
4271e08d MH |
125 | if (priv->btmrvl_dev.sendcmdflag && |
126 | event->data[1] == MODULE_BRINGUP_REQ) { | |
903c8437 BZ |
127 | 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"); | |
f120c6b6 | 131 | |
8f1e1742 DV |
132 | if (event->length > 3 && event->data[3]) |
133 | priv->btmrvl_dev.dev_type = HCI_AMP; | |
f120c6b6 BZ |
134 | else |
135 | priv->btmrvl_dev.dev_type = HCI_BREDR; | |
136 | ||
137 | BT_DBG("dev_type: %d", priv->btmrvl_dev.dev_type); | |
4271e08d MH |
138 | } else if (priv->btmrvl_dev.sendcmdflag && |
139 | event->data[1] == MODULE_SHUTDOWN_REQ) { | |
132ff4e5 BZ |
140 | 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 | ||
161 | exit: | |
162 | if (!ret) | |
163 | kfree_skb(skb); | |
164 | ||
132ff4e5 BZ |
165 | return ret; |
166 | } | |
167 | EXPORT_SYMBOL_GPL(btmrvl_process_event); | |
168 | ||
c037874c AK |
169 | static int btmrvl_send_sync_cmd(struct btmrvl_private *priv, u16 cmd_no, |
170 | const void *param, u8 len) | |
132ff4e5 | 171 | { |
4271e08d | 172 | struct sk_buff *skb; |
7d5b400c | 173 | struct hci_command_hdr *hdr; |
132ff4e5 | 174 | |
7d5b400c | 175 | skb = bt_skb_alloc(HCI_COMMAND_HDR_SIZE + len, GFP_ATOMIC); |
132ff4e5 BZ |
176 | if (skb == NULL) { |
177 | BT_ERR("No free skb"); | |
9374253f | 178 | return -ENOMEM; |
132ff4e5 BZ |
179 | } |
180 | ||
7d5b400c AK |
181 | 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; | |
c037874c AK |
184 | |
185 | if (len) | |
7d5b400c | 186 | memcpy(skb_put(skb, len), param, len); |
132ff4e5 BZ |
187 | |
188 | bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT; | |
189 | ||
4271e08d | 190 | skb->dev = (void *) priv->btmrvl_dev.hcidev; |
132ff4e5 BZ |
191 | skb_queue_head(&priv->adapter->tx_queue, skb); |
192 | ||
193 | priv->btmrvl_dev.sendcmdflag = true; | |
194 | ||
195 | priv->adapter->cmd_complete = false; | |
196 | ||
132ff4e5 BZ |
197 | wake_up_interruptible(&priv->main_thread.wait_q); |
198 | ||
4271e08d MH |
199 | if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q, |
200 | priv->adapter->cmd_complete, | |
c037874c AK |
201 | msecs_to_jiffies(WAIT_UNTIL_CMD_RESP))) |
202 | return -ETIMEDOUT; | |
203 | ||
204 | return 0; | |
205 | } | |
132ff4e5 | 206 | |
c037874c AK |
207 | int 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); | |
132ff4e5 | 214 | |
132ff4e5 BZ |
215 | return ret; |
216 | } | |
217 | EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd); | |
218 | ||
2a8ff611 AK |
219 | int btmrvl_send_hscfg_cmd(struct btmrvl_private *priv) |
220 | { | |
c037874c AK |
221 | int ret; |
222 | u8 param[2]; | |
2a8ff611 | 223 | |
c037874c AK |
224 | param[0] = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8; |
225 | param[1] = (u8) (priv->btmrvl_dev.gpio_gap & 0x00ff); | |
2a8ff611 | 226 | |
c037874c AK |
227 | BT_DBG("Sending HSCFG Command, gpio=0x%x, gap=0x%x", |
228 | param[0], param[1]); | |
2a8ff611 | 229 | |
c037874c AK |
230 | ret = btmrvl_send_sync_cmd(priv, BT_CMD_HOST_SLEEP_CONFIG, param, 2); |
231 | if (ret) | |
232 | BT_ERR("HSCFG command failed\n"); | |
2a8ff611 | 233 | |
c037874c | 234 | return ret; |
2a8ff611 AK |
235 | } |
236 | EXPORT_SYMBOL_GPL(btmrvl_send_hscfg_cmd); | |
237 | ||
32fd6398 AK |
238 | int btmrvl_enable_ps(struct btmrvl_private *priv) |
239 | { | |
c037874c AK |
240 | int ret; |
241 | u8 param; | |
32fd6398 AK |
242 | |
243 | if (priv->btmrvl_dev.psmode) | |
c037874c | 244 | param = BT_PS_ENABLE; |
32fd6398 | 245 | else |
c037874c | 246 | param = BT_PS_DISABLE; |
32fd6398 | 247 | |
c037874c AK |
248 | ret = btmrvl_send_sync_cmd(priv, BT_CMD_AUTO_SLEEP_MODE, ¶m, 1); |
249 | if (ret) | |
250 | BT_ERR("PSMODE command failed\n"); | |
32fd6398 AK |
251 | |
252 | return 0; | |
253 | } | |
254 | EXPORT_SYMBOL_GPL(btmrvl_enable_ps); | |
255 | ||
ba54a16f | 256 | int btmrvl_enable_hs(struct btmrvl_private *priv) |
132ff4e5 | 257 | { |
c037874c | 258 | int ret; |
132ff4e5 | 259 | |
c037874c AK |
260 | 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"); | |
132ff4e5 | 263 | |
132ff4e5 BZ |
264 | return ret; |
265 | } | |
ba54a16f | 266 | EXPORT_SYMBOL_GPL(btmrvl_enable_hs); |
132ff4e5 BZ |
267 | |
268 | int btmrvl_prepare_command(struct btmrvl_private *priv) | |
269 | { | |
9374253f | 270 | int ret = 0; |
132ff4e5 BZ |
271 | |
272 | if (priv->btmrvl_dev.hscfgcmd) { | |
273 | priv->btmrvl_dev.hscfgcmd = 0; | |
2a8ff611 | 274 | btmrvl_send_hscfg_cmd(priv); |
132ff4e5 BZ |
275 | } |
276 | ||
277 | if (priv->btmrvl_dev.pscmd) { | |
278 | priv->btmrvl_dev.pscmd = 0; | |
32fd6398 | 279 | btmrvl_enable_ps(priv); |
132ff4e5 BZ |
280 | } |
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 | ||
132ff4e5 BZ |
293 | return ret; |
294 | } | |
295 | ||
296 | static int btmrvl_tx_pkt(struct btmrvl_private *priv, struct sk_buff *skb) | |
297 | { | |
9374253f | 298 | int ret = 0; |
132ff4e5 | 299 | |
9374253f | 300 | if (!skb || !skb->data) |
132ff4e5 | 301 | return -EINVAL; |
132ff4e5 BZ |
302 | |
303 | if (!skb->len || ((skb->len + BTM_HEADER_LEN) > BTM_UPLD_SIZE)) { | |
304 | BT_ERR("Tx Error: Bad skb length %d : %d", | |
4271e08d | 305 | skb->len, BTM_UPLD_SIZE); |
132ff4e5 BZ |
306 | 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; | |
132ff4e5 BZ |
317 | 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 | ||
132ff4e5 BZ |
338 | return ret; |
339 | } | |
340 | ||
341 | static void btmrvl_init_adapter(struct btmrvl_private *priv) | |
342 | { | |
132ff4e5 BZ |
343 | 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); | |
132ff4e5 BZ |
348 | } |
349 | ||
350 | static void btmrvl_free_adapter(struct btmrvl_private *priv) | |
351 | { | |
132ff4e5 BZ |
352 | skb_queue_purge(&priv->adapter->tx_queue); |
353 | ||
354 | kfree(priv->adapter); | |
355 | ||
356 | priv->adapter = NULL; | |
132ff4e5 BZ |
357 | } |
358 | ||
4271e08d MH |
359 | static int btmrvl_ioctl(struct hci_dev *hdev, |
360 | unsigned int cmd, unsigned long arg) | |
132ff4e5 | 361 | { |
132ff4e5 BZ |
362 | return -ENOIOCTLCMD; |
363 | } | |
364 | ||
132ff4e5 BZ |
365 | static int btmrvl_send_frame(struct sk_buff *skb) |
366 | { | |
4271e08d | 367 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
132ff4e5 BZ |
368 | struct btmrvl_private *priv = NULL; |
369 | ||
9374253f | 370 | BT_DBG("type=%d, len=%d", skb->pkt_type, skb->len); |
132ff4e5 | 371 | |
155961e8 | 372 | if (!hdev) { |
132ff4e5 | 373 | BT_ERR("Frame for unknown HCI device"); |
132ff4e5 BZ |
374 | return -ENODEV; |
375 | } | |
376 | ||
155961e8 DH |
377 | priv = hci_get_drvdata(hdev); |
378 | ||
132ff4e5 BZ |
379 | 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, | |
4271e08d | 382 | skb->data, skb->len); |
132ff4e5 BZ |
383 | 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 | ||
132ff4e5 BZ |
404 | return 0; |
405 | } | |
406 | ||
407 | static int btmrvl_flush(struct hci_dev *hdev) | |
408 | { | |
155961e8 | 409 | struct btmrvl_private *priv = hci_get_drvdata(hdev); |
132ff4e5 | 410 | |
132ff4e5 BZ |
411 | skb_queue_purge(&priv->adapter->tx_queue); |
412 | ||
132ff4e5 BZ |
413 | return 0; |
414 | } | |
415 | ||
416 | static int btmrvl_close(struct hci_dev *hdev) | |
417 | { | |
155961e8 | 418 | struct btmrvl_private *priv = hci_get_drvdata(hdev); |
132ff4e5 | 419 | |
9374253f | 420 | if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) |
132ff4e5 | 421 | return 0; |
132ff4e5 BZ |
422 | |
423 | skb_queue_purge(&priv->adapter->tx_queue); | |
424 | ||
132ff4e5 BZ |
425 | return 0; |
426 | } | |
427 | ||
428 | static int btmrvl_open(struct hci_dev *hdev) | |
429 | { | |
132ff4e5 BZ |
430 | set_bit(HCI_RUNNING, &hdev->flags); |
431 | ||
132ff4e5 BZ |
432 | return 0; |
433 | } | |
434 | ||
2cc86890 AK |
435 | /* |
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 | */ | |
442 | static 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 | ||
479 | static 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 | ||
508 | static int | |
509 | btmrvl_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 | ||
527 | static 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 | ||
4b245722 AK |
548 | static 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 | ||
2cc86890 AK |
554 | if (btmrvl_cal_data_config(priv)) |
555 | BT_ERR("Set cal data failed"); | |
556 | ||
4b245722 AK |
557 | 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 | ||
132ff4e5 BZ |
566 | /* |
567 | * This function handles the event generated by firmware, rx data | |
568 | * received from firmware, and tx data sent from kernel. | |
569 | */ | |
570 | static 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 | ||
132ff4e5 BZ |
579 | init_waitqueue_entry(&wait, current); |
580 | ||
132ff4e5 BZ |
581 | for (;;) { |
582 | add_wait_queue(&thread->wait_q, &wait); | |
583 | ||
584 | set_current_state(TASK_INTERRUPTIBLE); | |
ea05fea9 DD |
585 | if (kthread_should_stop()) { |
586 | BT_DBG("main_thread: break from main thread"); | |
587 | break; | |
588 | } | |
132ff4e5 BZ |
589 | |
590 | if (adapter->wakeup_tries || | |
4271e08d MH |
591 | ((!adapter->int_count) && |
592 | (!priv->btmrvl_dev.tx_dnld_rdy || | |
593 | skb_queue_empty(&adapter->tx_queue)))) { | |
132ff4e5 BZ |
594 | 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 | ||
132ff4e5 BZ |
604 | spin_lock_irqsave(&priv->driver_lock, flags); |
605 | if (adapter->int_count) { | |
606 | adapter->int_count = 0; | |
d1d10d78 AK |
607 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
608 | priv->hw_process_int_status(priv); | |
4271e08d MH |
609 | } else if (adapter->ps_state == PS_SLEEP && |
610 | !skb_queue_empty(&adapter->tx_queue)) { | |
132ff4e5 BZ |
611 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
612 | adapter->wakeup_tries++; | |
613 | priv->hw_wakeup_firmware(priv); | |
614 | continue; | |
d1d10d78 AK |
615 | } else { |
616 | spin_unlock_irqrestore(&priv->driver_lock, flags); | |
132ff4e5 | 617 | } |
132ff4e5 BZ |
618 | |
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 | |
4271e08d | 630 | priv->btmrvl_dev.hcidev->stat.byte_tx += skb->len; |
132ff4e5 BZ |
631 | |
632 | kfree_skb(skb); | |
633 | } | |
634 | } | |
635 | ||
132ff4e5 BZ |
636 | return 0; |
637 | } | |
638 | ||
64061607 | 639 | int btmrvl_register_hdev(struct btmrvl_private *priv) |
132ff4e5 BZ |
640 | { |
641 | struct hci_dev *hdev = NULL; | |
132ff4e5 BZ |
642 | int ret; |
643 | ||
132ff4e5 BZ |
644 | hdev = hci_alloc_dev(); |
645 | if (!hdev) { | |
646 | BT_ERR("Can not allocate HCI device"); | |
647 | goto err_hdev; | |
648 | } | |
649 | ||
132ff4e5 | 650 | priv->btmrvl_dev.hcidev = hdev; |
155961e8 | 651 | hci_set_drvdata(hdev, priv); |
132ff4e5 | 652 | |
c13854ce | 653 | hdev->bus = HCI_SDIO; |
132ff4e5 BZ |
654 | hdev->open = btmrvl_open; |
655 | hdev->close = btmrvl_close; | |
656 | hdev->flush = btmrvl_flush; | |
657 | hdev->send = btmrvl_send_frame; | |
132ff4e5 | 658 | hdev->ioctl = btmrvl_ioctl; |
4b245722 | 659 | hdev->setup = btmrvl_setup; |
64061607 | 660 | |
f120c6b6 BZ |
661 | hdev->dev_type = priv->btmrvl_dev.dev_type; |
662 | ||
132ff4e5 BZ |
663 | 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 | ||
fb784f05 BZ |
669 | #ifdef CONFIG_DEBUG_FS |
670 | btmrvl_debugfs_init(hdev); | |
671 | #endif | |
672 | ||
64061607 | 673 | return 0; |
132ff4e5 BZ |
674 | |
675 | err_hci_register_dev: | |
132ff4e5 BZ |
676 | hci_free_dev(hdev); |
677 | ||
678 | err_hdev: | |
64061607 BZ |
679 | /* Stop the thread servicing the interrupts */ |
680 | kthread_stop(priv->main_thread.task); | |
681 | ||
132ff4e5 | 682 | btmrvl_free_adapter(priv); |
64061607 BZ |
683 | kfree(priv); |
684 | ||
685 | return -ENOMEM; | |
686 | } | |
687 | EXPORT_SYMBOL_GPL(btmrvl_register_hdev); | |
688 | ||
689 | struct btmrvl_private *btmrvl_add_card(void *card) | |
690 | { | |
691 | struct btmrvl_private *priv; | |
692 | ||
693 | 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 | ||
707 | 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, | |
713 | &priv->main_thread, "btmrvl_main_service"); | |
714 | ||
715 | priv->btmrvl_dev.card = card; | |
716 | priv->btmrvl_dev.tx_dnld_rdy = true; | |
717 | ||
718 | return priv; | |
132ff4e5 BZ |
719 | |
720 | err_adapter: | |
721 | kfree(priv); | |
722 | ||
723 | err_priv: | |
132ff4e5 BZ |
724 | return NULL; |
725 | } | |
726 | EXPORT_SYMBOL_GPL(btmrvl_add_card); | |
727 | ||
728 | int btmrvl_remove_card(struct btmrvl_private *priv) | |
729 | { | |
730 | struct hci_dev *hdev; | |
731 | ||
132ff4e5 BZ |
732 | hdev = priv->btmrvl_dev.hcidev; |
733 | ||
734 | wake_up_interruptible(&priv->adapter->cmd_wait_q); | |
735 | ||
736 | kthread_stop(priv->main_thread.task); | |
737 | ||
fb784f05 BZ |
738 | #ifdef CONFIG_DEBUG_FS |
739 | btmrvl_debugfs_remove(hdev); | |
740 | #endif | |
741 | ||
132ff4e5 BZ |
742 | 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 | ||
132ff4e5 BZ |
752 | return 0; |
753 | } | |
754 | EXPORT_SYMBOL_GPL(btmrvl_remove_card); | |
755 | ||
756 | MODULE_AUTHOR("Marvell International Ltd."); | |
9666fb35 | 757 | MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION); |
132ff4e5 BZ |
758 | MODULE_VERSION(VERSION); |
759 | MODULE_LICENSE("GPL v2"); |