arm64: dts: rk3368: add iep device node
[firefly-linux-kernel-4.4.55.git] / drivers / bluetooth / hci_ldisc.c
index bc68a440d432cd21b2ee10885e7106cfe2852b79..61bb2192a80b01b67b3ef5bf33031fe36ef819cb 100644 (file)
 #include <linux/signal.h>
 #include <linux/ioctl.h>
 #include <linux/skbuff.h>
+#include <linux/firmware.h>
 
 #include <net/bluetooth/bluetooth.h>
 #include <net/bluetooth/hci_core.h>
 
+#include "btintel.h"
+#include "btbcm.h"
 #include "hci_uart.h"
 
-#define VERSION "2.2"
+#define VERSION "2.3"
 
-static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
+static const struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
 
-int hci_uart_register_proto(struct hci_uart_proto *p)
+int hci_uart_register_proto(const struct hci_uart_proto *p)
 {
        if (p->id >= HCI_UART_MAX_PROTO)
                return -EINVAL;
@@ -60,10 +63,12 @@ int hci_uart_register_proto(struct hci_uart_proto *p)
 
        hup[p->id] = p;
 
+       BT_INFO("HCI UART protocol %s registered", p->name);
+
        return 0;
 }
 
-int hci_uart_unregister_proto(struct hci_uart_proto *p)
+int hci_uart_unregister_proto(const struct hci_uart_proto *p)
 {
        if (p->id >= HCI_UART_MAX_PROTO)
                return -EINVAL;
@@ -76,7 +81,7 @@ int hci_uart_unregister_proto(struct hci_uart_proto *p)
        return 0;
 }
 
-static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
+static const struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
 {
        if (id >= HCI_UART_MAX_PROTO)
                return NULL;
@@ -118,10 +123,6 @@ static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
 
 int hci_uart_tx_wakeup(struct hci_uart *hu)
 {
-       struct tty_struct *tty = hu->tty;
-       struct hci_dev *hdev = hu->hdev;
-       struct sk_buff *skb;
-
        if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
                set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
                return 0;
@@ -129,6 +130,22 @@ int hci_uart_tx_wakeup(struct hci_uart *hu)
 
        BT_DBG("");
 
+       schedule_work(&hu->write_work);
+
+       return 0;
+}
+
+static void hci_uart_write_work(struct work_struct *work)
+{
+       struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
+       struct tty_struct *tty = hu->tty;
+       struct hci_dev *hdev = hu->hdev;
+       struct sk_buff *skb;
+
+       /* REVISIT: should we cope with bad skbs or ->write() returning
+        * and error value ?
+        */
+
 restart:
        clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
 
@@ -153,7 +170,6 @@ restart:
                goto restart;
 
        clear_bit(HCI_UART_SENDING, &hu->tx_state);
-       return 0;
 }
 
 static void hci_uart_init_work(struct work_struct *work)
@@ -192,9 +208,6 @@ static int hci_uart_open(struct hci_dev *hdev)
        BT_DBG("%s %p", hdev->name, hdev);
 
        /* Nothing to do for UART driver */
-
-       set_bit(HCI_RUNNING, &hdev->flags);
-
        return 0;
 }
 
@@ -214,7 +227,7 @@ static int hci_uart_flush(struct hci_dev *hdev)
        tty_ldisc_flush(tty);
        tty_driver_flush_buffer(tty);
 
-       if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
+       if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
                hu->proto->flush(hu);
 
        return 0;
@@ -225,36 +238,192 @@ static int hci_uart_close(struct hci_dev *hdev)
 {
        BT_DBG("hdev %p", hdev);
 
-       if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
-               return 0;
-
        hci_uart_flush(hdev);
        hdev->flush = NULL;
        return 0;
 }
 
 /* Send frames from HCI layer */
