summaryrefslogtreecommitdiff
path: root/drivers/bluetooth/hci_ath.c
blob: 959b044404f0d15255a9124975459e10bf8b7fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * Copyright (c) 2009-2010 Atheros Communications Inc.
 *
 *  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
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>

#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/skbuff.h>

#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>

#include "hci_uart.h"

#ifdef DEBUG
#define ATH_DBG(fmt, arg...)   printk(KERN_ERR "[ATH_DBG] (%s) <%s>: " fmt "\n" , __FILE__ , __func__ , ## arg)
#define ATH_INFO(fmt, arg...)  printk(KERN_INFO "[ATH_DBG] (%s) <%s>: " fmt "\n" , __FILE__ , __func__ , ## arg)
#else
#define ATH_DBG(fmt, arg...)
#define ATH_INFO(fmt, arg...)
#endif


/* HCIATH receiver States */
#define HCIATH_W4_PACKET_TYPE			0
#define HCIATH_W4_EVENT_HDR			1
#define HCIATH_W4_ACL_HDR			2
#define HCIATH_W4_SCO_HDR			3
#define HCIATH_W4_DATA				4

struct ath_struct {
	struct hci_uart *hu;
	unsigned int rx_state;
	unsigned int rx_count;
	unsigned int cur_sleep;

	spinlock_t hciath_lock;
	struct sk_buff *rx_skb;
	struct sk_buff_head txq;
	wait_queue_head_t wqevt;
	struct work_struct ctxtsw;
};

int ath_wakeup_ar3001(struct tty_struct *tty)
{
	struct termios settings;
	int status = 0x00;
	mm_segment_t oldfs;
	status = tty->driver->ops->tiocmget(tty, NULL);

	ATH_DBG("");

	if ((status & TIOCM_CTS))
		return status;

	oldfs = get_fs();
	set_fs(KERNEL_DS);
	n_tty_ioctl_helper(tty, NULL, TCGETS, (unsigned long)&settings);

	settings.c_cflag &= ~CRTSCTS;
	n_tty_ioctl_helper(tty, NULL, TCSETS, (unsigned long)&settings);
	set_fs(oldfs);
	status = tty->driver->ops->tiocmget(tty, NULL);

	/* Wake up board */
	tty->driver->ops->tiocmset(tty, NULL, 0x00, TIOCM_RTS);
	mdelay(20);

	status = tty->driver->ops->tiocmget(tty, NULL);

	tty->driver->ops->tiocmset(tty, NULL, TIOCM_RTS, 0x00);
	mdelay(20);

	status = tty->driver->ops->tiocmget(tty, NULL);
	oldfs = get_fs();
	set_fs(KERNEL_DS);
	n_tty_ioctl_helper(tty, NULL, TCGETS, (unsigned long)&settings);

	settings.c_cflag |= CRTSCTS;
	n_tty_ioctl_helper(tty, NULL, TCSETS, (unsigned long)&settings);
	set_fs(oldfs);
	return status;
}

static void ath_context_switch(struct work_struct *work)
{
	int status;
	struct ath_struct *ath;
	struct hci_uart *hu;
	struct tty_struct *tty;

	ATH_DBG("");

	ath = container_of(work, struct ath_struct, ctxtsw);

	hu = ath->hu;
	tty = hu->tty;

	/* verify and wake up controller */
	if (ath->cur_sleep) {

		status = ath_wakeup_ar3001(tty);
		if (!(status & TIOCM_CTS))
			return;
	}

	/* Ready to send Data */
	clear_bit(HCI_UART_SENDING, &hu->tx_state);
	hci_uart_tx_wakeup(hu);
}

int ath_check_sleep_cmd(struct ath_struct *ath, unsigned char *packet)
{
	ATH_DBG("");

	if (packet[0] == 0x04 && packet[1] == 0xFC)
		ath->cur_sleep = packet[3];

	ATH_DBG("ath->cur_sleep:%d\n", ath->cur_sleep);

	return 0;
}


