- Reconfigure port speed only when initial speed was different.

- Time out HCI commands instead of hanging forever.
- When bcm43xx reset fails, assume that firmware is already
  running and start line discipline.

This allows to re-attach bcm43xx without reboot.
This commit is contained in:
mlelstv 2023-02-07 20:45:44 +00:00
parent 2b851f64b8
commit e2144167ec
3 changed files with 34 additions and 12 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: btattach.c,v 1.15 2017/08/11 11:54:08 jmcneill Exp $ */
/* $NetBSD: btattach.c,v 1.16 2023/02/07 20:45:44 mlelstv Exp $ */
/*-
* Copyright (c) 2008 Iain Hibbert
@ -27,7 +27,7 @@
#include <sys/cdefs.h>
__COPYRIGHT("@(#) Copyright (c) 2008 Iain Hibbert. All rights reserved.");
__RCSID("$NetBSD: btattach.c,v 1.15 2017/08/11 11:54:08 jmcneill Exp $");
__RCSID("$NetBSD: btattach.c,v 1.16 2023/02/07 20:45:44 mlelstv Exp $");
#include <sys/ioctl.h>
#include <sys/param.h>
@ -40,6 +40,7 @@ __RCSID("$NetBSD: btattach.c,v 1.15 2017/08/11 11:54:08 jmcneill Exp $");
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include <util.h>
@ -275,9 +276,11 @@ main(int argc, char *argv[])
if (type->init != NULL)
(*type->init)(fd, speed);
if (cfsetspeed(&tio, speed) < 0
|| tcsetattr(fd, TCSADRAIN, &tio) < 0)
err(EXIT_FAILURE, "tty setup failed");
if (speed != init_speed) {
if (cfsetspeed(&tio, speed) < 0
|| tcsetattr(fd, TCSANOW, &tio) < 0)
err(EXIT_FAILURE, "tty setup failed");
}
/* start line discipline */
if (ioctl(fd, TIOCSLINED, type->line) < 0)
@ -342,6 +345,12 @@ sighandler(int s)
sigcount++;
}
static void
timeout(int s)
{
}
static void
hexdump(uint8_t *ptr, size_t len)
{
@ -353,11 +362,13 @@ hexdump(uint8_t *ptr, size_t len)
/*
* send HCI comamnd
*/
void
int
uart_send_cmd(int fd, uint16_t opcode, void *buf, size_t len)
{
struct iovec iov[2];
hci_cmd_hdr_t hdr;
int r;
struct sigaction oaction, taction;
hdr.type = HCI_CMD_PKT;
hdr.opcode = htole16(opcode);
@ -379,7 +390,17 @@ uart_send_cmd(int fd, uint16_t opcode, void *buf, size_t len)
if (writev(fd, iov, __arraycount(iov)) < 0)
err(EXIT_FAILURE, "writev");
tcdrain(fd);
taction.sa_handler = timeout,
sigemptyset(&taction.sa_mask);
taction.sa_flags = 0,
sigaction(SIGALRM, &taction, &oaction);
alarm(1);
r = tcdrain(fd);
alarm(0);
sigaction(SIGALRM, &oaction, NULL);
return r;
}
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: btattach.h,v 1.4 2017/08/10 13:34:29 nat Exp $ */
/* $NetBSD: btattach.h,v 1.5 2023/02/07 20:45:44 mlelstv Exp $ */
/*-
* Copyright (c) 2008 Iain Hibbert
@ -50,6 +50,6 @@ devinit_t init_stlc2500;
devinit_t init_swave;
devinit_t init_unistone;
void uart_send_cmd(int, uint16_t, void *, size_t);
int uart_send_cmd(int, uint16_t, void *, size_t);
size_t uart_recv_ev(int, uint8_t, void *, size_t);
size_t uart_recv_cc(int, uint16_t, void *, size_t);

View File

@ -1,4 +1,4 @@
/* $NetBSD: init_bcm43xx.c,v 1.5 2017/09/03 22:54:12 nat Exp $ */
/* $NetBSD: init_bcm43xx.c,v 1.6 2023/02/07 20:45:44 mlelstv Exp $ */
/*-
* Copyright (c) 2017 Nathanial Sloss <nathanialsloss@yahoo.com.au>
@ -34,7 +34,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: init_bcm43xx.c,v 1.5 2017/09/03 22:54:12 nat Exp $");
__RCSID("$NetBSD: init_bcm43xx.c,v 1.6 2023/02/07 20:45:44 mlelstv Exp $");
#include <sys/param.h>
@ -102,7 +102,8 @@ init_bcm43xx(int fd, unsigned int speed)
memset(rate, 0, sizeof(rate));
memset(local_name, 0, sizeof(local_name));
uart_send_cmd(fd, HCI_CMD_RESET, NULL, 0);
if (uart_send_cmd(fd, HCI_CMD_RESET, NULL, 0))
return;
uart_recv_cc(fd, HCI_CMD_RESET, &resp, sizeof(resp));
/* assume it succeeded? */