1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Atheros Communication Bluetooth HCIATH3K UART protocol
5 * HCIATH3K (HCI Atheros AR300x Protocol) is a Atheros Communication's
6 * power management protocol extension to H4 to support AR300x Bluetooth Chip.
8 * Copyright (c) 2009-2010 Atheros Communications Inc.
11 * This file is based on hci_h4.c, which was written
12 * by Maxim Krasnyansky and Marcel Holtmann.
15 #include <linux/module.h>
16 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/tty.h>
21 #include <linux/errno.h>
22 #include <linux/ioctl.h>
23 #include <linux/skbuff.h>
25 #include <net/bluetooth/bluetooth.h>
26 #include <net/bluetooth/hci_core.h>
32 unsigned int cur_sleep
;
34 struct sk_buff
*rx_skb
;
35 struct sk_buff_head txq
;
36 struct work_struct ctxtsw
;
39 #define OP_WRITE_TAG 0x01
41 #define INDEX_BDADDR 0x01
43 struct ath_vendor_cmd
{
50 static int ath_wakeup_ar3k(struct tty_struct
*tty
)
52 int status
= tty
->driver
->ops
->tiocmget(tty
);
54 if (status
& TIOCM_CTS
)
58 tty
->driver
->ops
->tiocmget(tty
);
59 tty
->driver
->ops
->tiocmset(tty
, 0x00, TIOCM_RTS
);
62 /* Set RTS, wake up board */
63 tty
->driver
->ops
->tiocmget(tty
);
64 tty
->driver
->ops
->tiocmset(tty
, TIOCM_RTS
, 0x00);
67 status
= tty
->driver
->ops
->tiocmget(tty
);
71 static void ath_hci_uart_work(struct work_struct
*work
)
74 struct ath_struct
*ath
;
76 struct tty_struct
*tty
;
78 ath
= container_of(work
, struct ath_struct
, ctxtsw
);
83 /* verify and wake up controller */
85 status
= ath_wakeup_ar3k(tty
);
86 if (!(status
& TIOCM_CTS
))
90 /* Ready to send Data */
91 clear_bit(HCI_UART_SENDING
, &hu
->tx_state
);
92 hci_uart_tx_wakeup(hu
);
95 static int ath_open(struct hci_uart
*hu
)
97 struct ath_struct
*ath
;
101 if (!hci_uart_has_flow_control(hu
))
104 ath
= kzalloc(sizeof(*ath
), GFP_KERNEL
);
108 skb_queue_head_init(&ath
->txq
);
113 INIT_WORK(&ath
->ctxtsw
, ath_hci_uart_work
);
118 static int ath_close(struct hci_uart
*hu
)
120 struct ath_struct
*ath
= hu
->priv
;
124 skb_queue_purge(&ath
->txq
);
126 kfree_skb(ath
->rx_skb
);
128 cancel_work_sync(&ath
->ctxtsw
);
136 static int ath_flush(struct hci_uart
*hu
)
138 struct ath_struct
*ath
= hu
->priv
;
142 skb_queue_purge(&ath
->txq
);
147 static int ath_vendor_cmd(struct hci_dev
*hdev
, uint8_t opcode
, uint16_t index
,
148 const void *data
, size_t dlen
)
151 struct ath_vendor_cmd cmd
;
153 if (dlen
> sizeof(cmd
.data
))
157 cmd
.index
= cpu_to_le16(index
);
159 memcpy(cmd
.data
, data
, dlen
);
161 skb
= __hci_cmd_sync(hdev
, 0xfc0b, dlen
+ 4, &cmd
, HCI_INIT_TIMEOUT
);
169 static int ath_set_bdaddr(struct hci_dev
*hdev
, const bdaddr_t
*bdaddr
)
171 return ath_vendor_cmd(hdev
, OP_WRITE_TAG
, INDEX_BDADDR
, bdaddr
,
175 static int ath_setup(struct hci_uart
*hu
)
179 hu
->hdev
->set_bdaddr
= ath_set_bdaddr
;
184 static const struct h4_recv_pkt ath_recv_pkts
[] = {
185 { H4_RECV_ACL
, .recv
= hci_recv_frame
},
186 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
187 { H4_RECV_EVENT
, .recv
= hci_recv_frame
},
190 static int ath_recv(struct hci_uart
*hu
, const void *data
, int count
)
192 struct ath_struct
*ath
= hu
->priv
;
194 ath
->rx_skb
= h4_recv_buf(hu
->hdev
, ath
->rx_skb
, data
, count
,
195 ath_recv_pkts
, ARRAY_SIZE(ath_recv_pkts
));
196 if (IS_ERR(ath
->rx_skb
)) {
197 int err
= PTR_ERR(ath
->rx_skb
);
198 bt_dev_err(hu
->hdev
, "Frame reassembly failed (%d)", err
);
206 #define HCI_OP_ATH_SLEEP 0xFC04
208 static int ath_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
210 struct ath_struct
*ath
= hu
->priv
;
212 if (hci_skb_pkt_type(skb
) == HCI_SCODATA_PKT
) {
217 /* Update power management enable flag with parameters of
218 * HCI sleep enable vendor specific HCI command.
220 if (hci_skb_pkt_type(skb
) == HCI_COMMAND_PKT
) {
221 struct hci_command_hdr
*hdr
= (void *)skb
->data
;
223 if (__le16_to_cpu(hdr
->opcode
) == HCI_OP_ATH_SLEEP
)
224 ath
->cur_sleep
= skb
->data
[HCI_COMMAND_HDR_SIZE
];
227 BT_DBG("hu %p skb %p", hu
, skb
);
229 /* Prepend skb with frame type */
230 memcpy(skb_push(skb
, 1), &hci_skb_pkt_type(skb
), 1);
232 skb_queue_tail(&ath
->txq
, skb
);
233 set_bit(HCI_UART_SENDING
, &hu
->tx_state
);
235 schedule_work(&ath
->ctxtsw
);
240 static struct sk_buff
*ath_dequeue(struct hci_uart
*hu
)
242 struct ath_struct
*ath
= hu
->priv
;
244 return skb_dequeue(&ath
->txq
);
247 static const struct hci_uart_proto athp
= {
248 .id
= HCI_UART_ATH3K
,
256 .enqueue
= ath_enqueue
,
257 .dequeue
= ath_dequeue
,
260 int __init
ath_init(void)
262 return hci_uart_register_proto(&athp
);
265 int __exit
ath_deinit(void)
267 return hci_uart_unregister_proto(&athp
);