Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Texas Instruments' Bluetooth HCILL UART protocol |
| 3 | * |
| 4 | * HCILL (HCI Low Level) is a Texas Instruments' power management |
| 5 | * protocol extension to H4. |
| 6 | * |
| 7 | * Copyright (C) 2007 Texas Instruments, Inc. |
| 8 | * |
| 9 | * Written by Ohad Ben-Cohen <ohad@bencohen.org> |
| 10 | * |
| 11 | * Acknowledgements: |
| 12 | * This file is based on hci_h4.c, which was written |
| 13 | * by Maxim Krasnyansky and Marcel Holtmann. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License version 2 |
| 17 | * as published by the Free Software Foundation |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/kernel.h> |
| 32 | |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/sched.h> |
| 35 | #include <linux/types.h> |
| 36 | #include <linux/fcntl.h> |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 37 | #include <linux/firmware.h> |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 38 | #include <linux/interrupt.h> |
| 39 | #include <linux/ptrace.h> |
| 40 | #include <linux/poll.h> |
| 41 | |
| 42 | #include <linux/slab.h> |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 43 | #include <linux/errno.h> |
| 44 | #include <linux/string.h> |
| 45 | #include <linux/signal.h> |
| 46 | #include <linux/ioctl.h> |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 47 | #include <linux/of.h> |
| 48 | #include <linux/serdev.h> |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 49 | #include <linux/skbuff.h> |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 50 | #include <linux/ti_wilink_st.h> |
Ulf Hansson | 43d3d09 | 2017-06-07 11:08:21 +0200 | [diff] [blame] | 51 | #include <linux/clk.h> |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 52 | |
| 53 | #include <net/bluetooth/bluetooth.h> |
| 54 | #include <net/bluetooth/hci_core.h> |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 55 | #include <linux/gpio/consumer.h> |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 56 | |
| 57 | #include "hci_uart.h" |
| 58 | |
| 59 | /* HCILL commands */ |
| 60 | #define HCILL_GO_TO_SLEEP_IND 0x30 |
| 61 | #define HCILL_GO_TO_SLEEP_ACK 0x31 |
| 62 | #define HCILL_WAKE_UP_IND 0x32 |
| 63 | #define HCILL_WAKE_UP_ACK 0x33 |
| 64 | |
| 65 | /* HCILL receiver States */ |
| 66 | #define HCILL_W4_PACKET_TYPE 0 |
| 67 | #define HCILL_W4_EVENT_HDR 1 |
| 68 | #define HCILL_W4_ACL_HDR 2 |
| 69 | #define HCILL_W4_SCO_HDR 3 |
| 70 | #define HCILL_W4_DATA 4 |
| 71 | |
| 72 | /* HCILL states */ |
| 73 | enum hcill_states_e { |
| 74 | HCILL_ASLEEP, |
| 75 | HCILL_ASLEEP_TO_AWAKE, |
| 76 | HCILL_AWAKE, |
| 77 | HCILL_AWAKE_TO_ASLEEP |
| 78 | }; |
| 79 | |
| 80 | struct hcill_cmd { |
| 81 | u8 cmd; |
Gustavo F. Padovan | 81ca405 | 2010-07-19 13:54:05 -0300 | [diff] [blame] | 82 | } __packed; |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 83 | |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 84 | struct ll_device { |
| 85 | struct hci_uart hu; |
| 86 | struct serdev_device *serdev; |
| 87 | struct gpio_desc *enable_gpio; |
Ulf Hansson | 43d3d09 | 2017-06-07 11:08:21 +0200 | [diff] [blame] | 88 | struct clk *ext_clk; |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 89 | }; |
| 90 | |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 91 | struct ll_struct { |
| 92 | unsigned long rx_state; |
| 93 | unsigned long rx_count; |
| 94 | struct sk_buff *rx_skb; |
| 95 | struct sk_buff_head txq; |
| 96 | spinlock_t hcill_lock; /* HCILL state lock */ |
| 97 | unsigned long hcill_state; /* HCILL power state */ |
| 98 | struct sk_buff_head tx_wait_q; /* HCILL wait queue */ |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * Builds and sends an HCILL command packet. |
| 103 | * These are very simple packets with only 1 cmd byte |
| 104 | */ |
| 105 | static int send_hcill_cmd(u8 cmd, struct hci_uart *hu) |
| 106 | { |
| 107 | int err = 0; |
| 108 | struct sk_buff *skb = NULL; |
| 109 | struct ll_struct *ll = hu->priv; |
| 110 | struct hcill_cmd *hcill_packet; |
| 111 | |
| 112 | BT_DBG("hu %p cmd 0x%x", hu, cmd); |
| 113 | |
| 114 | /* allocate packet */ |
| 115 | skb = bt_skb_alloc(1, GFP_ATOMIC); |
| 116 | if (!skb) { |
| 117 | BT_ERR("cannot allocate memory for HCILL packet"); |
| 118 | err = -ENOMEM; |
| 119 | goto out; |
| 120 | } |
| 121 | |
| 122 | /* prepare packet */ |
Johannes Berg | 4df864c | 2017-06-16 14:29:21 +0200 | [diff] [blame] | 123 | hcill_packet = skb_put(skb, 1); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 124 | hcill_packet->cmd = cmd; |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 125 | |
| 126 | /* send packet */ |
| 127 | skb_queue_tail(&ll->txq, skb); |
| 128 | out: |
| 129 | return err; |
| 130 | } |
| 131 | |
| 132 | /* Initialize protocol */ |
| 133 | static int ll_open(struct hci_uart *hu) |
| 134 | { |
| 135 | struct ll_struct *ll; |
| 136 | |
| 137 | BT_DBG("hu %p", hu); |
| 138 | |
David Herrmann | 9eb648c | 2012-01-07 15:19:37 +0100 | [diff] [blame] | 139 | ll = kzalloc(sizeof(*ll), GFP_KERNEL); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 140 | if (!ll) |
| 141 | return -ENOMEM; |
| 142 | |
| 143 | skb_queue_head_init(&ll->txq); |
| 144 | skb_queue_head_init(&ll->tx_wait_q); |
| 145 | spin_lock_init(&ll->hcill_lock); |
| 146 | |
| 147 | ll->hcill_state = HCILL_AWAKE; |
| 148 | |
| 149 | hu->priv = ll; |
| 150 | |
Ulf Hansson | 43d3d09 | 2017-06-07 11:08:21 +0200 | [diff] [blame] | 151 | if (hu->serdev) { |
| 152 | struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev); |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 153 | serdev_device_open(hu->serdev); |
Ulf Hansson | 43d3d09 | 2017-06-07 11:08:21 +0200 | [diff] [blame] | 154 | if (!IS_ERR(lldev->ext_clk)) |
| 155 | clk_prepare_enable(lldev->ext_clk); |
| 156 | } |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 157 | |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* Flush protocol data */ |
| 162 | static int ll_flush(struct hci_uart *hu) |
| 163 | { |
| 164 | struct ll_struct *ll = hu->priv; |
| 165 | |
| 166 | BT_DBG("hu %p", hu); |
| 167 | |
| 168 | skb_queue_purge(&ll->tx_wait_q); |
| 169 | skb_queue_purge(&ll->txq); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* Close protocol */ |
| 175 | static int ll_close(struct hci_uart *hu) |
| 176 | { |
| 177 | struct ll_struct *ll = hu->priv; |
| 178 | |
| 179 | BT_DBG("hu %p", hu); |
| 180 | |
| 181 | skb_queue_purge(&ll->tx_wait_q); |
| 182 | skb_queue_purge(&ll->txq); |
| 183 | |
Wei Yongjun | b1fb068 | 2009-02-25 18:09:33 +0800 | [diff] [blame] | 184 | kfree_skb(ll->rx_skb); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 185 | |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 186 | if (hu->serdev) { |
| 187 | struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev); |
| 188 | gpiod_set_value_cansleep(lldev->enable_gpio, 0); |
| 189 | |
Ulf Hansson | 43d3d09 | 2017-06-07 11:08:21 +0200 | [diff] [blame] | 190 | clk_disable_unprepare(lldev->ext_clk); |
| 191 | |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 192 | serdev_device_close(hu->serdev); |
| 193 | } |
| 194 | |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 195 | hu->priv = NULL; |
| 196 | |
| 197 | kfree(ll); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * internal function, which does common work of the device wake up process: |
| 204 | * 1. places all pending packets (waiting in tx_wait_q list) in txq list. |
| 205 | * 2. changes internal state to HCILL_AWAKE. |
| 206 | * Note: assumes that hcill_lock spinlock is taken, |
| 207 | * shouldn't be called otherwise! |
| 208 | */ |
| 209 | static void __ll_do_awake(struct ll_struct *ll) |
| 210 | { |
| 211 | struct sk_buff *skb = NULL; |
| 212 | |
| 213 | while ((skb = skb_dequeue(&ll->tx_wait_q))) |
| 214 | skb_queue_tail(&ll->txq, skb); |
| 215 | |
| 216 | ll->hcill_state = HCILL_AWAKE; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Called upon a wake-up-indication from the device |
| 221 | */ |
| 222 | static void ll_device_want_to_wakeup(struct hci_uart *hu) |
| 223 | { |
| 224 | unsigned long flags; |
| 225 | struct ll_struct *ll = hu->priv; |
| 226 | |
| 227 | BT_DBG("hu %p", hu); |
| 228 | |
| 229 | /* lock hcill state */ |
| 230 | spin_lock_irqsave(&ll->hcill_lock, flags); |
| 231 | |
| 232 | switch (ll->hcill_state) { |
Ohad Ben-Cohen | 5c54822 | 2008-01-10 22:24:43 -0800 | [diff] [blame] | 233 | case HCILL_ASLEEP_TO_AWAKE: |
| 234 | /* |
| 235 | * This state means that both the host and the BRF chip |
| 236 | * have simultaneously sent a wake-up-indication packet. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 237 | * Traditionally, in this case, receiving a wake-up-indication |
Ohad Ben-Cohen | 5c54822 | 2008-01-10 22:24:43 -0800 | [diff] [blame] | 238 | * was enough and an additional wake-up-ack wasn't needed. |
| 239 | * This has changed with the BRF6350, which does require an |
| 240 | * explicit wake-up-ack. Other BRF versions, which do not |
| 241 | * require an explicit ack here, do accept it, thus it is |
| 242 | * perfectly safe to always send one. |
| 243 | */ |
| 244 | BT_DBG("dual wake-up-indication"); |
| 245 | /* deliberate fall-through - do not add break */ |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 246 | case HCILL_ASLEEP: |
| 247 | /* acknowledge device wake up */ |
| 248 | if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) { |
| 249 | BT_ERR("cannot acknowledge device wake up"); |
| 250 | goto out; |
| 251 | } |
| 252 | break; |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 253 | default: |
Ohad Ben-Cohen | 5c54822 | 2008-01-10 22:24:43 -0800 | [diff] [blame] | 254 | /* any other state is illegal */ |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 255 | BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state); |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | /* send pending packets and change state to HCILL_AWAKE */ |
| 260 | __ll_do_awake(ll); |
| 261 | |
| 262 | out: |
| 263 | spin_unlock_irqrestore(&ll->hcill_lock, flags); |
| 264 | |
| 265 | /* actually send the packets */ |
| 266 | hci_uart_tx_wakeup(hu); |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Called upon a sleep-indication from the device |
| 271 | */ |
| 272 | static void ll_device_want_to_sleep(struct hci_uart *hu) |
| 273 | { |
| 274 | unsigned long flags; |
| 275 | struct ll_struct *ll = hu->priv; |
| 276 | |
| 277 | BT_DBG("hu %p", hu); |
| 278 | |
| 279 | /* lock hcill state */ |
| 280 | spin_lock_irqsave(&ll->hcill_lock, flags); |
| 281 | |
| 282 | /* sanity check */ |
| 283 | if (ll->hcill_state != HCILL_AWAKE) |
| 284 | BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state); |
| 285 | |
| 286 | /* acknowledge device sleep */ |
| 287 | if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) { |
| 288 | BT_ERR("cannot acknowledge device sleep"); |
| 289 | goto out; |
| 290 | } |
| 291 | |
| 292 | /* update state */ |
| 293 | ll->hcill_state = HCILL_ASLEEP; |
| 294 | |
| 295 | out: |
| 296 | spin_unlock_irqrestore(&ll->hcill_lock, flags); |
| 297 | |
| 298 | /* actually send the sleep ack packet */ |
| 299 | hci_uart_tx_wakeup(hu); |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Called upon wake-up-acknowledgement from the device |
| 304 | */ |
| 305 | static void ll_device_woke_up(struct hci_uart *hu) |
| 306 | { |
| 307 | unsigned long flags; |
| 308 | struct ll_struct *ll = hu->priv; |
| 309 | |
| 310 | BT_DBG("hu %p", hu); |
| 311 | |
| 312 | /* lock hcill state */ |
| 313 | spin_lock_irqsave(&ll->hcill_lock, flags); |
| 314 | |
| 315 | /* sanity check */ |
| 316 | if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE) |
| 317 | BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state); |
| 318 | |
| 319 | /* send pending packets and change state to HCILL_AWAKE */ |
| 320 | __ll_do_awake(ll); |
| 321 | |
| 322 | spin_unlock_irqrestore(&ll->hcill_lock, flags); |
| 323 | |
| 324 | /* actually send the packets */ |
| 325 | hci_uart_tx_wakeup(hu); |
| 326 | } |
| 327 | |
| 328 | /* Enqueue frame for transmittion (padding, crc, etc) */ |
| 329 | /* may be called from two simultaneous tasklets */ |
| 330 | static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
| 331 | { |
| 332 | unsigned long flags = 0; |
| 333 | struct ll_struct *ll = hu->priv; |
| 334 | |
| 335 | BT_DBG("hu %p skb %p", hu, skb); |
| 336 | |
| 337 | /* Prepend skb with frame type */ |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 338 | memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 339 | |
| 340 | /* lock hcill state */ |
| 341 | spin_lock_irqsave(&ll->hcill_lock, flags); |
| 342 | |
| 343 | /* act according to current state */ |
| 344 | switch (ll->hcill_state) { |
| 345 | case HCILL_AWAKE: |
| 346 | BT_DBG("device awake, sending normally"); |
| 347 | skb_queue_tail(&ll->txq, skb); |
| 348 | break; |
| 349 | case HCILL_ASLEEP: |
| 350 | BT_DBG("device asleep, waking up and queueing packet"); |
| 351 | /* save packet for later */ |
| 352 | skb_queue_tail(&ll->tx_wait_q, skb); |
| 353 | /* awake device */ |
| 354 | if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) { |
| 355 | BT_ERR("cannot wake up device"); |
| 356 | break; |
| 357 | } |
| 358 | ll->hcill_state = HCILL_ASLEEP_TO_AWAKE; |
| 359 | break; |
| 360 | case HCILL_ASLEEP_TO_AWAKE: |
| 361 | BT_DBG("device waking up, queueing packet"); |
| 362 | /* transient state; just keep packet for later */ |
| 363 | skb_queue_tail(&ll->tx_wait_q, skb); |
| 364 | break; |
| 365 | default: |
| 366 | BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state); |
| 367 | kfree_skb(skb); |
| 368 | break; |
| 369 | } |
| 370 | |
| 371 | spin_unlock_irqrestore(&ll->hcill_lock, flags); |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 376 | static inline int ll_check_data_len(struct hci_dev *hdev, struct ll_struct *ll, int len) |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 377 | { |
Gustavo Padovan | fc5fef6 | 2012-05-23 04:04:19 -0300 | [diff] [blame] | 378 | int room = skb_tailroom(ll->rx_skb); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 379 | |
| 380 | BT_DBG("len %d room %d", len, room); |
| 381 | |
| 382 | if (!len) { |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 383 | hci_recv_frame(hdev, ll->rx_skb); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 384 | } else if (len > room) { |
| 385 | BT_ERR("Data length is too large"); |
| 386 | kfree_skb(ll->rx_skb); |
| 387 | } else { |
| 388 | ll->rx_state = HCILL_W4_DATA; |
| 389 | ll->rx_count = len; |
| 390 | return len; |
| 391 | } |
| 392 | |
| 393 | ll->rx_state = HCILL_W4_PACKET_TYPE; |
| 394 | ll->rx_skb = NULL; |
| 395 | ll->rx_count = 0; |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | /* Recv data */ |
Marcel Holtmann | 9d1c40e | 2015-04-04 20:59:41 -0700 | [diff] [blame] | 401 | static int ll_recv(struct hci_uart *hu, const void *data, int count) |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 402 | { |
| 403 | struct ll_struct *ll = hu->priv; |
Marcel Holtmann | 9d1c40e | 2015-04-04 20:59:41 -0700 | [diff] [blame] | 404 | const char *ptr; |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 405 | struct hci_event_hdr *eh; |
| 406 | struct hci_acl_hdr *ah; |
| 407 | struct hci_sco_hdr *sh; |
Gustavo Padovan | fc5fef6 | 2012-05-23 04:04:19 -0300 | [diff] [blame] | 408 | int len, type, dlen; |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 409 | |
| 410 | BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, ll->rx_state, ll->rx_count); |
| 411 | |
| 412 | ptr = data; |
| 413 | while (count) { |
| 414 | if (ll->rx_count) { |
| 415 | len = min_t(unsigned int, ll->rx_count, count); |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 416 | skb_put_data(ll->rx_skb, ptr, len); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 417 | ll->rx_count -= len; count -= len; ptr += len; |
| 418 | |
| 419 | if (ll->rx_count) |
| 420 | continue; |
| 421 | |
| 422 | switch (ll->rx_state) { |
| 423 | case HCILL_W4_DATA: |
| 424 | BT_DBG("Complete data"); |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 425 | hci_recv_frame(hu->hdev, ll->rx_skb); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 426 | |
| 427 | ll->rx_state = HCILL_W4_PACKET_TYPE; |
| 428 | ll->rx_skb = NULL; |
| 429 | continue; |
| 430 | |
| 431 | case HCILL_W4_EVENT_HDR: |
Gustavo F. Padovan | acce90d | 2010-05-01 16:15:34 -0300 | [diff] [blame] | 432 | eh = hci_event_hdr(ll->rx_skb); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 433 | |
| 434 | BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen); |
| 435 | |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 436 | ll_check_data_len(hu->hdev, ll, eh->plen); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 437 | continue; |
| 438 | |
| 439 | case HCILL_W4_ACL_HDR: |
Gustavo F. Padovan | acce90d | 2010-05-01 16:15:34 -0300 | [diff] [blame] | 440 | ah = hci_acl_hdr(ll->rx_skb); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 441 | dlen = __le16_to_cpu(ah->dlen); |
| 442 | |
| 443 | BT_DBG("ACL header: dlen %d", dlen); |
| 444 | |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 445 | ll_check_data_len(hu->hdev, ll, dlen); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 446 | continue; |
| 447 | |
| 448 | case HCILL_W4_SCO_HDR: |
Gustavo F. Padovan | acce90d | 2010-05-01 16:15:34 -0300 | [diff] [blame] | 449 | sh = hci_sco_hdr(ll->rx_skb); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 450 | |
| 451 | BT_DBG("SCO header: dlen %d", sh->dlen); |
| 452 | |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 453 | ll_check_data_len(hu->hdev, ll, sh->dlen); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 454 | continue; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /* HCILL_W4_PACKET_TYPE */ |
| 459 | switch (*ptr) { |
| 460 | case HCI_EVENT_PKT: |
| 461 | BT_DBG("Event packet"); |
| 462 | ll->rx_state = HCILL_W4_EVENT_HDR; |
| 463 | ll->rx_count = HCI_EVENT_HDR_SIZE; |
| 464 | type = HCI_EVENT_PKT; |
| 465 | break; |
| 466 | |
| 467 | case HCI_ACLDATA_PKT: |
| 468 | BT_DBG("ACL packet"); |
| 469 | ll->rx_state = HCILL_W4_ACL_HDR; |
| 470 | ll->rx_count = HCI_ACL_HDR_SIZE; |
| 471 | type = HCI_ACLDATA_PKT; |
| 472 | break; |
| 473 | |
| 474 | case HCI_SCODATA_PKT: |
| 475 | BT_DBG("SCO packet"); |
| 476 | ll->rx_state = HCILL_W4_SCO_HDR; |
| 477 | ll->rx_count = HCI_SCO_HDR_SIZE; |
| 478 | type = HCI_SCODATA_PKT; |
| 479 | break; |
| 480 | |
| 481 | /* HCILL signals */ |
| 482 | case HCILL_GO_TO_SLEEP_IND: |
| 483 | BT_DBG("HCILL_GO_TO_SLEEP_IND packet"); |
| 484 | ll_device_want_to_sleep(hu); |
| 485 | ptr++; count--; |
| 486 | continue; |
| 487 | |
| 488 | case HCILL_GO_TO_SLEEP_ACK: |
| 489 | /* shouldn't happen */ |
| 490 | BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll->hcill_state); |
| 491 | ptr++; count--; |
| 492 | continue; |
| 493 | |
| 494 | case HCILL_WAKE_UP_IND: |
| 495 | BT_DBG("HCILL_WAKE_UP_IND packet"); |
| 496 | ll_device_want_to_wakeup(hu); |
| 497 | ptr++; count--; |
| 498 | continue; |
| 499 | |
| 500 | case HCILL_WAKE_UP_ACK: |
| 501 | BT_DBG("HCILL_WAKE_UP_ACK packet"); |
| 502 | ll_device_woke_up(hu); |
| 503 | ptr++; count--; |
| 504 | continue; |
| 505 | |
| 506 | default: |
| 507 | BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr); |
| 508 | hu->hdev->stat.err_rx++; |
| 509 | ptr++; count--; |
| 510 | continue; |
Peter Senna Tschudin | d650cca | 2012-09-07 17:24:42 +0200 | [diff] [blame] | 511 | } |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 512 | |
| 513 | ptr++; count--; |
| 514 | |
| 515 | /* Allocate packet */ |
| 516 | ll->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC); |
| 517 | if (!ll->rx_skb) { |
| 518 | BT_ERR("Can't allocate mem for new packet"); |
| 519 | ll->rx_state = HCILL_W4_PACKET_TYPE; |
| 520 | ll->rx_count = 0; |
Gustavo F. Padovan | fe1aff7 | 2010-05-01 16:15:34 -0300 | [diff] [blame] | 521 | return -ENOMEM; |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 524 | hci_skb_pkt_type(ll->rx_skb) = type; |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | return count; |
| 528 | } |
| 529 | |
| 530 | static struct sk_buff *ll_dequeue(struct hci_uart *hu) |
| 531 | { |
| 532 | struct ll_struct *ll = hu->priv; |
| 533 | return skb_dequeue(&ll->txq); |
| 534 | } |
| 535 | |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 536 | #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS) |
| 537 | static int read_local_version(struct hci_dev *hdev) |
| 538 | { |
| 539 | int err = 0; |
| 540 | unsigned short version = 0; |
| 541 | struct sk_buff *skb; |
| 542 | struct hci_rp_read_local_version *ver; |
| 543 | |
| 544 | skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, HCI_INIT_TIMEOUT); |
| 545 | if (IS_ERR(skb)) { |
| 546 | bt_dev_err(hdev, "Reading TI version information failed (%ld)", |
| 547 | PTR_ERR(skb)); |
Sebastian Reichel | f2edd9f | 2017-04-15 23:54:13 +0200 | [diff] [blame] | 548 | return PTR_ERR(skb); |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 549 | } |
| 550 | if (skb->len != sizeof(*ver)) { |
| 551 | err = -EILSEQ; |
| 552 | goto out; |
| 553 | } |
| 554 | |
| 555 | ver = (struct hci_rp_read_local_version *)skb->data; |
| 556 | if (le16_to_cpu(ver->manufacturer) != 13) { |
| 557 | err = -ENODEV; |
| 558 | goto out; |
| 559 | } |
| 560 | |
| 561 | version = le16_to_cpu(ver->lmp_subver); |
| 562 | |
| 563 | out: |
| 564 | if (err) bt_dev_err(hdev, "Failed to read TI version info: %d", err); |
| 565 | kfree_skb(skb); |
| 566 | return err ? err : version; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * download_firmware - |
| 571 | * internal function which parses through the .bts firmware |
| 572 | * script file intreprets SEND, DELAY actions only as of now |
| 573 | */ |
| 574 | static int download_firmware(struct ll_device *lldev) |
| 575 | { |
| 576 | unsigned short chip, min_ver, maj_ver; |
| 577 | int version, err, len; |
| 578 | unsigned char *ptr, *action_ptr; |
| 579 | unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */ |
| 580 | const struct firmware *fw; |
| 581 | struct sk_buff *skb; |
| 582 | struct hci_command *cmd; |
| 583 | |
| 584 | version = read_local_version(lldev->hu.hdev); |
| 585 | if (version < 0) |
| 586 | return version; |
| 587 | |
| 588 | chip = (version & 0x7C00) >> 10; |
| 589 | min_ver = (version & 0x007F); |
| 590 | maj_ver = (version & 0x0380) >> 7; |
| 591 | if (version & 0x8000) |
| 592 | maj_ver |= 0x0008; |
| 593 | |
| 594 | snprintf(bts_scr_name, sizeof(bts_scr_name), |
| 595 | "ti-connectivity/TIInit_%d.%d.%d.bts", |
| 596 | chip, maj_ver, min_ver); |
| 597 | |
| 598 | err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev); |
| 599 | if (err || !fw->data || !fw->size) { |
| 600 | bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s", |
| 601 | err, bts_scr_name); |
| 602 | return -EINVAL; |
| 603 | } |
| 604 | ptr = (void *)fw->data; |
| 605 | len = fw->size; |
| 606 | /* bts_header to remove out magic number and |
| 607 | * version |
| 608 | */ |
| 609 | ptr += sizeof(struct bts_header); |
| 610 | len -= sizeof(struct bts_header); |
| 611 | |
| 612 | while (len > 0 && ptr) { |
| 613 | bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ", |
| 614 | ((struct bts_action *)ptr)->size, |
| 615 | ((struct bts_action *)ptr)->type); |
| 616 | |
| 617 | action_ptr = &(((struct bts_action *)ptr)->data[0]); |
| 618 | |
| 619 | switch (((struct bts_action *)ptr)->type) { |
| 620 | case ACTION_SEND_COMMAND: /* action send */ |
| 621 | bt_dev_dbg(lldev->hu.hdev, "S"); |
| 622 | cmd = (struct hci_command *)action_ptr; |
| 623 | if (cmd->opcode == 0xff36) { |
| 624 | /* ignore remote change |
| 625 | * baud rate HCI VS command */ |
| 626 | bt_dev_warn(lldev->hu.hdev, "change remote baud rate command in firmware"); |
| 627 | break; |
| 628 | } |
| 629 | if (cmd->prefix != 1) |
| 630 | bt_dev_dbg(lldev->hu.hdev, "command type %d\n", cmd->prefix); |
| 631 | |
| 632 | skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen, &cmd->speed, HCI_INIT_TIMEOUT); |
| 633 | if (IS_ERR(skb)) { |
| 634 | bt_dev_err(lldev->hu.hdev, "send command failed\n"); |
Guodong Xu | 823b842 | 2017-05-22 21:50:42 +0800 | [diff] [blame] | 635 | err = PTR_ERR(skb); |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 636 | goto out_rel_fw; |
| 637 | } |
| 638 | kfree_skb(skb); |
| 639 | break; |
| 640 | case ACTION_WAIT_EVENT: /* wait */ |
| 641 | /* no need to wait as command was synchronous */ |
| 642 | bt_dev_dbg(lldev->hu.hdev, "W"); |
| 643 | break; |
| 644 | case ACTION_DELAY: /* sleep */ |
| 645 | bt_dev_info(lldev->hu.hdev, "sleep command in scr"); |
| 646 | mdelay(((struct bts_action_delay *)action_ptr)->msec); |
| 647 | break; |
| 648 | } |
| 649 | len -= (sizeof(struct bts_action) + |
| 650 | ((struct bts_action *)ptr)->size); |
| 651 | ptr += sizeof(struct bts_action) + |
| 652 | ((struct bts_action *)ptr)->size; |
| 653 | } |
| 654 | |
| 655 | out_rel_fw: |
| 656 | /* fw download complete */ |
| 657 | release_firmware(fw); |
| 658 | return err; |
| 659 | } |
| 660 | |
| 661 | static int ll_setup(struct hci_uart *hu) |
| 662 | { |
| 663 | int err, retry = 3; |
| 664 | struct ll_device *lldev; |
| 665 | struct serdev_device *serdev = hu->serdev; |
| 666 | u32 speed; |
| 667 | |
| 668 | if (!serdev) |
| 669 | return 0; |
| 670 | |
| 671 | lldev = serdev_device_get_drvdata(serdev); |
| 672 | |
| 673 | serdev_device_set_flow_control(serdev, true); |
| 674 | |
| 675 | do { |
| 676 | /* Configure BT_EN to HIGH state */ |
| 677 | gpiod_set_value_cansleep(lldev->enable_gpio, 0); |
| 678 | msleep(5); |
| 679 | gpiod_set_value_cansleep(lldev->enable_gpio, 1); |
| 680 | msleep(100); |
| 681 | |
| 682 | err = download_firmware(lldev); |
| 683 | if (!err) |
| 684 | break; |
| 685 | |
| 686 | /* Toggle BT_EN and retry */ |
| 687 | bt_dev_err(hu->hdev, "download firmware failed, retrying..."); |
| 688 | } while (retry--); |
| 689 | |
| 690 | if (err) |
| 691 | return err; |
| 692 | |
| 693 | /* Operational speed if any */ |
| 694 | if (hu->oper_speed) |
| 695 | speed = hu->oper_speed; |
| 696 | else if (hu->proto->oper_speed) |
| 697 | speed = hu->proto->oper_speed; |
| 698 | else |
| 699 | speed = 0; |
| 700 | |
| 701 | if (speed) { |
| 702 | struct sk_buff *skb = __hci_cmd_sync(hu->hdev, 0xff36, sizeof(speed), &speed, HCI_INIT_TIMEOUT); |
| 703 | if (!IS_ERR(skb)) { |
| 704 | kfree_skb(skb); |
| 705 | serdev_device_set_baudrate(serdev, speed); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static const struct hci_uart_proto llp; |
| 713 | |
| 714 | static int hci_ti_probe(struct serdev_device *serdev) |
| 715 | { |
| 716 | struct hci_uart *hu; |
| 717 | struct ll_device *lldev; |
| 718 | u32 max_speed = 3000000; |
| 719 | |
| 720 | lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL); |
| 721 | if (!lldev) |
| 722 | return -ENOMEM; |
| 723 | hu = &lldev->hu; |
| 724 | |
| 725 | serdev_device_set_drvdata(serdev, lldev); |
| 726 | lldev->serdev = hu->serdev = serdev; |
| 727 | |
| 728 | lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev, "enable", GPIOD_OUT_LOW); |
| 729 | if (IS_ERR(lldev->enable_gpio)) |
| 730 | return PTR_ERR(lldev->enable_gpio); |
| 731 | |
Ulf Hansson | 43d3d09 | 2017-06-07 11:08:21 +0200 | [diff] [blame] | 732 | lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock"); |
| 733 | if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT) |
| 734 | return PTR_ERR(lldev->ext_clk); |
| 735 | |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 736 | of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed); |
| 737 | hci_uart_set_speeds(hu, 115200, max_speed); |
| 738 | |
| 739 | return hci_uart_register_device(hu, &llp); |
| 740 | } |
| 741 | |
| 742 | static void hci_ti_remove(struct serdev_device *serdev) |
| 743 | { |
| 744 | struct ll_device *lldev = serdev_device_get_drvdata(serdev); |
| 745 | struct hci_uart *hu = &lldev->hu; |
| 746 | struct hci_dev *hdev = hu->hdev; |
| 747 | |
| 748 | cancel_work_sync(&hu->write_work); |
| 749 | |
| 750 | hci_unregister_dev(hdev); |
| 751 | hci_free_dev(hdev); |
| 752 | hu->proto->close(hu); |
| 753 | } |
| 754 | |
| 755 | static const struct of_device_id hci_ti_of_match[] = { |
Sebastian Reichel | c127a87 | 2017-06-08 22:58:45 +0200 | [diff] [blame] | 756 | { .compatible = "ti,wl1271-st" }, |
| 757 | { .compatible = "ti,wl1273-st" }, |
| 758 | { .compatible = "ti,wl1281-st" }, |
| 759 | { .compatible = "ti,wl1283-st" }, |
| 760 | { .compatible = "ti,wl1285-st" }, |
| 761 | { .compatible = "ti,wl1801-st" }, |
| 762 | { .compatible = "ti,wl1805-st" }, |
| 763 | { .compatible = "ti,wl1807-st" }, |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 764 | { .compatible = "ti,wl1831-st" }, |
| 765 | { .compatible = "ti,wl1835-st" }, |
| 766 | { .compatible = "ti,wl1837-st" }, |
| 767 | {}, |
| 768 | }; |
| 769 | MODULE_DEVICE_TABLE(of, hci_ti_of_match); |
| 770 | |
| 771 | static struct serdev_device_driver hci_ti_drv = { |
| 772 | .driver = { |
| 773 | .name = "hci-ti", |
| 774 | .of_match_table = of_match_ptr(hci_ti_of_match), |
| 775 | }, |
| 776 | .probe = hci_ti_probe, |
| 777 | .remove = hci_ti_remove, |
| 778 | }; |
| 779 | #else |
| 780 | #define ll_setup NULL |
| 781 | #endif |
| 782 | |
Marcel Holtmann | 4ee7ef1 | 2015-04-04 22:11:43 -0700 | [diff] [blame] | 783 | static const struct hci_uart_proto llp = { |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 784 | .id = HCI_UART_LL, |
Marcel Holtmann | 7c40fb8 | 2015-04-04 22:27:34 -0700 | [diff] [blame] | 785 | .name = "LL", |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 786 | .setup = ll_setup, |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 787 | .open = ll_open, |
| 788 | .close = ll_close, |
| 789 | .recv = ll_recv, |
| 790 | .enqueue = ll_enqueue, |
| 791 | .dequeue = ll_dequeue, |
| 792 | .flush = ll_flush, |
| 793 | }; |
| 794 | |
Gustavo F. Padovan | f2b94bb | 2010-07-24 02:04:44 -0300 | [diff] [blame] | 795 | int __init ll_init(void) |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 796 | { |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 797 | serdev_device_driver_register(&hci_ti_drv); |
| 798 | |
Marcel Holtmann | 01009ee | 2015-04-04 22:27:35 -0700 | [diff] [blame] | 799 | return hci_uart_register_proto(&llp); |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 800 | } |
| 801 | |
Gustavo F. Padovan | f2b94bb | 2010-07-24 02:04:44 -0300 | [diff] [blame] | 802 | int __exit ll_deinit(void) |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 803 | { |
Rob Herring | 3718055 | 2017-04-13 10:03:52 -0500 | [diff] [blame] | 804 | serdev_device_driver_unregister(&hci_ti_drv); |
| 805 | |
Ohad Ben-Cohen | 166d2f6 | 2007-10-20 13:42:36 +0200 | [diff] [blame] | 806 | return hci_uart_unregister_proto(&llp); |
| 807 | } |