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"
29 #include "qapi/error.h"
41 uint8_t outfifo
[FIFO_LEN
* 2];
42 uint8_t inpkt
[FIFO_LEN
];
58 #define TYPE_CHARDEV_HCI "chardev-hci"
59 #define HCI_CHARDEV(obj) OBJECT_CHECK(struct csrhci_s, (obj), TYPE_CHARDEV_HCI)
61 /* H4+ packet types */
71 /* CSR41814 negotiation start magic packet */
72 static const uint8_t csrhci_neg_packet
[] = {
74 0x00, 0xa0, 0x01, 0x00, 0x00,
75 0x4c, 0x00, 0x96, 0x00, 0x00,
78 /* CSR41814 vendor-specific command OCFs */
80 OCF_CSR_SEND_FIRMWARE
= 0x000,
83 static inline void csrhci_fifo_wake(struct csrhci_s
*s
)
85 Chardev
*chr
= (Chardev
*)s
;
86 CharBackend
*be
= chr
->be
;
88 if (!s
->enable
|| !s
->out_len
)
91 /* XXX: Should wait for s->modem_state & CHR_TIOCM_RTS? */
92 if (be
&& be
->chr_can_read
&& be
->chr_can_read(be
->opaque
) &&
94 be
->chr_read(be
->opaque
,
95 s
->outfifo
+ s
->out_start
++, 1);
97 if (s
->out_start
>= s
->out_size
) {
99 s
->out_size
= FIFO_LEN
;
104 timer_mod(s
->out_tm
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->baud_delay
);
107 #define csrhci_out_packetz(s, len) memset(csrhci_out_packet(s, len), 0, len)
108 static uint8_t *csrhci_out_packet(struct csrhci_s
*s
, int len
)
110 int off
= s
->out_start
+ s
->out_len
;
112 /* TODO: do the padding here, i.e. align len */
115 if (off
< FIFO_LEN
) {
116 if (off
+ len
> FIFO_LEN
&& (s
->out_size
= off
+ len
) > FIFO_LEN
* 2) {
117 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
120 return s
->outfifo
+ off
;
123 if (s
->out_len
> s
->out_size
) {
124 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
128 return s
->outfifo
+ off
- s
->out_size
;
131 static inline uint8_t *csrhci_out_packet_csr(struct csrhci_s
*s
,
134 uint8_t *ret
= csrhci_out_packetz(s
, len
+ 2);
142 static inline uint8_t *csrhci_out_packet_event(struct csrhci_s
*s
,
145 uint8_t *ret
= csrhci_out_packetz(s
,
146 len
+ 1 + sizeof(struct hci_event_hdr
));
148 *ret
++ = H4_EVT_PKT
;
149 ((struct hci_event_hdr
*) ret
)->evt
= evt
;
150 ((struct hci_event_hdr
*) ret
)->plen
= len
;
152 return ret
+ sizeof(struct hci_event_hdr
);
155 static void csrhci_in_packet_vendor(struct csrhci_s
*s
, int ocf
,
156 uint8_t *data
, int len
)
162 case OCF_CSR_SEND_FIRMWARE
:
163 /* Check if this is the bd_address packet */
164 if (len
>= 18 + 8 && data
[12] == 0x01 && data
[13] == 0x00) {
166 s
->bd_addr
.b
[0] = data
[offset
+ 7]; /* Beyond cmd packet end(!?) */
167 s
->bd_addr
.b
[1] = data
[offset
+ 6];
168 s
->bd_addr
.b
[2] = data
[offset
+ 4];
169 s
->bd_addr
.b
[3] = data
[offset
+ 0];
170 s
->bd_addr
.b
[4] = data
[offset
+ 3];
171 s
->bd_addr
.b
[5] = data
[offset
+ 2];
173 s
->hci
->bdaddr_set(s
->hci
, s
->bd_addr
.b
);
174 fprintf(stderr
, "%s: bd_address loaded from firmware: "
175 "%02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__
,
176 s
->bd_addr
.b
[0], s
->bd_addr
.b
[1], s
->bd_addr
.b
[2],
177 s
->bd_addr
.b
[3], s
->bd_addr
.b
[4], s
->bd_addr
.b
[5]);
180 rpkt
= csrhci_out_packet_event(s
, EVT_VENDOR
, 11);
181 /* Status bytes: no error */
187 fprintf(stderr
, "%s: got a bad CMD packet\n", __FUNCTION__
);
194 static void csrhci_in_packet(struct csrhci_s
*s
, uint8_t *pkt
)
201 opc
= le16_to_cpu(((struct hci_command_hdr
*) pkt
)->opcode
);
202 if (cmd_opcode_ogf(opc
) == OGF_VENDOR_CMD
) {
203 csrhci_in_packet_vendor(s
, cmd_opcode_ocf(opc
),
204 pkt
+ sizeof(struct hci_command_hdr
),
205 s
->in_len
- sizeof(struct hci_command_hdr
) - 1);
209 /* TODO: if the command is OCF_READ_LOCAL_COMMANDS or the likes,
210 * we need to send it to the HCI layer and then add our supported
211 * commands to the returned mask (such as OGF_VENDOR_CMD). With
212 * bt-hci.c we could just have hooks for this kind of commands but
213 * we can't with bt-host.c. */
215 s
->hci
->cmd_send(s
->hci
, pkt
, s
->in_len
- 1);
222 s
->hci
->acl_send(s
->hci
, pkt
, s
->in_len
- 1);
226 s
->hci
->sco_send(s
->hci
, pkt
, s
->in_len
- 1);
230 if (s
->in_hdr
!= sizeof(csrhci_neg_packet
) ||
231 memcmp(pkt
- 1, csrhci_neg_packet
, s
->in_hdr
)) {
232 fprintf(stderr
, "%s: got a bad NEG packet\n", __FUNCTION__
);
237 rpkt
= csrhci_out_packet_csr(s
, H4_NEG_PKT
, 10);
239 *rpkt
++ = 0x20; /* Operational settings negotiation Ok */
240 memcpy(rpkt
, pkt
, 7); rpkt
+= 7;
246 if (s
->in_hdr
!= 4 || pkt
[1] != 0x55 || pkt
[2] != 0x00) {
247 fprintf(stderr
, "%s: got a bad ALIVE packet\n", __FUNCTION__
);
251 rpkt
= csrhci_out_packet_csr(s
, H4_ALIVE_PKT
, 2);
259 /* TODO: error out */
260 fprintf(stderr
, "%s: got a bad packet\n", __FUNCTION__
);
267 static int csrhci_header_len(const uint8_t *pkt
)
271 return HCI_COMMAND_HDR_SIZE
;
273 return HCI_EVENT_HDR_SIZE
;
275 return HCI_ACL_HDR_SIZE
;
277 return HCI_SCO_HDR_SIZE
;
287 static int csrhci_data_len(const uint8_t *pkt
)
291 /* It seems that vendor-specific command packets for H4+ are all
292 * one byte longer than indicated in the standard header. */
293 if (le16_to_cpu(((struct hci_command_hdr
*) pkt
)->opcode
) == 0xfc00)
294 return (((struct hci_command_hdr
*) pkt
)->plen
+ 1) & ~1;
296 return ((struct hci_command_hdr
*) pkt
)->plen
;
298 return ((struct hci_event_hdr
*) pkt
)->plen
;
300 return le16_to_cpu(((struct hci_acl_hdr
*) pkt
)->dlen
);
302 return ((struct hci_sco_hdr
*) pkt
)->dlen
;
311 static void csrhci_ready_for_next_inpkt(struct csrhci_s
*s
)
313 s
->in_state
= CSR_HDR_LEN
;
319 static int csrhci_write(struct Chardev
*chr
,
320 const uint8_t *buf
, int len
)
322 struct csrhci_s
*s
= (struct csrhci_s
*)chr
;
329 int cnt
= MIN(len
, s
->in_needed
- s
->in_len
);
331 memcpy(s
->inpkt
+ s
->in_len
, buf
, cnt
);
338 if (s
->in_len
< s
->in_needed
) {
342 if (s
->in_state
== CSR_HDR_LEN
) {
343 s
->in_hdr
= csrhci_header_len(s
->inpkt
) + 1;
344 assert(s
->in_hdr
>= s
->in_needed
);
345 s
->in_needed
= s
->in_hdr
;
346 s
->in_state
= CSR_DATA_LEN
;
350 if (s
->in_state
== CSR_DATA_LEN
) {
351 s
->in_needed
+= csrhci_data_len(s
->inpkt
);
352 /* hci_acl_hdr could specify more than 4096 bytes, so assert. */
353 assert(s
->in_needed
<= sizeof(s
->inpkt
));
354 s
->in_state
= CSR_DATA
;
358 if (s
->in_state
== CSR_DATA
) {
359 csrhci_in_packet(s
, s
->inpkt
);
360 csrhci_ready_for_next_inpkt(s
);
367 static void csrhci_out_hci_packet_event(void *opaque
,
368 const uint8_t *data
, int len
)
370 struct csrhci_s
*s
= (struct csrhci_s
*) opaque
;
371 uint8_t *pkt
= csrhci_out_packet(s
, (len
+ 2) & ~1); /* Align */
373 *pkt
++ = H4_EVT_PKT
;
374 memcpy(pkt
, data
, len
);
379 static void csrhci_out_hci_packet_acl(void *opaque
,
380 const uint8_t *data
, int len
)
382 struct csrhci_s
*s
= (struct csrhci_s
*) opaque
;
383 uint8_t *pkt
= csrhci_out_packet(s
, (len
+ 2) & ~1); /* Align */
385 *pkt
++ = H4_ACL_PKT
;
387 memcpy(pkt
, data
, len
);
392 static int csrhci_ioctl(struct Chardev
*chr
, int cmd
, void *arg
)
394 QEMUSerialSetParams
*ssp
;
395 struct csrhci_s
*s
= (struct csrhci_s
*) chr
;
396 int prev_state
= s
->modem_state
;
399 case CHR_IOCTL_SERIAL_SET_PARAMS
:
400 ssp
= (QEMUSerialSetParams
*) arg
;
401 s
->baud_delay
= NANOSECONDS_PER_SECOND
/ ssp
->speed
;
402 /* Moments later... (but shorter than 100ms) */
403 s
->modem_state
|= CHR_TIOCM_CTS
;
406 case CHR_IOCTL_SERIAL_GET_TIOCM
:
407 *(int *) arg
= s
->modem_state
;
410 case CHR_IOCTL_SERIAL_SET_TIOCM
:
411 s
->modem_state
= *(int *) arg
;
412 if (~s
->modem_state
& prev_state
& CHR_TIOCM_RTS
)
413 s
->modem_state
&= ~CHR_TIOCM_CTS
;
422 static void csrhci_reset(struct csrhci_s
*s
)
425 s
->out_size
= FIFO_LEN
;
426 csrhci_ready_for_next_inpkt(s
);
427 s
->baud_delay
= NANOSECONDS_PER_SECOND
;
431 /* After a while... (but sooner than 10ms) */
432 s
->modem_state
|= CHR_TIOCM_CTS
;
434 memset(&s
->bd_addr
, 0, sizeof(bdaddr_t
));
437 static void csrhci_out_tick(void *opaque
)
439 csrhci_fifo_wake((struct csrhci_s
*) opaque
);
442 static void csrhci_pins(void *opaque
, int line
, int level
)
444 struct csrhci_s
*s
= (struct csrhci_s
*) opaque
;
445 int state
= s
->pin_state
;
447 s
->pin_state
&= ~(1 << line
);
448 s
->pin_state
|= (!!level
) << line
;
450 if ((state
& ~s
->pin_state
) & (1 << csrhci_pin_reset
)) {
451 /* TODO: Disappear from lower layers */
455 if (s
->pin_state
== 3 && state
!= 3) {
457 /* TODO: Wake lower layers up */
461 qemu_irq
*csrhci_pins_get(Chardev
*chr
)
463 struct csrhci_s
*s
= (struct csrhci_s
*) chr
;
468 static void csrhci_open(Chardev
*chr
,
469 ChardevBackend
*backend
,
473 struct csrhci_s
*s
= HCI_CHARDEV(chr
);
475 s
->hci
= qemu_next_hci();
477 s
->hci
->evt_recv
= csrhci_out_hci_packet_event
;
478 s
->hci
->acl_recv
= csrhci_out_hci_packet_acl
;
480 s
->out_tm
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, csrhci_out_tick
, s
);
481 s
->pins
= qemu_allocate_irqs(csrhci_pins
, s
, __csrhci_pins
);
486 static void char_hci_class_init(ObjectClass
*oc
, void *data
)
488 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
491 cc
->open
= csrhci_open
;
492 cc
->chr_write
= csrhci_write
;
493 cc
->chr_ioctl
= csrhci_ioctl
;
496 static const TypeInfo char_hci_type_info
= {
497 .name
= TYPE_CHARDEV_HCI
,
498 .parent
= TYPE_CHARDEV
,
499 .instance_size
= sizeof(struct csrhci_s
),
500 .class_init
= char_hci_class_init
,
503 Chardev
*uart_hci_init(void)
505 return qemu_chardev_new(NULL
, TYPE_CHARDEV_HCI
,
509 static void register_types(void)
511 type_register_static(&char_hci_type_info
);
514 type_init(register_types
);