Skip to content

Commit 00401a1

Browse files
mgcaoacrnsi
authored andcommitted
DM: separate pty vuart operation from IOC
for pty vuart operation will be commonly used by other module, like pm-vuart: control UOS power off through vuart. Tracked-On: #3564 Signed-off-by: Minggui Cao <minggui.cao@intel.com> Acked-by: Yin Fengwei <fengwei.yin@intel.com>
1 parent d188afb commit 00401a1

File tree

4 files changed

+119
-82
lines changed

4 files changed

+119
-82
lines changed

devicemodel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ SRCS += hw/platform/ioapic.c
9292
SRCS += hw/platform/cmos_io.c
9393
SRCS += hw/platform/ioc.c
9494
SRCS += hw/platform/ioc_cbc.c
95+
SRCS += hw/platform/pty_vuart.c
9596
SRCS += hw/platform/acpi/acpi.c
9697
SRCS += hw/platform/acpi/acpi_pm.c
9798
SRCS += hw/platform/rpmb/rpmb_sim.c

devicemodel/hw/platform/ioc.c

Lines changed: 6 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
#include <unistd.h>
5656
#include <errno.h>
5757
#include <fcntl.h>
58-
#include <pty.h>
5958
#include <string.h>
6059
#include <stdbool.h>
6160
#include <types.h>
@@ -64,6 +63,8 @@
6463
#include <sys/stat.h>
6564
#include <sys/types.h>
6665

66+
#include "pty_vuart.h"
67+
6768
#include "dm.h"
6869
#include "ioc.h"
6970
#include "vmmapi.h"
@@ -709,83 +710,6 @@ ioc_open_native_ch(const char *dev_name)
709710
return fd;
710711
}
711712

