2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
*
|
2005-10-28 17:20:45 +00:00
|
|
|
* Bluetooth HCI UART driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2001 Qualcomm Incorporated
|
|
|
|
* Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
|
|
|
|
* Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/ptrace.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/tty.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/signal.h>
|
|
|
|
#include <linux/ioctl.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
|
|
|
|
#include <net/bluetooth/bluetooth.h>
|
|
|
|
#include <net/bluetooth/hci_core.h>
|
|
|
|
|
|
|
|
#include "hci_uart.h"
|
|
|
|
|
2005-10-28 17:20:45 +00:00
|
|
|
#define VERSION "2.2"
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static int reset = 0;
|
|
|
|
|
|
|
|
static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
|
|
|
|
|
|
|
|
int hci_uart_register_proto(struct hci_uart_proto *p)
|
|
|
|
{
|
|
|
|
if (p->id >= HCI_UART_MAX_PROTO)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (hup[p->id])
|
|
|
|
return -EEXIST;
|
|
|
|
|
|
|
|
hup[p->id] = p;
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hci_uart_unregister_proto(struct hci_uart_proto *p)
|
|
|
|
{
|
|
|
|
if (p->id >= HCI_UART_MAX_PROTO)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!hup[p->id])
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
hup[p->id] = NULL;
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
|
|
|
|
{
|
|
|
|
if (id >= HCI_UART_MAX_PROTO)
|
|
|
|
return NULL;
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return hup[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
|
|
|
|
{
|
|
|
|
struct hci_dev *hdev = hu->hdev;
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Update HCI stat counters */
|
|
|
|
switch (pkt_type) {
|
|
|
|
case HCI_COMMAND_PKT:
|
|
|
|
hdev->stat.cmd_tx++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_ACLDATA_PKT:
|
|
|
|
hdev->stat.acl_tx++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_SCODATA_PKT:
|
2010-10-07 19:57:10 +00:00
|
|
|
hdev->stat.sco_tx++;
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb = hu->tx_skb;
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!skb)
|
|
|
|
skb = hu->proto->dequeue(hu);
|
|
|
|
else
|
|
|
|
hu->tx_skb = NULL;
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
|
|
|
|
set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("");
|
|
|
|
|
|
|
|
restart:
|
|
|
|
clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
|
|
|
|
|
|
|
|
while ((skb = hci_uart_dequeue(hu))) {
|
|
|
|
int len;
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
|
2008-04-30 07:54:13 +00:00
|
|
|
len = tty->ops->write(tty, skb->data, skb->len);
|
2005-04-16 22:20:36 +00:00
|
|
|
hdev->stat.byte_tx += len;
|
|
|
|
|
|
|
|
skb_pull(skb, len);
|
|
|
|
if (skb->len) {
|
|
|
|
hu->tx_skb = skb;
|
|
|
|
break;
|
|
|
|
}
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-08-10 03:30:28 +00:00
|
|
|
hci_uart_tx_complete(hu, bt_cb(skb)->pkt_type);
|
2005-04-16 22:20:36 +00:00
|
|
|
kfree_skb(skb);
|
2005-10-28 17:20:45 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
|
|
|
|
goto restart;
|
|
|
|
|
|
|
|
clear_bit(HCI_UART_SENDING, &hu->tx_state);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------- Interface to HCI layer ------ */
|
|
|
|
/* Initialize device */
|
|
|
|
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);
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset device */
|
|
|
|
static int hci_uart_flush(struct hci_dev *hdev)
|
|
|
|
{
|
|
|
|
struct hci_uart *hu = (struct hci_uart *) hdev->driver_data;
|
|
|
|
struct tty_struct *tty = hu->tty;
|
|
|
|
|
|
|
|
BT_DBG("hdev %p tty %p", hdev, tty);
|
|
|
|
|
|
|
|
if (hu->tx_skb) {
|
|
|
|
kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush any pending characters in the driver and discipline. */
|
|
|
|
tty_ldisc_flush(tty);
|
2008-04-30 07:54:13 +00:00
|
|
|
tty_driver_flush_buffer(tty);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
|
|
|
|
hu->proto->flush(hu);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close device */
|
|
|
|
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);
|
hci_ldisc: fix null pointer deref
Arjan:
With the help of kerneloops.org I've spotted a nice little interaction
between the TTY layer and the bluetooth code, however the tty layer is not
something I'm all too familiar with so I rather ask than brute-force fix the
code incorrectly.
The raw details are at:
http://www.kerneloops.org/search.php?search=uart_flush_buffer
What happens is that, on closing the bluetooth tty, the tty layer goes
into the release_dev() function, which first does a bunch of stuff, then
sets the file->private_data to NULL, does some more stuff and then calls the
ldisc close function. Which in this case, is hci_uart_tty_close().
Now, hci_uart_tty_close() calls hci_uart_close() which clears some
internal bit, and then calls hci_uart_flush()... which calls back to the
tty layers' uart_flush_buffer() function. (in drivers/bluetooth/hci_tty.c
around line 194) Which then WARN_ON()'s because that's not allowed/supposed
to be called this late in the shutdown of the port....
Should the bluetooth driver even call this flush function at all??
David:
This seems to be what happens: Hci_uart_close() flushes using
hci_uart_flush(). Subsequently, in hci_dev_do_close(), (one step in
hci_unregister_dev()), hci_uart_flush() is called again. The comment in
uart_flush_buffer(), relating to the WARN_ON(), indicates you can't flush
after the port is closed; which sounds reasonable. I think hci_uart_close()
should set hdev->flush to NULL before returning. Hci_dev_do_close() does
check for this. The code path is rather involved and I'm not entirely clear
of all steps, but I think that's what should be done.
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2008-02-12 05:41:30 +00:00
|
|
|
hdev->flush = NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send frames from HCI layer */
|
|
|
|
static int hci_uart_send_frame(struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct hci_dev* hdev = (struct hci_dev *) skb->dev;
|
|
|
|
struct hci_uart *hu;
|
|
|
|
|
|
|
|
if (!hdev) {
|
tree-wide: fix assorted typos all over the place
That is "success", "unknown", "through", "performance", "[re|un]mapping"
, "access", "default", "reasonable", "[con]currently", "temperature"
, "channel", "[un]used", "application", "example","hierarchy", "therefore"
, "[over|under]flow", "contiguous", "threshold", "enough" and others.
Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2009-11-14 15:09:05 +00:00
|
|
|
BT_ERR("Frame for unknown device (hdev=NULL)");
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!test_bit(HCI_RUNNING, &hdev->flags))
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
hu = (struct hci_uart *) hdev->driver_data;
|
|
|
|
|
2005-08-10 03:30:28 +00:00
|
|
|
BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
hu->proto->enqueue(hu, skb);
|
|
|
|
|
|
|
|
hci_uart_tx_wakeup(hu);
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_uart_destruct(struct hci_dev *hdev)
|
|
|
|
{
|
2005-10-28 17:20:45 +00:00
|
|
|
if (!hdev)
|
|
|
|
return;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
BT_DBG("%s", hdev->name);
|
2006-09-21 14:23:19 +00:00
|
|
|
kfree(hdev->driver_data);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------ LDISC part ------ */
|
|
|
|
/* hci_uart_tty_open
|
|
|
|
*
|
|
|
|
* Called when line discipline changed to HCI_UART.
|
|
|
|
*
|
|
|
|
* Arguments:
|
|
|
|
* tty pointer to tty info structure
|
|
|
|
* Return Value:
|
|
|
|
* 0 if success, otherwise error code
|
|
|
|
*/
|
|
|
|
static int hci_uart_tty_open(struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct hci_uart *hu = (void *) tty->disc_data;
|
|
|
|
|
|
|
|
BT_DBG("tty %p", tty);
|
|
|
|
|
2010-10-22 13:11:26 +00:00
|
|
|
/* FIXME: This btw is bogus, nothing requires the old ldisc to clear
|
|
|
|
the pointer */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (hu)
|
|
|
|
return -EEXIST;
|
|
|
|
|
2010-10-22 13:11:26 +00:00
|
|
|
/* Error if the tty has no write op instead of leaving an exploitable
|
|
|
|
hole */
|
|
|
|
if (tty->ops->write == NULL)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2005-11-07 09:01:26 +00:00
|
|
|
if (!(hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
|
2006-09-21 14:23:19 +00:00
|
|
|
BT_ERR("Can't allocate control structure");
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENFILE;
|
|
|
|
}
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
tty->disc_data = hu;
|
|
|
|
hu->tty = tty;
|
[PATCH] TTY layer buffering revamp
The API and code have been through various bits of initial review by
serial driver people but they definitely need to live somewhere for a
while so the unconverted drivers can get knocked into shape, existing
drivers that have been updated can be better tuned and bugs whacked out.
This replaces the tty flip buffers with kmalloc objects in rings. In the
normal situation for an IRQ driven serial port at typical speeds the
behaviour is pretty much the same, two buffers end up allocated and the
kernel cycles between them as before.
When there are delays or at high speed we now behave far better as the
buffer pool can grow a bit rather than lose characters. This also means
that we can operate at higher speeds reliably.
For drivers that receive characters in blocks (DMA based, USB and
especially virtualisation) the layer allows a lot of driver specific
code that works around the tty layer with private secondary queues to be
removed. The IBM folks need this sort of layer, the smart serial port
people do, the virtualisers do (because a virtualised tty typically
operates at infinite speed rather than emulating 9600 baud).
Finally many drivers had invalid and unsafe attempts to avoid buffer
overflows by directly invoking tty methods extracted out of the innards
of work queue structs. These are no longer needed and all go away. That
fixes various random hangs with serial ports on overflow.
The other change in here is to optimise the receive_room path that is
used by some callers. It turns out that only one ldisc uses receive room
except asa constant and it updates it far far less than the value is
read. We thus make it a variable not a function call.
I expect the code to contain bugs due to the size alone but I'll be
watching and squashing them and feeding out new patches as it goes.
Because the buffers now dynamically expand you should only run out of
buffering when the kernel runs out of memory for real. That means a lot of
the horrible hacks high performance drivers used to do just aren't needed any
more.
Description:
tty_insert_flip_char is an old API and continues to work as before, as does
tty_flip_buffer_push() [this is why many drivers dont need modification]. It
does now also return the number of chars inserted
There are also
tty_buffer_request_room(tty, len)
which asks for a buffer block of the length requested and returns the space
found. This improves efficiency with hardware that knows how much to
transfer.
and tty_insert_flip_string_flags(tty, str, flags, len)
to insert a string of characters and flags
For a smart interface the usual code is
len = tty_request_buffer_room(tty, amount_hardware_says);
tty_insert_flip_string(tty, buffer_from_card, len);
More description!
At the moment tty buffers are attached directly to the tty. This is causing a
lot of the problems related to tty layer locking, also problems at high speed
and also with bursty data (such as occurs in virtualised environments)
I'm working on ripping out the flip buffers and replacing them with a pool of
dynamically allocated buffers. This allows both for old style "byte I/O"
devices and also helps virtualisation and smart devices where large blocks of
data suddenely materialise and need storing.
So far so good. Lots of drivers reference tty->flip.*. Several of them also
call directly and unsafely into function pointers it provides. This will all
break. Most drivers can use tty_insert_flip_char which can be kept as an API
but others need more.
At the moment I've added the following interfaces, if people think more will
be needed now is a good time to say
int tty_buffer_request_room(tty, size)
Try and ensure at least size bytes are available, returns actual room (may be
zero). At the moment it just uses the flipbuf space but that will change.
Repeated calls without characters being added are not cumulative. (ie if you
call it with 1, 1, 1, and then 4 you'll have four characters of space. The
other functions will also try and grow buffers in future but this will be a
more efficient way when you know block sizes.
int tty_insert_flip_char(tty, ch, flag)
As before insert a character if there is room. Now returns 1 for success, 0
for failure.
int tty_insert_flip_string(tty, str, len)
Insert a block of non error characters. Returns the number inserted.
int tty_prepare_flip_string(tty, strptr, len)
Adjust the buffer to allow len characters to be added. Returns a buffer
pointer in strptr and the length available. This allows for hardware that
needs to use functions like insl or mencpy_fromio.
Signed-off-by: Alan Cox <alan@redhat.com>
Cc: Paul Fulghum <paulkf@microgate.com>
Signed-off-by: Hirokazu Takata <takata@linux-m32r.org>
Signed-off-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Signed-off-by: John Hawkes <hawkes@sgi.com>
Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
Signed-off-by: Adrian Bunk <bunk@stusta.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-01-10 04:54:13 +00:00
|
|
|
tty->receive_room = 65536;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
spin_lock_init(&hu->rx_lock);
|
|
|
|
|
|
|
|
/* Flush any pending characters in the driver and line discipline. */
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* FIXME: why is this needed. Note don't use ldisc_ref here as the
|
|
|
|
open path is before the ldisc is referencable */
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2009-06-11 11:50:12 +00:00
|
|
|
if (tty->ldisc->ops->flush_buffer)
|
|
|
|
tty->ldisc->ops->flush_buffer(tty);
|
2008-04-30 07:54:13 +00:00
|
|
|
tty_driver_flush_buffer(tty);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hci_uart_tty_close()
|
|
|
|
*
|
|
|
|
* Called when the line discipline is changed to something
|
|
|
|
* else, the tty is closed, or the tty detects a hangup.
|
|
|
|
*/
|
|
|
|
static void hci_uart_tty_close(struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct hci_uart *hu = (void *)tty->disc_data;
|
|
|
|
|
|
|
|
BT_DBG("tty %p", tty);
|
|
|
|
|
|
|
|
/* Detach from the tty */
|
|
|
|
tty->disc_data = NULL;
|
|
|
|
|
|
|
|
if (hu) {
|
|
|
|
struct hci_dev *hdev = hu->hdev;
|
2007-05-09 07:15:40 +00:00
|
|
|
|
|
|
|
if (hdev)
|
|
|
|
hci_uart_close(hdev);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
|
|
|
|
hu->proto->close(hu);
|
|
|
|
hci_unregister_dev(hdev);
|
|
|
|
hci_free_dev(hdev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hci_uart_tty_wakeup()
|
|
|
|
*
|
|
|
|
* Callback for transmit wakeup. Called when low level
|
|
|
|
* device driver can accept more send data.
|
|
|
|
*
|
|
|
|
* Arguments: tty pointer to associated tty instance data
|
|
|
|
* Return Value: None
|
|
|
|
*/
|
|
|
|
static void hci_uart_tty_wakeup(struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct hci_uart *hu = (void *)tty->disc_data;
|
|
|
|
|
|
|
|
BT_DBG("");
|
|
|
|
|
|
|
|
if (!hu)
|
|
|
|
return;
|
|
|
|
|
|
|
|
clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
|
|
|
|
|
|
|
|
if (tty != hu->tty)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
|
|
|
|
hci_uart_tx_wakeup(hu);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hci_uart_tty_receive()
|
|
|
|
*
|
|
|
|
* Called by tty low level driver when receive data is
|
|
|
|
* available.
|
|
|
|
*
|
|
|
|
* Arguments: tty pointer to tty isntance data
|
|
|
|
* data pointer to received data
|
|
|
|
* flags pointer to flags for data
|
|
|
|
* count count of received data in bytes
|
|
|
|
*
|
|
|
|
* Return Value: None
|
|
|
|
*/
|
2006-09-21 14:23:19 +00:00
|
|
|
static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct hci_uart *hu = (void *)tty->disc_data;
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!hu || tty != hu->tty)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
|
|
|
|
return;
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
spin_lock(&hu->rx_lock);
|
|
|
|
hu->proto->recv(hu, (void *) data, count);
|
|
|
|
hu->hdev->stat.byte_rx += count;
|
|
|
|
spin_unlock(&hu->rx_lock);
|
|
|
|
|
2008-04-30 07:54:18 +00:00
|
|
|
tty_unthrottle(tty);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int hci_uart_register_dev(struct hci_uart *hu)
|
|
|
|
{
|
|
|
|
struct hci_dev *hdev;
|
|
|
|
|
|
|
|
BT_DBG("");
|
|
|
|
|
|
|
|
/* Initialize and register HCI device */
|
|
|
|
hdev = hci_alloc_dev();
|
|
|
|
if (!hdev) {
|
|
|
|
BT_ERR("Can't allocate HCI device");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
hu->hdev = hdev;
|
|
|
|
|
2010-02-08 14:27:07 +00:00
|
|
|
hdev->bus = HCI_UART;
|
2005-04-16 22:20:36 +00:00
|
|
|
hdev->driver_data = hu;
|
|
|
|
|
|
|
|
hdev->open = hci_uart_open;
|
|
|
|
hdev->close = hci_uart_close;
|
|
|
|
hdev->flush = hci_uart_flush;
|
|
|
|
hdev->send = hci_uart_send_frame;
|
|
|
|
hdev->destruct = hci_uart_destruct;
|
|
|
|
|
|
|
|
hdev->owner = THIS_MODULE;
|
|
|
|
|
2008-11-30 11:17:26 +00:00
|
|
|
if (!reset)
|
|
|
|
set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-07-12 14:37:04 +00:00
|
|
|
if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
|
|
|
|
set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (hci_register_dev(hdev) < 0) {
|
|
|
|
BT_ERR("Can't register HCI device");
|
|
|
|
hci_free_dev(hdev);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hci_uart_set_proto(struct hci_uart *hu, int id)
|
|
|
|
{
|
|
|
|
struct hci_uart_proto *p;
|
2005-10-28 17:20:45 +00:00
|
|
|
int err;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
p = hci_uart_get_proto(id);
|
|
|
|
if (!p)
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
|
|
|
|
err = p->open(hu);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
hu->proto = p;
|
|
|
|
|
|
|
|
err = hci_uart_register_dev(hu);
|
|
|
|
if (err) {
|
|
|
|
p->close(hu);
|
|
|
|
return err;
|
|
|
|
}
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hci_uart_tty_ioctl()
|
|
|
|
*
|
|
|
|
* Process IOCTL system call for the tty device.
|
|
|
|
*
|
|
|
|
* Arguments:
|
|
|
|
*
|
|
|
|
* tty pointer to tty instance data
|
|
|
|
* file pointer to open file object for device
|
|
|
|
* cmd IOCTL command code
|
|
|
|
* arg argument for IOCTL call (cmd dependent)
|
|
|
|
*
|
|
|
|
* Return Value: Command dependent
|
|
|
|
*/
|
|
|
|
static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
|
2005-10-28 17:20:45 +00:00
|
|
|
unsigned int cmd, unsigned long arg)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct hci_uart *hu = (void *)tty->disc_data;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
BT_DBG("");
|
|
|
|
|
|
|
|
/* Verify the status of the device */
|
|
|
|
if (!hu)
|
|
|
|
return -EBADF;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case HCIUARTSETPROTO:
|
|
|
|
if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
|
|
|
|
err = hci_uart_set_proto(hu, arg);
|
|
|
|
if (err) {
|
|
|
|
clear_bit(HCI_UART_PROTO_SET, &hu->flags);
|
|
|
|
return err;
|
|
|
|
}
|
2005-10-28 17:20:45 +00:00
|
|
|
} else
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EBUSY;
|
2007-05-09 07:15:45 +00:00
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
case HCIUARTGETPROTO:
|
|
|
|
if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
|
|
|
|
return hu->proto->id;
|
|
|
|
return -EUNATCH;
|
2005-10-28 17:20:45 +00:00
|
|
|
|
2007-05-09 07:15:35 +00:00
|
|
|
case HCIUARTGETDEVICE:
|
|
|
|
if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
|
|
|
|
return hu->hdev->id;
|
|
|
|
return -EUNATCH;
|
|
|
|
|
2010-07-12 14:37:04 +00:00
|
|
|
case HCIUARTSETFLAGS:
|
|
|
|
if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
|
|
|
|
return -EBUSY;
|
|
|
|
hu->hdev_flags = arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCIUARTGETFLAGS:
|
|
|
|
return hu->hdev_flags;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
default:
|
2008-10-13 09:44:17 +00:00
|
|
|
err = n_tty_ioctl_helper(tty, file, cmd, arg);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't provide read/write/poll interface for user space.
|
|
|
|
*/
|
2005-10-28 17:20:45 +00:00
|
|
|
static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
|
|
|
|
unsigned char __user *buf, size_t nr)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2005-10-28 17:20:45 +00:00
|
|
|
|
|
|
|
static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
|
|
|
|
const unsigned char *data, size_t count)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2005-10-28 17:20:45 +00:00
|
|
|
|
|
|
|
static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
|
|
|
|
struct file *filp, poll_table *wait)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init hci_uart_init(void)
|
|
|
|
{
|
2008-07-16 20:53:12 +00:00
|
|
|
static struct tty_ldisc_ops hci_uart_ldisc;
|
2005-04-16 22:20:36 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
BT_INFO("HCI UART driver ver %s", VERSION);
|
|
|
|
|
|
|
|
/* Register the tty discipline */
|
|
|
|
|
|
|
|
memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
|
2005-10-28 17:20:45 +00:00
|
|
|
hci_uart_ldisc.magic = TTY_LDISC_MAGIC;
|
|
|
|
hci_uart_ldisc.name = "n_hci";
|
|
|
|
hci_uart_ldisc.open = hci_uart_tty_open;
|
|
|
|
hci_uart_ldisc.close = hci_uart_tty_close;
|
|
|
|
hci_uart_ldisc.read = hci_uart_tty_read;
|
|
|
|
hci_uart_ldisc.write = hci_uart_tty_write;
|
|
|
|
hci_uart_ldisc.ioctl = hci_uart_tty_ioctl;
|
|
|
|
hci_uart_ldisc.poll = hci_uart_tty_poll;
|
|
|
|
hci_uart_ldisc.receive_buf = hci_uart_tty_receive;
|
|
|
|
hci_uart_ldisc.write_wakeup = hci_uart_tty_wakeup;
|
|
|
|
hci_uart_ldisc.owner = THIS_MODULE;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
|
|
|
|
BT_ERR("HCI line discipline registration failed. (%d)", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_BT_HCIUART_H4
|
|
|
|
h4_init();
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_BT_HCIUART_BCSP
|
|
|
|
bcsp_init();
|
|
|
|
#endif
|
2007-10-20 11:42:36 +00:00
|
|
|
#ifdef CONFIG_BT_HCIUART_LL
|
|
|
|
ll_init();
|
|
|
|
#endif
|
2010-07-19 07:04:07 +00:00
|
|
|
#ifdef CONFIG_BT_HCIUART_ATH3K
|
|
|
|
ath_init();
|
|
|
|
#endif
|
2007-10-20 11:42:36 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit hci_uart_exit(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
#ifdef CONFIG_BT_HCIUART_H4
|
|
|
|
h4_deinit();
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_BT_HCIUART_BCSP
|
|
|
|
bcsp_deinit();
|
|
|
|
#endif
|
2007-10-20 11:42:36 +00:00
|
|
|
#ifdef CONFIG_BT_HCIUART_LL
|
|
|
|
ll_deinit();
|
|
|
|
#endif
|
2010-07-19 07:04:07 +00:00
|
|
|
#ifdef CONFIG_BT_HCIUART_ATH3K
|
|
|
|
ath_deinit();
|
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Release tty registration of line discipline */
|
2005-06-23 07:10:33 +00:00
|
|
|
if ((err = tty_unregister_ldisc(N_HCI)))
|
2005-04-16 22:20:36 +00:00
|
|
|
BT_ERR("Can't unregister HCI line discipline (%d)", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(hci_uart_init);
|
|
|
|
module_exit(hci_uart_exit);
|
|
|
|
|
|
|
|
module_param(reset, bool, 0644);
|
|
|
|
MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
|
|
|
|
|
2008-08-18 11:23:53 +00:00
|
|
|
MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
|
2005-04-16 22:20:36 +00:00
|
|
|
MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
|
|
|
|
MODULE_VERSION(VERSION);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS_LDISC(N_HCI);
|