Blame view

drivers/bluetooth/hci_serdev.c 8.8 KB
c942fddf8   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
82f5169bf   Rob Herring   Bluetooth: hci_ua...
2
3
4
5
6
7
8
9
10
11
  /*
   *  Bluetooth HCI serdev driver lib
   *
   *  Copyright (C) 2017  Linaro, Ltd., Rob Herring <robh@kernel.org>
   *
   *  Based on hci_ldisc.c:
   *
   *  Copyright (C) 2000-2001  Qualcomm Incorporated
   *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
   *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
82f5169bf   Rob Herring   Bluetooth: hci_ua...
12
13
14
15
16
17
18
19
20
21
22
   */
  
  #include <linux/kernel.h>
  #include <linux/types.h>
  #include <linux/serdev.h>
  #include <linux/skbuff.h>
  
  #include <net/bluetooth/bluetooth.h>
  #include <net/bluetooth/hci_core.h>
  
  #include "hci_uart.h"
82f5169bf   Rob Herring   Bluetooth: hci_ua...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
  {
  	struct hci_dev *hdev = hu->hdev;
  
  	/* Update HCI stat counters */
  	switch (pkt_type) {
  	case HCI_COMMAND_PKT:
  		hdev->stat.cmd_tx++;
  		break;
  
  	case HCI_ACLDATA_PKT:
  		hdev->stat.acl_tx++;
  		break;
  
  	case HCI_SCODATA_PKT:
  		hdev->stat.sco_tx++;
  		break;
  	}
  }
  
  static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
  {
  	struct sk_buff *skb = hu->tx_skb;
5a637751e   Balakrishna Godavarthi   Bluetooth: hci_se...
46
47
48
49
  	if (!skb) {
  		if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  			skb = hu->proto->dequeue(hu);
  	} else
82f5169bf   Rob Herring   Bluetooth: hci_ua...
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
  		hu->tx_skb = NULL;
  
  	return skb;
  }
  
  static void hci_uart_write_work(struct work_struct *work)
  {
  	struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
  	struct serdev_device *serdev = hu->serdev;
  	struct hci_dev *hdev = hu->hdev;
  	struct sk_buff *skb;
  
  	/* REVISIT:
  	 * should we cope with bad skbs or ->write() returning an error value?
  	 */
  	do {
  		clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
  
  		while ((skb = hci_uart_dequeue(hu))) {
  			int len;
  
  			len = serdev_device_write_buf(serdev,
  						      skb->data, skb->len);
  			hdev->stat.byte_tx += len;
  
  			skb_pull(skb, len);
  			if (skb->len) {
  				hu->tx_skb = skb;
  				break;
  			}
  
  			hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
  			kfree_skb(skb);
  		}
ea9ed991c   Jagdish Tirumala   Bluetooth: hci_se...
84
  	} while (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state));
82f5169bf   Rob Herring   Bluetooth: hci_ua...
85
86
87
88
89
  
  	clear_bit(HCI_UART_SENDING, &hu->tx_state);
  }
  
  /* ------- Interface to HCI layer ------ */
82f5169bf   Rob Herring   Bluetooth: hci_ua...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  /* Reset device */
  static int hci_uart_flush(struct hci_dev *hdev)
  {
  	struct hci_uart *hu  = hci_get_drvdata(hdev);
  
  	BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
  
  	if (hu->tx_skb) {
  		kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
  	}
  
  	/* Flush any pending characters in the driver and discipline. */
  	serdev_device_write_flush(hu->serdev);
  
  	if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  		hu->proto->flush(hu);
  
  	return 0;
  }
412fe29ff   Hans de Goede   Bluetooth: hci_ua...
109
110
111
  /* Initialize device */
  static int hci_uart_open(struct hci_dev *hdev)
  {
de8892df7   Venkata Lakshmi Narayana Gubba   Bluetooth: hci_se...
112
113
  	struct hci_uart *hu = hci_get_drvdata(hdev);
  	int err;
412fe29ff   Hans de Goede   Bluetooth: hci_ua...
114
  	BT_DBG("%s %p", hdev->name, hdev);
de8892df7   Venkata Lakshmi Narayana Gubba   Bluetooth: hci_se...
115
116
117
118
119
120
121
122
123
124
  	/* When Quirk HCI_QUIRK_NON_PERSISTENT_SETUP is set by
  	 * driver, BT SoC is completely turned OFF during
  	 * BT OFF. Upon next BT ON UART port should be opened.
  	 */
  	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
  		err = serdev_device_open(hu->serdev);
  		if (err)
  			return err;
  		set_bit(HCI_UART_PROTO_READY, &hu->flags);
  	}
412fe29ff   Hans de Goede   Bluetooth: hci_ua...
125
126
127
128
129
  	/* Undo clearing this from hci_uart_close() */
  	hdev->flush = hci_uart_flush;
  
  	return 0;
  }
