Linux 4.19.133
[linux/fpc-iii.git] / drivers / bluetooth / hci_ath.c
blob20235925344dd224327a12ededac2589906be334
1 /*
2 * Atheros Communication Bluetooth HCIATH3K UART protocol
4 * HCIATH3K (HCI Atheros AR300x Protocol) is a Atheros Communication's
5 * power management protocol extension to H4 to support AR300x Bluetooth Chip.
7 * Copyright (c) 2009-2010 Atheros Communications Inc.
9 * Acknowledgements:
10 * This file is based on hci_h4.c, which was written
11 * by Maxim Krasnyansky and Marcel Holtmann.
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.
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.
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
29 #include <linux/module.h>
30 #include <linux/kernel.h>
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>
39 #include <net/bluetooth/bluetooth.h>
40 #include <net/bluetooth/hci_core.h>
42 #include "hci_uart.h"
44 struct ath_struct {
45 struct hci_uart *hu;
46 unsigned int cur_sleep;
48 struct sk_buff *rx_skb;
49 struct sk_buff_head txq;
50 struct work_struct ctxtsw;
53 #define OP_WRITE_TAG 0x01
55 #define INDEX_BDADDR 0x01
57 struct ath_vendor_cmd {
58 __u8 opcode;
59 __le16 index;
60 __u8 len;
61 __u8 data[251];
62 } __packed;
64 static int ath_wakeup_ar3k(struct tty_struct *tty)
66 int status = tty->driver->ops->tiocmget(tty);
68 if (status & TIOCM_CTS)
69 return status;
71 /* Clear RTS first */
72 tty->driver->ops->tiocmget(tty);
73 tty->driver->ops->tiocmset(tty, 0x00, TIOCM_RTS);
74 msleep(20);
76 /* Set RTS, wake up board */
77 tty->driver->ops->tiocmget(tty);
78 tty->driver->ops->tiocmset(tty, TIOCM_RTS, 0x00);
79 msleep(20);
81 status = tty->driver->ops->tiocmget(tty);
82 return status;
85 static void ath_hci_uart_work(struct work_struct *work)
87 int status;
88 struct ath_struct *ath;
89 struct hci_uart *hu;
90 struct tty_struct *tty;
92 ath = container_of(work, struct ath_struct, ctxtsw);
94 hu = ath->hu;
95 tty = hu->tty;
97 /* verify and wake up controller */
98 if (ath->cur_sleep) {
99 status = ath_wakeup_ar3k(tty);
100 if (!(status & TIOCM_CTS))
101 return;
104 /* Ready to send Data */
105 clear_bit(HCI_UART_SENDING, &hu->tx_state);
106 hci_uart_tx_wakeup(hu);
109 static int ath_open(struct hci_uart *hu)
111 struct ath_struct *ath;
113 BT_DBG("hu %p", hu);
115 if (!hci_uart_has_flow_control(hu))
116 return -EOPNOTSUPP;
118 ath = kzalloc(sizeof(*ath), GFP_KERNEL);
119 if (!ath)
120 return -ENOMEM;
122 skb_queue_head_init(&ath->txq);
124 hu->priv = ath;
125 ath->hu = hu;
127 INIT_WORK(&ath->ctxtsw, ath_hci_uart_work);
129 return 0;
132 static int ath_close(struct hci_uart *hu)
134 struct ath_struct *ath = hu->priv;
136 BT_DBG("hu %p", hu);
138 skb_queue_purge(&ath->txq);
140 kfree_skb(ath->rx_skb);
142 cancel_work_sync(&ath->ctxtsw);
144 hu->priv = NULL;
145 kfree(ath);
147 return 0;
150 static int ath_flush(struct hci_uart *hu)
152 struct ath_struct *ath = hu->priv;
154 BT_DBG("hu %p", hu);
156 skb_queue_purge(&ath->txq);
158 return 0;
161 static int ath_vendor_cmd(struct hci_dev *hdev, uint8_t opcode, uint16_t index,
162 const void *data, size_t dlen)
164 struct sk_buff *skb;
165 struct ath_vendor_cmd cmd;
167 if (dlen > sizeof(cmd.data))
168 return -EINVAL;
170 cmd.opcode = opcode;
171 cmd.index = cpu_to_le16(index);
172 cmd.len = dlen;
173 memcpy(cmd.data, data, dlen);
175 skb = __hci_cmd_sync(hdev, 0xfc0b, dlen + 4, &cmd, HCI_INIT_TIMEOUT);
176 if (IS_ERR(skb))
177 return PTR_ERR(skb);
178 kfree_skb(skb);
180 return 0;
183 static int ath_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
185 return ath_vendor_cmd(hdev, OP_WRITE_TAG, INDEX_BDADDR, bdaddr,
186 sizeof(*bdaddr));
189 static int ath_setup(struct hci_uart *hu)
191 BT_DBG("hu %p", hu);
193 hu->hdev->set_bdaddr = ath_set_bdaddr;
195 return 0;
198 static const struct h4_recv_pkt ath_recv_pkts[] = {
199 { H4_RECV_ACL, .recv = hci_recv_frame },
200 { H4_RECV_SCO, .recv = hci_recv_frame },
201 { H4_RECV_EVENT, .recv = hci_recv_frame },
204 static int ath_recv(struct hci_uart *hu, const void *data, int count)
206 struct ath_struct *ath = hu->priv;
208 ath->rx_skb = h4_recv_buf(hu->hdev, ath->rx_skb, data, count,
209 ath_recv_pkts, ARRAY_SIZE(ath_recv_pkts));
210 if (IS_ERR(ath->rx_skb)) {
211 int err = PTR_ERR(ath->rx_skb);
212 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
213 ath->rx_skb = NULL;
214 return err;
217 return count;
220 #define HCI_OP_ATH_SLEEP 0xFC04
222 static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb)
224 struct ath_struct *ath = hu->priv;
226 if (hci_skb_pkt_type(skb) == HCI_SCODATA_PKT) {
227 kfree_skb(skb);
228 return 0;
231 /* Update power management enable flag with parameters of
232 * HCI sleep enable vendor specific HCI command.
234 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
235 struct hci_command_hdr *hdr = (void *)skb->data;
237 if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP)
238 ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE];
241 BT_DBG("hu %p skb %p", hu, skb);
243 /* Prepend skb with frame type */
244 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
246 skb_queue_tail(&ath->txq, skb);
247 set_bit(HCI_UART_SENDING, &hu->tx_state);
249 schedule_work(&ath->ctxtsw);
251 return 0;
254 static struct sk_buff *ath_dequeue(struct hci_uart *hu)
256 struct ath_struct *ath = hu->priv;
258 return skb_dequeue(&ath->txq);
261 static const struct hci_uart_proto athp = {
262 .id = HCI_UART_ATH3K,
263 .name = "ATH3K",
264 .manufacturer = 69,
265 .open = ath_open,
266 .close = ath_close,
267 .flush = ath_flush,
268 .setup = ath_setup,
269 .recv = ath_recv,
270 .enqueue = ath_enqueue,
271 .dequeue = ath_dequeue,
274 int __init ath_init(void)
276 return hci_uart_register_proto(&athp);
279 int __exit ath_deinit(void)
281 return hci_uart_unregister_proto(&athp);