712-
/*
713-
* Check and create the directory.
714-
* To avoid symlink failure if the directory does not exist.
715-
*/
716-
static int
717-
check_dir(const char *file)
718-
{
719-
char *tmp, *dir;
720-
721-
tmp = strdup(file);
722-
if (!tmp) {
723-
DPRINTF("ioc falied to dup file, error:%s\r\n",
724-
strerror(errno));
725-
return -1;
726-
}
727-
728-
dir = dirname(tmp);
729-
if (access(dir, F_OK) && mkdir(dir, 0666)) {
730-
DPRINTF("ioc falied to create dir:%s, erorr:%s\r\n", dir,
731-
strerror(errno));
732-
free(tmp);
733-
return -1;
734-
}
735-
free(tmp);
736-
return 0;
737-
}
738-
739-
/*
740-
* Open PTY master device for IOC mediator and the PTY slave device for virtual
741-
* UART. The pair(master/slave) can work as a communication channel between
742-
* IOC mediator and virtual UART.
743-
*/
744-
static int
745-
ioc_open_virtual_uart(const char *dev_name)
746-
{
747-
int fd;
748-
char *slave_name;
749-
struct termios attr;
750-
751-
fd = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_NONBLOCK);
752-
if (fd < 0)
753-
goto open_err;
754-
if (grantpt(fd) < 0)
755-
goto pty_err;
756-
if (unlockpt(fd) < 0)
757-
goto pty_err;
758-
slave_name = ptsname(fd);
759-
if (!slave_name)
760-
goto pty_err;
761-
if ((unlink(dev_name) < 0) && errno != ENOENT)
762-
goto pty_err;
763-
/*
764-
* The check_dir restriction is that only create one directory
765-
* not support multi-level directroy.
766-
*/
767-
if (check_dir(dev_name) < 0)
768-
goto pty_err;
769-
if (symlink(slave_name, dev_name) < 0)
770-
goto pty_err;
771-
if (chmod(dev_name, 0660) < 0)
772-
goto attr_err;
773-
if (tcgetattr(fd, &attr) < 0)
774-
goto attr_err;
775-
cfmakeraw(&attr);
776-
attr.c_cflag |= CLOCAL;
777-
if (tcsetattr(fd, TCSANOW, &attr) < 0)
778-
goto attr_err;
779-
return fd;
780-
781-
attr_err:
782-
unlink(dev_name);
783-
pty_err:
784-
close(fd);
785-
open_err:
786-
return -1;
787-
}
788-
789713
/*
790714
* Open native CBC cdevs and virtual UART.
791715
*/
@@ -807,7 +731,7 @@ ioc_ch_init(struct ioc_dev *ioc)
807731
fd = ioc_open_native_ch(chl->name);
808732
break;
809733
case IOC_VIRTUAL_UART:
810-
fd = ioc_open_virtual_uart(virtual_uart_path);
734+
fd = pty_open_virtual_uart(virtual_uart_path);
811735
break;
812736
case IOC_LOCAL_EVENT:
813737
if (!pipe(pipe_fds)) {
@@ -823,23 +747,23 @@ ioc_ch_init(struct ioc_dev *ioc)
823747
*/
824748
case IOC_NATIVE_DUMMY0:
825749
if (ioc_debug_enable) {
826-
fd = ioc_open_virtual_uart(chl->name);
750+
fd = pty_open_virtual_uart(chl->name);
827751
dummy0_sfd = open(chl->name, O_RDWR | O_NOCTTY |
828752
O_NONBLOCK);
829753
} else
830754
fd = -1;
831755
break;
832756
case IOC_NATIVE_DUMMY1:
833757
if (ioc_debug_enable) {
834-
fd = ioc_open_virtual_uart(chl->name);
758+
fd = pty_open_virtual_uart(chl->name);
835759
dummy1_sfd = open(chl->name, O_RDWR | O_NOCTTY |
836760
O_NONBLOCK);
837761
} else
838762
fd = -1;
839763
break;
840764
case IOC_NATIVE_DUMMY2:
841765
if (ioc_debug_enable) {
842-
fd = ioc_open_virtual_uart(chl->name);
766+
fd = pty_open_virtual_uart(chl->name);
843767
dummy2_sfd = open(chl->name, O_RDWR | O_NOCTTY |
844768
O_NONBLOCK);
845769
} else

devicemodel/hw/platform/pty_vuart.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Project Acrn
3+
* Acrn-dm-pty
4+
*
5+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
6+
*
7+
* SPDX-License-Identifier: BSD-3-Clause
8+
*
9+
*/
10+
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <string.h>
14+
#include <unistd.h>
15+
#include <errno.h>
16+
#include <fcntl.h>
17+
#include <sys/stat.h>
18+
#include <dirent.h>
19+
#include <termios.h>
20+
#include <stdbool.h>
21+
#include <libgen.h>
22+
23+
#include "log.h"
24+
25+
/*
26+
* Check and create the directory.
27+
* To avoid symlink failure if the directory does not exist.
28+
*/
29+
static int check_dir(const char *file)
30+
{
31+
char *tmp, *dir;
32+
33+
tmp = strdup(file);
34+
if (!tmp) {
35+
pr_err("failed to dup file, error:%s\n", strerror(errno));
36+
return -1;
37+
}
38+
39+
dir = dirname(tmp);
40+
if (access(dir, F_OK) && mkdir(dir, 0666)) {
41+
pr_err("failed to create dir:%s, erorr:%s\n", dir, strerror(errno));
42+
free(tmp);
43+
return -1;
44+
}
45+
free(tmp);
46+
return 0;
47+
}
48+
49+
/*
50+
* Open PTY master device to used by caller and the PTY slave device for virtual
51+
* UART. The pair(master/slave) can work as a communication channel between
52+
* the caller and virtual UART.
53+
*/
54+
int pty_open_virtual_uart(const char *dev_name)
55+
{
56+
int fd;
57+
char *slave_name;
58+
struct termios attr;
59+
60+
fd = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_NONBLOCK);
61+
if (fd < 0)
62+
goto open_err;
63+
if (grantpt(fd) < 0)
64+
goto pty_err;
65+
if (unlockpt(fd) < 0)
66+
goto pty_err;
67+
slave_name = ptsname(fd);
68+
if (!slave_name)
69+
goto pty_err;
70+
if ((unlink(dev_name) < 0) && errno != ENOENT)
71+
goto pty_err;
72+
/*
73+
* The check_dir restriction is that only create one directory
74+
* not support multi-level directroy.
75+
*/
76+
if (check_dir(dev_name) < 0)
77+
goto pty_err;
78+
if (symlink(slave_name, dev_name) < 0)
79+
goto pty_err;
80+
if (chmod(dev_name, 0660) < 0)
81+
goto attr_err;
82+
if (tcgetattr(fd, &attr) < 0)
83+
goto attr_err;
84+
cfmakeraw(&attr);
85+
attr.c_cflag |= CLOCAL;
86+
if (tcsetattr(fd, TCSANOW, &attr) < 0)
87+
goto attr_err;
88+
return fd;
89+
90+
attr_err:
91+
unlink(dev_name);
92+
pty_err:
93+
close(fd);
94+
open_err:
95+
return -1;
96+
}

devicemodel/include/pty_vuart.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Project Acrn
3+
* Acrn-dm-pty
4+
*
5+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
6+
*
7+
* SPDX-License-Identifier: BSD-3-Clause
8+
*
9+
*/
10+
11+
#ifndef __PTY_H__
12+
#define __PTY_H__
13+
14+
int pty_open_virtual_uart(const char *dev_name);
15+
16+
#endif

0 commit comments

Comments
 (0)