/* Initialize protocol */
static int ath_open(struct hci_uart *hu)
{
	struct ath_struct *ath;
	BT_DBG("hu %p", hu);
	ATH_INFO("hu %p", hu);

	ath = kzalloc(sizeof(*ath), GFP_ATOMIC);
	if (!ath)
		return -ENOMEM;

	skb_queue_head_init(&ath->txq);
	spin_lock_init(&ath->hciath_lock);

	ath->cur_sleep = 0;
	hu->priv = ath;
	ath->hu = hu;

	init_waitqueue_head(&ath->wqevt);
	INIT_WORK(&ath->ctxtsw, ath_context_switch);
	return 0;
}

/* Flush protocol data */
static int ath_flush(struct hci_uart *hu)
{
	struct ath_struct *ath = hu->priv;
	BT_DBG("hu %p", hu);
	ATH_INFO("hu %p", hu);
	skb_queue_purge(&ath->txq);

	return 0;
}

/* Close protocol */
static int ath_close(struct hci_uart *hu)
{
	struct ath_struct *ath = hu->priv;
	BT_DBG("hu %p", hu);
	ATH_INFO("hu %p", hu);

	skb_queue_purge(&ath->txq);

	if (ath->rx_skb)
		kfree_skb(ath->rx_skb);

	wake_up_interruptible(&ath->wqevt);
	hu->priv = NULL;
	kfree(ath);
	return 0;
}

/* Enqueue frame for transmittion */
static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb)
{
	struct ath_struct *ath = hu->priv;
	if (bt_cb(skb)->pkt_type == HCI_SCODATA_PKT) {

		/* Discard SCO packet.AR3001 does not support SCO over HCI */
		BT_DBG("SCO Packet over HCI received Dropping\n");
		kfree(skb);
		return 0;
	}
	BT_DBG("hu %p skb %p", hu, skb);
	ATH_DBG("hu %p skb %p", hu, skb);

	/* Prepend skb with frame type */
	memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);

	skb_queue_tail(&ath->txq, skb);
	set_bit(HCI_UART_SENDING, &hu->tx_state);

	schedule_work(&ath->ctxtsw);
	return 0;
}

static struct sk_buff *ath_dequeue(struct hci_uart *hu)
{
	struct ath_struct *ath = hu->priv;
	struct sk_buff *skbuf;

	ATH_DBG("");

	skbuf = skb_dequeue(&ath->txq);
	if (skbuf != NULL)
		ath_check_sleep_cmd(ath, &skbuf->data[1]);

	return skbuf;
}

static inline int ath_check_data_len(struct ath_struct *ath, int len)
{
	register int room = skb_tailroom(ath->rx_skb);
	BT_DBG("len %d room %d", len, room);
	ATH_DBG("len %d room %d", len, room);

	if (len > room) {
		BT_ERR("Data length is too large");
		kfree_skb(ath->rx_skb);
		ath->rx_state = HCIATH_W4_PACKET_TYPE;
		ath->rx_skb = NULL;
		ath->rx_count = 0;
	} else {
		ath->rx_state = HCIATH_W4_DATA;
		ath->rx_count = len;
		return len;
	}

	return 0;
}

