Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  *  Bluetooth HCI UART driver
0005  *
0006  *  Copyright (C) 2000-2001  Qualcomm Incorporated
0007  *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
0008  *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
0009  */
0010 
0011 #include <linux/module.h>
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/init.h>
0015 #include <linux/types.h>
0016 #include <linux/fcntl.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/ptrace.h>
0019 #include <linux/poll.h>
0020 
0021 #include <linux/slab.h>
0022 #include <linux/tty.h>
0023 #include <linux/errno.h>
0024 #include <linux/string.h>
0025 #include <linux/signal.h>
0026 #include <linux/ioctl.h>
0027 #include <linux/skbuff.h>
0028 #include <linux/firmware.h>
0029 #include <linux/serdev.h>
0030 
0031 #include <net/bluetooth/bluetooth.h>
0032 #include <net/bluetooth/hci_core.h>
0033 
0034 #include "btintel.h"
0035 #include "btbcm.h"
0036 #include "hci_uart.h"
0037 
0038 #define VERSION "2.3"
0039 
0040 static const struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
0041 
0042 int hci_uart_register_proto(const struct hci_uart_proto *p)
0043 {
0044     if (p->id >= HCI_UART_MAX_PROTO)
0045         return -EINVAL;
0046 
0047     if (hup[p->id])
0048         return -EEXIST;
0049 
0050     hup[p->id] = p;
0051 
0052     BT_INFO("HCI UART protocol %s registered", p->name);
0053 
0054     return 0;
0055 }
0056 
0057 int hci_uart_unregister_proto(const struct hci_uart_proto *p)
0058 {
0059     if (p->id >= HCI_UART_MAX_PROTO)
0060         return -EINVAL;
0061 
0062     if (!hup[p->id])
0063         return -EINVAL;
0064 
0065     hup[p->id] = NULL;
0066 
0067     return 0;
0068 }
0069 
0070 static const struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
0071 {
0072     if (id >= HCI_UART_MAX_PROTO)
0073         return NULL;
0074 
0075     return hup[id];
0076 }
0077 
0078 static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
0079 {
0080     struct hci_dev *hdev = hu->hdev;
0081 
0082     /* Update HCI stat counters */
0083     switch (pkt_type) {
0084     case HCI_COMMAND_PKT:
0085         hdev->stat.cmd_tx++;
0086         break;
0087 
0088     case HCI_ACLDATA_PKT:
0089         hdev->stat.acl_tx++;
0090         break;
0091 
0092     case HCI_SCODATA_PKT:
0093         hdev->stat.sco_tx++;
0094         break;
0095     }
0096 }
0097 
0098 static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
0099 {
0100     struct sk_buff *skb = hu->tx_skb;
0101 
0102     if (!skb) {
0103         percpu_down_read(&hu->proto_lock);
0104 
0105         if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
0106             skb = hu->proto->dequeue(hu);
0107 
0108         percpu_up_read(&hu->proto_lock);
0109     } else {
0110         hu->tx_skb = NULL;
0111     }
0112 
0113     return skb;
0114 }
0115 
0116 int hci_uart_tx_wakeup(struct hci_uart *hu)
0117 {
0118     /* This may be called in an IRQ context, so we can't sleep. Therefore
0119      * we try to acquire the lock only, and if that fails we assume the
0120      * tty is being closed because that is the only time the write lock is
0121      * acquired. If, however, at some point in the future the write lock
0122      * is also acquired in other situations, then this must be revisited.
0123      */
0124     if (!percpu_down_read_trylock(&hu->proto_lock))
0125         return 0;
0126 
0127     if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
0128         goto no_schedule;
0129 
0130     set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
0131     if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state))
0132         goto no_schedule;
0133 
0134     BT_DBG("");
0135 
0136     schedule_work(&hu->write_work);
0137 
0138 no_schedule:
0139     percpu_up_read(&hu->proto_lock);
0140 
0141     return 0;
0142 }
0143 EXPORT_SYMBOL_GPL(hci_uart_tx_wakeup);
0144 
0145 static void hci_uart_write_work(struct work_struct *work)
0146 {
0147     struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
0148     struct tty_struct *tty = hu->tty;
0149     struct hci_dev *hdev = hu->hdev;
0150     struct sk_buff *skb;
0151 
0152     /* REVISIT: should we cope with bad skbs or ->write() returning
0153      * and error value ?
0154      */
0155 
0156 restart:
0157     clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
0158 
0159     while ((skb = hci_uart_dequeue(hu))) {
0160         int len;
0161 
0162         set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0163         len = tty->ops->write(tty, skb->data, skb->len);
0164         hdev->stat.byte_tx += len;
0165 
0166         skb_pull(skb, len);
0167         if (skb->len) {
0168             hu->tx_skb = skb;
0169             break;
0170         }
0171 
0172         hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
0173         kfree_skb(skb);
0174     }
0175 
0176     clear_bit(HCI_UART_SENDING, &hu->tx_state);
0177     if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
0178         goto restart;
0179 
0180     wake_up_bit(&hu->tx_state, HCI_UART_SENDING);
0181 }
0182 
0183 void hci_uart_init_work(struct work_struct *work)
0184 {
0185     struct hci_uart *hu = container_of(work, struct hci_uart, init_ready);
0186     int err;
0187     struct hci_dev *hdev;
0188 
0189     if (!test_and_clear_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
0190         return;
0191 
0192     err = hci_register_dev(hu->hdev);
0193     if (err < 0) {
0194         BT_ERR("Can't register HCI device");
0195         clear_bit(HCI_UART_PROTO_READY, &hu->flags);
0196         hu->proto->close(hu);
0197         hdev = hu->hdev;
0198         hu->hdev = NULL;
0199         hci_free_dev(hdev);
0200         return;
0201     }
0202 
0203     set_bit(HCI_UART_REGISTERED, &hu->flags);
0204 }
0205 
0206 int hci_uart_init_ready(struct hci_uart *hu)
0207 {
0208     if (!test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
0209         return -EALREADY;
0210 
0211     schedule_work(&hu->init_ready);
0212 
0213     return 0;
0214 }
0215 
0216 int hci_uart_wait_until_sent(struct hci_uart *hu)
0217 {
0218     return wait_on_bit_timeout(&hu->tx_state, HCI_UART_SENDING,
0219                    TASK_INTERRUPTIBLE,
0220                    msecs_to_jiffies(2000));
0221 }
0222 
0223 /* ------- Interface to HCI layer ------ */
0224 /* Reset device */
0225 static int hci_uart_flush(struct hci_dev *hdev)
0226 {
0227     struct hci_uart *hu  = hci_get_drvdata(hdev);
0228     struct tty_struct *tty = hu->tty;
0229 
0230     BT_DBG("hdev %p tty %p", hdev, tty);
0231 
0232     if (hu->tx_skb) {
0233         kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
0234     }
0235 
0236     /* Flush any pending characters in the driver and discipline. */
0237     tty_ldisc_flush(tty);
0238     tty_driver_flush_buffer(tty);
0239 
0240     percpu_down_read(&hu->proto_lock);
0241 
0242     if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
0243         hu->proto->flush(hu);
0244 
0245     percpu_up_read(&hu->proto_lock);
0246 
0247     return 0;
0248 }
0249 
0250 /* Initialize device */
0251 static int hci_uart_open(struct hci_dev *hdev)
0252 {
0253     BT_DBG("%s %p", hdev->name, hdev);
0254 
0255     /* Undo clearing this from hci_uart_close() */
0256     hdev->flush = hci_uart_flush;
0257 
0258     return 0;
0259 }
0260 
0261 /* Close device */
0262 static int hci_uart_close(struct hci_dev *hdev)
0263 {
0264     BT_DBG("hdev %p", hdev);
0265 
0266     hci_uart_flush(hdev);
0267     hdev->flush = NULL;
0268     return 0;
0269 }
0270 
0271 /* Send frames from HCI layer */
0272 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
0273 {
0274     struct hci_uart *hu = hci_get_drvdata(hdev);
0275 
0276     BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
0277            skb->len);
0278 
0279     percpu_down_read(&hu->proto_lock);
0280 
0281     if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
0282         percpu_up_read(&hu->proto_lock);
0283         return -EUNATCH;
0284     }
0285 
0286     hu->proto->enqueue(hu, skb);
0287     percpu_up_read(&hu->proto_lock);
0288 
0289     hci_uart_tx_wakeup(hu);
0290 
0291     return 0;
0292 }
0293 
0294 /* Check the underlying device or tty has flow control support */
0295 bool hci_uart_has_flow_control(struct hci_uart *hu)
0296 {
0297     /* serdev nodes check if the needed operations are present */
0298     if (hu->serdev)
0299         return true;
0300 
0301     if (hu->tty->driver->ops->tiocmget && hu->tty->driver->ops->tiocmset)
0302         return true;
0303 
0304     return false;
0305 }
0306 
0307 /* Flow control or un-flow control the device */
0308 void hci_uart_set_flow_control(struct hci_uart *hu, bool enable)
0309 {
0310     struct tty_struct *tty = hu->tty;
0311     struct ktermios ktermios;
0312     int status;
0313     unsigned int set = 0;
0314     unsigned int clear = 0;
0315 
0316     if (hu->serdev) {
0317         serdev_device_set_flow_control(hu->serdev, !enable);
0318         serdev_device_set_rts(hu->serdev, !enable);
0319         return;
0320     }
0321 
0322     if (enable) {
0323         /* Disable hardware flow control */
0324         ktermios = tty->termios;
0325         ktermios.c_cflag &= ~CRTSCTS;
0326         status = tty_set_termios(tty, &ktermios);
0327         BT_DBG("Disabling hardware flow control: %s",
0328                status ? "failed" : "success");
0329 
0330         /* Clear RTS to prevent the device from sending */
0331         /* Most UARTs need OUT2 to enable interrupts */
0332         status = tty->driver->ops->tiocmget(tty);
0333         BT_DBG("Current tiocm 0x%x", status);
0334 
0335         set &= ~(TIOCM_OUT2 | TIOCM_RTS);
0336         clear = ~set;
0337         set &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
0338                TIOCM_OUT2 | TIOCM_LOOP;
0339         clear &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
0340              TIOCM_OUT2 | TIOCM_LOOP;
0341         status = tty->driver->ops->tiocmset(tty, set, clear);
0342         BT_DBG("Clearing RTS: %s", status ? "failed" : "success");
0343     } else {
0344         /* Set RTS to allow the device to send again */
0345         status = tty->driver->ops->tiocmget(tty);
0346         BT_DBG("Current tiocm 0x%x", status);
0347 
0348         set |= (TIOCM_OUT2 | TIOCM_RTS);
0349         clear = ~set;
0350         set &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
0351                TIOCM_OUT2 | TIOCM_LOOP;
0352         clear &= TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 |
0353              TIOCM_OUT2 | TIOCM_LOOP;
0354         status = tty->driver->ops->tiocmset(tty, set, clear);
0355         BT_DBG("Setting RTS: %s", status ? "failed" : "success");
0356 
0357         /* Re-enable hardware flow control */
0358         ktermios = tty->termios;
0359         ktermios.c_cflag |= CRTSCTS;
0360         status = tty_set_termios(tty, &ktermios);
0361         BT_DBG("Enabling hardware flow control: %s",
0362                status ? "failed" : "success");
0363     }
0364 }
0365 
0366 void hci_uart_set_speeds(struct hci_uart *hu, unsigned int init_speed,
0367              unsigned int oper_speed)
0368 {
0369     hu->init_speed = init_speed;
0370     hu->oper_speed = oper_speed;
0371 }
0372 
0373 void hci_uart_set_baudrate(struct hci_uart *hu, unsigned int speed)
0374 {
0375     struct tty_struct *tty = hu->tty;
0376     struct ktermios ktermios;
0377 
0378     ktermios = tty->termios;
0379     ktermios.c_cflag &= ~CBAUD;
0380     tty_termios_encode_baud_rate(&ktermios, speed, speed);
0381 
0382     /* tty_set_termios() return not checked as it is always 0 */
0383     tty_set_termios(tty, &ktermios);
0384 
0385     BT_DBG("%s: New tty speeds: %d/%d", hu->hdev->name,
0386            tty->termios.c_ispeed, tty->termios.c_ospeed);
0387 }
0388 
0389 static int hci_uart_setup(struct hci_dev *hdev)
0390 {
0391     struct hci_uart *hu = hci_get_drvdata(hdev);
0392     struct hci_rp_read_local_version *ver;
0393     struct sk_buff *skb;
0394     unsigned int speed;
0395     int err;
0396 
0397     /* Init speed if any */
0398     if (hu->init_speed)
0399         speed = hu->init_speed;
0400     else if (hu->proto->init_speed)
0401         speed = hu->proto->init_speed;
0402     else
0403         speed = 0;
0404 
0405     if (speed)
0406         hci_uart_set_baudrate(hu, speed);
0407 
0408     /* Operational speed if any */
0409     if (hu->oper_speed)
0410         speed = hu->oper_speed;
0411     else if (hu->proto->oper_speed)
0412         speed = hu->proto->oper_speed;
0413     else
0414         speed = 0;
0415 
0416     if (hu->proto->set_baudrate && speed) {
0417         err = hu->proto->set_baudrate(hu, speed);
0418         if (!err)
0419             hci_uart_set_baudrate(hu, speed);
0420     }
0421 
0422     if (hu->proto->setup)
0423         return hu->proto->setup(hu);
0424 
0425     if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
0426         return 0;
0427 
0428     skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
0429                  HCI_INIT_TIMEOUT);
0430     if (IS_ERR(skb)) {
0431         BT_ERR("%s: Reading local version information failed (%ld)",
0432                hdev->name, PTR_ERR(skb));
0433         return 0;
0434     }
0435 
0436     if (skb->len != sizeof(*ver)) {
0437         BT_ERR("%s: Event length mismatch for version information",
0438                hdev->name);
0439         goto done;
0440     }
0441 
0442     ver = (struct hci_rp_read_local_version *)skb->data;
0443 
0444     switch (le16_to_cpu(ver->manufacturer)) {
0445 #ifdef CONFIG_BT_HCIUART_INTEL
0446     case 2:
0447         hdev->set_bdaddr = btintel_set_bdaddr;
0448         btintel_check_bdaddr(hdev);
0449         break;
0450 #endif
0451 #ifdef CONFIG_BT_HCIUART_BCM
0452     case 15:
0453         hdev->set_bdaddr = btbcm_set_bdaddr;
0454         btbcm_check_bdaddr(hdev);
0455         break;
0456 #endif
0457     default:
0458         break;
0459     }
0460 
0461 done:
0462     kfree_skb(skb);
0463     return 0;
0464 }
0465 
0466 /* ------ LDISC part ------ */
0467 /* hci_uart_tty_open
0468  *
0469  *     Called when line discipline changed to HCI_UART.
0470  *
0471  * Arguments:
0472  *     tty    pointer to tty info structure
0473  * Return Value:
0474  *     0 if success, otherwise error code
0475  */
0476 static int hci_uart_tty_open(struct tty_struct *tty)
0477 {
0478     struct hci_uart *hu;
0479 
0480     BT_DBG("tty %p", tty);
0481 
0482     if (!capable(CAP_NET_ADMIN))
0483         return -EPERM;
0484 
0485     /* Error if the tty has no write op instead of leaving an exploitable
0486      * hole
0487      */
0488     if (tty->ops->write == NULL)
0489         return -EOPNOTSUPP;
0490 
0491     hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL);
0492     if (!hu) {
0493         BT_ERR("Can't allocate control structure");
0494         return -ENFILE;
0495     }
0496 
0497     tty->disc_data = hu;
0498     hu->tty = tty;
0499     tty->receive_room = 65536;
0500 
0501     /* disable alignment support by default */
0502     hu->alignment = 1;
0503     hu->padding = 0;
0504 
0505     INIT_WORK(&hu->init_ready, hci_uart_init_work);
0506     INIT_WORK(&hu->write_work, hci_uart_write_work);
0507 
0508     percpu_init_rwsem(&hu->proto_lock);
0509 
0510     /* Flush any pending characters in the driver */
0511     tty_driver_flush_buffer(tty);
0512 
0513     return 0;
0514 }
0515 
0516 /* hci_uart_tty_close()
0517  *
0518  *    Called when the line discipline is changed to something
0519  *    else, the tty is closed, or the tty detects a hangup.
0520  */
0521 static void hci_uart_tty_close(struct tty_struct *tty)
0522 {
0523     struct hci_uart *hu = tty->disc_data;
0524     struct hci_dev *hdev;
0525 
0526     BT_DBG("tty %p", tty);
0527 
0528     /* Detach from the tty */
0529     tty->disc_data = NULL;
0530 
0531     if (!hu)
0532         return;
0533 
0534     hdev = hu->hdev;
0535     if (hdev)
0536         hci_uart_close(hdev);
0537 
0538     if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
0539         percpu_down_write(&hu->proto_lock);
0540         clear_bit(HCI_UART_PROTO_READY, &hu->flags);
0541         percpu_up_write(&hu->proto_lock);
0542 
0543         cancel_work_sync(&hu->init_ready);
0544         cancel_work_sync(&hu->write_work);
0545 
0546         if (hdev) {
0547             if (test_bit(HCI_UART_REGISTERED, &hu->flags))
0548                 hci_unregister_dev(hdev);
0549             hci_free_dev(hdev);
0550         }
0551         hu->proto->close(hu);
0552     }
0553     clear_bit(HCI_UART_PROTO_SET, &hu->flags);
0554 
0555     percpu_free_rwsem(&hu->proto_lock);
0556 
0557     kfree(hu);
0558 }
0559 
0560 /* hci_uart_tty_wakeup()
0561  *
0562  *    Callback for transmit wakeup. Called when low level
0563  *    device driver can accept more send data.
0564  *
0565  * Arguments:        tty    pointer to associated tty instance data
0566  * Return Value:    None
0567  */
0568 static void hci_uart_tty_wakeup(struct tty_struct *tty)
0569 {
0570     struct hci_uart *hu = tty->disc_data;
0571 
0572     BT_DBG("");
0573 
0574     if (!hu)
0575         return;
0576 
0577     clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0578 
0579     if (tty != hu->tty)
0580         return;
0581 
0582     if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
0583         hci_uart_tx_wakeup(hu);
0584 }
0585 
0586 /* hci_uart_tty_receive()
0587  *
0588  *     Called by tty low level driver when receive data is
0589  *     available.
0590  *
0591  * Arguments:  tty          pointer to tty isntance data
0592  *             data         pointer to received data
0593  *             flags        pointer to flags for data
0594  *             count        count of received data in bytes
0595  *
0596  * Return Value:    None
0597  */
0598 static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
0599                  const char *flags, int count)
0600 {
0601     struct hci_uart *hu = tty->disc_data;
0602 
0603     if (!hu || tty != hu->tty)
0604         return;
0605 
0606     percpu_down_read(&hu->proto_lock);
0607 
0608     if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
0609         percpu_up_read(&hu->proto_lock);
0610         return;
0611     }
0612 
0613     /* It does not need a lock here as it is already protected by a mutex in
0614      * tty caller
0615      */
0616     hu->proto->recv(hu, data, count);
0617     percpu_up_read(&hu->proto_lock);
0618 
0619     if (hu->hdev)
0620         hu->hdev->stat.byte_rx += count;
0621 
0622     tty_unthrottle(tty);
0623 }
0624 
0625 static int hci_uart_register_dev(struct hci_uart *hu)
0626 {
0627     struct hci_dev *hdev;
0628     int err;
0629 
0630     BT_DBG("");
0631 
0632     /* Initialize and register HCI device */
0633     hdev = hci_alloc_dev();
0634     if (!hdev) {
0635         BT_ERR("Can't allocate HCI device");
0636         return -ENOMEM;
0637     }
0638 
0639     hu->hdev = hdev;
0640 
0641     hdev->bus = HCI_UART;
0642     hci_set_drvdata(hdev, hu);
0643 
0644     /* Only when vendor specific setup callback is provided, consider
0645      * the manufacturer information valid. This avoids filling in the
0646      * value for Ericsson when nothing is specified.
0647      */
0648     if (hu->proto->setup)
0649         hdev->manufacturer = hu->proto->manufacturer;
0650 
0651     hdev->open  = hci_uart_open;
0652     hdev->close = hci_uart_close;
0653     hdev->flush = hci_uart_flush;
0654     hdev->send  = hci_uart_send_frame;
0655     hdev->setup = hci_uart_setup;
0656     SET_HCIDEV_DEV(hdev, hu->tty->dev);
0657 
0658     if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
0659         set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
0660 
0661     if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
0662         set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
0663 
0664     if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
0665         set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
0666 
0667     if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
0668         hdev->dev_type = HCI_AMP;
0669     else
0670         hdev->dev_type = HCI_PRIMARY;
0671 
0672     /* Only call open() for the protocol after hdev is fully initialized as
0673      * open() (or a timer/workqueue it starts) may attempt to reference it.
0674      */
0675     err = hu->proto->open(hu);
0676     if (err) {
0677         hu->hdev = NULL;
0678         hci_free_dev(hdev);
0679         return err;
0680     }
0681 
0682     if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
0683         return 0;
0684 
0685     if (hci_register_dev(hdev) < 0) {
0686         BT_ERR("Can't register HCI device");
0687         hu->proto->close(hu);
0688         hu->hdev = NULL;
0689         hci_free_dev(hdev);
0690         return -ENODEV;
0691     }
0692 
0693     set_bit(HCI_UART_REGISTERED, &hu->flags);
0694 
0695     return 0;
0696 }
0697 
0698 static int hci_uart_set_proto(struct hci_uart *hu, int id)
0699 {
0700     const struct hci_uart_proto *p;
0701     int err;
0702 
0703     p = hci_uart_get_proto(id);
0704     if (!p)
0705         return -EPROTONOSUPPORT;
0706 
0707     hu->proto = p;
0708 
0709     err = hci_uart_register_dev(hu);
0710     if (err) {
0711         return err;
0712     }
0713 
0714     set_bit(HCI_UART_PROTO_READY, &hu->flags);
0715     return 0;
0716 }
0717 
0718 static int hci_uart_set_flags(struct hci_uart *hu, unsigned long flags)
0719 {
0720     unsigned long valid_flags = BIT(HCI_UART_RAW_DEVICE) |
0721                     BIT(HCI_UART_RESET_ON_INIT) |
0722                     BIT(HCI_UART_CREATE_AMP) |
0723                     BIT(HCI_UART_INIT_PENDING) |
0724                     BIT(HCI_UART_EXT_CONFIG) |
0725                     BIT(HCI_UART_VND_DETECT);
0726 
0727     if (flags & ~valid_flags)
0728         return -EINVAL;
0729 
0730     hu->hdev_flags = flags;
0731 
0732     return 0;
0733 }
0734 
0735 /* hci_uart_tty_ioctl()
0736  *
0737  *    Process IOCTL system call for the tty device.
0738  *
0739  * Arguments:
0740  *
0741  *    tty        pointer to tty instance data
0742  *    cmd        IOCTL command code
0743  *    arg        argument for IOCTL call (cmd dependent)
0744  *
0745  * Return Value:    Command dependent
0746  */
0747 static int hci_uart_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
0748                   unsigned long arg)
0749 {
0750     struct hci_uart *hu = tty->disc_data;
0751     int err = 0;
0752 
0753     BT_DBG("");
0754 
0755     /* Verify the status of the device */
0756     if (!hu)
0757         return -EBADF;
0758 
0759     switch (cmd) {
0760     case HCIUARTSETPROTO:
0761         if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
0762             err = hci_uart_set_proto(hu, arg);
0763             if (err)
0764                 clear_bit(HCI_UART_PROTO_SET, &hu->flags);
0765         } else
0766             err = -EBUSY;
0767         break;
0768 
0769     case HCIUARTGETPROTO:
0770         if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
0771             err = hu->proto->id;
0772         else
0773             err = -EUNATCH;
0774         break;
0775 
0776     case HCIUARTGETDEVICE:
0777         if (test_bit(HCI_UART_REGISTERED, &hu->flags))
0778             err = hu->hdev->id;
0779         else
0780             err = -EUNATCH;
0781         break;
0782 
0783     case HCIUARTSETFLAGS:
0784         if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
0785             err = -EBUSY;
0786         else
0787             err = hci_uart_set_flags(hu, arg);
0788         break;
0789 
0790     case HCIUARTGETFLAGS:
0791         err = hu->hdev_flags;
0792         break;
0793 
0794     default:
0795         err = n_tty_ioctl_helper(tty, cmd, arg);
0796         break;
0797     }
0798 
0799     return err;
0800 }
0801 
0802 /*
0803  * We don't provide read/write/poll interface for user space.
0804  */
0805 static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
0806                  unsigned char *buf, size_t nr,
0807                  void **cookie, unsigned long offset)
0808 {
0809     return 0;
0810 }
0811 
0812 static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
0813                   const unsigned char *data, size_t count)
0814 {
0815     return 0;
0816 }
0817 
0818 static __poll_t hci_uart_tty_poll(struct tty_struct *tty,
0819                       struct file *filp, poll_table *wait)
0820 {
0821     return 0;
0822 }
0823 
0824 static struct tty_ldisc_ops hci_uart_ldisc = {
0825     .owner      = THIS_MODULE,
0826     .num        = N_HCI,
0827     .name       = "n_hci",
0828     .open       = hci_uart_tty_open,
0829     .close      = hci_uart_tty_close,
0830     .read       = hci_uart_tty_read,
0831     .write      = hci_uart_tty_write,
0832     .ioctl      = hci_uart_tty_ioctl,
0833     .compat_ioctl   = hci_uart_tty_ioctl,
0834     .poll       = hci_uart_tty_poll,
0835     .receive_buf    = hci_uart_tty_receive,
0836     .write_wakeup   = hci_uart_tty_wakeup,
0837 };
0838 
0839 static int __init hci_uart_init(void)
0840 {
0841     int err;
0842 
0843     BT_INFO("HCI UART driver ver %s", VERSION);
0844 
0845     /* Register the tty discipline */
0846     err = tty_register_ldisc(&hci_uart_ldisc);
0847     if (err) {
0848         BT_ERR("HCI line discipline registration failed. (%d)", err);
0849         return err;
0850     }
0851 
0852 #ifdef CONFIG_BT_HCIUART_H4
0853     h4_init();
0854 #endif
0855 #ifdef CONFIG_BT_HCIUART_BCSP
0856     bcsp_init();
0857 #endif
0858 #ifdef CONFIG_BT_HCIUART_LL
0859     ll_init();
0860 #endif
0861 #ifdef CONFIG_BT_HCIUART_ATH3K
0862     ath_init();
0863 #endif
0864 #ifdef CONFIG_BT_HCIUART_3WIRE
0865     h5_init();
0866 #endif
0867 #ifdef CONFIG_BT_HCIUART_INTEL
0868     intel_init();
0869 #endif
0870 #ifdef CONFIG_BT_HCIUART_BCM
0871     bcm_init();
0872 #endif
0873 #ifdef CONFIG_BT_HCIUART_QCA
0874     qca_init();
0875 #endif
0876 #ifdef CONFIG_BT_HCIUART_AG6XX
0877     ag6xx_init();
0878 #endif
0879 #ifdef CONFIG_BT_HCIUART_MRVL
0880     mrvl_init();
0881 #endif
0882 
0883     return 0;
0884 }
0885 
0886 static void __exit hci_uart_exit(void)
0887 {
0888 #ifdef CONFIG_BT_HCIUART_H4
0889     h4_deinit();
0890 #endif
0891 #ifdef CONFIG_BT_HCIUART_BCSP
0892     bcsp_deinit();
0893 #endif
0894 #ifdef CONFIG_BT_HCIUART_LL
0895     ll_deinit();
0896 #endif
0897 #ifdef CONFIG_BT_HCIUART_ATH3K
0898     ath_deinit();
0899 #endif
0900 #ifdef CONFIG_BT_HCIUART_3WIRE
0901     h5_deinit();
0902 #endif
0903 #ifdef CONFIG_BT_HCIUART_INTEL
0904     intel_deinit();
0905 #endif
0906 #ifdef CONFIG_BT_HCIUART_BCM
0907     bcm_deinit();
0908 #endif
0909 #ifdef CONFIG_BT_HCIUART_QCA
0910     qca_deinit();
0911 #endif
0912 #ifdef CONFIG_BT_HCIUART_AG6XX
0913     ag6xx_deinit();
0914 #endif
0915 #ifdef CONFIG_BT_HCIUART_MRVL
0916     mrvl_deinit();
0917 #endif
0918 
0919     tty_unregister_ldisc(&hci_uart_ldisc);
0920 }
0921 
0922 module_init(hci_uart_init);
0923 module_exit(hci_uart_exit);
0924 
0925 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
0926 MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
0927 MODULE_VERSION(VERSION);
0928 MODULE_LICENSE("GPL");
0929 MODULE_ALIAS_LDISC(N_HCI);