1/*
2 *
3 *  Bluetooth HCI UART driver
4 *
5 *  Copyright (C) 2000-2001  Qualcomm Incorporated
6 *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
7 *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
8 *
9 *
10 *  This program is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License as published by
12 *  the Free Software Foundation; either version 2 of the License, or
13 *  (at your option) any later version.
14 *
15 *  This program is distributed in the hope that it will be useful,
16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *  GNU General Public License for more details.
19 *
20 *  You should have received a copy of the GNU General Public License
21 *  along with this program; if not, write to the Free Software
22 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 *
24 */
25
26#include <linux/module.h>
27
28#include <linux/kernel.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/fcntl.h>
32#include <linux/interrupt.h>
33#include <linux/ptrace.h>
34#include <linux/poll.h>
35
36#include <linux/slab.h>
37#include <linux/tty.h>
38#include <linux/errno.h>
39#include <linux/string.h>
40#include <linux/signal.h>
41#include <linux/ioctl.h>
42#include <linux/skbuff.h>
43
44#include <net/bluetooth/bluetooth.h>
45#include <net/bluetooth/hci_core.h>
46
47#include "hci_uart.h"
48
49#ifndef CONFIG_BT_HCIUART_DEBUG
50#undef  BT_DBG
51#define BT_DBG( A... )
52#endif
53
54#define VERSION "2.2"
55
56static int reset = 0;
57
58static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
59
60int hci_uart_register_proto(struct hci_uart_proto *p)
61{
62	if (p->id >= HCI_UART_MAX_PROTO)
63		return -EINVAL;
64
65	if (hup[p->id])
66		return -EEXIST;
67
68	hup[p->id] = p;
69
70	return 0;
71}
72
73int hci_uart_unregister_proto(struct hci_uart_proto *p)
74{
75	if (p->id >= HCI_UART_MAX_PROTO)
76		return -EINVAL;
77
78	if (!hup[p->id])
79		return -EINVAL;
80
81	hup[p->id] = NULL;
82
83	return 0;
84}
85
86static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
87{
88	if (id >= HCI_UART_MAX_PROTO)
89		return NULL;
90
91	return hup[id];
92}
93
94static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
95{
96	struct hci_dev *hdev = hu->hdev;
97
98	/* Update HCI stat counters */
99	switch (pkt_type) {
100	case HCI_COMMAND_PKT:
101		hdev->stat.cmd_tx++;
102		break;
103
104	case HCI_ACLDATA_PKT:
105		hdev->stat.acl_tx++;
106		break;
107
108	case HCI_SCODATA_PKT:
109		hdev->stat.cmd_tx++;
110		break;
111	}
112}
113
114static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
115{
116	struct sk_buff *skb = hu->tx_skb;
117
118	if (!skb)
119		skb = hu->proto->dequeue(hu);
120	else
121		hu->tx_skb = NULL;
122
123	return skb;
124}
125
126int hci_uart_tx_wakeup(struct hci_uart *hu)
127{
128	struct tty_struct *tty = hu->tty;
129	struct hci_dev *hdev = hu->hdev;
130	struct sk_buff *skb;
131
132	if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
133		set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
134		return 0;
135	}
136
137	BT_DBG("");
138
139restart:
140	clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
141
142	while ((skb = hci_uart_dequeue(hu))) {
143		int len;
144
145		set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
146		len = tty->driver->write(tty, skb->data, skb->len);
147		hdev->stat.byte_tx += len;
148
149		skb_pull(skb, len);
150		if (skb->len) {
151			hu->tx_skb = skb;
152			break;
153		}
154
155		hci_uart_tx_complete(hu, bt_cb(skb)->pkt_type);
156		kfree_skb(skb);
157	}
158
159	if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
160		goto restart;
161
162	clear_bit(HCI_UART_SENDING, &hu->tx_state);
163	return 0;
164}
165
166/* ------- Interface to HCI layer ------ */
167/* Initialize device */
168static int hci_uart_open(struct hci_dev *hdev)
169{
170	BT_DBG("%s %p", hdev->name, hdev);
171
172	/* Nothing to do for UART driver */
173
174	set_bit(HCI_RUNNING, &hdev->flags);
175
176	return 0;
177}
178
179/* Reset device */
180static int hci_uart_flush(struct hci_dev *hdev)
181{
182	struct hci_uart *hu  = (struct hci_uart *) hdev->driver_data;
183	struct tty_struct *tty = hu->tty;
184
185	BT_DBG("hdev %p tty %p", hdev, tty);
186
187	if (hu->tx_skb) {
188		kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
189	}
190
191	/* Flush any pending characters in the driver and discipline. */
192	tty_ldisc_flush(tty);
193	if (tty->driver && tty->driver->flush_buffer)
194		tty->driver->flush_buffer(tty);
195
196	if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
197		hu->proto->flush(hu);
198
199	return 0;
200}
201
202/* Close device */
203static int hci_uart_close(struct hci_dev *hdev)
204{
205	BT_DBG("hdev %p", hdev);
206
207	if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
208		return 0;
209
210	hci_uart_flush(hdev);
211	return 0;
212}
213
214/* Send frames from HCI layer */
215static int hci_uart_send_frame(struct sk_buff *skb)
216{
217	struct hci_dev* hdev = (struct hci_dev *) skb->dev;
218	struct tty_struct *tty;
219	struct hci_uart *hu;
220
221	if (!hdev) {
222		BT_ERR("Frame for uknown device (hdev=NULL)");
223		return -ENODEV;
224	}
225
226	if (!test_bit(HCI_RUNNING, &hdev->flags))
227		return -EBUSY;
228
229	hu = (struct hci_uart *) hdev->driver_data;
230	tty = hu->tty;
231
232	BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
233
234	hu->proto->enqueue(hu, skb);
235
236	hci_uart_tx_wakeup(hu);
237
238	return 0;
239}
240
241static void hci_uart_destruct(struct hci_dev *hdev)
242{
243	if (!hdev)
244		return;
245
246	BT_DBG("%s", hdev->name);
247	kfree(hdev->driver_data);
248}
249
250/* ------ LDISC part ------ */
251/* hci_uart_tty_open
252 *
253 *     Called when line discipline changed to HCI_UART.
254 *
255 * Arguments:
256 *     tty    pointer to tty info structure
257 * Return Value:
258 *     0 if success, otherwise error code
259 */
260static int hci_uart_tty_open(struct tty_struct *tty)
261{
262	struct hci_uart *hu = (void *) tty->disc_data;
263
264	BT_DBG("tty %p", tty);
265
266	if (hu)
267		return -EEXIST;
268
269	if (!(hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
270		BT_ERR("Can't allocate control structure");
271		return -ENFILE;
272	}
273
274	tty->disc_data = hu;
275	hu->tty = tty;
276	tty->receive_room = 65536;
277
278	spin_lock_init(&hu->rx_lock);
279
280	/* Flush any pending characters in the driver and line discipline. */
281
282
283	if (tty->ldisc.flush_buffer)
284		tty->ldisc.flush_buffer(tty);
285
286	if (tty->driver && tty->driver->flush_buffer)
287		tty->driver->flush_buffer(tty);
288
289	return 0;
290}
291
292/* hci_uart_tty_close()
293 *
294 *    Called when the line discipline is changed to something
295 *    else, the tty is closed, or the tty detects a hangup.
296 */
297static void hci_uart_tty_close(struct tty_struct *tty)
298{
299	struct hci_uart *hu = (void *)tty->disc_data;
300
301	BT_DBG("tty %p", tty);
302
303	/* Detach from the tty */
304	tty->disc_data = NULL;
305
306	if (hu) {
307		struct hci_dev *hdev = hu->hdev;
308
309		if (hdev)
310			hci_uart_close(hdev);
311
312		if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
313			hu->proto->close(hu);
314			hci_unregister_dev(hdev);
315			hci_free_dev(hdev);
316		}
317	}
318}
319
320/* hci_uart_tty_wakeup()
321 *
322 *    Callback for transmit wakeup. Called when low level
323 *    device driver can accept more send data.
324 *
325 * Arguments:        tty    pointer to associated tty instance data
326 * Return Value:    None
327 */
328static void hci_uart_tty_wakeup(struct tty_struct *tty)
329{
330	struct hci_uart *hu = (void *)tty->disc_data;
331
332	BT_DBG("");
333
334	if (!hu)
335		return;
336
337	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
338
339	if (tty != hu->tty)
340		return;
341
342	if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
343		hci_uart_tx_wakeup(hu);
344}
345
346/* hci_uart_tty_receive()
347 *
348 *     Called by tty low level driver when receive data is
349 *     available.
350 *
351 * Arguments:  tty          pointer to tty isntance data
352 *             data         pointer to received data
353 *             flags        pointer to flags for data
354 *             count        count of received data in bytes
355 *
356 * Return Value:    None
357 */
358static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
359{
360	struct hci_uart *hu = (void *)tty->disc_data;
361
362	if (!hu || tty != hu->tty)
363		return;
364
365	if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
366		return;
367
368	spin_lock(&hu->rx_lock);
369	hu->proto->recv(hu, (void *) data, count);
370	hu->hdev->stat.byte_rx += count;
371	spin_unlock(&hu->rx_lock);
372
373	if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
374					tty->driver->unthrottle)
375		tty->driver->unthrottle(tty);
376}
377
378static int hci_uart_register_dev(struct hci_uart *hu)
379{
380	struct hci_dev *hdev;
381
382	BT_DBG("");
383
384	/* Initialize and register HCI device */
385	hdev = hci_alloc_dev();
386	if (!hdev) {
387		BT_ERR("Can't allocate HCI device");
388		return -ENOMEM;
389	}
390
391	hu->hdev = hdev;
392
393	hdev->type = HCI_UART;
394	hdev->driver_data = hu;
395
396	hdev->open  = hci_uart_open;
397	hdev->close = hci_uart_close;
398	hdev->flush = hci_uart_flush;
399	hdev->send  = hci_uart_send_frame;
400	hdev->destruct = hci_uart_destruct;
401
402	hdev->owner = THIS_MODULE;
403
404	if (reset)
405		set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
406
407	if (hci_register_dev(hdev) < 0) {
408		BT_ERR("Can't register HCI device");
409		hci_free_dev(hdev);
410		return -ENODEV;
411	}
412
413	return 0;
414}
415
416static int hci_uart_set_proto(struct hci_uart *hu, int id)
417{
418	struct hci_uart_proto *p;
419	int err;
420
421	p = hci_uart_get_proto(id);
422	if (!p)
423		return -EPROTONOSUPPORT;
424
425	err = p->open(hu);
426	if (err)
427		return err;
428
429	hu->proto = p;
430
431	err = hci_uart_register_dev(hu);
432	if (err) {
433		p->close(hu);
434		return err;
435	}
436
437	return 0;
438}
439
440/* hci_uart_tty_ioctl()
441 *
442 *    Process IOCTL system call for the tty device.
443 *
444 * Arguments:
445 *
446 *    tty        pointer to tty instance data
447 *    file       pointer to open file object for device
448 *    cmd        IOCTL command code
449 *    arg        argument for IOCTL call (cmd dependent)
450 *
451 * Return Value:    Command dependent
452 */
453static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
454					unsigned int cmd, unsigned long arg)
455{
456	struct hci_uart *hu = (void *)tty->disc_data;
457	int err = 0;
458
459	BT_DBG("");
460
461	/* Verify the status of the device */
462	if (!hu)
463		return -EBADF;
464
465	switch (cmd) {
466	case HCIUARTSETPROTO:
467		if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
468			err = hci_uart_set_proto(hu, arg);
469			if (err) {
470				clear_bit(HCI_UART_PROTO_SET, &hu->flags);
471				return err;
472			}
473			tty->low_latency = 1;
474		} else
475			return -EBUSY;
476		break;
477
478	case HCIUARTGETPROTO:
479		if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
480			return hu->proto->id;
481		return -EUNATCH;
482
483	case HCIUARTGETDEVICE:
484		if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
485			return hu->hdev->id;
486		return -EUNATCH;
487
488	default:
489		err = n_tty_ioctl(tty, file, cmd, arg);
490		break;
491	};
492
493	return err;
494}
495
496/*
497 * We don't provide read/write/poll interface for user space.
498 */
499static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
500					unsigned char __user *buf, size_t nr)
501{
502	return 0;
503}
504
505static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
506					const unsigned char *data, size_t count)
507{
508	return 0;
509}
510
511static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
512					struct file *filp, poll_table *wait)
513{
514	return 0;
515}
516
517static int __init hci_uart_init(void)
518{
519	static struct tty_ldisc hci_uart_ldisc;
520	int err;
521
522	BT_INFO("HCI UART driver ver %s", VERSION);
523
524	/* Register the tty discipline */
525
526	memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
527	hci_uart_ldisc.magic		= TTY_LDISC_MAGIC;
528	hci_uart_ldisc.name		= "n_hci";
529	hci_uart_ldisc.open		= hci_uart_tty_open;
530	hci_uart_ldisc.close		= hci_uart_tty_close;
531	hci_uart_ldisc.read		= hci_uart_tty_read;
532	hci_uart_ldisc.write		= hci_uart_tty_write;
533	hci_uart_ldisc.ioctl		= hci_uart_tty_ioctl;
534	hci_uart_ldisc.poll		= hci_uart_tty_poll;
535	hci_uart_ldisc.receive_buf	= hci_uart_tty_receive;
536	hci_uart_ldisc.write_wakeup	= hci_uart_tty_wakeup;
537	hci_uart_ldisc.owner		= THIS_MODULE;
538
539	if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
540		BT_ERR("HCI line discipline registration failed. (%d)", err);
541		return err;
542	}
543
544#ifdef CONFIG_BT_HCIUART_H4
545	h4_init();
546#endif
547#ifdef CONFIG_BT_HCIUART_BCSP
548	bcsp_init();
549#endif
550
551	return 0;
552}
553
554static void __exit hci_uart_exit(void)
555{
556	int err;
557
558#ifdef CONFIG_BT_HCIUART_H4
559	h4_deinit();
560#endif
561#ifdef CONFIG_BT_HCIUART_BCSP
562	bcsp_deinit();
563#endif
564
565	/* Release tty registration of line discipline */
566	if ((err = tty_unregister_ldisc(N_HCI)))
567		BT_ERR("Can't unregister HCI line discipline (%d)", err);
568}
569
570module_init(hci_uart_init);
571module_exit(hci_uart_exit);
572
573module_param(reset, bool, 0644);
574MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
575
576MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
577MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
578MODULE_VERSION(VERSION);
579MODULE_LICENSE("GPL");
580MODULE_ALIAS_LDISC(N_HCI);
581