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