blob: c50b68bbecdc48f46df0641e02898281a23de2d5 [file] [log] [blame]
Suraj Sumangalab3190df2010-07-19 12:34:07 +05301/*
2 * Atheros Communication Bluetooth HCIATH3K UART protocol
3 *
4 * HCIATH3K (HCI Atheros AR300x Protocol) is a Atheros Communication's
5 * power management protocol extension to H4 to support AR300x Bluetooth Chip.
6 *
7 * Copyright (c) 2009-2010 Atheros Communications Inc.
8 *
9 * Acknowledgements:
10 * This file is based on hci_h4.c, which was written
11 * by Maxim Krasnyansky and Marcel Holtmann.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
29#include <linux/module.h>
30#include <linux/kernel.h>
31
32#include <linux/init.h>
33#include <linux/slab.h>
34#include <linux/tty.h>
35#include <linux/errno.h>
36#include <linux/ioctl.h>
37#include <linux/skbuff.h>
38
39#include <net/bluetooth/bluetooth.h>
40#include <net/bluetooth/hci_core.h>
41
42#include "hci_uart.h"
43
44struct ath_struct {
45 struct hci_uart *hu;
46 unsigned int cur_sleep;
47
Marcel Holtmannd90aa682015-04-04 21:59:26 -070048 struct sk_buff *rx_skb;
Suraj Sumangalab3190df2010-07-19 12:34:07 +053049 struct sk_buff_head txq;
50 struct work_struct ctxtsw;
51};
52
53static int ath_wakeup_ar3k(struct tty_struct *tty)
54{
Alan Coxafaae082011-02-14 16:28:18 +000055 int status = tty->driver->ops->tiocmget(tty);
Suraj Sumangalab3190df2010-07-19 12:34:07 +053056
57 if (status & TIOCM_CTS)
58 return status;
59
Suraj Sumangalab3190df2010-07-19 12:34:07 +053060 /* Clear RTS first */
Peter Hurley632f32e2015-01-25 14:44:54 -050061 tty->driver->ops->tiocmget(tty);
Alan Coxafaae082011-02-14 16:28:18 +000062 tty->driver->ops->tiocmset(tty, 0x00, TIOCM_RTS);
Suraj Sumangalab3190df2010-07-19 12:34:07 +053063 mdelay(20);
64
65 /* Set RTS, wake up board */
Peter Hurley632f32e2015-01-25 14:44:54 -050066 tty->driver->ops->tiocmget(tty);
Alan Coxafaae082011-02-14 16:28:18 +000067 tty->driver->ops->tiocmset(tty, TIOCM_RTS, 0x00);
Suraj Sumangalab3190df2010-07-19 12:34:07 +053068 mdelay(20);
69
Alan Coxafaae082011-02-14 16:28:18 +000070 status = tty->driver->ops->tiocmget(tty);
Suraj Sumangalab3190df2010-07-19 12:34:07 +053071 return status;
72}
73
74static void ath_hci_uart_work(struct work_struct *work)
75{
76 int status;
77 struct ath_struct *ath;
78 struct hci_uart *hu;
79 struct tty_struct *tty;
80
81 ath = container_of(work, struct ath_struct, ctxtsw);
82
83 hu = ath->hu;
84 tty = hu->tty;
85
86 /* verify and wake up controller */
87 if (ath->cur_sleep) {
88 status = ath_wakeup_ar3k(tty);
89 if (!(status & TIOCM_CTS))
90 return;
91 }
92
93 /* Ready to send Data */
94 clear_bit(HCI_UART_SENDING, &hu->tx_state);
95 hci_uart_tx_wakeup(hu);
96}
97
Suraj Sumangalab3190df2010-07-19 12:34:07 +053098static int ath_open(struct hci_uart *hu)
99{
100 struct ath_struct *ath;
101
102 BT_DBG("hu %p", hu);
103
Vladis Dronov58a01b02019-07-30 11:33:45 +0200104 if (!hci_uart_has_flow_control(hu))
105 return -EOPNOTSUPP;
106
David Herrmannf5fd5ba2012-01-07 15:19:40 +0100107 ath = kzalloc(sizeof(*ath), GFP_KERNEL);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530108 if (!ath)
109 return -ENOMEM;
110
111 skb_queue_head_init(&ath->txq);
112
113 hu->priv = ath;
114 ath->hu = hu;
115
116 INIT_WORK(&ath->ctxtsw, ath_hci_uart_work);
117
118 return 0;
119}
120
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530121static int ath_close(struct hci_uart *hu)
122{
123 struct ath_struct *ath = hu->priv;
124
125 BT_DBG("hu %p", hu);
126
127 skb_queue_purge(&ath->txq);
128
Marcel Holtmannd90aa682015-04-04 21:59:26 -0700129 kfree_skb(ath->rx_skb);
130
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530131 cancel_work_sync(&ath->ctxtsw);
132
133 hu->priv = NULL;
134 kfree(ath);
135
136 return 0;
137}
138
Marcel Holtmannc0ba7ac2015-04-11 05:35:37 -0700139static int ath_flush(struct hci_uart *hu)
140{
141 struct ath_struct *ath = hu->priv;
142
143 BT_DBG("hu %p", hu);
144
145 skb_queue_purge(&ath->txq);
146
147 return 0;
148}
149
Marcel Holtmann4c876c02015-04-11 05:35:38 -0700150static int ath_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
151{
152 struct sk_buff *skb;
153 u8 buf[10];
154 int err;
155
156 buf[0] = 0x01;
157 buf[1] = 0x01;
158 buf[2] = 0x00;
159 buf[3] = sizeof(bdaddr_t);
160 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
161
162 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
163 if (IS_ERR(skb)) {
164 err = PTR_ERR(skb);
165 BT_ERR("%s: Change address command failed (%d)",
166 hdev->name, err);
167 return err;
168 }
169 kfree_skb(skb);
170
171 return 0;
172}
173
174static int ath_setup(struct hci_uart *hu)
175{
176 BT_DBG("hu %p", hu);
177
178 hu->hdev->set_bdaddr = ath_set_bdaddr;
179
180 return 0;
181}
182
Marcel Holtmannc0ba7ac2015-04-11 05:35:37 -0700183static const struct h4_recv_pkt ath_recv_pkts[] = {
184 { H4_RECV_ACL, .recv = hci_recv_frame },
185 { H4_RECV_SCO, .recv = hci_recv_frame },
186 { H4_RECV_EVENT, .recv = hci_recv_frame },
187};
188
189static int ath_recv(struct hci_uart *hu, const void *data, int count)
190{
191 struct ath_struct *ath = hu->priv;
192
193 ath->rx_skb = h4_recv_buf(hu->hdev, ath->rx_skb, data, count,
194 ath_recv_pkts, ARRAY_SIZE(ath_recv_pkts));
195 if (IS_ERR(ath->rx_skb)) {
196 int err = PTR_ERR(ath->rx_skb);
197 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
Chan-yeol Park37134162015-06-17 21:10:39 +0900198 ath->rx_skb = NULL;
Marcel Holtmannc0ba7ac2015-04-11 05:35:37 -0700199 return err;
200 }
201
202 return count;
203}
204
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530205#define HCI_OP_ATH_SLEEP 0xFC04
206
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530207static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb)
208{
209 struct ath_struct *ath = hu->priv;
210
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100211 if (hci_skb_pkt_type(skb) == HCI_SCODATA_PKT) {
Dan Carpenter4ebaa4e2010-07-23 12:11:04 +0200212 kfree_skb(skb);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530213 return 0;
214 }
215
Marcel Holtmannc0ba7ac2015-04-11 05:35:37 -0700216 /* Update power management enable flag with parameters of
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530217 * HCI sleep enable vendor specific HCI command.
218 */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100219 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530220 struct hci_command_hdr *hdr = (void *)skb->data;
221
222 if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP)
223 ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE];
224 }
225
226 BT_DBG("hu %p skb %p", hu, skb);
227
228 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100229 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530230
231 skb_queue_tail(&ath->txq, skb);
232 set_bit(HCI_UART_SENDING, &hu->tx_state);
233
234 schedule_work(&ath->ctxtsw);
235
236 return 0;
237}
238
239static struct sk_buff *ath_dequeue(struct hci_uart *hu)
240{
241 struct ath_struct *ath = hu->priv;
242
243 return skb_dequeue(&ath->txq);
244}
245
Marcel Holtmann4ee7ef12015-04-04 22:11:43 -0700246static const struct hci_uart_proto athp = {
Marcel Holtmann7c40fb82015-04-04 22:27:34 -0700247 .id = HCI_UART_ATH3K,
248 .name = "ATH3K",
Marcel Holtmannaee61f72015-10-20 21:30:45 +0200249 .manufacturer = 69,
Marcel Holtmann7c40fb82015-04-04 22:27:34 -0700250 .open = ath_open,
251 .close = ath_close,
Marcel Holtmannc0ba7ac2015-04-11 05:35:37 -0700252 .flush = ath_flush,
Marcel Holtmann4c876c02015-04-11 05:35:38 -0700253 .setup = ath_setup,
Marcel Holtmann7c40fb82015-04-04 22:27:34 -0700254 .recv = ath_recv,
255 .enqueue = ath_enqueue,
256 .dequeue = ath_dequeue,
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530257};
258
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300259int __init ath_init(void)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530260{
Marcel Holtmann01009ee2015-04-04 22:27:35 -0700261 return hci_uart_register_proto(&athp);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530262}
263
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300264int __exit ath_deinit(void)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530265{
266 return hci_uart_unregister_proto(&athp);
267}