说明
本文档是基于Android 8.1版本的一款博通蓝牙的驱动移植全过程记录。本驱动是通过/dev/ttyS2和蓝牙进行通信。因为较android以前的版本有很大变化尤其是上层关于蓝牙协议栈的部分,以前老版本用Bluez再到后来的BlueDroid,到现在的集到/system/bt下。蓝牙的固件烧录方式也有变化,以前broadcom蓝牙用systembluetoothbrcm_patchram_plus目录下编译出的可执行程序brcm_patchram_plus进行.hcd格式的蓝牙固件烧录,现在android 8.1版本是由hardware/broadcom/libbt/src/下的工程进行固件烧录和pskey(蓝牙寄存器初始化配置)设置。
一 、软硬件平台
系统软件:android-8.1、linux-4.4.117
硬件平台:Ti jacinto6
蓝牙芯片:cyw89342(bcm89342)
二、蓝牙移植流程
2.1 kernel 对蓝牙的驱动支持配置
对蓝牙的驱动支持配置((UART)H4 、BCSP、HCILL等)要全部确保勾选:
Bluetooth subsystem support —>
RF switch subsystem support —>
2.2 kernel层编写蓝牙电源管理(bt rfkill)驱动
将蓝牙电源管理添加到linux rfkill(无线设备电源开关)子系统。主要是通过bt_reg_on 这个gpio管脚控制蓝牙的上电、下电。添加rfkill驱动后,就可以在命令行执行echo操作进行蓝牙的开启和关闭。因为蓝牙设备默认是 /sys/class/rfkill/rfkill0所以通过:cat /sys/class/rfkill/rfkill0/name 确认是否是自己驱动创建的rfkill设备,我这里是bcm89342_bt_rfkill。(详细驱动代码见下载文件)确认后可执行开关操作:
蓝牙上电: echo 1 > /sys/class/rfkill/rfkill0/state
蓝牙下电 : echo 0 > /sys/class/rfkill/rfkill0/state
rfkill驱动bcm89342_bt_rfkill.c部分源代码如下:
#include
#include
#include
#include
#include
#include
#define BT_REG_ON 169
static int rfkill_set_power(void *data, bool blocked);
static struct platform_device *rfkill_pdev;
static struct rfkill *rfkill; /* for driver only */
static struct rfkill_ops rfkill_ops = {
.set_block = rfkill_set_power,
};
static int bt_gpio_init(void)
{
int ret;
ret = gpio_request_one(BT_REG_ON, GPIOF_OUT_INIT_LOW, "BT_REG_ON");
if (ret == 0) {
printk("Yangql obtain gpio for BT_REG_ON n");
gpio_export(BT_REG_ON, 0);
} else {
pr_err("could not obtain gpio for BT_REG_ONn");
}
#if 0
ret = gpio_request_one(BT_HOST_WAKE, GPIOF_IN, "BT_HOST_WAKE");
<