blob: 3897e7506089bca2f3949188228e4917a52947d5 [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>
Marcel Holtmann16e38872015-04-04 16:13:02 -070035
36#include <net/bluetooth/bluetooth.h>
37#include <net/bluetooth/hci_core.h>
38
39#include "hci_uart.h"
Loic Poulainca93cee2015-07-01 12:20:26 +020040#include "btintel.h"
41
42#define STATE_BOOTLOADER 0
43#define STATE_DOWNLOADING 1
44#define STATE_FIRMWARE_LOADED 2
45#define STATE_FIRMWARE_FAILED 3
46#define STATE_BOOTING 4
Loic Poulainb98469f2015-08-29 13:38:19 +020047#define STATE_LPM_ENABLED 5
48#define STATE_TX_ACTIVE 6
Loic Poulain89436542015-09-02 12:04:11 +020049#define STATE_SUSPENDED 7
50#define STATE_LPM_TRANSACTION 8
Loic Poulainb98469f2015-08-29 13:38:19 +020051
Loic Poulain89436542015-09-02 12:04:11 +020052#define HCI_LPM_WAKE_PKT 0xf0
Loic Poulainb98469f2015-08-29 13:38:19 +020053#define HCI_LPM_PKT 0xf1
54#define HCI_LPM_MAX_SIZE 10
55#define HCI_LPM_HDR_SIZE HCI_EVENT_HDR_SIZE
56
57#define LPM_OP_TX_NOTIFY 0x00
Loic Poulain89436542015-09-02 12:04:11 +020058#define LPM_OP_SUSPEND_ACK 0x02
59#define LPM_OP_RESUME_ACK 0x03
Loic Poulainb98469f2015-08-29 13:38:19 +020060
61struct hci_lpm_pkt {
62 __u8 opcode;
63 __u8 dlen;
64 __u8 data[0];
65} __packed;
Loic Poulainca93cee2015-07-01 12:20:26 +020066
Loic Poulain1ab1f232015-08-27 07:21:51 +020067struct intel_device {
68 struct list_head list;
69 struct platform_device *pdev;
70 struct gpio_desc *reset;
Loic Poulainaa6802d2015-09-02 12:04:12 +020071 struct hci_uart *hu;
72 struct mutex hu_lock;
Loic Poulain765ea3a2015-08-29 13:38:18 +020073 int irq;
Loic Poulain1ab1f232015-08-27 07:21:51 +020074};
75
76static LIST_HEAD(intel_device_list);
Loic Poulain67c8bde2015-08-31 18:34:31 +020077static DEFINE_MUTEX(intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +020078
Loic Poulainca93cee2015-07-01 12:20:26 +020079struct intel_data {
80 struct sk_buff *rx_skb;
81 struct sk_buff_head txq;
82 unsigned long flags;
83};
84
Loic Poulainff289552015-08-25 17:55:44 +020085static u8 intel_convert_speed(unsigned int speed)
86{
87 switch (speed) {
88 case 9600:
89 return 0x00;
90 case 19200:
91 return 0x01;
92 case 38400:
93 return 0x02;
94 case 57600:
95 return 0x03;
96 case 115200:
97 return 0x04;
98 case 230400:
99 return 0x05;
100 case 460800:
101 return 0x06;
102 case 921600:
103 return 0x07;
104 case 1843200:
105 return 0x08;
106 case 3250000:
107 return 0x09;
108 case 2000000:
109 return 0x0a;
110 case 3000000:
111 return 0x0b;
112 default:
113 return 0xff;
114 }
115}
116
Loic Poulain1ab1f232015-08-27 07:21:51 +0200117static int intel_wait_booting(struct hci_uart *hu)
118{
119 struct intel_data *intel = hu->priv;
120 int err;
121
122 err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
123 TASK_INTERRUPTIBLE,
124 msecs_to_jiffies(1000));
125
126 if (err == 1) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200127 bt_dev_err(hu->hdev, "Device boot interrupted");
Loic Poulain1ab1f232015-08-27 07:21:51 +0200128 return -EINTR;
129 }
130
131 if (err) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200132 bt_dev_err(hu->hdev, "Device boot timeout");
Loic Poulain1ab1f232015-08-27 07:21:51 +0200133 return -ETIMEDOUT;
134 }
135
136 return err;
137}
138
Loic Poulain89436542015-09-02 12:04:11 +0200139static int intel_wait_lpm_transaction(struct hci_uart *hu)
140{
141 struct intel_data *intel = hu->priv;
142 int err;
143
144 err = wait_on_bit_timeout(&intel->flags, STATE_LPM_TRANSACTION,
145 TASK_INTERRUPTIBLE,
146 msecs_to_jiffies(1000));
147
148 if (err == 1) {
149 bt_dev_err(hu->hdev, "LPM transaction interrupted");
150 return -EINTR;
151 }
152
153 if (err) {
154 bt_dev_err(hu->hdev, "LPM transaction timeout");
155 return -ETIMEDOUT;
156 }
157
158 return err;
159}
160
161static int intel_lpm_suspend(struct hci_uart *hu)
162{
163 static const u8 suspend[] = { 0x01, 0x01, 0x01 };
164 struct intel_data *intel = hu->priv;
165 struct sk_buff *skb;
166
167 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
168 test_bit(STATE_SUSPENDED, &intel->flags))
169 return 0;
170
171 if (test_bit(STATE_TX_ACTIVE, &intel->flags))
172 return -EAGAIN;
173
174 bt_dev_dbg(hu->hdev, "Suspending");
175
176 skb = bt_skb_alloc(sizeof(suspend), GFP_KERNEL);
177 if (!skb) {
178 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
179 return -ENOMEM;
180 }
181
182 memcpy(skb_put(skb, sizeof(suspend)), suspend, sizeof(suspend));
183 bt_cb(skb)->pkt_type = HCI_LPM_PKT;
184
185 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
186
187 skb_queue_tail(&intel->txq, skb);
188 hci_uart_tx_wakeup(hu);
189
190 intel_wait_lpm_transaction(hu);
191 /* Even in case of failure, continue and test the suspended flag */
192
193 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
194
195 if (!test_bit(STATE_SUSPENDED, &intel->flags)) {
196 bt_dev_err(hu->hdev, "Device suspend error");
197 return -EINVAL;
198 }
199
200 bt_dev_dbg(hu->hdev, "Suspended");
201
202 hci_uart_set_flow_control(hu, true);
203
204 return 0;
205}
206
207static int intel_lpm_resume(struct hci_uart *hu)
208{
209 struct intel_data *intel = hu->priv;
210 struct sk_buff *skb;
211
212 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
213 !test_bit(STATE_SUSPENDED, &intel->flags))
214 return 0;
215
216 bt_dev_dbg(hu->hdev, "Resuming");
217
218 hci_uart_set_flow_control(hu, false);
219
220 skb = bt_skb_alloc(0, GFP_KERNEL);
221 if (!skb) {
222 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
223 return -ENOMEM;
224 }
225
226 bt_cb(skb)->pkt_type = HCI_LPM_WAKE_PKT;
227
228 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
229
230 skb_queue_tail(&intel->txq, skb);
231 hci_uart_tx_wakeup(hu);
232
233 intel_wait_lpm_transaction(hu);
234 /* Even in case of failure, continue and test the suspended flag */
235
236 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
237
238 if (test_bit(STATE_SUSPENDED, &intel->flags)) {
239 bt_dev_err(hu->hdev, "Device resume error");
240 return -EINVAL;
241 }
242
243 bt_dev_dbg(hu->hdev, "Resumed");
244
245 return 0;
246}
247
248static int intel_lpm_host_wake(struct hci_uart *hu)
249{
250 static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 };
251 struct intel_data *intel = hu->priv;
252 struct sk_buff *skb;
253
254 hci_uart_set_flow_control(hu, false);
255
256 clear_bit(STATE_SUSPENDED, &intel->flags);
257
258 skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL);
259 if (!skb) {
260 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
261 return -ENOMEM;
262 }
263
264 memcpy(skb_put(skb, sizeof(lpm_resume_ack)), lpm_resume_ack,
265 sizeof(lpm_resume_ack));
266 bt_cb(skb)->pkt_type = HCI_LPM_PKT;
267
268 skb_queue_tail(&intel->txq, skb);
269 hci_uart_tx_wakeup(hu);
270
271 bt_dev_dbg(hu->hdev, "Resumed by controller");
272
273 return 0;
274}
275
Loic Poulain765ea3a2015-08-29 13:38:18 +0200276static irqreturn_t intel_irq(int irq, void *dev_id)
277{
278 struct intel_device *idev = dev_id;
279
280 dev_info(&idev->pdev->dev, "hci_intel irq\n");
281
Loic Poulainaa6802d2015-09-02 12:04:12 +0200282 mutex_lock(&idev->hu_lock);
283 if (idev->hu)
284 intel_lpm_host_wake(idev->hu);
285 mutex_unlock(&idev->hu_lock);
286
Loic Poulain765ea3a2015-08-29 13:38:18 +0200287 return IRQ_HANDLED;
288}
289
Loic Poulain1ab1f232015-08-27 07:21:51 +0200290static int intel_set_power(struct hci_uart *hu, bool powered)
291{
292 struct list_head *p;
293 int err = -ENODEV;
294
Loic Poulain67c8bde2015-08-31 18:34:31 +0200295 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200296
297 list_for_each(p, &intel_device_list) {
298 struct intel_device *idev = list_entry(p, struct intel_device,
299 list);
300
301 /* tty device and pdev device should share the same parent
302 * which is the UART port.
303 */
304 if (hu->tty->dev->parent != idev->pdev->dev.parent)
305 continue;
306
307 if (!idev->reset) {
308 err = -ENOTSUPP;
309 break;
310 }
311
312 BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
313 hu, dev_name(&idev->pdev->dev), powered);
314
315 gpiod_set_value(idev->reset, powered);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200316
Loic Poulainaa6802d2015-09-02 12:04:12 +0200317 /* Provide to idev a hu reference which is used to run LPM
318 * transactions (lpm suspend/resume) from PM callbacks.
319 * hu needs to be protected against concurrent removing during
320 * these PM ops.
321 */
322 mutex_lock(&idev->hu_lock);
323 idev->hu = powered ? hu : NULL;
324 mutex_unlock(&idev->hu_lock);
325
Loic Poulain765ea3a2015-08-29 13:38:18 +0200326 if (idev->irq < 0)
327 break;
328
329 if (powered && device_can_wakeup(&idev->pdev->dev)) {
330 err = devm_request_threaded_irq(&idev->pdev->dev,
331 idev->irq, NULL,
332 intel_irq,
333 IRQF_ONESHOT,
334 "bt-host-wake", idev);
335 if (err) {
336 BT_ERR("hu %p, unable to allocate irq-%d",
337 hu, idev->irq);
338 break;
339 }
340
341 device_wakeup_enable(&idev->pdev->dev);
342 } else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
343 devm_free_irq(&idev->pdev->dev, idev->irq, idev);
344 device_wakeup_disable(&idev->pdev->dev);
345 }
Loic Poulain1ab1f232015-08-27 07:21:51 +0200346 }
347
Loic Poulain67c8bde2015-08-31 18:34:31 +0200348 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200349
350 return err;
351}
352
Loic Poulainca93cee2015-07-01 12:20:26 +0200353static int intel_open(struct hci_uart *hu)
354{
355 struct intel_data *intel;
356
357 BT_DBG("hu %p", hu);
358
359 intel = kzalloc(sizeof(*intel), GFP_KERNEL);
360 if (!intel)
361 return -ENOMEM;
362
363 skb_queue_head_init(&intel->txq);
364
365 hu->priv = intel;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200366
367 if (!intel_set_power(hu, true))
368 set_bit(STATE_BOOTING, &intel->flags);
369
Loic Poulainca93cee2015-07-01 12:20:26 +0200370 return 0;
371}
372
373static int intel_close(struct hci_uart *hu)
374{
375 struct intel_data *intel = hu->priv;
376
377 BT_DBG("hu %p", hu);
378
Loic Poulain1ab1f232015-08-27 07:21:51 +0200379 intel_set_power(hu, false);
380
Loic Poulainca93cee2015-07-01 12:20:26 +0200381 skb_queue_purge(&intel->txq);
382 kfree_skb(intel->rx_skb);
383 kfree(intel);
384
385 hu->priv = NULL;
386 return 0;
387}
388
389static int intel_flush(struct hci_uart *hu)
390{
391 struct intel_data *intel = hu->priv;
392
393 BT_DBG("hu %p", hu);
394
395 skb_queue_purge(&intel->txq);
396
397 return 0;
398}
399
400static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
401{
402 struct sk_buff *skb;
403 struct hci_event_hdr *hdr;
404 struct hci_ev_cmd_complete *evt;
405
406 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
407 if (!skb)
408 return -ENOMEM;
409
410 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
411 hdr->evt = HCI_EV_CMD_COMPLETE;
412 hdr->plen = sizeof(*evt) + 1;
413
414 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
415 evt->ncmd = 0x01;
416 evt->opcode = cpu_to_le16(opcode);
417
418 *skb_put(skb, 1) = 0x00;
419
420 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
421
422 return hci_recv_frame(hdev, skb);
423}
424
Loic Poulainff289552015-08-25 17:55:44 +0200425static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
426{
427 struct intel_data *intel = hu->priv;
428 struct hci_dev *hdev = hu->hdev;
429 u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
430 struct sk_buff *skb;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200431 int err;
432
433 /* This can be the first command sent to the chip, check
434 * that the controller is ready.
435 */
436 err = intel_wait_booting(hu);
437
438 clear_bit(STATE_BOOTING, &intel->flags);
439
440 /* In case of timeout, try to continue anyway */
441 if (err && err != ETIMEDOUT)
442 return err;
Loic Poulainff289552015-08-25 17:55:44 +0200443
Loic Poulainf44e78a2015-08-31 18:34:30 +0200444 bt_dev_info(hdev, "Change controller speed to %d", speed);
Loic Poulainff289552015-08-25 17:55:44 +0200445
446 speed_cmd[3] = intel_convert_speed(speed);
447 if (speed_cmd[3] == 0xff) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200448 bt_dev_err(hdev, "Unsupported speed");
Loic Poulainff289552015-08-25 17:55:44 +0200449 return -EINVAL;
450 }
451
452 /* Device will not accept speed change if Intel version has not been
453 * previously requested.
454 */
455 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
456 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200457 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
458 PTR_ERR(skb));
Loic Poulainff289552015-08-25 17:55:44 +0200459 return PTR_ERR(skb);
460 }
461 kfree_skb(skb);
462
463 skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
464 if (!skb) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200465 bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
Loic Poulainff289552015-08-25 17:55:44 +0200466 return -ENOMEM;
467 }
468
469 memcpy(skb_put(skb, sizeof(speed_cmd)), speed_cmd, sizeof(speed_cmd));
470 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
471
472 hci_uart_set_flow_control(hu, true);
473
474 skb_queue_tail(&intel->txq, skb);
475 hci_uart_tx_wakeup(hu);
476
477 /* wait 100ms to change baudrate on controller side */
478 msleep(100);
479
480 hci_uart_set_baudrate(hu, speed);
481 hci_uart_set_flow_control(hu, false);
482
483 return 0;
484}
485
Loic Poulainca93cee2015-07-01 12:20:26 +0200486static int intel_setup(struct hci_uart *hu)
487{
488 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
489 0x00, 0x08, 0x04, 0x00 };
Loic Poulainb98469f2015-08-29 13:38:19 +0200490 static const u8 lpm_param[] = { 0x03, 0x07, 0x01, 0x0b };
Loic Poulainca93cee2015-07-01 12:20:26 +0200491 struct intel_data *intel = hu->priv;
Loic Poulainb98469f2015-08-29 13:38:19 +0200492 struct intel_device *idev = NULL;
Loic Poulainca93cee2015-07-01 12:20:26 +0200493 struct hci_dev *hdev = hu->hdev;
494 struct sk_buff *skb;
495 struct intel_version *ver;
496 struct intel_boot_params *params;
Loic Poulainb98469f2015-08-29 13:38:19 +0200497 struct list_head *p;
Loic Poulainca93cee2015-07-01 12:20:26 +0200498 const struct firmware *fw;
499 const u8 *fw_ptr;
500 char fwname[64];
501 u32 frag_len;
502 ktime_t calltime, delta, rettime;
503 unsigned long long duration;
Loic Poulainff289552015-08-25 17:55:44 +0200504 unsigned int init_speed, oper_speed;
505 int speed_change = 0;
Loic Poulainca93cee2015-07-01 12:20:26 +0200506 int err;
507
Loic Poulainf44e78a2015-08-31 18:34:30 +0200508 bt_dev_dbg(hdev, "start intel_setup");
Loic Poulainca93cee2015-07-01 12:20:26 +0200509
Marcel Holtmann35ab8152015-07-05 14:37:40 +0200510 hu->hdev->set_bdaddr = btintel_set_bdaddr;
511
Loic Poulainca93cee2015-07-01 12:20:26 +0200512 calltime = ktime_get();
513
Loic Poulainff289552015-08-25 17:55:44 +0200514 if (hu->init_speed)
515 init_speed = hu->init_speed;
516 else
517 init_speed = hu->proto->init_speed;
518
519 if (hu->oper_speed)
520 oper_speed = hu->oper_speed;
521 else
522 oper_speed = hu->proto->oper_speed;
523
524 if (oper_speed && init_speed && oper_speed != init_speed)
525 speed_change = 1;
526
Loic Poulain1ab1f232015-08-27 07:21:51 +0200527 /* Check that the controller is ready */
528 err = intel_wait_booting(hu);
529
530 clear_bit(STATE_BOOTING, &intel->flags);
531
532 /* In case of timeout, try to continue anyway */
533 if (err && err != ETIMEDOUT)
534 return err;
535
Loic Poulainca93cee2015-07-01 12:20:26 +0200536 set_bit(STATE_BOOTLOADER, &intel->flags);
537
538 /* Read the Intel version information to determine if the device
539 * is in bootloader mode or if it already has operational firmware
540 * loaded.
541 */
542 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
543 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200544 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
545 PTR_ERR(skb));
Loic Poulainca93cee2015-07-01 12:20:26 +0200546 return PTR_ERR(skb);
547 }
548
549 if (skb->len != sizeof(*ver)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200550 bt_dev_err(hdev, "Intel version event size mismatch");
Loic Poulainca93cee2015-07-01 12:20:26 +0200551 kfree_skb(skb);
552 return -EILSEQ;
553 }
554
555 ver = (struct intel_version *)skb->data;
556 if (ver->status) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200557 bt_dev_err(hdev, "Intel version command failure (%02x)",
558 ver->status);
Loic Poulainca93cee2015-07-01 12:20:26 +0200559 err = -bt_to_errno(ver->status);
560 kfree_skb(skb);
561 return err;
562 }
563
564 /* The hardware platform number has a fixed value of 0x37 and
565 * for now only accept this single value.
566 */
567 if (ver->hw_platform != 0x37) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200568 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
569 ver->hw_platform);
Loic Poulainca93cee2015-07-01 12:20:26 +0200570 kfree_skb(skb);
571 return -EINVAL;
572 }
573
574 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
575 * supported by this firmware loading method. This check has been
576 * put in place to ensure correct forward compatibility options
577 * when newer hardware variants come along.
578 */
579 if (ver->hw_variant != 0x0b) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200580 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
581 ver->hw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200582 kfree_skb(skb);
583 return -EINVAL;
584 }
585
Marcel Holtmann7feb99e2015-07-05 15:02:07 +0200586 btintel_version_info(hdev, ver);
Loic Poulainca93cee2015-07-01 12:20:26 +0200587
588 /* The firmware variant determines if the device is in bootloader
589 * mode or is running operational firmware. The value 0x06 identifies
590 * the bootloader and the value 0x23 identifies the operational
591 * firmware.
592 *
593 * When the operational firmware is already present, then only
594 * the check for valid Bluetooth device address is needed. This
595 * determines if the device will be added as configured or
596 * unconfigured controller.
597 *
598 * It is not possible to use the Secure Boot Parameters in this
599 * case since that command is only available in bootloader mode.
600 */
601 if (ver->fw_variant == 0x23) {
602 kfree_skb(skb);
603 clear_bit(STATE_BOOTLOADER, &intel->flags);
604 btintel_check_bdaddr(hdev);
605 return 0;
606 }
607
608 /* If the device is not in bootloader mode, then the only possible
609 * choice is to return an error and abort the device initialization.
610 */
611 if (ver->fw_variant != 0x06) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200612 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
613 ver->fw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200614 kfree_skb(skb);
615 return -ENODEV;
616 }
617
618 kfree_skb(skb);
619
620 /* Read the secure boot parameters to identify the operating
621 * details of the bootloader.
622 */
623 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
624 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200625 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
626 PTR_ERR(skb));
Loic Poulainca93cee2015-07-01 12:20:26 +0200627 return PTR_ERR(skb);
628 }
629
630 if (skb->len != sizeof(*params)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200631 bt_dev_err(hdev, "Intel boot parameters size mismatch");
Loic Poulainca93cee2015-07-01 12:20:26 +0200632 kfree_skb(skb);
633 return -EILSEQ;
634 }
635
636 params = (struct intel_boot_params *)skb->data;
637 if (params->status) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200638 bt_dev_err(hdev, "Intel boot parameters command failure (%02x)",
639 params->status);
Loic Poulainca93cee2015-07-01 12:20:26 +0200640 err = -bt_to_errno(params->status);
641 kfree_skb(skb);
642 return err;
643 }
644
Loic Poulainf44e78a2015-08-31 18:34:30 +0200645 bt_dev_info(hdev, "Device revision is %u",
646 le16_to_cpu(params->dev_revid));
Loic Poulainca93cee2015-07-01 12:20:26 +0200647
Loic Poulainf44e78a2015-08-31 18:34:30 +0200648 bt_dev_info(hdev, "Secure boot is %s",
649 params->secure_boot ? "enabled" : "disabled");
Loic Poulainca93cee2015-07-01 12:20:26 +0200650
Loic Poulainf44e78a2015-08-31 18:34:30 +0200651 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
Loic Poulainca93cee2015-07-01 12:20:26 +0200652 params->min_fw_build_nn, params->min_fw_build_cw,
653 2000 + params->min_fw_build_yy);
654
655 /* It is required that every single firmware fragment is acknowledged
656 * with a command complete event. If the boot parameters indicate
657 * that this bootloader does not send them, then abort the setup.
658 */
659 if (params->limited_cce != 0x00) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200660 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
661 params->limited_cce);
Loic Poulainca93cee2015-07-01 12:20:26 +0200662 kfree_skb(skb);
663 return -EINVAL;
664 }
665
666 /* If the OTP has no valid Bluetooth device address, then there will
667 * also be no valid address for the operational firmware.
668 */
669 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200670 bt_dev_info(hdev, "No device address configured");
Loic Poulainca93cee2015-07-01 12:20:26 +0200671 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
672 }
673
674 /* With this Intel bootloader only the hardware variant and device
675 * revision information are used to select the right firmware.
676 *
677 * Currently this bootloader support is limited to hardware variant
678 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
679 */
680 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
681 le16_to_cpu(params->dev_revid));
682
683 err = request_firmware(&fw, fwname, &hdev->dev);
684 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200685 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
686 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200687 kfree_skb(skb);
688 return err;
689 }
690
Loic Poulainf44e78a2015-08-31 18:34:30 +0200691 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Loic Poulainca93cee2015-07-01 12:20:26 +0200692
693 kfree_skb(skb);
694
695 if (fw->size < 644) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200696 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
697 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200698 err = -EBADF;
699 goto done;
700 }
701
702 set_bit(STATE_DOWNLOADING, &intel->flags);
703
704 /* Start the firmware download transaction with the Init fragment
705 * represented by the 128 bytes of CSS header.
706 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200707 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Loic Poulainca93cee2015-07-01 12:20:26 +0200708 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200709 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200710 goto done;
711 }
712
713 /* Send the 256 bytes of public key information from the firmware
714 * as the PKey fragment.
715 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200716 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Loic Poulainca93cee2015-07-01 12:20:26 +0200717 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200718 bt_dev_err(hdev, "Failed to send firmware public key (%d)",
719 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200720 goto done;
721 }
722
723 /* Send the 256 bytes of signature information from the firmware
724 * as the Sign fragment.
725 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200726 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Loic Poulainca93cee2015-07-01 12:20:26 +0200727 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200728 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
729 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200730 goto done;
731 }
732
733 fw_ptr = fw->data + 644;
734 frag_len = 0;
735
736 while (fw_ptr - fw->data < fw->size) {
737 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
738
739 frag_len += sizeof(*cmd) + cmd->plen;
740
Loic Poulainf44e78a2015-08-31 18:34:30 +0200741 bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data),
742 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200743
744 /* The parameter length of the secure send command requires
745 * a 4 byte alignment. It happens so that the firmware file
746 * contains proper Intel_NOP commands to align the fragments
747 * as needed.
748 *
749 * Send set of commands with 4 byte alignment from the
750 * firmware data buffer as a single Data fragement.
751 */
752 if (frag_len % 4)
753 continue;
754
755 /* Send each command from the firmware data buffer as
756 * a single Data fragment.
757 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200758 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Loic Poulainca93cee2015-07-01 12:20:26 +0200759 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200760 bt_dev_err(hdev, "Failed to send firmware data (%d)",
761 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200762 goto done;
763 }
764
765 fw_ptr += frag_len;
766 frag_len = 0;
767 }
768
769 set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
770
Loic Poulainf44e78a2015-08-31 18:34:30 +0200771 bt_dev_info(hdev, "Waiting for firmware download to complete");
Loic Poulainca93cee2015-07-01 12:20:26 +0200772
773 /* Before switching the device into operational mode and with that
774 * booting the loaded firmware, wait for the bootloader notification
775 * that all fragments have been successfully received.
776 *
777 * When the event processing receives the notification, then the
778 * STATE_DOWNLOADING flag will be cleared.
779 *
780 * The firmware loading should not take longer than 5 seconds
781 * and thus just timeout if that happens and fail the setup
782 * of this device.
783 */
784 err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
785 TASK_INTERRUPTIBLE,
786 msecs_to_jiffies(5000));
787 if (err == 1) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200788 bt_dev_err(hdev, "Firmware loading interrupted");
Loic Poulainca93cee2015-07-01 12:20:26 +0200789 err = -EINTR;
790 goto done;
791 }
792
793 if (err) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200794 bt_dev_err(hdev, "Firmware loading timeout");
Loic Poulainca93cee2015-07-01 12:20:26 +0200795 err = -ETIMEDOUT;
796 goto done;
797 }
798
799 if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200800 bt_dev_err(hdev, "Firmware loading failed");
Loic Poulainca93cee2015-07-01 12:20:26 +0200801 err = -ENOEXEC;
802 goto done;
803 }
804
805 rettime = ktime_get();
806 delta = ktime_sub(rettime, calltime);
807 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
808
Loic Poulainf44e78a2015-08-31 18:34:30 +0200809 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200810
811done:
812 release_firmware(fw);
813
814 if (err < 0)
815 return err;
816
Loic Poulainff289552015-08-25 17:55:44 +0200817 /* We need to restore the default speed before Intel reset */
818 if (speed_change) {
819 err = intel_set_baudrate(hu, init_speed);
820 if (err)
821 return err;
822 }
823
Loic Poulainca93cee2015-07-01 12:20:26 +0200824 calltime = ktime_get();
825
826 set_bit(STATE_BOOTING, &intel->flags);
827
828 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
829 HCI_INIT_TIMEOUT);
830 if (IS_ERR(skb))
831 return PTR_ERR(skb);
832
833 kfree_skb(skb);
834
835 /* The bootloader will not indicate when the device is ready. This
836 * is done by the operational firmware sending bootup notification.
837 *
838 * Booting into operational firmware should not take longer than
839 * 1 second. However if that happens, then just fail the setup
840 * since something went wrong.
841 */
Loic Poulainf44e78a2015-08-31 18:34:30 +0200842 bt_dev_info(hdev, "Waiting for device to boot");
Loic Poulainca93cee2015-07-01 12:20:26 +0200843
Loic Poulain1ab1f232015-08-27 07:21:51 +0200844 err = intel_wait_booting(hu);
845 if (err)
846 return err;
Loic Poulainca93cee2015-07-01 12:20:26 +0200847
Loic Poulain1ab1f232015-08-27 07:21:51 +0200848 clear_bit(STATE_BOOTING, &intel->flags);
Loic Poulainca93cee2015-07-01 12:20:26 +0200849
850 rettime = ktime_get();
851 delta = ktime_sub(rettime, calltime);
852 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
853
Loic Poulainf44e78a2015-08-31 18:34:30 +0200854 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200855
Loic Poulainb98469f2015-08-29 13:38:19 +0200856 /* Enable LPM if matching pdev with wakeup enabled */
Loic Poulain67c8bde2015-08-31 18:34:31 +0200857 mutex_lock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200858 list_for_each(p, &intel_device_list) {
859 struct intel_device *dev = list_entry(p, struct intel_device,
860 list);
861 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
862 if (device_may_wakeup(&dev->pdev->dev))
863 idev = dev;
864 break;
865 }
866 }
Loic Poulain67c8bde2015-08-31 18:34:31 +0200867 mutex_unlock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200868
869 if (!idev)
870 goto no_lpm;
871
Loic Poulainf44e78a2015-08-31 18:34:30 +0200872 bt_dev_info(hdev, "Enabling LPM");
Loic Poulainb98469f2015-08-29 13:38:19 +0200873
874 skb = __hci_cmd_sync(hdev, 0xfc8b, sizeof(lpm_param), lpm_param,
875 HCI_CMD_TIMEOUT);
876 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200877 bt_dev_err(hdev, "Failed to enable LPM");
Loic Poulainb98469f2015-08-29 13:38:19 +0200878 goto no_lpm;
879 }
880 kfree_skb(skb);
881
882 set_bit(STATE_LPM_ENABLED, &intel->flags);
883
884no_lpm:
Loic Poulainff289552015-08-25 17:55:44 +0200885 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
886 if (IS_ERR(skb))
887 return PTR_ERR(skb);
888 kfree_skb(skb);
889
890 if (speed_change) {
891 err = intel_set_baudrate(hu, oper_speed);
892 if (err)
893 return err;
894 }
895
Loic Poulainf44e78a2015-08-31 18:34:30 +0200896 bt_dev_info(hdev, "Setup complete");
Loic Poulainff289552015-08-25 17:55:44 +0200897
Loic Poulainca93cee2015-07-01 12:20:26 +0200898 clear_bit(STATE_BOOTLOADER, &intel->flags);
899
900 return 0;
901}
902
903static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
904{
905 struct hci_uart *hu = hci_get_drvdata(hdev);
906 struct intel_data *intel = hu->priv;
907 struct hci_event_hdr *hdr;
908
Loic Poulain1ab1f232015-08-27 07:21:51 +0200909 if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
910 !test_bit(STATE_BOOTING, &intel->flags))
Loic Poulainca93cee2015-07-01 12:20:26 +0200911 goto recv;
912
913 hdr = (void *)skb->data;
914
915 /* When the firmware loading completes the device sends
916 * out a vendor specific event indicating the result of
917 * the firmware loading.
918 */
919 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
920 skb->data[2] == 0x06) {
921 if (skb->data[3] != 0x00)
922 set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
923
924 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
925 test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
926 smp_mb__after_atomic();
927 wake_up_bit(&intel->flags, STATE_DOWNLOADING);
928 }
929
930 /* When switching to the operational firmware the device
931 * sends a vendor specific event indicating that the bootup
932 * completed.
933 */
934 } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
935 skb->data[2] == 0x02) {
936 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
937 smp_mb__after_atomic();
938 wake_up_bit(&intel->flags, STATE_BOOTING);
939 }
940 }
941recv:
942 return hci_recv_frame(hdev, skb);
943}
944
Loic Poulainb98469f2015-08-29 13:38:19 +0200945static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
946{
947 struct hci_uart *hu = hci_get_drvdata(hdev);
948 struct intel_data *intel = hu->priv;
949
Loic Poulainf44e78a2015-08-31 18:34:30 +0200950 bt_dev_dbg(hdev, "TX idle notification (%d)", value);
Loic Poulainb98469f2015-08-29 13:38:19 +0200951
952 if (value)
953 set_bit(STATE_TX_ACTIVE, &intel->flags);
954 else
955 clear_bit(STATE_TX_ACTIVE, &intel->flags);
956}
957
958static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
959{
960 struct hci_lpm_pkt *lpm = (void *)skb->data;
Loic Poulain89436542015-09-02 12:04:11 +0200961 struct hci_uart *hu = hci_get_drvdata(hdev);
962 struct intel_data *intel = hu->priv;
Loic Poulainb98469f2015-08-29 13:38:19 +0200963
964 switch (lpm->opcode) {
965 case LPM_OP_TX_NOTIFY:
966 if (lpm->dlen)
967 intel_recv_lpm_notify(hdev, lpm->data[0]);
968 break;
Loic Poulain89436542015-09-02 12:04:11 +0200969 case LPM_OP_SUSPEND_ACK:
970 set_bit(STATE_SUSPENDED, &intel->flags);
971 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
972 smp_mb__after_atomic();
973 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
974 }
975 break;
976 case LPM_OP_RESUME_ACK:
977 clear_bit(STATE_SUSPENDED, &intel->flags);
978 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
979 smp_mb__after_atomic();
980 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
981 }
982 break;
Loic Poulainb98469f2015-08-29 13:38:19 +0200983 default:
Loic Poulainf44e78a2015-08-31 18:34:30 +0200984 bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
Loic Poulainb98469f2015-08-29 13:38:19 +0200985 break;
986 }
987
988 kfree_skb(skb);
989
990 return 0;
991}
992
993#define INTEL_RECV_LPM \
994 .type = HCI_LPM_PKT, \
995 .hlen = HCI_LPM_HDR_SIZE, \
996 .loff = 1, \
997 .lsize = 1, \
998 .maxlen = HCI_LPM_MAX_SIZE
999
Loic Poulainca93cee2015-07-01 12:20:26 +02001000static const struct h4_recv_pkt intel_recv_pkts[] = {
Loic Poulainb98469f2015-08-29 13:38:19 +02001001 { H4_RECV_ACL, .recv = hci_recv_frame },
1002 { H4_RECV_SCO, .recv = hci_recv_frame },
1003 { H4_RECV_EVENT, .recv = intel_recv_event },
1004 { INTEL_RECV_LPM, .recv = intel_recv_lpm },
Loic Poulainca93cee2015-07-01 12:20:26 +02001005};
1006
1007static int intel_recv(struct hci_uart *hu, const void *data, int count)
1008{
1009 struct intel_data *intel = hu->priv;
1010
1011 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1012 return -EUNATCH;
1013
1014 intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
1015 intel_recv_pkts,
1016 ARRAY_SIZE(intel_recv_pkts));
1017 if (IS_ERR(intel->rx_skb)) {
1018 int err = PTR_ERR(intel->rx_skb);
Loic Poulainf44e78a2015-08-31 18:34:30 +02001019 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +02001020 intel->rx_skb = NULL;
1021 return err;
1022 }
1023
1024 return count;
1025}
1026
1027static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
1028{
1029 struct intel_data *intel = hu->priv;
1030
1031 BT_DBG("hu %p skb %p", hu, skb);
1032
1033 skb_queue_tail(&intel->txq, skb);
1034
1035 return 0;
1036}
1037
1038static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1039{
1040 struct intel_data *intel = hu->priv;
1041 struct sk_buff *skb;
1042
1043 skb = skb_dequeue(&intel->txq);
1044 if (!skb)
1045 return skb;
1046
1047 if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
1048 (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) {
1049 struct hci_command_hdr *cmd = (void *)skb->data;
1050 __u16 opcode = le16_to_cpu(cmd->opcode);
1051
1052 /* When the 0xfc01 command is issued to boot into
1053 * the operational firmware, it will actually not
1054 * send a command complete event. To keep the flow
1055 * control working inject that event here.
1056 */
1057 if (opcode == 0xfc01)
1058 inject_cmd_complete(hu->hdev, opcode);
1059 }
1060
1061 /* Prepend skb with frame type */
1062 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
1063
1064 return skb;
1065}
1066
1067static const struct hci_uart_proto intel_proto = {
1068 .id = HCI_UART_INTEL,
1069 .name = "Intel",
1070 .init_speed = 115200,
Loic Poulainff289552015-08-25 17:55:44 +02001071 .oper_speed = 3000000,
Loic Poulainca93cee2015-07-01 12:20:26 +02001072 .open = intel_open,
1073 .close = intel_close,
1074 .flush = intel_flush,
1075 .setup = intel_setup,
Loic Poulainff289552015-08-25 17:55:44 +02001076 .set_baudrate = intel_set_baudrate,
Loic Poulainca93cee2015-07-01 12:20:26 +02001077 .recv = intel_recv,
1078 .enqueue = intel_enqueue,
1079 .dequeue = intel_dequeue,
1080};
1081
Loic Poulain1ab1f232015-08-27 07:21:51 +02001082#ifdef CONFIG_ACPI
1083static const struct acpi_device_id intel_acpi_match[] = {
1084 { "INT33E1", 0 },
1085 { },
1086};
1087MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
1088
1089static int intel_acpi_probe(struct intel_device *idev)
1090{
1091 const struct acpi_device_id *id;
1092
1093 id = acpi_match_device(intel_acpi_match, &idev->pdev->dev);
1094 if (!id)
1095 return -ENODEV;
1096
1097 return 0;
1098}
1099#else
1100static int intel_acpi_probe(struct intel_device *idev)
1101{
1102 return -ENODEV;
1103}
1104#endif
1105
Loic Poulainaa6802d2015-09-02 12:04:12 +02001106#ifdef CONFIG_PM_SLEEP
1107static int intel_suspend(struct device *dev)
1108{
1109 struct intel_device *idev = dev_get_drvdata(dev);
1110
1111 dev_dbg(dev, "intel_suspend");
1112
1113 mutex_lock(&idev->hu_lock);
1114 if (idev->hu)
1115 intel_lpm_suspend(idev->hu);
1116 mutex_unlock(&idev->hu_lock);
1117
1118 return 0;
1119}
1120
1121static int intel_resume(struct device *dev)
1122{
1123 struct intel_device *idev = dev_get_drvdata(dev);
1124
1125 dev_dbg(dev, "intel_resume");
1126
1127 mutex_lock(&idev->hu_lock);
1128 if (idev->hu)
1129 intel_lpm_resume(idev->hu);
1130 mutex_unlock(&idev->hu_lock);
1131
1132 return 0;
1133}
1134#endif
1135
1136static const struct dev_pm_ops intel_pm_ops = {
1137 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
1138};
1139
Loic Poulain1ab1f232015-08-27 07:21:51 +02001140static int intel_probe(struct platform_device *pdev)
1141{
1142 struct intel_device *idev;
1143
1144 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
1145 if (!idev)
1146 return -ENOMEM;
1147
Loic Poulainaa6802d2015-09-02 12:04:12 +02001148 mutex_init(&idev->hu_lock);
1149
Loic Poulain1ab1f232015-08-27 07:21:51 +02001150 idev->pdev = pdev;
1151
1152 if (ACPI_HANDLE(&pdev->dev)) {
1153 int err = intel_acpi_probe(idev);
1154 if (err)
1155 return err;
1156 } else {
1157 return -ENODEV;
1158 }
1159
1160 idev->reset = devm_gpiod_get_optional(&pdev->dev, "reset",
1161 GPIOD_OUT_LOW);
1162 if (IS_ERR(idev->reset)) {
1163 dev_err(&pdev->dev, "Unable to retrieve gpio\n");
1164 return PTR_ERR(idev->reset);
1165 }
1166
Loic Poulain765ea3a2015-08-29 13:38:18 +02001167 idev->irq = platform_get_irq(pdev, 0);
1168 if (idev->irq < 0) {
1169 struct gpio_desc *host_wake;
1170
1171 dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1172
1173 host_wake = devm_gpiod_get_optional(&pdev->dev, "host-wake",
1174 GPIOD_IN);
1175 if (IS_ERR(host_wake)) {
1176 dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1177 goto no_irq;
1178 }
1179
1180 idev->irq = gpiod_to_irq(host_wake);
1181 if (idev->irq < 0) {
1182 dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1183 goto no_irq;
1184 }
1185 }
1186
1187 /* Only enable wake-up/irq when controller is powered */
1188 device_set_wakeup_capable(&pdev->dev, true);
1189 device_wakeup_disable(&pdev->dev);
1190
1191no_irq:
Loic Poulain1ab1f232015-08-27 07:21:51 +02001192 platform_set_drvdata(pdev, idev);
1193
1194 /* Place this instance on the device list */
Loic Poulain67c8bde2015-08-31 18:34:31 +02001195 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001196 list_add_tail(&idev->list, &intel_device_list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001197 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001198
Loic Poulain765ea3a2015-08-29 13:38:18 +02001199 dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1200 desc_to_gpio(idev->reset), idev->irq);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001201
1202 return 0;
1203}
1204
1205static int intel_remove(struct platform_device *pdev)
1206{
1207 struct intel_device *idev = platform_get_drvdata(pdev);
1208
Loic Poulain765ea3a2015-08-29 13:38:18 +02001209 device_wakeup_disable(&pdev->dev);
1210
Loic Poulain67c8bde2015-08-31 18:34:31 +02001211 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001212 list_del(&idev->list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001213 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001214
1215 dev_info(&pdev->dev, "unregistered.\n");
1216
1217 return 0;
1218}
1219
1220static struct platform_driver intel_driver = {
1221 .probe = intel_probe,
1222 .remove = intel_remove,
1223 .driver = {
1224 .name = "hci_intel",
1225 .acpi_match_table = ACPI_PTR(intel_acpi_match),
Loic Poulainaa6802d2015-09-02 12:04:12 +02001226 .pm = &intel_pm_ops,
Loic Poulain1ab1f232015-08-27 07:21:51 +02001227 },
1228};
1229
Loic Poulainca93cee2015-07-01 12:20:26 +02001230int __init intel_init(void)
1231{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001232 platform_driver_register(&intel_driver);
1233
Loic Poulainca93cee2015-07-01 12:20:26 +02001234 return hci_uart_register_proto(&intel_proto);
1235}
1236
1237int __exit intel_deinit(void)
1238{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001239 platform_driver_unregister(&intel_driver);
1240
Loic Poulainca93cee2015-07-01 12:20:26 +02001241 return hci_uart_unregister_proto(&intel_proto);
1242}