2 * Bluetooth serial HCI transport.
3 * CSR41814 HCI with H4p vendor extensions.
5 * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 or
10 * (at your option) version 3 of the License.
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.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu-common.h"
23 #include "sysemu/char.h"
24 #include "qemu/timer.h"
25 #include "qemu/bswap.h"
27 #include "sysemu/bt.h"
40 uint8_t outfifo
[FIFO_LEN
* 2];
41 uint8_t inpkt
[FIFO_LEN
];
57 /* H4+ packet types */
67 /* CSR41814 negotiation start magic packet */
68 static const uint8_t csrhci_neg_packet
[] = {
70 0x00, 0xa0, 0x01, 0x00, 0x00,
71 0x4c, 0x00, 0x96, 0x00, 0x00,
74 /* CSR41814 vendor-specific command OCFs */
76 OCF_CSR_SEND_FIRMWARE
= 0x000,
79 static inline void csrhci_fifo_wake(struct csrhci_s
*s
)
81 if (!s
->enable
|| !s
->out_len
)
84 /* XXX: Should wait for s->modem_state & CHR_TIOCM_RTS? */
85 if (s
->chr
.chr_can_read
&& s
->chr
.chr_can_read(s
->chr
.handler_opaque
) &&
87 s
->chr
.chr_read(s
->chr
.handler_opaque
,
88 s
->outfifo
+ s
->out_start
++, 1);
90 if (s
->out_start
>= s
->out_size
) {
92 s
->out_size
= FIFO_LEN
;
97 timer_mod(s
->out_tm
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->baud_delay
);
100 #define csrhci_out_packetz(s, len) memset(csrhci_out_packet(s, len), 0, len)
101 static uint8_t *csrhci_out_packet(struct csrhci_s
*s
, int len
)
103 int off
= s
->out_start
+ s
->out_len
;
105 /* TODO: do the padding here, i.e. align len */
108 if (off
< FIFO_LEN
) {
109 if (off
+ len
> FIFO_LEN
&& (s
->out_size
= off
+ len
) > FIFO_LEN
* 2) {
110 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
113 return s
->outfifo
+ off
;
116 if (s
->out_len
> s
->out_size
) {
117 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
121 return s
->outfifo
+ off
- s
->out_size
;
124 static inline uint8_t *csrhci_out_packet_csr(struct csrhci_s
*s
,
127 uint8_t *ret
= csrhci_out_packetz(s
, len
+ 2);
135 static inline uint8_t *csrhci_out_packet_event(struct csrhci_s
*s
,
138 uint8_t *ret
= csrhci_out_packetz(s
,
139 len
+ 1 + sizeof(struct hci_event_hdr
));
141 *ret
++ = H4_EVT_PKT
;
142 ((struct hci_event_hdr
*) ret
)->evt
= evt
;
143 ((struct hci_event_hdr
*) ret
)->plen
= len
;
145 return ret
+ sizeof(struct hci_event_hdr
);
148 static void csrhci_in_packet_vendor(struct csrhci_s
*s
, int ocf
,
149 uint8_t *data
, int len
)
155 case OCF_CSR_SEND_FIRMWARE
:
156 /* Check if this is the bd_address packet */
157 if (len
>= 18 + 8 && data
[12] == 0x01 && data
[13] == 0x00) {
159 s
->bd_addr
.b
[0] = data
[offset
+ 7]; /* Beyond cmd packet end(!?) */
160 s
->bd_addr
.b
[1] = data
[offset
+ 6];
161 s
->bd_addr
.b
[2] = data
[offset
+ 4];
162 s
->bd_addr
.b
[3] = data
[offset
+ 0];
163 s
->bd_addr
.b
[4] = data
[offset
+ 3];
164 s
->bd_addr
.b
[5] = data
[offset
+ 2];
166 s
->hci
->bdaddr_set(s
->hci
, s
->bd_addr
.b
);
167 fprintf(stderr
, "%s: bd_address loaded from firmware: "
168 "%02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__
,
169 s
->bd_addr
.b
[0], s
->bd_addr
.b
[1], s
->bd_addr
.b
[2],
170 s
->bd_addr
.b
[3], s
->bd_addr
.b
[4], s
->bd_addr
.b
[5]);
173 rpkt
= csrhci_out_packet_event(s
, EVT_VENDOR
, 11);
174 /* Status bytes: no error */
180 fprintf(stderr
, "%s: got a bad CMD packet\n", __FUNCTION__
);
187 static void csrhci_in_packet(struct csrhci_s
*s
, uint8_t *pkt
)
194 opc
= le16_to_cpu(((struct hci_command_hdr
*) pkt
)->opcode
);
195 if (cmd_opcode_ogf(opc
) == OGF_VENDOR_CMD
) {
196 csrhci_in_packet_vendor(s
, cmd_opcode_ocf(opc
),
197 pkt
+ sizeof(struct hci_command_hdr
),
198 s
->in_len
- sizeof(struct hci_command_hdr
) - 1);
202 /* TODO: if the command is OCF_READ_LOCAL_COMMANDS or the likes,
203 * we need to send it to the HCI layer and then add our supported
204 * commands to the returned mask (such as OGF_VENDOR_CMD). With
205 * bt-hci.c we could just have hooks for this kind of commands but
206 * we can't with bt-host.c. */
208 s
->hci
->cmd_send(s
->hci
, pkt
, s
->in_len
- 1);
215 s
->hci
->acl_send(s
->hci
, pkt
, s
->in_len
- 1);
219 s
->hci
->sco_send(s
->hci
, pkt
, s
->in_len
- 1);
223 if (s
->in_hdr
!= sizeof(csrhci_neg_packet
) ||
224 memcmp(pkt
- 1, csrhci_neg_packet
, s
->in_hdr
)) {
225 fprintf(stderr
, "%s: got a bad NEG packet\n", __FUNCTION__
);
230 rpkt
= csrhci_out_packet_csr(s
, H4_NEG_PKT
, 10);
232 *rpkt
++ = 0x20; /* Operational settings negotiation Ok */
233 memcpy(rpkt
, pkt
, 7); rpkt
+= 7;
239 if (s
->in_hdr
!= 4 || pkt
[1] != 0x55 || pkt
[2] != 0x00) {
240 fprintf(stderr
, "%s: got a bad ALIVE packet\n", __FUNCTION__
);
244 rpkt
= csrhci_out_packet_csr(s
, H4_ALIVE_PKT
, 2);
252 /* TODO: error out */
253 fprintf(stderr
, "%s: got a bad packet\n", __FUNCTION__
);
260 static int csrhci_header_len(const uint8_t *pkt
)
264 return HCI_COMMAND_HDR_SIZE
;
266 return HCI_EVENT_HDR_SIZE
;
268 return HCI_ACL_HDR_SIZE
;
270 return HCI_SCO_HDR_SIZE
;
280 static int csrhci_data_len(const uint8_t *pkt
)
284 /* It seems that vendor-specific command packets for H4+ are all
285 * one byte longer than indicated in the standard header. */
286 if (le16_to_cpu(((struct hci_command_hdr
*) pkt
)->opcode
) == 0xfc00)
287 return (((struct hci_command_hdr
*) pkt
)->plen
+ 1) & ~1;
289 return ((struct hci_command_hdr
*) pkt
)->plen
;
291 return ((struct hci_event_hdr
*) pkt
)->plen
;
293 return le16_to_cpu(((struct hci_acl_hdr
*) pkt
)->dlen
);
295 return ((struct hci_sco_hdr
*) pkt
)->dlen
;
304 static void csrhci_ready_for_next_inpkt(struct csrhci_s
*s
)
306 s
->in_state
= CSR_HDR_LEN
;
312 static int csrhci_write(struct CharDriverState
*chr
,
313 const uint8_t *buf
, int len
)
315 struct csrhci_s
*s
= (struct csrhci_s
*) chr
->opaque
;
322 int cnt
= MIN(len
, s
->in_needed
- s
->in_len
);
324 memcpy(s
->inpkt
+ s
->in_len
, buf
, cnt
);
331 if (s
->in_len
< s
->in_needed
) {
335 if (s
->in_state
== CSR_HDR_LEN
) {
336 s
->in_hdr
= csrhci_header_len(s
->inpkt
) + 1;
337 assert(s
->in_hdr
>= s
->in_needed
);
338 s
->in_needed
= s
->in_hdr
;
339 s
->in_state
= CSR_DATA_LEN
;
343 if (s
->in_state
== CSR_DATA_LEN
) {
344 s
->in_needed
+= csrhci_data_len(s
->inpkt
);
345 /* hci_acl_hdr could specify more than 4096 bytes, so assert. */
346 assert(s
->in_needed
<= sizeof(s
->inpkt
));
347 s
->in_state
= CSR_DATA
;
351 if (s
->in_state
== CSR_DATA
) {
352 csrhci_in_packet(s
, s
->inpkt
);
353 csrhci_ready_for_next_inpkt(s
);
360 static void csrhci_out_hci_packet_event(void *opaque
,
361 const uint8_t *data
, int len
)
363 struct csrhci_s
*s
= (struct csrhci_s
*) opaque
;
364 uint8_t *pkt
= csrhci_out_packet(s
, (len
+ 2) & ~1); /* Align */
366 *pkt
++ = H4_EVT_PKT
;
367 memcpy(pkt
, data
, len
);
372 static void csrhci_out_hci_packet_acl(void *opaque
,
373 const uint8_t *data
, int len
)
375 struct csrhci_s
*s
= (struct csrhci_s
*) opaque
;
376 uint8_t *pkt
= csrhci_out_packet(s
, (len
+ 2) & ~1); /* Align */
378 *pkt
++ = H4_ACL_PKT
;
380 memcpy(pkt
, data
, len
);
385 static int csrhci_ioctl(struct CharDriverState
*chr
, int cmd
, void *arg
)
387 QEMUSerialSetParams
*ssp
;
388 struct csrhci_s
*s
= (struct csrhci_s
*) chr
->opaque
;
389 int prev_state
= s
->modem_state
;
392 case CHR_IOCTL_SERIAL_SET_PARAMS
:
393 ssp
= (QEMUSerialSetParams
*) arg
;
394 s
->baud_delay
= NANOSECONDS_PER_SECOND
/ ssp
->speed
;
395 /* Moments later... (but shorter than 100ms) */
396 s
->modem_state
|= CHR_TIOCM_CTS
;
399 case CHR_IOCTL_SERIAL_GET_TIOCM
:
400 *(int *) arg
= s
->modem_state
;
403 case CHR_IOCTL_SERIAL_SET_TIOCM
:
404 s
->modem_state
= *(int *) arg
;
405 if (~s
->modem_state
& prev_state
& CHR_TIOCM_RTS
)
406 s
->modem_state
&= ~CHR_TIOCM_CTS
;
415 static void csrhci_reset(struct csrhci_s
*s
)
418 s
->out_size
= FIFO_LEN
;
419 csrhci_ready_for_next_inpkt(s
);
420 s
->baud_delay
= NANOSECONDS_PER_SECOND
;
424 /* After a while... (but sooner than 10ms) */
425 s
->modem_state
|= CHR_TIOCM_CTS
;
427 memset(&s
->bd_addr
, 0, sizeof(bdaddr_t
));
430 static void csrhci_out_tick(void *opaque
)
432 csrhci_fifo_wake((struct csrhci_s
*) opaque
);
435 static void csrhci_pins(void *opaque
, int line
, int level
)
437 struct csrhci_s
*s
= (struct csrhci_s
*) opaque
;
438 int state
= s
->pin_state
;
440 s
->pin_state
&= ~(1 << line
);
441 s
->pin_state
|= (!!level
) << line
;
443 if ((state
& ~s
->pin_state
) & (1 << csrhci_pin_reset
)) {
444 /* TODO: Disappear from lower layers */
448 if (s
->pin_state
== 3 && state
!= 3) {
450 /* TODO: Wake lower layers up */
454 qemu_irq
*csrhci_pins_get(CharDriverState
*chr
)
456 struct csrhci_s
*s
= (struct csrhci_s
*) chr
->opaque
;
461 CharDriverState
*uart_hci_init(qemu_irq wakeup
)
463 struct csrhci_s
*s
= (struct csrhci_s
*)
464 g_malloc0(sizeof(struct csrhci_s
));
467 s
->chr
.chr_write
= csrhci_write
;
468 s
->chr
.chr_ioctl
= csrhci_ioctl
;
469 s
->chr
.avail_connections
= 1;
471 s
->hci
= qemu_next_hci();
473 s
->hci
->evt_recv
= csrhci_out_hci_packet_event
;
474 s
->hci
->acl_recv
= csrhci_out_hci_packet_acl
;
476 s
->out_tm
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, csrhci_out_tick
, s
);
477 s
->pins
= qemu_allocate_irqs(csrhci_pins
, s
, __csrhci_pins
);