Skip to content

Commit

Permalink
dm: Support guest vm modification of baud rate for virtual serial port
Browse files Browse the repository at this point in the history
The baud rate of the virtual serial port added by acrn-dm -l can only be 9600,
The guest VM needs to be able to modify the baud rate of the virtual serial port.

Tracked-On: projectacrn#8616
Signed-off-by: sungao2x <[email protected]>
  • Loading branch information
gaofei2x committed Jun 25, 2024
1 parent 29137b9 commit eec4041
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions devicemodel/hw/uart_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,57 @@ uart_drain(int fd, enum ev_type ev, void *arg)
pthread_mutex_unlock(&uart->mtx);
}

void
uart_update_speed(struct uart_vdev *uart)
{
struct termios tio;
uint32_t speed = 0;
int data;
speed_t baud_rate = 0;


data = (uart->dlh << 8) | uart->dll;

tcgetattr(uart->be.fd, &tio);

#define check_speed(val) \
if (speed <= val) { \
baud_rate = B##val; \
goto done; \
}
speed = DEFAULT_RCLK / 16 / data;
check_speed(50);
check_speed(75);
check_speed(110);
check_speed(134);
check_speed(150);
check_speed(200);
check_speed(300);
check_speed(600);
check_speed(1200);
check_speed(1800);
check_speed(2400);
check_speed(4800);
check_speed(9600);
check_speed(19200);
check_speed(38400);
check_speed(57600);
check_speed(115200);
baud_rate = B115200;

#undef check_speed
done:
cfsetispeed(&tio, baud_rate);
cfsetospeed(&tio, baud_rate);
cfmakeraw(&tio);
tio.c_cflag |= CLOCAL;
tcflush(uart->be.fd, TCIOFLUSH);

if (tcsetattr(uart->be.fd, TCSANOW, &tio) != 0) {
pr_err("Error setting serial port attributes");
}
}

void
uart_write(struct uart_vdev *uart, int offset, uint8_t value)
{
Expand All @@ -419,6 +470,7 @@ uart_write(struct uart_vdev *uart, int offset, uint8_t value)

if (offset == REG_DLH) {
uart->dlh = value;
uart_update_speed(uart);
goto done;
}
}
Expand Down

0 comments on commit eec4041

Please sign in to comment.