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 serdev driver lib
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 2017  Linaro, Ltd., Rob Herring <robh@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  Based on hci_ldisc.c:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *  Copyright (C) 2000-2001  Qualcomm Incorporated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^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/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/serdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <net/bluetooth/bluetooth.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <net/bluetooth/hci_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "hci_uart.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct hci_dev *hdev = hu->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	/* Update HCI stat counters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	switch (pkt_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	case HCI_COMMAND_PKT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		hdev->stat.cmd_tx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	case HCI_ACLDATA_PKT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		hdev->stat.acl_tx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	case HCI_SCODATA_PKT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		hdev->stat.sco_tx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct sk_buff *skb = hu->tx_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			skb = hu->proto->dequeue(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		hu->tx_skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static void hci_uart_write_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct serdev_device *serdev = hu->serdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct hci_dev *hdev = hu->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	/* REVISIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	 * should we cope with bad skbs or ->write() returning an error value?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		while ((skb = hci_uart_dequeue(hu))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			len = serdev_device_write_buf(serdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 						      skb->data, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			hdev->stat.byte_tx += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			skb_pull(skb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			if (skb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 				hu->tx_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		clear_bit(HCI_UART_SENDING, &hu->tx_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	} while (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) /* ------- Interface to HCI layer ------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) /* Reset device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static int hci_uart_flush(struct hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct hci_uart *hu  = hci_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (hu->tx_skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/* Flush any pending characters in the driver and discipline. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	serdev_device_write_flush(hu->serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		hu->proto->flush(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* Initialize device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int hci_uart_open(struct hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct hci_uart *hu = hci_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	BT_DBG("%s %p", hdev->name, hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	/* When Quirk HCI_QUIRK_NON_PERSISTENT_SETUP is set by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	 * driver, BT SoC is completely turned OFF during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	 * BT OFF. Upon next BT ON UART port should be opened.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		err = serdev_device_open(hu->serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		set_bit(HCI_UART_PROTO_READY, &hu->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	/* Undo clearing this from hci_uart_close() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	hdev->flush = hci_uart_flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* Close device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int hci_uart_close(struct hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct hci_uart *hu = hci_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	BT_DBG("hdev %p", hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	hci_uart_flush(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	hdev->flush = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	/* When QUIRK HCI_QUIRK_NON_PERSISTENT_SETUP is set by driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	 * BT SOC is completely powered OFF during BT OFF, holding port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	 * open may drain the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		clear_bit(HCI_UART_PROTO_READY, &hu->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		serdev_device_close(hu->serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* Send frames from HCI layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	struct hci_uart *hu = hci_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	       skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	hu->proto->enqueue(hu, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	hci_uart_tx_wakeup(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static int hci_uart_setup(struct hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	struct hci_uart *hu = hci_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	struct hci_rp_read_local_version *ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	unsigned int speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	/* Init speed if any */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (hu->init_speed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		speed = hu->init_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	else if (hu->proto->init_speed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		speed = hu->proto->init_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		speed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (speed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		serdev_device_set_baudrate(hu->serdev, speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	/* Operational speed if any */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (hu->oper_speed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		speed = hu->oper_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	else if (hu->proto->oper_speed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		speed = hu->proto->oper_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		speed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (hu->proto->set_baudrate && speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		err = hu->proto->set_baudrate(hu, speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			bt_dev_err(hdev, "Failed to set baudrate");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			serdev_device_set_baudrate(hu->serdev, speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (hu->proto->setup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		return hu->proto->setup(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			     HCI_INIT_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (IS_ERR(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		bt_dev_err(hdev, "Reading local version info failed (%ld)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			   PTR_ERR(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (skb->len != sizeof(*ver))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		bt_dev_err(hdev, "Event length mismatch for version info");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /** hci_uart_write_wakeup - transmit buffer wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * @serdev: serial device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  * This function is called by the serdev framework when it accepts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  * more data being sent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static void hci_uart_write_wakeup(struct serdev_device *serdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	struct hci_uart *hu = serdev_device_get_drvdata(serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	BT_DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (!hu || serdev != hu->serdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		hci_uart_tx_wakeup(hu);
^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) /** hci_uart_receive_buf - receive buffer wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * @serdev: serial device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  * @data:   pointer to received data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  * @count:  count of received data in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  * This function is called by the serdev framework when it received data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  * in the RX buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  * Return: number of processed bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				   size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	struct hci_uart *hu = serdev_device_get_drvdata(serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (!hu || serdev != hu->serdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	/* It does not need a lock here as it is already protected by a mutex in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	 * tty caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	hu->proto->recv(hu, data, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (hu->hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		hu->hdev->stat.byte_rx += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static const struct serdev_device_ops hci_serdev_client_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	.receive_buf = hci_uart_receive_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	.write_wakeup = hci_uart_write_wakeup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int hci_uart_register_device(struct hci_uart *hu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			     const struct hci_uart_proto *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct hci_dev *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	BT_DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	err = serdev_device_open(hu->serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	percpu_init_rwsem(&hu->proto_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	err = p->open(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		goto err_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	hu->proto = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	set_bit(HCI_UART_PROTO_READY, &hu->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	/* Initialize and register HCI device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	hdev = hci_alloc_dev();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (!hdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		BT_ERR("Can't allocate HCI device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	hu->hdev = hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	hdev->bus = HCI_UART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	hci_set_drvdata(hdev, hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	INIT_WORK(&hu->init_ready, hci_uart_init_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	INIT_WORK(&hu->write_work, hci_uart_write_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	/* Only when vendor specific setup callback is provided, consider
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	 * the manufacturer information valid. This avoids filling in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	 * value for Ericsson when nothing is specified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	if (hu->proto->setup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		hdev->manufacturer = hu->proto->manufacturer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	hdev->open  = hci_uart_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	hdev->close = hci_uart_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	hdev->flush = hci_uart_flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	hdev->send  = hci_uart_send_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	hdev->setup = hci_uart_setup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		hdev->dev_type = HCI_AMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		hdev->dev_type = HCI_PRIMARY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (hci_register_dev(hdev) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		BT_ERR("Can't register HCI device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		goto err_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	set_bit(HCI_UART_REGISTERED, &hu->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) err_register:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	hci_free_dev(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) err_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	clear_bit(HCI_UART_PROTO_READY, &hu->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	p->close(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) err_open:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	serdev_device_close(hu->serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) EXPORT_SYMBOL_GPL(hci_uart_register_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) void hci_uart_unregister_device(struct hci_uart *hu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	struct hci_dev *hdev = hu->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	cancel_work_sync(&hu->init_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (test_bit(HCI_UART_REGISTERED, &hu->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		hci_unregister_dev(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	hci_free_dev(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	cancel_work_sync(&hu->write_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	hu->proto->close(hu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		clear_bit(HCI_UART_PROTO_READY, &hu->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		serdev_device_close(hu->serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) EXPORT_SYMBOL_GPL(hci_uart_unregister_device);