1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Bluetooth HCI serdev driver lib 4 * 5 * Copyright (C) 2017 Linaro, Ltd., Rob Herring <robh@kernel.org> 6 * 7 * Based on hci_ldisc.c: 8 * 9 * Copyright (C) 2000-2001 Qualcomm Incorporated 10 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com> 11 * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org> 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/types.h> 16 #include <linux/serdev.h> 17 #include <linux/skbuff.h> 18 19 #include <net/bluetooth/bluetooth.h> 20 #include <net/bluetooth/hci_core.h> 21 22 #include "hci_uart.h" 23 24 static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type) 25 { 26 struct hci_dev *hdev = hu->hdev; 27 28 /* Update HCI stat counters */ 29 switch (pkt_type) { 30 case HCI_COMMAND_PKT: 31 hdev->stat.cmd_tx++; 32 break; 33 34 case HCI_ACLDATA_PKT: 35 hdev->stat.acl_tx++; 36 break; 37 38 case HCI_SCODATA_PKT: 39 hdev->stat.sco_tx++; 40 break; 41 } 42 } 43 44 static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu) 45 { 46 struct sk_buff *skb = hu->tx_skb; 47 48 if (!skb) { 49 if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) 50 skb = hu->proto->dequeue(hu); 51 } else 52 hu->tx_skb = NULL; 53 54 return skb; 55 } 56 57 static void hci_uart_write_work(struct work_struct *work) 58 { 59 struct hci_uart *hu = container_of(work, struct hci_uart, write_work); 60 struct serdev_device *serdev = hu->serdev; 61 struct hci_dev *hdev = hu->hdev; 62 struct sk_buff *skb; 63 64 /* REVISIT: 65 * should we cope with bad skbs or ->write() returning an error value? 66 */ 67 do { 68 clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state); 69 70 while ((skb = hci_uart_dequeue(hu))) { 71 int len; 72 73 len = serdev_device_write_buf(serdev, 74 skb->data, skb->len); 75 hdev->stat.byte_tx += len; 76 77 skb_pull(skb, len); 78 if (skb->len) { 79 hu->tx_skb = skb; 80 break; 81 } 82 83 hci_uart_tx_complete(hu, hci_skb_pkt_type(skb)); 84 kfree_skb(skb); 85 } 86 87 clear_bit(HCI_UART_SENDING, &hu->tx_state); 88 } while (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state)); 89 } 90 91 /* ------- Interface to HCI layer ------ */ 92 93 /* Reset device */ 94 static int hci_uart_flush(struct hci_dev *hdev) 95 { 96 struct hci_uart *hu = hci_get_drvdata(hdev); 97 98 BT_DBG("hdev %p serdev %p", hdev, hu->serdev); 99 100 /* Stop write_work and hold SENDING so wakeups only set TX_WAKEUP. */ 101 disable_work_sync(&hu->write_work); 102 set_bit(HCI_UART_SENDING, &hu->tx_state); 103 104 if (hu->tx_skb) { 105 kfree_skb(hu->tx_skb); hu->tx_skb = NULL; 106 } 107 108 if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) { 109 /* Flush any pending characters in the driver and discipline. */ 110 serdev_device_write_flush(hu->serdev); 111 112 hu->proto->flush(hu); 113 } 114 115 /* Resume TX and replay a wakeup recorded while it was stopped. */ 116 enable_work(&hu->write_work); 117 clear_bit(HCI_UART_SENDING, &hu->tx_state); 118 if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state)) 119 hci_uart_tx_wakeup(hu); 120 121 return 0; 122 } 123 124 /* Initialize device */ 125 static int hci_uart_open(struct hci_dev *hdev) 126 { 127 struct hci_uart *hu = hci_get_drvdata(hdev); 128 int err; 129 130 BT_DBG("%s %p", hdev->name, hdev); 131 132 /* When Quirk HCI_QUIRK_NON_PERSISTENT_SETUP is set by 133 * driver, BT SoC is completely turned OFF during 134 * BT OFF. Upon next BT ON UART port should be opened. 135 */ 136 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) { 137 err = serdev_device_open(hu->serdev); 138 if (err) 139 return err; 140 set_bit(HCI_UART_PROTO_READY, &hu->flags); 141 } 142 143 /* Undo clearing this from hci_uart_close() */ 144 hdev->flush = hci_uart_flush; 145 146 return 0; 147 } 148 149 /* Close device */ 150 static int hci_uart_close(struct hci_dev *hdev) 151 { 152 struct hci_uart *hu = hci_get_drvdata(hdev); 153 154 BT_DBG("hdev %p", hdev); 155 156 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) 157 return 0; 158 159 hci_uart_flush(hdev); 160 hdev->flush = NULL; 161 162 /* When QUIRK HCI_QUIRK_NON_PERSISTENT_SETUP is set by driver, 163 * BT SOC is completely powered OFF during BT OFF, holding port 164 * open may drain the battery. 165 */ 166 if (hci_test_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_SETUP)) { 167 clear_bit(HCI_UART_PROTO_READY, &hu->flags); 168 serdev_device_close(hu->serdev); 169 } 170 171 return 0; 172 } 173 174 /* Send frames from HCI layer */ 175 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb) 176 { 177 struct hci_uart *hu = hci_get_drvdata(hdev); 178 179 BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb), 180 skb->len); 181 182 hu->proto->enqueue(hu, skb); 183 184 hci_uart_tx_wakeup(hu); 185 186 return 0; 187 } 188 189 static int hci_uart_setup(struct hci_dev *hdev) 190 { 191 struct hci_uart *hu = hci_get_drvdata(hdev); 192 struct hci_rp_read_local_version *ver; 193 struct sk_buff *skb; 194 unsigned int speed; 195 int err; 196 197 /* Init speed if any */ 198 if (hu->init_speed) 199 speed = hu->init_speed; 200 else if (hu->proto->init_speed) 201 speed = hu->proto->init_speed; 202 else 203 speed = 0; 204 205 if (speed) 206 serdev_device_set_baudrate(hu->serdev, speed); 207 208 /* Operational speed if any */ 209 if (hu->oper_speed) 210 speed = hu->oper_speed; 211 else if (hu->proto->oper_speed) 212 speed = hu->proto->oper_speed; 213 else 214 speed = 0; 215 216 if (hu->proto->set_baudrate && speed) { 217 err = hu->proto->set_baudrate(hu, speed); 218 if (err) 219 bt_dev_err(hdev, "Failed to set baudrate"); 220 else 221 serdev_device_set_baudrate(hu->serdev, speed); 222 } 223 224 if (hu->proto->setup) 225 return hu->proto->setup(hu); 226 227 if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags)) 228 return 0; 229 230 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, 231 HCI_INIT_TIMEOUT); 232 if (IS_ERR(skb)) { 233 bt_dev_err(hdev, "Reading local version info failed (%ld)", 234 PTR_ERR(skb)); 235 return PTR_ERR(skb); 236 } 237 238 if (skb->len != sizeof(*ver)) 239 bt_dev_err(hdev, "Event length mismatch for version info"); 240 241 kfree_skb(skb); 242 return 0; 243 } 244 245 /* Check if the device is wakeable */ 246 static bool hci_uart_wakeup(struct hci_dev *hdev) 247 { 248 /* HCI UART devices are assumed to be wakeable by default. 249 * Implement wakeup callback to override this behavior. 250 */ 251 return true; 252 } 253 254 /** hci_uart_write_wakeup - transmit buffer wakeup 255 * @serdev: serial device 256 * 257 * This function is called by the serdev framework when it accepts 258 * more data being sent. 259 */ 260 static void hci_uart_write_wakeup(struct serdev_device *serdev) 261 { 262 struct hci_uart *hu = serdev_device_get_drvdata(serdev); 263 264 BT_DBG(""); 265 266 if (!hu || serdev != hu->serdev) { 267 WARN_ON(1); 268 return; 269 } 270 271 if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) 272 hci_uart_tx_wakeup(hu); 273 } 274 275 /** hci_uart_receive_buf - receive buffer wakeup 276 * @serdev: serial device 277 * @data: pointer to received data 278 * @count: count of received data in bytes 279 * 280 * This function is called by the serdev framework when it received data 281 * in the RX buffer. 282 * 283 * Return: number of processed bytes 284 */ 285 static size_t hci_uart_receive_buf(struct serdev_device *serdev, 286 const u8 *data, size_t count) 287 { 288 struct hci_uart *hu = serdev_device_get_drvdata(serdev); 289 290 if (!hu || serdev != hu->serdev) { 291 WARN_ON(1); 292 return 0; 293 } 294 295 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) 296 return 0; 297 298 /* It does not need a lock here as it is already protected by a mutex in 299 * tty caller 300 */ 301 hu->proto->recv(hu, data, count); 302 303 if (hu->hdev) 304 hu->hdev->stat.byte_rx += count; 305 306 return count; 307 } 308 309 static const struct serdev_device_ops hci_serdev_client_ops = { 310 .receive_buf = hci_uart_receive_buf, 311 .write_wakeup = hci_uart_write_wakeup, 312 }; 313 314 int hci_uart_register_device_priv(struct hci_uart *hu, 315 const struct hci_uart_proto *p, 316 int sizeof_priv) 317 { 318 int err; 319 struct hci_dev *hdev; 320 321 BT_DBG(""); 322 323 serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops); 324 325 if (percpu_init_rwsem(&hu->proto_lock)) 326 return -ENOMEM; 327 328 err = serdev_device_open(hu->serdev); 329 if (err) 330 goto err_rwsem; 331 332 err = p->open(hu); 333 if (err) 334 goto err_open; 335 336 hu->proto = p; 337 set_bit(HCI_UART_PROTO_READY, &hu->flags); 338 339 /* Initialize and register HCI device */ 340 hdev = hci_alloc_dev_priv(sizeof_priv); 341 if (!hdev) { 342 BT_ERR("Can't allocate HCI device"); 343 err = -ENOMEM; 344 goto err_alloc; 345 } 346 347 hu->hdev = hdev; 348 349 hdev->bus = HCI_UART; 350 hci_set_drvdata(hdev, hu); 351 352 INIT_WORK(&hu->init_ready, hci_uart_init_work); 353 INIT_WORK(&hu->write_work, hci_uart_write_work); 354 355 /* Only when vendor specific setup callback is provided, consider 356 * the manufacturer information valid. This avoids filling in the 357 * value for Ericsson when nothing is specified. 358 */ 359 if (hu->proto->setup) 360 hdev->manufacturer = hu->proto->manufacturer; 361 362 hdev->open = hci_uart_open; 363 hdev->close = hci_uart_close; 364 hdev->flush = hci_uart_flush; 365 hdev->send = hci_uart_send_frame; 366 hdev->setup = hci_uart_setup; 367 if (!hdev->wakeup) 368 hdev->wakeup = hci_uart_wakeup; 369 SET_HCIDEV_DEV(hdev, &hu->serdev->dev); 370 371 if (test_bit(HCI_UART_NO_SUSPEND_NOTIFIER, &hu->flags)) 372 hci_set_quirk(hdev, HCI_QUIRK_NO_SUSPEND_NOTIFIER); 373 374 if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags)) 375 hci_set_quirk(hdev, HCI_QUIRK_RAW_DEVICE); 376 377 if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags)) 378 hci_set_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG); 379 380 if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags)) 381 return 0; 382 383 if (hci_register_dev(hdev) < 0) { 384 BT_ERR("Can't register HCI device"); 385 err = -ENODEV; 386 goto err_register; 387 } 388 389 set_bit(HCI_UART_REGISTERED, &hu->flags); 390 391 return 0; 392 393 err_register: 394 hci_free_dev(hdev); 395 err_alloc: 396 clear_bit(HCI_UART_PROTO_READY, &hu->flags); 397 p->close(hu); 398 err_open: 399 serdev_device_close(hu->serdev); 400 err_rwsem: 401 percpu_free_rwsem(&hu->proto_lock); 402 return err; 403 } 404 EXPORT_SYMBOL_GPL(hci_uart_register_device_priv); 405 406 void hci_uart_unregister_device(struct hci_uart *hu) 407 { 408 struct hci_dev *hdev = hu->hdev; 409 bool proto_ready; 410 411 /* Wait for init_ready to finish to prevent registration races */ 412 cancel_work_sync(&hu->init_ready); 413 414 proto_ready = test_bit(HCI_UART_PROTO_READY, &hu->flags); 415 if (proto_ready) { 416 /* Clear HCI_UART_PROTO_READY under the write lock so a 417 * concurrent hci_uart_tx_wakeup() cannot re-schedule 418 * write_work via the write_wakeup callback once the device 419 * is torn down. 420 */ 421 percpu_down_write(&hu->proto_lock); 422 clear_bit(HCI_UART_PROTO_READY, &hu->flags); 423 percpu_up_write(&hu->proto_lock); 424 } 425 426 /* Unconditionally cancel write_work AFTER clearing PROTO_READY. 427 * This ensures that concurrent protocol timers cannot requeue 428 * write_work, permanently preventing double-free races and UAFs, 429 * and guarantees no write_work is in flight before the serdev 430 * device is closed. 431 */ 432 cancel_work_sync(&hu->write_work); 433 434 /* Free any partially transmitted frame left over by write_work now 435 * that the transmit path is fully quiesced. hci_uart_close() would 436 * skip hci_uart_flush() because HCI_UART_PROTO_READY is cleared. 437 */ 438 if (hu->tx_skb) { 439 kfree_skb(hu->tx_skb); 440 hu->tx_skb = NULL; 441 } 442 443 if (test_bit(HCI_UART_REGISTERED, &hu->flags)) 444 hci_unregister_dev(hdev); 445 446 /* Close the protocol before freeing hdev (intrinsically purges queues). 447 * Some protocol close handlers (e.g. qca_close) may still access the 448 * serdev device, so keep the serdev port open until this completes. 449 */ 450 hu->proto->close(hu); 451 452 if (proto_ready) 453 serdev_device_close(hu->serdev); 454 455 hci_free_dev(hdev); 456 457 percpu_free_rwsem(&hu->proto_lock); 458 } 459 EXPORT_SYMBOL_GPL(hci_uart_unregister_device); 460