1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/module.h>
22
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
32
33
34
35void btmrvl_interrupt(struct btmrvl_private *priv)
36{
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);
44}
45EXPORT_SYMBOL_GPL(btmrvl_interrupt);
46
47bool btmrvl_check_evtpkt(struct btmrvl_private *priv, struct sk_buff *skb)
48{
49 struct hci_event_hdr *hdr = (void *) skb->data;
50
51 if (hdr->evt == HCI_EV_CMD_COMPLETE) {
52 struct hci_ev_cmd_complete *ec;
53 u16 opcode, ocf, ogf;
54
55 ec = (void *) (skb->data + HCI_EVENT_HDR_SIZE);
56 opcode = __le16_to_cpu(ec->opcode);
57 ocf = hci_opcode_ocf(opcode);
58 ogf = hci_opcode_ogf(opcode);
59
60 if (priv->btmrvl_dev.sendcmdflag) {
61 priv->btmrvl_dev.sendcmdflag = false;
62 priv->adapter->cmd_complete = true;
63 wake_up_interruptible(&priv->adapter->cmd_wait_q);
64 }
65
66 if (ogf == OGF) {
67 BT_DBG("vendor event skipped: ogf 0x%4.4x ocf 0x%4.4x",
68 ogf, ocf);
69 kfree_skb(skb);
70 return false;
71 }
72 }
73
74 return true;
75}
76EXPORT_SYMBOL_GPL(btmrvl_check_evtpkt);
77
78int btmrvl_process_event(struct btmrvl_private *priv, struct sk_buff *skb)
79{
80 struct btmrvl_adapter *adapter = priv->adapter;
81 struct btmrvl_event *event;
82 int ret = 0;
83
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],
108 event->data[2]);
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;
118 BT_DBG("HS ACTIVATED!");
119 } else {
120 BT_DBG("HS Enable failed");
121 }
122 break;
123
124 case BT_CMD_MODULE_CFG_REQ:
125 if (priv->btmrvl_dev.sendcmdflag &&
126 event->data[1] == MODULE_BRINGUP_REQ) {
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");
131
132 if (event->length > 3 && event->data[3])
133 priv->btmrvl_dev.dev_type = HCI_AMP;
134 else
135 priv->btmrvl_dev.dev_type = HCI_BREDR;
136
137 BT_DBG("dev_type: %d", priv->btmrvl_dev.dev_type);
138 } else if (priv->btmrvl_dev.sendcmdflag &&
139 event->data[1] == MODULE_SHUTDOWN_REQ) {
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
161exit:
162 if (!ret)
163 kfree_skb(skb);
164
165 return ret;
166}
167EXPORT_SYMBOL_GPL(btmrvl_process_event);
168
169static int btmrvl_send_sync_cmd(struct btmrvl_private *priv, u16 cmd_no,
170 const void *param, u8 len)
171{
172 struct sk_buff *skb;
173 struct hci_command_hdr *hdr;
174
175 skb = bt_skb_alloc(HCI_COMMAND_HDR_SIZE + len, GFP_ATOMIC);
176 if (skb == NULL) {
177 BT_ERR("No free skb");
178 return -ENOMEM;
179 }
180
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;
184
185 if (len)
186 memcpy(skb_put(skb, len), param, len);
187
188 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
189
190 skb_queue_head(&priv->adapter->tx_queue, skb);
191
192 priv->btmrvl_dev.sendcmdflag = true;
193
194 priv->adapter->cmd_complete = false;
195
196 wake_up_interruptible(&priv->main_thread.wait_q);
197
198 if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
199 priv->adapter->cmd_complete,
200 msecs_to_jiffies(WAIT_UNTIL_CMD_RESP)))
201 return -ETIMEDOUT;
202
203 return 0;
204}
205
206int btmrvl_send_module_cfg_cmd(struct btmrvl_private *priv, int subcmd)
207{
208 int ret;
209
210 ret = btmrvl_send_sync_cmd(priv, BT_CMD_MODULE_CFG_REQ, &subcmd, 1);
211 if (ret)
212 BT_ERR("module_cfg_cmd(%x) failed\n", subcmd);
213
214 return ret;
215}
216EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd);
217
218int btmrvl_send_hscfg_cmd(struct btmrvl_private *priv)
219{
220 int ret;
221 u8 param[2];
222
223 param[0] = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8;
224 param[1] = (u8) (priv->btmrvl_dev.gpio_gap & 0x00ff);
225
226 BT_DBG("Sending HSCFG Command, gpio=0x%x, gap=0x%x",
227 param[0], param[1]);
228
229 ret = btmrvl_send_sync_cmd(priv, BT_CMD_HOST_SLEEP_CONFIG, param, 2);
230 if (ret)
231 BT_ERR("HSCFG command failed\n");
232
233 return ret;
234}
235EXPORT_SYMBOL_GPL(btmrvl_send_hscfg_cmd);
236
237int btmrvl_enable_ps(struct btmrvl_private *priv)
238{
239 int ret;
240 u8 param;
241
242 if (priv->btmrvl_dev.psmode)
243 param = BT_PS_ENABLE;
244 else
245 param = BT_PS_DISABLE;
246
247 ret = btmrvl_send_sync_cmd(priv, BT_CMD_AUTO_SLEEP_MODE, ¶m, 1);
248 if (ret)
249 BT_ERR("PSMODE command failed\n");
250
251 return 0;
252}
253EXPORT_SYMBOL_GPL(btmrvl_enable_ps);
254
255int btmrvl_enable_hs(struct btmrvl_private *priv)
256{
257 int ret;
258
259 ret = btmrvl_send_sync_cmd(priv, BT_CMD_HOST_SLEEP_ENABLE, NULL, 0);
260 if (ret)
261 BT_ERR("Host sleep enable command failed\n");
262
263 return ret;
264}
265EXPORT_SYMBOL_GPL(btmrvl_enable_hs);
266
267int btmrvl_prepare_command(struct btmrvl_private *priv)
268{
269 int ret = 0;
270
271 if (priv->btmrvl_dev.hscfgcmd) {
272 priv->btmrvl_dev.hscfgcmd = 0;
273 btmrvl_send_hscfg_cmd(priv);
274 }
275
276 if (priv->btmrvl_dev.pscmd) {
277 priv->btmrvl_dev.pscmd = 0;
278 btmrvl_enable_ps(priv);
279 }
280
281 if (priv->btmrvl_dev.hscmd) {
282 priv->btmrvl_dev.hscmd = 0;
283
284 if (priv->btmrvl_dev.hsmode) {
285 ret = btmrvl_enable_hs(priv);
286 } else {
287 ret = priv->hw_wakeup_firmware(priv);
288 priv->adapter->hs_state = HS_DEACTIVATED;
289 }
290 }
291
292 return ret;
293}
294
295static int btmrvl_tx_pkt(struct btmrvl_private *priv, struct sk_buff *skb)
296{
297 int ret = 0;
298
299 if (!skb || !skb->data)
300 return -EINVAL;
301
302 if (!skb->len || ((skb->len + BTM_HEADER_LEN) > BTM_UPLD_SIZE)) {
303 BT_ERR("Tx Error: Bad skb length %d : %d",
304 skb->len, BTM_UPLD_SIZE);
305 return -EINVAL;
306 }
307
308 if (skb_headroom(skb) < BTM_HEADER_LEN) {
309 struct sk_buff *tmp = skb;
310
311 skb = skb_realloc_headroom(skb, BTM_HEADER_LEN);
312 if (!skb) {
313 BT_ERR("Tx Error: realloc_headroom failed %d",
314 BTM_HEADER_LEN);
315 skb = tmp;
316 return -EINVAL;
317 }
318
319 kfree_skb(tmp);
320 }
321
322 skb_push(skb, BTM_HEADER_LEN);
323
324
325
326
327
328
329 skb->data[0] = (skb->len & 0x0000ff);
330 skb->data[1] = (skb->len & 0x00ff00) >> 8;
331 skb->data[2] = (skb->len & 0xff0000) >> 16;
332 skb->data[3] = bt_cb(skb)->pkt_type;
333
334 if (priv->hw_host_to_card)
335 ret = priv->hw_host_to_card(priv, skb->data, skb->len);
336
337 return ret;
338}
339
340static void btmrvl_init_adapter(struct btmrvl_private *priv)
341{
342 skb_queue_head_init(&priv->adapter->tx_queue);
343
344 priv->adapter->ps_state = PS_AWAKE;
345
346 init_waitqueue_head(&priv->adapter->cmd_wait_q);
347}
348
349static void btmrvl_free_adapter(struct btmrvl_private *priv)
350{
351 skb_queue_purge(&priv->adapter->tx_queue);
352
353 kfree(priv->adapter);
354
355 priv->adapter = NULL;
356}
357
358static int btmrvl_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
359{
360 struct btmrvl_private *priv = hci_get_drvdata(hdev);
361
362 BT_DBG("type=%d, len=%d", skb->pkt_type, skb->len);
363
364 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
365 BT_ERR("Failed testing HCI_RUNING, flags=%lx", hdev->flags);
366 print_hex_dump_bytes("data: ", DUMP_PREFIX_OFFSET,
367 skb->data, skb->len);
368 return -EBUSY;
369 }
370
371 switch (bt_cb(skb)->pkt_type) {
372 case HCI_COMMAND_PKT:
373 hdev->stat.cmd_tx++;
374 break;
375
376 case HCI_ACLDATA_PKT:
377 hdev->stat.acl_tx++;
378 break;
379
380 case HCI_SCODATA_PKT:
381 hdev->stat.sco_tx++;
382 break;
383 }
384
385 skb_queue_tail(&priv->adapter->tx_queue, skb);
386
387 wake_up_interruptible(&priv->main_thread.wait_q);
388
389 return 0;
390}
391
392static int btmrvl_flush(struct hci_dev *hdev)
393{
394 struct btmrvl_private *priv = hci_get_drvdata(hdev);
395
396 skb_queue_purge(&priv->adapter->tx_queue);
397
398 return 0;
399}
400
401static int btmrvl_close(struct hci_dev *hdev)
402{
403 struct btmrvl_private *priv = hci_get_drvdata(hdev);
404
405 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
406 return 0;
407
408 skb_queue_purge(&priv->adapter->tx_queue);
409
410 return 0;
411}
412
413static int btmrvl_open(struct hci_dev *hdev)
414{
415 set_bit(HCI_RUNNING, &hdev->flags);
416
417 return 0;
418}
419
420
421
422
423
424
425
426
427static int btmrvl_parse_cal_cfg(const u8 *src, u32 len, u8 *dst, u32 dst_size)
428{
429 const u8 *s = src;
430 u8 *d = dst;
431 int ret;
432 u8 tmp[3];
433
434 tmp[2] = '\0';
435 while ((s - src) <= len - 2) {
436 if (isspace(*s)) {
437 s++;
438 continue;
439 }
440
441 if (isxdigit(*s)) {
442 if ((d - dst) >= dst_size) {
443 BT_ERR("calibration data file too big!!!");
444 return -EINVAL;
445 }
446
447 memcpy(tmp, s, 2);
448
449 ret = kstrtou8(tmp, 16, d++);
450 if (ret < 0)
451 return ret;
452
453 s += 2;
454 } else {
455 return -EINVAL;
456 }
457 }
458 if (d == dst)
459 return -EINVAL;
460
461 return 0;
462}
463
464static int btmrvl_load_cal_data(struct btmrvl_private *priv,
465 u8 *config_data)
466{
467 int i, ret;
468 u8 data[BT_CMD_DATA_SIZE];
469
470 data[0] = 0x00;
471 data[1] = 0x00;
472 data[2] = 0x00;
473 data[3] = BT_CMD_DATA_SIZE - 4;
474
475
476
477
478
479 for (i = 4; i < BT_CMD_DATA_SIZE; i++)
480 data[i] = config_data[(i / 4) * 8 - 1 - i];
481
482 print_hex_dump_bytes("Calibration data: ",
483 DUMP_PREFIX_OFFSET, data, BT_CMD_DATA_SIZE);
484
485 ret = btmrvl_send_sync_cmd(priv, BT_CMD_LOAD_CONFIG_DATA, data,
486 BT_CMD_DATA_SIZE);
487 if (ret)
488 BT_ERR("Failed to download caibration data\n");
489
490 return 0;
491}
492
493static int
494btmrvl_process_cal_cfg(struct btmrvl_private *priv, u8 *data, u32 size)
495{
496 u8 cal_data[BT_CAL_DATA_SIZE];
497 int ret;
498
499 ret = btmrvl_parse_cal_cfg(data, size, cal_data, sizeof(cal_data));
500 if (ret)
501 return ret;
502
503 ret = btmrvl_load_cal_data(priv, cal_data);
504 if (ret) {
505 BT_ERR("Fail to load calibrate data");
506 return ret;
507 }
508
509 return 0;
510}
511
512static int btmrvl_cal_data_config(struct btmrvl_private *priv)
513{
514 const struct firmware *cfg;
515 int ret;
516 const char *cal_data = priv->btmrvl_dev.cal_data;
517
518 if (!cal_data)
519 return 0;
520
521 ret = request_firmware(&cfg, cal_data, priv->btmrvl_dev.dev);
522 if (ret < 0) {
523 BT_DBG("Failed to get %s file, skipping cal data download",
524 cal_data);
525 return 0;
526 }
527
528 ret = btmrvl_process_cal_cfg(priv, (u8 *)cfg->data, cfg->size);
529 release_firmware(cfg);
530 return ret;
531}
532
533static int btmrvl_setup(struct hci_dev *hdev)
534{
535 struct btmrvl_private *priv = hci_get_drvdata(hdev);
536
537 btmrvl_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ);
538
539 if (btmrvl_cal_data_config(priv))
540 BT_ERR("Set cal data failed");
541
542 priv->btmrvl_dev.psmode = 1;
543 btmrvl_enable_ps(priv);
544
545 priv->btmrvl_dev.gpio_gap = 0xffff;
546 btmrvl_send_hscfg_cmd(priv);
547
548 return 0;
549}
550
551
552
553
554
555static int btmrvl_service_main_thread(void *data)
556{
557 struct btmrvl_thread *thread = data;
558 struct btmrvl_private *priv = thread->priv;
559 struct btmrvl_adapter *adapter = priv->adapter;
560 wait_queue_t wait;
561 struct sk_buff *skb;
562 ulong flags;
563
564 init_waitqueue_entry(&wait, current);
565
566 for (;;) {
567 add_wait_queue(&thread->wait_q, &wait);
568
569 set_current_state(TASK_INTERRUPTIBLE);
570 if (kthread_should_stop()) {
571 BT_DBG("main_thread: break from main thread");
572 break;
573 }
574
575 if (adapter->wakeup_tries ||
576 ((!adapter->int_count) &&
577 (!priv->btmrvl_dev.tx_dnld_rdy ||
578 skb_queue_empty(&adapter->tx_queue)))) {
579 BT_DBG("main_thread is sleeping...");
580 schedule();
581 }
582
583 set_current_state(TASK_RUNNING);
584
585 remove_wait_queue(&thread->wait_q, &wait);
586
587 BT_DBG("main_thread woke up");
588
589 spin_lock_irqsave(&priv->driver_lock, flags);
590 if (adapter->int_count) {
591 adapter->int_count = 0;
592 spin_unlock_irqrestore(&priv->driver_lock, flags);
593 priv->hw_process_int_status(priv);
594 } else if (adapter->ps_state == PS_SLEEP &&
595 !skb_queue_empty(&adapter->tx_queue)) {
596 spin_unlock_irqrestore(&priv->driver_lock, flags);
597 adapter->wakeup_tries++;
598 priv->hw_wakeup_firmware(priv);
599 continue;
600 } else {
601 spin_unlock_irqrestore(&priv->driver_lock, flags);
602 }
603
604 if (adapter->ps_state == PS_SLEEP)
605 continue;
606
607 if (!priv->btmrvl_dev.tx_dnld_rdy)
608 continue;
609
610 skb = skb_dequeue(&adapter->tx_queue);
611 if (skb) {
612 if (btmrvl_tx_pkt(priv, skb))
613 priv->btmrvl_dev.hcidev->stat.err_tx++;
614 else
615 priv->btmrvl_dev.hcidev->stat.byte_tx += skb->len;
616
617 kfree_skb(skb);
618 }
619 }
620
621 return 0;
622}
623
624int btmrvl_register_hdev(struct btmrvl_private *priv)
625{
626 struct hci_dev *hdev = NULL;
627 int ret;
628
629 hdev = hci_alloc_dev();
630 if (!hdev) {
631 BT_ERR("Can not allocate HCI device");
632 goto err_hdev;
633 }
634
635 priv->btmrvl_dev.hcidev = hdev;
636 hci_set_drvdata(hdev, priv);
637
638 hdev->bus = HCI_SDIO;
639 hdev->open = btmrvl_open;
640 hdev->close = btmrvl_close;
641 hdev->flush = btmrvl_flush;
642 hdev->send = btmrvl_send_frame;
643 hdev->setup = btmrvl_setup;
644
645 hdev->dev_type = priv->btmrvl_dev.dev_type;
646
647 ret = hci_register_dev(hdev);
648 if (ret < 0) {
649 BT_ERR("Can not register HCI device");
650 goto err_hci_register_dev;
651 }
652
653#ifdef CONFIG_DEBUG_FS
654 btmrvl_debugfs_init(hdev);
655#endif
656
657 return 0;
658
659err_hci_register_dev:
660 hci_free_dev(hdev);
661
662err_hdev:
663
664 kthread_stop(priv->main_thread.task);
665
666 btmrvl_free_adapter(priv);
667 kfree(priv);
668
669 return -ENOMEM;
670}
671EXPORT_SYMBOL_GPL(btmrvl_register_hdev);
672
673struct btmrvl_private *btmrvl_add_card(void *card)
674{
675 struct btmrvl_private *priv;
676
677 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
678 if (!priv) {
679 BT_ERR("Can not allocate priv");
680 goto err_priv;
681 }
682
683 priv->adapter = kzalloc(sizeof(*priv->adapter), GFP_KERNEL);
684 if (!priv->adapter) {
685 BT_ERR("Allocate buffer for btmrvl_adapter failed!");
686 goto err_adapter;
687 }
688
689 btmrvl_init_adapter(priv);
690
691 BT_DBG("Starting kthread...");
692 priv->main_thread.priv = priv;
693 spin_lock_init(&priv->driver_lock);
694
695 init_waitqueue_head(&priv->main_thread.wait_q);
696 priv->main_thread.task = kthread_run(btmrvl_service_main_thread,
697 &priv->main_thread, "btmrvl_main_service");
698
699 priv->btmrvl_dev.card = card;
700 priv->btmrvl_dev.tx_dnld_rdy = true;
701
702 return priv;
703
704err_adapter:
705 kfree(priv);
706
707err_priv:
708 return NULL;
709}
710EXPORT_SYMBOL_GPL(btmrvl_add_card);
711
712int btmrvl_remove_card(struct btmrvl_private *priv)
713{
714 struct hci_dev *hdev;
715
716 hdev = priv->btmrvl_dev.hcidev;
717
718 wake_up_interruptible(&priv->adapter->cmd_wait_q);
719
720 kthread_stop(priv->main_thread.task);
721
722#ifdef CONFIG_DEBUG_FS
723 btmrvl_debugfs_remove(hdev);
724#endif
725
726 hci_unregister_dev(hdev);
727
728 hci_free_dev(hdev);
729
730 priv->btmrvl_dev.hcidev = NULL;
731
732 btmrvl_free_adapter(priv);
733
734 kfree(priv);
735
736 return 0;
737}
738EXPORT_SYMBOL_GPL(btmrvl_remove_card);
739
740MODULE_AUTHOR("Marvell International Ltd.");
741MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION);
742MODULE_VERSION(VERSION);
743MODULE_LICENSE("GPL v2");
744