Blame view

drivers/bluetooth/hci_ath.c 5.27 KB
1a59d1b8e   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
2
3
4
5
6
7
8
9
10
11
12
  /*
   *  Atheros Communication Bluetooth HCIATH3K UART protocol
   *
   *  HCIATH3K (HCI Atheros AR300x Protocol) is a Atheros Communication's
   *  power management protocol extension to H4 to support AR300x Bluetooth Chip.
   *
   *  Copyright (c) 2009-2010 Atheros Communications Inc.
   *
   *  Acknowledgements:
   *  This file is based on hci_h4.c, which was written
   *  by Maxim Krasnyansky and Marcel Holtmann.
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
   */
  
  #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"
  
  struct ath_struct {
  	struct hci_uart *hu;
  	unsigned int cur_sleep;
d90aa6823   Marcel Holtmann   Bluetooth: hci_ua...
33
  	struct sk_buff *rx_skb;
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
34
35
36
  	struct sk_buff_head txq;
  	struct work_struct ctxtsw;
  };
13df5000d   Loic Poulain   Bluetooth: hci_at...
37
38
39
40
41
42
43
44
45
46
  #define OP_WRITE_TAG	0x01
  
  #define INDEX_BDADDR	0x01
  
  struct ath_vendor_cmd {
  	__u8 opcode;
  	__le16 index;
  	__u8 len;
  	__u8 data[251];
  } __packed;
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
47
48
  static int ath_wakeup_ar3k(struct tty_struct *tty)
  {
afaae0844   Alan Cox   hci_ath: Fix the ...
49
  	int status = tty->driver->ops->tiocmget(tty);
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
50
51
52
  
  	if (status & TIOCM_CTS)
  		return status;
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
53
  	/* Clear RTS first */
632f32e21   Peter Hurley   tty: Remove exter...
54
  	tty->driver->ops->tiocmget(tty);
afaae0844   Alan Cox   hci_ath: Fix the ...
55
  	tty->driver->ops->tiocmset(tty, 0x00, TIOCM_RTS);
688d6240e   Jia-Ju Bai   Bluetooth: hci_at...
56
  	msleep(20);
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
57
58
  
  	/* Set RTS, wake up board */
632f32e21   Peter Hurley   tty: Remove exter...
59
  	tty->driver->ops->tiocmget(tty);
afaae0844   Alan Cox   hci_ath: Fix the ...
60
  	tty->driver->ops->tiocmset(tty, TIOCM_RTS, 0x00);
688d6240e   Jia-Ju Bai   Bluetooth: hci_at...
61
  	msleep(20);
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
62

afaae0844   Alan Cox   hci_ath: Fix the ...
63
  	status = tty->driver->ops->tiocmget(tty);
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
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
  	return status;
  }
  
  static void ath_hci_uart_work(struct work_struct *work)
  {
  	int status;
  	struct ath_struct *ath;
  	struct hci_uart *hu;
  	struct tty_struct *tty;
  
  	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_ar3k(tty);
  		if (!(status & TIOCM_CTS))
  			return;
  	}
  
  	/* Ready to send Data */
  	clear_bit(HCI_UART_SENDING, &hu->tx_state);
  	hci_uart_tx_wakeup(hu);
  }
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
90
91
92
93
94
  static int ath_open(struct hci_uart *hu)
  {
  	struct ath_struct *ath;
  
  	BT_DBG("hu %p", hu);
b36a1552d   Vladis Dronov   Bluetooth: hci_ua...
95
96
  	if (!hci_uart_has_flow_control(hu))
  		return -EOPNOTSUPP;
f5fd5baee   David Herrmann   Bluetooth: hci-ua...
97
  	ath = kzalloc(sizeof(*ath), GFP_KERNEL);
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
98
99
100
101
102
103
104
105
106
107
108
109
  	if (!ath)
  		return -ENOMEM;
  
  	skb_queue_head_init(&ath->txq);
  
  	hu->priv = ath;
  	ath->hu = hu;
  
  	INIT_WORK(&ath->ctxtsw, ath_hci_uart_work);
  
  	return 0;
  }
c0ba7acd4   Marcel Holtmann   Bluetooth: hci_ua...
110
  static int ath_close(struct hci_uart *hu)
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
111
112
113
114
115
116
  {
  	struct ath_struct *ath = hu->priv;
  
  	BT_DBG("hu %p", hu);
  
  	skb_queue_purge(&ath->txq);
c0ba7acd4   Marcel Holtmann   Bluetooth: hci_ua...
117
118
119
120
121
122
  	kfree_skb(ath->rx_skb);
  
  	cancel_work_sync(&ath->ctxtsw);
  
  	hu->priv = NULL;
  	kfree(ath);
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
123
124
  	return 0;
  }
c0ba7acd4   Marcel Holtmann   Bluetooth: hci_ua...
125
  static int ath_flush(struct hci_uart *hu)
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
126
127
128
129
130
131
  {
  	struct ath_struct *ath = hu->priv;
  
  	BT_DBG("hu %p", hu);
  
  	skb_queue_purge(&ath->txq);
c0ba7acd4   Marcel Holtmann   Bluetooth: hci_ua...
132
133
  	return 0;
  }
d90aa6823   Marcel Holtmann   Bluetooth: hci_ua...
134

13df5000d   Loic Poulain   Bluetooth: hci_at...
135
136
  static int ath_vendor_cmd(struct hci_dev *hdev, uint8_t opcode, uint16_t index,
  			  const void *data, size_t dlen)
