xref: /linux/drivers/bluetooth/hci_ll.c (revision 35c2c39832e569449b9192fa1afbbc4c66227af7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Texas Instruments' Bluetooth HCILL UART protocol
4  *
5  *  HCILL (HCI Low Level) is a Texas Instruments' power management
6  *  protocol extension to H4.
7  *
8  *  Copyright (C) 2007 Texas Instruments, Inc.
9  *
10  *  Written by Ohad Ben-Cohen <ohad@bencohen.org>
11  *
12  *  Acknowledgements:
13  *  This file is based on hci_h4.c, which was written
14  *  by Maxim Krasnyansky and Marcel Holtmann.
15  */
16 
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 
20 #include <linux/init.h>
21 #include <linux/sched.h>
22 #include <linux/types.h>
23 #include <linux/fcntl.h>
24 #include <linux/firmware.h>
25 #include <linux/interrupt.h>
26 #include <linux/ptrace.h>
27 #include <linux/poll.h>
28 
29 #include <linux/slab.h>
30 #include <linux/errno.h>
31 #include <linux/string.h>
32 #include <linux/signal.h>
33 #include <linux/ioctl.h>
34 #include <linux/of.h>
35 #include <linux/serdev.h>
36 #include <linux/skbuff.h>
37 #include <linux/ti_wilink_st.h>
38 #include <linux/clk.h>
39 
40 #include <net/bluetooth/bluetooth.h>
41 #include <net/bluetooth/hci_core.h>
42 #include <linux/gpio/consumer.h>
43 #include <linux/nvmem-consumer.h>
44 
45 #include "hci_uart.h"
46 
47 /* Vendor-specific HCI commands */
48 #define HCI_VS_WRITE_BD_ADDR			0xfc06
49 #define HCI_VS_UPDATE_UART_HCI_BAUDRATE		0xff36
50 
51 /* HCILL commands */
52 #define HCILL_GO_TO_SLEEP_IND	0x30
53 #define HCILL_GO_TO_SLEEP_ACK	0x31
54 #define HCILL_WAKE_UP_IND	0x32
55 #define HCILL_WAKE_UP_ACK	0x33
56 
57 /* HCILL states */
58 enum hcill_states_e {
59 	HCILL_ASLEEP,
60 	HCILL_ASLEEP_TO_AWAKE,
61 	HCILL_AWAKE,
62 	HCILL_AWAKE_TO_ASLEEP
63 };
64 
65 struct ll_device {
66 	struct hci_uart hu;
67 	struct serdev_device *serdev;
68 	struct gpio_desc *enable_gpio;
69 	struct clk *ext_clk;
70 	bdaddr_t bdaddr;
71 	bool broken_enhanced_setup;
72 };
73 
74 struct ll_struct {
75 	struct sk_buff *rx_skb;
76 	struct sk_buff_head txq;
77 	spinlock_t hcill_lock;		/* HCILL state lock	*/
78 	unsigned long hcill_state;	/* HCILL power state	*/
79 	struct sk_buff_head tx_wait_q;	/* HCILL wait queue	*/
80 };
81 
82 /*
83  * Builds and sends an HCILL command packet.
84  * These are very simple packets with only 1 cmd byte
85  */
86 static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
87 {
88 	int err = 0;
89 	struct sk_buff *skb = NULL;
90 	struct ll_struct *ll = hu->priv;
91 
92 	BT_DBG("hu %p cmd 0x%x", hu, cmd);
93 
94 	/* allocate packet */
95 	skb = bt_skb_alloc(1, GFP_ATOMIC);
96 	if (!skb) {
97 		BT_ERR("cannot allocate memory for HCILL packet");
98 		err = -ENOMEM;
99 		goto out;
100 	}
101 
102 	/* prepare packet */
103 	skb_put_u8(skb, cmd);
104 
105 	/* send packet */
106 	skb_queue_tail(&ll->txq, skb);
107 out:
108 	return err;
109 }
110 
111 /* Initialize protocol */
112 static int ll_open(struct hci_uart *hu)
113 {
114 	struct ll_struct *ll;
115 
116 	BT_DBG("hu %p", hu);
117 
118 	ll = kzalloc_obj(*ll);
119 	if (!ll)
120 		return -ENOMEM;
121 
122 	skb_queue_head_init(&ll->txq);
123 	skb_queue_head_init(&ll->tx_wait_q);
124 	spin_lock_init(&ll->hcill_lock);
125 
126 	ll->hcill_state = HCILL_AWAKE;
127 
128 	hu->priv = ll;
129 
130 	if (hu->serdev) {
131 		struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
132 
133 		if (!IS_ERR(lldev->ext_clk))
134 			clk_prepare_enable(lldev->ext_clk);
135 	}
136 
137 	return 0;
138 }
139 
140 /* Flush protocol data */
141 static int ll_flush(struct hci_uart *hu)
142 {
143 	struct ll_struct *ll = hu->priv;
144 
145 	BT_DBG("hu %p", hu);
146 
147 	skb_queue_purge(&ll->tx_wait_q);
148 	skb_queue_purge(&ll->txq);
149 
150 	return 0;
151 }
152 
153 /* Close protocol */
154 static int ll_close(struct hci_uart *hu)
155 {
156 	struct ll_struct *ll = hu->priv;
157 
158 	BT_DBG("hu %p", hu);
159 
160 	skb_queue_purge(&ll->tx_wait_q);
161 	skb_queue_purge(&ll->txq);
162 
163 	kfree_skb(ll->rx_skb);
164 
165 	if (hu->serdev) {
166 		struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
167 
168 		gpiod_set_value_cansleep(lldev->enable_gpio, 0);
169 
170 		clk_disable_unprepare(lldev->ext_clk);
171 	}
172 
173 	hu->priv = NULL;
174 
175 	kfree(ll);
176 
177 	return 0;
178 }
179 
180 /*
181  * internal function, which does common work of the device wake up process:
182  * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
183  * 2. changes internal state to HCILL_AWAKE.
184  * Note: assumes that hcill_lock spinlock is taken,
185  * shouldn't be called otherwise!
186  */
187 static void __ll_do_awake(struct ll_struct *ll)
188 {
189 	struct sk_buff *skb = NULL;
190 
191 	while ((skb = skb_dequeue(&ll->tx_wait_q)))
192 		skb_queue_tail(&ll->txq, skb);
193 
194 	ll->hcill_state = HCILL_AWAKE;
195 }
196 
197 /*
198  * Called upon a wake-up-indication from the device
199  */
200 static void ll_device_want_to_wakeup(struct hci_uart *hu)
201 {
202 	unsigned long flags;
203 	struct ll_struct *ll = hu->priv;
204 
205 	BT_DBG("hu %p", hu);
206 
207 	/* lock hcill state */
208 	spin_lock_irqsave(&ll->hcill_lock, flags);
209 
210 	switch (ll->hcill_state) {
211 	case HCILL_ASLEEP_TO_AWAKE:
212 		/*
213 		 * This state means that both the host and the BRF chip
214 		 * have simultaneously sent a wake-up-indication packet.
215 		 * Traditionally, in this case, receiving a wake-up-indication
216 		 * was enough and an additional wake-up-ack wasn't needed.
217 		 * This has changed with the BRF6350, which does require an
218 		 * explicit wake-up-ack. Other BRF versions, which do not
219 		 * require an explicit ack here, do accept it, thus it is
220 		 * perfectly safe to always send one.
221 		 */
222 		BT_DBG("dual wake-up-indication");
223 		fallthrough;
224 	case HCILL_ASLEEP:
225 		/* acknowledge device wake up */
226 		if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
227 			BT_ERR("cannot acknowledge device wake up");
228 			goto out;
229 		}
230 		break;
231 	default:
232 		/* any other state is illegal */
233 		BT_ERR("received HCILL_WAKE_UP_IND in state %ld",
234 		       ll->hcill_state);
235 		break;
236 	}
237 
238 	/* send pending packets and change state to HCILL_AWAKE */
239 	__ll_do_awake(ll);
240 
241 out:
242 	spin_unlock_irqrestore(&ll->hcill_lock, flags);
243 
244 	/* actually send the packets */
245 	hci_uart_tx_wakeup(hu);
246 }
247 
248 /*
249  * Called upon a sleep-indication from the device
250  */
251 static void ll_device_want_to_sleep(struct hci_uart *hu)
252 {
253 	unsigned long flags;
254 	struct ll_struct *ll = hu->priv;
255 
256 	BT_DBG("hu %p", hu);
257 
258 	/* lock hcill state */
259 	spin_lock_irqsave(&ll->hcill_lock, flags);
260 
261 	/* sanity check */
262 	if (ll->hcill_state != HCILL_AWAKE)
263 		BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld",
264 		       ll->hcill_state);
265 
266 	/* acknowledge device sleep */
267 	if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
268 		BT_ERR("cannot acknowledge device sleep");
269 		goto out;
270 	}
271 
272 	/* update state */
273 	ll->hcill_state = HCILL_ASLEEP;
274 
275 out:
276 	spin_unlock_irqrestore(&ll->hcill_lock, flags);
277 
278 	/* actually send the sleep ack packet */
279 	hci_uart_tx_wakeup(hu);
280 }
281 
282 /*
283  * Called upon wake-up-acknowledgement from the device
284  */
285 static void ll_device_woke_up(struct hci_uart *hu)
286 {
287 	unsigned long flags;
288 	struct ll_struct *ll = hu->priv;
289 
290 	BT_DBG("hu %p", hu);
291 
292 	/* lock hcill state */
293 	spin_lock_irqsave(&ll->hcill_lock, flags);
294 
295 	/* sanity check */
296 	if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
297 		BT_ERR("received HCILL_WAKE_UP_ACK in state %ld",
298 		       ll->hcill_state);
299 
300 	/* send pending packets and change state to HCILL_AWAKE */
301 	__ll_do_awake(ll);
302 
303 	spin_unlock_irqrestore(&ll->hcill_lock, flags);
304 
305 	/* actually send the packets */
306 	hci_uart_tx_wakeup(hu);
307 }
308 
309 /* Enqueue frame for transmission (padding, crc, etc) */
310 /* may be called from two simultaneous tasklets */
311 static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
312 {
313 	unsigned long flags = 0;
314 	struct ll_struct *ll = hu->priv;
315 
316 	BT_DBG("hu %p skb %p", hu, skb);
317 
318 	/* Prepend skb with frame type */
319 	memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
320 
321 	/* lock hcill state */
322 	spin_lock_irqsave(&ll->hcill_lock, flags);
323 
324 	/* act according to current state */
325 	switch (ll->hcill_state) {
326 	case HCILL_AWAKE:
327 		BT_DBG("device awake, sending normally");
328 		skb_queue_tail(&ll->txq, skb);
329 		break;
330 	case HCILL_ASLEEP:
331 		BT_DBG("device asleep, waking up and queueing packet");
332 		/* save packet for later */
333 		skb_queue_tail(&ll->tx_wait_q, skb);
334 		/* awake device */
335 		if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
336 			BT_ERR("cannot wake up device");
337 			break;
338 		}
339 		ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
340 		break;
341 	case HCILL_ASLEEP_TO_AWAKE:
342 		BT_DBG("device waking up, queueing packet");
343 		/* transient state; just keep packet for later */
344 		skb_queue_tail(&ll->tx_wait_q, skb);
345 		break;
346 	default:
347 		BT_ERR("illegal hcill state: %ld (losing packet)",
348 		       ll->hcill_state);
349 		dev_kfree_skb_irq(skb);
350 		break;
351 	}
352 
353 	spin_unlock_irqrestore(&ll->hcill_lock, flags);
354 
355 	return 0;
356 }
357 
358 static int ll_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
359 {
360 	struct hci_uart *hu = hci_get_drvdata(hdev);
361 	struct ll_struct *ll = hu->priv;
362 
363 	switch (hci_skb_pkt_type(skb)) {
364 	case HCILL_GO_TO_SLEEP_IND:
365 		BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
366 		ll_device_want_to_sleep(hu);
367 		break;
368 	case HCILL_GO_TO_SLEEP_ACK:
369 		/* shouldn't happen */
370 		bt_dev_err(hdev, "received HCILL_GO_TO_SLEEP_ACK in state %ld",
371 			   ll->hcill_state);
372 		break;
373 	case HCILL_WAKE_UP_IND:
374 		BT_DBG("HCILL_WAKE_UP_IND packet");
375 		ll_device_want_to_wakeup(hu);
376 		break;
377 	case HCILL_WAKE_UP_ACK:
378 		BT_DBG("HCILL_WAKE_UP_ACK packet");
379 		ll_device_woke_up(hu);
380 		break;
381 	}
382 
383 	kfree_skb(skb);
384 	return 0;
385 }
386 
387 #define LL_RECV_SLEEP_IND \
388 	.type = HCILL_GO_TO_SLEEP_IND, \
389 	.hlen = 0, \
390 	.loff = 0, \
391 	.lsize = 0, \
392 	.maxlen = 0
393 
394 #define LL_RECV_SLEEP_ACK \
395 	.type = HCILL_GO_TO_SLEEP_ACK, \
396 	.hlen = 0, \
397 	.loff = 0, \
398 	.lsize = 0, \
399 	.maxlen = 0
400 
401 #define LL_RECV_WAKE_IND \
402 	.type = HCILL_WAKE_UP_IND, \
403 	.hlen = 0, \
404 	.loff = 0, \
405 	.lsize = 0, \
406 	.maxlen = 0
407 
408 #define LL_RECV_WAKE_ACK \
409 	.type = HCILL_WAKE_UP_ACK, \
410 	.hlen = 0, \
411 	.loff = 0, \
412 	.lsize = 0, \
413 	.maxlen = 0
414 
415 static const struct h4_recv_pkt ll_recv_pkts[] = {
416 	{ H4_RECV_ACL,       .recv = hci_recv_frame },
417 	{ H4_RECV_SCO,       .recv = hci_recv_frame },
418 	{ H4_RECV_EVENT,     .recv = hci_recv_frame },
419 	{ LL_RECV_SLEEP_IND, .recv = ll_recv_frame  },
420 	{ LL_RECV_SLEEP_ACK, .recv = ll_recv_frame  },
421 	{ LL_RECV_WAKE_IND,  .recv = ll_recv_frame  },
422 	{ LL_RECV_WAKE_ACK,  .recv = ll_recv_frame  },
423 };
424 
425 /* Recv data */
426 static int ll_recv(struct hci_uart *hu, const void *data, int count)
427 {
428 	struct ll_struct *ll = hu->priv;
429 
430 	if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
431 		return -EUNATCH;
432 
433 	ll->rx_skb = h4_recv_buf(hu, ll->rx_skb, data, count,
434 				 ll_recv_pkts, ARRAY_SIZE(ll_recv_pkts));
435 	if (IS_ERR(ll->rx_skb)) {
436 		int err = PTR_ERR(ll->rx_skb);
437 		bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
438 		ll->rx_skb = NULL;
439 		return err;
440 	}
441 
442 	return count;
443 }
444 
445 static struct sk_buff *ll_dequeue(struct hci_uart *hu)
446 {
447 	struct ll_struct *ll = hu->priv;
448 
449 	return skb_dequeue(&ll->txq);
450 }
451 
452 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
453 static int read_local_version(struct hci_dev *hdev)
454 {
455 	int err = 0;
456 	unsigned short version = 0;
457 	struct sk_buff *skb;
458 	struct hci_rp_read_local_version *ver;
459 
460 	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
461 			     HCI_INIT_TIMEOUT);
462 	if (IS_ERR(skb)) {
463 		bt_dev_err(hdev, "Reading TI version information failed (%ld)",
464 			   PTR_ERR(skb));
465 		return PTR_ERR(skb);
466 	}
467 	if (skb->len != sizeof(*ver)) {
468 		err = -EILSEQ;
469 		goto out;
470 	}
471 
472 	ver = (struct hci_rp_read_local_version *)skb->data;
473 	if (le16_to_cpu(ver->manufacturer) != 13) {
474 		err = -ENODEV;
475 		goto out;
476 	}
477 
478 	version = le16_to_cpu(ver->lmp_subver);
479 
480 out:
481 	if (err)
482 		bt_dev_err(hdev, "Failed to read TI version info: %d", err);
483 	kfree_skb(skb);
484 	return err ? err : version;
485 }
486 
487 static int send_command_from_firmware(struct ll_device *lldev,
488 				      struct hci_command *cmd)
489 {
490 	struct sk_buff *skb;
491 
492 	if (cmd->opcode == HCI_VS_UPDATE_UART_HCI_BAUDRATE) {
493 		/* ignore remote change
494 		 * baud rate HCI VS command
495 		 */
496 		bt_dev_warn(lldev->hu.hdev,
497 			    "change remote baud rate command in firmware");
498 		return 0;
499 	}
500 	if (cmd->prefix != 1)
501 		bt_dev_dbg(lldev->hu.hdev, "command type %d", cmd->prefix);
502 
503 	skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen,
504 			     &cmd->speed, HCI_INIT_TIMEOUT);
505 	if (IS_ERR(skb)) {
506 		bt_dev_err(lldev->hu.hdev, "send command failed");
507 		return PTR_ERR(skb);
508 	}
509 	kfree_skb(skb);
510 	return 0;
511 }
512 
513 /*
514  * download_firmware -
515  *	internal function which parses through the .bts firmware
516  *	script file intreprets SEND, DELAY actions only as of now
517  */
518 static int download_firmware(struct ll_device *lldev)
519 {
520 	unsigned short chip, min_ver, maj_ver;
521 	int version, err, len;
522 	unsigned char *ptr, *action_ptr;
523 	unsigned char bts_scr_name[40];	/* 40 char long bts scr name? */
524 	const struct firmware *fw;
525 	struct hci_command *cmd;
526 
527 	version = read_local_version(lldev->hu.hdev);
528 	if (version < 0)
529 		return version;
530 
531 	chip = (version & 0x7C00) >> 10;
532 	min_ver = (version & 0x007F);
533 	maj_ver = (version & 0x0380) >> 7;
534 	if (version & 0x8000)
535 		maj_ver |= 0x0008;
536 
537 	snprintf(bts_scr_name, sizeof(bts_scr_name),
538 		 "ti-connectivity/TIInit_%d.%d.%d.bts",
539 		 chip, maj_ver, min_ver);
540 
541 	err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev);
542 	if (err || !fw->data || !fw->size) {
543 		bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s",
544 			   err, bts_scr_name);
545 		if (!err)
546 			release_firmware(fw);
547 		return -EINVAL;
548 	}
549 	ptr = (void *)fw->data;
550 	len = fw->size;
551 	/* bts_header to remove out magic number and
552 	 * version
553 	 */
554 	ptr += sizeof(struct bts_header);
555 	len -= sizeof(struct bts_header);
556 
557 	while (len > 0 && ptr) {
558 		bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ",
559 			   ((struct bts_action *)ptr)->size,
560 			   ((struct bts_action *)ptr)->type);
561 
562 		action_ptr = &(((struct bts_action *)ptr)->data[0]);
563 
564 		switch (((struct bts_action *)ptr)->type) {
565 		case ACTION_SEND_COMMAND:	/* action send */
566 			bt_dev_dbg(lldev->hu.hdev, "S");
567 			cmd = (struct hci_command *)action_ptr;
568 			err = send_command_from_firmware(lldev, cmd);
569 			if (err)
570 				goto out_rel_fw;
571 			break;
572 		case ACTION_WAIT_EVENT:  /* wait */
573 			/* no need to wait as command was synchronous */
574 			bt_dev_dbg(lldev->hu.hdev, "W");
575 			break;
576 		case ACTION_DELAY:	/* sleep */
577 			bt_dev_info(lldev->hu.hdev, "sleep command in scr");
578 			msleep(((struct bts_action_delay *)action_ptr)->msec);
579 			break;
580 		}
581 		len -= (sizeof(struct bts_action) +
582 			((struct bts_action *)ptr)->size);
583 		ptr += sizeof(struct bts_action) +
584 			((struct bts_action *)ptr)->size;
585 	}
586 
587 out_rel_fw:
588 	/* fw download complete */
589 	release_firmware(fw);
590 	return err;
591 }
592 
593 static int ll_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
594 {
595 	bdaddr_t bdaddr_swapped;
596 	struct sk_buff *skb;
597 
598 	/* HCI_VS_WRITE_BD_ADDR (at least on a CC2560A chip) expects the BD
599 	 * address to be MSB first, but bdaddr_t has the convention of being
600 	 * LSB first.
601 	 */
602 	baswap(&bdaddr_swapped, bdaddr);
603 	skb = __hci_cmd_sync(hdev, HCI_VS_WRITE_BD_ADDR, sizeof(bdaddr_t),
604 			     &bdaddr_swapped, HCI_INIT_TIMEOUT);
605 	if (!IS_ERR(skb))
606 		kfree_skb(skb);
607 
608 	return PTR_ERR_OR_ZERO(skb);
609 }
610 
611 static int ll_setup(struct hci_uart *hu)
612 {
613 	int err, retry = 3;
614 	struct ll_device *lldev;
615 	struct serdev_device *serdev = hu->serdev;
616 	u32 speed;
617 
618 	if (!serdev)
619 		return 0;
620 
621 	lldev = serdev_device_get_drvdata(serdev);
622 
623 	hu->hdev->set_bdaddr = ll_set_bdaddr;
624 
625 	serdev_device_set_flow_control(serdev, true);
626 
627 	do {
628 		/* Reset the Bluetooth device */
629 		gpiod_set_value_cansleep(lldev->enable_gpio, 0);
630 		msleep(5);
631 		gpiod_set_value_cansleep(lldev->enable_gpio, 1);
632 		mdelay(100);
633 		err = serdev_device_wait_for_cts(serdev, true, 200);
634 		if (err) {
635 			bt_dev_err(hu->hdev, "Failed to get CTS");
636 			return err;
637 		}
638 
639 		err = download_firmware(lldev);
640 		if (!err)
641 			break;
642 
643 		/* Toggle BT_EN and retry */
644 		bt_dev_err(hu->hdev, "download firmware failed, retrying...");
645 	} while (retry--);
646 
647 	if (err)
648 		return err;
649 
650 	/* Set BD address if one was specified at probe */
651 	if (!bacmp(&lldev->bdaddr, BDADDR_NONE)) {
652 		/* This means that there was an error getting the BD address
653 		 * during probe, so mark the device as having a bad address.
654 		 */
655 		hci_set_quirk(hu->hdev, HCI_QUIRK_INVALID_BDADDR);
656 	} else if (bacmp(&lldev->bdaddr, BDADDR_ANY)) {
657 		err = ll_set_bdaddr(hu->hdev, &lldev->bdaddr);
658 		if (err)
659 			hci_set_quirk(hu->hdev, HCI_QUIRK_INVALID_BDADDR);
660 	}
661 
662 	if (lldev->broken_enhanced_setup)
663 		hci_set_quirk(hu->hdev,
664 			      HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN);
665 
666 	/* Operational speed if any */
667 	if (hu->oper_speed)
668 		speed = hu->oper_speed;
669 	else if (hu->proto->oper_speed)
670 		speed = hu->proto->oper_speed;
671 	else
672 		speed = 0;
673 
674 	if (speed) {
675 		__le32 speed_le = cpu_to_le32(speed);
676 		struct sk_buff *skb;
677 
678 		skb = __hci_cmd_sync(hu->hdev, HCI_VS_UPDATE_UART_HCI_BAUDRATE,
679 				     sizeof(speed_le), &speed_le,
680 				     HCI_INIT_TIMEOUT);
681 		if (!IS_ERR(skb)) {
682 			kfree_skb(skb);
683 			serdev_device_set_baudrate(serdev, speed);
684 		}
685 	}
686 
687 	return 0;
688 }
689 
690 static const struct hci_uart_proto llp;
691 
692 static int hci_ti_probe(struct serdev_device *serdev)
693 {
694 	struct hci_uart *hu;
695 	struct ll_device *lldev;
696 	struct nvmem_cell *bdaddr_cell;
697 	u32 max_speed = 3000000;
698 
699 	lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL);
700 	if (!lldev)
701 		return -ENOMEM;
702 	hu = &lldev->hu;
703 
704 	serdev_device_set_drvdata(serdev, lldev);
705 	lldev->serdev = hu->serdev = serdev;
706 
707 	lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev,
708 						     "enable",
709 						     GPIOD_OUT_LOW);
710 	if (IS_ERR(lldev->enable_gpio))
711 		return PTR_ERR(lldev->enable_gpio);
712 
713 	lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock");
714 	if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT)
715 		return PTR_ERR(lldev->ext_clk);
716 
717 	of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
718 	hci_uart_set_speeds(hu, 115200, max_speed);
719 
720 	if (of_device_is_compatible(serdev->dev.of_node, "ti,wl1831-st") ||
721 	    of_device_is_compatible(serdev->dev.of_node, "ti,wl1835-st") ||
722 	    of_device_is_compatible(serdev->dev.of_node, "ti,wl1837-st"))
723 		lldev->broken_enhanced_setup = true;
724 
725 	/* optional BD address from nvram */
726 	bdaddr_cell = nvmem_cell_get(&serdev->dev, "bd-address");
727 	if (IS_ERR(bdaddr_cell)) {
728 		int err = PTR_ERR(bdaddr_cell);
729 
730 		if (err == -EPROBE_DEFER)
731 			return err;
732 
733 		/* ENOENT means there is no matching nvmem cell and ENOSYS
734 		 * means that nvmem is not enabled in the kernel configuration.
735 		 */
736 		if (err != -ENOENT && err != -ENOSYS) {
737 			/* If there was some other error, give userspace a
738 			 * chance to fix the problem instead of failing to load
739 			 * the driver. Using BDADDR_NONE as a flag that is
740 			 * tested later in the setup function.
741 			 */
742 			dev_warn(&serdev->dev,
743 				 "Failed to get \"bd-address\" nvmem cell (%d)\n",
744 				 err);
745 			bacpy(&lldev->bdaddr, BDADDR_NONE);
746 		}
747 	} else {
748 		bdaddr_t *bdaddr;
749 		size_t len;
750 
751 		bdaddr = nvmem_cell_read(bdaddr_cell, &len);
752 		nvmem_cell_put(bdaddr_cell);
753 		if (IS_ERR(bdaddr)) {
754 			dev_err(&serdev->dev, "Failed to read nvmem bd-address\n");
755 			return PTR_ERR(bdaddr);
756 		}
757 		if (len != sizeof(bdaddr_t)) {
758 			dev_err(&serdev->dev, "Invalid nvmem bd-address length\n");
759 			kfree(bdaddr);
760 			return -EINVAL;
761 		}
762 
763 		/* As per the device tree bindings, the value from nvmem is
764 		 * expected to be MSB first, but in the kernel it is expected
765 		 * that bdaddr_t is LSB first.
766 		 */
767 		baswap(&lldev->bdaddr, bdaddr);
768 		kfree(bdaddr);
769 	}
770 
771 	return hci_uart_register_device(hu, &llp);
772 }
773 
774 static void hci_ti_remove(struct serdev_device *serdev)
775 {
776 	struct ll_device *lldev = serdev_device_get_drvdata(serdev);
777 
778 	hci_uart_unregister_device(&lldev->hu);
779 }
780 
781 static const struct of_device_id hci_ti_of_match[] = {
782 	{ .compatible = "ti,cc2560" },
783 	{ .compatible = "ti,wl1271-st" },
784 	{ .compatible = "ti,wl1273-st" },
785 	{ .compatible = "ti,wl1281-st" },
786 	{ .compatible = "ti,wl1283-st" },
787 	{ .compatible = "ti,wl1285-st" },
788 	{ .compatible = "ti,wl1801-st" },
789 	{ .compatible = "ti,wl1805-st" },
790 	{ .compatible = "ti,wl1807-st" },
791 	{ .compatible = "ti,wl1831-st" },
792 	{ .compatible = "ti,wl1835-st" },
793 	{ .compatible = "ti,wl1837-st" },
794 	{},
795 };
796 MODULE_DEVICE_TABLE(of, hci_ti_of_match);
797 
798 static struct serdev_device_driver hci_ti_drv = {
799 	.driver		= {
800 		.name	= "hci-ti",
801 		.of_match_table = hci_ti_of_match,
802 	},
803 	.probe	= hci_ti_probe,
804 	.remove	= hci_ti_remove,
805 };
806 #else
807 #define ll_setup NULL
808 #endif
809 
810 static const struct hci_uart_proto llp = {
811 	.id		= HCI_UART_LL,
812 	.name		= "LL",
813 	.setup		= ll_setup,
814 	.open		= ll_open,
815 	.close		= ll_close,
816 	.recv		= ll_recv,
817 	.enqueue	= ll_enqueue,
818 	.dequeue	= ll_dequeue,
819 	.flush		= ll_flush,
820 };
821 
822 int __init ll_init(void)
823 {
824 	serdev_device_driver_register(&hci_ti_drv);
825 
826 	return hci_uart_register_proto(&llp);
827 }
828 
829 int __exit ll_deinit(void)
830 {
831 	serdev_device_driver_unregister(&hci_ti_drv);
832 
833 	return hci_uart_unregister_proto(&llp);
834 }
835