-
Notifications
You must be signed in to change notification settings - Fork 137
/
main_on.c
144 lines (115 loc) · 3.06 KB
/
main_on.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <linux/device.h>
#include <linux/serial.h>
#include <linux/tty.h>
#include <linux/module.h>
#include "main.h"
static struct tty_driver *ldd_tty_driver;
static struct tty_port ldd_tty_port[LDD_TTY_MINOR_NR];
static int ldd_tty_open(struct tty_struct *tty, struct file *filp)
{
int idx;
// tty->driver_data = &tpk_port;
idx = tty->index;
pr_info("open, index= %d\n", idx);
return tty_port_open(ldd_tty_port idx, tty, filp);
}
static void ldd_tty_close(struct tty_struct *tty, struct file *filp)
{
// struct ttyprintk_port *tpkp = tty->driver_data;
// unsigned long flags;
int idx;
idx = tty->index;
pr_info("close, index = %d...\n", idx);
tty_port_close(ldd_tty_port idx, tty, filp);
}
/*
* TTY operations write function.
*/
static int ldd_tty_write(struct tty_struct *tty,
const unsigned char *buf, int count)
{
// struct ttyprintk_port *tpkp = tty->driver_data;
// unsigned long flags;
/* exclusive use of tpk_printk within this tty */
pr_info("write ... \n");
return count;
}
/*
* TTY operations write_room function.
*/
static int ldd_tty_write_room(struct tty_struct *tty)
{
return LDD_TTY_MAX_ROOM;
}
static int ldd_tty_ioctl(struct tty_struct *tty,
unsigned int cmd, unsigned long arg)
{
// struct ttyprintk_port *tpkp = tty->driver_data;
// if (!tpkp)
// return -EINVAL;
// switch (cmd) {
// [> Stop TIOCCONS <]
// case TIOCCONS:
// return -EOPNOTSUPP;
// default:
// return -ENOIOCTLCMD;
// }
return 0;
}
static const struct tty_operations ttyprintk_ops = {
.open = ldd_tty_open,
.close = ldd_tty_close,
.write = ldd_tty_write,
.write_room = ldd_tty_write_room,
.ioctl = ldd_tty_ioctl,
};
static const struct tty_port_operations null_ops = { };
static
int __init m_init(void)
{
int i, ret = -ENOMEM;
ldd_tty_driver = tty_alloc_driver(LDD_TTY_MINOR_NR,
TTY_DRIVER_RESET_TERMIOS |
TTY_DRIVER_REAL_RAW);
if (IS_ERR(ldd_tty_driver)) {
pr_info("-----------x\n");
return PTR_ERR(ldd_tty_driver);
}
for (i = 0; i < LDD_TTY_MINOR_NR; i) {
tty_port_init(ldd_tty_port i);
ldd_tty_port[i].ops = &null_ops;
tty_port_link_device(ldd_tty_port i, ldd_tty_driver, 0);
}
ldd_tty_driver->driver_name = MODULE_NAME;
ldd_tty_driver->name = MODULE_NAME;
ldd_tty_driver->major = LDD_TTY_MAJOR;
ldd_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
ldd_tty_driver->init_termios = tty_std_termios;
ldd_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
tty_set_operations(ldd_tty_driver, &ttyprintk_ops);
ret = tty_register_driver(ldd_tty_driver);
if (ret < 0) {
printk(KERN_ERR "Couldn't register ttyprintk driver\n");
goto error;
}
return 0;
error:
put_tty_driver(ldd_tty_driver);
for (i = 0; i < LDD_TTY_MINOR_NR; i)
tty_port_destroy(ldd_tty_port i);
return ret;
}
static
void __exit m_exit(void)
{
int i;
tty_unregister_driver(ldd_tty_driver);
put_tty_driver(ldd_tty_driver);
for (i = 0; i < LDD_TTY_MINOR_NR; i)
tty_port_destroy(ldd_tty_port i);
}
module_init(m_init);
module_exit(m_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("d0u9");
MODULE_DESCRIPTION("A fake tty device");