82f5169bf   Rob Herring   Bluetooth: hci_ua...
130
131
132
  /* Close device */
  static int hci_uart_close(struct hci_dev *hdev)
  {
de8892df7   Venkata Lakshmi Narayana Gubba   Bluetooth: hci_se...
133
  	struct hci_uart *hu = hci_get_drvdata(hdev);
82f5169bf   Rob Herring   Bluetooth: hci_ua...
134
  	BT_DBG("hdev %p", hdev);
de8892df7   Venkata Lakshmi Narayana Gubba   Bluetooth: hci_se...
135
136
  	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
  		return 0;
82f5169bf   Rob Herring   Bluetooth: hci_ua...
137
138
  	hci_uart_flush(hdev);
  	hdev->flush = NULL;
de8892df7   Venkata Lakshmi Narayana Gubba   Bluetooth: hci_se...
139
140
141
142
143
144
145
146
  	/* When QUIRK HCI_QUIRK_NON_PERSISTENT_SETUP is set by driver,
  	 * BT SOC is completely powered OFF during BT OFF, holding port
  	 * open may drain the battery.
  	 */
  	if (test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks)) {
  		clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  		serdev_device_close(hu->serdev);
  	}
82f5169bf   Rob Herring   Bluetooth: hci_ua...
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
  	return 0;
  }
  
  /* Send frames from HCI layer */
  static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  {
  	struct hci_uart *hu = hci_get_drvdata(hdev);
  
  	BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
  	       skb->len);
  
  	hu->proto->enqueue(hu, skb);
  
  	hci_uart_tx_wakeup(hu);
  
  	return 0;
  }
  
  static int hci_uart_setup(struct hci_dev *hdev)
  {
  	struct hci_uart *hu = hci_get_drvdata(hdev);
  	struct hci_rp_read_local_version *ver;
  	struct sk_buff *skb;
  	unsigned int speed;
  	int err;
  
  	/* Init speed if any */
  	if (hu->init_speed)
  		speed = hu->init_speed;
  	else if (hu->proto->init_speed)
  		speed = hu->proto->init_speed;
  	else
  		speed = 0;
  
  	if (speed)
  		serdev_device_set_baudrate(hu->serdev, speed);
  
  	/* Operational speed if any */
  	if (hu->oper_speed)
  		speed = hu->oper_speed;
  	else if (hu->proto->oper_speed)
  		speed = hu->proto->oper_speed;
  	else
  		speed = 0;
  
  	if (hu->proto->set_baudrate && speed) {
  		err = hu->proto->set_baudrate(hu, speed);
  		if (err)
2064ee332   Marcel Holtmann   Bluetooth: Use bt...
195
  			bt_dev_err(hdev, "Failed to set baudrate");
82f5169bf   Rob Herring   Bluetooth: hci_ua...
196
197
198
199
200
201
202
203
204
205
206
207
208
  		else
  			serdev_device_set_baudrate(hu->serdev, speed);
  	}
  
  	if (hu->proto->setup)
  		return hu->proto->setup(hu);
  
  	if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
  		return 0;
  
  	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  			     HCI_INIT_TIMEOUT);
  	if (IS_ERR(skb)) {
2064ee332   Marcel Holtmann   Bluetooth: Use bt...
209
210
  		bt_dev_err(hdev, "Reading local version info failed (%ld)",
  			   PTR_ERR(skb));
82f5169bf   Rob Herring   Bluetooth: hci_ua...
211
212
  		return 0;
  	}
0c0c09ff0   Vaibhav Murkute   Bluetooth: hci_se...
213
  	if (skb->len != sizeof(*ver))
2064ee332   Marcel Holtmann   Bluetooth: Use bt...
214
  		bt_dev_err(hdev, "Event length mismatch for version info");
82f5169bf   Rob Herring   Bluetooth: hci_ua...
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
  
  	kfree_skb(skb);
  	return 0;
  }
  
  /** hci_uart_write_wakeup - transmit buffer wakeup
   * @serdev: serial device
   *
   * This function is called by the serdev framework when it accepts
   * more data being sent.
   */
  static void hci_uart_write_wakeup(struct serdev_device *serdev)
  {
  	struct hci_uart *hu = serdev_device_get_drvdata(serdev);
  
  	BT_DBG("");
  
  	if (!hu || serdev != hu->serdev) {
  		WARN_ON(1);
  		return;
  	}
  
  	if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
  		hci_uart_tx_wakeup(hu);
  }
  
  /** hci_uart_receive_buf - receive buffer wakeup
   * @serdev: serial device
   * @data:   pointer to received data
   * @count:  count of received data in bytes
   *
   * This function is called by the serdev framework when it received data
   * in the RX buffer.
   *
   * Return: number of processed bytes
   */
  static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
  				   size_t count)
  {
  	struct hci_uart *hu = serdev_device_get_drvdata(serdev);
  
  	if (!hu || serdev != hu->serdev) {
  		WARN_ON(1);
  		return 0;
  	}
  
  	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
  		return 0;
  
  	/* It does not need a lock here as it is already protected by a mutex in
  	 * tty caller
  	 */
  	hu->proto->recv(hu, data, count);
  
  	if (hu->hdev)
  		hu->hdev->stat.byte_rx += count;
  
  	return count;
  }
