]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/bluetooth/hci_bcm.c
Bluetooth: hci_bcm: Handle possible error from acpi_dev_get_resources()
[karo-tx-linux.git] / drivers / bluetooth / hci_bcm.c
1 /*
2  *
3  *  Bluetooth HCI UART driver for Broadcom 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>
27 #include <linux/firmware.h>
28 #include <linux/module.h>
29 #include <linux/acpi.h>
30 #include <linux/platform_device.h>
31 #include <linux/clk.h>
32 #include <linux/gpio/consumer.h>
33 #include <linux/tty.h>
34 #include <linux/interrupt.h>
35 #include <linux/dmi.h>
36 #include <linux/pm_runtime.h>
37
38 #include <net/bluetooth/bluetooth.h>
39 #include <net/bluetooth/hci_core.h>
40
41 #include "btbcm.h"
42 #include "hci_uart.h"
43
44 #define BCM_AUTOSUSPEND_DELAY   5000 /* default autosleep delay */
45
46 struct bcm_device {
47         struct list_head        list;
48
49         struct platform_device  *pdev;
50
51         const char              *name;
52         struct gpio_desc        *device_wakeup;
53         struct gpio_desc        *shutdown;
54
55         struct clk              *clk;
56         bool                    clk_enabled;
57
58         u32                     init_speed;
59         int                     irq;
60         u8                      irq_polarity;
61
62 #ifdef CONFIG_PM
63         struct hci_uart         *hu;
64         bool                    is_suspended; /* suspend/resume flag */
65 #endif
66 };
67
68 struct bcm_data {
69         struct sk_buff          *rx_skb;
70         struct sk_buff_head     txq;
71
72         struct bcm_device       *dev;
73 };
74
75 /* List of BCM BT UART devices */
76 static DEFINE_MUTEX(bcm_device_lock);
77 static LIST_HEAD(bcm_device_list);
78
79 static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
80 {
81         struct hci_dev *hdev = hu->hdev;
82         struct sk_buff *skb;
83         struct bcm_update_uart_baud_rate param;
84
85         if (speed > 3000000) {
86                 struct bcm_write_uart_clock_setting clock;
87
88                 clock.type = BCM_UART_CLOCK_48MHZ;
89
90                 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
91
92                 /* This Broadcom specific command changes the UART's controller
93                  * clock for baud rate > 3000000.
94                  */
95                 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
96                 if (IS_ERR(skb)) {
97                         int err = PTR_ERR(skb);
98                         bt_dev_err(hdev, "BCM: failed to write clock (%d)",
99                                    err);
100                         return err;
101                 }
102
103                 kfree_skb(skb);
104         }
105
106         bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
107
108         param.zero = cpu_to_le16(0);
109         param.baud_rate = cpu_to_le32(speed);
110
111         /* This Broadcom specific command changes the UART's controller baud
112          * rate.
113          */
114         skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
115                              HCI_INIT_TIMEOUT);
116         if (IS_ERR(skb)) {
117                 int err = PTR_ERR(skb);
118                 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
119                            err);
120                 return err;
121         }
122
123         kfree_skb(skb);
124
125         return 0;
126 }
127
128 /* bcm_device_exists should be protected by bcm_device_lock */
129 static bool bcm_device_exists(struct bcm_device *device)
130 {
131         struct list_head *p;
132
133         list_for_each(p, &bcm_device_list) {
134                 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
135
136                 if (device == dev)
137                         return true;
138         }
139
140         return false;
141 }
142
143 static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
144 {
145         if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
146                 clk_enable(dev->clk);
147
148         gpiod_set_value(dev->shutdown, powered);
149         gpiod_set_value(dev->device_wakeup, powered);
150
151         if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
152                 clk_disable(dev->clk);
153
154         dev->clk_enabled = powered;
155
156         return 0;
157 }
158
159 #ifdef CONFIG_PM
160 static irqreturn_t bcm_host_wake(int irq, void *data)
161 {
162         struct bcm_device *bdev = data;
163
164         bt_dev_dbg(bdev, "Host wake IRQ");
165
166         pm_runtime_get(&bdev->pdev->dev);
167         pm_runtime_mark_last_busy(&bdev->pdev->dev);
168         pm_runtime_put_autosuspend(&bdev->pdev->dev);
169
170         return IRQ_HANDLED;
171 }
172
173 static int bcm_request_irq(struct bcm_data *bcm)
174 {
175         struct bcm_device *bdev = bcm->dev;
176         int err = 0;
177
178         /* If this is not a platform device, do not enable PM functionalities */
179         mutex_lock(&bcm_device_lock);
180         if (!bcm_device_exists(bdev)) {
181                 err = -ENODEV;
182                 goto unlock;
183         }
184
185         if (bdev->irq > 0) {
186                 err = devm_request_irq(&bdev->pdev->dev, bdev->irq,
187                                        bcm_host_wake, IRQF_TRIGGER_RISING,
188                                        "host_wake", bdev);
189                 if (err)
190                         goto unlock;
191
192                 device_init_wakeup(&bdev->pdev->dev, true);
193
194                 pm_runtime_set_autosuspend_delay(&bdev->pdev->dev,
195                                                  BCM_AUTOSUSPEND_DELAY);
196                 pm_runtime_use_autosuspend(&bdev->pdev->dev);
197                 pm_runtime_set_active(&bdev->pdev->dev);
198                 pm_runtime_enable(&bdev->pdev->dev);
199         }
200
201 unlock:
202         mutex_unlock(&bcm_device_lock);
203
204         return err;
205 }
206
207 static const struct bcm_set_sleep_mode default_sleep_params = {
208         .sleep_mode = 1,        /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
209         .idle_host = 2,         /* idle threshold HOST, in 300ms */
210         .idle_dev = 2,          /* idle threshold device, in 300ms */
211         .bt_wake_active = 1,    /* BT_WAKE active mode: 1 = high, 0 = low */
212         .host_wake_active = 0,  /* HOST_WAKE active mode: 1 = high, 0 = low */
213         .allow_host_sleep = 1,  /* Allow host sleep in SCO flag */
214         .combine_modes = 1,     /* Combine sleep and LPM flag */
215         .tristate_control = 0,  /* Allow tri-state control of UART tx flag */
216         /* Irrelevant USB flags */
217         .usb_auto_sleep = 0,
218         .usb_resume_timeout = 0,
219         .pulsed_host_wake = 0,
220         .break_to_host = 0
221 };
222
223 static int bcm_setup_sleep(struct hci_uart *hu)
224 {
225         struct bcm_data *bcm = hu->priv;
226         struct sk_buff *skb;
227         struct bcm_set_sleep_mode sleep_params = default_sleep_params;
228
229         sleep_params.host_wake_active = !bcm->dev->irq_polarity;
230
231         skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
232                              &sleep_params, HCI_INIT_TIMEOUT);
233         if (IS_ERR(skb)) {
234                 int err = PTR_ERR(skb);
235                 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
236                 return err;
237         }
238         kfree_skb(skb);
239
240         bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
241
242         return 0;
243 }
244 #else
245 static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
246 static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
247 #endif
248
249 static int bcm_open(struct hci_uart *hu)
250 {
251         struct bcm_data *bcm;
252         struct list_head *p;
253
254         bt_dev_dbg(hu->hdev, "hu %p", hu);
255
256         bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
257         if (!bcm)
258                 return -ENOMEM;
259
260         skb_queue_head_init(&bcm->txq);
261
262         hu->priv = bcm;
263
264         mutex_lock(&bcm_device_lock);
265         list_for_each(p, &bcm_device_list) {
266                 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
267
268                 /* Retrieve saved bcm_device based on parent of the
269                  * platform device (saved during device probe) and
270                  * parent of tty device used by hci_uart
271                  */
272                 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
273                         bcm->dev = dev;
274                         hu->init_speed = dev->init_speed;
275 #ifdef CONFIG_PM
276                         dev->hu = hu;
277 #endif
278                         bcm_gpio_set_power(bcm->dev, true);
279                         break;
280                 }
281         }
282
283         mutex_unlock(&bcm_device_lock);
284
285         return 0;
286 }
287
288 static int bcm_close(struct hci_uart *hu)
289 {
290         struct bcm_data *bcm = hu->priv;
291         struct bcm_device *bdev = bcm->dev;
292
293         bt_dev_dbg(hu->hdev, "hu %p", hu);
294
295         /* Protect bcm->dev against removal of the device or driver */
296         mutex_lock(&bcm_device_lock);
297         if (bcm_device_exists(bdev)) {
298                 bcm_gpio_set_power(bdev, false);
299 #ifdef CONFIG_PM
300                 pm_runtime_disable(&bdev->pdev->dev);
301                 pm_runtime_set_suspended(&bdev->pdev->dev);
302
303                 if (device_can_wakeup(&bdev->pdev->dev)) {
304                         devm_free_irq(&bdev->pdev->dev, bdev->irq, bdev);
305                         device_init_wakeup(&bdev->pdev->dev, false);
306                 }
307
308                 bdev->hu = NULL;
309 #endif
310         }
311         mutex_unlock(&bcm_device_lock);
312
313         skb_queue_purge(&bcm->txq);
314         kfree_skb(bcm->rx_skb);
315         kfree(bcm);
316
317         hu->priv = NULL;
318         return 0;
319 }
320
321 static int bcm_flush(struct hci_uart *hu)
322 {
323         struct bcm_data *bcm = hu->priv;
324
325         bt_dev_dbg(hu->hdev, "hu %p", hu);
326
327         skb_queue_purge(&bcm->txq);
328
329         return 0;
330 }
331
332 static int bcm_setup(struct hci_uart *hu)
333 {
334         struct bcm_data *bcm = hu->priv;
335         char fw_name[64];
336         const struct firmware *fw;
337         unsigned int speed;
338         int err;
339
340         bt_dev_dbg(hu->hdev, "hu %p", hu);
341
342         hu->hdev->set_bdaddr = btbcm_set_bdaddr;
343
344         err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
345         if (err)
346                 return err;
347
348         err = request_firmware(&fw, fw_name, &hu->hdev->dev);
349         if (err < 0) {
350                 bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
351                 return 0;
352         }
353
354         err = btbcm_patchram(hu->hdev, fw);
355         if (err) {
356                 bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
357                 goto finalize;
358         }
359
360         /* Init speed if any */
361         if (hu->init_speed)
362                 speed = hu->init_speed;
363         else if (hu->proto->init_speed)
364                 speed = hu->proto->init_speed;
365         else
366                 speed = 0;
367
368         if (speed)
369                 hci_uart_set_baudrate(hu, speed);
370
371         /* Operational speed if any */
372         if (hu->oper_speed)
373                 speed = hu->oper_speed;
374         else if (hu->proto->oper_speed)
375                 speed = hu->proto->oper_speed;
376         else
377                 speed = 0;
378
379         if (speed) {
380                 err = bcm_set_baudrate(hu, speed);
381                 if (!err)
382                         hci_uart_set_baudrate(hu, speed);
383         }
384
385 finalize:
386         release_firmware(fw);
387
388         err = btbcm_finalize(hu->hdev);
389         if (err)
390                 return err;
391
392         err = bcm_request_irq(bcm);
393         if (!err)
394                 err = bcm_setup_sleep(hu);
395
396         return err;
397 }
398
399 static const struct h4_recv_pkt bcm_recv_pkts[] = {
400         { H4_RECV_ACL,   .recv = hci_recv_frame },
401         { H4_RECV_SCO,   .recv = hci_recv_frame },
402         { H4_RECV_EVENT, .recv = hci_recv_frame },
403 };
404
405 static int bcm_recv(struct hci_uart *hu, const void *data, int count)
406 {
407         struct bcm_data *bcm = hu->priv;
408
409         if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
410                 return -EUNATCH;
411
412         bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
413                                   bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
414         if (IS_ERR(bcm->rx_skb)) {
415                 int err = PTR_ERR(bcm->rx_skb);
416                 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
417                 bcm->rx_skb = NULL;
418                 return err;
419         } else if (!bcm->rx_skb) {
420                 /* Delay auto-suspend when receiving completed packet */
421                 mutex_lock(&bcm_device_lock);
422                 if (bcm->dev && bcm_device_exists(bcm->dev)) {
423                         pm_runtime_get(&bcm->dev->pdev->dev);
424                         pm_runtime_mark_last_busy(&bcm->dev->pdev->dev);
425                         pm_runtime_put_autosuspend(&bcm->dev->pdev->dev);
426                 }
427                 mutex_unlock(&bcm_device_lock);
428         }
429
430         return count;
431 }
432
433 static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
434 {
435         struct bcm_data *bcm = hu->priv;
436
437         bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
438
439         /* Prepend skb with frame type */
440         memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
441         skb_queue_tail(&bcm->txq, skb);
442
443         return 0;
444 }
445
446 static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
447 {
448         struct bcm_data *bcm = hu->priv;
449         struct sk_buff *skb = NULL;
450         struct bcm_device *bdev = NULL;
451
452         mutex_lock(&bcm_device_lock);
453
454         if (bcm_device_exists(bcm->dev)) {
455                 bdev = bcm->dev;
456                 pm_runtime_get_sync(&bdev->pdev->dev);
457                 /* Shall be resumed here */
458         }
459
460         skb = skb_dequeue(&bcm->txq);
461
462         if (bdev) {
463                 pm_runtime_mark_last_busy(&bdev->pdev->dev);
464                 pm_runtime_put_autosuspend(&bdev->pdev->dev);
465         }
466
467         mutex_unlock(&bcm_device_lock);
468
469         return skb;
470 }
471
472 #ifdef CONFIG_PM
473 static int bcm_suspend_device(struct device *dev)
474 {
475         struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
476
477         bt_dev_dbg(bdev, "");
478
479         if (!bdev->is_suspended && bdev->hu) {
480                 hci_uart_set_flow_control(bdev->hu, true);
481
482                 /* Once this returns, driver suspends BT via GPIO */
483                 bdev->is_suspended = true;
484         }
485
486         /* Suspend the device */
487         if (bdev->device_wakeup) {
488                 gpiod_set_value(bdev->device_wakeup, false);
489                 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
490                 mdelay(15);
491         }
492
493         return 0;
494 }
495
496 static int bcm_resume_device(struct device *dev)
497 {
498         struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
499
500         bt_dev_dbg(bdev, "");
501
502         if (bdev->device_wakeup) {
503                 gpiod_set_value(bdev->device_wakeup, true);
504                 bt_dev_dbg(bdev, "resume, delaying 15 ms");
505                 mdelay(15);
506         }
507
508         /* When this executes, the device has woken up already */
509         if (bdev->is_suspended && bdev->hu) {
510                 bdev->is_suspended = false;
511
512                 hci_uart_set_flow_control(bdev->hu, false);
513         }
514
515         return 0;
516 }
517 #endif
518
519 #ifdef CONFIG_PM_SLEEP
520 /* Platform suspend callback */
521 static int bcm_suspend(struct device *dev)
522 {
523         struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
524         int error;
525
526         bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
527
528         /* bcm_suspend can be called at any time as long as platform device is
529          * bound, so it should use bcm_device_lock to protect access to hci_uart
530          * and device_wake-up GPIO.
531          */
532         mutex_lock(&bcm_device_lock);
533
534         if (!bdev->hu)
535                 goto unlock;
536
537         if (pm_runtime_active(dev))
538                 bcm_suspend_device(dev);
539
540         if (device_may_wakeup(&bdev->pdev->dev)) {
541                 error = enable_irq_wake(bdev->irq);
542                 if (!error)
543                         bt_dev_dbg(bdev, "BCM irq: enabled");
544         }
545
546 unlock:
547         mutex_unlock(&bcm_device_lock);
548
549         return 0;
550 }
551
552 /* Platform resume callback */
553 static int bcm_resume(struct device *dev)
554 {
555         struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
556
557         bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
558
559         /* bcm_resume can be called at any time as long as platform device is
560          * bound, so it should use bcm_device_lock to protect access to hci_uart
561          * and device_wake-up GPIO.
562          */
563         mutex_lock(&bcm_device_lock);
564
565         if (!bdev->hu)
566                 goto unlock;
567
568         if (device_may_wakeup(&bdev->pdev->dev)) {
569                 disable_irq_wake(bdev->irq);
570                 bt_dev_dbg(bdev, "BCM irq: disabled");
571         }
572
573         bcm_resume_device(dev);
574
575 unlock:
576         mutex_unlock(&bcm_device_lock);
577
578         pm_runtime_disable(dev);
579         pm_runtime_set_active(dev);
580         pm_runtime_enable(dev);
581
582         return 0;
583 }
584 #endif
585
586 static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
587 static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
588 static const struct acpi_gpio_params host_wakeup_gpios = { 2, 0, false };
589
590 static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
591         { "device-wakeup-gpios", &device_wakeup_gpios, 1 },
592         { "shutdown-gpios", &shutdown_gpios, 1 },
593         { "host-wakeup-gpios", &host_wakeup_gpios, 1 },
594         { },
595 };
596
597 #ifdef CONFIG_ACPI
598 static u8 acpi_active_low = ACPI_ACTIVE_LOW;
599
600 /* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
601 static const struct dmi_system_id bcm_wrong_irq_dmi_table[] = {
602         {
603                 .ident = "Asus T100TA",
604                 .matches = {
605                         DMI_EXACT_MATCH(DMI_SYS_VENDOR,
606                                         "ASUSTeK COMPUTER INC."),
607                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
608                 },
609                 .driver_data = &acpi_active_low,
610         },
611         { }
612 };
613
614 static int bcm_resource(struct acpi_resource *ares, void *data)
615 {
616         struct bcm_device *dev = data;
617         struct acpi_resource_extended_irq *irq;
618         struct acpi_resource_gpio *gpio;
619         struct acpi_resource_uart_serialbus *sb;
620
621         switch (ares->type) {
622         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
623                 irq = &ares->data.extended_irq;
624                 dev->irq_polarity = irq->polarity;
625                 break;
626
627         case ACPI_RESOURCE_TYPE_GPIO:
628                 gpio = &ares->data.gpio;
629                 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
630                         dev->irq_polarity = gpio->polarity;
631                 break;
632
633         case ACPI_RESOURCE_TYPE_SERIAL_BUS:
634                 sb = &ares->data.uart_serial_bus;
635                 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
636                         dev->init_speed = sb->default_baud_rate;
637                 break;
638
639         default:
640                 break;
641         }
642
643         /* Always tell the ACPI core to skip this resource */
644         return 1;
645 }
646
647 static int bcm_acpi_probe(struct bcm_device *dev)
648 {
649         struct platform_device *pdev = dev->pdev;
650         const struct acpi_device_id *id;
651         struct acpi_device *adev;
652         LIST_HEAD(resources);
653         const struct dmi_system_id *dmi_id;
654         int ret;
655
656         id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
657         if (!id)
658                 return -ENODEV;
659
660         /* Retrieve GPIO data */
661         dev->name = dev_name(&pdev->dev);
662         ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev),
663                                         acpi_bcm_default_gpios);
664         if (ret)
665                 return ret;
666
667         dev->clk = devm_clk_get(&pdev->dev, NULL);
668
669         dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev,
670                                                      "device-wakeup",
671                                                      GPIOD_OUT_LOW);
672         if (IS_ERR(dev->device_wakeup))
673                 return PTR_ERR(dev->device_wakeup);
674
675         dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown",
676                                                 GPIOD_OUT_LOW);
677         if (IS_ERR(dev->shutdown))
678                 return PTR_ERR(dev->shutdown);
679
680         /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
681         dev->irq = platform_get_irq(pdev, 0);
682         if (dev->irq <= 0) {
683                 struct gpio_desc *gpio;
684
685                 gpio = devm_gpiod_get_optional(&pdev->dev, "host-wakeup",
686                                                GPIOD_IN);
687                 if (IS_ERR(gpio))
688                         return PTR_ERR(gpio);
689
690                 dev->irq = gpiod_to_irq(gpio);
691         }
692
693         dev_info(&pdev->dev, "BCM irq: %d\n", dev->irq);
694
695         /* Make sure at-least one of the GPIO is defined and that
696          * a name is specified for this instance
697          */
698         if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
699                 dev_err(&pdev->dev, "invalid platform data\n");
700                 return -EINVAL;
701         }
702
703         /* Retrieve UART ACPI info */
704         adev = ACPI_COMPANION(&dev->pdev->dev);
705         if (!adev)
706                 return 0;
707
708         ret = acpi_dev_get_resources(adev, &resources, bcm_resource, dev);
709         if (ret < 0)
710                 return ret;
711         acpi_dev_free_resource_list(&resources);
712
713         dmi_id = dmi_first_match(bcm_wrong_irq_dmi_table);
714         if (dmi_id) {
715                 bt_dev_warn(dev, "%s: Overwriting IRQ polarity to active low",
716                             dmi_id->ident);
717                 dev->irq_polarity = *(u8 *)dmi_id->driver_data;
718         }
719
720         return 0;
721 }
722 #else
723 static int bcm_acpi_probe(struct bcm_device *dev)
724 {
725         return -EINVAL;
726 }
727 #endif /* CONFIG_ACPI */
728
729 static int bcm_probe(struct platform_device *pdev)
730 {
731         struct bcm_device *dev;
732         struct acpi_device_id *pdata = pdev->dev.platform_data;
733         int ret;
734
735         dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
736         if (!dev)
737                 return -ENOMEM;
738
739         dev->pdev = pdev;
740
741         if (ACPI_HANDLE(&pdev->dev)) {
742                 ret = bcm_acpi_probe(dev);
743                 if (ret)
744                         return ret;
745         } else if (pdata) {
746                 dev->name = pdata->id;
747         } else {
748                 return -ENODEV;
749         }
750
751         platform_set_drvdata(pdev, dev);
752
753         dev_info(&pdev->dev, "%s device registered.\n", dev->name);
754
755         /* Place this instance on the device list */
756         mutex_lock(&bcm_device_lock);
757         list_add_tail(&dev->list, &bcm_device_list);
758         mutex_unlock(&bcm_device_lock);
759
760         bcm_gpio_set_power(dev, false);
761
762         return 0;
763 }
764
765 static int bcm_remove(struct platform_device *pdev)
766 {
767         struct bcm_device *dev = platform_get_drvdata(pdev);
768
769         mutex_lock(&bcm_device_lock);
770         list_del(&dev->list);
771         mutex_unlock(&bcm_device_lock);
772
773         acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
774
775         dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
776
777         return 0;
778 }
779
780 static const struct hci_uart_proto bcm_proto = {
781         .id             = HCI_UART_BCM,
782         .name           = "BCM",
783         .init_speed     = 115200,
784         .oper_speed     = 4000000,
785         .open           = bcm_open,
786         .close          = bcm_close,
787         .flush          = bcm_flush,
788         .setup          = bcm_setup,
789         .set_baudrate   = bcm_set_baudrate,
790         .recv           = bcm_recv,
791         .enqueue        = bcm_enqueue,
792         .dequeue        = bcm_dequeue,
793 };
794
795 #ifdef CONFIG_ACPI
796 static const struct acpi_device_id bcm_acpi_match[] = {
797         { "BCM2E39", 0 },
798         { "BCM2E67", 0 },
799         { },
800 };
801 MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
802 #endif
803
804 /* Platform suspend and resume callbacks */
805 static const struct dev_pm_ops bcm_pm_ops = {
806         SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
807         SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
808 };
809
810 static struct platform_driver bcm_driver = {
811         .probe = bcm_probe,
812         .remove = bcm_remove,
813         .driver = {
814                 .name = "hci_bcm",
815                 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
816                 .pm = &bcm_pm_ops,
817         },
818 };
819
820 int __init bcm_init(void)
821 {
822         platform_driver_register(&bcm_driver);
823
824         return hci_uart_register_proto(&bcm_proto);
825 }
826
827 int __exit bcm_deinit(void)
828 {
829         platform_driver_unregister(&bcm_driver);
830
831         return hci_uart_unregister_proto(&bcm_proto);
832 }