linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ayush Garg <ayush.garg@samsung.com>
To: linux-bluetooth@vger.kernel.org
Cc: anupam.r@samsung.com, nitin.j@samsung.com
Subject: [PATCH 2/4] Bluetooth: Implement BT_PHY setsocket option
Date: Thu, 22 Jul 2021 11:08:41 +0530	[thread overview]
Message-ID: <20210722053843.6691-3-ayush.garg@samsung.com> (raw)
In-Reply-To: <20210722053843.6691-1-ayush.garg@samsung.com>

This adds BT_PHY setsocket option which can be used
to set the PHYs for the underline LE connection.

< HCI Command: LE Set PHY (0x08|0x0032) plen 7
		Handle: 0
		All PHYs preference: 0x00
		TX PHYs preference: 0x02
		  LE 2M
		RX PHYs preference: 0x02
		  LE 2M
		PHY options preference: Reserved (0x0000)
> HCI Event: Command Status (0x0f) plen 4
	  LE Set PHY (0x08|0x0032) ncmd 1
		Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 6
	  LE PHY Update Complete (0x0c)
		Status: Success (0x00)
		Handle: 0
		TX PHY: LE 2M (0x02)
		RX PHY: LE 2M (0x02)

Reviewed-by: Anupam Roy <anupam.r@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
---
 include/net/bluetooth/bluetooth.h |  7 ++++
 include/net/bluetooth/hci.h       | 12 +++++++
 include/net/bluetooth/hci_core.h  |  1 +
 net/bluetooth/hci_conn.c          | 56 +++++++++++++++++++++++++++++++
 net/bluetooth/l2cap_sock.c        | 21 +++++++++++-
 5 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h
index 9125effbf448..af53a98e4236 100644
--- a/include/net/bluetooth/bluetooth.h
+++ b/include/net/bluetooth/bluetooth.h
@@ -140,6 +140,13 @@ struct bt_voice {
 #define BT_PHY_LE_2M_RX		0x00001000
 #define BT_PHY_LE_CODED_TX	0x00002000
 #define BT_PHY_LE_CODED_RX	0x00004000
+#define BT_PHY_LE_CODED_S2      0x00008000
+#define BT_PHY_LE_CODED_S8      0x00010000
+
+#define BT_PHY_LE_TX_MASK (BT_PHY_LE_1M_TX | BT_PHY_LE_2M_TX | \
+			     BT_PHY_LE_CODED_TX)
+#define BT_PHY_LE_RX_MASK (BT_PHY_LE_1M_RX | BT_PHY_LE_2M_RX | \
+			     BT_PHY_LE_CODED_RX)
 
 #define BT_MODE			15
 
diff --git a/include/net/bluetooth/hci.h b/include/net/bluetooth/hci.h
index 56542a09ec43..06e10ccf2a1c 100644
--- a/include/net/bluetooth/hci.h
+++ b/include/net/bluetooth/hci.h
@@ -1677,10 +1677,22 @@ struct hci_cp_le_set_default_phy {
 	__u8    rx_phys;
 } __packed;
 
+#define HCI_OP_LE_SET_PHY	0x2032
+struct hci_cp_le_set_phy {
+	__le16	handle;
+	__u8	all_phys;
+	__u8	tx_phys;
+	__u8	rx_phys;
+	__le16	phy_opt;
+} __packed;
+
 #define HCI_LE_SET_PHY_1M		0x01
 #define HCI_LE_SET_PHY_2M		0x02
 #define HCI_LE_SET_PHY_CODED		0x04
 
+#define HCI_LE_PHY_CODED_S2		0x01
+#define HCI_LE_PHY_CODED_S8		0x02
+
 #define HCI_LE_READ_PHY_1M		0x01
 #define HCI_LE_READ_PHY_2M		0x02
 #define HCI_LE_READ_PHY_CODED		0x03
diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index a53e94459ecd..257467f9d28d 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -1702,6 +1702,7 @@ struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
 			     const void *param, u32 timeout);
 
 u32 hci_conn_get_phy(struct hci_conn *conn);
+int hci_conn_set_phy(struct hci_conn *conn, u32 phys);
 
 /* ----- HCI Sockets ----- */
 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index 383efd969840..3b69c7cb9523 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -1935,3 +1935,59 @@ u32 hci_conn_get_phy(struct hci_conn *conn)
 
 	return phys;
 }