608c39f43   Rikard Falkeborn   Bluetooth: serdev...
274
  static const struct serdev_device_ops hci_serdev_client_ops = {
82f5169bf   Rob Herring   Bluetooth: hci_ua...
275
276
277
278
279
280
281
282
283
284
285
  	.receive_buf = hci_uart_receive_buf,
  	.write_wakeup = hci_uart_write_wakeup,
  };
  
  int hci_uart_register_device(struct hci_uart *hu,
  			     const struct hci_uart_proto *p)
  {
  	int err;
  	struct hci_dev *hdev;
  
  	BT_DBG("");
52b318e61   Sebastian Reichel   Bluetooth: hci_se...
286
  	serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
e9ca08074   Hans de Goede   Bluetooth: hci_se...
287
  	err = serdev_device_open(hu->serdev);
82f5169bf   Rob Herring   Bluetooth: hci_ua...
288
289
  	if (err)
  		return err;
e9ca08074   Hans de Goede   Bluetooth: hci_se...
290
291
292
  	err = p->open(hu);
  	if (err)
  		goto err_open;
82f5169bf   Rob Herring   Bluetooth: hci_ua...
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  	hu->proto = p;
  	set_bit(HCI_UART_PROTO_READY, &hu->flags);
  
  	/* Initialize and register HCI device */
  	hdev = hci_alloc_dev();
  	if (!hdev) {
  		BT_ERR("Can't allocate HCI device");
  		err = -ENOMEM;
  		goto err_alloc;
  	}
  
  	hu->hdev = hdev;
  
  	hdev->bus = HCI_UART;
  	hci_set_drvdata(hdev, hu);
fdee6d8fc   Hans de Goede   Bluetooth: hci_se...
308
  	INIT_WORK(&hu->init_ready, hci_uart_init_work);
82f5169bf   Rob Herring   Bluetooth: hci_ua...
309
  	INIT_WORK(&hu->write_work, hci_uart_write_work);
d73e17281   Lukas Wunner   Bluetooth: hci_se...
310
  	percpu_init_rwsem(&hu->proto_lock);
82f5169bf   Rob Herring   Bluetooth: hci_ua...
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  
  	/* Only when vendor specific setup callback is provided, consider
  	 * the manufacturer information valid. This avoids filling in the
  	 * value for Ericsson when nothing is specified.
  	 */
  	if (hu->proto->setup)
  		hdev->manufacturer = hu->proto->manufacturer;
  
  	hdev->open  = hci_uart_open;
  	hdev->close = hci_uart_close;
  	hdev->flush = hci_uart_flush;
  	hdev->send  = hci_uart_send_frame;
  	hdev->setup = hci_uart_setup;
  	SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
  
  	if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
  		set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  
  	if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
  		set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
82f5169bf   Rob Herring   Bluetooth: hci_ua...
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  	if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
  		hdev->dev_type = HCI_AMP;
  	else
  		hdev->dev_type = HCI_PRIMARY;
  
  	if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
  		return 0;
  
  	if (hci_register_dev(hdev) < 0) {
  		BT_ERR("Can't register HCI device");
  		err = -ENODEV;
  		goto err_register;
  	}
  
  	set_bit(HCI_UART_REGISTERED, &hu->flags);
  
  	return 0;
  
  err_register:
  	hci_free_dev(hdev);
  err_alloc:
  	clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  	p->close(hu);
e9ca08074   Hans de Goede   Bluetooth: hci_se...
354
355
  err_open:
  	serdev_device_close(hu->serdev);
82f5169bf   Rob Herring   Bluetooth: hci_ua...
356
357
  	return err;
  }
081f36a8c   Sebastian Reichel   Bluetooth: hci_se...
358
  EXPORT_SYMBOL_GPL(hci_uart_register_device);
c34dc3bfa   Ian Molton   Bluetooth: hci_se...
359
360
361
362
  
  void hci_uart_unregister_device(struct hci_uart *hu)
  {
  	struct hci_dev *hdev = hu->hdev;
3b799254c   Samuel Holland   Bluetooth: hci_ua...
363
  	cancel_work_sync(&hu->init_ready);
202798db9   Nicolas Boichat   Bluetooth: hci_se...
364
365
  	if (test_bit(HCI_UART_REGISTERED, &hu->flags))
  		hci_unregister_dev(hdev);
c34dc3bfa   Ian Molton   Bluetooth: hci_se...
366
367
368
369
370
  	hci_free_dev(hdev);
  
  	cancel_work_sync(&hu->write_work);
  
  	hu->proto->close(hu);
de8892df7   Venkata Lakshmi Narayana Gubba   Bluetooth: hci_se...
371
372
373
374
375
  
  	if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
  		clear_bit(HCI_UART_PROTO_READY, &hu->flags);
  		serdev_device_close(hu->serdev);
  	}
c34dc3bfa   Ian Molton   Bluetooth: hci_se...
376
377
  }
  EXPORT_SYMBOL_GPL(hci_uart_unregister_device);