2 * Bluetooth serial HCI transport.
3 * CSR41814 HCI with H4p vendor extensions.
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 "chardev/char-serial.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);
87 if (!s->enable || !s->out_len)
90 /* XXX: Should wait for s->modem_state & CHR_TIOCM_RTS? */
91 if (qemu_chr_be_can_write(chr)) {
92 qemu_chr_be_write(chr, s->outfifo + s->out_start++, 1);
94 if (s->out_start >= s->out_size) {
96 s->out_size = FIFO_LEN;
101 timer_mod(s->out_tm, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->baud_delay);
104 #define csrhci_out_packetz(s, len) memset(csrhci_out_packet(s, len), 0, len)
105 static uint8_t *csrhci_out_packet(struct csrhci_s *s, int len)
107 int off = s->out_start + s->out_len;
109 /* TODO: do the padding here, i.e. align len */
112 if (off < FIFO_LEN) {
113 if (off + len > FIFO_LEN && (s->out_size = off + len) > FIFO_LEN * 2) {
114 fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
117 return s->outfifo + off;
120 if (s->out_len > s->out_size) {
121 fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
125 return s->outfifo + off - s->out_size;
128 static inline uint8_t *csrhci_out_packet_csr(struct csrhci_s *s,
131 uint8_t *ret = csrhci_out_packetz(s, len + 2);
139 static inline uint8_t *csrhci_out_packet_event(struct csrhci_s *s,
142 uint8_t *ret = csrhci_out_packetz(s,
143 len + 1 + sizeof(struct hci_event_hdr));
145 *ret ++ = H4_EVT_PKT;
146 ((struct hci_event_hdr *) ret)->evt = evt;
147 ((struct hci_event_hdr *) ret)->plen = len;
149 return ret + sizeof(struct hci_event_hdr);
152 static void csrhci_in_packet_vendor(struct csrhci_s *s, int ocf,
153 uint8_t *data, int len)
159 case OCF_CSR_SEND_FIRMWARE:
160 /* Check if this is the bd_address packet */
161 if (len >= 18 + 8 && data[12] == 0x01 && data[13] == 0x00) {
163 s->bd_addr.b[0] = data[offset + 7]; /* Beyond cmd packet end(!?) */
164 s->bd_addr.b[1] = data[offset + 6];
165 s->bd_addr.b[2] = data[offset + 4];
166 s->bd_addr.b[3] = data[offset + 0];
167 s->bd_addr.b[4] = data[offset + 3];
168 s->bd_addr.b[5] = data[offset + 2];
170 s->hci->bdaddr_set(s->hci, s->bd_addr.b);
171 fprintf(stderr, "%s: bd_address loaded from firmware: "
172 "%02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
173 s->bd_addr.b[0], s->bd_addr.b[1], s->bd_addr.b[2],
174 s->bd_addr.b[3], s->bd_addr.b[4], s->bd_addr.b[5]);
177 rpkt = csrhci_out_packet_event(s, EVT_VENDOR, 11);
178 /* Status bytes: no error */
184 fprintf(stderr, "%s: got a bad CMD packet\n", __FUNCTION__);
191 static void csrhci_in_packet(struct csrhci_s *s, uint8_t *pkt)
198 opc = le16_to_cpu(((struct hci_command_hdr *) pkt)->opcode);
199 if (cmd_opcode_ogf(opc) == OGF_VENDOR_CMD) {
200 csrhci_in_packet_vendor(s, cmd_opcode_ocf(opc),
201 pkt + sizeof(struct hci_command_hdr),
202 s->in_len - sizeof(struct hci_command_hdr) - 1);
206 /* TODO: if the command is OCF_READ_LOCAL_COMMANDS or the likes,
207 * we need to send it to the HCI layer and then add our supported
208 * commands to the returned mask (such as OGF_VENDOR_CMD). With
209 * bt-hci.c we could just have hooks for this kind of commands but
210 * we can't with bt-host.c. */
212 s->hci->cmd_send(s->hci, pkt, s->in_len - 1);
219 s->hci->acl_send(s->hci, pkt, s->in_len - 1);
223 s->hci->sco_send(s->hci, pkt, s->in_len - 1);
227 if (s->in_hdr != sizeof(csrhci_neg_packet) ||
228 memcmp(pkt - 1, csrhci_neg_packet, s->in_hdr)) {
229 fprintf(stderr, "%s: got a bad NEG packet\n", __FUNCTION__);
234 rpkt = csrhci_out_packet_csr(s, H4_NEG_PKT, 10);
236 *rpkt ++ = 0x20; /* Operational settings negotiation Ok */
237 memcpy(rpkt, pkt, 7); rpkt += 7;
243 if (s->in_hdr != 4 || pkt[1] != 0x55 || pkt[2] != 0x00) {
244 fprintf(stderr, "%s: got a bad ALIVE packet\n", __FUNCTION__);
248 rpkt = csrhci_out_packet_csr(s, H4_ALIVE_PKT, 2);
256 /* TODO: error out */
257 fprintf(stderr, "%s: got a bad packet\n", __FUNCTION__);
264 static int csrhci_header_len(const uint8_t *pkt)
268 return HCI_COMMAND_HDR_SIZE;
270 return HCI_EVENT_HDR_SIZE;
272 return HCI_ACL_HDR_SIZE;
274 return HCI_SCO_HDR_SIZE;
284 static int csrhci_data_len(const uint8_t *pkt)
288 /* It seems that vendor-specific command packets for H4+ are all
289 * one byte longer than indicated in the standard header. */
290 if (le16_to_cpu(((struct hci_command_hdr *) pkt)->opcode) == 0xfc00)
291 return (((struct hci_command_hdr *) pkt)->plen + 1) & ~1;
293 return ((struct hci_command_hdr *) pkt)->plen;
295 return ((struct hci_event_hdr *) pkt)->plen;
297 return le16_to_cpu(((struct hci_acl_hdr *) pkt)->dlen);
299 return ((struct hci_sco_hdr *) pkt)->dlen;
308 static void csrhci_ready_for_next_inpkt(struct csrhci_s *s)
310 s->in_state = CSR_HDR_LEN;
316 static int csrhci_write(struct Chardev *chr,
317 const uint8_t *buf, int len)
319 struct csrhci_s *s = (struct csrhci_s *)chr;
326 int cnt = MIN(len, s->in_needed - s->in_len);
328 memcpy(s->inpkt + s->in_len, buf, cnt);
335 if (s->in_len < s->in_needed) {
339 if (s->in_state == CSR_HDR_LEN) {
340 s->in_hdr = csrhci_header_len(s->inpkt) + 1;
341 assert(s->in_hdr >= s->in_needed);
342 s->in_needed = s->in_hdr;
343 s->in_state = CSR_DATA_LEN;
347 if (s->in_state == CSR_DATA_LEN) {
348 s->in_needed += csrhci_data_len(s->inpkt);
349 /* hci_acl_hdr could specify more than 4096 bytes, so assert. */
350 assert(s->in_needed <= sizeof(s->inpkt));
351 s->in_state = CSR_DATA;
355 if (s->in_state == CSR_DATA) {
356 csrhci_in_packet(s, s->inpkt);
357 csrhci_ready_for_next_inpkt(s);
364 static void csrhci_out_hci_packet_event(void *opaque,
365 const uint8_t *data, int len)
367 struct csrhci_s *s = (struct csrhci_s *) opaque;
368 uint8_t *pkt = csrhci_out_packet(s, (len + 2) & ~1); /* Align */
370 *pkt ++ = H4_EVT_PKT;
371 memcpy(pkt, data, len);
376 static void csrhci_out_hci_packet_acl(void *opaque,
377 const uint8_t *data, int len)
379 struct csrhci_s *s = (struct csrhci_s *) opaque;
380 uint8_t *pkt = csrhci_out_packet(s, (len + 2) & ~1); /* Align */
382 *pkt ++ = H4_ACL_PKT;
384 memcpy(pkt, data, len);
389 static int csrhci_ioctl(struct Chardev *chr, int cmd, void *arg)
391 QEMUSerialSetParams *ssp;
392 struct csrhci_s *s = (struct csrhci_s *) chr;
393 int prev_state = s->modem_state;
396 case CHR_IOCTL_SERIAL_SET_PARAMS:
397 ssp = (QEMUSerialSetParams *) arg;
398 s->baud_delay = NANOSECONDS_PER_SECOND / ssp->speed;
399 /* Moments later... (but shorter than 100ms) */
400 s->modem_state |= CHR_TIOCM_CTS;
403 case CHR_IOCTL_SERIAL_GET_TIOCM:
404 *(int *) arg = s->modem_state;
407 case CHR_IOCTL_SERIAL_SET_TIOCM:
408 s->modem_state = *(int *) arg;
409 if (~s->modem_state & prev_state & CHR_TIOCM_RTS)
410 s->modem_state &= ~CHR_TIOCM_CTS;
419 static void csrhci_reset(struct csrhci_s *s)
422 s->out_size = FIFO_LEN;
423 csrhci_ready_for_next_inpkt(s);
424 s->baud_delay = NANOSECONDS_PER_SECOND;
428 /* After a while... (but sooner than 10ms) */
429 s->modem_state |= CHR_TIOCM_CTS;
431 memset(&s->bd_addr, 0, sizeof(bdaddr_t));
434 static void csrhci_out_tick(void *opaque)
436 csrhci_fifo_wake((struct csrhci_s *) opaque);
439 static void csrhci_pins(void *opaque, int line, int level)
441 struct csrhci_s *s = (struct csrhci_s *) opaque;
442 int state = s->pin_state;
444 s->pin_state &= ~(1 << line);
445 s->pin_state |= (!!level) << line;
447 if ((state & ~s->pin_state) & (1 << csrhci_pin_reset)) {
448 /* TODO: Disappear from lower layers */
452 if (s->pin_state == 3 && state != 3) {
454 /* TODO: Wake lower layers up */
458 qemu_irq *csrhci_pins_get(Chardev *chr)
460 struct csrhci_s *s = (struct csrhci_s *) chr;
465 static void csrhci_open(Chardev *chr,
466 ChardevBackend *backend,
470 struct csrhci_s *s = HCI_CHARDEV(chr);
472 s->hci = qemu_next_hci();
474 s->hci->evt_recv = csrhci_out_hci_packet_event;
475 s->hci->acl_recv = csrhci_out_hci_packet_acl;
477 s->out_tm = timer_new_ns(QEMU_CLOCK_VIRTUAL, csrhci_out_tick, s);
478 s->pins = qemu_allocate_irqs(csrhci_pins, s, __csrhci_pins);
483 static void char_hci_class_init(ObjectClass *oc, void *data)
485 ChardevClass *cc = CHARDEV_CLASS(oc);
488 cc->open = csrhci_open;
489 cc->chr_write = csrhci_write;
490 cc->chr_ioctl = csrhci_ioctl;
493 static const TypeInfo char_hci_type_info = {
494 .name = TYPE_CHARDEV_HCI,
495 .parent = TYPE_CHARDEV,
496 .instance_size = sizeof(struct csrhci_s),
497 .class_init = char_hci_class_init,
500 Chardev *uart_hci_init(void)
502 return qemu_chardev_new(NULL, TYPE_CHARDEV_HCI,
506 static void register_types(void)
508 type_register_static(&char_hci_type_info);
511 type_init(register_types);