diff options
author | Sebastian Reichel <sre@kernel.org> | 2017-04-12 20:26:59 -0400 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2017-04-13 04:32:23 -0400 |
commit | 7bb318680e868cd049922f6761170b42ff89687d (patch) | |
tree | c68cdc9088fa513e855856bda803529bba102ba7 | |
parent | 1286be270e9a9edaf534856b1143a06f0dd1f30d (diff) |
Bluetooth: add nokia driver
This adds a driver for the Nokia H4+ protocol, which is used
at least on the Nokia N9, N900 & N950.
Signed-off-by: Sebastian Reichel <sre@kernel.org>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
-rw-r--r-- | drivers/bluetooth/Kconfig | 12 | ||||
-rw-r--r-- | drivers/bluetooth/Makefile | 2 | ||||
-rw-r--r-- | drivers/bluetooth/hci_nokia.c | 820 |
3 files changed, 834 insertions, 0 deletions
diff --git a/drivers/bluetooth/Kconfig b/drivers/bluetooth/Kconfig index a6a9dd4d0eef..8aafbed9e160 100644 --- a/drivers/bluetooth/Kconfig +++ b/drivers/bluetooth/Kconfig | |||
@@ -86,6 +86,18 @@ config BT_HCIUART_H4 | |||
86 | 86 | ||
87 | Say Y here to compile support for HCI UART (H4) protocol. | 87 | Say Y here to compile support for HCI UART (H4) protocol. |
88 | 88 | ||
89 | config BT_HCIUART_NOKIA | ||
90 | tristate "UART Nokia H4+ protocol support" | ||
91 | depends on BT_HCIUART | ||
92 | depends on SERIAL_DEV_BUS | ||
93 | depends on PM | ||
94 | help | ||
95 | Nokia H4+ is serial protocol for communication between Bluetooth | ||
96 | device and host. This protocol is required for Bluetooth devices | ||
97 | with UART interface in Nokia devices. | ||
98 | |||
99 | Say Y here to compile support for Nokia's H4+ protocol. | ||
100 | |||
89 | config BT_HCIUART_BCSP | 101 | config BT_HCIUART_BCSP |
90 | bool "BCSP protocol support" | 102 | bool "BCSP protocol support" |
91 | depends on BT_HCIUART | 103 | depends on BT_HCIUART |
diff --git a/drivers/bluetooth/Makefile b/drivers/bluetooth/Makefile index fd571689eed6..a7f237320f4b 100644 --- a/drivers/bluetooth/Makefile +++ b/drivers/bluetooth/Makefile | |||
@@ -25,6 +25,8 @@ obj-$(CONFIG_BT_BCM) += btbcm.o | |||
25 | obj-$(CONFIG_BT_RTL) += btrtl.o | 25 | obj-$(CONFIG_BT_RTL) += btrtl.o |
26 | obj-$(CONFIG_BT_QCA) += btqca.o | 26 | obj-$(CONFIG_BT_QCA) += btqca.o |
27 | 27 | ||
28 | obj-$(CONFIG_BT_HCIUART_NOKIA) += hci_nokia.o | ||
29 | |||
28 | btmrvl-y := btmrvl_main.o | 30 | btmrvl-y := btmrvl_main.o |
29 | btmrvl-$(CONFIG_DEBUG_FS) += btmrvl_debugfs.o | 31 | btmrvl-$(CONFIG_DEBUG_FS) += btmrvl_debugfs.o |
30 | 32 | ||
diff --git a/drivers/bluetooth/hci_nokia.c b/drivers/bluetooth/hci_nokia.c new file mode 100644 index 000000000000..4038daf78d24 --- /dev/null +++ b/drivers/bluetooth/hci_nokia.c | |||
@@ -0,0 +1,820 @@ | |||
1 | /* | ||
2 | * Bluetooth HCI UART H4 driver with Nokia Extensions AKA Nokia H4+ | ||
3 | * | ||
4 | * Copyright (C) 2015 Marcel Holtmann <marcel@holtmann.org> | ||
5 | * Copyright (C) 2015-2017 Sebastian Reichel <sre@kernel.org> | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | */ | ||
17 | |||
18 | #include <linux/clk.h> | ||
19 | #include <linux/errno.h> | ||
20 | #include <linux/firmware.h> | ||
21 | #include <linux/gpio/consumer.h> | ||
22 | #include <linux/interrupt.h> | ||
23 | #include <linux/kernel.h> | ||
24 | #include <linux/module.h> | ||
25 | #include <linux/of.h> | ||
26 | #include <linux/pm_runtime.h> | ||
27 | #include <linux/serdev.h> | ||
28 | #include <linux/skbuff.h> | ||
29 | #include <linux/slab.h> | ||
30 | #include <linux/string.h> | ||
31 | #include <linux/types.h> | ||
32 | #include <linux/unaligned/le_struct.h> | ||
33 | #include <net/bluetooth/bluetooth.h> | ||
34 | #include <net/bluetooth/hci_core.h> | ||
35 | |||
36 | #include "hci_uart.h" | ||
37 | #include "btbcm.h" | ||
38 | |||
39 | #define NOKIA_ID_BCM2048 0x04 | ||
40 | #define NOKIA_ID_TI1271 0x31 | ||
41 | |||
42 | #define FIRMWARE_BCM2048 "nokia/bcmfw.bin" | ||
43 | #define FIRMWARE_TI1271 "nokia/ti1273.bin" | ||
44 | |||
45 | #define HCI_NOKIA_NEG_PKT 0x06 | ||
46 | #define HCI_NOKIA_ALIVE_PKT 0x07 | ||
47 | #define HCI_NOKIA_RADIO_PKT 0x08 | ||
48 | |||
49 | #define HCI_NOKIA_NEG_HDR_SIZE 1 | ||
50 | #define HCI_NOKIA_MAX_NEG_SIZE 255 | ||
51 | #define HCI_NOKIA_ALIVE_HDR_SIZE 1 | ||
52 | #define HCI_NOKIA_MAX_ALIVE_SIZE 255 | ||
53 | #define HCI_NOKIA_RADIO_HDR_SIZE 2 | ||
54 | #define HCI_NOKIA_MAX_RADIO_SIZE 255 | ||
55 | |||
56 | #define NOKIA_PROTO_PKT 0x44 | ||
57 | #define NOKIA_PROTO_BYTE 0x4c | ||
58 | |||
59 | #define NOKIA_NEG_REQ 0x00 | ||
60 | #define NOKIA_NEG_ACK 0x20 | ||
61 | #define NOKIA_NEG_NAK 0x40 | ||
62 | |||
63 | #define H4_TYPE_SIZE 1 | ||
64 | |||
65 | #define NOKIA_RECV_ALIVE \ | ||
66 | .type = HCI_NOKIA_ALIVE_PKT, \ | ||
67 | .hlen = HCI_NOKIA_ALIVE_HDR_SIZE, \ | ||
68 | .loff = 0, \ | ||
69 | .lsize = 1, \ | ||
70 | .maxlen = HCI_NOKIA_MAX_ALIVE_SIZE \ | ||
71 | |||
72 | #define NOKIA_RECV_NEG \ | ||
73 | .type = HCI_NOKIA_NEG_PKT, \ | ||
74 | .hlen = HCI_NOKIA_NEG_HDR_SIZE, \ | ||
75 | .loff = 0, \ | ||
76 | .lsize = 1, \ | ||
77 | .maxlen = HCI_NOKIA_MAX_NEG_SIZE \ | ||
78 | |||
79 | #define NOKIA_RECV_RADIO \ | ||
80 | .type = HCI_NOKIA_RADIO_PKT, \ | ||
81 | .hlen = HCI_NOKIA_RADIO_HDR_SIZE, \ | ||
82 | .loff = 1, \ | ||
83 | .lsize = 1, \ | ||
84 | .maxlen = HCI_NOKIA_MAX_RADIO_SIZE \ | ||
85 | |||
86 | struct hci_nokia_neg_hdr { | ||
87 | u8 dlen; | ||
88 | } __packed; | ||
89 | |||
90 | struct hci_nokia_neg_cmd { | ||
91 | u8 ack; | ||
92 | u16 baud; | ||
93 | u16 unused1; | ||
94 | u8 proto; | ||
95 | u16 sys_clk; | ||
96 | u16 unused2; | ||
97 | } __packed; | ||
98 | |||
99 | #define NOKIA_ALIVE_REQ 0x55 | ||
100 | #define NOKIA_ALIVE_RESP 0xcc | ||
101 | |||
102 | struct hci_nokia_alive_hdr { | ||
103 | u8 dlen; | ||
104 | } __packed; | ||
105 | |||
106 | struct hci_nokia_alive_pkt { | ||
107 | u8 mid; | ||
108 | u8 unused; | ||
109 | } __packed; | ||
110 | |||
111 | struct hci_nokia_neg_evt { | ||
112 | u8 ack; | ||
113 | u16 baud; | ||
114 | u16 unused1; | ||
115 | u8 proto; | ||
116 | u16 sys_clk; | ||
117 | u16 unused2; | ||
118 | u8 man_id; | ||
119 | u8 ver_id; | ||
120 | } __packed; | ||
121 | |||
122 | #define MAX_BAUD_RATE 3692300 | ||
123 | #define SETUP_BAUD_RATE 921600 | ||
124 | #define INIT_BAUD_RATE 120000 | ||
125 | |||
126 | struct hci_nokia_radio_hdr { | ||
127 | u8 evt; | ||
128 | u8 dlen; | ||
129 | } __packed; | ||
130 | |||
131 | struct nokia_bt_dev { | ||
132 | struct hci_uart hu; | ||
133 | struct serdev_device *serdev; | ||
134 | |||
135 | struct gpio_desc *reset; | ||
136 | struct gpio_desc *wakeup_host; | ||
137 | struct gpio_desc *wakeup_bt; | ||
138 | unsigned long sysclk_speed; | ||
139 | |||
140 | int wake_irq; | ||
141 | struct sk_buff *rx_skb; | ||
142 | struct sk_buff_head txq; | ||
143 | bdaddr_t bdaddr; | ||
144 | |||
145 | int init_error; | ||
146 | struct completion init_completion; | ||
147 | |||
148 | u8 man_id; | ||
149 | u8 ver_id; | ||
150 | |||
151 | bool initialized; | ||
152 | bool tx_enabled; | ||
153 | bool rx_enabled; | ||
154 | }; | ||
155 | |||
156 | static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb); | ||
157 | |||
158 | static void nokia_flow_control(struct serdev_device *serdev, bool enable) | ||
159 | { | ||
160 | if (enable) { | ||
161 | serdev_device_set_rts(serdev, true); | ||
162 | serdev_device_set_flow_control(serdev, true); | ||
163 | } else { | ||
164 | serdev_device_set_flow_control(serdev, false); | ||
165 | serdev_device_set_rts(serdev, false); | ||
166 | } | ||
167 | } | ||
168 | |||
169 | static irqreturn_t wakeup_handler(int irq, void *data) | ||
170 | { | ||
171 | struct nokia_bt_dev *btdev = data; | ||
172 | struct device *dev = &btdev->serdev->dev; | ||
173 | int wake_state = gpiod_get_value(btdev->wakeup_host); | ||
174 | |||
175 | if (btdev->rx_enabled == wake_state) | ||
176 | return IRQ_HANDLED; | ||
177 | |||
178 | if (wake_state) | ||
179 | pm_runtime_get(dev); | ||
180 | else | ||
181 | pm_runtime_put(dev); | ||
182 | |||
183 | btdev->rx_enabled = wake_state; | ||
184 | |||
185 | return IRQ_HANDLED; | ||
186 | } | ||
187 | |||
188 | static int nokia_reset(struct hci_uart *hu) | ||
189 | { | ||
190 | struct nokia_bt_dev *btdev = hu->priv; | ||
191 | struct device *dev = &btdev->serdev->dev; | ||
192 | int err; | ||
193 | |||
194 | /* reset routine */ | ||
195 | gpiod_set_value_cansleep(btdev->reset, 1); | ||
196 | gpiod_set_value_cansleep(btdev->wakeup_bt, 1); | ||
197 | |||
198 | msleep(100); | ||
199 | |||
200 | /* safety check */ | ||
201 | err = gpiod_get_value_cansleep(btdev->wakeup_host); | ||
202 | if (err == 1) { | ||
203 | dev_err(dev, "reset: host wakeup not low!"); | ||
204 | return -EPROTO; | ||
205 | } | ||
206 | |||
207 | /* flush queue */ | ||
208 | serdev_device_write_flush(btdev->serdev); | ||
209 | |||
210 | /* init uart */ | ||
211 | nokia_flow_control(btdev->serdev, false); | ||
212 | serdev_device_set_baudrate(btdev->serdev, INIT_BAUD_RATE); | ||
213 | |||
214 | gpiod_set_value_cansleep(btdev->reset, 0); | ||
215 | |||
216 | /* wait for cts */ | ||
217 | err = serdev_device_wait_for_cts(btdev->serdev, true, 200); | ||
218 | if (err < 0) { | ||
219 | dev_err(dev, "CTS not received: %d", err); | ||
220 | return err; | ||
221 | } | ||
222 | |||
223 | nokia_flow_control(btdev->serdev, true); | ||
224 | |||
225 | return 0; | ||
226 | } | ||
227 | |||
228 | static int nokia_send_alive_packet(struct hci_uart *hu) | ||
229 | { | ||
230 | struct nokia_bt_dev *btdev = hu->priv; | ||
231 | struct device *dev = &btdev->serdev->dev; | ||
232 | struct hci_nokia_alive_hdr *hdr; | ||
233 | struct hci_nokia_alive_pkt *pkt; | ||
234 | struct sk_buff *skb; | ||
235 | int len; | ||
236 | |||
237 | init_completion(&btdev->init_completion); | ||
238 | |||
239 | len = H4_TYPE_SIZE + sizeof(*hdr) + sizeof(*pkt); | ||
240 | skb = bt_skb_alloc(len, GFP_KERNEL); | ||
241 | if (!skb) | ||
242 | return -ENOMEM; | ||
243 | |||
244 | hci_skb_pkt_type(skb) = HCI_NOKIA_ALIVE_PKT; | ||
245 | memset(skb->data, 0x00, len); | ||
246 | |||
247 | hdr = (struct hci_nokia_alive_hdr *)skb_put(skb, sizeof(*hdr)); | ||
248 | hdr->dlen = sizeof(*pkt); | ||
249 | pkt = (struct hci_nokia_alive_pkt *)skb_put(skb, sizeof(*pkt)); | ||
250 | pkt->mid = NOKIA_ALIVE_REQ; | ||
251 | |||
252 | nokia_enqueue(hu, skb); | ||
253 | hci_uart_tx_wakeup(hu); | ||
254 | |||
255 | dev_dbg(dev, "Alive sent"); | ||
256 | |||
257 | if (!wait_for_completion_interruptible_timeout(&btdev->init_completion, | ||
258 | msecs_to_jiffies(1000))) { | ||
259 | return -ETIMEDOUT; | ||
260 | } | ||
261 | |||
262 | if (btdev->init_error < 0) | ||
263 | return btdev->init_error; | ||
264 | |||
265 | return 0; | ||
266 | } | ||
267 | |||
268 | static int nokia_send_negotiation(struct hci_uart *hu) | ||
269 | { | ||
270 | struct nokia_bt_dev *btdev = hu->priv; | ||
271 | struct device *dev = &btdev->serdev->dev; | ||
272 | struct hci_nokia_neg_cmd *neg_cmd; | ||
273 | struct hci_nokia_neg_hdr *neg_hdr; | ||
274 | struct sk_buff *skb; | ||
275 | int len, err; | ||
276 | u16 baud = DIV_ROUND_CLOSEST(btdev->sysclk_speed * 10, SETUP_BAUD_RATE); | ||
277 | int sysclk = btdev->sysclk_speed / 1000; | ||
278 | |||
279 | len = H4_TYPE_SIZE + sizeof(*neg_hdr) + sizeof(*neg_cmd); | ||
280 | skb = bt_skb_alloc(len, GFP_KERNEL); | ||
281 | if (!skb) | ||
282 | return -ENOMEM; | ||
283 | |||
284 | hci_skb_pkt_type(skb) = HCI_NOKIA_NEG_PKT; | ||
285 | |||
286 | neg_hdr = (struct hci_nokia_neg_hdr *)skb_put(skb, sizeof(*neg_hdr)); | ||
287 | neg_hdr->dlen = sizeof(*neg_cmd); | ||
288 | |||
289 | neg_cmd = (struct hci_nokia_neg_cmd *)skb_put(skb, sizeof(*neg_cmd)); | ||
290 | neg_cmd->ack = NOKIA_NEG_REQ; | ||
291 | neg_cmd->baud = cpu_to_le16(baud); | ||
292 | neg_cmd->unused1 = 0x0000; | ||
293 | neg_cmd->proto = NOKIA_PROTO_BYTE; | ||
294 | neg_cmd->sys_clk = cpu_to_le16(sysclk); | ||
295 | neg_cmd->unused2 = 0x0000; | ||
296 | |||
297 | btdev->init_error = 0; | ||
298 | init_completion(&btdev->init_completion); | ||
299 | |||
300 | nokia_enqueue(hu, skb); | ||
301 | hci_uart_tx_wakeup(hu); | ||
302 | |||
303 | dev_dbg(dev, "Negotiation sent"); | ||
304 | |||
305 | if (!wait_for_completion_interruptible_timeout(&btdev->init_completion, | ||
306 | msecs_to_jiffies(10000))) { | ||
307 | return -ETIMEDOUT; | ||
308 | } | ||
309 | |||
310 | if (btdev->init_error < 0) | ||
311 | return btdev->init_error; | ||
312 | |||
313 | /* Change to previously negotiated speed. Flow Control | ||
314 | * is disabled until bluetooth adapter is ready to avoid | ||
315 | * broken bytes being received. | ||
316 | */ | ||
317 | nokia_flow_control(btdev->serdev, false); | ||
318 | serdev_device_set_baudrate(btdev->serdev, SETUP_BAUD_RATE); | ||
319 | err = serdev_device_wait_for_cts(btdev->serdev, true, 200); | ||
320 | if (err < 0) { | ||
321 | dev_err(dev, "CTS not received: %d", err); | ||
322 | return err; | ||
323 | } | ||
324 | nokia_flow_control(btdev->serdev, true); | ||
325 | |||
326 | dev_dbg(dev, "Negotiation successful"); | ||
327 | |||
328 | return 0; | ||
329 | } | ||
330 | |||
331 | static int nokia_setup_fw(struct hci_uart *hu) | ||
332 | { | ||
333 | struct nokia_bt_dev *btdev = hu->priv; | ||
334 | struct device *dev = &btdev->serdev->dev; | ||
335 | const char *fwname; | ||
336 | const struct firmware *fw; | ||
337 | const u8 *fw_ptr; | ||
338 | size_t fw_size; | ||
339 | int err; | ||
340 | |||
341 | dev_dbg(dev, "setup firmware"); | ||
342 | |||
343 | if (btdev->man_id == NOKIA_ID_BCM2048) { | ||
344 | fwname = FIRMWARE_BCM2048; | ||
345 | } else if (btdev->man_id == NOKIA_ID_TI1271) { | ||
346 | fwname = FIRMWARE_TI1271; | ||
347 | } else { | ||
348 | dev_err(dev, "Unsupported bluetooth device!"); | ||
349 | return -ENODEV; | ||
350 | } | ||
351 | |||
352 | err = request_firmware(&fw, fwname, dev); | ||
353 | if (err < 0) { | ||
354 | dev_err(dev, "%s: Failed to load Nokia firmware file (%d)", | ||
355 | hu->hdev->name, err); | ||
356 | return err; | ||
357 | } | ||
358 | |||
359 | fw_ptr = fw->data; | ||
360 | fw_size = fw->size; | ||
361 | |||
362 | while (fw_size >= 4) { | ||
363 | u16 pkt_size = get_unaligned_le16(fw_ptr); | ||
364 | u8 pkt_type = fw_ptr[2]; | ||
365 | const struct hci_command_hdr *cmd; | ||
366 | u16 opcode; | ||
367 | struct sk_buff *skb; | ||
368 | |||
369 | switch (pkt_type) { | ||
370 | case HCI_COMMAND_PKT: | ||
371 | cmd = (struct hci_command_hdr *)(fw_ptr + 3); | ||
372 | opcode = le16_to_cpu(cmd->opcode); | ||
373 | |||
374 | skb = __hci_cmd_sync(hu->hdev, opcode, cmd->plen, | ||
375 | fw_ptr + 3 + HCI_COMMAND_HDR_SIZE, | ||
376 | HCI_INIT_TIMEOUT); | ||
377 | if (IS_ERR(skb)) { | ||
378 | err = PTR_ERR(skb); | ||
379 | dev_err(dev, "%s: FW command %04x failed (%d)", | ||
380 | hu->hdev->name, opcode, err); | ||
381 | goto done; | ||
382 | } | ||
383 | kfree_skb(skb); | ||
384 | break; | ||
385 | case HCI_NOKIA_RADIO_PKT: | ||
386 | case HCI_NOKIA_NEG_PKT: | ||
387 | case HCI_NOKIA_ALIVE_PKT: | ||
388 | break; | ||
389 | } | ||
390 | |||
391 | fw_ptr += pkt_size + 2; | ||
392 | fw_size -= pkt_size + 2; | ||
393 | } | ||
394 | |||
395 | done: | ||
396 | release_firmware(fw); | ||
397 | return err; | ||
398 | } | ||
399 | |||
400 | static int nokia_setup(struct hci_uart *hu) | ||
401 | { | ||
402 | struct nokia_bt_dev *btdev = hu->priv; | ||
403 | struct device *dev = &btdev->serdev->dev; | ||
404 | int err; | ||
405 | |||
406 | btdev->initialized = false; | ||
407 | |||
408 | nokia_flow_control(btdev->serdev, false); | ||
409 | |||
410 | pm_runtime_get_sync(dev); | ||
411 | |||
412 | if (btdev->tx_enabled) { | ||
413 | gpiod_set_value_cansleep(btdev->wakeup_bt, 0); | ||
414 | pm_runtime_put(&btdev->serdev->dev); | ||
415 | btdev->tx_enabled = false; | ||
416 | } | ||
417 | |||
418 | dev_dbg(dev, "protocol setup"); | ||
419 | |||
420 | /* 0. reset connection */ | ||
421 | err = nokia_reset(hu); | ||
422 | if (err < 0) { | ||
423 | dev_err(dev, "Reset failed: %d", err); | ||
424 | goto out; | ||
425 | } | ||
426 | |||
427 | /* 1. negotiate speed etc */ | ||
428 | err = nokia_send_negotiation(hu); | ||
429 | if (err < 0) { | ||
430 | dev_err(dev, "Negotiation failed: %d", err); | ||
431 | goto out; | ||
432 | } | ||
433 | |||
434 | /* 2. verify correct setup using alive packet */ | ||
435 | err = nokia_send_alive_packet(hu); | ||
436 | if (err < 0) { | ||
437 | dev_err(dev, "Alive check failed: %d", err); | ||
438 | goto out; | ||
439 | } | ||
440 | |||
441 | /* 3. send firmware */ | ||
442 | err = nokia_setup_fw(hu); | ||
443 | if (err < 0) { | ||
444 | dev_err(dev, "Could not setup FW: %d", err); | ||
445 | goto out; | ||
446 | } | ||
447 | |||
448 | nokia_flow_control(btdev->serdev, false); | ||
449 | serdev_device_set_baudrate(btdev->serdev, MAX_BAUD_RATE); | ||
450 | nokia_flow_control(btdev->serdev, true); | ||
451 | |||
452 | if (btdev->man_id == NOKIA_ID_BCM2048) { | ||
453 | hu->hdev->set_bdaddr = btbcm_set_bdaddr; | ||
454 | set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks); | ||
455 | dev_dbg(dev, "bcm2048 has invalid bluetooth address!"); | ||
456 | } | ||
457 | |||
458 | dev_dbg(dev, "protocol setup done!"); | ||
459 | |||
460 | gpiod_set_value_cansleep(btdev->wakeup_bt, 0); | ||
461 | pm_runtime_put(dev); | ||
462 | btdev->tx_enabled = false; | ||
463 | btdev->initialized = true; | ||
464 | |||
465 | return 0; | ||
466 | out: | ||
467 | pm_runtime_put(dev); | ||
468 | |||
469 | return err; | ||
470 | } | ||
471 | |||
472 | static int nokia_open(struct hci_uart *hu) | ||
473 | { | ||
474 | struct device *dev = &hu->serdev->dev; | ||
475 | |||
476 | dev_dbg(dev, "protocol open"); | ||
477 | |||
478 | serdev_device_open(hu->serdev); | ||
479 | |||
480 | pm_runtime_enable(dev); | ||
481 | |||
482 | return 0; | ||
483 | } | ||
484 | |||
485 | static int nokia_flush(struct hci_uart *hu) | ||
486 | { | ||
487 | struct nokia_bt_dev *btdev = hu->priv; | ||
488 | |||
489 | dev_dbg(&btdev->serdev->dev, "flush device"); | ||
490 | |||
491 | skb_queue_purge(&btdev->txq); | ||
492 | |||
493 | return 0; | ||
494 | } | ||
495 | |||
496 | static int nokia_close(struct hci_uart *hu) | ||
497 | { | ||
498 | struct nokia_bt_dev *btdev = hu->priv; | ||
499 | struct device *dev = &btdev->serdev->dev; | ||
500 | |||
501 | dev_dbg(dev, "close device"); | ||
502 | |||
503 | btdev->initialized = false; | ||
504 | |||
505 | skb_queue_purge(&btdev->txq); | ||
506 | |||
507 | kfree_skb(btdev->rx_skb); | ||
508 | |||
509 | /* disable module */ | ||
510 | gpiod_set_value(btdev->reset, 1); | ||
511 | gpiod_set_value(btdev->wakeup_bt, 0); | ||
512 | |||
513 | pm_runtime_disable(&btdev->serdev->dev); | ||
514 | serdev_device_close(btdev->serdev); | ||
515 | |||
516 | return 0; | ||
517 | } | ||
518 | |||
519 | /* Enqueue frame for transmittion (padding, crc, etc) */ | ||
520 | static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb) | ||
521 | { | ||
522 | struct nokia_bt_dev *btdev = hu->priv; | ||
523 | int err; | ||
524 | |||
525 | /* Prepend skb with frame type */ | ||
526 | memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); | ||
527 | |||
528 | /* Packets must be word aligned */ | ||
529 | if (skb->len % 2) { | ||
530 | err = skb_pad(skb, 1); | ||
531 | if (err) | ||
532 | return err; | ||
533 | *skb_put(skb, 1) = 0x00; | ||
534 | } | ||
535 | |||
536 | skb_queue_tail(&btdev->txq, skb); | ||
537 | |||
538 | return 0; | ||
539 | } | ||
540 | |||
541 | static int nokia_recv_negotiation_packet(struct hci_dev *hdev, | ||
542 | struct sk_buff *skb) | ||
543 | { | ||
544 | struct hci_uart *hu = hci_get_drvdata(hdev); | ||
545 | struct nokia_bt_dev *btdev = hu->priv; | ||
546 | struct device *dev = &btdev->serdev->dev; | ||
547 | struct hci_nokia_neg_hdr *hdr; | ||
548 | struct hci_nokia_neg_evt *evt; | ||
549 | int ret = 0; | ||
550 | |||
551 | hdr = (struct hci_nokia_neg_hdr *)skb->data; | ||
552 | if (hdr->dlen != sizeof(*evt)) { | ||
553 | btdev->init_error = -EIO; | ||
554 | ret = -EIO; | ||
555 | goto finish_neg; | ||
556 | } | ||
557 | |||
558 | evt = (struct hci_nokia_neg_evt *)skb_pull(skb, sizeof(*hdr)); | ||
559 | |||
560 | if (evt->ack != NOKIA_NEG_ACK) { | ||
561 | dev_err(dev, "Negotiation received: wrong reply"); | ||
562 | btdev->init_error = -EINVAL; | ||
563 | ret = -EINVAL; | ||
564 | goto finish_neg; | ||
565 | } | ||
566 | |||
567 | btdev->man_id = evt->man_id; | ||
568 | btdev->ver_id = evt->ver_id; | ||
569 | |||
570 | dev_dbg(dev, "Negotiation received: baud=%u:clk=%u:manu=%u:vers=%u", | ||
571 | evt->baud, evt->sys_clk, evt->man_id, evt->ver_id); | ||
572 | |||
573 | finish_neg: | ||
574 | complete(&btdev->init_completion); | ||
575 | kfree_skb(skb); | ||
576 | return ret; | ||
577 | } | ||
578 | |||
579 | static int nokia_recv_alive_packet(struct hci_dev *hdev, struct sk_buff *skb) | ||
580 | { | ||
581 | struct hci_uart *hu = hci_get_drvdata(hdev); | ||
582 | struct nokia_bt_dev *btdev = hu->priv; | ||
583 | struct device *dev = &btdev->serdev->dev; | ||
584 | struct hci_nokia_alive_hdr *hdr; | ||
585 | struct hci_nokia_alive_pkt *pkt; | ||
586 | int ret = 0; | ||
587 | |||
588 | hdr = (struct hci_nokia_alive_hdr *)skb->data; | ||
589 | if (hdr->dlen != sizeof(*pkt)) { | ||
590 | dev_err(dev, "Corrupted alive message"); | ||
591 | btdev->init_error = -EIO; | ||
592 | ret = -EIO; | ||
593 | goto finish_alive; | ||
594 | } | ||
595 | |||
596 | pkt = (struct hci_nokia_alive_pkt *)skb_pull(skb, sizeof(*hdr)); | ||
597 | |||
598 | if (pkt->mid != NOKIA_ALIVE_RESP) { | ||
599 | dev_err(dev, "Alive received: invalid response: 0x%02x!", | ||
600 | pkt->mid); | ||
601 | btdev->init_error = -EINVAL; | ||
602 | ret = -EINVAL; | ||
603 | goto finish_alive; | ||
604 | } | ||
605 | |||
606 | dev_dbg(dev, "Alive received"); | ||
607 | |||
608 | finish_alive: | ||
609 | complete(&btdev->init_completion); | ||
610 | kfree_skb(skb); | ||
611 | return ret; | ||
612 | } | ||
613 | |||
614 | static int nokia_recv_radio(struct hci_dev *hdev, struct sk_buff *skb) | ||
615 | { | ||
616 | /* Packets received on the dedicated radio channel are | ||
617 | * HCI events and so feed them back into the core. | ||
618 | */ | ||
619 | hci_skb_pkt_type(skb) = HCI_EVENT_PKT; | ||
620 | return hci_recv_frame(hdev, skb); | ||
621 | } | ||
622 | |||
623 | /* Recv data */ | ||
624 | static const struct h4_recv_pkt nokia_recv_pkts[] = { | ||
625 | { H4_RECV_ACL, .recv = hci_recv_frame }, | ||
626 | { H4_RECV_SCO, .recv = hci_recv_frame }, | ||
627 | { H4_RECV_EVENT, .recv = hci_recv_frame }, | ||
628 | { NOKIA_RECV_ALIVE, .recv = nokia_recv_alive_packet }, | ||
629 | { NOKIA_RECV_NEG, .recv = nokia_recv_negotiation_packet }, | ||
630 | { NOKIA_RECV_RADIO, .recv = nokia_recv_radio }, | ||
631 | }; | ||
632 | |||
633 | static int nokia_recv(struct hci_uart *hu, const void *data, int count) | ||
634 | { | ||
635 | struct nokia_bt_dev *btdev = hu->priv; | ||
636 | struct device *dev = &btdev->serdev->dev; | ||
637 | int err; | ||
638 | |||
639 | if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) | ||
640 | return -EUNATCH; | ||
641 | |||
642 | btdev->rx_skb = h4_recv_buf(hu->hdev, btdev->rx_skb, data, count, | ||
643 | nokia_recv_pkts, ARRAY_SIZE(nokia_recv_pkts)); | ||
644 | if (IS_ERR(btdev->rx_skb)) { | ||
645 | err = PTR_ERR(btdev->rx_skb); | ||
646 | dev_err(dev, "Frame reassembly failed (%d)", err); | ||
647 | btdev->rx_skb = NULL; | ||
648 | return err; | ||
649 | } | ||
650 | |||
651 | return count; | ||
652 | } | ||
653 | |||
654 | static struct sk_buff *nokia_dequeue(struct hci_uart *hu) | ||
655 | { | ||
656 | struct nokia_bt_dev *btdev = hu->priv; | ||
657 | struct device *dev = &btdev->serdev->dev; | ||
658 | struct sk_buff *result = skb_dequeue(&btdev->txq); | ||
659 | |||
660 | if (!btdev->initialized) | ||
661 | return result; | ||
662 | |||
663 | if (btdev->tx_enabled == !!result) | ||
664 | return result; | ||
665 | |||
666 | if (result) { | ||
667 | pm_runtime_get_sync(dev); | ||
668 | gpiod_set_value_cansleep(btdev->wakeup_bt, 1); | ||
669 | } else { | ||
670 | serdev_device_wait_until_sent(btdev->serdev, 0); | ||
671 | gpiod_set_value_cansleep(btdev->wakeup_bt, 0); | ||
672 | pm_runtime_put(dev); | ||
673 | } | ||
674 | |||
675 | btdev->tx_enabled = !!result; | ||
676 | |||
677 | return result; | ||
678 | } | ||
679 | |||
680 | static const struct hci_uart_proto nokia_proto = { | ||
681 | .id = HCI_UART_NOKIA, | ||
682 | .name = "Nokia", | ||
683 | .open = nokia_open, | ||
684 | .close = nokia_close, | ||
685 | .recv = nokia_recv, | ||
686 | .enqueue = nokia_enqueue, | ||
687 | .dequeue = nokia_dequeue, | ||
688 | .flush = nokia_flush, | ||
689 | .setup = nokia_setup, | ||
690 | .manufacturer = 1, | ||
691 | }; | ||
692 | |||
693 | static int nokia_bluetooth_serdev_probe(struct serdev_device *serdev) | ||
694 | { | ||
695 | struct device *dev = &serdev->dev; | ||
696 | struct nokia_bt_dev *btdev; | ||
697 | struct clk *sysclk; | ||
698 | int err = 0; | ||
699 | |||
700 | btdev = devm_kzalloc(dev, sizeof(*btdev), GFP_KERNEL); | ||
701 | if (!btdev) | ||
702 | return -ENOMEM; | ||
703 | |||
704 | btdev->hu.serdev = btdev->serdev = serdev; | ||
705 | serdev_device_set_drvdata(serdev, btdev); | ||
706 | |||
707 | btdev->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); | ||
708 | if (IS_ERR(btdev->reset)) { | ||
709 | err = PTR_ERR(btdev->reset); | ||
710 | dev_err(dev, "could not get reset gpio: %d", err); | ||
711 | return err; | ||
712 | } | ||
713 | |||
714 | btdev->wakeup_host = devm_gpiod_get(dev, "host-wakeup", GPIOD_IN); | ||
715 | if (IS_ERR(btdev->wakeup_host)) { | ||
716 | err = PTR_ERR(btdev->wakeup_host); | ||
717 | dev_err(dev, "could not get host wakeup gpio: %d", err); | ||
718 | return err; | ||
719 | } | ||
720 | |||
721 | btdev->wake_irq = gpiod_to_irq(btdev->wakeup_host); | ||
722 | |||
723 | err = devm_request_threaded_irq(dev, btdev->wake_irq, NULL, | ||
724 | wakeup_handler, | ||
725 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, | ||
726 | "wakeup", btdev); | ||
727 | if (err) { | ||
728 | dev_err(dev, "could request wakeup irq: %d", err); | ||
729 | return err; | ||
730 | } | ||
731 | |||
732 | btdev->wakeup_bt = devm_gpiod_get(dev, "bluetooth-wakeup", | ||
733 | GPIOD_OUT_LOW); | ||
734 | if (IS_ERR(btdev->wakeup_bt)) { | ||
735 | err = PTR_ERR(btdev->wakeup_bt); | ||
736 | dev_err(dev, "could not get BT wakeup gpio: %d", err); | ||
737 | return err; | ||
738 | } | ||
739 | |||
740 | sysclk = devm_clk_get(dev, "sysclk"); | ||
741 | if (IS_ERR(sysclk)) { | ||
742 | err = PTR_ERR(sysclk); | ||
743 | dev_err(dev, "could not get sysclk: %d", err); | ||
744 | return err; | ||
745 | } | ||
746 | |||
747 | clk_prepare_enable(sysclk); | ||
748 | btdev->sysclk_speed = clk_get_rate(sysclk); | ||
749 | clk_disable_unprepare(sysclk); | ||
750 | |||
751 | skb_queue_head_init(&btdev->txq); | ||
752 | |||
753 | btdev->hu.priv = btdev; | ||
754 | btdev->hu.alignment = 2; /* Nokia H4+ is word aligned */ | ||
755 | |||
756 | err = hci_uart_register_device(&btdev->hu, &nokia_proto); | ||
757 | if (err) { | ||
758 | dev_err(dev, "could not register bluetooth uart: %d", err); | ||
759 | return err; | ||
760 | } | ||
761 | |||
762 | return 0; | ||
763 | } | ||
764 | |||
765 | static void nokia_bluetooth_serdev_remove(struct serdev_device *serdev) | ||
766 | { | ||
767 | struct nokia_bt_dev *btdev = serdev_device_get_drvdata(serdev); | ||
768 | struct hci_uart *hu = &btdev->hu; | ||
769 | struct hci_dev *hdev = hu->hdev; | ||
770 | |||
771 | cancel_work_sync(&hu->write_work); | ||
772 | |||
773 | hci_unregister_dev(hdev); | ||
774 | hci_free_dev(hdev); | ||
775 | hu->proto->close(hu); | ||
776 | |||
777 | pm_runtime_disable(&btdev->serdev->dev); | ||
778 | } | ||
779 | |||
780 | static int nokia_bluetooth_runtime_suspend(struct device *dev) | ||
781 | { | ||
782 | struct serdev_device *serdev = to_serdev_device(dev); | ||
783 | |||
784 | nokia_flow_control(serdev, false); | ||
785 | return 0; | ||
786 | } | ||
787 | |||
788 | static int nokia_bluetooth_runtime_resume(struct device *dev) | ||
789 | { | ||
790 | struct serdev_device *serdev = to_serdev_device(dev); | ||
791 | |||
792 | nokia_flow_control(serdev, true); | ||
793 | return 0; | ||
794 | } | ||
795 | |||
796 | static const struct dev_pm_ops nokia_bluetooth_pm_ops = { | ||
797 | SET_RUNTIME_PM_OPS(nokia_bluetooth_runtime_suspend, | ||
798 | nokia_bluetooth_runtime_resume, | ||
799 | NULL) | ||
800 | }; | ||
801 | |||
802 | #ifdef CONFIG_OF | ||
803 | static const struct of_device_id nokia_bluetooth_of_match[] = { | ||
804 | { .compatible = "nokia,h4p-bluetooth", }, | ||
805 | {}, | ||
806 | }; | ||
807 | MODULE_DEVICE_TABLE(of, nokia_bluetooth_of_match); | ||
808 | #endif | ||
809 | |||
810 | static struct serdev_device_driver nokia_bluetooth_serdev_driver = { | ||
811 | .probe = nokia_bluetooth_serdev_probe, | ||
812 | .remove = nokia_bluetooth_serdev_remove, | ||
813 | .driver = { | ||
814 | .name = "nokia-bluetooth", | ||
815 | .pm = &nokia_bluetooth_pm_ops, | ||
816 | .of_match_table = of_match_ptr(nokia_bluetooth_of_match), | ||
817 | }, | ||
818 | }; | ||
819 | |||
820 | module_serdev_device_driver(nokia_bluetooth_serdev_driver); | ||