/*
*
* Bluetooth HCI UART driver for Intel devices
*
* Copyright (C) 2015 Intel Corporation
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/firmware.h>
#include <linux/module.h>
#include <linux/wait.h>
#include <linux/tty.h>
#include <linux/platform_device.h>
#include <linux/gpio/consumer.h>
#include <linux/acpi.h>
#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>
#include "hci_uart.h"
#include "btintel.h"
#define STATE_BOOTLOADER 0
#define STATE_DOWNLOADING 1
#define STATE_FIRMWARE_LOADED 2
#define STATE_FIRMWARE_FAILED 3
#define STATE_BOOTING 4
struct intel_device {
struct list_head list;
struct platform_device *pdev;
struct gpio_desc *reset;
};
static LIST_HEAD(intel_device_list);
static DEFINE_SPINLOCK(intel_device_list_lock);
struct intel_data {
struct sk_buff *rx_skb;
struct sk_buff_head txq;
unsigned long flags;
};
static u8 intel_convert_speed(unsigned int speed)
{
switch (speed) {
case 9600:
return 0x00;
case 19200:
return 0x01;
case 38400:
return 0x02;
case 57600:
return 0x03;
case 115200:
return 0x04;
case 230400:
return 0x05;
case 460800:
return 0x06;
case 921600:
return 0x07;
case 1843200:
return 0x08;
case 3250000:
return 0x09;
case 2000000:
return 0x0a;
case 3000000:
return 0x0b;
default:
return 0xff;
}
}
static int intel_wait_booting(struct hci_uart *hu)
{
struct intel_data *intel = hu->priv;
int err;
err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
TASK_INTERRUPTIBLE,
msecs_to_jiffies(1000));
if (err == 1) {
BT_ERR("%s: Device boot interrupted", hu->hdev->name);
return -EINTR;
}
if (err) {
BT_ERR("%s: Device boot timeout", hu->hdev->name);
return -ETIMEDOUT;
}
return err;
}
static int intel_set_power(struct hci_uart *hu, bool powered)
{
struct list_head *p;
int err = -ENODEV;
spin_lock(&intel_device_list_lock);
list_for_each(p, &intel_device_list) {
struct intel_device *idev = list_entry(p, struct intel_device,
list);
/* tty device and pdev device should share the same parent
* which is the UART port.
*/
if (hu->tty->dev->parent != idev->pdev->dev.parent)
continue;
if (!idev->reset) {
err = -ENOTSUPP;
break;
}
BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
hu, dev_name(&idev->pdev->dev), powered);
gpiod_set_value(idev->reset, powered);
}
spin_unlock(&intel_device_list_lock);
return err;
}
static int intel_open(struct hci_uart *hu)
{
struct intel_data *intel;
BT_DBG("hu %p", hu);
intel = kzalloc(sizeof(*intel), GFP_KERNEL);
if (!intel)
return -ENOMEM;
skb_queue_head_init(&intel->txq);
hu->priv = intel;
if (!intel_set_power(hu, true))
set_bit(STATE_BOOTING, &intel->flags);
return 0;
}
static int intel_close(struct hci_uart *hu)
{
struct intel_data *intel = hu->priv;
BT_DBG("hu %p", hu);
intel_set_power(hu, false);
skb_queue_purge(&intel->txq);
kfree_skb(intel->rx_skb);
kfree(intel);
hu->priv = NULL;
return 0;
}
static int intel_flush(struct hci_uart *hu)
{
struct intel_data *intel = hu->priv;
BT_DBG("hu %p", hu);
skb_queue_purge(&intel->txq);
return 0;
}
static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
{
struct sk_buff *skb;
struct hci_event_hdr *hdr;
struct hci_ev_cmd_complete *evt;
skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
if (!skb)
return -ENOMEM;
hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
hdr->evt = HCI_EV_CMD_COMPLETE;
hdr-&