-static int hci_uart_send_frame(struct sk_buff *skb)
+static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
 {
-       struct hci_dev* hdev = (struct hci_dev *) skb->dev;
-       struct hci_uart *hu;
+       struct hci_uart *hu = hci_get_drvdata(hdev);
 
-       if (!hdev) {
-               BT_ERR("Frame for unknown device (hdev=NULL)");
-               return -ENODEV;
+       BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
+
+       hu->proto->enqueue(hu, skb);
+
+       hci_uart_tx_wakeup(hu);
+
+       return 0;
+}
+
+/* Flow control or un-flow control the device */
+void hci_uart_set_flow_control(struct hci_uart *hu, bool enable)
+{
+       struct tty_struct *tty = hu->tty;
+       struct ktermios ktermios;
+       int status;
+       unsigned int set = 0;
+       unsigned int clear = 0;
+
+       if (enable) {
+               /* Disable hardware flow control */
+               ktermios = tty->termios;
+               ktermios.c_cflag &= ~CRTSCTS;
+               status = tty_set_termios(tty, &ktermios);
+               BT_DBG("Disabling hardware flow control: %s",
+                      status ? "failed" : "success");
+
+               /* Clear RTS to prevent the device from sending */
+               /* Most UARTs need OUT2 to enable interrupts */
+               status = tty->driver->ops->tiocmget(tty);
+               BT_DBG("Current tiocm 0x%x", status);
+
+               set &= ~(TIOCM_OUT2 | TIOCM_RTS);
+               clear = ~set;
+               set &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
+                      TIOCM_OUT2 | TIOCM_LOOP;
+               clear &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
+                        TIOCM_OUT2 | TIOCM_LOOP;
+               status = tty->driver->ops->tiocmset(tty, set, clear);
+               BT_DBG("Clearing RTS: %s", status ? "failed" : "success");
+       } else {
+               /* Set RTS to allow the device to send again */
+               status = tty->driver->ops->tiocmget(tty);
+               BT_DBG("Current tiocm 0x%x", status);
+
+               set |= (TIOCM_OUT2 | TIOCM_RTS);
+               clear = ~set;
+               set &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
+                      TIOCM_OUT2 | TIOCM_LOOP;
+               clear &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
+                        TIOCM_OUT2 | TIOCM_LOOP;
+               status = tty->driver->ops->tiocmset(tty, set, clear);
+               BT_DBG("Setting RTS: %s", status ? "failed" : "success");
+
+               /* Re-enable hardware flow control */
+               ktermios = tty->termios;
+               ktermios.c_cflag |= CRTSCTS;
+               status = tty_set_termios(tty, &ktermios);
+               BT_DBG("Enabling hardware flow control: %s",
+                      status ? "failed" : "success");
        }
+}
 
-       if (!test_bit(HCI_RUNNING, &hdev->flags))
-               return -EBUSY;
+void hci_uart_set_speeds(struct hci_uart *hu, unsigned int init_speed,
+                        unsigned int oper_speed)
+{
+       hu->init_speed = init_speed;
+       hu->oper_speed = oper_speed;
+}
 
-       hu = hci_get_drvdata(hdev);
+void hci_uart_init_tty(struct hci_uart *hu)
+{
+       struct tty_struct *tty = hu->tty;
+       struct ktermios ktermios;
+
+       /* Bring the UART into a known 8 bits no parity hw fc state */
+       ktermios = tty->termios;
+       ktermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |
+                             INLCR | IGNCR | ICRNL | IXON);
+       ktermios.c_oflag &= ~OPOST;
+       ktermios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
+       ktermios.c_cflag &= ~(CSIZE | PARENB);
+       ktermios.c_cflag |= CS8;
+       ktermios.c_cflag |= CRTSCTS;
+
+       /* tty_set_termios() return not checked as it is always 0 */
+       tty_set_termios(tty, &ktermios);
+}
 
