Marcel Holtmann | 16e3887 | 2015-04-04 16:13:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Bluetooth HCI UART driver for Intel devices |
| 4 | * |
| 5 | * Copyright (C) 2015 Intel Corporation |
| 6 | * |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/skbuff.h> |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 27 | #include <linux/firmware.h> |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 28 | #include <linux/module.h> |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 29 | #include <linux/wait.h> |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 30 | #include <linux/tty.h> |
| 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/gpio/consumer.h> |
| 33 | #include <linux/acpi.h> |
Loic Poulain | 765ea3a | 2015-08-29 13:38:18 +0200 | [diff] [blame] | 34 | #include <linux/interrupt.h> |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 35 | #include <linux/pm_runtime.h> |
Marcel Holtmann | 16e3887 | 2015-04-04 16:13:02 -0700 | [diff] [blame] | 36 | |
| 37 | #include <net/bluetooth/bluetooth.h> |
| 38 | #include <net/bluetooth/hci_core.h> |
| 39 | |
| 40 | #include "hci_uart.h" |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 41 | #include "btintel.h" |
| 42 | |
| 43 | #define STATE_BOOTLOADER 0 |
| 44 | #define STATE_DOWNLOADING 1 |
| 45 | #define STATE_FIRMWARE_LOADED 2 |
| 46 | #define STATE_FIRMWARE_FAILED 3 |
| 47 | #define STATE_BOOTING 4 |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 48 | #define STATE_LPM_ENABLED 5 |
| 49 | #define STATE_TX_ACTIVE 6 |
Loic Poulain | 8943654 | 2015-09-02 12:04:11 +0200 | [diff] [blame] | 50 | #define STATE_SUSPENDED 7 |
| 51 | #define STATE_LPM_TRANSACTION 8 |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 52 | |
Loic Poulain | 8943654 | 2015-09-02 12:04:11 +0200 | [diff] [blame] | 53 | #define HCI_LPM_WAKE_PKT 0xf0 |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 54 | #define HCI_LPM_PKT 0xf1 |
| 55 | #define HCI_LPM_MAX_SIZE 10 |
| 56 | #define HCI_LPM_HDR_SIZE HCI_EVENT_HDR_SIZE |
| 57 | |
| 58 | #define LPM_OP_TX_NOTIFY 0x00 |
Loic Poulain | 8943654 | 2015-09-02 12:04:11 +0200 | [diff] [blame] | 59 | #define LPM_OP_SUSPEND_ACK 0x02 |
| 60 | #define LPM_OP_RESUME_ACK 0x03 |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 61 | |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 62 | #define LPM_SUSPEND_DELAY_MS 1000 |
| 63 | |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 64 | struct hci_lpm_pkt { |
| 65 | __u8 opcode; |
| 66 | __u8 dlen; |
| 67 | __u8 data[0]; |
| 68 | } __packed; |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 69 | |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 70 | struct intel_device { |
| 71 | struct list_head list; |
| 72 | struct platform_device *pdev; |
| 73 | struct gpio_desc *reset; |
Loic Poulain | aa6802d | 2015-09-02 12:04:12 +0200 | [diff] [blame] | 74 | struct hci_uart *hu; |
| 75 | struct mutex hu_lock; |
Loic Poulain | 765ea3a | 2015-08-29 13:38:18 +0200 | [diff] [blame] | 76 | int irq; |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | static LIST_HEAD(intel_device_list); |
Loic Poulain | 67c8bde | 2015-08-31 18:34:31 +0200 | [diff] [blame] | 80 | static DEFINE_MUTEX(intel_device_list_lock); |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 81 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 82 | struct intel_data { |
| 83 | struct sk_buff *rx_skb; |
| 84 | struct sk_buff_head txq; |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 85 | struct work_struct busy_work; |
| 86 | struct hci_uart *hu; |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 87 | unsigned long flags; |
| 88 | }; |
| 89 | |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 90 | static u8 intel_convert_speed(unsigned int speed) |
| 91 | { |
| 92 | switch (speed) { |
| 93 | case 9600: |
| 94 | return 0x00; |
| 95 | case 19200: |
| 96 | return 0x01; |
| 97 | case 38400: |
| 98 | return 0x02; |
| 99 | case 57600: |
| 100 | return 0x03; |
| 101 | case 115200: |
| 102 | return 0x04; |
| 103 | case 230400: |
| 104 | return 0x05; |
| 105 | case 460800: |
| 106 | return 0x06; |
| 107 | case 921600: |
| 108 | return 0x07; |
| 109 | case 1843200: |
| 110 | return 0x08; |
| 111 | case 3250000: |
| 112 | return 0x09; |
| 113 | case 2000000: |
| 114 | return 0x0a; |
| 115 | case 3000000: |
| 116 | return 0x0b; |
| 117 | default: |
| 118 | return 0xff; |
| 119 | } |
| 120 | } |
| 121 | |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 122 | static int intel_wait_booting(struct hci_uart *hu) |
| 123 | { |
| 124 | struct intel_data *intel = hu->priv; |
| 125 | int err; |
| 126 | |
| 127 | err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING, |
| 128 | TASK_INTERRUPTIBLE, |
| 129 | msecs_to_jiffies(1000)); |
| 130 | |
| 131 | if (err == 1) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 132 | bt_dev_err(hu->hdev, "Device boot interrupted"); |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 133 | return -EINTR; |
| 134 | } |
| 135 | |
| 136 | if (err) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 137 | bt_dev_err(hu->hdev, "Device boot timeout"); |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 138 | return -ETIMEDOUT; |
| 139 | } |
| 140 | |
| 141 | return err; |
| 142 | } |
| 143 | |
Loic Poulain | a9cb0fe | 2015-09-04 17:39:25 +0200 | [diff] [blame] | 144 | #ifdef CONFIG_PM |
Loic Poulain | 8943654 | 2015-09-02 12:04:11 +0200 | [diff] [blame] | 145 | static int intel_wait_lpm_transaction(struct hci_uart *hu) |
| 146 | { |
| 147 | struct intel_data *intel = hu->priv; |
| 148 | int err; |
| 149 | |
| 150 | err = wait_on_bit_timeout(&intel->flags, STATE_LPM_TRANSACTION, |
| 151 | TASK_INTERRUPTIBLE, |
| 152 | msecs_to_jiffies(1000)); |
| 153 | |
| 154 | if (err == 1) { |
| 155 | bt_dev_err(hu->hdev, "LPM transaction interrupted"); |
| 156 | return -EINTR; |
| 157 | } |
| 158 | |
| 159 | if (err) { |
| 160 | bt_dev_err(hu->hdev, "LPM transaction timeout"); |
| 161 | return -ETIMEDOUT; |
| 162 | } |
| 163 | |
| 164 | return err; |
| 165 | } |
| 166 | |
| 167 | static int intel_lpm_suspend(struct hci_uart *hu) |
| 168 | { |
| 169 | static const u8 suspend[] = { 0x01, 0x01, 0x01 }; |
| 170 | struct intel_data *intel = hu->priv; |
| 171 | struct sk_buff *skb; |
| 172 | |
| 173 | if (!test_bit(STATE_LPM_ENABLED, &intel->flags) || |
| 174 | test_bit(STATE_SUSPENDED, &intel->flags)) |
| 175 | return 0; |
| 176 | |
| 177 | if (test_bit(STATE_TX_ACTIVE, &intel->flags)) |
| 178 | return -EAGAIN; |
| 179 | |
| 180 | bt_dev_dbg(hu->hdev, "Suspending"); |
| 181 | |
| 182 | skb = bt_skb_alloc(sizeof(suspend), GFP_KERNEL); |
| 183 | if (!skb) { |
| 184 | bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet"); |
| 185 | return -ENOMEM; |
| 186 | } |
| 187 | |
| 188 | memcpy(skb_put(skb, sizeof(suspend)), suspend, sizeof(suspend)); |
| 189 | bt_cb(skb)->pkt_type = HCI_LPM_PKT; |
| 190 | |
| 191 | set_bit(STATE_LPM_TRANSACTION, &intel->flags); |
| 192 | |
| 193 | skb_queue_tail(&intel->txq, skb); |
| 194 | hci_uart_tx_wakeup(hu); |
| 195 | |
| 196 | intel_wait_lpm_transaction(hu); |
| 197 | /* Even in case of failure, continue and test the suspended flag */ |
| 198 | |
| 199 | clear_bit(STATE_LPM_TRANSACTION, &intel->flags); |
| 200 | |
| 201 | if (!test_bit(STATE_SUSPENDED, &intel->flags)) { |
| 202 | bt_dev_err(hu->hdev, "Device suspend error"); |
| 203 | return -EINVAL; |
| 204 | } |
| 205 | |
| 206 | bt_dev_dbg(hu->hdev, "Suspended"); |
| 207 | |
| 208 | hci_uart_set_flow_control(hu, true); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static int intel_lpm_resume(struct hci_uart *hu) |
| 214 | { |
| 215 | struct intel_data *intel = hu->priv; |
| 216 | struct sk_buff *skb; |
| 217 | |
| 218 | if (!test_bit(STATE_LPM_ENABLED, &intel->flags) || |
| 219 | !test_bit(STATE_SUSPENDED, &intel->flags)) |
| 220 | return 0; |
| 221 | |
| 222 | bt_dev_dbg(hu->hdev, "Resuming"); |
| 223 | |
| 224 | hci_uart_set_flow_control(hu, false); |
| 225 | |
| 226 | skb = bt_skb_alloc(0, GFP_KERNEL); |
| 227 | if (!skb) { |
| 228 | bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet"); |
| 229 | return -ENOMEM; |
| 230 | } |
| 231 | |
| 232 | bt_cb(skb)->pkt_type = HCI_LPM_WAKE_PKT; |
| 233 | |
| 234 | set_bit(STATE_LPM_TRANSACTION, &intel->flags); |
| 235 | |
| 236 | skb_queue_tail(&intel->txq, skb); |
| 237 | hci_uart_tx_wakeup(hu); |
| 238 | |
| 239 | intel_wait_lpm_transaction(hu); |
| 240 | /* Even in case of failure, continue and test the suspended flag */ |
| 241 | |
| 242 | clear_bit(STATE_LPM_TRANSACTION, &intel->flags); |
| 243 | |
| 244 | if (test_bit(STATE_SUSPENDED, &intel->flags)) { |
| 245 | bt_dev_err(hu->hdev, "Device resume error"); |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | bt_dev_dbg(hu->hdev, "Resumed"); |
| 250 | |
| 251 | return 0; |
| 252 | } |
Loic Poulain | a9cb0fe | 2015-09-04 17:39:25 +0200 | [diff] [blame] | 253 | #endif /* CONFIG_PM */ |
Loic Poulain | 8943654 | 2015-09-02 12:04:11 +0200 | [diff] [blame] | 254 | |
| 255 | static int intel_lpm_host_wake(struct hci_uart *hu) |
| 256 | { |
| 257 | static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 }; |
| 258 | struct intel_data *intel = hu->priv; |
| 259 | struct sk_buff *skb; |
| 260 | |
| 261 | hci_uart_set_flow_control(hu, false); |
| 262 | |
| 263 | clear_bit(STATE_SUSPENDED, &intel->flags); |
| 264 | |
| 265 | skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL); |
| 266 | if (!skb) { |
| 267 | bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet"); |
| 268 | return -ENOMEM; |
| 269 | } |
| 270 | |
| 271 | memcpy(skb_put(skb, sizeof(lpm_resume_ack)), lpm_resume_ack, |
| 272 | sizeof(lpm_resume_ack)); |
| 273 | bt_cb(skb)->pkt_type = HCI_LPM_PKT; |
| 274 | |
| 275 | skb_queue_tail(&intel->txq, skb); |
| 276 | hci_uart_tx_wakeup(hu); |
| 277 | |
| 278 | bt_dev_dbg(hu->hdev, "Resumed by controller"); |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Loic Poulain | 765ea3a | 2015-08-29 13:38:18 +0200 | [diff] [blame] | 283 | static irqreturn_t intel_irq(int irq, void *dev_id) |
| 284 | { |
| 285 | struct intel_device *idev = dev_id; |
| 286 | |
| 287 | dev_info(&idev->pdev->dev, "hci_intel irq\n"); |
| 288 | |
Loic Poulain | aa6802d | 2015-09-02 12:04:12 +0200 | [diff] [blame] | 289 | mutex_lock(&idev->hu_lock); |
| 290 | if (idev->hu) |
| 291 | intel_lpm_host_wake(idev->hu); |
| 292 | mutex_unlock(&idev->hu_lock); |
| 293 | |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 294 | /* Host/Controller are now LPM resumed, trigger a new delayed suspend */ |
| 295 | pm_runtime_get(&idev->pdev->dev); |
| 296 | pm_runtime_mark_last_busy(&idev->pdev->dev); |
| 297 | pm_runtime_put_autosuspend(&idev->pdev->dev); |
| 298 | |
Loic Poulain | 765ea3a | 2015-08-29 13:38:18 +0200 | [diff] [blame] | 299 | return IRQ_HANDLED; |
| 300 | } |
| 301 | |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 302 | static int intel_set_power(struct hci_uart *hu, bool powered) |
| 303 | { |
| 304 | struct list_head *p; |
| 305 | int err = -ENODEV; |
| 306 | |
Loic Poulain | 67c8bde | 2015-08-31 18:34:31 +0200 | [diff] [blame] | 307 | mutex_lock(&intel_device_list_lock); |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 308 | |
| 309 | list_for_each(p, &intel_device_list) { |
| 310 | struct intel_device *idev = list_entry(p, struct intel_device, |
| 311 | list); |
| 312 | |
| 313 | /* tty device and pdev device should share the same parent |
| 314 | * which is the UART port. |
| 315 | */ |
| 316 | if (hu->tty->dev->parent != idev->pdev->dev.parent) |
| 317 | continue; |
| 318 | |
| 319 | if (!idev->reset) { |
| 320 | err = -ENOTSUPP; |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | BT_INFO("hu %p, Switching compatible pm device (%s) to %u", |
| 325 | hu, dev_name(&idev->pdev->dev), powered); |
| 326 | |
| 327 | gpiod_set_value(idev->reset, powered); |
Loic Poulain | 765ea3a | 2015-08-29 13:38:18 +0200 | [diff] [blame] | 328 | |
Loic Poulain | aa6802d | 2015-09-02 12:04:12 +0200 | [diff] [blame] | 329 | /* Provide to idev a hu reference which is used to run LPM |
| 330 | * transactions (lpm suspend/resume) from PM callbacks. |
| 331 | * hu needs to be protected against concurrent removing during |
| 332 | * these PM ops. |
| 333 | */ |
| 334 | mutex_lock(&idev->hu_lock); |
| 335 | idev->hu = powered ? hu : NULL; |
| 336 | mutex_unlock(&idev->hu_lock); |
| 337 | |
Loic Poulain | 765ea3a | 2015-08-29 13:38:18 +0200 | [diff] [blame] | 338 | if (idev->irq < 0) |
| 339 | break; |
| 340 | |
| 341 | if (powered && device_can_wakeup(&idev->pdev->dev)) { |
| 342 | err = devm_request_threaded_irq(&idev->pdev->dev, |
| 343 | idev->irq, NULL, |
| 344 | intel_irq, |
| 345 | IRQF_ONESHOT, |
| 346 | "bt-host-wake", idev); |
| 347 | if (err) { |
| 348 | BT_ERR("hu %p, unable to allocate irq-%d", |
| 349 | hu, idev->irq); |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | device_wakeup_enable(&idev->pdev->dev); |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 354 | |
| 355 | pm_runtime_set_active(&idev->pdev->dev); |
| 356 | pm_runtime_use_autosuspend(&idev->pdev->dev); |
| 357 | pm_runtime_set_autosuspend_delay(&idev->pdev->dev, |
| 358 | LPM_SUSPEND_DELAY_MS); |
| 359 | pm_runtime_enable(&idev->pdev->dev); |
Loic Poulain | 765ea3a | 2015-08-29 13:38:18 +0200 | [diff] [blame] | 360 | } else if (!powered && device_may_wakeup(&idev->pdev->dev)) { |
| 361 | devm_free_irq(&idev->pdev->dev, idev->irq, idev); |
| 362 | device_wakeup_disable(&idev->pdev->dev); |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 363 | |
| 364 | pm_runtime_disable(&idev->pdev->dev); |
Loic Poulain | 765ea3a | 2015-08-29 13:38:18 +0200 | [diff] [blame] | 365 | } |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Loic Poulain | 67c8bde | 2015-08-31 18:34:31 +0200 | [diff] [blame] | 368 | mutex_unlock(&intel_device_list_lock); |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 369 | |
| 370 | return err; |
| 371 | } |
| 372 | |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 373 | static void intel_busy_work(struct work_struct *work) |
| 374 | { |
| 375 | struct list_head *p; |
| 376 | struct intel_data *intel = container_of(work, struct intel_data, |
| 377 | busy_work); |
| 378 | |
| 379 | /* Link is busy, delay the suspend */ |
| 380 | mutex_lock(&intel_device_list_lock); |
| 381 | list_for_each(p, &intel_device_list) { |
| 382 | struct intel_device *idev = list_entry(p, struct intel_device, |
| 383 | list); |
| 384 | |
| 385 | if (intel->hu->tty->dev->parent == idev->pdev->dev.parent) { |
| 386 | pm_runtime_get(&idev->pdev->dev); |
| 387 | pm_runtime_mark_last_busy(&idev->pdev->dev); |
| 388 | pm_runtime_put_autosuspend(&idev->pdev->dev); |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | mutex_unlock(&intel_device_list_lock); |
| 393 | } |
| 394 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 395 | static int intel_open(struct hci_uart *hu) |
| 396 | { |
| 397 | struct intel_data *intel; |
| 398 | |
| 399 | BT_DBG("hu %p", hu); |
| 400 | |
| 401 | intel = kzalloc(sizeof(*intel), GFP_KERNEL); |
| 402 | if (!intel) |
| 403 | return -ENOMEM; |
| 404 | |
| 405 | skb_queue_head_init(&intel->txq); |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 406 | INIT_WORK(&intel->busy_work, intel_busy_work); |
| 407 | |
| 408 | intel->hu = hu; |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 409 | |
| 410 | hu->priv = intel; |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 411 | |
| 412 | if (!intel_set_power(hu, true)) |
| 413 | set_bit(STATE_BOOTING, &intel->flags); |
| 414 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static int intel_close(struct hci_uart *hu) |
| 419 | { |
| 420 | struct intel_data *intel = hu->priv; |
| 421 | |
| 422 | BT_DBG("hu %p", hu); |
| 423 | |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 424 | cancel_work_sync(&intel->busy_work); |
| 425 | |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 426 | intel_set_power(hu, false); |
| 427 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 428 | skb_queue_purge(&intel->txq); |
| 429 | kfree_skb(intel->rx_skb); |
| 430 | kfree(intel); |
| 431 | |
| 432 | hu->priv = NULL; |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int intel_flush(struct hci_uart *hu) |
| 437 | { |
| 438 | struct intel_data *intel = hu->priv; |
| 439 | |
| 440 | BT_DBG("hu %p", hu); |
| 441 | |
| 442 | skb_queue_purge(&intel->txq); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode) |
| 448 | { |
| 449 | struct sk_buff *skb; |
| 450 | struct hci_event_hdr *hdr; |
| 451 | struct hci_ev_cmd_complete *evt; |
| 452 | |
| 453 | skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC); |
| 454 | if (!skb) |
| 455 | return -ENOMEM; |
| 456 | |
| 457 | hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr)); |
| 458 | hdr->evt = HCI_EV_CMD_COMPLETE; |
| 459 | hdr->plen = sizeof(*evt) + 1; |
| 460 | |
| 461 | evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt)); |
| 462 | evt->ncmd = 0x01; |
| 463 | evt->opcode = cpu_to_le16(opcode); |
| 464 | |
| 465 | *skb_put(skb, 1) = 0x00; |
| 466 | |
| 467 | bt_cb(skb)->pkt_type = HCI_EVENT_PKT; |
| 468 | |
| 469 | return hci_recv_frame(hdev, skb); |
| 470 | } |
| 471 | |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 472 | static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed) |
| 473 | { |
| 474 | struct intel_data *intel = hu->priv; |
| 475 | struct hci_dev *hdev = hu->hdev; |
| 476 | u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 }; |
| 477 | struct sk_buff *skb; |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 478 | int err; |
| 479 | |
| 480 | /* This can be the first command sent to the chip, check |
| 481 | * that the controller is ready. |
| 482 | */ |
| 483 | err = intel_wait_booting(hu); |
| 484 | |
| 485 | clear_bit(STATE_BOOTING, &intel->flags); |
| 486 | |
| 487 | /* In case of timeout, try to continue anyway */ |
| 488 | if (err && err != ETIMEDOUT) |
| 489 | return err; |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 490 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 491 | bt_dev_info(hdev, "Change controller speed to %d", speed); |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 492 | |
| 493 | speed_cmd[3] = intel_convert_speed(speed); |
| 494 | if (speed_cmd[3] == 0xff) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 495 | bt_dev_err(hdev, "Unsupported speed"); |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 496 | return -EINVAL; |
| 497 | } |
| 498 | |
| 499 | /* Device will not accept speed change if Intel version has not been |
| 500 | * previously requested. |
| 501 | */ |
| 502 | skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT); |
| 503 | if (IS_ERR(skb)) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 504 | bt_dev_err(hdev, "Reading Intel version information failed (%ld)", |
| 505 | PTR_ERR(skb)); |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 506 | return PTR_ERR(skb); |
| 507 | } |
| 508 | kfree_skb(skb); |
| 509 | |
| 510 | skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL); |
| 511 | if (!skb) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 512 | bt_dev_err(hdev, "Failed to alloc memory for baudrate packet"); |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 513 | return -ENOMEM; |
| 514 | } |
| 515 | |
| 516 | memcpy(skb_put(skb, sizeof(speed_cmd)), speed_cmd, sizeof(speed_cmd)); |
| 517 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
| 518 | |
| 519 | hci_uart_set_flow_control(hu, true); |
| 520 | |
| 521 | skb_queue_tail(&intel->txq, skb); |
| 522 | hci_uart_tx_wakeup(hu); |
| 523 | |
| 524 | /* wait 100ms to change baudrate on controller side */ |
| 525 | msleep(100); |
| 526 | |
| 527 | hci_uart_set_baudrate(hu, speed); |
| 528 | hci_uart_set_flow_control(hu, false); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 533 | static int intel_setup(struct hci_uart *hu) |
| 534 | { |
| 535 | static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01, |
| 536 | 0x00, 0x08, 0x04, 0x00 }; |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 537 | static const u8 lpm_param[] = { 0x03, 0x07, 0x01, 0x0b }; |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 538 | struct intel_data *intel = hu->priv; |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 539 | struct intel_device *idev = NULL; |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 540 | struct hci_dev *hdev = hu->hdev; |
| 541 | struct sk_buff *skb; |
| 542 | struct intel_version *ver; |
| 543 | struct intel_boot_params *params; |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 544 | struct list_head *p; |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 545 | const struct firmware *fw; |
| 546 | const u8 *fw_ptr; |
| 547 | char fwname[64]; |
| 548 | u32 frag_len; |
| 549 | ktime_t calltime, delta, rettime; |
| 550 | unsigned long long duration; |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 551 | unsigned int init_speed, oper_speed; |
| 552 | int speed_change = 0; |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 553 | int err; |
| 554 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 555 | bt_dev_dbg(hdev, "start intel_setup"); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 556 | |
Marcel Holtmann | 35ab815 | 2015-07-05 14:37:40 +0200 | [diff] [blame] | 557 | hu->hdev->set_bdaddr = btintel_set_bdaddr; |
| 558 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 559 | calltime = ktime_get(); |
| 560 | |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 561 | if (hu->init_speed) |
| 562 | init_speed = hu->init_speed; |
| 563 | else |
| 564 | init_speed = hu->proto->init_speed; |
| 565 | |
| 566 | if (hu->oper_speed) |
| 567 | oper_speed = hu->oper_speed; |
| 568 | else |
| 569 | oper_speed = hu->proto->oper_speed; |
| 570 | |
| 571 | if (oper_speed && init_speed && oper_speed != init_speed) |
| 572 | speed_change = 1; |
| 573 | |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 574 | /* Check that the controller is ready */ |
| 575 | err = intel_wait_booting(hu); |
| 576 | |
| 577 | clear_bit(STATE_BOOTING, &intel->flags); |
| 578 | |
| 579 | /* In case of timeout, try to continue anyway */ |
| 580 | if (err && err != ETIMEDOUT) |
| 581 | return err; |
| 582 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 583 | set_bit(STATE_BOOTLOADER, &intel->flags); |
| 584 | |
| 585 | /* Read the Intel version information to determine if the device |
| 586 | * is in bootloader mode or if it already has operational firmware |
| 587 | * loaded. |
| 588 | */ |
| 589 | skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT); |
| 590 | if (IS_ERR(skb)) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 591 | bt_dev_err(hdev, "Reading Intel version information failed (%ld)", |
| 592 | PTR_ERR(skb)); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 593 | return PTR_ERR(skb); |
| 594 | } |
| 595 | |
| 596 | if (skb->len != sizeof(*ver)) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 597 | bt_dev_err(hdev, "Intel version event size mismatch"); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 598 | kfree_skb(skb); |
| 599 | return -EILSEQ; |
| 600 | } |
| 601 | |
| 602 | ver = (struct intel_version *)skb->data; |
| 603 | if (ver->status) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 604 | bt_dev_err(hdev, "Intel version command failure (%02x)", |
| 605 | ver->status); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 606 | err = -bt_to_errno(ver->status); |
| 607 | kfree_skb(skb); |
| 608 | return err; |
| 609 | } |
| 610 | |
| 611 | /* The hardware platform number has a fixed value of 0x37 and |
| 612 | * for now only accept this single value. |
| 613 | */ |
| 614 | if (ver->hw_platform != 0x37) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 615 | bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)", |
| 616 | ver->hw_platform); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 617 | kfree_skb(skb); |
| 618 | return -EINVAL; |
| 619 | } |
| 620 | |
| 621 | /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is |
| 622 | * supported by this firmware loading method. This check has been |
| 623 | * put in place to ensure correct forward compatibility options |
| 624 | * when newer hardware variants come along. |
| 625 | */ |
| 626 | if (ver->hw_variant != 0x0b) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 627 | bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)", |
| 628 | ver->hw_variant); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 629 | kfree_skb(skb); |
| 630 | return -EINVAL; |
| 631 | } |
| 632 | |
Marcel Holtmann | 7feb99e | 2015-07-05 15:02:07 +0200 | [diff] [blame] | 633 | btintel_version_info(hdev, ver); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 634 | |
| 635 | /* The firmware variant determines if the device is in bootloader |
| 636 | * mode or is running operational firmware. The value 0x06 identifies |
| 637 | * the bootloader and the value 0x23 identifies the operational |
| 638 | * firmware. |
| 639 | * |
| 640 | * When the operational firmware is already present, then only |
| 641 | * the check for valid Bluetooth device address is needed. This |
| 642 | * determines if the device will be added as configured or |
| 643 | * unconfigured controller. |
| 644 | * |
| 645 | * It is not possible to use the Secure Boot Parameters in this |
| 646 | * case since that command is only available in bootloader mode. |
| 647 | */ |
| 648 | if (ver->fw_variant == 0x23) { |
| 649 | kfree_skb(skb); |
| 650 | clear_bit(STATE_BOOTLOADER, &intel->flags); |
| 651 | btintel_check_bdaddr(hdev); |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | /* If the device is not in bootloader mode, then the only possible |
| 656 | * choice is to return an error and abort the device initialization. |
| 657 | */ |
| 658 | if (ver->fw_variant != 0x06) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 659 | bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)", |
| 660 | ver->fw_variant); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 661 | kfree_skb(skb); |
| 662 | return -ENODEV; |
| 663 | } |
| 664 | |
| 665 | kfree_skb(skb); |
| 666 | |
| 667 | /* Read the secure boot parameters to identify the operating |
| 668 | * details of the bootloader. |
| 669 | */ |
| 670 | skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT); |
| 671 | if (IS_ERR(skb)) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 672 | bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)", |
| 673 | PTR_ERR(skb)); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 674 | return PTR_ERR(skb); |
| 675 | } |
| 676 | |
| 677 | if (skb->len != sizeof(*params)) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 678 | bt_dev_err(hdev, "Intel boot parameters size mismatch"); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 679 | kfree_skb(skb); |
| 680 | return -EILSEQ; |
| 681 | } |
| 682 | |
| 683 | params = (struct intel_boot_params *)skb->data; |
| 684 | if (params->status) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 685 | bt_dev_err(hdev, "Intel boot parameters command failure (%02x)", |
| 686 | params->status); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 687 | err = -bt_to_errno(params->status); |
| 688 | kfree_skb(skb); |
| 689 | return err; |
| 690 | } |
| 691 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 692 | bt_dev_info(hdev, "Device revision is %u", |
| 693 | le16_to_cpu(params->dev_revid)); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 694 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 695 | bt_dev_info(hdev, "Secure boot is %s", |
| 696 | params->secure_boot ? "enabled" : "disabled"); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 697 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 698 | bt_dev_info(hdev, "Minimum firmware build %u week %u %u", |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 699 | params->min_fw_build_nn, params->min_fw_build_cw, |
| 700 | 2000 + params->min_fw_build_yy); |
| 701 | |
| 702 | /* It is required that every single firmware fragment is acknowledged |
| 703 | * with a command complete event. If the boot parameters indicate |
| 704 | * that this bootloader does not send them, then abort the setup. |
| 705 | */ |
| 706 | if (params->limited_cce != 0x00) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 707 | bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)", |
| 708 | params->limited_cce); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 709 | kfree_skb(skb); |
| 710 | return -EINVAL; |
| 711 | } |
| 712 | |
| 713 | /* If the OTP has no valid Bluetooth device address, then there will |
| 714 | * also be no valid address for the operational firmware. |
| 715 | */ |
| 716 | if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 717 | bt_dev_info(hdev, "No device address configured"); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 718 | set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks); |
| 719 | } |
| 720 | |
| 721 | /* With this Intel bootloader only the hardware variant and device |
| 722 | * revision information are used to select the right firmware. |
| 723 | * |
| 724 | * Currently this bootloader support is limited to hardware variant |
| 725 | * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b). |
| 726 | */ |
| 727 | snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi", |
| 728 | le16_to_cpu(params->dev_revid)); |
| 729 | |
| 730 | err = request_firmware(&fw, fwname, &hdev->dev); |
| 731 | if (err < 0) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 732 | bt_dev_err(hdev, "Failed to load Intel firmware file (%d)", |
| 733 | err); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 734 | kfree_skb(skb); |
| 735 | return err; |
| 736 | } |
| 737 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 738 | bt_dev_info(hdev, "Found device firmware: %s", fwname); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 739 | |
Loic Poulain | 1cfbabd | 2015-09-04 17:54:35 +0200 | [diff] [blame^] | 740 | /* Save the DDC file name for later */ |
| 741 | snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.ddc", |
| 742 | le16_to_cpu(params->dev_revid)); |
| 743 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 744 | kfree_skb(skb); |
| 745 | |
| 746 | if (fw->size < 644) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 747 | bt_dev_err(hdev, "Invalid size of firmware file (%zu)", |
| 748 | fw->size); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 749 | err = -EBADF; |
| 750 | goto done; |
| 751 | } |
| 752 | |
| 753 | set_bit(STATE_DOWNLOADING, &intel->flags); |
| 754 | |
| 755 | /* Start the firmware download transaction with the Init fragment |
| 756 | * represented by the 128 bytes of CSS header. |
| 757 | */ |
Marcel Holtmann | 09df123 | 2015-07-05 14:55:36 +0200 | [diff] [blame] | 758 | err = btintel_secure_send(hdev, 0x00, 128, fw->data); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 759 | if (err < 0) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 760 | bt_dev_err(hdev, "Failed to send firmware header (%d)", err); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 761 | goto done; |
| 762 | } |
| 763 | |
| 764 | /* Send the 256 bytes of public key information from the firmware |
| 765 | * as the PKey fragment. |
| 766 | */ |
Marcel Holtmann | 09df123 | 2015-07-05 14:55:36 +0200 | [diff] [blame] | 767 | err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 768 | if (err < 0) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 769 | bt_dev_err(hdev, "Failed to send firmware public key (%d)", |
| 770 | err); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 771 | goto done; |
| 772 | } |
| 773 | |
| 774 | /* Send the 256 bytes of signature information from the firmware |
| 775 | * as the Sign fragment. |
| 776 | */ |
Marcel Holtmann | 09df123 | 2015-07-05 14:55:36 +0200 | [diff] [blame] | 777 | err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 778 | if (err < 0) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 779 | bt_dev_err(hdev, "Failed to send firmware signature (%d)", |
| 780 | err); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 781 | goto done; |
| 782 | } |
| 783 | |
| 784 | fw_ptr = fw->data + 644; |
| 785 | frag_len = 0; |
| 786 | |
| 787 | while (fw_ptr - fw->data < fw->size) { |
| 788 | struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len); |
| 789 | |
| 790 | frag_len += sizeof(*cmd) + cmd->plen; |
| 791 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 792 | bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data), |
| 793 | fw->size); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 794 | |
| 795 | /* The parameter length of the secure send command requires |
| 796 | * a 4 byte alignment. It happens so that the firmware file |
| 797 | * contains proper Intel_NOP commands to align the fragments |
| 798 | * as needed. |
| 799 | * |
| 800 | * Send set of commands with 4 byte alignment from the |
| 801 | * firmware data buffer as a single Data fragement. |
| 802 | */ |
| 803 | if (frag_len % 4) |
| 804 | continue; |
| 805 | |
| 806 | /* Send each command from the firmware data buffer as |
| 807 | * a single Data fragment. |
| 808 | */ |
Marcel Holtmann | 09df123 | 2015-07-05 14:55:36 +0200 | [diff] [blame] | 809 | err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 810 | if (err < 0) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 811 | bt_dev_err(hdev, "Failed to send firmware data (%d)", |
| 812 | err); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 813 | goto done; |
| 814 | } |
| 815 | |
| 816 | fw_ptr += frag_len; |
| 817 | frag_len = 0; |
| 818 | } |
| 819 | |
| 820 | set_bit(STATE_FIRMWARE_LOADED, &intel->flags); |
| 821 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 822 | bt_dev_info(hdev, "Waiting for firmware download to complete"); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 823 | |
| 824 | /* Before switching the device into operational mode and with that |
| 825 | * booting the loaded firmware, wait for the bootloader notification |
| 826 | * that all fragments have been successfully received. |
| 827 | * |
| 828 | * When the event processing receives the notification, then the |
| 829 | * STATE_DOWNLOADING flag will be cleared. |
| 830 | * |
| 831 | * The firmware loading should not take longer than 5 seconds |
| 832 | * and thus just timeout if that happens and fail the setup |
| 833 | * of this device. |
| 834 | */ |
| 835 | err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING, |
| 836 | TASK_INTERRUPTIBLE, |
| 837 | msecs_to_jiffies(5000)); |
| 838 | if (err == 1) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 839 | bt_dev_err(hdev, "Firmware loading interrupted"); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 840 | err = -EINTR; |
| 841 | goto done; |
| 842 | } |
| 843 | |
| 844 | if (err) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 845 | bt_dev_err(hdev, "Firmware loading timeout"); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 846 | err = -ETIMEDOUT; |
| 847 | goto done; |
| 848 | } |
| 849 | |
| 850 | if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 851 | bt_dev_err(hdev, "Firmware loading failed"); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 852 | err = -ENOEXEC; |
| 853 | goto done; |
| 854 | } |
| 855 | |
| 856 | rettime = ktime_get(); |
| 857 | delta = ktime_sub(rettime, calltime); |
| 858 | duration = (unsigned long long) ktime_to_ns(delta) >> 10; |
| 859 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 860 | bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 861 | |
| 862 | done: |
| 863 | release_firmware(fw); |
| 864 | |
| 865 | if (err < 0) |
| 866 | return err; |
| 867 | |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 868 | /* We need to restore the default speed before Intel reset */ |
| 869 | if (speed_change) { |
| 870 | err = intel_set_baudrate(hu, init_speed); |
| 871 | if (err) |
| 872 | return err; |
| 873 | } |
| 874 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 875 | calltime = ktime_get(); |
| 876 | |
| 877 | set_bit(STATE_BOOTING, &intel->flags); |
| 878 | |
| 879 | skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param, |
| 880 | HCI_INIT_TIMEOUT); |
| 881 | if (IS_ERR(skb)) |
| 882 | return PTR_ERR(skb); |
| 883 | |
| 884 | kfree_skb(skb); |
| 885 | |
| 886 | /* The bootloader will not indicate when the device is ready. This |
| 887 | * is done by the operational firmware sending bootup notification. |
| 888 | * |
| 889 | * Booting into operational firmware should not take longer than |
| 890 | * 1 second. However if that happens, then just fail the setup |
| 891 | * since something went wrong. |
| 892 | */ |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 893 | bt_dev_info(hdev, "Waiting for device to boot"); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 894 | |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 895 | err = intel_wait_booting(hu); |
| 896 | if (err) |
| 897 | return err; |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 898 | |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 899 | clear_bit(STATE_BOOTING, &intel->flags); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 900 | |
| 901 | rettime = ktime_get(); |
| 902 | delta = ktime_sub(rettime, calltime); |
| 903 | duration = (unsigned long long) ktime_to_ns(delta) >> 10; |
| 904 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 905 | bt_dev_info(hdev, "Device booted in %llu usecs", duration); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 906 | |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 907 | /* Enable LPM if matching pdev with wakeup enabled */ |
Loic Poulain | 67c8bde | 2015-08-31 18:34:31 +0200 | [diff] [blame] | 908 | mutex_lock(&intel_device_list_lock); |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 909 | list_for_each(p, &intel_device_list) { |
| 910 | struct intel_device *dev = list_entry(p, struct intel_device, |
| 911 | list); |
| 912 | if (hu->tty->dev->parent == dev->pdev->dev.parent) { |
| 913 | if (device_may_wakeup(&dev->pdev->dev)) |
| 914 | idev = dev; |
| 915 | break; |
| 916 | } |
| 917 | } |
Loic Poulain | 67c8bde | 2015-08-31 18:34:31 +0200 | [diff] [blame] | 918 | mutex_unlock(&intel_device_list_lock); |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 919 | |
| 920 | if (!idev) |
| 921 | goto no_lpm; |
| 922 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 923 | bt_dev_info(hdev, "Enabling LPM"); |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 924 | |
| 925 | skb = __hci_cmd_sync(hdev, 0xfc8b, sizeof(lpm_param), lpm_param, |
| 926 | HCI_CMD_TIMEOUT); |
| 927 | if (IS_ERR(skb)) { |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 928 | bt_dev_err(hdev, "Failed to enable LPM"); |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 929 | goto no_lpm; |
| 930 | } |
| 931 | kfree_skb(skb); |
| 932 | |
| 933 | set_bit(STATE_LPM_ENABLED, &intel->flags); |
| 934 | |
| 935 | no_lpm: |
Loic Poulain | 1cfbabd | 2015-09-04 17:54:35 +0200 | [diff] [blame^] | 936 | /* Ignore errors, device can work without DDC parameters */ |
| 937 | btintel_load_ddc_config(hdev, fwname); |
| 938 | |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 939 | skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT); |
| 940 | if (IS_ERR(skb)) |
| 941 | return PTR_ERR(skb); |
| 942 | kfree_skb(skb); |
| 943 | |
| 944 | if (speed_change) { |
| 945 | err = intel_set_baudrate(hu, oper_speed); |
| 946 | if (err) |
| 947 | return err; |
| 948 | } |
| 949 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 950 | bt_dev_info(hdev, "Setup complete"); |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 951 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 952 | clear_bit(STATE_BOOTLOADER, &intel->flags); |
| 953 | |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb) |
| 958 | { |
| 959 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 960 | struct intel_data *intel = hu->priv; |
| 961 | struct hci_event_hdr *hdr; |
| 962 | |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 963 | if (!test_bit(STATE_BOOTLOADER, &intel->flags) && |
| 964 | !test_bit(STATE_BOOTING, &intel->flags)) |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 965 | goto recv; |
| 966 | |
| 967 | hdr = (void *)skb->data; |
| 968 | |
| 969 | /* When the firmware loading completes the device sends |
| 970 | * out a vendor specific event indicating the result of |
| 971 | * the firmware loading. |
| 972 | */ |
| 973 | if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 && |
| 974 | skb->data[2] == 0x06) { |
| 975 | if (skb->data[3] != 0x00) |
| 976 | set_bit(STATE_FIRMWARE_FAILED, &intel->flags); |
| 977 | |
| 978 | if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) && |
| 979 | test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) { |
| 980 | smp_mb__after_atomic(); |
| 981 | wake_up_bit(&intel->flags, STATE_DOWNLOADING); |
| 982 | } |
| 983 | |
| 984 | /* When switching to the operational firmware the device |
| 985 | * sends a vendor specific event indicating that the bootup |
| 986 | * completed. |
| 987 | */ |
| 988 | } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 && |
| 989 | skb->data[2] == 0x02) { |
| 990 | if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) { |
| 991 | smp_mb__after_atomic(); |
| 992 | wake_up_bit(&intel->flags, STATE_BOOTING); |
| 993 | } |
| 994 | } |
| 995 | recv: |
| 996 | return hci_recv_frame(hdev, skb); |
| 997 | } |
| 998 | |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 999 | static void intel_recv_lpm_notify(struct hci_dev *hdev, int value) |
| 1000 | { |
| 1001 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1002 | struct intel_data *intel = hu->priv; |
| 1003 | |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 1004 | bt_dev_dbg(hdev, "TX idle notification (%d)", value); |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 1005 | |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 1006 | if (value) { |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 1007 | set_bit(STATE_TX_ACTIVE, &intel->flags); |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 1008 | schedule_work(&intel->busy_work); |
| 1009 | } else { |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 1010 | clear_bit(STATE_TX_ACTIVE, &intel->flags); |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 1011 | } |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb) |
| 1015 | { |
| 1016 | struct hci_lpm_pkt *lpm = (void *)skb->data; |
Loic Poulain | 8943654 | 2015-09-02 12:04:11 +0200 | [diff] [blame] | 1017 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 1018 | struct intel_data *intel = hu->priv; |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 1019 | |
| 1020 | switch (lpm->opcode) { |
| 1021 | case LPM_OP_TX_NOTIFY: |
Loic Poulain | 1b19757 | 2015-09-02 12:04:14 +0200 | [diff] [blame] | 1022 | if (lpm->dlen < 1) { |
| 1023 | bt_dev_err(hu->hdev, "Invalid LPM notification packet"); |
| 1024 | break; |
| 1025 | } |
| 1026 | intel_recv_lpm_notify(hdev, lpm->data[0]); |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 1027 | break; |
Loic Poulain | 8943654 | 2015-09-02 12:04:11 +0200 | [diff] [blame] | 1028 | case LPM_OP_SUSPEND_ACK: |
| 1029 | set_bit(STATE_SUSPENDED, &intel->flags); |
| 1030 | if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) { |
| 1031 | smp_mb__after_atomic(); |
| 1032 | wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION); |
| 1033 | } |
| 1034 | break; |
| 1035 | case LPM_OP_RESUME_ACK: |
| 1036 | clear_bit(STATE_SUSPENDED, &intel->flags); |
| 1037 | if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) { |
| 1038 | smp_mb__after_atomic(); |
| 1039 | wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION); |
| 1040 | } |
| 1041 | break; |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 1042 | default: |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 1043 | bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode); |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 1044 | break; |
| 1045 | } |
| 1046 | |
| 1047 | kfree_skb(skb); |
| 1048 | |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | #define INTEL_RECV_LPM \ |
| 1053 | .type = HCI_LPM_PKT, \ |
| 1054 | .hlen = HCI_LPM_HDR_SIZE, \ |
| 1055 | .loff = 1, \ |
| 1056 | .lsize = 1, \ |
| 1057 | .maxlen = HCI_LPM_MAX_SIZE |
| 1058 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 1059 | static const struct h4_recv_pkt intel_recv_pkts[] = { |
Loic Poulain | b98469f | 2015-08-29 13:38:19 +0200 | [diff] [blame] | 1060 | { H4_RECV_ACL, .recv = hci_recv_frame }, |
| 1061 | { H4_RECV_SCO, .recv = hci_recv_frame }, |
| 1062 | { H4_RECV_EVENT, .recv = intel_recv_event }, |
| 1063 | { INTEL_RECV_LPM, .recv = intel_recv_lpm }, |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 1064 | }; |
| 1065 | |
| 1066 | static int intel_recv(struct hci_uart *hu, const void *data, int count) |
| 1067 | { |
| 1068 | struct intel_data *intel = hu->priv; |
| 1069 | |
| 1070 | if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) |
| 1071 | return -EUNATCH; |
| 1072 | |
| 1073 | intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count, |
| 1074 | intel_recv_pkts, |
| 1075 | ARRAY_SIZE(intel_recv_pkts)); |
| 1076 | if (IS_ERR(intel->rx_skb)) { |
| 1077 | int err = PTR_ERR(intel->rx_skb); |
Loic Poulain | f44e78a | 2015-08-31 18:34:30 +0200 | [diff] [blame] | 1078 | bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 1079 | intel->rx_skb = NULL; |
| 1080 | return err; |
| 1081 | } |
| 1082 | |
| 1083 | return count; |
| 1084 | } |
| 1085 | |
| 1086 | static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
| 1087 | { |
| 1088 | struct intel_data *intel = hu->priv; |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 1089 | struct list_head *p; |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 1090 | |
| 1091 | BT_DBG("hu %p skb %p", hu, skb); |
| 1092 | |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 1093 | /* Be sure our controller is resumed and potential LPM transaction |
| 1094 | * completed before enqueuing any packet. |
| 1095 | */ |
| 1096 | mutex_lock(&intel_device_list_lock); |
| 1097 | list_for_each(p, &intel_device_list) { |
| 1098 | struct intel_device *idev = list_entry(p, struct intel_device, |
| 1099 | list); |
| 1100 | |
| 1101 | if (hu->tty->dev->parent == idev->pdev->dev.parent) { |
| 1102 | pm_runtime_get_sync(&idev->pdev->dev); |
| 1103 | pm_runtime_mark_last_busy(&idev->pdev->dev); |
| 1104 | pm_runtime_put_autosuspend(&idev->pdev->dev); |
| 1105 | break; |
| 1106 | } |
| 1107 | } |
| 1108 | mutex_unlock(&intel_device_list_lock); |
| 1109 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 1110 | skb_queue_tail(&intel->txq, skb); |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | static struct sk_buff *intel_dequeue(struct hci_uart *hu) |
| 1116 | { |
| 1117 | struct intel_data *intel = hu->priv; |
| 1118 | struct sk_buff *skb; |
| 1119 | |
| 1120 | skb = skb_dequeue(&intel->txq); |
| 1121 | if (!skb) |
| 1122 | return skb; |
| 1123 | |
| 1124 | if (test_bit(STATE_BOOTLOADER, &intel->flags) && |
| 1125 | (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) { |
| 1126 | struct hci_command_hdr *cmd = (void *)skb->data; |
| 1127 | __u16 opcode = le16_to_cpu(cmd->opcode); |
| 1128 | |
| 1129 | /* When the 0xfc01 command is issued to boot into |
| 1130 | * the operational firmware, it will actually not |
| 1131 | * send a command complete event. To keep the flow |
| 1132 | * control working inject that event here. |
| 1133 | */ |
| 1134 | if (opcode == 0xfc01) |
| 1135 | inject_cmd_complete(hu->hdev, opcode); |
| 1136 | } |
| 1137 | |
| 1138 | /* Prepend skb with frame type */ |
| 1139 | memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); |
| 1140 | |
| 1141 | return skb; |
| 1142 | } |
| 1143 | |
| 1144 | static const struct hci_uart_proto intel_proto = { |
| 1145 | .id = HCI_UART_INTEL, |
| 1146 | .name = "Intel", |
| 1147 | .init_speed = 115200, |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 1148 | .oper_speed = 3000000, |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 1149 | .open = intel_open, |
| 1150 | .close = intel_close, |
| 1151 | .flush = intel_flush, |
| 1152 | .setup = intel_setup, |
Loic Poulain | ff28955 | 2015-08-25 17:55:44 +0200 | [diff] [blame] | 1153 | .set_baudrate = intel_set_baudrate, |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 1154 | .recv = intel_recv, |
| 1155 | .enqueue = intel_enqueue, |
| 1156 | .dequeue = intel_dequeue, |
| 1157 | }; |
| 1158 | |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1159 | #ifdef CONFIG_ACPI |
| 1160 | static const struct acpi_device_id intel_acpi_match[] = { |
| 1161 | { "INT33E1", 0 }, |
| 1162 | { }, |
| 1163 | }; |
| 1164 | MODULE_DEVICE_TABLE(acpi, intel_acpi_match); |
| 1165 | |
| 1166 | static int intel_acpi_probe(struct intel_device *idev) |
| 1167 | { |
| 1168 | const struct acpi_device_id *id; |
| 1169 | |
| 1170 | id = acpi_match_device(intel_acpi_match, &idev->pdev->dev); |
| 1171 | if (!id) |
| 1172 | return -ENODEV; |
| 1173 | |
| 1174 | return 0; |
| 1175 | } |
| 1176 | #else |
| 1177 | static int intel_acpi_probe(struct intel_device *idev) |
| 1178 | { |
| 1179 | return -ENODEV; |
| 1180 | } |
| 1181 | #endif |
| 1182 | |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 1183 | #ifdef CONFIG_PM |
Loic Poulain | aa6802d | 2015-09-02 12:04:12 +0200 | [diff] [blame] | 1184 | static int intel_suspend(struct device *dev) |
| 1185 | { |
| 1186 | struct intel_device *idev = dev_get_drvdata(dev); |
| 1187 | |
| 1188 | dev_dbg(dev, "intel_suspend"); |
| 1189 | |
| 1190 | mutex_lock(&idev->hu_lock); |
| 1191 | if (idev->hu) |
| 1192 | intel_lpm_suspend(idev->hu); |
| 1193 | mutex_unlock(&idev->hu_lock); |
| 1194 | |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | static int intel_resume(struct device *dev) |
| 1199 | { |
| 1200 | struct intel_device *idev = dev_get_drvdata(dev); |
| 1201 | |
| 1202 | dev_dbg(dev, "intel_resume"); |
| 1203 | |
| 1204 | mutex_lock(&idev->hu_lock); |
| 1205 | if (idev->hu) |
| 1206 | intel_lpm_resume(idev->hu); |
| 1207 | mutex_unlock(&idev->hu_lock); |
| 1208 | |
| 1209 | return 0; |
| 1210 | } |
| 1211 | #endif |
| 1212 | |
| 1213 | static const struct dev_pm_ops intel_pm_ops = { |
| 1214 | SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume) |
Loic Poulain | 74cdad3 | 2015-09-02 12:04:13 +0200 | [diff] [blame] | 1215 | SET_RUNTIME_PM_OPS(intel_suspend, intel_resume, NULL) |
Loic Poulain | aa6802d | 2015-09-02 12:04:12 +0200 | [diff] [blame] | 1216 | }; |
| 1217 | |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1218 | static int intel_probe(struct platform_device *pdev) |
| 1219 | { |
| 1220 | struct intel_device *idev; |
| 1221 | |
| 1222 | idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL); |
| 1223 | if (!idev) |
| 1224 | return -ENOMEM; |
| 1225 | |
Loic Poulain | aa6802d | 2015-09-02 12:04:12 +0200 | [diff] [blame] | 1226 | mutex_init(&idev->hu_lock); |
| 1227 | |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1228 | idev->pdev = pdev; |
| 1229 | |
| 1230 | if (ACPI_HANDLE(&pdev->dev)) { |
| 1231 | int err = intel_acpi_probe(idev); |
| 1232 | if (err) |
| 1233 | return err; |
| 1234 | } else { |
| 1235 | return -ENODEV; |
| 1236 | } |
| 1237 | |
| 1238 | idev->reset = devm_gpiod_get_optional(&pdev->dev, "reset", |
| 1239 | GPIOD_OUT_LOW); |
| 1240 | if (IS_ERR(idev->reset)) { |
| 1241 | dev_err(&pdev->dev, "Unable to retrieve gpio\n"); |
| 1242 | return PTR_ERR(idev->reset); |
| 1243 | } |
| 1244 | |
Loic Poulain | 765ea3a | 2015-08-29 13:38:18 +0200 | [diff] [blame] | 1245 | idev->irq = platform_get_irq(pdev, 0); |
| 1246 | if (idev->irq < 0) { |
| 1247 | struct gpio_desc *host_wake; |
| 1248 | |
| 1249 | dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n"); |
| 1250 | |
| 1251 | host_wake = devm_gpiod_get_optional(&pdev->dev, "host-wake", |
| 1252 | GPIOD_IN); |
| 1253 | if (IS_ERR(host_wake)) { |
| 1254 | dev_err(&pdev->dev, "Unable to retrieve IRQ\n"); |
| 1255 | goto no_irq; |
| 1256 | } |
| 1257 | |
| 1258 | idev->irq = gpiod_to_irq(host_wake); |
| 1259 | if (idev->irq < 0) { |
| 1260 | dev_err(&pdev->dev, "No corresponding irq for gpio\n"); |
| 1261 | goto no_irq; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | /* Only enable wake-up/irq when controller is powered */ |
| 1266 | device_set_wakeup_capable(&pdev->dev, true); |
| 1267 | device_wakeup_disable(&pdev->dev); |
| 1268 | |
| 1269 | no_irq: |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1270 | platform_set_drvdata(pdev, idev); |
| 1271 | |
| 1272 | /* Place this instance on the device list */ |
Loic Poulain | 67c8bde | 2015-08-31 18:34:31 +0200 | [diff] [blame] | 1273 | mutex_lock(&intel_device_list_lock); |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1274 | list_add_tail(&idev->list, &intel_device_list); |
Loic Poulain | 67c8bde | 2015-08-31 18:34:31 +0200 | [diff] [blame] | 1275 | mutex_unlock(&intel_device_list_lock); |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1276 | |
Loic Poulain | 765ea3a | 2015-08-29 13:38:18 +0200 | [diff] [blame] | 1277 | dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n", |
| 1278 | desc_to_gpio(idev->reset), idev->irq); |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1279 | |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | static int intel_remove(struct platform_device *pdev) |
| 1284 | { |
| 1285 | struct intel_device *idev = platform_get_drvdata(pdev); |
| 1286 | |
Loic Poulain | 765ea3a | 2015-08-29 13:38:18 +0200 | [diff] [blame] | 1287 | device_wakeup_disable(&pdev->dev); |
| 1288 | |
Loic Poulain | 67c8bde | 2015-08-31 18:34:31 +0200 | [diff] [blame] | 1289 | mutex_lock(&intel_device_list_lock); |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1290 | list_del(&idev->list); |
Loic Poulain | 67c8bde | 2015-08-31 18:34:31 +0200 | [diff] [blame] | 1291 | mutex_unlock(&intel_device_list_lock); |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1292 | |
| 1293 | dev_info(&pdev->dev, "unregistered.\n"); |
| 1294 | |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | static struct platform_driver intel_driver = { |
| 1299 | .probe = intel_probe, |
| 1300 | .remove = intel_remove, |
| 1301 | .driver = { |
| 1302 | .name = "hci_intel", |
| 1303 | .acpi_match_table = ACPI_PTR(intel_acpi_match), |
Loic Poulain | aa6802d | 2015-09-02 12:04:12 +0200 | [diff] [blame] | 1304 | .pm = &intel_pm_ops, |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1305 | }, |
| 1306 | }; |
| 1307 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 1308 | int __init intel_init(void) |
| 1309 | { |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1310 | platform_driver_register(&intel_driver); |
| 1311 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 1312 | return hci_uart_register_proto(&intel_proto); |
| 1313 | } |
| 1314 | |
| 1315 | int __exit intel_deinit(void) |
| 1316 | { |
Loic Poulain | 1ab1f23 | 2015-08-27 07:21:51 +0200 | [diff] [blame] | 1317 | platform_driver_unregister(&intel_driver); |
| 1318 | |
Loic Poulain | ca93cee | 2015-07-01 12:20:26 +0200 | [diff] [blame] | 1319 | return hci_uart_unregister_proto(&intel_proto); |
| 1320 | } |