blob: 2e15ca5c1fd750bd506fea8ce03cd9425cae0635 [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 Poulain765ea3a2015-08-29 13:38:18 +020071 int irq;
Loic Poulain1ab1f232015-08-27 07:21:51 +020072};
73
74static LIST_HEAD(intel_device_list);
Loic Poulain67c8bde2015-08-31 18:34:31 +020075static DEFINE_MUTEX(intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +020076
Loic Poulainca93cee2015-07-01 12:20:26 +020077struct intel_data {
78 struct sk_buff *rx_skb;
79 struct sk_buff_head txq;
80 unsigned long flags;
81};
82
Loic Poulainff289552015-08-25 17:55:44 +020083static u8 intel_convert_speed(unsigned int speed)
84{
85 switch (speed) {
86 case 9600:
87 return 0x00;
88 case 19200:
89 return 0x01;
90 case 38400:
91 return 0x02;
92 case 57600:
93 return 0x03;
94 case 115200:
95 return 0x04;
96 case 230400:
97 return 0x05;
98 case 460800:
99 return 0x06;
100 case 921600:
101 return 0x07;
102 case 1843200:
103 return 0x08;
104 case 3250000:
105 return 0x09;
106 case 2000000:
107 return 0x0a;
108 case 3000000:
109 return 0x0b;
110 default:
111 return 0xff;
112 }
113}
114
Loic Poulain1ab1f232015-08-27 07:21:51 +0200115static int intel_wait_booting(struct hci_uart *hu)
116{
117 struct intel_data *intel = hu->priv;
118 int err;
119
120 err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
121 TASK_INTERRUPTIBLE,
122 msecs_to_jiffies(1000));
123
124 if (err == 1) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200125 bt_dev_err(hu->hdev, "Device boot interrupted");
Loic Poulain1ab1f232015-08-27 07:21:51 +0200126 return -EINTR;
127 }
128
129 if (err) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200130 bt_dev_err(hu->hdev, "Device boot timeout");
Loic Poulain1ab1f232015-08-27 07:21:51 +0200131 return -ETIMEDOUT;
132 }
133
134 return err;
135}
136
Loic Poulain89436542015-09-02 12:04:11 +0200137static int intel_wait_lpm_transaction(struct hci_uart *hu)
138{
139 struct intel_data *intel = hu->priv;
140 int err;
141
142 err = wait_on_bit_timeout(&intel->flags, STATE_LPM_TRANSACTION,
143 TASK_INTERRUPTIBLE,
144 msecs_to_jiffies(1000));
145
146 if (err == 1) {
147 bt_dev_err(hu->hdev, "LPM transaction interrupted");
148 return -EINTR;
149 }
150
151 if (err) {
152 bt_dev_err(hu->hdev, "LPM transaction timeout");
153 return -ETIMEDOUT;
154 }
155
156 return err;
157}
158
159static int intel_lpm_suspend(struct hci_uart *hu)
160{
161 static const u8 suspend[] = { 0x01, 0x01, 0x01 };
162 struct intel_data *intel = hu->priv;
163 struct sk_buff *skb;
164
165 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
166 test_bit(STATE_SUSPENDED, &intel->flags))
167 return 0;
168
169 if (test_bit(STATE_TX_ACTIVE, &intel->flags))
170 return -EAGAIN;
171
172 bt_dev_dbg(hu->hdev, "Suspending");
173
174 skb = bt_skb_alloc(sizeof(suspend), GFP_KERNEL);
175 if (!skb) {
176 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
177 return -ENOMEM;
178 }
179
180 memcpy(skb_put(skb, sizeof(suspend)), suspend, sizeof(suspend));
181 bt_cb(skb)->pkt_type = HCI_LPM_PKT;
182
183 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
184
185 skb_queue_tail(&intel->txq, skb);
186 hci_uart_tx_wakeup(hu);
187
188 intel_wait_lpm_transaction(hu);
189 /* Even in case of failure, continue and test the suspended flag */
190
191 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
192
193 if (!test_bit(STATE_SUSPENDED, &intel->flags)) {
194 bt_dev_err(hu->hdev, "Device suspend error");
195 return -EINVAL;
196 }
197
198 bt_dev_dbg(hu->hdev, "Suspended");
199
200 hci_uart_set_flow_control(hu, true);
201
202 return 0;
203}
204
205static int intel_lpm_resume(struct hci_uart *hu)
206{
207 struct intel_data *intel = hu->priv;
208 struct sk_buff *skb;
209
210 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
211 !test_bit(STATE_SUSPENDED, &intel->flags))
212 return 0;
213
214 bt_dev_dbg(hu->hdev, "Resuming");
215
216 hci_uart_set_flow_control(hu, false);
217
218 skb = bt_skb_alloc(0, GFP_KERNEL);
219 if (!skb) {
220 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
221 return -ENOMEM;
222 }
223
224 bt_cb(skb)->pkt_type = HCI_LPM_WAKE_PKT;
225
226 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
227
228 skb_queue_tail(&intel->txq, skb);
229 hci_uart_tx_wakeup(hu);
230
231 intel_wait_lpm_transaction(hu);
232 /* Even in case of failure, continue and test the suspended flag */
233
234 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
235
236 if (test_bit(STATE_SUSPENDED, &intel->flags)) {
237 bt_dev_err(hu->hdev, "Device resume error");
238 return -EINVAL;
239 }
240
241 bt_dev_dbg(hu->hdev, "Resumed");
242
243 return 0;
244}
245
246static int intel_lpm_host_wake(struct hci_uart *hu)
247{
248 static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 };
249 struct intel_data *intel = hu->priv;
250 struct sk_buff *skb;
251
252 hci_uart_set_flow_control(hu, false);
253
254 clear_bit(STATE_SUSPENDED, &intel->flags);
255
256 skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL);
257 if (!skb) {
258 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
259 return -ENOMEM;
260 }
261
262 memcpy(skb_put(skb, sizeof(lpm_resume_ack)), lpm_resume_ack,
263 sizeof(lpm_resume_ack));
264 bt_cb(skb)->pkt_type = HCI_LPM_PKT;
265
266 skb_queue_tail(&intel->txq, skb);
267 hci_uart_tx_wakeup(hu);
268
269 bt_dev_dbg(hu->hdev, "Resumed by controller");
270
271 return 0;
272}
273
Loic Poulain765ea3a2015-08-29 13:38:18 +0200274static irqreturn_t intel_irq(int irq, void *dev_id)
275{
276 struct intel_device *idev = dev_id;
277
278 dev_info(&idev->pdev->dev, "hci_intel irq\n");
279
280 return IRQ_HANDLED;
281}
282
Loic Poulain1ab1f232015-08-27 07:21:51 +0200283static int intel_set_power(struct hci_uart *hu, bool powered)
284{
285 struct list_head *p;
286 int err = -ENODEV;
287
Loic Poulain67c8bde2015-08-31 18:34:31 +0200288 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200289
290 list_for_each(p, &intel_device_list) {
291 struct intel_device *idev = list_entry(p, struct intel_device,
292 list);
293
294 /* tty device and pdev device should share the same parent
295 * which is the UART port.
296 */
297 if (hu->tty->dev->parent != idev->pdev->dev.parent)
298 continue;
299
300 if (!idev->reset) {
301 err = -ENOTSUPP;
302 break;
303 }
304
305 BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
306 hu, dev_name(&idev->pdev->dev), powered);
307
308 gpiod_set_value(idev->reset, powered);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200309
310 if (idev->irq < 0)
311 break;
312
313 if (powered && device_can_wakeup(&idev->pdev->dev)) {
314 err = devm_request_threaded_irq(&idev->pdev->dev,
315 idev->irq, NULL,
316 intel_irq,
317 IRQF_ONESHOT,
318 "bt-host-wake", idev);
319 if (err) {
320 BT_ERR("hu %p, unable to allocate irq-%d",
321 hu, idev->irq);
322 break;
323 }
324
325 device_wakeup_enable(&idev->pdev->dev);
326 } else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
327 devm_free_irq(&idev->pdev->dev, idev->irq, idev);
328 device_wakeup_disable(&idev->pdev->dev);
329 }
Loic Poulain1ab1f232015-08-27 07:21:51 +0200330 }
331
Loic Poulain67c8bde2015-08-31 18:34:31 +0200332 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200333
334 return err;
335}
336
Loic Poulainca93cee2015-07-01 12:20:26 +0200337static int intel_open(struct hci_uart *hu)
338{
339 struct intel_data *intel;
340
341 BT_DBG("hu %p", hu);
342
343 intel = kzalloc(sizeof(*intel), GFP_KERNEL);
344 if (!intel)
345 return -ENOMEM;
346
347 skb_queue_head_init(&intel->txq);
348
349 hu->priv = intel;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200350
351 if (!intel_set_power(hu, true))
352 set_bit(STATE_BOOTING, &intel->flags);
353
Loic Poulainca93cee2015-07-01 12:20:26 +0200354 return 0;
355}
356
357static int intel_close(struct hci_uart *hu)
358{
359 struct intel_data *intel = hu->priv;
360
361 BT_DBG("hu %p", hu);
362
Loic Poulain1ab1f232015-08-27 07:21:51 +0200363 intel_set_power(hu, false);
364
Loic Poulainca93cee2015-07-01 12:20:26 +0200365 skb_queue_purge(&intel->txq);
366 kfree_skb(intel->rx_skb);
367 kfree(intel);
368
369 hu->priv = NULL;
370 return 0;
371}
372
373static int intel_flush(struct hci_uart *hu)
374{
375 struct intel_data *intel = hu->priv;
376
377 BT_DBG("hu %p", hu);
378
379 skb_queue_purge(&intel->txq);
380
381 return 0;
382}
383
384static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
385{
386 struct sk_buff *skb;
387 struct hci_event_hdr *hdr;
388 struct hci_ev_cmd_complete *evt;
389
390 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
391 if (!skb)
392 return -ENOMEM;
393
394 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
395 hdr->evt = HCI_EV_CMD_COMPLETE;
396 hdr->plen = sizeof(*evt) + 1;
397
398 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
399 evt->ncmd = 0x01;
400 evt->opcode = cpu_to_le16(opcode);
401
402 *skb_put(skb, 1) = 0x00;
403
404 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
405
406 return hci_recv_frame(hdev, skb);
407}
408
Loic Poulainff289552015-08-25 17:55:44 +0200409static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
410{
411 struct intel_data *intel = hu->priv;
412 struct hci_dev *hdev = hu->hdev;
413 u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
414 struct sk_buff *skb;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200415 int err;
416
417 /* This can be the first command sent to the chip, check
418 * that the controller is ready.
419 */
420 err = intel_wait_booting(hu);
421
422 clear_bit(STATE_BOOTING, &intel->flags);
423
424 /* In case of timeout, try to continue anyway */
425 if (err && err != ETIMEDOUT)
426 return err;
Loic Poulainff289552015-08-25 17:55:44 +0200427
Loic Poulainf44e78a2015-08-31 18:34:30 +0200428 bt_dev_info(hdev, "Change controller speed to %d", speed);
Loic Poulainff289552015-08-25 17:55:44 +0200429
430 speed_cmd[3] = intel_convert_speed(speed);
431 if (speed_cmd[3] == 0xff) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200432 bt_dev_err(hdev, "Unsupported speed");
Loic Poulainff289552015-08-25 17:55:44 +0200433 return -EINVAL;
434 }
435
436 /* Device will not accept speed change if Intel version has not been
437 * previously requested.
438 */
439 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
440 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200441 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
442 PTR_ERR(skb));
Loic Poulainff289552015-08-25 17:55:44 +0200443 return PTR_ERR(skb);
444 }
445 kfree_skb(skb);
446
447 skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
448 if (!skb) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200449 bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
Loic Poulainff289552015-08-25 17:55:44 +0200450 return -ENOMEM;
451 }
452
453 memcpy(skb_put(skb, sizeof(speed_cmd)), speed_cmd, sizeof(speed_cmd));
454 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
455
456 hci_uart_set_flow_control(hu, true);
457
458 skb_queue_tail(&intel->txq, skb);
459 hci_uart_tx_wakeup(hu);
460
461 /* wait 100ms to change baudrate on controller side */
462 msleep(100);
463
464 hci_uart_set_baudrate(hu, speed);
465 hci_uart_set_flow_control(hu, false);
466
467 return 0;
468}
469
Loic Poulainca93cee2015-07-01 12:20:26 +0200470static int intel_setup(struct hci_uart *hu)
471{
472 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
473 0x00, 0x08, 0x04, 0x00 };
Loic Poulainb98469f2015-08-29 13:38:19 +0200474 static const u8 lpm_param[] = { 0x03, 0x07, 0x01, 0x0b };
Loic Poulainca93cee2015-07-01 12:20:26 +0200475 struct intel_data *intel = hu->priv;
Loic Poulainb98469f2015-08-29 13:38:19 +0200476 struct intel_device *idev = NULL;
Loic Poulainca93cee2015-07-01 12:20:26 +0200477 struct hci_dev *hdev = hu->hdev;
478 struct sk_buff *skb;
479 struct intel_version *ver;
480 struct intel_boot_params *params;
Loic Poulainb98469f2015-08-29 13:38:19 +0200481 struct list_head *p;
Loic Poulainca93cee2015-07-01 12:20:26 +0200482 const struct firmware *fw;
483 const u8 *fw_ptr;
484 char fwname[64];
485 u32 frag_len;
486 ktime_t calltime, delta, rettime;
487 unsigned long long duration;
Loic Poulainff289552015-08-25 17:55:44 +0200488 unsigned int init_speed, oper_speed;
489 int speed_change = 0;
Loic Poulainca93cee2015-07-01 12:20:26 +0200490 int err;
491
Loic Poulainf44e78a2015-08-31 18:34:30 +0200492 bt_dev_dbg(hdev, "start intel_setup");
Loic Poulainca93cee2015-07-01 12:20:26 +0200493
Marcel Holtmann35ab8152015-07-05 14:37:40 +0200494 hu->hdev->set_bdaddr = btintel_set_bdaddr;
495
Loic Poulainca93cee2015-07-01 12:20:26 +0200496 calltime = ktime_get();
497
Loic Poulainff289552015-08-25 17:55:44 +0200498 if (hu->init_speed)
499 init_speed = hu->init_speed;
500 else
501 init_speed = hu->proto->init_speed;
502
503 if (hu->oper_speed)
504 oper_speed = hu->oper_speed;
505 else
506 oper_speed = hu->proto->oper_speed;
507
508 if (oper_speed && init_speed && oper_speed != init_speed)
509 speed_change = 1;
510
Loic Poulain1ab1f232015-08-27 07:21:51 +0200511 /* Check that the controller is ready */
512 err = intel_wait_booting(hu);
513
514 clear_bit(STATE_BOOTING, &intel->flags);
515
516 /* In case of timeout, try to continue anyway */
517 if (err && err != ETIMEDOUT)
518 return err;
519
Loic Poulainca93cee2015-07-01 12:20:26 +0200520 set_bit(STATE_BOOTLOADER, &intel->flags);
521
522 /* Read the Intel version information to determine if the device
523 * is in bootloader mode or if it already has operational firmware
524 * loaded.
525 */
526 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
527 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200528 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
529 PTR_ERR(skb));
Loic Poulainca93cee2015-07-01 12:20:26 +0200530 return PTR_ERR(skb);
531 }
532
533 if (skb->len != sizeof(*ver)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200534 bt_dev_err(hdev, "Intel version event size mismatch");
Loic Poulainca93cee2015-07-01 12:20:26 +0200535 kfree_skb(skb);
536 return -EILSEQ;
537 }
538
539 ver = (struct intel_version *)skb->data;
540 if (ver->status) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200541 bt_dev_err(hdev, "Intel version command failure (%02x)",
542 ver->status);
Loic Poulainca93cee2015-07-01 12:20:26 +0200543 err = -bt_to_errno(ver->status);
544 kfree_skb(skb);
545 return err;
546 }
547
548 /* The hardware platform number has a fixed value of 0x37 and
549 * for now only accept this single value.
550 */
551 if (ver->hw_platform != 0x37) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200552 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
553 ver->hw_platform);
Loic Poulainca93cee2015-07-01 12:20:26 +0200554 kfree_skb(skb);
555 return -EINVAL;
556 }
557
558 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
559 * supported by this firmware loading method. This check has been
560 * put in place to ensure correct forward compatibility options
561 * when newer hardware variants come along.
562 */
563 if (ver->hw_variant != 0x0b) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200564 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
565 ver->hw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200566 kfree_skb(skb);
567 return -EINVAL;
568 }
569
Marcel Holtmann7feb99e2015-07-05 15:02:07 +0200570 btintel_version_info(hdev, ver);
Loic Poulainca93cee2015-07-01 12:20:26 +0200571
572 /* The firmware variant determines if the device is in bootloader
573 * mode or is running operational firmware. The value 0x06 identifies
574 * the bootloader and the value 0x23 identifies the operational
575 * firmware.
576 *
577 * When the operational firmware is already present, then only
578 * the check for valid Bluetooth device address is needed. This
579 * determines if the device will be added as configured or
580 * unconfigured controller.
581 *
582 * It is not possible to use the Secure Boot Parameters in this
583 * case since that command is only available in bootloader mode.
584 */
585 if (ver->fw_variant == 0x23) {
586 kfree_skb(skb);
587 clear_bit(STATE_BOOTLOADER, &intel->flags);
588 btintel_check_bdaddr(hdev);
589 return 0;
590 }
591
592 /* If the device is not in bootloader mode, then the only possible
593 * choice is to return an error and abort the device initialization.
594 */
595 if (ver->fw_variant != 0x06) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200596 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
597 ver->fw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200598 kfree_skb(skb);
599 return -ENODEV;
600 }
601
602 kfree_skb(skb);
603
604 /* Read the secure boot parameters to identify the operating
605 * details of the bootloader.
606 */
607 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
608 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200609 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
610 PTR_ERR(skb));
Loic Poulainca93cee2015-07-01 12:20:26 +0200611 return PTR_ERR(skb);
612 }
613
614 if (skb->len != sizeof(*params)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200615 bt_dev_err(hdev, "Intel boot parameters size mismatch");
Loic Poulainca93cee2015-07-01 12:20:26 +0200616 kfree_skb(skb);
617 return -EILSEQ;
618 }
619
620 params = (struct intel_boot_params *)skb->data;
621 if (params->status) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200622 bt_dev_err(hdev, "Intel boot parameters command failure (%02x)",
623 params->status);
Loic Poulainca93cee2015-07-01 12:20:26 +0200624 err = -bt_to_errno(params->status);
625 kfree_skb(skb);
626 return err;
627 }
628
Loic Poulainf44e78a2015-08-31 18:34:30 +0200629 bt_dev_info(hdev, "Device revision is %u",
630 le16_to_cpu(params->dev_revid));
Loic Poulainca93cee2015-07-01 12:20:26 +0200631
Loic Poulainf44e78a2015-08-31 18:34:30 +0200632 bt_dev_info(hdev, "Secure boot is %s",
633 params->secure_boot ? "enabled" : "disabled");
Loic Poulainca93cee2015-07-01 12:20:26 +0200634
Loic Poulainf44e78a2015-08-31 18:34:30 +0200635 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
Loic Poulainca93cee2015-07-01 12:20:26 +0200636 params->min_fw_build_nn, params->min_fw_build_cw,
637 2000 + params->min_fw_build_yy);
638
639 /* It is required that every single firmware fragment is acknowledged
640 * with a command complete event. If the boot parameters indicate
641 * that this bootloader does not send them, then abort the setup.
642 */
643 if (params->limited_cce != 0x00) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200644 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
645 params->limited_cce);
Loic Poulainca93cee2015-07-01 12:20:26 +0200646 kfree_skb(skb);
647 return -EINVAL;
648 }
649
650 /* If the OTP has no valid Bluetooth device address, then there will
651 * also be no valid address for the operational firmware.
652 */
653 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200654 bt_dev_info(hdev, "No device address configured");
Loic Poulainca93cee2015-07-01 12:20:26 +0200655 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
656 }
657
658 /* With this Intel bootloader only the hardware variant and device
659 * revision information are used to select the right firmware.
660 *
661 * Currently this bootloader support is limited to hardware variant
662 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
663 */
664 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
665 le16_to_cpu(params->dev_revid));
666
667 err = request_firmware(&fw, fwname, &hdev->dev);
668 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200669 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
670 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200671 kfree_skb(skb);
672 return err;
673 }
674
Loic Poulainf44e78a2015-08-31 18:34:30 +0200675 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Loic Poulainca93cee2015-07-01 12:20:26 +0200676
677 kfree_skb(skb);
678
679 if (fw->size < 644) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200680 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
681 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200682 err = -EBADF;
683 goto done;
684 }
685
686 set_bit(STATE_DOWNLOADING, &intel->flags);
687
688 /* Start the firmware download transaction with the Init fragment
689 * represented by the 128 bytes of CSS header.
690 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200691 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Loic Poulainca93cee2015-07-01 12:20:26 +0200692 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200693 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200694 goto done;
695 }
696
697 /* Send the 256 bytes of public key information from the firmware
698 * as the PKey fragment.
699 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200700 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Loic Poulainca93cee2015-07-01 12:20:26 +0200701 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200702 bt_dev_err(hdev, "Failed to send firmware public key (%d)",
703 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200704 goto done;
705 }
706
707 /* Send the 256 bytes of signature information from the firmware
708 * as the Sign fragment.
709 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200710 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Loic Poulainca93cee2015-07-01 12:20:26 +0200711 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200712 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
713 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200714 goto done;
715 }
716
717 fw_ptr = fw->data + 644;
718 frag_len = 0;
719
720 while (fw_ptr - fw->data < fw->size) {
721 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
722
723 frag_len += sizeof(*cmd) + cmd->plen;
724
Loic Poulainf44e78a2015-08-31 18:34:30 +0200725 bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data),
726 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200727
728 /* The parameter length of the secure send command requires
729 * a 4 byte alignment. It happens so that the firmware file
730 * contains proper Intel_NOP commands to align the fragments
731 * as needed.
732 *
733 * Send set of commands with 4 byte alignment from the
734 * firmware data buffer as a single Data fragement.
735 */
736 if (frag_len % 4)
737 continue;
738
739 /* Send each command from the firmware data buffer as
740 * a single Data fragment.
741 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200742 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Loic Poulainca93cee2015-07-01 12:20:26 +0200743 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200744 bt_dev_err(hdev, "Failed to send firmware data (%d)",
745 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200746 goto done;
747 }
748
749 fw_ptr += frag_len;
750 frag_len = 0;
751 }
752
753 set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
754
Loic Poulainf44e78a2015-08-31 18:34:30 +0200755 bt_dev_info(hdev, "Waiting for firmware download to complete");
Loic Poulainca93cee2015-07-01 12:20:26 +0200756
757 /* Before switching the device into operational mode and with that
758 * booting the loaded firmware, wait for the bootloader notification
759 * that all fragments have been successfully received.
760 *
761 * When the event processing receives the notification, then the
762 * STATE_DOWNLOADING flag will be cleared.
763 *
764 * The firmware loading should not take longer than 5 seconds
765 * and thus just timeout if that happens and fail the setup
766 * of this device.
767 */
768 err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
769 TASK_INTERRUPTIBLE,
770 msecs_to_jiffies(5000));
771 if (err == 1) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200772 bt_dev_err(hdev, "Firmware loading interrupted");
Loic Poulainca93cee2015-07-01 12:20:26 +0200773 err = -EINTR;
774 goto done;
775 }
776
777 if (err) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200778 bt_dev_err(hdev, "Firmware loading timeout");
Loic Poulainca93cee2015-07-01 12:20:26 +0200779 err = -ETIMEDOUT;
780 goto done;
781 }
782
783 if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200784 bt_dev_err(hdev, "Firmware loading failed");
Loic Poulainca93cee2015-07-01 12:20:26 +0200785 err = -ENOEXEC;
786 goto done;
787 }
788
789 rettime = ktime_get();
790 delta = ktime_sub(rettime, calltime);
791 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
792
Loic Poulainf44e78a2015-08-31 18:34:30 +0200793 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200794
795done:
796 release_firmware(fw);
797
798 if (err < 0)
799 return err;
800
Loic Poulainff289552015-08-25 17:55:44 +0200801 /* We need to restore the default speed before Intel reset */
802 if (speed_change) {
803 err = intel_set_baudrate(hu, init_speed);
804 if (err)
805 return err;
806 }
807
Loic Poulainca93cee2015-07-01 12:20:26 +0200808 calltime = ktime_get();
809
810 set_bit(STATE_BOOTING, &intel->flags);
811
812 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
813 HCI_INIT_TIMEOUT);
814 if (IS_ERR(skb))
815 return PTR_ERR(skb);
816
817 kfree_skb(skb);
818
819 /* The bootloader will not indicate when the device is ready. This
820 * is done by the operational firmware sending bootup notification.
821 *
822 * Booting into operational firmware should not take longer than
823 * 1 second. However if that happens, then just fail the setup
824 * since something went wrong.
825 */
Loic Poulainf44e78a2015-08-31 18:34:30 +0200826 bt_dev_info(hdev, "Waiting for device to boot");
Loic Poulainca93cee2015-07-01 12:20:26 +0200827
Loic Poulain1ab1f232015-08-27 07:21:51 +0200828 err = intel_wait_booting(hu);
829 if (err)
830 return err;
Loic Poulainca93cee2015-07-01 12:20:26 +0200831
Loic Poulain1ab1f232015-08-27 07:21:51 +0200832 clear_bit(STATE_BOOTING, &intel->flags);
Loic Poulainca93cee2015-07-01 12:20:26 +0200833
834 rettime = ktime_get();
835 delta = ktime_sub(rettime, calltime);
836 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
837
Loic Poulainf44e78a2015-08-31 18:34:30 +0200838 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200839
Loic Poulainb98469f2015-08-29 13:38:19 +0200840 /* Enable LPM if matching pdev with wakeup enabled */
Loic Poulain67c8bde2015-08-31 18:34:31 +0200841 mutex_lock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200842 list_for_each(p, &intel_device_list) {
843 struct intel_device *dev = list_entry(p, struct intel_device,
844 list);
845 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
846 if (device_may_wakeup(&dev->pdev->dev))
847 idev = dev;
848 break;
849 }
850 }
Loic Poulain67c8bde2015-08-31 18:34:31 +0200851 mutex_unlock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200852
853 if (!idev)
854 goto no_lpm;
855
Loic Poulainf44e78a2015-08-31 18:34:30 +0200856 bt_dev_info(hdev, "Enabling LPM");
Loic Poulainb98469f2015-08-29 13:38:19 +0200857
858 skb = __hci_cmd_sync(hdev, 0xfc8b, sizeof(lpm_param), lpm_param,
859 HCI_CMD_TIMEOUT);
860 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200861 bt_dev_err(hdev, "Failed to enable LPM");
Loic Poulainb98469f2015-08-29 13:38:19 +0200862 goto no_lpm;
863 }
864 kfree_skb(skb);
865
866 set_bit(STATE_LPM_ENABLED, &intel->flags);
867
868no_lpm:
Loic Poulainff289552015-08-25 17:55:44 +0200869 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
870 if (IS_ERR(skb))
871 return PTR_ERR(skb);
872 kfree_skb(skb);
873
874 if (speed_change) {
875 err = intel_set_baudrate(hu, oper_speed);
876 if (err)
877 return err;
878 }
879
Loic Poulainf44e78a2015-08-31 18:34:30 +0200880 bt_dev_info(hdev, "Setup complete");
Loic Poulainff289552015-08-25 17:55:44 +0200881
Loic Poulainca93cee2015-07-01 12:20:26 +0200882 clear_bit(STATE_BOOTLOADER, &intel->flags);
883
884 return 0;
885}
886
887static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
888{
889 struct hci_uart *hu = hci_get_drvdata(hdev);
890 struct intel_data *intel = hu->priv;
891 struct hci_event_hdr *hdr;
892
Loic Poulain1ab1f232015-08-27 07:21:51 +0200893 if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
894 !test_bit(STATE_BOOTING, &intel->flags))
Loic Poulainca93cee2015-07-01 12:20:26 +0200895 goto recv;
896
897 hdr = (void *)skb->data;
898
899 /* When the firmware loading completes the device sends
900 * out a vendor specific event indicating the result of
901 * the firmware loading.
902 */
903 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
904 skb->data[2] == 0x06) {
905 if (skb->data[3] != 0x00)
906 set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
907
908 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
909 test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
910 smp_mb__after_atomic();
911 wake_up_bit(&intel->flags, STATE_DOWNLOADING);
912 }
913
914 /* When switching to the operational firmware the device
915 * sends a vendor specific event indicating that the bootup
916 * completed.
917 */
918 } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
919 skb->data[2] == 0x02) {
920 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
921 smp_mb__after_atomic();
922 wake_up_bit(&intel->flags, STATE_BOOTING);
923 }
924 }
925recv:
926 return hci_recv_frame(hdev, skb);
927}
928
Loic Poulainb98469f2015-08-29 13:38:19 +0200929static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
930{
931 struct hci_uart *hu = hci_get_drvdata(hdev);
932 struct intel_data *intel = hu->priv;
933
Loic Poulainf44e78a2015-08-31 18:34:30 +0200934 bt_dev_dbg(hdev, "TX idle notification (%d)", value);
Loic Poulainb98469f2015-08-29 13:38:19 +0200935
936 if (value)
937 set_bit(STATE_TX_ACTIVE, &intel->flags);
938 else
939 clear_bit(STATE_TX_ACTIVE, &intel->flags);
940}
941
942static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
943{
944 struct hci_lpm_pkt *lpm = (void *)skb->data;
Loic Poulain89436542015-09-02 12:04:11 +0200945 struct hci_uart *hu = hci_get_drvdata(hdev);
946 struct intel_data *intel = hu->priv;
Loic Poulainb98469f2015-08-29 13:38:19 +0200947
948 switch (lpm->opcode) {
949 case LPM_OP_TX_NOTIFY:
950 if (lpm->dlen)
951 intel_recv_lpm_notify(hdev, lpm->data[0]);
952 break;
Loic Poulain89436542015-09-02 12:04:11 +0200953 case LPM_OP_SUSPEND_ACK:
954 set_bit(STATE_SUSPENDED, &intel->flags);
955 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
956 smp_mb__after_atomic();
957 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
958 }
959 break;
960 case LPM_OP_RESUME_ACK:
961 clear_bit(STATE_SUSPENDED, &intel->flags);
962 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
963 smp_mb__after_atomic();
964 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
965 }
966 break;
Loic Poulainb98469f2015-08-29 13:38:19 +0200967 default:
Loic Poulainf44e78a2015-08-31 18:34:30 +0200968 bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
Loic Poulainb98469f2015-08-29 13:38:19 +0200969 break;
970 }
971
972 kfree_skb(skb);
973
974 return 0;
975}
976
977#define INTEL_RECV_LPM \
978 .type = HCI_LPM_PKT, \
979 .hlen = HCI_LPM_HDR_SIZE, \
980 .loff = 1, \
981 .lsize = 1, \
982 .maxlen = HCI_LPM_MAX_SIZE
983
Loic Poulainca93cee2015-07-01 12:20:26 +0200984static const struct h4_recv_pkt intel_recv_pkts[] = {
Loic Poulainb98469f2015-08-29 13:38:19 +0200985 { H4_RECV_ACL, .recv = hci_recv_frame },
986 { H4_RECV_SCO, .recv = hci_recv_frame },
987 { H4_RECV_EVENT, .recv = intel_recv_event },
988 { INTEL_RECV_LPM, .recv = intel_recv_lpm },
Loic Poulainca93cee2015-07-01 12:20:26 +0200989};
990
991static int intel_recv(struct hci_uart *hu, const void *data, int count)
992{
993 struct intel_data *intel = hu->priv;
994
995 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
996 return -EUNATCH;
997
998 intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
999 intel_recv_pkts,
1000 ARRAY_SIZE(intel_recv_pkts));
1001 if (IS_ERR(intel->rx_skb)) {
1002 int err = PTR_ERR(intel->rx_skb);
Loic Poulainf44e78a2015-08-31 18:34:30 +02001003 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +02001004 intel->rx_skb = NULL;
1005 return err;
1006 }
1007
1008 return count;
1009}
1010
1011static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
1012{
1013 struct intel_data *intel = hu->priv;
1014
1015 BT_DBG("hu %p skb %p", hu, skb);
1016
1017 skb_queue_tail(&intel->txq, skb);
1018
1019 return 0;
1020}
1021
1022static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1023{
1024 struct intel_data *intel = hu->priv;
1025 struct sk_buff *skb;
1026
1027 skb = skb_dequeue(&intel->txq);
1028 if (!skb)
1029 return skb;
1030
1031 if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
1032 (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) {
1033 struct hci_command_hdr *cmd = (void *)skb->data;
1034 __u16 opcode = le16_to_cpu(cmd->opcode);
1035
1036 /* When the 0xfc01 command is issued to boot into
1037 * the operational firmware, it will actually not
1038 * send a command complete event. To keep the flow
1039 * control working inject that event here.
1040 */
1041 if (opcode == 0xfc01)
1042 inject_cmd_complete(hu->hdev, opcode);
1043 }
1044
1045 /* Prepend skb with frame type */
1046 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
1047
1048 return skb;
1049}
1050
1051static const struct hci_uart_proto intel_proto = {
1052 .id = HCI_UART_INTEL,
1053 .name = "Intel",
1054 .init_speed = 115200,
Loic Poulainff289552015-08-25 17:55:44 +02001055 .oper_speed = 3000000,
Loic Poulainca93cee2015-07-01 12:20:26 +02001056 .open = intel_open,
1057 .close = intel_close,
1058 .flush = intel_flush,
1059 .setup = intel_setup,
Loic Poulainff289552015-08-25 17:55:44 +02001060 .set_baudrate = intel_set_baudrate,
Loic Poulainca93cee2015-07-01 12:20:26 +02001061 .recv = intel_recv,
1062 .enqueue = intel_enqueue,
1063 .dequeue = intel_dequeue,
1064};
1065
Loic Poulain1ab1f232015-08-27 07:21:51 +02001066#ifdef CONFIG_ACPI
1067static const struct acpi_device_id intel_acpi_match[] = {
1068 { "INT33E1", 0 },
1069 { },
1070};
1071MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
1072
1073static int intel_acpi_probe(struct intel_device *idev)
1074{
1075 const struct acpi_device_id *id;
1076
1077 id = acpi_match_device(intel_acpi_match, &idev->pdev->dev);
1078 if (!id)
1079 return -ENODEV;
1080
1081 return 0;
1082}
1083#else
1084static int intel_acpi_probe(struct intel_device *idev)
1085{
1086 return -ENODEV;
1087}
1088#endif
1089
1090static int intel_probe(struct platform_device *pdev)
1091{
1092 struct intel_device *idev;
1093
1094 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
1095 if (!idev)
1096 return -ENOMEM;
1097
1098 idev->pdev = pdev;
1099
1100 if (ACPI_HANDLE(&pdev->dev)) {
1101 int err = intel_acpi_probe(idev);
1102 if (err)
1103 return err;
1104 } else {
1105 return -ENODEV;
1106 }
1107
1108 idev->reset = devm_gpiod_get_optional(&pdev->dev, "reset",
1109 GPIOD_OUT_LOW);
1110 if (IS_ERR(idev->reset)) {
1111 dev_err(&pdev->dev, "Unable to retrieve gpio\n");
1112 return PTR_ERR(idev->reset);
1113 }
1114
Loic Poulain765ea3a2015-08-29 13:38:18 +02001115 idev->irq = platform_get_irq(pdev, 0);
1116 if (idev->irq < 0) {
1117 struct gpio_desc *host_wake;
1118
1119 dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1120
1121 host_wake = devm_gpiod_get_optional(&pdev->dev, "host-wake",
1122 GPIOD_IN);
1123 if (IS_ERR(host_wake)) {
1124 dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1125 goto no_irq;
1126 }
1127
1128 idev->irq = gpiod_to_irq(host_wake);
1129 if (idev->irq < 0) {
1130 dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1131 goto no_irq;
1132 }
1133 }
1134
1135 /* Only enable wake-up/irq when controller is powered */
1136 device_set_wakeup_capable(&pdev->dev, true);
1137 device_wakeup_disable(&pdev->dev);
1138
1139no_irq:
Loic Poulain1ab1f232015-08-27 07:21:51 +02001140 platform_set_drvdata(pdev, idev);
1141
1142 /* Place this instance on the device list */
Loic Poulain67c8bde2015-08-31 18:34:31 +02001143 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001144 list_add_tail(&idev->list, &intel_device_list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001145 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001146
Loic Poulain765ea3a2015-08-29 13:38:18 +02001147 dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1148 desc_to_gpio(idev->reset), idev->irq);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001149
1150 return 0;
1151}
1152
1153static int intel_remove(struct platform_device *pdev)
1154{
1155 struct intel_device *idev = platform_get_drvdata(pdev);
1156
Loic Poulain765ea3a2015-08-29 13:38:18 +02001157 device_wakeup_disable(&pdev->dev);
1158
Loic Poulain67c8bde2015-08-31 18:34:31 +02001159 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001160 list_del(&idev->list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001161 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001162
1163 dev_info(&pdev->dev, "unregistered.\n");
1164
1165 return 0;
1166}
1167
1168static struct platform_driver intel_driver = {
1169 .probe = intel_probe,
1170 .remove = intel_remove,
1171 .driver = {
1172 .name = "hci_intel",
1173 .acpi_match_table = ACPI_PTR(intel_acpi_match),
1174 },
1175};
1176
Loic Poulainca93cee2015-07-01 12:20:26 +02001177int __init intel_init(void)
1178{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001179 platform_driver_register(&intel_driver);
1180
Loic Poulainca93cee2015-07-01 12:20:26 +02001181 return hci_uart_register_proto(&intel_proto);
1182}
1183
1184int __exit intel_deinit(void)
1185{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001186 platform_driver_unregister(&intel_driver);
1187
Loic Poulainca93cee2015-07-01 12:20:26 +02001188 return hci_uart_unregister_proto(&intel_proto);
1189}