-       BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
+void hci_uart_set_baudrate(struct hci_uart *hu, unsigned int speed)
+{
+       struct tty_struct *tty = hu->tty;
+       struct ktermios ktermios;
 
-       hu->proto->enqueue(hu, skb);
+       ktermios = tty->termios;
+       ktermios.c_cflag &= ~CBAUD;
+       tty_termios_encode_baud_rate(&ktermios, speed, speed);
 
-       hci_uart_tx_wakeup(hu);
+       /* tty_set_termios() return not checked as it is always 0 */
+       tty_set_termios(tty, &ktermios);
+
+       BT_DBG("%s: New tty speeds: %d/%d", hu->hdev->name,
+              tty->termios.c_ispeed, tty->termios.c_ospeed);
+}
 
+static int hci_uart_setup(struct hci_dev *hdev)
+{
+       struct hci_uart *hu = hci_get_drvdata(hdev);
+       struct hci_rp_read_local_version *ver;
+       struct sk_buff *skb;
+       unsigned int speed;
+       int err;
+
+       /* Init speed if any */
+       if (hu->init_speed)
+               speed = hu->init_speed;
+       else if (hu->proto->init_speed)
+               speed = hu->proto->init_speed;
+       else
+               speed = 0;
+
+       if (speed)
+               hci_uart_set_baudrate(hu, speed);
+
+       /* 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 (hu->proto->set_baudrate && speed) {
+               err = hu->proto->set_baudrate(hu, speed);
+               if (!err)
+                       hci_uart_set_baudrate(hu, speed);
+       }
+
+       if (hu->proto->setup)
+               return hu->proto->setup(hu);
+
+       if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
+               return 0;
+
+       skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
+                            HCI_INIT_TIMEOUT);
+       if (IS_ERR(skb)) {
+               BT_ERR("%s: Reading local version information failed (%ld)",
+                      hdev->name, PTR_ERR(skb));
+               return 0;
+       }
+
+       if (skb->len != sizeof(*ver)) {
+               BT_ERR("%s: Event length mismatch for version information",
+                      hdev->name);
+               goto done;
+       }
+
+       ver = (struct hci_rp_read_local_version *)skb->data;
+
+       switch (le16_to_cpu(ver->manufacturer)) {
+#ifdef CONFIG_BT_HCIUART_INTEL
+       case 2:
+               hdev->set_bdaddr = btintel_set_bdaddr;
+               btintel_check_bdaddr(hdev);
+               break;
+#endif
+#ifdef CONFIG_BT_HCIUART_BCM
+       case 15:
+               hdev->set_bdaddr = btbcm_set_bdaddr;
+               btbcm_check_bdaddr(hdev);
+               break;
+#endif
+       }
+
+done:
+       kfree_skb(skb);
        return 0;
 }
 
@@ -279,7 +448,8 @@ static int hci_uart_tty_open(struct tty_struct *tty)
        if (tty->ops->write == NULL)
                return -EOPNOTSUPP;
 
-       if (!(hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
+       hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL);
+       if (!hu) {
                BT_ERR("Can't allocate control structure");
                return -ENFILE;
        }
@@ -289,8 +459,7 @@ static int hci_uart_tty_open(struct tty_struct *tty)
        tty->receive_room = 65536;
 
        INIT_WORK(&hu->init_ready, hci_uart_init_work);
-
-       spin_lock_init(&hu->rx_lock);
+       INIT_WORK(&hu->write_work, hci_uart_write_work);
 
        /* Flush any pending characters in the driver and line discipline. */
 
@@ -311,7 +480,7 @@ static int hci_uart_tty_open(struct tty_struct *tty)
  */
 static void hci_uart_tty_close(struct tty_struct *tty)
 {
-       struct hci_uart *hu = (void *)tty->disc_data;
+       struct hci_uart *hu = tty->disc_data;
        struct hci_dev *hdev;
 
        BT_DBG("tty %p", tty);
@@ -326,7 +495,9 @@ static void hci_uart_tty_close(struct tty_struct *tty)
        if (hdev)
                hci_uart_close(hdev);
 
-       if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
+       cancel_work_sync(&hu->write_work);
+
+       if (test_and_clear_bit(HCI_UART_PROTO_READY, &hu->flags)) {
                if (hdev) {
                        if (test_bit(HCI_UART_REGISTERED, &hu->flags))
                                hci_unregister_dev(hdev);
@@ -334,6 +505,7 @@ static void hci_uart_tty_close(struct tty_struct *tty)
                }
                hu->proto->close(hu);
        }
+       clear_bit(HCI_UART_PROTO_SET, &hu->flags);
 
        kfree(hu);
 }
@@ -348,7 +520,7 @@ static void hci_uart_tty_close(struct tty_struct *tty)
  */
 static void hci_uart_tty_wakeup(struct tty_struct *tty)
 {
-       struct hci_uart *hu = (void *)tty->disc_data;
+       struct hci_uart *hu = tty->disc_data;
 
        BT_DBG("");
 
@@ -360,7 +532,7 @@ static void hci_uart_tty_wakeup(struct tty_struct *tty)
        if (tty != hu->tty)
                return;
 
-       if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
+       if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
                hci_uart_tx_wakeup(hu);
 }
 
@@ -376,24 +548,25 @@ static void hci_uart_tty_wakeup(struct tty_struct *tty)
  *
  * Return Value:    None
  */
-static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
+static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
+                                char *flags, int count)
 {
-       struct hci_uart *hu = (void *)tty->disc_data;
+       struct hci_uart *hu = tty->disc_data;
 
        if (!hu || tty != hu->tty)
                return;
 
-       if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
+       if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
                return;
 
-       spin_lock(&hu->rx_lock);
-       hu->proto->recv(hu, (void *) data, count);
+       /* It does not need a lock here as it is already protected by a mutex in
+        * tty caller
+        */
+       hu->proto->recv(hu, data, count);
 
        if (hu->hdev)
                hu->hdev->stat.byte_rx += count;
 
-       spin_unlock(&hu->rx_lock);
-
        tty_unthrottle(tty);
 }
 
