Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  Bluetooth HCI UART H4 driver with Nokia Extensions AKA Nokia H4+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 2015 Marcel Holtmann <marcel@holtmann.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  Copyright (C) 2015-2017 Sebastian Reichel <sre@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/serdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <net/bluetooth/bluetooth.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <net/bluetooth/hci_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include "hci_uart.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include "btbcm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define VERSION "0.1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define NOKIA_ID_BCM2048	0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define NOKIA_ID_TI1271		0x31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define FIRMWARE_BCM2048	"nokia/bcmfw.bin"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define FIRMWARE_TI1271		"nokia/ti1273.bin"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define HCI_NOKIA_NEG_PKT	0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define HCI_NOKIA_ALIVE_PKT	0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define HCI_NOKIA_RADIO_PKT	0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define HCI_NOKIA_NEG_HDR_SIZE		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define HCI_NOKIA_MAX_NEG_SIZE		255
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define HCI_NOKIA_ALIVE_HDR_SIZE	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define HCI_NOKIA_MAX_ALIVE_SIZE	255
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define HCI_NOKIA_RADIO_HDR_SIZE	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define HCI_NOKIA_MAX_RADIO_SIZE	255
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define NOKIA_PROTO_PKT		0x44
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define NOKIA_PROTO_BYTE	0x4c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define NOKIA_NEG_REQ		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define NOKIA_NEG_ACK		0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define NOKIA_NEG_NAK		0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define H4_TYPE_SIZE		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define NOKIA_RECV_ALIVE \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	.type = HCI_NOKIA_ALIVE_PKT, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	.hlen = HCI_NOKIA_ALIVE_HDR_SIZE, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	.loff = 0, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	.lsize = 1, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	.maxlen = HCI_NOKIA_MAX_ALIVE_SIZE \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define NOKIA_RECV_NEG \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	.type = HCI_NOKIA_NEG_PKT, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	.hlen = HCI_NOKIA_NEG_HDR_SIZE, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	.loff = 0, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	.lsize = 1, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	.maxlen = HCI_NOKIA_MAX_NEG_SIZE \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define NOKIA_RECV_RADIO \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	.type = HCI_NOKIA_RADIO_PKT, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	.hlen = HCI_NOKIA_RADIO_HDR_SIZE, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	.loff = 1, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	.lsize = 1, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	.maxlen = HCI_NOKIA_MAX_RADIO_SIZE \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) struct hci_nokia_neg_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	u8	dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) struct hci_nokia_neg_cmd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	u8	ack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	u16	baud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	u16	unused1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	u8	proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	u16	sys_clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	u16	unused2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) #define NOKIA_ALIVE_REQ   0x55
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) #define NOKIA_ALIVE_RESP  0xcc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) struct hci_nokia_alive_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	u8	dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) struct hci_nokia_alive_pkt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	u8	mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	u8	unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct hci_nokia_neg_evt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	u8	ack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	u16	baud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	u16	unused1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	u8	proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	u16	sys_clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	u16	unused2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	u8	man_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	u8	ver_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #define MAX_BAUD_RATE		3692300
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #define SETUP_BAUD_RATE		921600
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #define INIT_BAUD_RATE		120000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct hci_nokia_radio_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	u8	evt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	u8	dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct nokia_bt_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct hci_uart hu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct serdev_device *serdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct gpio_desc *reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct gpio_desc *wakeup_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct gpio_desc *wakeup_bt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	unsigned long sysclk_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	int wake_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct sk_buff *rx_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	struct sk_buff_head txq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	bdaddr_t bdaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	int init_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	struct completion init_completion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	u8 man_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	u8 ver_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	bool initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	bool tx_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	bool rx_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static void nokia_flow_control(struct serdev_device *serdev, bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		serdev_device_set_rts(serdev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		serdev_device_set_flow_control(serdev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		serdev_device_set_flow_control(serdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		serdev_device_set_rts(serdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static irqreturn_t wakeup_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	struct nokia_bt_dev *btdev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	struct device *dev = &btdev->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	int wake_state = gpiod_get_value(btdev->wakeup_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (btdev->rx_enabled == wake_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (wake_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		pm_runtime_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		pm_runtime_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	btdev->rx_enabled = wake_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static int nokia_reset(struct hci_uart *hu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	struct device *dev = &btdev->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	/* reset routine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	gpiod_set_value_cansleep(btdev->reset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	gpiod_set_value_cansleep(btdev->wakeup_bt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	msleep(100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	/* safety check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	err = gpiod_get_value_cansleep(btdev->wakeup_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (err == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		dev_err(dev, "reset: host wakeup not low!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		return -EPROTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	/* flush queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	serdev_device_write_flush(btdev->serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	/* init uart */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	nokia_flow_control(btdev->serdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	serdev_device_set_baudrate(btdev->serdev, INIT_BAUD_RATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	gpiod_set_value_cansleep(btdev->reset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	/* wait for cts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	err = serdev_device_wait_for_cts(btdev->serdev, true, 200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		dev_err(dev, "CTS not received: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	nokia_flow_control(btdev->serdev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static int nokia_send_alive_packet(struct hci_uart *hu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	struct device *dev = &btdev->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	struct hci_nokia_alive_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	struct hci_nokia_alive_pkt *pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	init_completion(&btdev->init_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	len = H4_TYPE_SIZE + sizeof(*hdr) + sizeof(*pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	skb = bt_skb_alloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	hci_skb_pkt_type(skb) = HCI_NOKIA_ALIVE_PKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	memset(skb->data, 0x00, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	hdr = skb_put(skb, sizeof(*hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	hdr->dlen = sizeof(*pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	pkt = skb_put(skb, sizeof(*pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	pkt->mid = NOKIA_ALIVE_REQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	nokia_enqueue(hu, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	hci_uart_tx_wakeup(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	dev_dbg(dev, "Alive sent");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (!wait_for_completion_interruptible_timeout(&btdev->init_completion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		msecs_to_jiffies(1000))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (btdev->init_error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		return btdev->init_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int nokia_send_negotiation(struct hci_uart *hu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	struct device *dev = &btdev->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	struct hci_nokia_neg_cmd *neg_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	struct hci_nokia_neg_hdr *neg_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	int len, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	u16 baud = DIV_ROUND_CLOSEST(btdev->sysclk_speed * 10, SETUP_BAUD_RATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	int sysclk = btdev->sysclk_speed / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	len = H4_TYPE_SIZE + sizeof(*neg_hdr) + sizeof(*neg_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	skb = bt_skb_alloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	hci_skb_pkt_type(skb) = HCI_NOKIA_NEG_PKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	neg_hdr = skb_put(skb, sizeof(*neg_hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	neg_hdr->dlen = sizeof(*neg_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	neg_cmd = skb_put(skb, sizeof(*neg_cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	neg_cmd->ack = NOKIA_NEG_REQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	neg_cmd->baud = cpu_to_le16(baud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	neg_cmd->unused1 = 0x0000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	neg_cmd->proto = NOKIA_PROTO_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	neg_cmd->sys_clk = cpu_to_le16(sysclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	neg_cmd->unused2 = 0x0000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	btdev->init_error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	init_completion(&btdev->init_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	nokia_enqueue(hu, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	hci_uart_tx_wakeup(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	dev_dbg(dev, "Negotiation sent");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (!wait_for_completion_interruptible_timeout(&btdev->init_completion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		msecs_to_jiffies(10000))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (btdev->init_error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		return btdev->init_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	/* Change to previously negotiated speed. Flow Control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	 * is disabled until bluetooth adapter is ready to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	 * broken bytes being received.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	nokia_flow_control(btdev->serdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	serdev_device_set_baudrate(btdev->serdev, SETUP_BAUD_RATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	err = serdev_device_wait_for_cts(btdev->serdev, true, 200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		dev_err(dev, "CTS not received: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	nokia_flow_control(btdev->serdev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	dev_dbg(dev, "Negotiation successful");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int nokia_setup_fw(struct hci_uart *hu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct device *dev = &btdev->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	const char *fwname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	const struct firmware *fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	const u8 *fw_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	size_t fw_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	dev_dbg(dev, "setup firmware");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (btdev->man_id == NOKIA_ID_BCM2048) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		fwname = FIRMWARE_BCM2048;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	} else if (btdev->man_id == NOKIA_ID_TI1271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		fwname = FIRMWARE_TI1271;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		dev_err(dev, "Unsupported bluetooth device!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	err = request_firmware(&fw, fwname, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		dev_err(dev, "%s: Failed to load Nokia firmware file (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			hu->hdev->name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	fw_ptr = fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	fw_size = fw->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	while (fw_size >= 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		u16 pkt_size = get_unaligned_le16(fw_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		u8 pkt_type = fw_ptr[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		const struct hci_command_hdr *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		u16 opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		switch (pkt_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		case HCI_COMMAND_PKT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			cmd = (struct hci_command_hdr *)(fw_ptr + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			opcode = le16_to_cpu(cmd->opcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			skb = __hci_cmd_sync(hu->hdev, opcode, cmd->plen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 					     fw_ptr + 3 + HCI_COMMAND_HDR_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 					     HCI_INIT_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			if (IS_ERR(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 				err = PTR_ERR(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 				dev_err(dev, "%s: FW command %04x failed (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 				       hu->hdev->name, opcode, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		case HCI_NOKIA_RADIO_PKT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		case HCI_NOKIA_NEG_PKT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		case HCI_NOKIA_ALIVE_PKT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		fw_ptr += pkt_size + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		fw_size -= pkt_size + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static int nokia_setup(struct hci_uart *hu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	struct device *dev = &btdev->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	btdev->initialized = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	nokia_flow_control(btdev->serdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	pm_runtime_get_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	if (btdev->tx_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		pm_runtime_put(&btdev->serdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		btdev->tx_enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	dev_dbg(dev, "protocol setup");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	/* 0. reset connection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	err = nokia_reset(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		dev_err(dev, "Reset failed: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	/* 1. negotiate speed etc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	err = nokia_send_negotiation(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		dev_err(dev, "Negotiation failed: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	/* 2. verify correct setup using alive packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	err = nokia_send_alive_packet(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		dev_err(dev, "Alive check failed: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	/* 3. send firmware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	err = nokia_setup_fw(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		dev_err(dev, "Could not setup FW: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	nokia_flow_control(btdev->serdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	serdev_device_set_baudrate(btdev->serdev, MAX_BAUD_RATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	nokia_flow_control(btdev->serdev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (btdev->man_id == NOKIA_ID_BCM2048) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		hu->hdev->set_bdaddr = btbcm_set_bdaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		dev_dbg(dev, "bcm2048 has invalid bluetooth address!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	dev_dbg(dev, "protocol setup done!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	pm_runtime_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	btdev->tx_enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	btdev->initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	pm_runtime_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) static int nokia_open(struct hci_uart *hu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	struct device *dev = &hu->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	dev_dbg(dev, "protocol open");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	pm_runtime_enable(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) static int nokia_flush(struct hci_uart *hu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	dev_dbg(&btdev->serdev->dev, "flush device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	skb_queue_purge(&btdev->txq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) static int nokia_close(struct hci_uart *hu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	struct device *dev = &btdev->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	dev_dbg(dev, "close device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	btdev->initialized = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	skb_queue_purge(&btdev->txq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	kfree_skb(btdev->rx_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	/* disable module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	gpiod_set_value(btdev->reset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	gpiod_set_value(btdev->wakeup_bt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	pm_runtime_disable(&btdev->serdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) /* Enqueue frame for transmittion (padding, crc, etc) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	/* Prepend skb with frame type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	/* Packets must be word aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (skb->len % 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		err = skb_pad(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		skb_put(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	skb_queue_tail(&btdev->txq, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static int nokia_recv_negotiation_packet(struct hci_dev *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 					 struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	struct hci_uart *hu = hci_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	struct device *dev = &btdev->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	struct hci_nokia_neg_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	struct hci_nokia_neg_evt *evt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	hdr = (struct hci_nokia_neg_hdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	if (hdr->dlen != sizeof(*evt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		btdev->init_error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		goto finish_neg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	evt = skb_pull(skb, sizeof(*hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	if (evt->ack != NOKIA_NEG_ACK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		dev_err(dev, "Negotiation received: wrong reply");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		btdev->init_error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		goto finish_neg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	btdev->man_id = evt->man_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	btdev->ver_id = evt->ver_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	dev_dbg(dev, "Negotiation received: baud=%u:clk=%u:manu=%u:vers=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		evt->baud, evt->sys_clk, evt->man_id, evt->ver_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) finish_neg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	complete(&btdev->init_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static int nokia_recv_alive_packet(struct hci_dev *hdev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	struct hci_uart *hu = hci_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	struct device *dev = &btdev->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	struct hci_nokia_alive_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	struct hci_nokia_alive_pkt *pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	hdr = (struct hci_nokia_alive_hdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (hdr->dlen != sizeof(*pkt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		dev_err(dev, "Corrupted alive message");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		btdev->init_error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		goto finish_alive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	pkt = skb_pull(skb, sizeof(*hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	if (pkt->mid != NOKIA_ALIVE_RESP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		dev_err(dev, "Alive received: invalid response: 0x%02x!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 			pkt->mid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		btdev->init_error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		goto finish_alive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	dev_dbg(dev, "Alive received");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) finish_alive:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	complete(&btdev->init_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) static int nokia_recv_radio(struct hci_dev *hdev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	/* Packets received on the dedicated radio channel are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	 * HCI events and so feed them back into the core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	return hci_recv_frame(hdev, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) /* Recv data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) static const struct h4_recv_pkt nokia_recv_pkts[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	{ H4_RECV_ACL,		.recv = hci_recv_frame },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	{ H4_RECV_SCO,		.recv = hci_recv_frame },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	{ H4_RECV_EVENT,	.recv = hci_recv_frame },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	{ NOKIA_RECV_ALIVE,	.recv = nokia_recv_alive_packet },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	{ NOKIA_RECV_NEG,	.recv = nokia_recv_negotiation_packet },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	{ NOKIA_RECV_RADIO,	.recv = nokia_recv_radio },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) static int nokia_recv(struct hci_uart *hu, const void *data, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	struct device *dev = &btdev->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		return -EUNATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	btdev->rx_skb = h4_recv_buf(hu->hdev, btdev->rx_skb, data, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 				  nokia_recv_pkts, ARRAY_SIZE(nokia_recv_pkts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	if (IS_ERR(btdev->rx_skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		err = PTR_ERR(btdev->rx_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		dev_err(dev, "Frame reassembly failed (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		btdev->rx_skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) static struct sk_buff *nokia_dequeue(struct hci_uart *hu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	struct nokia_bt_dev *btdev = hu->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	struct device *dev = &btdev->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	struct sk_buff *result = skb_dequeue(&btdev->txq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	if (!btdev->initialized)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	if (btdev->tx_enabled == !!result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		pm_runtime_get_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		gpiod_set_value_cansleep(btdev->wakeup_bt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		serdev_device_wait_until_sent(btdev->serdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		pm_runtime_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	btdev->tx_enabled = !!result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) static const struct hci_uart_proto nokia_proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	.id		= HCI_UART_NOKIA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	.name		= "Nokia",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	.open		= nokia_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	.close		= nokia_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	.recv		= nokia_recv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	.enqueue	= nokia_enqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	.dequeue	= nokia_dequeue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	.flush		= nokia_flush,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	.setup		= nokia_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	.manufacturer	= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static int nokia_bluetooth_serdev_probe(struct serdev_device *serdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	struct device *dev = &serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	struct nokia_bt_dev *btdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	struct clk *sysclk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	btdev = devm_kzalloc(dev, sizeof(*btdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	if (!btdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	btdev->hu.serdev = btdev->serdev = serdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	serdev_device_set_drvdata(serdev, btdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	btdev->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	if (IS_ERR(btdev->reset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		err = PTR_ERR(btdev->reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		dev_err(dev, "could not get reset gpio: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	btdev->wakeup_host = devm_gpiod_get(dev, "host-wakeup", GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	if (IS_ERR(btdev->wakeup_host)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		err = PTR_ERR(btdev->wakeup_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		dev_err(dev, "could not get host wakeup gpio: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	btdev->wake_irq = gpiod_to_irq(btdev->wakeup_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	err = devm_request_threaded_irq(dev, btdev->wake_irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 		wakeup_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 		"wakeup", btdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 		dev_err(dev, "could request wakeup irq: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	btdev->wakeup_bt = devm_gpiod_get(dev, "bluetooth-wakeup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 					   GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	if (IS_ERR(btdev->wakeup_bt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		err = PTR_ERR(btdev->wakeup_bt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		dev_err(dev, "could not get BT wakeup gpio: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	sysclk = devm_clk_get(dev, "sysclk");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	if (IS_ERR(sysclk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		err = PTR_ERR(sysclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		dev_err(dev, "could not get sysclk: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	clk_prepare_enable(sysclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	btdev->sysclk_speed = clk_get_rate(sysclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	clk_disable_unprepare(sysclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	skb_queue_head_init(&btdev->txq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	btdev->hu.priv = btdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	btdev->hu.alignment = 2; /* Nokia H4+ is word aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	err = hci_uart_register_device(&btdev->hu, &nokia_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		dev_err(dev, "could not register bluetooth uart: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) static void nokia_bluetooth_serdev_remove(struct serdev_device *serdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	struct nokia_bt_dev *btdev = serdev_device_get_drvdata(serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	hci_uart_unregister_device(&btdev->hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) static int nokia_bluetooth_runtime_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	struct serdev_device *serdev = to_serdev_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	nokia_flow_control(serdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) static int nokia_bluetooth_runtime_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	struct serdev_device *serdev = to_serdev_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	nokia_flow_control(serdev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) static const struct dev_pm_ops nokia_bluetooth_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	SET_RUNTIME_PM_OPS(nokia_bluetooth_runtime_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 			   nokia_bluetooth_runtime_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 			   NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) static const struct of_device_id nokia_bluetooth_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	{ .compatible = "nokia,h4p-bluetooth", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) MODULE_DEVICE_TABLE(of, nokia_bluetooth_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) static struct serdev_device_driver nokia_bluetooth_serdev_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	.probe = nokia_bluetooth_serdev_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	.remove = nokia_bluetooth_serdev_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 		.name = "nokia-bluetooth",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 		.pm = &nokia_bluetooth_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 		.of_match_table = of_match_ptr(nokia_bluetooth_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) module_serdev_device_driver(nokia_bluetooth_serdev_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) MODULE_DESCRIPTION("Bluetooth HCI UART Nokia H4+ driver ver " VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) MODULE_VERSION(VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) MODULE_LICENSE("GPL");