4c876c0ed   Marcel Holtmann   Bluetooth: hci_ua...
137
138
  {
  	struct sk_buff *skb;
13df5000d   Loic Poulain   Bluetooth: hci_at...
139
140
141
142
143
144
145
146
147
148
149
150
151
  	struct ath_vendor_cmd cmd;
  
  	if (dlen > sizeof(cmd.data))
  		return -EINVAL;
  
  	cmd.opcode = opcode;
  	cmd.index = cpu_to_le16(index);
  	cmd.len = dlen;
  	memcpy(cmd.data, data, dlen);
  
  	skb = __hci_cmd_sync(hdev, 0xfc0b, dlen + 4, &cmd, HCI_INIT_TIMEOUT);
  	if (IS_ERR(skb))
  		return PTR_ERR(skb);
4c876c0ed   Marcel Holtmann   Bluetooth: hci_ua...
152
153
154
155
  	kfree_skb(skb);
  
  	return 0;
  }
13df5000d   Loic Poulain   Bluetooth: hci_at...
156
157
158
159
160
  static int ath_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  {
  	return ath_vendor_cmd(hdev, OP_WRITE_TAG, INDEX_BDADDR, bdaddr,
  			      sizeof(*bdaddr));
  }
4c876c0ed   Marcel Holtmann   Bluetooth: hci_ua...
161
162
163
164
165
166
167
168
  static int ath_setup(struct hci_uart *hu)
  {
  	BT_DBG("hu %p", hu);
  
  	hu->hdev->set_bdaddr = ath_set_bdaddr;
  
  	return 0;
  }
c0ba7acd4   Marcel Holtmann   Bluetooth: hci_ua...
169
170
171
172
173
  static const struct h4_recv_pkt ath_recv_pkts[] = {
  	{ H4_RECV_ACL,   .recv = hci_recv_frame },
  	{ H4_RECV_SCO,   .recv = hci_recv_frame },
  	{ H4_RECV_EVENT, .recv = hci_recv_frame },
  };
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
174

c0ba7acd4   Marcel Holtmann   Bluetooth: hci_ua...
175
176
177
  static int ath_recv(struct hci_uart *hu, const void *data, int count)
  {
  	struct ath_struct *ath = hu->priv;
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
178

c0ba7acd4   Marcel Holtmann   Bluetooth: hci_ua...
179
180
181
182
  	ath->rx_skb = h4_recv_buf(hu->hdev, ath->rx_skb, data, count,
  				  ath_recv_pkts, ARRAY_SIZE(ath_recv_pkts));
  	if (IS_ERR(ath->rx_skb)) {
  		int err = PTR_ERR(ath->rx_skb);
2064ee332   Marcel Holtmann   Bluetooth: Use bt...
183
  		bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
371341676   Chan-yeol Park   Bluetooth: hci_ua...
184
  		ath->rx_skb = NULL;
c0ba7acd4   Marcel Holtmann   Bluetooth: hci_ua...
185
186
187
188
  		return err;
  	}
  
  	return count;
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
189
190
191
  }
  
  #define HCI_OP_ATH_SLEEP 0xFC04
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
192
193
194
  static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  {
  	struct ath_struct *ath = hu->priv;
618e8bc22   Marcel Holtmann   Bluetooth: Use ne...
195
  	if (hci_skb_pkt_type(skb) == HCI_SCODATA_PKT) {
4ebaa4edf   Dan Carpenter   Bluetooth: Fix kf...
196
  		kfree_skb(skb);
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
197
198
  		return 0;
  	}
c0ba7acd4   Marcel Holtmann   Bluetooth: hci_ua...
199
  	/* Update power management enable flag with parameters of
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
200
201
  	 * HCI sleep enable vendor specific HCI command.
  	 */
618e8bc22   Marcel Holtmann   Bluetooth: Use ne...
202
  	if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
203
204
205
206
207
208
209
210
211
  		struct hci_command_hdr *hdr = (void *)skb->data;
  
  		if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP)
  			ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE];
  	}
  
  	BT_DBG("hu %p skb %p", hu, skb);
  
  	/* Prepend skb with frame type */
618e8bc22   Marcel Holtmann   Bluetooth: Use ne...
212
  	memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  
  	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;
  
  	return skb_dequeue(&ath->txq);
  }
4ee7ef198   Marcel Holtmann   Bluetooth: hci_ua...
228
  static const struct hci_uart_proto athp = {
7c40fb8db   Marcel Holtmann   Bluetooth: hci_ua...
229
230
  	.id		= HCI_UART_ATH3K,
  	.name		= "ATH3K",
aee61f7aa   Marcel Holtmann   Bluetooth: hci_ua...
231
  	.manufacturer	= 69,
7c40fb8db   Marcel Holtmann   Bluetooth: hci_ua...
232
233
  	.open		= ath_open,
  	.close		= ath_close,
c0ba7acd4   Marcel Holtmann   Bluetooth: hci_ua...
234
  	.flush		= ath_flush,
4c876c0ed   Marcel Holtmann   Bluetooth: hci_ua...
235
  	.setup		= ath_setup,
7c40fb8db   Marcel Holtmann   Bluetooth: hci_ua...
236
237
238
  	.recv		= ath_recv,
  	.enqueue	= ath_enqueue,
  	.dequeue	= ath_dequeue,
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
239
  };
f2b94bb9e   Gustavo F. Padovan   Bluetooth: Add __...
240
  int __init ath_init(void)
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
241
  {
01009eec7   Marcel Holtmann   Bluetooth: hci_ua...
242
  	return hci_uart_register_proto(&athp);
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
243
  }
f2b94bb9e   Gustavo F. Padovan   Bluetooth: Add __...
244
  int __exit ath_deinit(void)
b3190df62   Suraj Sumangala   Bluetooth: Suppor...
245
246
247
  {
  	return hci_uart_unregister_proto(&athp);
  }