@@ -415,15 +588,26 @@ static int hci_uart_register_dev(struct hci_uart *hu)
        hdev->bus = HCI_UART;
        hci_set_drvdata(hdev, hu);
 
+       /* Only when vendor specific setup callback is provided, consider
+        * the manufacturer information valid. This avoids filling in the
+        * value for Ericsson when nothing is specified.
+        */
+       if (hu->proto->setup)
+               hdev->manufacturer = hu->proto->manufacturer;
+
        hdev->open  = hci_uart_open;
        hdev->close = hci_uart_close;
        hdev->flush = hci_uart_flush;
        hdev->send  = hci_uart_send_frame;
+       hdev->setup = hci_uart_setup;
        SET_HCIDEV_DEV(hdev, hu->tty->dev);
 
        if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
                set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
 
+       if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
+               set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
+
        if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
                set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
 
@@ -448,7 +632,7 @@ static int hci_uart_register_dev(struct hci_uart *hu)
 
 static int hci_uart_set_proto(struct hci_uart *hu, int id)
 {
-       struct hci_uart_proto *p;
+       const struct hci_uart_proto *p;
        int err;
 
        p = hci_uart_get_proto(id);
@@ -460,9 +644,11 @@ static int hci_uart_set_proto(struct hci_uart *hu, int id)
                return err;
 
        hu->proto = p;
+       set_bit(HCI_UART_PROTO_READY, &hu->flags);
 
        err = hci_uart_register_dev(hu);
        if (err) {
+               clear_bit(HCI_UART_PROTO_READY, &hu->flags);
                p->close(hu);
                return err;
        }
@@ -470,6 +656,23 @@ static int hci_uart_set_proto(struct hci_uart *hu, int id)
        return 0;
 }
 
