1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Bluetooth HCI UART H4 driver with Nokia Extensions AKA Nokia H4+
5 * Copyright (C) 2015 Marcel Holtmann <marcel@holtmann.org>
6 * Copyright (C) 2015-2017 Sebastian Reichel <sre@kernel.org>
10 #include <linux/errno.h>
11 #include <linux/firmware.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/serdev.h>
19 #include <linux/skbuff.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/types.h>
23 #include <linux/unaligned.h>
24 #include <net/bluetooth/bluetooth.h>
25 #include <net/bluetooth/hci_core.h>
32 #define NOKIA_ID_BCM2048 0x04
33 #define NOKIA_ID_TI1271 0x31
35 #define FIRMWARE_BCM2048 "nokia/bcmfw.bin"
36 #define FIRMWARE_TI1271 "nokia/ti1273.bin"
38 #define HCI_NOKIA_NEG_PKT 0x06
39 #define HCI_NOKIA_ALIVE_PKT 0x07
40 #define HCI_NOKIA_RADIO_PKT 0x08
42 #define HCI_NOKIA_NEG_HDR_SIZE 1
43 #define HCI_NOKIA_MAX_NEG_SIZE 255
44 #define HCI_NOKIA_ALIVE_HDR_SIZE 1
45 #define HCI_NOKIA_MAX_ALIVE_SIZE 255
46 #define HCI_NOKIA_RADIO_HDR_SIZE 2
47 #define HCI_NOKIA_MAX_RADIO_SIZE 255
49 #define NOKIA_PROTO_PKT 0x44
50 #define NOKIA_PROTO_BYTE 0x4c
52 #define NOKIA_NEG_REQ 0x00
53 #define NOKIA_NEG_ACK 0x20
54 #define NOKIA_NEG_NAK 0x40
56 #define H4_TYPE_SIZE 1
58 #define NOKIA_RECV_ALIVE \
59 .type = HCI_NOKIA_ALIVE_PKT, \
60 .hlen = HCI_NOKIA_ALIVE_HDR_SIZE, \
63 .maxlen = HCI_NOKIA_MAX_ALIVE_SIZE \
65 #define NOKIA_RECV_NEG \
66 .type = HCI_NOKIA_NEG_PKT, \
67 .hlen = HCI_NOKIA_NEG_HDR_SIZE, \
70 .maxlen = HCI_NOKIA_MAX_NEG_SIZE \
72 #define NOKIA_RECV_RADIO \
73 .type = HCI_NOKIA_RADIO_PKT, \
74 .hlen = HCI_NOKIA_RADIO_HDR_SIZE, \
77 .maxlen = HCI_NOKIA_MAX_RADIO_SIZE \
79 struct hci_nokia_neg_hdr {
83 struct hci_nokia_neg_cmd
{
92 #define NOKIA_ALIVE_REQ 0x55
93 #define NOKIA_ALIVE_RESP 0xcc
95 struct hci_nokia_alive_hdr
{
99 struct hci_nokia_alive_pkt
{
104 struct hci_nokia_neg_evt
{
115 #define MAX_BAUD_RATE 3692300
116 #define SETUP_BAUD_RATE 921600
117 #define INIT_BAUD_RATE 120000
119 struct nokia_bt_dev
{
121 struct serdev_device
*serdev
;
123 struct gpio_desc
*reset
;
124 struct gpio_desc
*wakeup_host
;
125 struct gpio_desc
*wakeup_bt
;
126 unsigned long sysclk_speed
;
129 struct sk_buff
*rx_skb
;
130 struct sk_buff_head txq
;
134 struct completion init_completion
;
144 static int nokia_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
);
146 static void nokia_flow_control(struct serdev_device
*serdev
, bool enable
)
149 serdev_device_set_rts(serdev
, true);
150 serdev_device_set_flow_control(serdev
, true);
152 serdev_device_set_flow_control(serdev
, false);
153 serdev_device_set_rts(serdev
, false);
157 static irqreturn_t
wakeup_handler(int irq
, void *data
)
159 struct nokia_bt_dev
*btdev
= data
;
160 struct device
*dev
= &btdev
->serdev
->dev
;
161 int wake_state
= gpiod_get_value(btdev
->wakeup_host
);
163 if (btdev
->rx_enabled
== wake_state
)
171 btdev
->rx_enabled
= wake_state
;
176 static int nokia_reset(struct hci_uart
*hu
)
178 struct nokia_bt_dev
*btdev
= hu
->priv
;
179 struct device
*dev
= &btdev
->serdev
->dev
;
183 gpiod_set_value_cansleep(btdev
->reset
, 1);
184 gpiod_set_value_cansleep(btdev
->wakeup_bt
, 1);
189 err
= gpiod_get_value_cansleep(btdev
->wakeup_host
);
191 dev_err(dev
, "reset: host wakeup not low!");
196 serdev_device_write_flush(btdev
->serdev
);
199 nokia_flow_control(btdev
->serdev
, false);
200 serdev_device_set_baudrate(btdev
->serdev
, INIT_BAUD_RATE
);
202 gpiod_set_value_cansleep(btdev
->reset
, 0);
205 err
= serdev_device_wait_for_cts(btdev
->serdev
, true, 200);
207 dev_err(dev
, "CTS not received: %d", err
);
211 nokia_flow_control(btdev
->serdev
, true);
216 static int nokia_send_alive_packet(struct hci_uart
*hu
)
218 struct nokia_bt_dev
*btdev
= hu
->priv
;
219 struct device
*dev
= &btdev
->serdev
->dev
;
220 struct hci_nokia_alive_hdr
*hdr
;
221 struct hci_nokia_alive_pkt
*pkt
;
225 init_completion(&btdev
->init_completion
);
227 len
= H4_TYPE_SIZE
+ sizeof(*hdr
) + sizeof(*pkt
);
228 skb
= bt_skb_alloc(len
, GFP_KERNEL
);
232 hci_skb_pkt_type(skb
) = HCI_NOKIA_ALIVE_PKT
;
233 memset(skb
->data
, 0x00, len
);
235 hdr
= skb_put(skb
, sizeof(*hdr
));
236 hdr
->dlen
= sizeof(*pkt
);
237 pkt
= skb_put(skb
, sizeof(*pkt
));
238 pkt
->mid
= NOKIA_ALIVE_REQ
;
240 nokia_enqueue(hu
, skb
);
241 hci_uart_tx_wakeup(hu
);
243 dev_dbg(dev
, "Alive sent");
245 if (!wait_for_completion_interruptible_timeout(&btdev
->init_completion
,
246 msecs_to_jiffies(1000))) {
250 if (btdev
->init_error
< 0)
251 return btdev
->init_error
;
256 static int nokia_send_negotiation(struct hci_uart
*hu
)
258 struct nokia_bt_dev
*btdev
= hu
->priv
;
259 struct device
*dev
= &btdev
->serdev
->dev
;
260 struct hci_nokia_neg_cmd
*neg_cmd
;
261 struct hci_nokia_neg_hdr
*neg_hdr
;
264 u16 baud
= DIV_ROUND_CLOSEST(btdev
->sysclk_speed
* 10, SETUP_BAUD_RATE
);
265 int sysclk
= btdev
->sysclk_speed
/ 1000;
267 len
= H4_TYPE_SIZE
+ sizeof(*neg_hdr
) + sizeof(*neg_cmd
);
268 skb
= bt_skb_alloc(len
, GFP_KERNEL
);
272 hci_skb_pkt_type(skb
) = HCI_NOKIA_NEG_PKT
;
274 neg_hdr
= skb_put(skb
, sizeof(*neg_hdr
));
275 neg_hdr
->dlen
= sizeof(*neg_cmd
);
277 neg_cmd
= skb_put(skb
, sizeof(*neg_cmd
));
278 neg_cmd
->ack
= NOKIA_NEG_REQ
;
279 neg_cmd
->baud
= cpu_to_le16(baud
);
280 neg_cmd
->unused1
= 0x0000;
281 neg_cmd
->proto
= NOKIA_PROTO_BYTE
;
282 neg_cmd
->sys_clk
= cpu_to_le16(sysclk
);
283 neg_cmd
->unused2
= 0x0000;
285 btdev
->init_error
= 0;
286 init_completion(&btdev
->init_completion
);
288 nokia_enqueue(hu
, skb
);
289 hci_uart_tx_wakeup(hu
);
291 dev_dbg(dev
, "Negotiation sent");
293 if (!wait_for_completion_interruptible_timeout(&btdev
->init_completion
,
294 msecs_to_jiffies(10000))) {
298 if (btdev
->init_error
< 0)
299 return btdev
->init_error
;
301 /* Change to previously negotiated speed. Flow Control
302 * is disabled until bluetooth adapter is ready to avoid
303 * broken bytes being received.
305 nokia_flow_control(btdev
->serdev
, false);
306 serdev_device_set_baudrate(btdev
->serdev
, SETUP_BAUD_RATE
);
307 err
= serdev_device_wait_for_cts(btdev
->serdev
, true, 200);
309 dev_err(dev
, "CTS not received: %d", err
);
312 nokia_flow_control(btdev
->serdev
, true);
314 dev_dbg(dev
, "Negotiation successful");
319 static int nokia_setup_fw(struct hci_uart
*hu
)
321 struct nokia_bt_dev
*btdev
= hu
->priv
;
322 struct device
*dev
= &btdev
->serdev
->dev
;
324 const struct firmware
*fw
;
329 dev_dbg(dev
, "setup firmware");
331 if (btdev
->man_id
== NOKIA_ID_BCM2048
) {
332 fwname
= FIRMWARE_BCM2048
;
333 } else if (btdev
->man_id
== NOKIA_ID_TI1271
) {
334 fwname
= FIRMWARE_TI1271
;
336 dev_err(dev
, "Unsupported bluetooth device!");
340 err
= request_firmware(&fw
, fwname
, dev
);
342 dev_err(dev
, "%s: Failed to load Nokia firmware file (%d)",
343 hu
->hdev
->name
, err
);
350 while (fw_size
>= 4) {
351 u16 pkt_size
= get_unaligned_le16(fw_ptr
);
352 u8 pkt_type
= fw_ptr
[2];
353 const struct hci_command_hdr
*cmd
;
358 case HCI_COMMAND_PKT
:
359 cmd
= (struct hci_command_hdr
*)(fw_ptr
+ 3);
360 opcode
= le16_to_cpu(cmd
->opcode
);
362 skb
= __hci_cmd_sync(hu
->hdev
, opcode
, cmd
->plen
,
363 fw_ptr
+ 3 + HCI_COMMAND_HDR_SIZE
,
367 dev_err(dev
, "%s: FW command %04x failed (%d)",
368 hu
->hdev
->name
, opcode
, err
);
373 case HCI_NOKIA_RADIO_PKT
:
374 case HCI_NOKIA_NEG_PKT
:
375 case HCI_NOKIA_ALIVE_PKT
:
379 fw_ptr
+= pkt_size
+ 2;
380 fw_size
-= pkt_size
+ 2;
384 release_firmware(fw
);
388 static int nokia_setup(struct hci_uart
*hu
)
390 struct nokia_bt_dev
*btdev
= hu
->priv
;
391 struct device
*dev
= &btdev
->serdev
->dev
;
394 btdev
->initialized
= false;
396 nokia_flow_control(btdev
->serdev
, false);
398 pm_runtime_get_sync(dev
);
400 if (btdev
->tx_enabled
) {
401 gpiod_set_value_cansleep(btdev
->wakeup_bt
, 0);
402 pm_runtime_put(&btdev
->serdev
->dev
);
403 btdev
->tx_enabled
= false;
406 dev_dbg(dev
, "protocol setup");
408 /* 0. reset connection */
409 err
= nokia_reset(hu
);
411 dev_err(dev
, "Reset failed: %d", err
);
415 /* 1. negotiate speed etc */
416 err
= nokia_send_negotiation(hu
);
418 dev_err(dev
, "Negotiation failed: %d", err
);
422 /* 2. verify correct setup using alive packet */
423 err
= nokia_send_alive_packet(hu
);
425 dev_err(dev
, "Alive check failed: %d", err
);
429 /* 3. send firmware */
430 err
= nokia_setup_fw(hu
);
432 dev_err(dev
, "Could not setup FW: %d", err
);
436 nokia_flow_control(btdev
->serdev
, false);
437 serdev_device_set_baudrate(btdev
->serdev
, MAX_BAUD_RATE
);
438 nokia_flow_control(btdev
->serdev
, true);
440 if (btdev
->man_id
== NOKIA_ID_BCM2048
) {
441 hu
->hdev
->set_bdaddr
= btbcm_set_bdaddr
;
442 set_bit(HCI_QUIRK_INVALID_BDADDR
, &hu
->hdev
->quirks
);
443 dev_dbg(dev
, "bcm2048 has invalid bluetooth address!");
446 dev_dbg(dev
, "protocol setup done!");
448 gpiod_set_value_cansleep(btdev
->wakeup_bt
, 0);
450 btdev
->tx_enabled
= false;
451 btdev
->initialized
= true;
460 static int nokia_open(struct hci_uart
*hu
)
462 struct device
*dev
= &hu
->serdev
->dev
;
464 dev_dbg(dev
, "protocol open");
466 pm_runtime_enable(dev
);
471 static int nokia_flush(struct hci_uart
*hu
)
473 struct nokia_bt_dev
*btdev
= hu
->priv
;
475 dev_dbg(&btdev
->serdev
->dev
, "flush device");
477 skb_queue_purge(&btdev
->txq
);
482 static int nokia_close(struct hci_uart
*hu
)
484 struct nokia_bt_dev
*btdev
= hu
->priv
;
485 struct device
*dev
= &btdev
->serdev
->dev
;
487 dev_dbg(dev
, "close device");
489 btdev
->initialized
= false;
491 skb_queue_purge(&btdev
->txq
);
493 kfree_skb(btdev
->rx_skb
);
496 gpiod_set_value(btdev
->reset
, 1);
497 gpiod_set_value(btdev
->wakeup_bt
, 0);
499 pm_runtime_disable(&btdev
->serdev
->dev
);
504 /* Enqueue frame for transmission (padding, crc, etc) */
505 static int nokia_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
507 struct nokia_bt_dev
*btdev
= hu
->priv
;
510 /* Prepend skb with frame type */
511 memcpy(skb_push(skb
, 1), &bt_cb(skb
)->pkt_type
, 1);
513 /* Packets must be word aligned */
515 err
= skb_pad(skb
, 1);
521 skb_queue_tail(&btdev
->txq
, skb
);
526 static int nokia_recv_negotiation_packet(struct hci_dev
*hdev
,
529 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
530 struct nokia_bt_dev
*btdev
= hu
->priv
;
531 struct device
*dev
= &btdev
->serdev
->dev
;
532 struct hci_nokia_neg_hdr
*hdr
;
533 struct hci_nokia_neg_evt
*evt
;
536 hdr
= (struct hci_nokia_neg_hdr
*)skb
->data
;
537 if (hdr
->dlen
!= sizeof(*evt
)) {
538 btdev
->init_error
= -EIO
;
543 evt
= skb_pull(skb
, sizeof(*hdr
));
545 if (evt
->ack
!= NOKIA_NEG_ACK
) {
546 dev_err(dev
, "Negotiation received: wrong reply");
547 btdev
->init_error
= -EINVAL
;
552 btdev
->man_id
= evt
->man_id
;
553 btdev
->ver_id
= evt
->ver_id
;
555 dev_dbg(dev
, "Negotiation received: baud=%u:clk=%u:manu=%u:vers=%u",
556 evt
->baud
, evt
->sys_clk
, evt
->man_id
, evt
->ver_id
);
559 complete(&btdev
->init_completion
);
564 static int nokia_recv_alive_packet(struct hci_dev
*hdev
, struct sk_buff
*skb
)
566 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
567 struct nokia_bt_dev
*btdev
= hu
->priv
;
568 struct device
*dev
= &btdev
->serdev
->dev
;
569 struct hci_nokia_alive_hdr
*hdr
;
570 struct hci_nokia_alive_pkt
*pkt
;
573 hdr
= (struct hci_nokia_alive_hdr
*)skb
->data
;
574 if (hdr
->dlen
!= sizeof(*pkt
)) {
575 dev_err(dev
, "Corrupted alive message");
576 btdev
->init_error
= -EIO
;
581 pkt
= skb_pull(skb
, sizeof(*hdr
));
583 if (pkt
->mid
!= NOKIA_ALIVE_RESP
) {
584 dev_err(dev
, "Alive received: invalid response: 0x%02x!",
586 btdev
->init_error
= -EINVAL
;
591 dev_dbg(dev
, "Alive received");
594 complete(&btdev
->init_completion
);
599 static int nokia_recv_radio(struct hci_dev
*hdev
, struct sk_buff
*skb
)
601 /* Packets received on the dedicated radio channel are
602 * HCI events and so feed them back into the core.
604 hci_skb_pkt_type(skb
) = HCI_EVENT_PKT
;
605 return hci_recv_frame(hdev
, skb
);
609 static const struct h4_recv_pkt nokia_recv_pkts
[] = {
610 { H4_RECV_ACL
, .recv
= hci_recv_frame
},
611 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
612 { H4_RECV_EVENT
, .recv
= hci_recv_frame
},
613 { NOKIA_RECV_ALIVE
, .recv
= nokia_recv_alive_packet
},
614 { NOKIA_RECV_NEG
, .recv
= nokia_recv_negotiation_packet
},
615 { NOKIA_RECV_RADIO
, .recv
= nokia_recv_radio
},
618 static int nokia_recv(struct hci_uart
*hu
, const void *data
, int count
)
620 struct nokia_bt_dev
*btdev
= hu
->priv
;
621 struct device
*dev
= &btdev
->serdev
->dev
;
624 if (!test_bit(HCI_UART_REGISTERED
, &hu
->flags
))
627 btdev
->rx_skb
= h4_recv_buf(hu
->hdev
, btdev
->rx_skb
, data
, count
,
628 nokia_recv_pkts
, ARRAY_SIZE(nokia_recv_pkts
));
629 if (IS_ERR(btdev
->rx_skb
)) {
630 err
= PTR_ERR(btdev
->rx_skb
);
631 dev_err(dev
, "Frame reassembly failed (%d)", err
);
632 btdev
->rx_skb
= NULL
;
639 static struct sk_buff
*nokia_dequeue(struct hci_uart
*hu
)
641 struct nokia_bt_dev
*btdev
= hu
->priv
;
642 struct device
*dev
= &btdev
->serdev
->dev
;
643 struct sk_buff
*result
= skb_dequeue(&btdev
->txq
);
645 if (!btdev
->initialized
)
648 if (btdev
->tx_enabled
== !!result
)
652 pm_runtime_get_sync(dev
);
653 gpiod_set_value_cansleep(btdev
->wakeup_bt
, 1);
655 serdev_device_wait_until_sent(btdev
->serdev
, 0);
656 gpiod_set_value_cansleep(btdev
->wakeup_bt
, 0);
660 btdev
->tx_enabled
= !!result
;
665 static const struct hci_uart_proto nokia_proto
= {
666 .id
= HCI_UART_NOKIA
,
669 .close
= nokia_close
,
671 .enqueue
= nokia_enqueue
,
672 .dequeue
= nokia_dequeue
,
673 .flush
= nokia_flush
,
674 .setup
= nokia_setup
,
678 static int nokia_bluetooth_serdev_probe(struct serdev_device
*serdev
)
680 struct device
*dev
= &serdev
->dev
;
681 struct nokia_bt_dev
*btdev
;
685 btdev
= devm_kzalloc(dev
, sizeof(*btdev
), GFP_KERNEL
);
689 btdev
->hu
.serdev
= btdev
->serdev
= serdev
;
690 serdev_device_set_drvdata(serdev
, btdev
);
692 btdev
->reset
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
693 if (IS_ERR(btdev
->reset
)) {
694 err
= PTR_ERR(btdev
->reset
);
695 dev_err(dev
, "could not get reset gpio: %d", err
);
699 btdev
->wakeup_host
= devm_gpiod_get(dev
, "host-wakeup", GPIOD_IN
);
700 if (IS_ERR(btdev
->wakeup_host
)) {
701 err
= PTR_ERR(btdev
->wakeup_host
);
702 dev_err(dev
, "could not get host wakeup gpio: %d", err
);
706 btdev
->wake_irq
= gpiod_to_irq(btdev
->wakeup_host
);
708 err
= devm_request_threaded_irq(dev
, btdev
->wake_irq
, NULL
,
710 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
713 dev_err(dev
, "could request wakeup irq: %d", err
);
717 btdev
->wakeup_bt
= devm_gpiod_get(dev
, "bluetooth-wakeup",
719 if (IS_ERR(btdev
->wakeup_bt
)) {
720 err
= PTR_ERR(btdev
->wakeup_bt
);
721 dev_err(dev
, "could not get BT wakeup gpio: %d", err
);
725 sysclk
= devm_clk_get(dev
, "sysclk");
726 if (IS_ERR(sysclk
)) {
727 err
= PTR_ERR(sysclk
);
728 dev_err(dev
, "could not get sysclk: %d", err
);
732 err
= clk_prepare_enable(sysclk
);
734 dev_err(dev
, "could not enable sysclk: %d", err
);
737 btdev
->sysclk_speed
= clk_get_rate(sysclk
);
738 clk_disable_unprepare(sysclk
);
740 skb_queue_head_init(&btdev
->txq
);
742 btdev
->hu
.priv
= btdev
;
743 btdev
->hu
.alignment
= 2; /* Nokia H4+ is word aligned */
745 err
= hci_uart_register_device(&btdev
->hu
, &nokia_proto
);
747 dev_err(dev
, "could not register bluetooth uart: %d", err
);
754 static void nokia_bluetooth_serdev_remove(struct serdev_device
*serdev
)
756 struct nokia_bt_dev
*btdev
= serdev_device_get_drvdata(serdev
);
758 hci_uart_unregister_device(&btdev
->hu
);
761 static int nokia_bluetooth_runtime_suspend(struct device
*dev
)
763 struct serdev_device
*serdev
= to_serdev_device(dev
);
765 nokia_flow_control(serdev
, false);
769 static int nokia_bluetooth_runtime_resume(struct device
*dev
)
771 struct serdev_device
*serdev
= to_serdev_device(dev
);
773 nokia_flow_control(serdev
, true);
777 static const struct dev_pm_ops nokia_bluetooth_pm_ops
= {
778 SET_RUNTIME_PM_OPS(nokia_bluetooth_runtime_suspend
,
779 nokia_bluetooth_runtime_resume
,
784 static const struct of_device_id nokia_bluetooth_of_match
[] = {
785 { .compatible
= "nokia,h4p-bluetooth", },
788 MODULE_DEVICE_TABLE(of
, nokia_bluetooth_of_match
);
791 static struct serdev_device_driver nokia_bluetooth_serdev_driver
= {
792 .probe
= nokia_bluetooth_serdev_probe
,
793 .remove
= nokia_bluetooth_serdev_remove
,
795 .name
= "nokia-bluetooth",
796 .pm
= &nokia_bluetooth_pm_ops
,
797 .of_match_table
= of_match_ptr(nokia_bluetooth_of_match
),
801 module_serdev_device_driver(nokia_bluetooth_serdev_driver
);
803 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
804 MODULE_DESCRIPTION("Bluetooth HCI UART Nokia H4+ driver ver " VERSION
);
805 MODULE_VERSION(VERSION
);
806 MODULE_LICENSE("GPL");