/* Recv data */
static int ath_recv(struct hci_uart *hu, void *data, int count)
{
	struct ath_struct *ath = hu->priv;
	register char *ptr;
	struct hci_event_hdr *eh;
	struct hci_acl_hdr *ah;
	struct hci_sco_hdr *sh;
	struct sk_buff *skbuf;
	register int len, type, dlen;

	skbuf = NULL;
	BT_DBG("hu %p count %d rx_state %d rx_count %d", hu, count,
	       ath->rx_state, ath->rx_count);
	ATH_DBG("hu %p count %d rx_state %d rx_count %d", hu, count,
	       ath->rx_state, ath->rx_count);
	ptr = data;
	while (count) {
		if (ath->rx_count) {

			len = min_t(unsigned int, ath->rx_count, count);
			memcpy(skb_put(ath->rx_skb, len), ptr, len);
			ath->rx_count -= len;
			count -= len;
			ptr += len;

			if (ath->rx_count)
				continue;
			switch (ath->rx_state) {
			case HCIATH_W4_DATA:
				hci_recv_frame(ath->rx_skb);
				ath->rx_state = HCIATH_W4_PACKET_TYPE;
				ath->rx_skb = NULL;
				ath->rx_count = 0;
				continue;
			case HCIATH_W4_EVENT_HDR:
				eh = (struct hci_event_hdr *)ath->rx_skb->data;
				BT_DBG("Event header: evt 0x%2.2x plen %d",
				       eh->evt, eh->plen);
				ATH_DBG("Event header: evt 0x%2.2x plen %d",
				       eh->evt, eh->plen);
				ath_check_data_len(ath, eh->plen);
				continue;
			case HCIATH_W4_ACL_HDR:
				ah = (struct hci_acl_hdr *)ath->rx_skb->data;
				dlen = __le16_to_cpu(ah->dlen);
				BT_DBG("ACL header: dlen %d", dlen);
				ATH_DBG("ACL header: dlen %d", dlen);
				ath_check_data_len(ath, dlen);
				continue;
			case HCIATH_W4_SCO_HDR:
				sh = (struct hci_sco_hdr *)ath->rx_skb->data;
				BT_DBG("SCO header: dlen %d", sh->dlen);
				ATH_DBG("SCO header: dlen %d", sh->dlen);
				ath_check_data_len(ath, sh->dlen);
				continue;
			}
		}

		/* HCIATH_W4_PACKET_TYPE */
		switch (*ptr) {
		case HCI_EVENT_PKT:
			BT_DBG("Event packet");
			ATH_DBG("Event packet");
			ath->rx_state = HCIATH_W4_EVENT_HDR;
			ath->rx_count = HCI_EVENT_HDR_SIZE;
			type = HCI_EVENT_PKT;
			break;
		case HCI_ACLDATA_PKT:
			BT_DBG("ACL packet");
			ATH_DBG("ACL packet");
			ath->rx_state = HCIATH_W4_ACL_HDR;
			ath->rx_count = HCI_ACL_HDR_SIZE;
			type = HCI_ACLDATA_PKT;
			break;
		case HCI_SCODATA_PKT:
			BT_DBG("SCO packet");
			ATH_DBG("SCO packet");
			ath->rx_state = HCIATH_W4_SCO_HDR;
			ath->rx_count = HCI_SCO_HDR_SIZE;
			type = HCI_SCODATA_PKT;
			break;
		default:
			BT_ERR("Unknown HCI packet type %2.2x", (__u8) *ptr);
			hu->hdev->stat.err_rx++;
			ptr++;
			count--;
			continue;
		};
		ptr++;
		count--;

		/* Allocate packet */
		ath->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
		if (!ath->rx_skb) {
			BT_ERR("Can't allocate mem for new packet");
			ath->rx_state = HCIATH_W4_PACKET_TYPE;
			ath->rx_count = 0;
			return -ENOMEM;
		}
		ath->rx_skb->dev = (void *)hu->hdev;
		bt_cb(ath->rx_skb)->pkt_type = type;
	} return count;
}

static struct hci_uart_proto athp = {
	.id = HCI_UART_ATH,
	.open = ath_open,
	.close = ath_close,
	.recv = ath_recv,
	.enqueue = ath_enqueue,
	.dequeue = ath_dequeue,
	.flush = ath_flush,
};

int ath_init(void)
{
	int err = hci_uart_register_proto(&athp);
	ATH_INFO("");
	if (!err)
		BT_INFO("HCIATH protocol initialized");
	else
		BT_ERR("HCIATH protocol registration failed with err %d", err);
	return err;
}

int ath_deinit(void)
{
	ATH_INFO("");
	return hci_uart_unregister_proto(&athp);
}