+static int hci_uart_set_flags(struct hci_uart *hu, unsigned long flags)
+{
+       unsigned long valid_flags = BIT(HCI_UART_RAW_DEVICE) |
+                                   BIT(HCI_UART_RESET_ON_INIT) |
+                                   BIT(HCI_UART_CREATE_AMP) |
+                                   BIT(HCI_UART_INIT_PENDING) |
+                                   BIT(HCI_UART_EXT_CONFIG) |
+                                   BIT(HCI_UART_VND_DETECT);
+
+       if (flags & ~valid_flags)
+               return -EINVAL;
+
+       hu->hdev_flags = flags;
+
+       return 0;
+}
+
 /* hci_uart_tty_ioctl()
  *
  *    Process IOCTL system call for the tty device.
@@ -483,10 +686,10 @@ static int hci_uart_set_proto(struct hci_uart *hu, int id)
  *
  * Return Value:    Command dependent
  */
-static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
-                                       unsigned int cmd, unsigned long arg)
+static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,
+                             unsigned int cmd, unsigned long arg)
 {
-       struct hci_uart *hu = (void *)tty->disc_data;
+       struct hci_uart *hu = tty->disc_data;
        int err = 0;
 
        BT_DBG("");
@@ -513,14 +716,16 @@ static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
                return -EUNATCH;
 
        case HCIUARTGETDEVICE:
-               if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
+               if (test_bit(HCI_UART_REGISTERED, &hu->flags))
                        return hu->hdev->id;
                return -EUNATCH;
 
        case HCIUARTSETFLAGS:
                if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
                        return -EBUSY;
-               hu->hdev_flags = arg;
+               err = hci_uart_set_flags(hu, arg);
+               if (err)
+                       return err;
                break;
 
        case HCIUARTGETFLAGS:
@@ -538,19 +743,19 @@ static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
  * We don't provide read/write/poll interface for user space.
  */
 static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
-                                       unsigned char __user *buf, size_t nr)
+                                unsigned char __user *buf, size_t nr)
 {
        return 0;
 }
 
 static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
-                                       const unsigned char *data, size_t count)
+                                 const unsigned char *data, size_t count)
 {
        return 0;
 }
 
 static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
-                                       struct file *filp, poll_table *wait)
+                                     struct file *filp, poll_table *wait)
 {
        return 0;
 }
@@ -564,7 +769,7 @@ static int __init hci_uart_init(void)
 
        /* Register the tty discipline */
 
-       memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
+       memset(&hci_uart_ldisc, 0, sizeof(hci_uart_ldisc));
        hci_uart_ldisc.magic            = TTY_LDISC_MAGIC;
        hci_uart_ldisc.name             = "n_hci";
        hci_uart_ldisc.open             = hci_uart_tty_open;
@@ -577,7 +782,8 @@ static int __init hci_uart_init(void)
        hci_uart_ldisc.write_wakeup     = hci_uart_tty_wakeup;
        hci_uart_ldisc.owner            = THIS_MODULE;
 
-       if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
+       err = tty_register_ldisc(N_HCI, &hci_uart_ldisc);
+       if (err) {
                BT_ERR("HCI line discipline registration failed. (%d)", err);
                return err;
        }
@@ -597,6 +803,15 @@ static int __init hci_uart_init(void)
 #ifdef CONFIG_BT_HCIUART_3WIRE
        h5_init();
 #endif
+#ifdef CONFIG_BT_HCIUART_INTEL
+       intel_init();
+#endif
+#ifdef CONFIG_BT_HCIUART_BCM
+       bcm_init();
+#endif
+#ifdef CONFIG_BT_HCIUART_QCA
+       qca_init();
+#endif
 
        return 0;
 }
@@ -620,9 +835,19 @@ static void __exit hci_uart_exit(void)
 #ifdef CONFIG_BT_HCIUART_3WIRE
        h5_deinit();
 #endif
+#ifdef CONFIG_BT_HCIUART_INTEL
+       intel_deinit();
+#endif
+#ifdef CONFIG_BT_HCIUART_BCM
+       bcm_deinit();
+#endif
+#ifdef CONFIG_BT_HCIUART_QCA
+       qca_deinit();
+#endif
 
        /* Release tty registration of line discipline */
-       if ((err = tty_unregister_ldisc(N_HCI)))
+       err = tty_unregister_ldisc(N_HCI);
+       if (err)
                BT_ERR("Can't unregister HCI line discipline (%d)", err);
 }