2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22 SOFTWARE IS DISCLAIMED.
26 * BlueZ HCI UART driver.
28 * $Id: hci_ldisc.c,v 1.1.1.1 2005/04/11 02:50:17 jack Exp $
32 #include <linux/config.h>
33 #include <linux/module.h>
35 #include <linux/version.h>
36 #include <linux/config.h>
37 #include <linux/kernel.h>
38 #include <linux/init.h>
39 #include <linux/sched.h>
40 #include <linux/types.h>
41 #include <linux/fcntl.h>
42 #include <linux/interrupt.h>
43 #include <linux/ptrace.h>
44 #include <linux/poll.h>
46 #include <linux/slab.h>
47 #include <linux/tty.h>
48 #include <linux/errno.h>
49 #include <linux/string.h>
50 #include <linux/signal.h>
51 #include <linux/ioctl.h>
52 #include <linux/skbuff.h>
54 #include <net/bluetooth/bluetooth.h>
55 #include <net/bluetooth/hci_core.h>
58 #ifndef HCI_UART_DEBUG
60 #define BT_DBG( A... )
62 #define BT_DMP( A... )
65 static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
67 int hci_uart_register_proto(struct hci_uart_proto *p)
69 if (p->id >= HCI_UART_MAX_PROTO)
79 int hci_uart_unregister_proto(struct hci_uart_proto *p)
81 if (p->id >= HCI_UART_MAX_PROTO)
91 static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
93 if (id >= HCI_UART_MAX_PROTO)
98 static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
100 struct hci_dev *hdev = &hu->hdev;
102 /* Update HCI stat counters */
104 case HCI_COMMAND_PKT:
108 case HCI_ACLDATA_PKT:
112 case HCI_SCODATA_PKT:
118 static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
120 struct sk_buff *skb = hu->tx_skb;
122 skb = hu->proto->dequeue(hu);
128 int hci_uart_tx_wakeup(struct hci_uart *hu)
130 struct tty_struct *tty = hu->tty;
131 struct hci_dev *hdev = &hu->hdev;
134 if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
135 set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
142 clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
144 while ((skb = hci_uart_dequeue(hu))) {
147 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
148 len = tty->driver.write(tty, 0, skb->data, skb->len);
149 hdev->stat.byte_tx += len;
157 hci_uart_tx_complete(hu, skb->pkt_type);
161 if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
164 clear_bit(HCI_UART_SENDING, &hu->tx_state);
168 /* ------- Interface to HCI layer ------ */
169 /* Initialize device */
170 static int hci_uart_open(struct hci_dev *hdev)
172 BT_DBG("%s %p", hdev->name, hdev);
174 /* Nothing to do for UART driver */
176 set_bit(HCI_RUNNING, &hdev->flags);
181 static int hci_uart_flush(struct hci_dev *hdev)
183 struct hci_uart *hu = (struct hci_uart *) hdev->driver_data;
184 struct tty_struct *tty = hu->tty;
186 BT_DBG("hdev %p tty %p", hdev, tty);
189 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
192 /* Flush any pending characters in the driver and discipline. */
193 if (tty->ldisc.flush_buffer)
194 tty->ldisc.flush_buffer(tty);
196 if (tty->driver.flush_buffer)
197 tty->driver.flush_buffer(tty);
199 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
200 hu->proto->flush(hu);
206 static int hci_uart_close(struct hci_dev *hdev)
208 BT_DBG("hdev %p", hdev);
210 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
213 hci_uart_flush(hdev);
217 /* Send frames from HCI layer */
218 static int hci_uart_send_frame(struct sk_buff *skb)
220 struct hci_dev* hdev = (struct hci_dev *) skb->dev;
221 struct tty_struct *tty;
225 BT_ERR("Frame for uknown device (hdev=NULL)");
229 if (!test_bit(HCI_RUNNING, &hdev->flags))
232 hu = (struct hci_uart *) hdev->driver_data;
235 BT_DBG("%s: type %d len %d", hdev->name, skb->pkt_type, skb->len);
237 hu->proto->enqueue(hu, skb);
239 hci_uart_tx_wakeup(hu);
243 static void hci_uart_destruct(struct hci_dev *hdev)
249 BT_DBG("%s", hdev->name);
251 hu = (struct hci_uart *) hdev->driver_data;
257 /* ------ LDISC part ------ */
260 * Called when line discipline changed to HCI_UART.
263 * tty pointer to tty info structure
265 * 0 if success, otherwise error code
267 static int hci_uart_tty_open(struct tty_struct *tty)
269 struct hci_uart *hu = (void *) tty->disc_data;
271 BT_DBG("tty %p", tty);
276 if (!(hu = kmalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
277 BT_ERR("Can't allocate controll structure");
280 memset(hu, 0, sizeof(struct hci_uart));
285 spin_lock_init(&hu->rx_lock);
287 /* Flush any pending characters in the driver and line discipline */
288 if (tty->ldisc.flush_buffer)
289 tty->ldisc.flush_buffer(tty);
291 if (tty->driver.flush_buffer)
292 tty->driver.flush_buffer(tty);
298 /* hci_uart_tty_close()
300 * Called when the line discipline is changed to something
301 * else, the tty is closed, or the tty detects a hangup.
303 static void hci_uart_tty_close(struct tty_struct *tty)
305 struct hci_uart *hu = (void *)tty->disc_data;
307 BT_DBG("tty %p", tty);
309 /* Detach from the tty */
310 tty->disc_data = NULL;
313 struct hci_dev *hdev = &hu->hdev;
314 hci_uart_close(hdev);
316 if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
317 hu->proto->close(hu);
318 hci_unregister_dev(hdev);
325 /* hci_uart_tty_wakeup()
327 * Callback for transmit wakeup. Called when low level
328 * device driver can accept more send data.
330 * Arguments: tty pointer to associated tty instance data
333 static void hci_uart_tty_wakeup(struct tty_struct *tty)
335 struct hci_uart *hu = (void *)tty->disc_data;
342 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
347 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
348 hci_uart_tx_wakeup(hu);
351 /* hci_uart_tty_room()
353 * Callback function from tty driver. Return the amount of
354 * space left in the receiver's buffer to decide if remote
355 * transmitter is to be throttled.
357 * Arguments: tty pointer to associated tty instance data
358 * Return Value: number of bytes left in receive buffer
360 static int hci_uart_tty_room (struct tty_struct *tty)
365 /* hci_uart_tty_receive()
367 * Called by tty low level driver when receive data is
370 * Arguments: tty pointer to tty isntance data
371 * data pointer to received data
372 * flags pointer to flags for data
373 * count count of received data in bytes
377 static void hci_uart_tty_receive(struct tty_struct *tty, const __u8 *data, char *flags, int count)
379 struct hci_uart *hu = (void *)tty->disc_data;
381 if (!hu || tty != hu->tty)
384 if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
387 spin_lock(&hu->rx_lock);
388 hu->proto->recv(hu, (void *) data, count);
389 hu->hdev.stat.byte_rx += count;
390 spin_unlock(&hu->rx_lock);
392 if (test_and_clear_bit(TTY_THROTTLED,&tty->flags) && tty->driver.unthrottle)
393 tty->driver.unthrottle(tty);
396 static int hci_uart_register_dev(struct hci_uart *hu)
398 struct hci_dev *hdev;
402 /* Initialize and register HCI device */
405 hdev->type = HCI_UART;
406 hdev->driver_data = hu;
408 hdev->open = hci_uart_open;
409 hdev->close = hci_uart_close;
410 hdev->flush = hci_uart_flush;
411 hdev->send = hci_uart_send_frame;
412 hdev->destruct = hci_uart_destruct;
414 if (hci_register_dev(hdev) < 0) {
415 BT_ERR("Can't register HCI device %s", hdev->name);
422 static int hci_uart_set_proto(struct hci_uart *hu, int id)
424 struct hci_uart_proto *p;
427 p = hci_uart_get_proto(id);
429 return -EPROTONOSUPPORT;
437 err = hci_uart_register_dev(hu);
445 /* hci_uart_tty_ioctl()
447 * Process IOCTL system call for the tty device.
451 * tty pointer to tty instance data
452 * file pointer to open file object for device
453 * cmd IOCTL command code
454 * arg argument for IOCTL call (cmd dependent)
456 * Return Value: Command dependent
458 static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
459 unsigned int cmd, unsigned long arg)
461 struct hci_uart *hu = (void *)tty->disc_data;
466 /* Verify the status of the device */
471 case HCIUARTSETPROTO:
472 if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
473 err = hci_uart_set_proto(hu, arg);
475 clear_bit(HCI_UART_PROTO_SET, &hu->flags);
478 tty->low_latency = 1;
482 case HCIUARTGETPROTO:
483 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
484 return hu->proto->id;
488 err = n_tty_ioctl(tty, file, cmd, arg);
496 * We don't provide read/write/poll interface for user space.
498 static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file, unsigned char *buf, size_t nr)
502 static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file, const unsigned char *data, size_t count)
506 static unsigned int hci_uart_tty_poll(struct tty_struct *tty, struct file *filp, poll_table *wait)
511 #ifdef CONFIG_BLUEZ_HCIUART_H4
515 #ifdef CONFIG_BLUEZ_HCIUART_BCSP
517 int bcsp_deinit(void);
520 int __init hci_uart_init(void)
522 static struct tty_ldisc hci_uart_ldisc;
525 BT_INFO("BlueZ HCI UART driver ver %s Copyright (C) 2000,2001 Qualcomm Inc",
527 BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
529 /* Register the tty discipline */
531 memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
532 hci_uart_ldisc.magic = TTY_LDISC_MAGIC;
533 hci_uart_ldisc.name = "n_hci";
534 hci_uart_ldisc.open = hci_uart_tty_open;
535 hci_uart_ldisc.close = hci_uart_tty_close;
536 hci_uart_ldisc.read = hci_uart_tty_read;
537 hci_uart_ldisc.write = hci_uart_tty_write;
538 hci_uart_ldisc.ioctl = hci_uart_tty_ioctl;
539 hci_uart_ldisc.poll = hci_uart_tty_poll;
540 hci_uart_ldisc.receive_room= hci_uart_tty_room;
541 hci_uart_ldisc.receive_buf = hci_uart_tty_receive;
542 hci_uart_ldisc.write_wakeup= hci_uart_tty_wakeup;
544 if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
545 BT_ERR("Can't register HCI line discipline (%d)", err);
549 #ifdef CONFIG_BLUEZ_HCIUART_H4
552 #ifdef CONFIG_BLUEZ_HCIUART_BCSP
559 void hci_uart_cleanup(void)
563 #ifdef CONFIG_BLUEZ_HCIUART_H4
566 #ifdef CONFIG_BLUEZ_HCIUART_BCSP
570 /* Release tty registration of line discipline */
571 if ((err = tty_register_ldisc(N_HCI, NULL)))
572 BT_ERR("Can't unregister HCI line discipline (%d)", err);
575 module_init(hci_uart_init);
576 module_exit(hci_uart_cleanup);
578 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
579 MODULE_DESCRIPTION("BlueZ HCI UART driver ver " VERSION);
580 MODULE_LICENSE("GPL");