blob: 215e8e9dd65f664174e728bef4a91c46a826f1a4 [file] [log] [blame]
Marcel Holtmann16e38872015-04-04 16:13:02 -07001/*
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 Poulainca93cee2015-07-01 12:20:26 +020027#include <linux/firmware.h>
Loic Poulain1ab1f232015-08-27 07:21:51 +020028#include <linux/module.h>
Loic Poulainca93cee2015-07-01 12:20:26 +020029#include <linux/wait.h>
Loic Poulain1ab1f232015-08-27 07:21:51 +020030#include <linux/tty.h>
31#include <linux/platform_device.h>
32#include <linux/gpio/consumer.h>
33#include <linux/acpi.h>
Loic Poulain765ea3a2015-08-29 13:38:18 +020034#include <linux/interrupt.h>
Loic Poulain74cdad32015-09-02 12:04:13 +020035#include <linux/pm_runtime.h>
Marcel Holtmann16e38872015-04-04 16:13:02 -070036
37#include <net/bluetooth/bluetooth.h>
38#include <net/bluetooth/hci_core.h>
39
40#include "hci_uart.h"
Loic Poulainca93cee2015-07-01 12:20:26 +020041#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 Poulainb98469f2015-08-29 13:38:19 +020048#define STATE_LPM_ENABLED 5
49#define STATE_TX_ACTIVE 6
Loic Poulain89436542015-09-02 12:04:11 +020050#define STATE_SUSPENDED 7
51#define STATE_LPM_TRANSACTION 8
Loic Poulainb98469f2015-08-29 13:38:19 +020052
Loic Poulain89436542015-09-02 12:04:11 +020053#define HCI_LPM_WAKE_PKT 0xf0
Loic Poulainb98469f2015-08-29 13:38:19 +020054#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 Poulain89436542015-09-02 12:04:11 +020059#define LPM_OP_SUSPEND_ACK 0x02
60#define LPM_OP_RESUME_ACK 0x03
Loic Poulainb98469f2015-08-29 13:38:19 +020061
Loic Poulain74cdad32015-09-02 12:04:13 +020062#define LPM_SUSPEND_DELAY_MS 1000
63
Loic Poulainb98469f2015-08-29 13:38:19 +020064struct hci_lpm_pkt {
65 __u8 opcode;
66 __u8 dlen;
67 __u8 data[0];
68} __packed;
Loic Poulainca93cee2015-07-01 12:20:26 +020069
Loic Poulain1ab1f232015-08-27 07:21:51 +020070struct intel_device {
71 struct list_head list;
72 struct platform_device *pdev;
73 struct gpio_desc *reset;
Loic Poulainaa6802d2015-09-02 12:04:12 +020074 struct hci_uart *hu;
75 struct mutex hu_lock;
Loic Poulain765ea3a2015-08-29 13:38:18 +020076 int irq;
Loic Poulain1ab1f232015-08-27 07:21:51 +020077};
78
79static LIST_HEAD(intel_device_list);
Loic Poulain67c8bde2015-08-31 18:34:31 +020080static DEFINE_MUTEX(intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +020081
Loic Poulainca93cee2015-07-01 12:20:26 +020082struct intel_data {
83 struct sk_buff *rx_skb;
84 struct sk_buff_head txq;
Loic Poulain74cdad32015-09-02 12:04:13 +020085 struct work_struct busy_work;
86 struct hci_uart *hu;
Loic Poulainca93cee2015-07-01 12:20:26 +020087 unsigned long flags;
88};
89
Loic Poulainff289552015-08-25 17:55:44 +020090static 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 Poulain1ab1f232015-08-27 07:21:51 +0200122static 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 Poulainf44e78a2015-08-31 18:34:30 +0200132 bt_dev_err(hu->hdev, "Device boot interrupted");
Loic Poulain1ab1f232015-08-27 07:21:51 +0200133 return -EINTR;
134 }
135
136 if (err) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200137 bt_dev_err(hu->hdev, "Device boot timeout");
Loic Poulain1ab1f232015-08-27 07:21:51 +0200138 return -ETIMEDOUT;
139 }
140
141 return err;
142}
143
Loic Poulaina9cb0fe2015-09-04 17:39:25 +0200144#ifdef CONFIG_PM
Loic Poulain89436542015-09-02 12:04:11 +0200145static 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
167static 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
213static 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 Poulaina9cb0fe2015-09-04 17:39:25 +0200253#endif /* CONFIG_PM */
Loic Poulain89436542015-09-02 12:04:11 +0200254
255static 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 Poulain765ea3a2015-08-29 13:38:18 +0200283static 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 Poulainaa6802d2015-09-02 12:04:12 +0200289 mutex_lock(&idev->hu_lock);
290 if (idev->hu)
291 intel_lpm_host_wake(idev->hu);
292 mutex_unlock(&idev->hu_lock);
293
Loic Poulain74cdad32015-09-02 12:04:13 +0200294 /* 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 Poulain765ea3a2015-08-29 13:38:18 +0200299 return IRQ_HANDLED;
300}
301
Loic Poulain1ab1f232015-08-27 07:21:51 +0200302static int intel_set_power(struct hci_uart *hu, bool powered)
303{
304 struct list_head *p;
305 int err = -ENODEV;
306
Loic Poulain67c8bde2015-08-31 18:34:31 +0200307 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200308
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 Poulain765ea3a2015-08-29 13:38:18 +0200328
Loic Poulainaa6802d2015-09-02 12:04:12 +0200329 /* 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 Poulain765ea3a2015-08-29 13:38:18 +0200338 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 Poulain74cdad32015-09-02 12:04:13 +0200354
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 Poulain765ea3a2015-08-29 13:38:18 +0200360 } 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 Poulain74cdad32015-09-02 12:04:13 +0200363
364 pm_runtime_disable(&idev->pdev->dev);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200365 }
Loic Poulain1ab1f232015-08-27 07:21:51 +0200366 }
367
Loic Poulain67c8bde2015-08-31 18:34:31 +0200368 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200369
370 return err;
371}
372
Loic Poulain74cdad32015-09-02 12:04:13 +0200373static 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 Poulainca93cee2015-07-01 12:20:26 +0200395static 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 Poulain74cdad32015-09-02 12:04:13 +0200406 INIT_WORK(&intel->busy_work, intel_busy_work);
407
408 intel->hu = hu;
Loic Poulainca93cee2015-07-01 12:20:26 +0200409
410 hu->priv = intel;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200411
412 if (!intel_set_power(hu, true))
413 set_bit(STATE_BOOTING, &intel->flags);
414
Loic Poulainca93cee2015-07-01 12:20:26 +0200415 return 0;
416}
417
418static int intel_close(struct hci_uart *hu)
419{
420 struct intel_data *intel = hu->priv;
421
422 BT_DBG("hu %p", hu);
423
Loic Poulain74cdad32015-09-02 12:04:13 +0200424 cancel_work_sync(&intel->busy_work);
425
Loic Poulain1ab1f232015-08-27 07:21:51 +0200426 intel_set_power(hu, false);
427
Loic Poulainca93cee2015-07-01 12:20:26 +0200428 skb_queue_purge(&intel->txq);
429 kfree_skb(intel->rx_skb);
430 kfree(intel);
431
432 hu->priv = NULL;
433 return 0;
434}
435
436static 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
447static 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 Poulainff289552015-08-25 17:55:44 +0200472static 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 Poulain1ab1f232015-08-27 07:21:51 +0200478 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 Poulainff289552015-08-25 17:55:44 +0200490
Loic Poulainf44e78a2015-08-31 18:34:30 +0200491 bt_dev_info(hdev, "Change controller speed to %d", speed);
Loic Poulainff289552015-08-25 17:55:44 +0200492
493 speed_cmd[3] = intel_convert_speed(speed);
494 if (speed_cmd[3] == 0xff) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200495 bt_dev_err(hdev, "Unsupported speed");
Loic Poulainff289552015-08-25 17:55:44 +0200496 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 Poulainf44e78a2015-08-31 18:34:30 +0200504 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
505 PTR_ERR(skb));
Loic Poulainff289552015-08-25 17:55:44 +0200506 return PTR_ERR(skb);
507 }
508 kfree_skb(skb);
509
510 skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
511 if (!skb) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200512 bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
Loic Poulainff289552015-08-25 17:55:44 +0200513 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 Poulainca93cee2015-07-01 12:20:26 +0200533static int intel_setup(struct hci_uart *hu)
534{
535 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
536 0x00, 0x08, 0x04, 0x00 };
Loic Poulainb98469f2015-08-29 13:38:19 +0200537 static const u8 lpm_param[] = { 0x03, 0x07, 0x01, 0x0b };
Loic Poulainca93cee2015-07-01 12:20:26 +0200538 struct intel_data *intel = hu->priv;
Loic Poulainb98469f2015-08-29 13:38:19 +0200539 struct intel_device *idev = NULL;
Loic Poulainca93cee2015-07-01 12:20:26 +0200540 struct hci_dev *hdev = hu->hdev;
541 struct sk_buff *skb;
542 struct intel_version *ver;
543 struct intel_boot_params *params;
Loic Poulainb98469f2015-08-29 13:38:19 +0200544 struct list_head *p;
Loic Poulainca93cee2015-07-01 12:20:26 +0200545 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 Poulainff289552015-08-25 17:55:44 +0200551 unsigned int init_speed, oper_speed;
552 int speed_change = 0;
Loic Poulainca93cee2015-07-01 12:20:26 +0200553 int err;
554
Loic Poulainf44e78a2015-08-31 18:34:30 +0200555 bt_dev_dbg(hdev, "start intel_setup");
Loic Poulainca93cee2015-07-01 12:20:26 +0200556
Marcel Holtmann35ab8152015-07-05 14:37:40 +0200557 hu->hdev->set_bdaddr = btintel_set_bdaddr;
558
Loic Poulainca93cee2015-07-01 12:20:26 +0200559 calltime = ktime_get();
560
Loic Poulainff289552015-08-25 17:55:44 +0200561 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 Poulain1ab1f232015-08-27 07:21:51 +0200574 /* 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 Poulainca93cee2015-07-01 12:20:26 +0200583 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 Poulainf44e78a2015-08-31 18:34:30 +0200591 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
592 PTR_ERR(skb));
Loic Poulainca93cee2015-07-01 12:20:26 +0200593 return PTR_ERR(skb);
594 }
595
596 if (skb->len != sizeof(*ver)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200597 bt_dev_err(hdev, "Intel version event size mismatch");
Loic Poulainca93cee2015-07-01 12:20:26 +0200598 kfree_skb(skb);
599 return -EILSEQ;
600 }
601
602 ver = (struct intel_version *)skb->data;
603 if (ver->status) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200604 bt_dev_err(hdev, "Intel version command failure (%02x)",
605 ver->status);
Loic Poulainca93cee2015-07-01 12:20:26 +0200606 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 Poulainf44e78a2015-08-31 18:34:30 +0200615 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
616 ver->hw_platform);
Loic Poulainca93cee2015-07-01 12:20:26 +0200617 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 Poulainf44e78a2015-08-31 18:34:30 +0200627 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
628 ver->hw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200629 kfree_skb(skb);
630 return -EINVAL;
631 }
632
Marcel Holtmann7feb99e2015-07-05 15:02:07 +0200633 btintel_version_info(hdev, ver);
Loic Poulainca93cee2015-07-01 12:20:26 +0200634
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 Poulainf44e78a2015-08-31 18:34:30 +0200659 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
660 ver->fw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200661 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 Poulainf44e78a2015-08-31 18:34:30 +0200672 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
673 PTR_ERR(skb));
Loic Poulainca93cee2015-07-01 12:20:26 +0200674 return PTR_ERR(skb);
675 }
676
677 if (skb->len != sizeof(*params)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200678 bt_dev_err(hdev, "Intel boot parameters size mismatch");
Loic Poulainca93cee2015-07-01 12:20:26 +0200679 kfree_skb(skb);
680 return -EILSEQ;
681 }
682
683 params = (struct intel_boot_params *)skb->data;
684 if (params->status) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200685 bt_dev_err(hdev, "Intel boot parameters command failure (%02x)",
686 params->status);
Loic Poulainca93cee2015-07-01 12:20:26 +0200687 err = -bt_to_errno(params->status);
688 kfree_skb(skb);
689 return err;
690 }
691
Loic Poulainf44e78a2015-08-31 18:34:30 +0200692 bt_dev_info(hdev, "Device revision is %u",
693 le16_to_cpu(params->dev_revid));
Loic Poulainca93cee2015-07-01 12:20:26 +0200694
Loic Poulainf44e78a2015-08-31 18:34:30 +0200695 bt_dev_info(hdev, "Secure boot is %s",
696 params->secure_boot ? "enabled" : "disabled");
Loic Poulainca93cee2015-07-01 12:20:26 +0200697
Loic Poulainf44e78a2015-08-31 18:34:30 +0200698 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
Loic Poulainca93cee2015-07-01 12:20:26 +0200699 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 Poulainf44e78a2015-08-31 18:34:30 +0200707 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
708 params->limited_cce);
Loic Poulainca93cee2015-07-01 12:20:26 +0200709 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(&params->otp_bdaddr, BDADDR_ANY)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200717 bt_dev_info(hdev, "No device address configured");
Loic Poulainca93cee2015-07-01 12:20:26 +0200718 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 Poulainf44e78a2015-08-31 18:34:30 +0200732 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
733 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200734 kfree_skb(skb);
735 return err;
736 }
737
Loic Poulainf44e78a2015-08-31 18:34:30 +0200738 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Loic Poulainca93cee2015-07-01 12:20:26 +0200739
740 kfree_skb(skb);
741
742 if (fw->size < 644) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200743 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
744 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200745 err = -EBADF;
746 goto done;
747 }
748
749 set_bit(STATE_DOWNLOADING, &intel->flags);
750
751 /* Start the firmware download transaction with the Init fragment
752 * represented by the 128 bytes of CSS header.
753 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200754 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Loic Poulainca93cee2015-07-01 12:20:26 +0200755 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200756 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200757 goto done;
758 }
759
760 /* Send the 256 bytes of public key information from the firmware
761 * as the PKey fragment.
762 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200763 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Loic Poulainca93cee2015-07-01 12:20:26 +0200764 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200765 bt_dev_err(hdev, "Failed to send firmware public key (%d)",
766 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200767 goto done;
768 }
769
770 /* Send the 256 bytes of signature information from the firmware
771 * as the Sign fragment.
772 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200773 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Loic Poulainca93cee2015-07-01 12:20:26 +0200774 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200775 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
776 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200777 goto done;
778 }
779
780 fw_ptr = fw->data + 644;
781 frag_len = 0;
782
783 while (fw_ptr - fw->data < fw->size) {
784 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
785
786 frag_len += sizeof(*cmd) + cmd->plen;
787
Loic Poulainf44e78a2015-08-31 18:34:30 +0200788 bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data),
789 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200790
791 /* The parameter length of the secure send command requires
792 * a 4 byte alignment. It happens so that the firmware file
793 * contains proper Intel_NOP commands to align the fragments
794 * as needed.
795 *
796 * Send set of commands with 4 byte alignment from the
797 * firmware data buffer as a single Data fragement.
798 */
799 if (frag_len % 4)
800 continue;
801
802 /* Send each command from the firmware data buffer as
803 * a single Data fragment.
804 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200805 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Loic Poulainca93cee2015-07-01 12:20:26 +0200806 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200807 bt_dev_err(hdev, "Failed to send firmware data (%d)",
808 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200809 goto done;
810 }
811
812 fw_ptr += frag_len;
813 frag_len = 0;
814 }
815
816 set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
817
Loic Poulainf44e78a2015-08-31 18:34:30 +0200818 bt_dev_info(hdev, "Waiting for firmware download to complete");
Loic Poulainca93cee2015-07-01 12:20:26 +0200819
820 /* Before switching the device into operational mode and with that
821 * booting the loaded firmware, wait for the bootloader notification
822 * that all fragments have been successfully received.
823 *
824 * When the event processing receives the notification, then the
825 * STATE_DOWNLOADING flag will be cleared.
826 *
827 * The firmware loading should not take longer than 5 seconds
828 * and thus just timeout if that happens and fail the setup
829 * of this device.
830 */
831 err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
832 TASK_INTERRUPTIBLE,
833 msecs_to_jiffies(5000));
834 if (err == 1) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200835 bt_dev_err(hdev, "Firmware loading interrupted");
Loic Poulainca93cee2015-07-01 12:20:26 +0200836 err = -EINTR;
837 goto done;
838 }
839
840 if (err) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200841 bt_dev_err(hdev, "Firmware loading timeout");
Loic Poulainca93cee2015-07-01 12:20:26 +0200842 err = -ETIMEDOUT;
843 goto done;
844 }
845
846 if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200847 bt_dev_err(hdev, "Firmware loading failed");
Loic Poulainca93cee2015-07-01 12:20:26 +0200848 err = -ENOEXEC;
849 goto done;
850 }
851
852 rettime = ktime_get();
853 delta = ktime_sub(rettime, calltime);
854 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
855
Loic Poulainf44e78a2015-08-31 18:34:30 +0200856 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200857
858done:
859 release_firmware(fw);
860
861 if (err < 0)
862 return err;
863
Loic Poulainff289552015-08-25 17:55:44 +0200864 /* We need to restore the default speed before Intel reset */
865 if (speed_change) {
866 err = intel_set_baudrate(hu, init_speed);
867 if (err)
868 return err;
869 }
870
Loic Poulainca93cee2015-07-01 12:20:26 +0200871 calltime = ktime_get();
872
873 set_bit(STATE_BOOTING, &intel->flags);
874
875 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
876 HCI_INIT_TIMEOUT);
877 if (IS_ERR(skb))
878 return PTR_ERR(skb);
879
880 kfree_skb(skb);
881
882 /* The bootloader will not indicate when the device is ready. This
883 * is done by the operational firmware sending bootup notification.
884 *
885 * Booting into operational firmware should not take longer than
886 * 1 second. However if that happens, then just fail the setup
887 * since something went wrong.
888 */
Loic Poulainf44e78a2015-08-31 18:34:30 +0200889 bt_dev_info(hdev, "Waiting for device to boot");
Loic Poulainca93cee2015-07-01 12:20:26 +0200890
Loic Poulain1ab1f232015-08-27 07:21:51 +0200891 err = intel_wait_booting(hu);
892 if (err)
893 return err;
Loic Poulainca93cee2015-07-01 12:20:26 +0200894
Loic Poulain1ab1f232015-08-27 07:21:51 +0200895 clear_bit(STATE_BOOTING, &intel->flags);
Loic Poulainca93cee2015-07-01 12:20:26 +0200896
897 rettime = ktime_get();
898 delta = ktime_sub(rettime, calltime);
899 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
900
Loic Poulainf44e78a2015-08-31 18:34:30 +0200901 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200902
Loic Poulainb98469f2015-08-29 13:38:19 +0200903 /* Enable LPM if matching pdev with wakeup enabled */
Loic Poulain67c8bde2015-08-31 18:34:31 +0200904 mutex_lock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200905 list_for_each(p, &intel_device_list) {
906 struct intel_device *dev = list_entry(p, struct intel_device,
907 list);
908 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
909 if (device_may_wakeup(&dev->pdev->dev))
910 idev = dev;
911 break;
912 }
913 }
Loic Poulain67c8bde2015-08-31 18:34:31 +0200914 mutex_unlock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200915
916 if (!idev)
917 goto no_lpm;
918
Loic Poulainf44e78a2015-08-31 18:34:30 +0200919 bt_dev_info(hdev, "Enabling LPM");
Loic Poulainb98469f2015-08-29 13:38:19 +0200920
921 skb = __hci_cmd_sync(hdev, 0xfc8b, sizeof(lpm_param), lpm_param,
922 HCI_CMD_TIMEOUT);
923 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200924 bt_dev_err(hdev, "Failed to enable LPM");
Loic Poulainb98469f2015-08-29 13:38:19 +0200925 goto no_lpm;
926 }
927 kfree_skb(skb);
928
929 set_bit(STATE_LPM_ENABLED, &intel->flags);
930
931no_lpm:
Loic Poulainff289552015-08-25 17:55:44 +0200932 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
933 if (IS_ERR(skb))
934 return PTR_ERR(skb);
935 kfree_skb(skb);
936
937 if (speed_change) {
938 err = intel_set_baudrate(hu, oper_speed);
939 if (err)
940 return err;
941 }
942
Loic Poulainf44e78a2015-08-31 18:34:30 +0200943 bt_dev_info(hdev, "Setup complete");
Loic Poulainff289552015-08-25 17:55:44 +0200944
Loic Poulainca93cee2015-07-01 12:20:26 +0200945 clear_bit(STATE_BOOTLOADER, &intel->flags);
946
947 return 0;
948}
949
950static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
951{
952 struct hci_uart *hu = hci_get_drvdata(hdev);
953 struct intel_data *intel = hu->priv;
954 struct hci_event_hdr *hdr;
955
Loic Poulain1ab1f232015-08-27 07:21:51 +0200956 if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
957 !test_bit(STATE_BOOTING, &intel->flags))
Loic Poulainca93cee2015-07-01 12:20:26 +0200958 goto recv;
959
960 hdr = (void *)skb->data;
961
962 /* When the firmware loading completes the device sends
963 * out a vendor specific event indicating the result of
964 * the firmware loading.
965 */
966 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
967 skb->data[2] == 0x06) {
968 if (skb->data[3] != 0x00)
969 set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
970
971 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
972 test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
973 smp_mb__after_atomic();
974 wake_up_bit(&intel->flags, STATE_DOWNLOADING);
975 }
976
977 /* When switching to the operational firmware the device
978 * sends a vendor specific event indicating that the bootup
979 * completed.
980 */
981 } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
982 skb->data[2] == 0x02) {
983 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
984 smp_mb__after_atomic();
985 wake_up_bit(&intel->flags, STATE_BOOTING);
986 }
987 }
988recv:
989 return hci_recv_frame(hdev, skb);
990}
991
Loic Poulainb98469f2015-08-29 13:38:19 +0200992static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
993{
994 struct hci_uart *hu = hci_get_drvdata(hdev);
995 struct intel_data *intel = hu->priv;
996
Loic Poulainf44e78a2015-08-31 18:34:30 +0200997 bt_dev_dbg(hdev, "TX idle notification (%d)", value);
Loic Poulainb98469f2015-08-29 13:38:19 +0200998
Loic Poulain74cdad32015-09-02 12:04:13 +0200999 if (value) {
Loic Poulainb98469f2015-08-29 13:38:19 +02001000 set_bit(STATE_TX_ACTIVE, &intel->flags);
Loic Poulain74cdad32015-09-02 12:04:13 +02001001 schedule_work(&intel->busy_work);
1002 } else {
Loic Poulainb98469f2015-08-29 13:38:19 +02001003 clear_bit(STATE_TX_ACTIVE, &intel->flags);
Loic Poulain74cdad32015-09-02 12:04:13 +02001004 }
Loic Poulainb98469f2015-08-29 13:38:19 +02001005}
1006
1007static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
1008{
1009 struct hci_lpm_pkt *lpm = (void *)skb->data;
Loic Poulain89436542015-09-02 12:04:11 +02001010 struct hci_uart *hu = hci_get_drvdata(hdev);
1011 struct intel_data *intel = hu->priv;
Loic Poulainb98469f2015-08-29 13:38:19 +02001012
1013 switch (lpm->opcode) {
1014 case LPM_OP_TX_NOTIFY:
Loic Poulain1b197572015-09-02 12:04:14 +02001015 if (lpm->dlen < 1) {
1016 bt_dev_err(hu->hdev, "Invalid LPM notification packet");
1017 break;
1018 }
1019 intel_recv_lpm_notify(hdev, lpm->data[0]);
Loic Poulainb98469f2015-08-29 13:38:19 +02001020 break;
Loic Poulain89436542015-09-02 12:04:11 +02001021 case LPM_OP_SUSPEND_ACK:
1022 set_bit(STATE_SUSPENDED, &intel->flags);
1023 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
1024 smp_mb__after_atomic();
1025 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
1026 }
1027 break;
1028 case LPM_OP_RESUME_ACK:
1029 clear_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;
Loic Poulainb98469f2015-08-29 13:38:19 +02001035 default:
Loic Poulainf44e78a2015-08-31 18:34:30 +02001036 bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
Loic Poulainb98469f2015-08-29 13:38:19 +02001037 break;
1038 }
1039
1040 kfree_skb(skb);
1041
1042 return 0;
1043}
1044
1045#define INTEL_RECV_LPM \
1046 .type = HCI_LPM_PKT, \
1047 .hlen = HCI_LPM_HDR_SIZE, \
1048 .loff = 1, \
1049 .lsize = 1, \
1050 .maxlen = HCI_LPM_MAX_SIZE
1051
Loic Poulainca93cee2015-07-01 12:20:26 +02001052static const struct h4_recv_pkt intel_recv_pkts[] = {
Loic Poulainb98469f2015-08-29 13:38:19 +02001053 { H4_RECV_ACL, .recv = hci_recv_frame },
1054 { H4_RECV_SCO, .recv = hci_recv_frame },
1055 { H4_RECV_EVENT, .recv = intel_recv_event },
1056 { INTEL_RECV_LPM, .recv = intel_recv_lpm },
Loic Poulainca93cee2015-07-01 12:20:26 +02001057};
1058
1059static int intel_recv(struct hci_uart *hu, const void *data, int count)
1060{
1061 struct intel_data *intel = hu->priv;
1062
1063 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1064 return -EUNATCH;
1065
1066 intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
1067 intel_recv_pkts,
1068 ARRAY_SIZE(intel_recv_pkts));
1069 if (IS_ERR(intel->rx_skb)) {
1070 int err = PTR_ERR(intel->rx_skb);
Loic Poulainf44e78a2015-08-31 18:34:30 +02001071 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +02001072 intel->rx_skb = NULL;
1073 return err;
1074 }
1075
1076 return count;
1077}
1078
1079static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
1080{
1081 struct intel_data *intel = hu->priv;
Loic Poulain74cdad32015-09-02 12:04:13 +02001082 struct list_head *p;
Loic Poulainca93cee2015-07-01 12:20:26 +02001083
1084 BT_DBG("hu %p skb %p", hu, skb);
1085
Loic Poulain74cdad32015-09-02 12:04:13 +02001086 /* Be sure our controller is resumed and potential LPM transaction
1087 * completed before enqueuing any packet.
1088 */
1089 mutex_lock(&intel_device_list_lock);
1090 list_for_each(p, &intel_device_list) {
1091 struct intel_device *idev = list_entry(p, struct intel_device,
1092 list);
1093
1094 if (hu->tty->dev->parent == idev->pdev->dev.parent) {
1095 pm_runtime_get_sync(&idev->pdev->dev);
1096 pm_runtime_mark_last_busy(&idev->pdev->dev);
1097 pm_runtime_put_autosuspend(&idev->pdev->dev);
1098 break;
1099 }
1100 }
1101 mutex_unlock(&intel_device_list_lock);
1102
Loic Poulainca93cee2015-07-01 12:20:26 +02001103 skb_queue_tail(&intel->txq, skb);
1104
1105 return 0;
1106}
1107
1108static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1109{
1110 struct intel_data *intel = hu->priv;
1111 struct sk_buff *skb;
1112
1113 skb = skb_dequeue(&intel->txq);
1114 if (!skb)
1115 return skb;
1116
1117 if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
1118 (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) {
1119 struct hci_command_hdr *cmd = (void *)skb->data;
1120 __u16 opcode = le16_to_cpu(cmd->opcode);
1121
1122 /* When the 0xfc01 command is issued to boot into
1123 * the operational firmware, it will actually not
1124 * send a command complete event. To keep the flow
1125 * control working inject that event here.
1126 */
1127 if (opcode == 0xfc01)
1128 inject_cmd_complete(hu->hdev, opcode);
1129 }
1130
1131 /* Prepend skb with frame type */
1132 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
1133
1134 return skb;
1135}
1136
1137static const struct hci_uart_proto intel_proto = {
1138 .id = HCI_UART_INTEL,
1139 .name = "Intel",
1140 .init_speed = 115200,
Loic Poulainff289552015-08-25 17:55:44 +02001141 .oper_speed = 3000000,
Loic Poulainca93cee2015-07-01 12:20:26 +02001142 .open = intel_open,
1143 .close = intel_close,
1144 .flush = intel_flush,
1145 .setup = intel_setup,
Loic Poulainff289552015-08-25 17:55:44 +02001146 .set_baudrate = intel_set_baudrate,
Loic Poulainca93cee2015-07-01 12:20:26 +02001147 .recv = intel_recv,
1148 .enqueue = intel_enqueue,
1149 .dequeue = intel_dequeue,
1150};
1151
Loic Poulain1ab1f232015-08-27 07:21:51 +02001152#ifdef CONFIG_ACPI
1153static const struct acpi_device_id intel_acpi_match[] = {
1154 { "INT33E1", 0 },
1155 { },
1156};
1157MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
1158
1159static int intel_acpi_probe(struct intel_device *idev)
1160{
1161 const struct acpi_device_id *id;
1162
1163 id = acpi_match_device(intel_acpi_match, &idev->pdev->dev);
1164 if (!id)
1165 return -ENODEV;
1166
1167 return 0;
1168}
1169#else
1170static int intel_acpi_probe(struct intel_device *idev)
1171{
1172 return -ENODEV;
1173}
1174#endif
1175
Loic Poulain74cdad32015-09-02 12:04:13 +02001176#ifdef CONFIG_PM
Loic Poulainaa6802d2015-09-02 12:04:12 +02001177static int intel_suspend(struct device *dev)
1178{
1179 struct intel_device *idev = dev_get_drvdata(dev);
1180
1181 dev_dbg(dev, "intel_suspend");
1182
1183 mutex_lock(&idev->hu_lock);
1184 if (idev->hu)
1185 intel_lpm_suspend(idev->hu);
1186 mutex_unlock(&idev->hu_lock);
1187
1188 return 0;
1189}
1190
1191static int intel_resume(struct device *dev)
1192{
1193 struct intel_device *idev = dev_get_drvdata(dev);
1194
1195 dev_dbg(dev, "intel_resume");
1196
1197 mutex_lock(&idev->hu_lock);
1198 if (idev->hu)
1199 intel_lpm_resume(idev->hu);
1200 mutex_unlock(&idev->hu_lock);
1201
1202 return 0;
1203}
1204#endif
1205
1206static const struct dev_pm_ops intel_pm_ops = {
1207 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
Loic Poulain74cdad32015-09-02 12:04:13 +02001208 SET_RUNTIME_PM_OPS(intel_suspend, intel_resume, NULL)
Loic Poulainaa6802d2015-09-02 12:04:12 +02001209};
1210
Loic Poulain1ab1f232015-08-27 07:21:51 +02001211static int intel_probe(struct platform_device *pdev)
1212{
1213 struct intel_device *idev;
1214
1215 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
1216 if (!idev)
1217 return -ENOMEM;
1218
Loic Poulainaa6802d2015-09-02 12:04:12 +02001219 mutex_init(&idev->hu_lock);
1220
Loic Poulain1ab1f232015-08-27 07:21:51 +02001221 idev->pdev = pdev;
1222
1223 if (ACPI_HANDLE(&pdev->dev)) {
1224 int err = intel_acpi_probe(idev);
1225 if (err)
1226 return err;
1227 } else {
1228 return -ENODEV;
1229 }
1230
1231 idev->reset = devm_gpiod_get_optional(&pdev->dev, "reset",
1232 GPIOD_OUT_LOW);
1233 if (IS_ERR(idev->reset)) {
1234 dev_err(&pdev->dev, "Unable to retrieve gpio\n");
1235 return PTR_ERR(idev->reset);
1236 }
1237
Loic Poulain765ea3a2015-08-29 13:38:18 +02001238 idev->irq = platform_get_irq(pdev, 0);
1239 if (idev->irq < 0) {
1240 struct gpio_desc *host_wake;
1241
1242 dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1243
1244 host_wake = devm_gpiod_get_optional(&pdev->dev, "host-wake",
1245 GPIOD_IN);
1246 if (IS_ERR(host_wake)) {
1247 dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1248 goto no_irq;
1249 }
1250
1251 idev->irq = gpiod_to_irq(host_wake);
1252 if (idev->irq < 0) {
1253 dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1254 goto no_irq;
1255 }
1256 }
1257
1258 /* Only enable wake-up/irq when controller is powered */
1259 device_set_wakeup_capable(&pdev->dev, true);
1260 device_wakeup_disable(&pdev->dev);
1261
1262no_irq:
Loic Poulain1ab1f232015-08-27 07:21:51 +02001263 platform_set_drvdata(pdev, idev);
1264
1265 /* Place this instance on the device list */
Loic Poulain67c8bde2015-08-31 18:34:31 +02001266 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001267 list_add_tail(&idev->list, &intel_device_list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001268 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001269
Loic Poulain765ea3a2015-08-29 13:38:18 +02001270 dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1271 desc_to_gpio(idev->reset), idev->irq);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001272
1273 return 0;
1274}
1275
1276static int intel_remove(struct platform_device *pdev)
1277{
1278 struct intel_device *idev = platform_get_drvdata(pdev);
1279
Loic Poulain765ea3a2015-08-29 13:38:18 +02001280 device_wakeup_disable(&pdev->dev);
1281
Loic Poulain67c8bde2015-08-31 18:34:31 +02001282 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001283 list_del(&idev->list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001284 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001285
1286 dev_info(&pdev->dev, "unregistered.\n");
1287
1288 return 0;
1289}
1290
1291static struct platform_driver intel_driver = {
1292 .probe = intel_probe,
1293 .remove = intel_remove,
1294 .driver = {
1295 .name = "hci_intel",
1296 .acpi_match_table = ACPI_PTR(intel_acpi_match),
Loic Poulainaa6802d2015-09-02 12:04:12 +02001297 .pm = &intel_pm_ops,
Loic Poulain1ab1f232015-08-27 07:21:51 +02001298 },
1299};
1300
Loic Poulainca93cee2015-07-01 12:20:26 +02001301int __init intel_init(void)
1302{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001303 platform_driver_register(&intel_driver);
1304
Loic Poulainca93cee2015-07-01 12:20:26 +02001305 return hci_uart_register_proto(&intel_proto);
1306}
1307
1308int __exit intel_deinit(void)
1309{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001310 platform_driver_unregister(&intel_driver);
1311
Loic Poulainca93cee2015-07-01 12:20:26 +02001312 return hci_uart_unregister_proto(&intel_proto);
1313}