From patchwork Wed Jan 18 18:05:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Herring X-Patchwork-Id: 91820 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp1137518qgi; Wed, 18 Jan 2017 10:05:11 -0800 (PST) X-Received: by 10.99.173.14 with SMTP id g14mr5396365pgf.176.1484762711894; Wed, 18 Jan 2017 10:05:11 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x128si912710pfd.87.2017.01.18.10.05.11; Wed, 18 Jan 2017 10:05:11 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752898AbdARSFH (ORCPT + 25 others); Wed, 18 Jan 2017 13:05:07 -0500 Received: from mail-oi0-f67.google.com ([209.85.218.67]:34205 "EHLO mail-oi0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752688AbdARSFE (ORCPT ); Wed, 18 Jan 2017 13:05:04 -0500 Received: by mail-oi0-f67.google.com with SMTP id w144so1464517oiw.1; Wed, 18 Jan 2017 10:05:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=L3XzIXE+XeYMexjGGhwPBcycDasX2TUSy9zQtAY98Bc=; b=ZM1xXdG0lJRqKBKUK/go1iMY6/tjAIlOEysFZqmG10EukM9lgVRyn5kWKlYwNMt/JD VcSeBaKyM/4kebUsGA2ju+Aegqz9jNepg7NHAoeAbDJp4attrCoRm+GmB3a3DF5DkzYm 7G546VdhNxsXTGNrKzGrHvXpkja3ifrbP9NERS7n0NMManrs8FvK8lk0k8ddL6iwxnFE ELIoI2rfd+BtlapWi2fTGj3JweExqwe9sZSI8Q2tvJ7s1d58k2baOpjbPXmWSbk7H6Cn PcJc0m51S6bkgbYFbnybiznUWREYjHgufZhc2wFKFsEeMFEcsiBL9m4aBHt644/um38E AxYQ== X-Gm-Message-State: AIkVDXL4bLlHcLjT9MfELFXVH4cbkW/IaeHOrYt8vXReniOh4iXfQYgTXvRJye6hCLJAiw== X-Received: by 10.157.8.83 with SMTP id 77mr2112930oty.170.1484762703840; Wed, 18 Jan 2017 10:05:03 -0800 (PST) Received: from rob-hp-laptop.herring.priv (72-48-98-129.dyn.grandenetworks.net. [72.48.98.129]) by smtp.googlemail.com with ESMTPSA id r41sm490765otc.40.2017.01.18.10.05.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Jan 2017 10:05:03 -0800 (PST) From: Rob Herring To: Marcel Holtmann Cc: linux-kernel@vger.kernel.org, john.stultz@linaro.org, Eyal Reizer , Gigi Joseph , Gustavo Padovan , Johan Hedberg , linux-bluetooth@vger.kernel.org Subject: [RFC 2/2] bluetooth: hci_uart: add LL protocol serdev driver support Date: Wed, 18 Jan 2017 12:05:00 -0600 Message-Id: <20170118180500.7791-3-robh@kernel.org> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20170118180500.7791-1-robh@kernel.org> References: <20170118180500.7791-1-robh@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Turns out that the LL protocol and the TI-ST are the same thing AFAICT. The TI-ST adds firmware loading, GPIO control, and shared access for NFC, FM radio, etc. For now, we're only implementing what is needed for BT. This mirrors other drivers like BCM and Intel, but uses the new serdev bus. The firmware loading is greatly simplified by using existing infrastructure to send commands. It may be a bit slower than the original code using synchronous functions, but the real bottleneck is likely doing firmware load at 115.2kbps. Signed-off-by: Rob Herring Cc: Marcel Holtmann Cc: Gustavo Padovan Cc: Johan Hedberg Cc: linux-bluetooth@vger.kernel.org --- drivers/bluetooth/hci_ll.c | 248 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 247 insertions(+), 1 deletion(-) -- 2.10.1 diff --git a/drivers/bluetooth/hci_ll.c b/drivers/bluetooth/hci_ll.c index 02692fe30279..716c610fa54a 100644 --- a/drivers/bluetooth/hci_ll.c +++ b/drivers/bluetooth/hci_ll.c @@ -34,20 +34,23 @@ #include #include #include +#include #include #include #include #include -#include #include #include #include #include +#include #include +#include #include #include +#include #include "hci_uart.h" @@ -76,6 +79,12 @@ struct hcill_cmd { u8 cmd; } __packed; +struct ll_device { + struct hci_uart hu; + struct serdev_device *serdev; + struct gpio_desc *nshutdown; +}; + struct ll_struct { unsigned long rx_state; unsigned long rx_count; @@ -164,6 +173,11 @@ static int ll_close(struct hci_uart *hu) kfree_skb(ll->rx_skb); + if (hu->serdev) { + struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev); + gpiod_set_value_cansleep(lldev->nshutdown, 1); + } + hu->priv = NULL; kfree(ll); @@ -171,6 +185,184 @@ static int ll_close(struct hci_uart *hu) return 0; } +static int read_local_version(struct hci_dev *hdev) +{ + int err = 0; + unsigned short version = 0; + struct sk_buff *skb; + struct hci_rp_read_local_version *ver; + + skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, HCI_INIT_TIMEOUT); + if (IS_ERR(skb)) { + bt_dev_err(hdev, "Reading TI version information failed (%ld)", + PTR_ERR(skb)); + err = PTR_ERR(skb); + goto out; + } + if (skb->len != sizeof(*ver)) { + err = -EILSEQ; + goto out; + } + + ver = (struct hci_rp_read_local_version *)skb->data; + if (le16_to_cpu(ver->manufacturer) != 13) { + err = -ENODEV; + goto out; + } + + version = le16_to_cpu(ver->lmp_subver); + +out: + if (err) bt_dev_err(hdev, "Failed to read TI version info: %d", err); + kfree_skb(skb); + return err ? err : version; +} + +/** + * download_firmware - + * internal function which parses through the .bts firmware + * script file intreprets SEND, DELAY actions only as of now + */ +static int download_firmware(struct ll_device *lldev) +{ + unsigned short chip, min_ver, maj_ver; + int version, err, len; + unsigned char *ptr, *action_ptr; + unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */ + const struct firmware *fw; + struct sk_buff *skb; + struct hci_command *cmd; + + version = read_local_version(lldev->hu.hdev); + if (version < 0) + return version; + + chip = (version & 0x7C00) >> 10; + min_ver = (version & 0x007F); + maj_ver = (version & 0x0380) >> 7; + if (version & 0x8000) + maj_ver |= 0x0008; + + snprintf(bts_scr_name, sizeof(bts_scr_name), + "ti-connectivity/TIInit_%d.%d.%d.bts", + chip, maj_ver, min_ver); + + err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev); + if (err || !fw->data || !fw->size) { + pr_err(" request_firmware failed(errno %d) for %s", err, + bts_scr_name); + return -EINVAL; + } + ptr = (void *)fw->data; + len = fw->size; + /* bts_header to remove out magic number and + * version + */ + ptr += sizeof(struct bts_header); + len -= sizeof(struct bts_header); + + while (len > 0 && ptr) { + pr_debug(" action size %d, type %d ", + ((struct bts_action *)ptr)->size, + ((struct bts_action *)ptr)->type); + + action_ptr = &(((struct bts_action *)ptr)->data[0]); + + switch (((struct bts_action *)ptr)->type) { + case ACTION_SEND_COMMAND: /* action send */ + pr_debug("S"); + cmd = (struct hci_command *)action_ptr; + if (cmd->opcode == 0xff36) { + /* ignore remote change + * baud rate HCI VS command */ + pr_warn("change remote baud" + " rate command in firmware"); + break; + } + if (cmd->prefix != 1) + printk("command type %d\n", cmd->prefix); + + skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen, &cmd->speed, HCI_INIT_TIMEOUT); + if (IS_ERR(skb)) { + printk("send command failed\n"); + goto out_rel_fw; + } + kfree_skb(skb); + break; + case ACTION_WAIT_EVENT: /* wait */ + /* no need to wait as command was synchronous */ + pr_debug("W"); + break; + case ACTION_DELAY: /* sleep */ + pr_info("sleep command in scr"); + mdelay(((struct bts_action_delay *)action_ptr)->msec); + break; + } + len -= (sizeof(struct bts_action) + + ((struct bts_action *)ptr)->size); + ptr += sizeof(struct bts_action) + + ((struct bts_action *)ptr)->size; + } + +out_rel_fw: + /* fw download complete */ + release_firmware(fw); + return err; +} + +static int ll_setup(struct hci_uart *hu) +{ + int err, retry = 3; + struct ll_device *lldev; + u32 speed; + + if (!hu->serdev) + return 0; + + lldev = serdev_device_get_drvdata(hu->serdev); + + serdev_device_set_flow_control(hu->serdev, true); + + do { + /* Configure BT nShutdown to HIGH state */ + gpiod_set_value_cansleep(lldev->nshutdown, 1); + msleep(5); /* FIXME: a proper toggle */ + gpiod_set_value_cansleep(lldev->nshutdown, 0); + msleep(100); + + err = download_firmware(lldev); + if (err != 0) { + /* ldisc installed but fw download failed, + * flush uart & power cycle BT_EN */ + pr_err("download firmware failed"); + continue; + } else { /* on success don't retry */ + break; + } + } while (retry--); + + if (err) + return err; + + /* Operational speed if any */ + if (hu->oper_speed) + speed = hu->oper_speed; + else if (hu->proto->oper_speed) + speed = hu->proto->oper_speed; + else + speed = 0; + + if (speed) { + struct sk_buff *skb = __hci_cmd_sync(hu->hdev, 0xff36, sizeof(speed), &speed, HCI_INIT_TIMEOUT); + if (!IS_ERR(skb)) { + kfree_skb(skb); + serdev_device_set_baudrate(hu->serdev, speed); + } + } + + return 0; +} + /* * internal function, which does common work of the device wake up process: * 1. places all pending packets (waiting in tx_wait_q list) in txq list. @@ -508,6 +700,7 @@ static struct sk_buff *ll_dequeue(struct hci_uart *hu) static const struct hci_uart_proto llp = { .id = HCI_UART_LL, .name = "LL", + .setup = ll_setup, .open = ll_open, .close = ll_close, .recv = ll_recv, @@ -525,3 +718,56 @@ int __exit ll_deinit(void) { return hci_uart_unregister_proto(&llp); } + +static int hci_ti_probe(struct serdev_device *serdev) +{ + struct hci_uart *hu; + struct ll_device *lldev; + u32 max_speed = 3000000; + + lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL); + if (!lldev) + return -ENOMEM; + hu = &lldev->hu; + + serdev_device_set_drvdata(serdev, lldev); + lldev->serdev = hu->serdev = serdev; + + lldev->nshutdown = devm_gpiod_get(&serdev->dev, "shutdown", GPIOD_OUT_HIGH); + if (IS_ERR(lldev->nshutdown)) + return PTR_ERR(lldev->nshutdown); + + of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed); + hci_uart_set_speeds(hu, 115200, max_speed); + + return hci_uart_register_device(hu, &llp); +} + +static void hci_ti_remove(struct serdev_device *serdev) +{ + struct ll_device *lldev = serdev_device_get_drvdata(serdev); + struct hci_uart *hu = &lldev->hu; + struct hci_dev *hdev = hu->hdev; + + cancel_work_sync(&hu->write_work); + + hci_unregister_dev(hdev); + hci_free_dev(hdev); + hu->proto->close(hu); +} + +static const struct of_device_id hci_ti_of_match[] = { + { .compatible = "ti,wl1835-st" }, + {}, +}; +MODULE_DEVICE_TABLE(of, hci_ti_of_match); + +static struct serdev_device_driver hci_ti_drv = { + .driver = { + .name = "hci-ti", + .of_match_table = of_match_ptr(hci_ti_of_match), + }, + .probe = hci_ti_probe, + .remove = hci_ti_remove, +}; +module_serdev_device_driver(hci_ti_drv);