Bluetooth: Introduce HCI Driver protocol
Subsystems (4)
PATCH v2 1/4 Bluetooth: Introduce HCI Driver protocol
From: Hsin-chen Chuang <[email protected]>
Although commit 75ddcd5ad40e ("Bluetooth: btusb: Configure altsetting for HCI_USER_CHANNEL") has enabled the HCI_USER_CHANNEL user to send out SCO data through USB Bluetooth chips, it's observed that with the patch HFP is flaky on most of the existing USB Bluetooth controllers: Intel chips sometimes send out no packet for Transparent codec; MTK chips may generate SCO data with a wrong handle for CVSD codec; RTK could split the data with a wrong packet size for Transparent codec; ... etc.
To address the issue above one needs to reset the altsetting back to zero when there is no active SCO connection, which is the same as the BlueZ behavior, and another benefit is the bus doesn't need to reserve bandwidth when no SCO connection.
This patch adds the infrastructure that allow the user space program to talk to Bluetooth drivers directly:
- Define the new packet type HCI_DRV_PKT which is specifically used for communication between the user space program and the Bluetooth drviers
- hci_send_frame intercepts the packets and invokes drivers' HCI Drv callbacks (so far only defined for btusb)
- 2 kinds of events to user space: Command Status and Command Complete, the former simply returns the status while the later may contain additional response data.
---
Changes in v2:
- Specifying drvier specific commands by hci_opcode_ogf
drivers/bluetooth/btusb.c | 69 ++++++++++++++++++-- include/net/bluetooth/hci.h | 1 + include/net/bluetooth/hci_core.h | 3 + include/net/bluetooth/hci_drv.h | 76 ++++++++++++++++++++++ include/net/bluetooth/hci_mon.h | 2 + net/bluetooth/Makefile | 3 +- net/bluetooth/hci_core.c | 10 +++ net/bluetooth/hci_drv.c | 104 +++++++++++++++++++++++++++++++ net/bluetooth/hci_sock.c | 12 +++- 9 files changed, 271 insertions(+), 9 deletions(-) create mode 100644 include/net/bluetooth/hci_drv.h create mode 100644 net/bluetooth/hci_drv.c
drivers/bluetooth/btusb.c +63 -6
@@ -21,6 +21,7 @@#include <net/bluetooth/bluetooth.h> #include <net/bluetooth/hci_core.h> +#include <net/bluetooth/hci_drv.h> #include "btintel.h" #include "btbcm.h"
@@ -3754,6 +3755,61 @@ static ssize_t isoc_alt_store(struct device *dev,static DEVICE_ATTR_RW (isoc_alt); +static const struct { +u16 opcode; +const char *desc; +} btusb_hci_drv_supported_commands[] = { +/* Common commands */ + {HCI_DRV_OP_READ_INFO ,"Read Info" }, +}; +static int btusb_hci_drv_read_info (struct hci_dev *hdev,void *data, +u16 data_len) +{ +struct hci_drv_rp_read_info *rp; +size_t rp_size; +int err, i; +u16 opcode, num_supported_commands = +ARRAY_SIZE (btusb_hci_drv_supported_commands); + + rp_size =sizeof (*rp) + num_supported_commands *2 ; + + rp =kmalloc (rp_size,GFP_KERNEL ); +if (!rp) +return -ENOMEM ; + +strscpy_pad (rp->driver_name, btusb_driver.name); + + rp->num_supported_commands =cpu_to_le16 (num_supported_commands); +for (i =0 ; i < num_supported_commands; i++) { + opcode = btusb_hci_drv_supported_commands[i].opcode; +bt_dev_info (hdev, +"Supported HCI Drv command (0x%02x|0x%04x): %s" , +hci_opcode_ogf (opcode), +hci_opcode_ocf (opcode), + btusb_hci_drv_supported_commands[i].desc); + rp->supported_commands[i] =cpu_to_le16 (opcode); + } + + err =hci_drv_cmd_complete (hdev,HCI_DRV_OP_READ_INFO , +HCI_DRV_STATUS_SUCCESS , rp, rp_size); + +kfree (rp); +return err; +} + +static const struct hci_drv_handler btusb_hci_drv_common_handlers[] = { + { btusb_hci_drv_read_info,HCI_DRV_READ_INFO_SIZE }, +}; + +static const struct hci_drv_handler btusb_hci_drv_specific_handlers[] = {}; + +static struct hci_drv btusb_hci_drv = { + .common_handler_count =ARRAY_SIZE (btusb_hci_drv_common_handlers), + .common_handlers = btusb_hci_drv_common_handlers, + .specific_handler_count =ARRAY_SIZE (btusb_hci_drv_specific_handlers), + .specific_handlers = btusb_hci_drv_specific_handlers, +}; +static int btusb_probe (struct usb_interface *intf,const struct usb_device_id *id) {
@@ -3893,12 +3949,13 @@ static int btusb_probe(struct usb_interface *intf, data->reset_gpio = reset_gpio; } - hdev->open = btusb_open; - hdev->close = btusb_close; - hdev->flush = btusb_flush; - hdev->send = btusb_send_frame; - hdev->notify = btusb_notify; - hdev->wakeup = btusb_wakeup; + hdev->open = btusb_open; + hdev->close = btusb_close; + hdev->flush = btusb_flush; + hdev->send = btusb_send_frame; + hdev->notify = btusb_notify; + hdev->wakeup = btusb_wakeup; + hdev->hci_drv = &btusb_hci_drv;#ifdef CONFIG_PM err =btusb_config_oob_wake (hdev);
include/net/bluetooth/hci.h +1 -0
@@ -494,6 +494,7 @@ enum {#define HCI_EVENT_PKT 0x04#define HCI_ISODATA_PKT 0x05#define HCI_DIAG_PKT 0xf0 +#define HCI_DRV_PKT 0xf1#define HCI_VENDOR_PKT 0xff/* HCI packet types */
include/net/bluetooth/hci_core.h +3 -0
@@ -31,6 +31,7 @@#include <linux/rculist.h> #include <net/bluetooth/hci.h> +#include <net/bluetooth/hci_drv.h> #include <net/bluetooth/hci_sync.h> #include <net/bluetooth/hci_sock.h> #include <net/bluetooth/coredump.h>
@@ -613,6 +614,8 @@ struct hci_dev {struct list_head monitored_devices;bool advmon_pend_notify; +struct hci_drv *hci_drv; +#if IS_ENABLED (CONFIG_BT_LEDS )struct led_trigger *power_led;#endif
include/net/bluetooth/hci_drv.h new file +76 -0
76 lines not on this page · raw message
include/net/bluetooth/hci_mon.h +2 -0
@@ -51,6 +51,8 @@ struct hci_mon_hdr {#define HCI_MON_CTRL_EVENT 17#define HCI_MON_ISO_TX_PKT 18#define HCI_MON_ISO_RX_PKT 19 +#define HCI_MON_DRV_TX_PKT 20 +#define HCI_MON_DRV_RX_PKT 21struct hci_mon_new_index {__u8 type;
net/bluetooth/Makefile +2 -1
@@ -14,7 +14,8 @@ bluetooth_6lowpan-y := 6lowpan.o bluetooth-y := af_bluetooth.o hci_core.o hci_conn.o hci_event.o mgmt.o \ hci_sock.o hci_sysfs.o l2cap_core.o l2cap_sock.o smp.o lib.o \ - ecdh_helper.o mgmt_util.o mgmt_config.o hci_codec.o eir.o hci_sync.o + ecdh_helper.o mgmt_util.o mgmt_config.o hci_codec.o eir.o hci_sync.o \ + hci_drv.o bluetooth-$(CONFIG_DEV_COREDUMP) += coredump.o
net/bluetooth/hci_core.c +10 -0
@@ -2911,6 +2911,8 @@ int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)break ;case HCI_ISODATA_PKT :break ; +case HCI_DRV_PKT : +break ;default :kfree_skb (skb);return -EINVAL ;
@@ -3019,6 +3021,14 @@ static int hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)return -EINVAL ; } +if (hci_skb_pkt_type (skb) ==HCI_DRV_PKT ) { +// Intercept HCI Drv packet here and don't go with hdev->send +// callabck. + err =hci_drv_process_cmd (hdev, skb); +kfree_skb (skb); +return err; + } + err = hdev->send (hdev, skb);if (err <0 ) {bt_dev_err (hdev,"sending frame failed (%d)" , err);
net/bluetooth/hci_drv.c new file +104 -0
104 lines not on this page · raw message
net/bluetooth/hci_sock.c +10 -2
@@ -234,7 +234,8 @@ void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)if (hci_skb_pkt_type (skb) !=HCI_EVENT_PKT &&hci_skb_pkt_type (skb) !=HCI_ACLDATA_PKT &&hci_skb_pkt_type (skb) !=HCI_SCODATA_PKT && -hci_skb_pkt_type (skb) !=HCI_ISODATA_PKT ) +hci_skb_pkt_type (skb) !=HCI_ISODATA_PKT && +hci_skb_pkt_type (skb) !=HCI_DRV_PKT )continue ; }else {/* Don't send frame to other channel types */
@@ -391,6 +392,12 @@ void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)else opcode =cpu_to_le16 (HCI_MON_ISO_TX_PKT );break ; +case HCI_DRV_PKT : +if (bt_cb (skb)->incoming) + opcode =cpu_to_le16 (HCI_MON_DRV_RX_PKT ); +else + opcode =cpu_to_le16 (HCI_MON_DRV_TX_PKT ); +break ;case HCI_DIAG_PKT : opcode =cpu_to_le16 (HCI_MON_VENDOR_DIAG );break ;
@@ -1860,7 +1867,8 @@ static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,if (hci_skb_pkt_type (skb) !=HCI_COMMAND_PKT &&hci_skb_pkt_type (skb) !=HCI_ACLDATA_PKT &&hci_skb_pkt_type (skb) !=HCI_SCODATA_PKT && -hci_skb_pkt_type (skb) !=HCI_ISODATA_PKT ) { +hci_skb_pkt_type (skb) !=HCI_ISODATA_PKT && +hci_skb_pkt_type (skb) !=HCI_DRV_PKT ) { err = -EINVAL ; goto drop; }
Signature · 1 line
2.49.0.604.gff1f9ca942-goog
PATCH v2 2/4 Bluetooth: btusb: Add HCI Drv commands for configuring altsetting
From: Hsin-chen Chuang <[email protected]>
Although commit 75ddcd5ad40e ("Bluetooth: btusb: Configure altsetting for HCI_USER_CHANNEL") has enabled the HCI_USER_CHANNEL user to send out SCO data through USB Bluetooth chips, it's observed that with the patch HFP is flaky on most of the existing USB Bluetooth controllers: Intel chips sometimes send out no packet for Transparent codec; MTK chips may generate SCO data with a wrong handle for CVSD codec; RTK could split the data with a wrong packet size for Transparent codec; ... etc.
To address the issue above one needs to reset the altsetting back to zero when there is no active SCO connection, which is the same as the BlueZ behavior, and another benefit is the bus doesn't need to reserve bandwidth when no SCO connection.
This patch adds "Supported Altsettings" and "Switch Altsetting" commands that allow the user space program to configure the altsetting freely.
This patch is tested on ChromeOS devices. The USB Bluetooth models (CVSD, TRANS alt3, and TRANS alt6) could pass the stress HFP test narrow band speech and wide band speech.
---
(no changes since v1)
drivers/bluetooth/btusb.c | 72 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-)
drivers/bluetooth/btusb.c +71 -1
@@ -3755,12 +3755,31 @@ static ssize_t isoc_alt_store(struct device *dev,static DEVICE_ATTR_RW (isoc_alt); +#define BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS \ + hci_opcode_pack(HCI_DRV_OGF_DRIVER_SPECIFIC, 0x0000) +#define BTUSB_HCI_DRV_SUPPORTED_ALTSETTINGS_SIZE 0 +struct btusb_hci_drv_rp_supported_altsettings { +__u8 num_supported_altsettings; +__u8 supported_altsettings[]; +} __packed; + +#define BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING \ + hci_opcode_pack(HCI_DRV_OGF_DRIVER_SPECIFIC, 0x0001) +#define BTUSB_HCI_DRV_SWITCH_ALTSETTING_SIZE 1 +struct btusb_hci_drv_cmd_switch_altsetting { +__u8 new_altsetting; +} __packed; +static const struct {u16 opcode;const char *desc; } btusb_hci_drv_supported_commands[] = {/* Common commands */ {HCI_DRV_OP_READ_INFO ,"Read Info" }, + +/* Driver specific commands */ + {BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS ,"Supported Altsettings" }, + {BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING ,"Switch Altsetting" }, };static int btusb_hci_drv_read_info (struct hci_dev *hdev,void *data,u16 data_len)
@@ -3797,11 +3816,62 @@ static int btusb_hci_drv_read_info(struct hci_dev *hdev, void *data,return err; } +static int btusb_hci_drv_supported_altsettings (struct hci_dev *hdev,void *data, +u16 data_len) +{ +struct btusb_data *drvdata =hci_get_drvdata (hdev); +struct btusb_hci_drv_rp_supported_altsettings *rp; +size_t rp_size; +int err; +u8 i; + +/* There are at most 7 alt (0 - 6) */ + rp =kmalloc (sizeof (*rp) +7 ,GFP_KERNEL ); + + rp->num_supported_altsettings =0 ; +if (drvdata->isoc) +for (i =0 ; i <=6 ; i++) +if (btusb_find_altsetting (drvdata, i)) + rp->supported_altsettings[ + rp->num_supported_altsettings++] = i; + + rp_size =sizeof (*rp) + rp->num_supported_altsettings; + + err =hci_drv_cmd_complete (hdev,BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS , +HCI_DRV_STATUS_SUCCESS , rp, rp_size); +kfree (rp); +return err; +} + +static int btusb_hci_drv_switch_altsetting (struct hci_dev *hdev,void *data, +u16 data_len) +{ +struct btusb_hci_drv_cmd_switch_altsetting *cmd = data; +u8 status; + +if (cmd->new_altsetting >6 ) { + status =HCI_DRV_STATUS_INVALID_PARAMETERS ; + }else { +if (btusb_switch_alt_setting (hdev, cmd->new_altsetting)) + status =HCI_DRV_STATUS_UNSPECIFIED_ERROR ; +else + status =HCI_DRV_STATUS_SUCCESS ; + } + +return hci_drv_cmd_status (hdev,BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING , + status); +} +static const struct hci_drv_handler btusb_hci_drv_common_handlers[] = { { btusb_hci_drv_read_info,HCI_DRV_READ_INFO_SIZE }, }; -static const struct hci_drv_handler btusb_hci_drv_specific_handlers[] = {}; +static const struct hci_drv_handler btusb_hci_drv_specific_handlers[] = { + { btusb_hci_drv_supported_altsettings, +BTUSB_HCI_DRV_SUPPORTED_ALTSETTINGS_SIZE }, + { btusb_hci_drv_switch_altsetting, +BTUSB_HCI_DRV_SWITCH_ALTSETTING_SIZE }, +};static struct hci_drv btusb_hci_drv = { .common_handler_count =ARRAY_SIZE (btusb_hci_drv_common_handlers),
Signature · 1 line
2.49.0.604.gff1f9ca942-goog
PATCH v2 3/4 Revert "Bluetooth: btusb: Configure altsetting for HCI_USER_CHANNEL"
In reply to Hsin-chen ChuangFrom: Hsin-chen Chuang <[email protected]>
This reverts commit 75ddcd5ad40ecd9fbc9f5a7a2ed0e1e74921db3c.
This patch doesn't work quite well - It's observed that with this patch HFP is flaky on most of the existing USB Bluetooth controllers: Intel chips sometimes send out no packet for Transparent codec; MTK chips may generate SCO data with a wrong handle for CVSD codec; RTK could split the data with a wrong packet size for Transparent codec; ... etc.
---
(no changes since v1)
drivers/bluetooth/Kconfig | 12 ------------ drivers/bluetooth/btusb.c | 41 --------------------------------------- 2 files changed, 53 deletions(-)
drivers/bluetooth/Kconfig +0 -12
@@ -56,18 +56,6 @@ config BT_HCIBTUSB_POLL_SYNC Say Y here to enable USB poll_sync for Bluetooth USB devices by default. -config BT_HCIBTUSB_AUTO_ISOC_ALT - bool "Automatically adjust alternate setting for Isoc endpoints" - depends on BT_HCIBTUSB - default y if CHROME_PLATFORMS - help - Say Y here to automatically adjusting the alternate setting for - HCI_USER_CHANNEL whenever a SCO link is established. - - When enabled, btusb intercepts the HCI_EV_SYNC_CONN_COMPLETE packets - and configures isoc endpoint alternate setting automatically when - HCI_USER_CHANNEL is in use. - config BT_HCIBTUSB_BCM bool "Broadcom protocol support" depends on BT_HCIBTUSB
drivers/bluetooth/btusb.c +0 -41
59 lines not on this page · raw message
Signature · 1 line
2.49.0.604.gff1f9ca942-goog
PATCH v2 4/4 Revert "Bluetooth: btusb: add sysfs attribute to control USB alt setting"
In reply to Hsin-chen ChuangFrom: Hsin-chen Chuang <[email protected]>
This reverts commit b16b327edb4d030fb4c8fe38c7d299074d47ee3f.
The sysfs node introduced by this patch could potentially race with user space. The original motivation - Support configuring altsetting from the user space will be added by another series.
---
(no changes since v1)
drivers/bluetooth/btusb.c | 34 +--------------------------------- 1 file changed, 1 insertion(+), 33 deletions(-)
drivers/bluetooth/btusb.c +1 -33
@@ -3688,32 +3688,6 @@ static const struct file_operations force_poll_sync_fops = { .llseek = default_llseek, }; -static ssize_t isoc_alt_show (struct device *dev, -struct device_attribute *attr, -char *buf) -{ -struct btusb_data *data =dev_get_drvdata (dev); - -return sysfs_emit (buf,"%d\n" , data->isoc_altsetting); -} - -static ssize_t isoc_alt_store (struct device *dev, -struct device_attribute *attr, -const char *buf,size_t count) -{ -struct btusb_data *data =dev_get_drvdata (dev); -int alt; -int ret; - -if (kstrtoint (buf,10 , &alt)) -return -EINVAL ; - - ret =btusb_switch_alt_setting (data->hdev, alt); -return ret <0 ? ret : count; -} - -static DEVICE_ATTR_RW (isoc_alt); -#define BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS \ hci_opcode_pack(HCI_DRV_OGF_DRIVER_SPECIFIC, 0x0000)#define BTUSB_HCI_DRV_SUPPORTED_ALTSETTINGS_SIZE 0
@@ -4203,10 +4177,6 @@ static int btusb_probe(struct usb_interface *intf, data->isoc, data);if (err <0 ) goto out_free_dev; - - err =device_create_file (&intf->dev, &dev_attr_isoc_alt); -if (err) - goto out_free_dev; }if (IS_ENABLED (CONFIG_BT_HCIBTUSB_BCM ) && data->diag) {
@@ -4253,10 +4223,8 @@ static void btusb_disconnect(struct usb_interface *intf) hdev = data->hdev;usb_set_intfdata (data->intf,NULL ); -if (data->isoc) { -device_remove_file (&intf->dev, &dev_attr_isoc_alt); +if (data->isoc)usb_set_intfdata (data->isoc,NULL ); - }if (data->diag)usb_set_intfdata (data->diag,NULL );
Signature · 1 line
2.49.0.604.gff1f9ca942-goog
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=953913
---Test result---
Test Summary:
CheckPatch PENDING 0.43 seconds GitLint PENDING 0.33 seconds SubjectPrefix PASS 3.42 seconds BuildKernel PASS 25.58 seconds CheckAllWarning PASS 28.36 seconds CheckSparse PASS 40.61 seconds BuildKernel32 PASS 32.78 seconds TestRunnerSetup PASS 484.26 seconds TestRunner_l2cap-tester PASS 22.46 seconds TestRunner_iso-tester PASS 37.98 seconds TestRunner_bnep-tester PASS 4.95 seconds TestRunner_mgmt-tester FAIL 126.63 seconds TestRunner_rfcomm-tester PASS 8.44 seconds TestRunner_sco-tester PASS 28.71 seconds TestRunner_ioctl-tester PASS 8.94 seconds TestRunner_mesh-tester PASS 6.37 seconds TestRunner_smp-tester PASS 7.56 seconds TestRunner_userchan-tester PASS 7.87 seconds IncrementalBuild PENDING 0.64 seconds
Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script Output:
##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner Output:
Total: 490, Passed: 485 (99.0%), Failed: 1, Not Run: 4
Failed Test Cases
LL Privacy - Set Flags 3 (2 Devices to RL) Failed 0.180 seconds
##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series Output:
---
Regards,
Linux Bluetooth
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 11 lines · On Wed, 16 Apr 2025 09:53:35 +0000 you wrote:
From: Hsin-chen Chuang <[email protected]>
Although commit
75ddcd5ad40e("Bluetooth: btusb: Configure altsetting forHCI_USER_CHANNEL") has enabled theHCI_USER_CHANNELuser to send out SCO data through USB Bluetooth chips, it's observed that with the patch HFP is flaky on most of the existing USB Bluetooth controllers: Intel chips sometimes send out no packet for Transparent codec; MTK chips may generate SCO data with a wrong handle for CVSD codec; RTK could split the data with a wrong packet size for Transparent codec; ... etc.[...]
Here is the summary with links:
- [v2,1/4] Bluetooth: Introduce HCI Driver protocol
(no matching commit)
- [v2,2/4] Bluetooth: btusb: Add HCI Drv commands for configuring altsetting (no matching commit)
- [v2,3/4] Revert "Bluetooth: btusb: Configure altsetting for HCI_USER_CHANNEL"
https://git.kernel.org/bluetooth/bluetooth-next/c/3947fa617367 - [v2,4/4] Revert "Bluetooth: btusb: add sysfs attribute to control USB alt setting" https://git.kernel.org/bluetooth/bluetooth-next/c/79f21135b7b0
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html