+
+int hci_conn_set_phy(struct hci_conn *conn, u32 phys)
+{
+	struct hci_cp_le_set_phy cp_phy;
+	u16 phy_opt = 0;
+
+	if (conn->type != LE_LINK)
+		return -ENOTCONN;
+
+	/* Check whether HCI_LE_Set_PHY command is supported or not */
+	if (!(conn->hdev->commands[35] & 0x40))
+		return -EOPNOTSUPP;
+
+	hci_dev_lock(conn->hdev);
+
+	memset(&cp_phy, 0, sizeof(cp_phy));
+	cp_phy.handle = cpu_to_le16(conn->handle);
+
+	if (!(phys & BT_PHY_LE_TX_MASK))
+		cp_phy.all_phys |= 0x01;
+
+	if (!(phys & BT_PHY_LE_RX_MASK))
+		cp_phy.all_phys |= 0x02;
+
+	if (phys & BT_PHY_LE_1M_TX)
+		cp_phy.tx_phys |= HCI_LE_SET_PHY_1M;
+
+	if (phys & BT_PHY_LE_2M_TX)
+		cp_phy.tx_phys |= HCI_LE_SET_PHY_2M;
+
+	if (phys & BT_PHY_LE_CODED_TX)
+		cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED;
+
+	if (phys & BT_PHY_LE_1M_RX)
+		cp_phy.rx_phys |= HCI_LE_SET_PHY_1M;
+
+	if (phys & BT_PHY_LE_2M_RX)
+		cp_phy.rx_phys |= HCI_LE_SET_PHY_2M;
+
+	if (phys & BT_PHY_LE_CODED_RX)
+		cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED;
+
+	if (phys & BT_PHY_LE_CODED_S2)
+		phy_opt |= HCI_LE_PHY_CODED_S2;
+
+	if (phys & BT_PHY_LE_CODED_S8)
+		phy_opt |= HCI_LE_PHY_CODED_S8;
+
+	cp_phy.phy_opt = cpu_to_le16(phy_opt);
+
+	hci_send_cmd(conn->hdev, HCI_OP_LE_SET_PHY, sizeof(cp_phy), &cp_phy);
+
+	hci_dev_unlock(conn->hdev);
+
+	return 0;
+}
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index c99d65ef13b1..eda5d61e37be 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -875,7 +875,7 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
 	struct bt_power pwr;
 	struct l2cap_conn *conn;
 	int len, err = 0;
-	u32 opt;
+	u32 opt, phys;
 
 	BT_DBG("sk %p", sk);
 
@@ -1071,6 +1071,25 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
 
 		break;
 
+	case BT_PHY:
+		if (sk->sk_state != BT_CONNECTED) {
+			err = -ENOTCONN;
+			break;
+		}
+
+		if (copy_from_sockptr(&phys, optval, sizeof(u32))) {
+			err = -EFAULT;
+			break;
+		}
+
+		err = hci_conn_set_phy(chan->conn->hcon, phys);
+
+		if (err)
+			break;
+
+		BT_DBG("phys %u", phys);
+		break;
+
 	case BT_MODE:
 		if (!enable_ecred) {
 			err = -ENOPROTOOPT;
-- 
2.17.1


  parent reply	other threads:[~2021-07-22  6:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20210722053858epcas5p2ecff386b264d4d8578a0ee454f898880@epcas5p2.samsung.com>
2021-07-22  5:38 ` [PATCH 0/4] Bluetooth: Support for LE Set Preferred PHY Ayush Garg
     [not found]   ` <CGME20210722053859epcas5p4b9441d43196ee1111b33b7c8d4827262@epcas5p4.samsung.com>
2021-07-22  5:38     ` [PATCH 1/4] Bluetooth: Fix incorrect parsing of LE_PHY params Ayush Garg
2021-07-22  7:20       ` Bluetooth: Support for LE Set Preferred PHY bluez.test.bot
     [not found]   ` <CGME20210722053900epcas5p48430eb1335c3e566be02135e63749f33@epcas5p4.samsung.com>
2021-07-22  5:38     ` Ayush Garg [this message]
     [not found]   ` <CGME20210722053902epcas5p4a38e75d06dcebec336ac1b1743c1a540@epcas5p4.samsung.com>
2021-07-22  5:38     ` [PATCH 3/4] Bluetooth: Add support for sending LE PHY Update event over mgmt Ayush Garg
2021-07-22 14:43       ` Marcel Holtmann
     [not found]   ` <CGME20210722053903epcas5p4e25daf9341dca446a0fbfd7b5d54324c@epcas5p4.samsung.com>
2021-07-22  5:38     ` [PATCH 4/4] Bluetooth: Update LE TX & RX PHYs when connection established Ayush Garg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210722053843.6691-3-ayush.garg@samsung.com \
    --to=ayush.garg@samsung.com \
    --cc=anupam.r@samsung.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=nitin.j@samsung.com \
    --subject='Re: [PATCH 2/4] Bluetooth: Implement BT_PHY setsocket option' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).