Bluetooth: Fix SCO link type handling on connection complete
[linux-2.6-block.git] / drivers / bluetooth / hci_bcm.c
CommitLineData
e9a2dd26
MH
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>
18aeb444 27#include <linux/firmware.h>
0395ffc1
FD
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>
e9a2dd26
MH
34
35#include <net/bluetooth/bluetooth.h>
36#include <net/bluetooth/hci_core.h>
37
bdd8818e 38#include "btbcm.h"
e9a2dd26 39#include "hci_uart.h"
bdd8818e 40
0395ffc1
FD
41struct bcm_device {
42 struct list_head list;
43
44 struct platform_device *pdev;
45
46 const char *name;
47 struct gpio_desc *device_wakeup;
48 struct gpio_desc *shutdown;
49
50 struct clk *clk;
51 bool clk_enabled;
ae056908
FD
52
53 u32 init_speed;
118612fb
FD
54
55#ifdef CONFIG_PM_SLEEP
56 struct hci_uart *hu;
57 bool is_suspended; /* suspend/resume flag */
58#endif
0395ffc1
FD
59};
60
bdd8818e 61struct bcm_data {
0395ffc1
FD
62 struct sk_buff *rx_skb;
63 struct sk_buff_head txq;
64
65 struct bcm_device *dev;
bdd8818e
MH
66};
67
0395ffc1
FD
68/* List of BCM BT UART devices */
69static DEFINE_SPINLOCK(bcm_device_list_lock);
70static LIST_HEAD(bcm_device_list);
71
61b2fc2b
FD
72static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
73{
74 struct hci_dev *hdev = hu->hdev;
75 struct sk_buff *skb;
76 struct bcm_update_uart_baud_rate param;
77
78 if (speed > 3000000) {
79 struct bcm_write_uart_clock_setting clock;
80
81 clock.type = BCM_UART_CLOCK_48MHZ;
82
83 BT_DBG("%s: Set Controller clock (%d)", hdev->name, clock.type);
84
85 /* This Broadcom specific command changes the UART's controller
86 * clock for baud rate > 3000000.
87 */
88 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
89 if (IS_ERR(skb)) {
90 int err = PTR_ERR(skb);
91 BT_ERR("%s: BCM: failed to write clock command (%d)",
92 hdev->name, err);
93 return err;
94 }
95
96 kfree_skb(skb);
97 }
98
99 BT_DBG("%s: Set Controller UART speed to %d bit/s", hdev->name, speed);
100
101 param.zero = cpu_to_le16(0);
102 param.baud_rate = cpu_to_le32(speed);
103
104 /* This Broadcom specific command changes the UART's controller baud
105 * rate.
106 */
107 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
108 HCI_INIT_TIMEOUT);
109 if (IS_ERR(skb)) {
110 int err = PTR_ERR(skb);
111 BT_ERR("%s: BCM: failed to write update baudrate command (%d)",
112 hdev->name, err);
113 return err;
114 }
115
116 kfree_skb(skb);
117
118 return 0;
119}
120
0395ffc1
FD
121/* bcm_device_exists should be protected by bcm_device_list_lock */
122static bool bcm_device_exists(struct bcm_device *device)
123{
124 struct list_head *p;
125
126 list_for_each(p, &bcm_device_list) {
127 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
128
129 if (device == dev)
130 return true;
131 }
132
133 return false;
134}
135
136static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
137{
138 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
139 clk_enable(dev->clk);
140
2af0a709
LP
141 gpiod_set_value(dev->shutdown, powered);
142 gpiod_set_value(dev->device_wakeup, powered);
0395ffc1
FD
143
144 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
145 clk_disable(dev->clk);
146
147 dev->clk_enabled = powered;
148
149 return 0;
150}
151
bdd8818e
MH
152static int bcm_open(struct hci_uart *hu)
153{
154 struct bcm_data *bcm;
0395ffc1 155 struct list_head *p;
bdd8818e
MH
156
157 BT_DBG("hu %p", hu);
158
159 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
160 if (!bcm)
161 return -ENOMEM;
162
163 skb_queue_head_init(&bcm->txq);
164
165 hu->priv = bcm;
0395ffc1
FD
166
167 spin_lock(&bcm_device_list_lock);
168 list_for_each(p, &bcm_device_list) {
169 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
170
171 /* Retrieve saved bcm_device based on parent of the
172 * platform device (saved during device probe) and
173 * parent of tty device used by hci_uart
174 */
175 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
176 bcm->dev = dev;
ae056908 177 hu->init_speed = dev->init_speed;
118612fb
FD
178#ifdef CONFIG_PM_SLEEP
179 dev->hu = hu;
180#endif
0395ffc1
FD
181 break;
182 }
183 }
184
185 if (bcm->dev)
186 bcm_gpio_set_power(bcm->dev, true);
187
188 spin_unlock(&bcm_device_list_lock);
189
bdd8818e
MH
190 return 0;
191}
192
193static int bcm_close(struct hci_uart *hu)
194{
195 struct bcm_data *bcm = hu->priv;
196
197 BT_DBG("hu %p", hu);
198
0395ffc1
FD
199 /* Protect bcm->dev against removal of the device or driver */
200 spin_lock(&bcm_device_list_lock);
118612fb 201 if (bcm_device_exists(bcm->dev)) {
0395ffc1 202 bcm_gpio_set_power(bcm->dev, false);
118612fb
FD
203#ifdef CONFIG_PM_SLEEP
204 bcm->dev->hu = NULL;
205#endif
206 }
0395ffc1
FD
207 spin_unlock(&bcm_device_list_lock);
208
bdd8818e
MH
209 skb_queue_purge(&bcm->txq);
210 kfree_skb(bcm->rx_skb);
211 kfree(bcm);
212
213 hu->priv = NULL;
214 return 0;
215}
216
217static int bcm_flush(struct hci_uart *hu)
218{
219 struct bcm_data *bcm = hu->priv;
220
221 BT_DBG("hu %p", hu);
222
223 skb_queue_purge(&bcm->txq);
224
225 return 0;
226}
227
228static int bcm_setup(struct hci_uart *hu)
229{
6be09b48
FD
230 char fw_name[64];
231 const struct firmware *fw;
960ef1d7 232 unsigned int speed;
6be09b48
FD
233 int err;
234
bdd8818e
MH
235 BT_DBG("hu %p", hu);
236
237 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
238
6be09b48
FD
239 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
240 if (err)
241 return err;
242
243 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
244 if (err < 0) {
245 BT_INFO("%s: BCM: Patch %s not found", hu->hdev->name, fw_name);
246 return 0;
247 }
248
249 err = btbcm_patchram(hu->hdev, fw);
250 if (err) {
251 BT_INFO("%s: BCM: Patch failed (%d)", hu->hdev->name, err);
252 goto finalize;
253 }
254
960ef1d7
FD
255 /* Init speed if any */
256 if (hu->init_speed)
257 speed = hu->init_speed;
258 else if (hu->proto->init_speed)
259 speed = hu->proto->init_speed;
260 else
261 speed = 0;
262
263 if (speed)
264 hci_uart_set_baudrate(hu, speed);
265
266 /* Operational speed if any */
267 if (hu->oper_speed)
268 speed = hu->oper_speed;
269 else if (hu->proto->oper_speed)
270 speed = hu->proto->oper_speed;
271 else
272 speed = 0;
273
274 if (speed) {
275 err = bcm_set_baudrate(hu, speed);
61b2fc2b 276 if (!err)
960ef1d7 277 hci_uart_set_baudrate(hu, speed);
61b2fc2b
FD
278 }
279
6be09b48
FD
280finalize:
281 release_firmware(fw);
282
283 err = btbcm_finalize(hu->hdev);
284
285 return err;
bdd8818e
MH
286}
287
79b8df93
MH
288static const struct h4_recv_pkt bcm_recv_pkts[] = {
289 { H4_RECV_ACL, .recv = hci_recv_frame },
290 { H4_RECV_SCO, .recv = hci_recv_frame },
291 { H4_RECV_EVENT, .recv = hci_recv_frame },
292};
293
bdd8818e
MH
294static int bcm_recv(struct hci_uart *hu, const void *data, int count)
295{
296 struct bcm_data *bcm = hu->priv;
297
298 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
299 return -EUNATCH;
300
79b8df93
MH
301 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
302 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
bdd8818e
MH
303 if (IS_ERR(bcm->rx_skb)) {
304 int err = PTR_ERR(bcm->rx_skb);
305 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
37134167 306 bcm->rx_skb = NULL;
bdd8818e
MH
307 return err;
308 }
309
310 return count;
311}
312
313static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
314{
315 struct bcm_data *bcm = hu->priv;
316
317 BT_DBG("hu %p skb %p", hu, skb);
318
319 /* Prepend skb with frame type */
320 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
321 skb_queue_tail(&bcm->txq, skb);
322
323 return 0;
324}
325
326static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
327{
328 struct bcm_data *bcm = hu->priv;
329
330 return skb_dequeue(&bcm->txq);
331}
332
118612fb
FD
333#ifdef CONFIG_PM_SLEEP
334/* Platform suspend callback */
335static int bcm_suspend(struct device *dev)
336{
337 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
338
339 BT_DBG("suspend (%p): is_suspended %d", bdev, bdev->is_suspended);
340
341 if (!bdev->is_suspended) {
342 hci_uart_set_flow_control(bdev->hu, true);
343
344 /* Once this callback returns, driver suspends BT via GPIO */
345 bdev->is_suspended = true;
346 }
347
348 /* Suspend the device */
349 if (bdev->device_wakeup) {
350 gpiod_set_value(bdev->device_wakeup, false);
351 BT_DBG("suspend, delaying 15 ms");
352 mdelay(15);
353 }
354
355 return 0;
356}
357
358/* Platform resume callback */
359static int bcm_resume(struct device *dev)
360{
361 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
362
363 BT_DBG("resume (%p): is_suspended %d", bdev, bdev->is_suspended);
364
365 if (bdev->device_wakeup) {
366 gpiod_set_value(bdev->device_wakeup, true);
367 BT_DBG("resume, delaying 15 ms");
368 mdelay(15);
369 }
370
371 /* When this callback executes, the device has woken up already */
372 if (bdev->is_suspended) {
373 bdev->is_suspended = false;
374
375 hci_uart_set_flow_control(bdev->hu, false);
376 }
377
378 return 0;
379}
380#endif
381
0395ffc1
FD
382static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
383static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
384
385static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
386 { "device-wakeup-gpios", &device_wakeup_gpios, 1 },
387 { "shutdown-gpios", &shutdown_gpios, 1 },
388 { },
389};
390
50d78bcf 391#ifdef CONFIG_ACPI
ae056908
FD
392static int bcm_resource(struct acpi_resource *ares, void *data)
393{
394 struct bcm_device *dev = data;
395
396 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
397 struct acpi_resource_uart_serialbus *sb;
398
399 sb = &ares->data.uart_serial_bus;
400 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
401 dev->init_speed = sb->default_baud_rate;
402 }
403
404 /* Always tell the ACPI core to skip this resource */
405 return 1;
406}
407
0395ffc1
FD
408static int bcm_acpi_probe(struct bcm_device *dev)
409{
410 struct platform_device *pdev = dev->pdev;
411 const struct acpi_device_id *id;
ae056908
FD
412 struct acpi_device *adev;
413 LIST_HEAD(resources);
0395ffc1
FD
414 int ret;
415
416 id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
417 if (!id)
418 return -ENODEV;
419
420 /* Retrieve GPIO data */
421 dev->name = dev_name(&pdev->dev);
422 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev),
423 acpi_bcm_default_gpios);
424 if (ret)
425 return ret;
426
427 dev->clk = devm_clk_get(&pdev->dev, NULL);
428
62aaefa7
UKK
429 dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev,
430 "device-wakeup",
431 GPIOD_OUT_LOW);
432 if (IS_ERR(dev->device_wakeup))
433 return PTR_ERR(dev->device_wakeup);
0395ffc1 434
62aaefa7
UKK
435 dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown",
436 GPIOD_OUT_LOW);
437 if (IS_ERR(dev->shutdown))
438 return PTR_ERR(dev->shutdown);
0395ffc1
FD
439
440 /* Make sure at-least one of the GPIO is defined and that
441 * a name is specified for this instance
442 */
443 if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
444 dev_err(&pdev->dev, "invalid platform data\n");
445 return -EINVAL;
446 }
447
ae056908
FD
448 /* Retrieve UART ACPI info */
449 adev = ACPI_COMPANION(&dev->pdev->dev);
450 if (!adev)
451 return 0;
452
453 acpi_dev_get_resources(adev, &resources, bcm_resource, dev);
454
0395ffc1
FD
455 return 0;
456}
50d78bcf
FD
457#else
458static int bcm_acpi_probe(struct bcm_device *dev)
459{
460 return -EINVAL;
461}
462#endif /* CONFIG_ACPI */
0395ffc1
FD
463
464static int bcm_probe(struct platform_device *pdev)
465{
466 struct bcm_device *dev;
467 struct acpi_device_id *pdata = pdev->dev.platform_data;
468 int ret;
469
470 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
471 if (!dev)
472 return -ENOMEM;
473
474 dev->pdev = pdev;
475
476 if (ACPI_HANDLE(&pdev->dev)) {
477 ret = bcm_acpi_probe(dev);
478 if (ret)
479 return ret;
480 } else if (pdata) {
481 dev->name = pdata->id;
482 } else {
483 return -ENODEV;
484 }
485
486 platform_set_drvdata(pdev, dev);
487
488 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
489
490 /* Place this instance on the device list */
491 spin_lock(&bcm_device_list_lock);
492 list_add_tail(&dev->list, &bcm_device_list);
493 spin_unlock(&bcm_device_list_lock);
494
495 bcm_gpio_set_power(dev, false);
496
497 return 0;
498}
499
500static int bcm_remove(struct platform_device *pdev)
501{
502 struct bcm_device *dev = platform_get_drvdata(pdev);
503
504 spin_lock(&bcm_device_list_lock);
505 list_del(&dev->list);
506 spin_unlock(&bcm_device_list_lock);
507
508 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
509
510 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
511
512 return 0;
513}
514
bdd8818e
MH
515static const struct hci_uart_proto bcm_proto = {
516 .id = HCI_UART_BCM,
517 .name = "BCM",
61b2fc2b
FD
518 .init_speed = 115200,
519 .oper_speed = 4000000,
bdd8818e
MH
520 .open = bcm_open,
521 .close = bcm_close,
522 .flush = bcm_flush,
523 .setup = bcm_setup,
61b2fc2b 524 .set_baudrate = bcm_set_baudrate,
bdd8818e
MH
525 .recv = bcm_recv,
526 .enqueue = bcm_enqueue,
527 .dequeue = bcm_dequeue,
528};
529
0395ffc1
FD
530#ifdef CONFIG_ACPI
531static const struct acpi_device_id bcm_acpi_match[] = {
532 { "BCM2E39", 0 },
533 { "BCM2E67", 0 },
534 { },
535};
536MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
537#endif
538
118612fb
FD
539/* Platform suspend and resume callbacks */
540static SIMPLE_DEV_PM_OPS(bcm_pm_ops, bcm_suspend, bcm_resume);
541
0395ffc1
FD
542static struct platform_driver bcm_driver = {
543 .probe = bcm_probe,
544 .remove = bcm_remove,
545 .driver = {
546 .name = "hci_bcm",
547 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
118612fb 548 .pm = &bcm_pm_ops,
0395ffc1
FD
549 },
550};
551
bdd8818e
MH
552int __init bcm_init(void)
553{
0395ffc1
FD
554 platform_driver_register(&bcm_driver);
555
bdd8818e
MH
556 return hci_uart_register_proto(&bcm_proto);
557}
558
559int __exit bcm_deinit(void)
560{
0395ffc1
FD
561 platform_driver_unregister(&bcm_driver);
562
bdd8818e
MH
563 return hci_uart_unregister_proto(&bcm_proto);
564}