Commit | Line | Data |
---|---|---|
166d2f6a OBC |
1 | /* |
2 | * Texas Instruments' Bluetooth HCILL UART protocol | |
3 | * | |
4 | * HCILL (HCI Low Level) is a Texas Instruments' power management | |
5 | * protocol extension to H4. | |
6 | * | |
7 | * Copyright (C) 2007 Texas Instruments, Inc. | |
8 | * | |
9 | * Written by Ohad Ben-Cohen <ohad@bencohen.org> | |
10 | * | |
11 | * Acknowledgements: | |
12 | * This file is based on hci_h4.c, which was written | |
13 | * by Maxim Krasnyansky and Marcel Holtmann. | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or modify | |
16 | * it under the terms of the GNU General Public License version 2 | |
17 | * as published by the Free Software Foundation | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
27 | * | |
28 | */ | |
29 | ||
30 | #include <linux/module.h> | |
31 | #include <linux/kernel.h> | |
32 | ||
33 | #include <linux/init.h> | |
34 | #include <linux/sched.h> | |
35 | #include <linux/types.h> | |
36 | #include <linux/fcntl.h> | |
37180552 | 37 | #include <linux/firmware.h> |
166d2f6a OBC |
38 | #include <linux/interrupt.h> |
39 | #include <linux/ptrace.h> | |
40 | #include <linux/poll.h> | |
41 | ||
42 | #include <linux/slab.h> | |
166d2f6a OBC |
43 | #include <linux/errno.h> |
44 | #include <linux/string.h> | |
45 | #include <linux/signal.h> | |
46 | #include <linux/ioctl.h> | |
37180552 RH |
47 | #include <linux/of.h> |
48 | #include <linux/serdev.h> | |
166d2f6a | 49 | #include <linux/skbuff.h> |
37180552 | 50 | #include <linux/ti_wilink_st.h> |
43d3d092 | 51 | #include <linux/clk.h> |
166d2f6a OBC |
52 | |
53 | #include <net/bluetooth/bluetooth.h> | |
54 | #include <net/bluetooth/hci_core.h> | |
37180552 | 55 | #include <linux/gpio/consumer.h> |
166d2f6a OBC |
56 | |
57 | #include "hci_uart.h" | |
58 | ||
7c6ca120 DL |
59 | /* Vendor-specific HCI commands */ |
60 | #define HCI_VS_UPDATE_UART_HCI_BAUDRATE 0xff36 | |
61 | ||
166d2f6a OBC |
62 | /* HCILL commands */ |
63 | #define HCILL_GO_TO_SLEEP_IND 0x30 | |
64 | #define HCILL_GO_TO_SLEEP_ACK 0x31 | |
65 | #define HCILL_WAKE_UP_IND 0x32 | |
66 | #define HCILL_WAKE_UP_ACK 0x33 | |
67 | ||
68 | /* HCILL receiver States */ | |
69 | #define HCILL_W4_PACKET_TYPE 0 | |
70 | #define HCILL_W4_EVENT_HDR 1 | |
71 | #define HCILL_W4_ACL_HDR 2 | |
72 | #define HCILL_W4_SCO_HDR 3 | |
73 | #define HCILL_W4_DATA 4 | |
74 | ||
75 | /* HCILL states */ | |
76 | enum hcill_states_e { | |
77 | HCILL_ASLEEP, | |
78 | HCILL_ASLEEP_TO_AWAKE, | |
79 | HCILL_AWAKE, | |
80 | HCILL_AWAKE_TO_ASLEEP | |
81 | }; | |
82 | ||
83 | struct hcill_cmd { | |
84 | u8 cmd; | |
81ca405a | 85 | } __packed; |
166d2f6a | 86 | |
37180552 RH |
87 | struct ll_device { |
88 | struct hci_uart hu; | |
89 | struct serdev_device *serdev; | |
90 | struct gpio_desc *enable_gpio; | |
43d3d092 | 91 | struct clk *ext_clk; |
37180552 RH |
92 | }; |
93 | ||
166d2f6a OBC |
94 | struct ll_struct { |
95 | unsigned long rx_state; | |
96 | unsigned long rx_count; | |
97 | struct sk_buff *rx_skb; | |
98 | struct sk_buff_head txq; | |
99 | spinlock_t hcill_lock; /* HCILL state lock */ | |
100 | unsigned long hcill_state; /* HCILL power state */ | |
101 | struct sk_buff_head tx_wait_q; /* HCILL wait queue */ | |
102 | }; | |
103 | ||
104 | /* | |
105 | * Builds and sends an HCILL command packet. | |
106 | * These are very simple packets with only 1 cmd byte | |
107 | */ | |
108 | static int send_hcill_cmd(u8 cmd, struct hci_uart *hu) | |
109 | { | |
110 | int err = 0; | |
111 | struct sk_buff *skb = NULL; | |
112 | struct ll_struct *ll = hu->priv; | |
113 | struct hcill_cmd *hcill_packet; | |
114 | ||
115 | BT_DBG("hu %p cmd 0x%x", hu, cmd); | |
116 | ||
117 | /* allocate packet */ | |
118 | skb = bt_skb_alloc(1, GFP_ATOMIC); | |
119 | if (!skb) { | |
120 | BT_ERR("cannot allocate memory for HCILL packet"); | |
121 | err = -ENOMEM; | |
122 | goto out; | |
123 | } | |
124 | ||
125 | /* prepare packet */ | |
4df864c1 | 126 | hcill_packet = skb_put(skb, 1); |
166d2f6a | 127 | hcill_packet->cmd = cmd; |
166d2f6a OBC |
128 | |
129 | /* send packet */ | |
130 | skb_queue_tail(&ll->txq, skb); | |
131 | out: | |
132 | return err; | |
133 | } | |
134 | ||
135 | /* Initialize protocol */ | |
136 | static int ll_open(struct hci_uart *hu) | |
137 | { | |
138 | struct ll_struct *ll; | |
139 | ||
140 | BT_DBG("hu %p", hu); | |
141 | ||
9eb648c3 | 142 | ll = kzalloc(sizeof(*ll), GFP_KERNEL); |
166d2f6a OBC |
143 | if (!ll) |
144 | return -ENOMEM; | |
145 | ||
146 | skb_queue_head_init(&ll->txq); | |
147 | skb_queue_head_init(&ll->tx_wait_q); | |
148 | spin_lock_init(&ll->hcill_lock); | |
149 | ||
150 | ll->hcill_state = HCILL_AWAKE; | |
151 | ||
152 | hu->priv = ll; | |
153 | ||
43d3d092 UH |
154 | if (hu->serdev) { |
155 | struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev); | |
37180552 | 156 | serdev_device_open(hu->serdev); |
43d3d092 UH |
157 | if (!IS_ERR(lldev->ext_clk)) |
158 | clk_prepare_enable(lldev->ext_clk); | |
159 | } | |
37180552 | 160 | |
166d2f6a OBC |
161 | return 0; |
162 | } | |
163 | ||
164 | /* Flush protocol data */ | |
165 | static int ll_flush(struct hci_uart *hu) | |
166 | { | |
167 | struct ll_struct *ll = hu->priv; | |
168 | ||
169 | BT_DBG("hu %p", hu); | |
170 | ||
171 | skb_queue_purge(&ll->tx_wait_q); | |
172 | skb_queue_purge(&ll->txq); | |
173 | ||
174 | return 0; | |
175 | } | |
176 | ||
177 | /* Close protocol */ | |
178 | static int ll_close(struct hci_uart *hu) | |
179 | { | |
180 | struct ll_struct *ll = hu->priv; | |
181 | ||
182 | BT_DBG("hu %p", hu); | |
183 | ||
184 | skb_queue_purge(&ll->tx_wait_q); | |
185 | skb_queue_purge(&ll->txq); | |
186 | ||
b1fb0683 | 187 | kfree_skb(ll->rx_skb); |
166d2f6a | 188 | |
37180552 RH |
189 | if (hu->serdev) { |
190 | struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev); | |
191 | gpiod_set_value_cansleep(lldev->enable_gpio, 0); | |
192 | ||
43d3d092 UH |
193 | clk_disable_unprepare(lldev->ext_clk); |
194 | ||
37180552 RH |
195 | serdev_device_close(hu->serdev); |
196 | } | |
197 | ||
166d2f6a OBC |
198 | hu->priv = NULL; |
199 | ||
200 | kfree(ll); | |
201 | ||
202 | return 0; | |
203 | } | |
204 | ||
205 | /* | |
206 | * internal function, which does common work of the device wake up process: | |
207 | * 1. places all pending packets (waiting in tx_wait_q list) in txq list. | |
208 | * 2. changes internal state to HCILL_AWAKE. | |
209 | * Note: assumes that hcill_lock spinlock is taken, | |
210 | * shouldn't be called otherwise! | |
211 | */ | |
212 | static void __ll_do_awake(struct ll_struct *ll) | |
213 | { | |
214 | struct sk_buff *skb = NULL; | |
215 | ||
216 | while ((skb = skb_dequeue(&ll->tx_wait_q))) | |
217 | skb_queue_tail(&ll->txq, skb); | |
218 | ||
219 | ll->hcill_state = HCILL_AWAKE; | |
220 | } | |
221 | ||
222 | /* | |
223 | * Called upon a wake-up-indication from the device | |
224 | */ | |
225 | static void ll_device_want_to_wakeup(struct hci_uart *hu) | |
226 | { | |
227 | unsigned long flags; | |
228 | struct ll_struct *ll = hu->priv; | |
229 | ||
230 | BT_DBG("hu %p", hu); | |
231 | ||
232 | /* lock hcill state */ | |
233 | spin_lock_irqsave(&ll->hcill_lock, flags); | |
234 | ||
235 | switch (ll->hcill_state) { | |
5c548226 OBC |
236 | case HCILL_ASLEEP_TO_AWAKE: |
237 | /* | |
238 | * This state means that both the host and the BRF chip | |
239 | * have simultaneously sent a wake-up-indication packet. | |
25985edc | 240 | * Traditionally, in this case, receiving a wake-up-indication |
5c548226 OBC |
241 | * was enough and an additional wake-up-ack wasn't needed. |
242 | * This has changed with the BRF6350, which does require an | |
243 | * explicit wake-up-ack. Other BRF versions, which do not | |
244 | * require an explicit ack here, do accept it, thus it is | |
245 | * perfectly safe to always send one. | |
246 | */ | |
247 | BT_DBG("dual wake-up-indication"); | |
fac72b24 | 248 | /* fall through */ |
166d2f6a OBC |
249 | case HCILL_ASLEEP: |
250 | /* acknowledge device wake up */ | |
251 | if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) { | |
252 | BT_ERR("cannot acknowledge device wake up"); | |
253 | goto out; | |
254 | } | |
255 | break; | |
166d2f6a | 256 | default: |
5c548226 | 257 | /* any other state is illegal */ |
166d2f6a OBC |
258 | BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state); |
259 | break; | |
260 | } | |
261 | ||
262 | /* send pending packets and change state to HCILL_AWAKE */ | |
263 | __ll_do_awake(ll); | |
264 | ||
265 | out: | |
266 | spin_unlock_irqrestore(&ll->hcill_lock, flags); | |
267 | ||
268 | /* actually send the packets */ | |
269 | hci_uart_tx_wakeup(hu); | |
270 | } | |
271 | ||
272 | /* | |
273 | * Called upon a sleep-indication from the device | |
274 | */ | |
275 | static void ll_device_want_to_sleep(struct hci_uart *hu) | |
276 | { | |
277 | unsigned long flags; | |
278 | struct ll_struct *ll = hu->priv; | |
279 | ||
280 | BT_DBG("hu %p", hu); | |
281 | ||
282 | /* lock hcill state */ | |
283 | spin_lock_irqsave(&ll->hcill_lock, flags); | |
284 | ||
285 | /* sanity check */ | |
286 | if (ll->hcill_state != HCILL_AWAKE) | |
287 | BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state); | |
288 | ||
289 | /* acknowledge device sleep */ | |
290 | if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) { | |
291 | BT_ERR("cannot acknowledge device sleep"); | |
292 | goto out; | |
293 | } | |
294 | ||
295 | /* update state */ | |
296 | ll->hcill_state = HCILL_ASLEEP; | |
297 | ||
298 | out: | |
299 | spin_unlock_irqrestore(&ll->hcill_lock, flags); | |
300 | ||
301 | /* actually send the sleep ack packet */ | |
302 | hci_uart_tx_wakeup(hu); | |
303 | } | |
304 | ||
305 | /* | |
306 | * Called upon wake-up-acknowledgement from the device | |
307 | */ | |
308 | static void ll_device_woke_up(struct hci_uart *hu) | |
309 | { | |
310 | unsigned long flags; | |
311 | struct ll_struct *ll = hu->priv; | |
312 | ||
313 | BT_DBG("hu %p", hu); | |
314 | ||
315 | /* lock hcill state */ | |
316 | spin_lock_irqsave(&ll->hcill_lock, flags); | |
317 | ||
318 | /* sanity check */ | |
319 | if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE) | |
320 | BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state); | |
321 | ||
322 | /* send pending packets and change state to HCILL_AWAKE */ | |
323 | __ll_do_awake(ll); | |
324 | ||
325 | spin_unlock_irqrestore(&ll->hcill_lock, flags); | |
326 | ||
327 | /* actually send the packets */ | |
328 | hci_uart_tx_wakeup(hu); | |
329 | } | |
330 | ||
331 | /* Enqueue frame for transmittion (padding, crc, etc) */ | |
332 | /* may be called from two simultaneous tasklets */ | |
333 | static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb) | |
334 | { | |
335 | unsigned long flags = 0; | |
336 | struct ll_struct *ll = hu->priv; | |
337 | ||
338 | BT_DBG("hu %p skb %p", hu, skb); | |
339 | ||
340 | /* Prepend skb with frame type */ | |
618e8bc2 | 341 | memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); |
166d2f6a OBC |
342 | |
343 | /* lock hcill state */ | |
344 | spin_lock_irqsave(&ll->hcill_lock, flags); | |
345 | ||
346 | /* act according to current state */ | |
347 | switch (ll->hcill_state) { | |
348 | case HCILL_AWAKE: | |
349 | BT_DBG("device awake, sending normally"); | |
350 | skb_queue_tail(&ll->txq, skb); | |
351 | break; | |
352 | case HCILL_ASLEEP: | |
353 | BT_DBG("device asleep, waking up and queueing packet"); | |
354 | /* save packet for later */ | |
355 | skb_queue_tail(&ll->tx_wait_q, skb); | |
356 | /* awake device */ | |
357 | if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) { | |
358 | BT_ERR("cannot wake up device"); | |
359 | break; | |
360 | } | |
361 | ll->hcill_state = HCILL_ASLEEP_TO_AWAKE; | |
362 | break; | |
363 | case HCILL_ASLEEP_TO_AWAKE: | |
364 | BT_DBG("device waking up, queueing packet"); | |
365 | /* transient state; just keep packet for later */ | |
366 | skb_queue_tail(&ll->tx_wait_q, skb); | |
367 | break; | |
368 | default: | |
369 | BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state); | |
370 | kfree_skb(skb); | |
371 | break; | |
372 | } | |
373 | ||
374 | spin_unlock_irqrestore(&ll->hcill_lock, flags); | |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
e1a26170 | 379 | static inline int ll_check_data_len(struct hci_dev *hdev, struct ll_struct *ll, int len) |
166d2f6a | 380 | { |
fc5fef61 | 381 | int room = skb_tailroom(ll->rx_skb); |
166d2f6a OBC |
382 | |
383 | BT_DBG("len %d room %d", len, room); | |
384 | ||
385 | if (!len) { | |
e1a26170 | 386 | hci_recv_frame(hdev, ll->rx_skb); |
166d2f6a OBC |
387 | } else if (len > room) { |
388 | BT_ERR("Data length is too large"); | |
389 | kfree_skb(ll->rx_skb); | |
390 | } else { | |
391 | ll->rx_state = HCILL_W4_DATA; | |
392 | ll->rx_count = len; | |
393 | return len; | |
394 | } | |
395 | ||
396 | ll->rx_state = HCILL_W4_PACKET_TYPE; | |
397 | ll->rx_skb = NULL; | |
398 | ll->rx_count = 0; | |
399 | ||
400 | return 0; | |
401 | } | |
402 | ||
403 | /* Recv data */ | |
9d1c40eb | 404 | static int ll_recv(struct hci_uart *hu, const void *data, int count) |
166d2f6a OBC |
405 | { |
406 | struct ll_struct *ll = hu->priv; | |
9d1c40eb | 407 | const char *ptr; |
166d2f6a OBC |
408 | struct hci_event_hdr *eh; |
409 | struct hci_acl_hdr *ah; | |
410 | struct hci_sco_hdr *sh; | |
fc5fef61 | 411 | int len, type, dlen; |
166d2f6a OBC |
412 | |
413 | BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, ll->rx_state, ll->rx_count); | |
414 | ||
415 | ptr = data; | |
416 | while (count) { | |
417 | if (ll->rx_count) { | |
418 | len = min_t(unsigned int, ll->rx_count, count); | |
59ae1d12 | 419 | skb_put_data(ll->rx_skb, ptr, len); |
166d2f6a OBC |
420 | ll->rx_count -= len; count -= len; ptr += len; |
421 | ||
422 | if (ll->rx_count) | |
423 | continue; | |
424 | ||
425 | switch (ll->rx_state) { | |
426 | case HCILL_W4_DATA: | |
427 | BT_DBG("Complete data"); | |
e1a26170 | 428 | hci_recv_frame(hu->hdev, ll->rx_skb); |
166d2f6a OBC |
429 | |
430 | ll->rx_state = HCILL_W4_PACKET_TYPE; | |
431 | ll->rx_skb = NULL; | |
432 | continue; | |
433 | ||
434 | case HCILL_W4_EVENT_HDR: | |
acce90d6 | 435 | eh = hci_event_hdr(ll->rx_skb); |
166d2f6a OBC |
436 | |
437 | BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen); | |
438 | ||
e1a26170 | 439 | ll_check_data_len(hu->hdev, ll, eh->plen); |
166d2f6a OBC |
440 | continue; |
441 | ||
442 | case HCILL_W4_ACL_HDR: | |
acce90d6 | 443 | ah = hci_acl_hdr(ll->rx_skb); |
166d2f6a OBC |
444 | dlen = __le16_to_cpu(ah->dlen); |
445 | ||
446 | BT_DBG("ACL header: dlen %d", dlen); | |
447 | ||
e1a26170 | 448 | ll_check_data_len(hu->hdev, ll, dlen); |
166d2f6a OBC |
449 | continue; |
450 | ||
451 | case HCILL_W4_SCO_HDR: | |
acce90d6 | 452 | sh = hci_sco_hdr(ll->rx_skb); |
166d2f6a OBC |
453 | |
454 | BT_DBG("SCO header: dlen %d", sh->dlen); | |
455 | ||
e1a26170 | 456 | ll_check_data_len(hu->hdev, ll, sh->dlen); |
166d2f6a OBC |
457 | continue; |
458 | } | |
459 | } | |
460 | ||
461 | /* HCILL_W4_PACKET_TYPE */ | |
462 | switch (*ptr) { | |
463 | case HCI_EVENT_PKT: | |
464 | BT_DBG("Event packet"); | |
465 | ll->rx_state = HCILL_W4_EVENT_HDR; | |
466 | ll->rx_count = HCI_EVENT_HDR_SIZE; | |
467 | type = HCI_EVENT_PKT; | |
468 | break; | |
469 | ||
470 | case HCI_ACLDATA_PKT: | |
471 | BT_DBG("ACL packet"); | |
472 | ll->rx_state = HCILL_W4_ACL_HDR; | |
473 | ll->rx_count = HCI_ACL_HDR_SIZE; | |
474 | type = HCI_ACLDATA_PKT; | |
475 | break; | |
476 | ||
477 | case HCI_SCODATA_PKT: | |
478 | BT_DBG("SCO packet"); | |
479 | ll->rx_state = HCILL_W4_SCO_HDR; | |
480 | ll->rx_count = HCI_SCO_HDR_SIZE; | |
481 | type = HCI_SCODATA_PKT; | |
482 | break; | |
483 | ||
484 | /* HCILL signals */ | |
485 | case HCILL_GO_TO_SLEEP_IND: | |
486 | BT_DBG("HCILL_GO_TO_SLEEP_IND packet"); | |
487 | ll_device_want_to_sleep(hu); | |
488 | ptr++; count--; | |
489 | continue; | |
490 | ||
491 | case HCILL_GO_TO_SLEEP_ACK: | |
492 | /* shouldn't happen */ | |
493 | BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll->hcill_state); | |
494 | ptr++; count--; | |
495 | continue; | |
496 | ||
497 | case HCILL_WAKE_UP_IND: | |
498 | BT_DBG("HCILL_WAKE_UP_IND packet"); | |
499 | ll_device_want_to_wakeup(hu); | |
500 | ptr++; count--; | |
501 | continue; | |
502 | ||
503 | case HCILL_WAKE_UP_ACK: | |
504 | BT_DBG("HCILL_WAKE_UP_ACK packet"); | |
505 | ll_device_woke_up(hu); | |
506 | ptr++; count--; | |
507 | continue; | |
508 | ||
509 | default: | |
510 | BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr); | |
511 | hu->hdev->stat.err_rx++; | |
512 | ptr++; count--; | |
513 | continue; | |
d650ccae | 514 | } |
166d2f6a OBC |
515 | |
516 | ptr++; count--; | |
517 | ||
518 | /* Allocate packet */ | |
519 | ll->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC); | |
520 | if (!ll->rx_skb) { | |
521 | BT_ERR("Can't allocate mem for new packet"); | |
522 | ll->rx_state = HCILL_W4_PACKET_TYPE; | |
523 | ll->rx_count = 0; | |
fe1aff71 | 524 | return -ENOMEM; |
166d2f6a OBC |
525 | } |
526 | ||
618e8bc2 | 527 | hci_skb_pkt_type(ll->rx_skb) = type; |
166d2f6a OBC |
528 | } |
529 | ||
530 | return count; | |
531 | } | |
532 | ||
533 | static struct sk_buff *ll_dequeue(struct hci_uart *hu) | |
534 | { | |
535 | struct ll_struct *ll = hu->priv; | |
536 | return skb_dequeue(&ll->txq); | |
537 | } | |
538 | ||
37180552 RH |
539 | #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS) |
540 | static int read_local_version(struct hci_dev *hdev) | |
541 | { | |
542 | int err = 0; | |
543 | unsigned short version = 0; | |
544 | struct sk_buff *skb; | |
545 | struct hci_rp_read_local_version *ver; | |
546 | ||
547 | skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, HCI_INIT_TIMEOUT); | |
548 | if (IS_ERR(skb)) { | |
549 | bt_dev_err(hdev, "Reading TI version information failed (%ld)", | |
550 | PTR_ERR(skb)); | |
f2edd9f6 | 551 | return PTR_ERR(skb); |
37180552 RH |
552 | } |
553 | if (skb->len != sizeof(*ver)) { | |
554 | err = -EILSEQ; | |
555 | goto out; | |
556 | } | |
557 | ||
558 | ver = (struct hci_rp_read_local_version *)skb->data; | |
559 | if (le16_to_cpu(ver->manufacturer) != 13) { | |
560 | err = -ENODEV; | |
561 | goto out; | |
562 | } | |
563 | ||
564 | version = le16_to_cpu(ver->lmp_subver); | |
565 | ||
566 | out: | |
567 | if (err) bt_dev_err(hdev, "Failed to read TI version info: %d", err); | |
568 | kfree_skb(skb); | |
569 | return err ? err : version; | |
570 | } | |
571 | ||
572 | /** | |
573 | * download_firmware - | |
574 | * internal function which parses through the .bts firmware | |
575 | * script file intreprets SEND, DELAY actions only as of now | |
576 | */ | |
577 | static int download_firmware(struct ll_device *lldev) | |
578 | { | |
579 | unsigned short chip, min_ver, maj_ver; | |
580 | int version, err, len; | |
581 | unsigned char *ptr, *action_ptr; | |
582 | unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */ | |
583 | const struct firmware *fw; | |
584 | struct sk_buff *skb; | |
585 | struct hci_command *cmd; | |
586 | ||
587 | version = read_local_version(lldev->hu.hdev); | |
588 | if (version < 0) | |
589 | return version; | |
590 | ||
591 | chip = (version & 0x7C00) >> 10; | |
592 | min_ver = (version & 0x007F); | |
593 | maj_ver = (version & 0x0380) >> 7; | |
594 | if (version & 0x8000) | |
595 | maj_ver |= 0x0008; | |
596 | ||
597 | snprintf(bts_scr_name, sizeof(bts_scr_name), | |
598 | "ti-connectivity/TIInit_%d.%d.%d.bts", | |
599 | chip, maj_ver, min_ver); | |
600 | ||
601 | err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev); | |
602 | if (err || !fw->data || !fw->size) { | |
603 | bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s", | |
604 | err, bts_scr_name); | |
605 | return -EINVAL; | |
606 | } | |
607 | ptr = (void *)fw->data; | |
608 | len = fw->size; | |
609 | /* bts_header to remove out magic number and | |
610 | * version | |
611 | */ | |
612 | ptr += sizeof(struct bts_header); | |
613 | len -= sizeof(struct bts_header); | |
614 | ||
615 | while (len > 0 && ptr) { | |
616 | bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ", | |
617 | ((struct bts_action *)ptr)->size, | |
618 | ((struct bts_action *)ptr)->type); | |
619 | ||
620 | action_ptr = &(((struct bts_action *)ptr)->data[0]); | |
621 | ||
622 | switch (((struct bts_action *)ptr)->type) { | |
623 | case ACTION_SEND_COMMAND: /* action send */ | |
624 | bt_dev_dbg(lldev->hu.hdev, "S"); | |
625 | cmd = (struct hci_command *)action_ptr; | |
7c6ca120 | 626 | if (cmd->opcode == HCI_VS_UPDATE_UART_HCI_BAUDRATE) { |
37180552 | 627 | /* ignore remote change |
d98422cb DR |
628 | * baud rate HCI VS command |
629 | */ | |
37180552 RH |
630 | bt_dev_warn(lldev->hu.hdev, "change remote baud rate command in firmware"); |
631 | break; | |
632 | } | |
633 | if (cmd->prefix != 1) | |
059fb823 | 634 | bt_dev_dbg(lldev->hu.hdev, "command type %d", cmd->prefix); |
37180552 RH |
635 | |
636 | skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen, &cmd->speed, HCI_INIT_TIMEOUT); | |
637 | if (IS_ERR(skb)) { | |
059fb823 | 638 | bt_dev_err(lldev->hu.hdev, "send command failed"); |
823b8420 | 639 | err = PTR_ERR(skb); |
37180552 RH |
640 | goto out_rel_fw; |
641 | } | |
642 | kfree_skb(skb); | |
643 | break; | |
644 | case ACTION_WAIT_EVENT: /* wait */ | |
645 | /* no need to wait as command was synchronous */ | |
646 | bt_dev_dbg(lldev->hu.hdev, "W"); | |
647 | break; | |
648 | case ACTION_DELAY: /* sleep */ | |
649 | bt_dev_info(lldev->hu.hdev, "sleep command in scr"); | |
650 | mdelay(((struct bts_action_delay *)action_ptr)->msec); | |
651 | break; | |
652 | } | |
653 | len -= (sizeof(struct bts_action) + | |
654 | ((struct bts_action *)ptr)->size); | |
655 | ptr += sizeof(struct bts_action) + | |
656 | ((struct bts_action *)ptr)->size; | |
657 | } | |
658 | ||
659 | out_rel_fw: | |
660 | /* fw download complete */ | |
661 | release_firmware(fw); | |
662 | return err; | |
663 | } | |
664 | ||
665 | static int ll_setup(struct hci_uart *hu) | |
666 | { | |
667 | int err, retry = 3; | |
668 | struct ll_device *lldev; | |
669 | struct serdev_device *serdev = hu->serdev; | |
670 | u32 speed; | |
671 | ||
672 | if (!serdev) | |
673 | return 0; | |
674 | ||
675 | lldev = serdev_device_get_drvdata(serdev); | |
676 | ||
677 | serdev_device_set_flow_control(serdev, true); | |
678 | ||
679 | do { | |
d54fdcf9 | 680 | /* Reset the Bluetooth device */ |
37180552 RH |
681 | gpiod_set_value_cansleep(lldev->enable_gpio, 0); |
682 | msleep(5); | |
683 | gpiod_set_value_cansleep(lldev->enable_gpio, 1); | |
d54fdcf9 DL |
684 | err = serdev_device_wait_for_cts(serdev, true, 200); |
685 | if (err) { | |
686 | bt_dev_err(hu->hdev, "Failed to get CTS"); | |
687 | return err; | |
688 | } | |
37180552 RH |
689 | |
690 | err = download_firmware(lldev); | |
691 | if (!err) | |
692 | break; | |
693 | ||
694 | /* Toggle BT_EN and retry */ | |
695 | bt_dev_err(hu->hdev, "download firmware failed, retrying..."); | |
696 | } while (retry--); | |
697 | ||
698 | if (err) | |
699 | return err; | |
700 | ||
701 | /* Operational speed if any */ | |
702 | if (hu->oper_speed) | |
703 | speed = hu->oper_speed; | |
704 | else if (hu->proto->oper_speed) | |
705 | speed = hu->proto->oper_speed; | |
706 | else | |
707 | speed = 0; | |
708 | ||
709 | if (speed) { | |
c30b93ea | 710 | __le32 speed_le = cpu_to_le32(speed); |
7c6ca120 DL |
711 | struct sk_buff *skb; |
712 | ||
713 | skb = __hci_cmd_sync(hu->hdev, HCI_VS_UPDATE_UART_HCI_BAUDRATE, | |
c30b93ea DL |
714 | sizeof(speed_le), &speed_le, |
715 | HCI_INIT_TIMEOUT); | |
37180552 RH |
716 | if (!IS_ERR(skb)) { |
717 | kfree_skb(skb); | |
718 | serdev_device_set_baudrate(serdev, speed); | |
719 | } | |
720 | } | |
721 | ||
722 | return 0; | |
723 | } | |
724 | ||
725 | static const struct hci_uart_proto llp; | |
726 | ||
727 | static int hci_ti_probe(struct serdev_device *serdev) | |
728 | { | |
729 | struct hci_uart *hu; | |
730 | struct ll_device *lldev; | |
731 | u32 max_speed = 3000000; | |
732 | ||
733 | lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL); | |
734 | if (!lldev) | |
735 | return -ENOMEM; | |
736 | hu = &lldev->hu; | |
737 | ||
738 | serdev_device_set_drvdata(serdev, lldev); | |
739 | lldev->serdev = hu->serdev = serdev; | |
740 | ||
741 | lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev, "enable", GPIOD_OUT_LOW); | |
742 | if (IS_ERR(lldev->enable_gpio)) | |
743 | return PTR_ERR(lldev->enable_gpio); | |
744 | ||
43d3d092 UH |
745 | lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock"); |
746 | if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT) | |
747 | return PTR_ERR(lldev->ext_clk); | |
748 | ||
37180552 RH |
749 | of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed); |
750 | hci_uart_set_speeds(hu, 115200, max_speed); | |
751 | ||
752 | return hci_uart_register_device(hu, &llp); | |
753 | } | |
754 | ||
755 | static void hci_ti_remove(struct serdev_device *serdev) | |
756 | { | |
757 | struct ll_device *lldev = serdev_device_get_drvdata(serdev); | |
37180552 | 758 | |
37f5258d | 759 | hci_uart_unregister_device(&lldev->hu); |
37180552 RH |
760 | } |
761 | ||
762 | static const struct of_device_id hci_ti_of_match[] = { | |
c127a871 SR |
763 | { .compatible = "ti,wl1271-st" }, |
764 | { .compatible = "ti,wl1273-st" }, | |
765 | { .compatible = "ti,wl1281-st" }, | |
766 | { .compatible = "ti,wl1283-st" }, | |
767 | { .compatible = "ti,wl1285-st" }, | |
768 | { .compatible = "ti,wl1801-st" }, | |
769 | { .compatible = "ti,wl1805-st" }, | |
770 | { .compatible = "ti,wl1807-st" }, | |
37180552 RH |
771 | { .compatible = "ti,wl1831-st" }, |
772 | { .compatible = "ti,wl1835-st" }, | |
773 | { .compatible = "ti,wl1837-st" }, | |
774 | {}, | |
775 | }; | |
776 | MODULE_DEVICE_TABLE(of, hci_ti_of_match); | |
777 | ||
778 | static struct serdev_device_driver hci_ti_drv = { | |
779 | .driver = { | |
780 | .name = "hci-ti", | |
781 | .of_match_table = of_match_ptr(hci_ti_of_match), | |
782 | }, | |
783 | .probe = hci_ti_probe, | |
784 | .remove = hci_ti_remove, | |
785 | }; | |
786 | #else | |
787 | #define ll_setup NULL | |
788 | #endif | |
789 | ||
4ee7ef19 | 790 | static const struct hci_uart_proto llp = { |
166d2f6a | 791 | .id = HCI_UART_LL, |
7c40fb8d | 792 | .name = "LL", |
37180552 | 793 | .setup = ll_setup, |
166d2f6a OBC |
794 | .open = ll_open, |
795 | .close = ll_close, | |
796 | .recv = ll_recv, | |
797 | .enqueue = ll_enqueue, | |
798 | .dequeue = ll_dequeue, | |
799 | .flush = ll_flush, | |
800 | }; | |
801 | ||
f2b94bb9 | 802 | int __init ll_init(void) |
166d2f6a | 803 | { |
37180552 RH |
804 | serdev_device_driver_register(&hci_ti_drv); |
805 | ||
01009eec | 806 | return hci_uart_register_proto(&llp); |
166d2f6a OBC |
807 | } |
808 | ||
f2b94bb9 | 809 | int __exit ll_deinit(void) |
166d2f6a | 810 | { |
37180552 RH |
811 | serdev_device_driver_unregister(&hci_ti_drv); |
812 | ||
166d2f6a OBC |
813 | return hci_uart_unregister_proto(&llp); |
814 | } |