Commit | Line | Data |
---|---|---|
dbddf429 | 1 | // SPDX-License-Identifier: GPL-2.0 |
165dc591 | 2 | /* |
2f8a2dc2 | 3 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
1da177e4 LT |
4 | */ |
5 | ||
37185b33 AV |
6 | #include <linux/irqreturn.h> |
7 | #include <linux/kd.h> | |
3f07c014 | 8 | #include <linux/sched/signal.h> |
37185b33 | 9 | #include <linux/slab.h> |
3f07c014 | 10 | |
510c72a3 | 11 | #include "chan.h" |
37185b33 AV |
12 | #include <irq_kern.h> |
13 | #include <irq_user.h> | |
14 | #include <kern_util.h> | |
15 | #include <os.h> | |
1da177e4 LT |
16 | |
17 | #define LINE_BUFSIZE 4096 | |
18 | ||
7bea96fd | 19 | static irqreturn_t line_interrupt(int irq, void *data) |
1da177e4 | 20 | { |
165dc591 JD |
21 | struct chan *chan = data; |
22 | struct line *line = chan->line; | |
1da177e4 LT |
23 | |
24 | if (line) | |
2e124b4a JS |
25 | chan_interrupt(line, irq); |
26 | ||
1da177e4 LT |
27 | return IRQ_HANDLED; |
28 | } | |
29 | ||
2f8a2dc2 JD |
30 | /* |
31 | * Returns the free space inside the ring buffer of this line. | |
b97b77cc | 32 | * |
b60745b9 | 33 | * Should be called while holding line->lock (this does not modify data). |
b97b77cc | 34 | */ |
03b3b1a2 | 35 | static unsigned int write_room(struct line *line) |
1da177e4 LT |
36 | { |
37 | int n; | |
38 | ||
b97b77cc PBG |
39 | if (line->buffer == NULL) |
40 | return LINE_BUFSIZE - 1; | |
41 | ||
42 | /* This is for the case where the buffer is wrapped! */ | |
43 | n = line->head - line->tail; | |
1da177e4 | 44 | |
1da177e4 | 45 | if (n <= 0) |
acb2cf34 | 46 | n += LINE_BUFSIZE; /* The other case */ |
b97b77cc PBG |
47 | return n - 1; |
48 | } | |
49 | ||
03b3b1a2 | 50 | unsigned int line_write_room(struct tty_struct *tty) |
b97b77cc PBG |
51 | { |
52 | struct line *line = tty->driver_data; | |
53 | unsigned long flags; | |
03b3b1a2 | 54 | unsigned int room; |
b97b77cc | 55 | |
b97b77cc PBG |
56 | spin_lock_irqsave(&line->lock, flags); |
57 | room = write_room(line); | |
58 | spin_unlock_irqrestore(&line->lock, flags); | |
59 | ||
b97b77cc | 60 | return room; |
1da177e4 LT |
61 | } |
62 | ||
fff4ef17 | 63 | unsigned int line_chars_in_buffer(struct tty_struct *tty) |
b97b77cc PBG |
64 | { |
65 | struct line *line = tty->driver_data; | |
66 | unsigned long flags; | |
fff4ef17 | 67 | unsigned int ret; |
b97b77cc PBG |
68 | |
69 | spin_lock_irqsave(&line->lock, flags); | |
acb2cf34 | 70 | /* write_room subtracts 1 for the needed NULL, so we readd it.*/ |
b97b77cc PBG |
71 | ret = LINE_BUFSIZE - (write_room(line) + 1); |
72 | spin_unlock_irqrestore(&line->lock, flags); | |
73 | ||
74 | return ret; | |
75 | } | |
76 | ||
77 | /* | |
78 | * This copies the content of buf into the circular buffer associated with | |
79 | * this line. | |
80 | * The return value is the number of characters actually copied, i.e. the ones | |
81 | * for which there was space: this function is not supposed to ever flush out | |
82 | * the circular buffer. | |
83 | * | |
84 | * Must be called while holding line->lock! | |
85 | */ | |
1da177e4 LT |
86 | static int buffer_data(struct line *line, const char *buf, int len) |
87 | { | |
88 | int end, room; | |
89 | ||
2f8a2dc2 | 90 | if (line->buffer == NULL) { |
1da177e4 LT |
91 | line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC); |
92 | if (line->buffer == NULL) { | |
2f8a2dc2 JD |
93 | printk(KERN_ERR "buffer_data - atomic allocation " |
94 | "failed\n"); | |
95 | return 0; | |
1da177e4 LT |
96 | } |
97 | line->head = line->buffer; | |
98 | line->tail = line->buffer; | |
99 | } | |
100 | ||
101 | room = write_room(line); | |
102 | len = (len > room) ? room : len; | |
103 | ||
104 | end = line->buffer + LINE_BUFSIZE - line->tail; | |
b97b77cc | 105 | |
2f8a2dc2 | 106 | if (len < end) { |
1da177e4 LT |
107 | memcpy(line->tail, buf, len); |
108 | line->tail += len; | |
d50084a2 JD |
109 | } |
110 | else { | |
b97b77cc | 111 | /* The circular buffer is wrapping */ |
1da177e4 LT |
112 | memcpy(line->tail, buf, end); |
113 | buf += end; | |
114 | memcpy(line->buffer, buf, len - end); | |
115 | line->tail = line->buffer + len - end; | |
116 | } | |
117 | ||
b97b77cc | 118 | return len; |
1da177e4 LT |
119 | } |
120 | ||
b97b77cc PBG |
121 | /* |
122 | * Flushes the ring buffer to the output channels. That is, write_chan is | |
123 | * called, passing it line->head as buffer, and an appropriate count. | |
124 | * | |
125 | * On exit, returns 1 when the buffer is empty, | |
126 | * 0 when the buffer is not empty on exit, | |
127 | * and -errno when an error occurred. | |
128 | * | |
129 | * Must be called while holding line->lock!*/ | |
1da177e4 LT |
130 | static int flush_buffer(struct line *line) |
131 | { | |
132 | int n, count; | |
133 | ||
134 | if ((line->buffer == NULL) || (line->head == line->tail)) | |
b97b77cc | 135 | return 1; |
1da177e4 LT |
136 | |
137 | if (line->tail < line->head) { | |
b97b77cc | 138 | /* line->buffer + LINE_BUFSIZE is the end of the buffer! */ |
1da177e4 | 139 | count = line->buffer + LINE_BUFSIZE - line->head; |
b97b77cc | 140 | |
bed5e39c | 141 | n = write_chan(line->chan_out, line->head, count, |
d5a9597d | 142 | line->write_irq); |
1da177e4 | 143 | if (n < 0) |
b97b77cc PBG |
144 | return n; |
145 | if (n == count) { | |
2f8a2dc2 JD |
146 | /* |
147 | * We have flushed from ->head to buffer end, now we | |
148 | * must flush only from the beginning to ->tail. | |
149 | */ | |
1da177e4 | 150 | line->head = line->buffer; |
b97b77cc | 151 | } else { |
1da177e4 | 152 | line->head += n; |
b97b77cc | 153 | return 0; |
1da177e4 LT |
154 | } |
155 | } | |
156 | ||
157 | count = line->tail - line->head; | |
bed5e39c | 158 | n = write_chan(line->chan_out, line->head, count, |
d5a9597d | 159 | line->write_irq); |
b97b77cc | 160 | |
2f8a2dc2 | 161 | if (n < 0) |
b97b77cc | 162 | return n; |
1da177e4 LT |
163 | |
164 | line->head += n; | |
b97b77cc PBG |
165 | return line->head == line->tail; |
166 | } | |
167 | ||
168 | void line_flush_buffer(struct tty_struct *tty) | |
169 | { | |
170 | struct line *line = tty->driver_data; | |
171 | unsigned long flags; | |
b97b77cc | 172 | |
b97b77cc | 173 | spin_lock_irqsave(&line->lock, flags); |
f1c93e49 | 174 | flush_buffer(line); |
b97b77cc | 175 | spin_unlock_irqrestore(&line->lock, flags); |
b97b77cc PBG |
176 | } |
177 | ||
2f8a2dc2 JD |
178 | /* |
179 | * We map both ->flush_chars and ->put_char (which go in pair) onto | |
180 | * ->flush_buffer and ->write. Hope it's not that bad. | |
181 | */ | |
b97b77cc PBG |
182 | void line_flush_chars(struct tty_struct *tty) |
183 | { | |
184 | line_flush_buffer(tty); | |
185 | } | |
186 | ||
95713967 | 187 | ssize_t line_write(struct tty_struct *tty, const u8 *buf, size_t len) |
1da177e4 LT |
188 | { |
189 | struct line *line = tty->driver_data; | |
190 | unsigned long flags; | |
c59dbcad | 191 | int n, ret = 0; |
1da177e4 | 192 | |
b97b77cc | 193 | spin_lock_irqsave(&line->lock, flags); |
c59dbcad | 194 | if (line->head != line->tail) |
1da177e4 | 195 | ret = buffer_data(line, buf, len); |
c59dbcad | 196 | else { |
bed5e39c | 197 | n = write_chan(line->chan_out, buf, len, |
d5a9597d | 198 | line->write_irq); |
b97b77cc | 199 | if (n < 0) { |
1da177e4 LT |
200 | ret = n; |
201 | goto out_up; | |
202 | } | |
203 | ||
204 | len -= n; | |
205 | ret += n; | |
b97b77cc | 206 | if (len > 0) |
1da177e4 LT |
207 | ret += buffer_data(line, buf + n, len); |
208 | } | |
b97b77cc PBG |
209 | out_up: |
210 | spin_unlock_irqrestore(&line->lock, flags); | |
211 | return ret; | |
1da177e4 LT |
212 | } |
213 | ||
e4dcee80 JD |
214 | void line_throttle(struct tty_struct *tty) |
215 | { | |
216 | struct line *line = tty->driver_data; | |
217 | ||
d5a9597d | 218 | deactivate_chan(line->chan_in, line->read_irq); |
e4dcee80 JD |
219 | line->throttled = 1; |
220 | } | |
221 | ||
222 | void line_unthrottle(struct tty_struct *tty) | |
223 | { | |
224 | struct line *line = tty->driver_data; | |
225 | ||
226 | line->throttled = 0; | |
d5a9597d | 227 | chan_interrupt(line, line->read_irq); |
e4dcee80 JD |
228 | } |
229 | ||
7bea96fd | 230 | static irqreturn_t line_write_interrupt(int irq, void *data) |
1da177e4 | 231 | { |
165dc591 JD |
232 | struct chan *chan = data; |
233 | struct line *line = chan->line; | |
1da177e4 LT |
234 | int err; |
235 | ||
2f8a2dc2 | 236 | /* |
c0b79a90 YZ |
237 | * Interrupts are disabled here because genirq keep irqs disabled when |
238 | * calling the action handler. | |
2f8a2dc2 | 239 | */ |
b97b77cc | 240 | |
ec0ac8ad | 241 | spin_lock(&line->lock); |
1da177e4 | 242 | err = flush_buffer(line); |
b97b77cc | 243 | if (err == 0) { |
199eebba | 244 | spin_unlock(&line->lock); |
b97b77cc | 245 | return IRQ_NONE; |
917e2fd2 | 246 | } else if ((err < 0) && (err != -EAGAIN)) { |
1da177e4 LT |
247 | line->head = line->buffer; |
248 | line->tail = line->buffer; | |
249 | } | |
ec0ac8ad | 250 | spin_unlock(&line->lock); |
1da177e4 | 251 | |
6aad04f2 | 252 | tty_port_tty_wakeup(&line->port); |
6fc58845 | 253 | |
b97b77cc | 254 | return IRQ_HANDLED; |
1da177e4 LT |
255 | } |
256 | ||
165dc591 | 257 | int line_setup_irq(int fd, int input, int output, struct line *line, void *data) |
1da177e4 | 258 | { |
5e7672ec | 259 | const struct line_driver *driver = line->driver; |
36d46a59 | 260 | int err; |
1da177e4 | 261 | |
36d46a59 | 262 | if (input) { |
d5a9597d JB |
263 | err = um_request_irq(UM_IRQ_ALLOC, fd, IRQ_READ, |
264 | line_interrupt, 0, | |
aab94460 | 265 | driver->read_irq_name, data); |
36d46a59 JB |
266 | if (err < 0) |
267 | return err; | |
d5a9597d JB |
268 | |
269 | line->read_irq = err; | |
36d46a59 JB |
270 | } |
271 | ||
272 | if (output) { | |
d5a9597d JB |
273 | err = um_request_irq(UM_IRQ_ALLOC, fd, IRQ_WRITE, |
274 | line_write_interrupt, 0, | |
aab94460 | 275 | driver->write_irq_name, data); |
36d46a59 JB |
276 | if (err < 0) |
277 | return err; | |
d5a9597d JB |
278 | |
279 | line->write_irq = err; | |
36d46a59 JB |
280 | } |
281 | ||
282 | return 0; | |
1da177e4 LT |
283 | } |
284 | ||
79e0273d | 285 | static int line_activate(struct tty_port *port, struct tty_struct *tty) |
1da177e4 | 286 | { |
79e0273d RW |
287 | int ret; |
288 | struct line *line = tty->driver_data; | |
165dc591 | 289 | |
79e0273d RW |
290 | ret = enable_chan(line); |
291 | if (ret) | |
292 | return ret; | |
d14ad81f | 293 | |
2f8a2dc2 | 294 | if (!line->sigio) { |
2116bda6 | 295 | chan_enable_winch(line->chan_out, port); |
d79a5809 | 296 | line->sigio = 1; |
1da177e4 | 297 | } |
1da177e4 | 298 | |
bed5e39c | 299 | chan_window_size(line, &tty->winsize.ws_row, |
79e0273d RW |
300 | &tty->winsize.ws_col); |
301 | ||
302 | return 0; | |
1da177e4 LT |
303 | } |
304 | ||
cc4f0248 RW |
305 | static void unregister_winch(struct tty_struct *tty); |
306 | ||
307 | static void line_destruct(struct tty_port *port) | |
308 | { | |
309 | struct tty_struct *tty = tty_port_tty_get(port); | |
310 | struct line *line = tty->driver_data; | |
311 | ||
312 | if (line->sigio) { | |
313 | unregister_winch(tty); | |
314 | line->sigio = 0; | |
315 | } | |
316 | } | |
317 | ||
79e0273d RW |
318 | static const struct tty_port_operations line_port_ops = { |
319 | .activate = line_activate, | |
cc4f0248 | 320 | .destruct = line_destruct, |
79e0273d | 321 | }; |
cd2ee4a3 | 322 | |
79e0273d | 323 | int line_open(struct tty_struct *tty, struct file *filp) |
1da177e4 LT |
324 | { |
325 | struct line *line = tty->driver_data; | |
326 | ||
79e0273d RW |
327 | return tty_port_open(&line->port, tty, filp); |
328 | } | |
b97b77cc | 329 | |
79e0273d RW |
330 | int line_install(struct tty_driver *driver, struct tty_struct *tty, |
331 | struct line *line) | |
332 | { | |
333 | int ret; | |
b97b77cc | 334 | |
79e0273d RW |
335 | ret = tty_standard_install(driver, tty); |
336 | if (ret) | |
337 | return ret; | |
cd2ee4a3 | 338 | |
79e0273d | 339 | tty->driver_data = line; |
d79a5809 | 340 | |
79e0273d RW |
341 | return 0; |
342 | } | |
343 | ||
79e0273d RW |
344 | void line_close(struct tty_struct *tty, struct file * filp) |
345 | { | |
346 | struct line *line = tty->driver_data; | |
cd2ee4a3 | 347 | |
79e0273d RW |
348 | tty_port_close(&line->port, tty, filp); |
349 | } | |
350 | ||
351 | void line_hangup(struct tty_struct *tty) | |
352 | { | |
353 | struct line *line = tty->driver_data; | |
354 | ||
355 | tty_port_hangup(&line->port); | |
1da177e4 LT |
356 | } |
357 | ||
358 | void close_lines(struct line *lines, int nlines) | |
359 | { | |
360 | int i; | |
361 | ||
362 | for(i = 0; i < nlines; i++) | |
10c890c0 | 363 | close_chan(&lines[i]); |
1da177e4 LT |
364 | } |
365 | ||
04292b2c AV |
366 | int setup_one_line(struct line *lines, int n, char *init, |
367 | const struct chan_opts *opts, char **error_out) | |
d79a5809 JD |
368 | { |
369 | struct line *line = &lines[n]; | |
cfe6b7c7 | 370 | struct tty_driver *driver = line->driver->driver; |
f28169d2 | 371 | int err = -EINVAL; |
d79a5809 | 372 | |
060ed31d | 373 | if (line->port.count) { |
f28169d2 | 374 | *error_out = "Device is already open"; |
d79a5809 JD |
375 | goto out; |
376 | } | |
377 | ||
31efcebb AV |
378 | if (!strcmp(init, "none")) { |
379 | if (line->valid) { | |
380 | line->valid = 0; | |
381 | kfree(line->init_str); | |
cfe6b7c7 | 382 | tty_unregister_device(driver, n); |
31efcebb AV |
383 | parse_chan_pair(NULL, line, n, opts, error_out); |
384 | err = 0; | |
385 | } | |
386 | } else { | |
387 | char *new = kstrdup(init, GFP_KERNEL); | |
388 | if (!new) { | |
389 | *error_out = "Failed to allocate memory"; | |
390 | return -ENOMEM; | |
391 | } | |
c8e2876f | 392 | if (line->valid) { |
cfe6b7c7 | 393 | tty_unregister_device(driver, n); |
c8e2876f AV |
394 | kfree(line->init_str); |
395 | } | |
31efcebb | 396 | line->init_str = new; |
43574c1a | 397 | line->valid = 1; |
31efcebb | 398 | err = parse_chan_pair(new, line, n, opts, error_out); |
cfe6b7c7 | 399 | if (!err) { |
734cc178 JS |
400 | struct device *d = tty_port_register_device(&line->port, |
401 | driver, n, NULL); | |
cfe6b7c7 AV |
402 | if (IS_ERR(d)) { |
403 | *error_out = "Failed to register device"; | |
404 | err = PTR_ERR(d); | |
405 | parse_chan_pair(NULL, line, n, opts, error_out); | |
406 | } | |
407 | } | |
31efcebb AV |
408 | if (err) { |
409 | line->init_str = NULL; | |
410 | line->valid = 0; | |
411 | kfree(new); | |
412 | } | |
d79a5809 JD |
413 | } |
414 | out: | |
f28169d2 | 415 | return err; |
d79a5809 JD |
416 | } |
417 | ||
2f8a2dc2 JD |
418 | /* |
419 | * Common setup code for both startup command line and mconsole initialization. | |
4b3f686d | 420 | * @lines contains the array (of size @num) to modify; |
b97b77cc | 421 | * @init is the setup string; |
f28169d2 | 422 | * @error_out is an error string in the case of failure; |
d571cd18 | 423 | */ |
b97b77cc | 424 | |
43574c1a AV |
425 | int line_setup(char **conf, unsigned int num, char **def, |
426 | char *init, char *name) | |
1da177e4 | 427 | { |
43574c1a | 428 | char *error; |
1da177e4 | 429 | |
2f8a2dc2 JD |
430 | if (*init == '=') { |
431 | /* | |
432 | * We said con=/ssl= instead of con#=, so we are configuring all | |
433 | * consoles at once. | |
434 | */ | |
43574c1a AV |
435 | *def = init + 1; |
436 | } else { | |
437 | char *end; | |
438 | unsigned n = simple_strtoul(init, &end, 0); | |
439 | ||
2f8a2dc2 | 440 | if (*end != '=') { |
43574c1a AV |
441 | error = "Couldn't parse device number"; |
442 | goto out; | |
1da177e4 | 443 | } |
43574c1a AV |
444 | if (n >= num) { |
445 | error = "Device number out of range"; | |
446 | goto out; | |
f28169d2 | 447 | } |
43574c1a | 448 | conf[n] = end + 1; |
1da177e4 | 449 | } |
43574c1a AV |
450 | return 0; |
451 | ||
452 | out: | |
453 | printk(KERN_ERR "Failed to set up %s with " | |
454 | "configuration string \"%s\" : %s\n", name, init, error); | |
455 | return -EINVAL; | |
1da177e4 LT |
456 | } |
457 | ||
1f80171e | 458 | int line_config(struct line *lines, unsigned int num, char *str, |
f28169d2 | 459 | const struct chan_opts *opts, char **error_out) |
1da177e4 | 460 | { |
fe9a6b00 | 461 | char *end; |
31efcebb | 462 | int n; |
1da177e4 | 463 | |
2f8a2dc2 | 464 | if (*str == '=') { |
f28169d2 JD |
465 | *error_out = "Can't configure all devices from mconsole"; |
466 | return -EINVAL; | |
d571cd18 JD |
467 | } |
468 | ||
fe9a6b00 AV |
469 | n = simple_strtoul(str, &end, 0); |
470 | if (*end++ != '=') { | |
471 | *error_out = "Couldn't parse device number"; | |
472 | return -EINVAL; | |
473 | } | |
474 | if (n >= num) { | |
475 | *error_out = "Device number out of range"; | |
476 | return -EINVAL; | |
477 | } | |
478 | ||
31efcebb | 479 | return setup_one_line(lines, n, end, opts, error_out); |
1da177e4 LT |
480 | } |
481 | ||
b97b77cc | 482 | int line_get_config(char *name, struct line *lines, unsigned int num, char *str, |
1da177e4 LT |
483 | int size, char **error_out) |
484 | { | |
485 | struct line *line; | |
486 | char *end; | |
487 | int dev, n = 0; | |
488 | ||
489 | dev = simple_strtoul(name, &end, 0); | |
2f8a2dc2 | 490 | if ((*end != '\0') || (end == name)) { |
1da177e4 | 491 | *error_out = "line_get_config failed to parse device number"; |
b97b77cc | 492 | return 0; |
1da177e4 LT |
493 | } |
494 | ||
2f8a2dc2 | 495 | if ((dev < 0) || (dev >= num)) { |
b97b77cc PBG |
496 | *error_out = "device number out of range"; |
497 | return 0; | |
1da177e4 LT |
498 | } |
499 | ||
500 | line = &lines[dev]; | |
501 | ||
2f8a2dc2 | 502 | if (!line->valid) |
1da177e4 | 503 | CONFIG_CHUNK(str, size, n, "none", 1); |
6fc58845 JS |
504 | else { |
505 | struct tty_struct *tty = tty_port_tty_get(&line->port); | |
506 | if (tty == NULL) { | |
507 | CONFIG_CHUNK(str, size, n, line->init_str, 1); | |
508 | } else { | |
509 | n = chan_config_string(line, str, size, error_out); | |
510 | tty_kref_put(tty); | |
511 | } | |
512 | } | |
1da177e4 | 513 | |
b97b77cc | 514 | return n; |
1da177e4 LT |
515 | } |
516 | ||
29d56cfe JD |
517 | int line_id(char **str, int *start_out, int *end_out) |
518 | { | |
519 | char *end; | |
2f8a2dc2 | 520 | int n; |
29d56cfe JD |
521 | |
522 | n = simple_strtoul(*str, &end, 0); | |
2f8a2dc2 JD |
523 | if ((*end != '\0') || (end == *str)) |
524 | return -1; | |
29d56cfe | 525 | |
2f8a2dc2 JD |
526 | *str = end; |
527 | *start_out = n; | |
528 | *end_out = n; | |
529 | return n; | |
29d56cfe JD |
530 | } |
531 | ||
f28169d2 | 532 | int line_remove(struct line *lines, unsigned int num, int n, char **error_out) |
1da177e4 | 533 | { |
da645f3b AV |
534 | if (n >= num) { |
535 | *error_out = "Device number out of range"; | |
536 | return -EINVAL; | |
537 | } | |
31efcebb | 538 | return setup_one_line(lines, n, "none", NULL, error_out); |
1da177e4 LT |
539 | } |
540 | ||
cfe6b7c7 AV |
541 | int register_lines(struct line_driver *line_driver, |
542 | const struct tty_operations *ops, | |
543 | struct line *lines, int nlines) | |
1da177e4 | 544 | { |
39b7b42b | 545 | struct tty_driver *driver; |
cfe6b7c7 | 546 | int err; |
04292b2c | 547 | int i; |
1da177e4 | 548 | |
39b7b42b JS |
549 | driver = tty_alloc_driver(nlines, TTY_DRIVER_REAL_RAW | |
550 | TTY_DRIVER_DYNAMIC_DEV); | |
551 | if (IS_ERR(driver)) | |
552 | return PTR_ERR(driver); | |
1da177e4 LT |
553 | |
554 | driver->driver_name = line_driver->name; | |
555 | driver->name = line_driver->device_name; | |
1da177e4 LT |
556 | driver->major = line_driver->major; |
557 | driver->minor_start = line_driver->minor_start; | |
558 | driver->type = line_driver->type; | |
559 | driver->subtype = line_driver->subtype; | |
1da177e4 | 560 | driver->init_termios = tty_std_termios; |
39b7b42b | 561 | |
04292b2c | 562 | for (i = 0; i < nlines; i++) { |
060ed31d | 563 | tty_port_init(&lines[i].port); |
79e0273d | 564 | lines[i].port.ops = &line_port_ops; |
04292b2c | 565 | spin_lock_init(&lines[i].lock); |
04292b2c AV |
566 | lines[i].driver = line_driver; |
567 | INIT_LIST_HEAD(&lines[i].chan_list); | |
568 | } | |
1da177e4 LT |
569 | tty_set_operations(driver, ops); |
570 | ||
cfe6b7c7 AV |
571 | err = tty_register_driver(driver); |
572 | if (err) { | |
2f8a2dc2 JD |
573 | printk(KERN_ERR "register_lines : can't register %s driver\n", |
574 | line_driver->name); | |
9f90a4dd | 575 | tty_driver_kref_put(driver); |
191c5f10 JS |
576 | for (i = 0; i < nlines; i++) |
577 | tty_port_destroy(&lines[i].port); | |
cfe6b7c7 | 578 | return err; |
1da177e4 LT |
579 | } |
580 | ||
cfe6b7c7 | 581 | line_driver->driver = driver; |
1da177e4 | 582 | mconsole_register_dev(&line_driver->mc); |
cfe6b7c7 | 583 | return 0; |
1da177e4 LT |
584 | } |
585 | ||
9010772c JD |
586 | static DEFINE_SPINLOCK(winch_handler_lock); |
587 | static LIST_HEAD(winch_handlers); | |
605a69ac | 588 | |
1da177e4 LT |
589 | struct winch { |
590 | struct list_head list; | |
591 | int fd; | |
592 | int tty_fd; | |
593 | int pid; | |
2116bda6 | 594 | struct tty_port *port; |
42a359e3 | 595 | unsigned long stack; |
7cf3cf21 | 596 | struct work_struct work; |
1da177e4 LT |
597 | }; |
598 | ||
7cf3cf21 | 599 | static void __free_winch(struct work_struct *work) |
42a359e3 | 600 | { |
7cf3cf21 | 601 | struct winch *winch = container_of(work, struct winch, work); |
fa7a0449 | 602 | um_free_irq(WINCH_IRQ, winch); |
42a359e3 JD |
603 | |
604 | if (winch->pid != -1) | |
605 | os_kill_process(winch->pid, 1); | |
42a359e3 JD |
606 | if (winch->stack != 0) |
607 | free_stack(winch->stack, 0); | |
42a359e3 JD |
608 | kfree(winch); |
609 | } | |
610 | ||
7cf3cf21 AV |
611 | static void free_winch(struct winch *winch) |
612 | { | |
613 | int fd = winch->fd; | |
614 | winch->fd = -1; | |
615 | if (fd != -1) | |
616 | os_close_file(fd); | |
7cf3cf21 AV |
617 | __free_winch(&winch->work); |
618 | } | |
619 | ||
7bea96fd | 620 | static irqreturn_t winch_interrupt(int irq, void *data) |
1da177e4 LT |
621 | { |
622 | struct winch *winch = data; | |
623 | struct tty_struct *tty; | |
624 | struct line *line; | |
7cf3cf21 | 625 | int fd = winch->fd; |
1da177e4 LT |
626 | int err; |
627 | char c; | |
8a8a5510 | 628 | struct pid *pgrp; |
1da177e4 | 629 | |
7cf3cf21 AV |
630 | if (fd != -1) { |
631 | err = generic_read(fd, &c, NULL); | |
2f8a2dc2 JD |
632 | if (err < 0) { |
633 | if (err != -EAGAIN) { | |
7cf3cf21 AV |
634 | winch->fd = -1; |
635 | list_del(&winch->list); | |
636 | os_close_file(fd); | |
2f8a2dc2 JD |
637 | printk(KERN_ERR "winch_interrupt : " |
638 | "read failed, errno = %d\n", -err); | |
639 | printk(KERN_ERR "fd %d is losing SIGWINCH " | |
640 | "support\n", winch->tty_fd); | |
7cf3cf21 AV |
641 | INIT_WORK(&winch->work, __free_winch); |
642 | schedule_work(&winch->work); | |
b97b77cc | 643 | return IRQ_HANDLED; |
1da177e4 LT |
644 | } |
645 | goto out; | |
646 | } | |
647 | } | |
2116bda6 | 648 | tty = tty_port_tty_get(winch->port); |
1da177e4 LT |
649 | if (tty != NULL) { |
650 | line = tty->driver_data; | |
438ee679 | 651 | if (line != NULL) { |
bed5e39c | 652 | chan_window_size(line, &tty->winsize.ws_row, |
438ee679 | 653 | &tty->winsize.ws_col); |
8a8a5510 PH |
654 | pgrp = tty_get_pgrp(tty); |
655 | if (pgrp) | |
656 | kill_pgrp(pgrp, SIGWINCH, 1); | |
657 | put_pid(pgrp); | |
438ee679 | 658 | } |
2116bda6 | 659 | tty_kref_put(tty); |
1da177e4 LT |
660 | } |
661 | out: | |
b97b77cc | 662 | return IRQ_HANDLED; |
1da177e4 LT |
663 | } |
664 | ||
2116bda6 | 665 | void register_winch_irq(int fd, int tty_fd, int pid, struct tty_port *port, |
42a359e3 | 666 | unsigned long stack) |
1da177e4 LT |
667 | { |
668 | struct winch *winch; | |
669 | ||
1da177e4 LT |
670 | winch = kmalloc(sizeof(*winch), GFP_KERNEL); |
671 | if (winch == NULL) { | |
2f8a2dc2 | 672 | printk(KERN_ERR "register_winch_irq - kmalloc failed\n"); |
42a359e3 | 673 | goto cleanup; |
1da177e4 | 674 | } |
605a69ac | 675 | |
1da177e4 LT |
676 | *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list), |
677 | .fd = fd, | |
678 | .tty_fd = tty_fd, | |
679 | .pid = pid, | |
2116bda6 | 680 | .port = port, |
42a359e3 JD |
681 | .stack = stack }); |
682 | ||
683 | if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt, | |
aab94460 | 684 | IRQF_SHARED, "winch", winch) < 0) { |
2f8a2dc2 JD |
685 | printk(KERN_ERR "register_winch_irq - failed to register " |
686 | "IRQ\n"); | |
42a359e3 JD |
687 | goto out_free; |
688 | } | |
605a69ac PBG |
689 | |
690 | spin_lock(&winch_handler_lock); | |
1da177e4 | 691 | list_add(&winch->list, &winch_handlers); |
605a69ac PBG |
692 | spin_unlock(&winch_handler_lock); |
693 | ||
42a359e3 | 694 | return; |
e464bf2b | 695 | |
42a359e3 | 696 | out_free: |
e464bf2b | 697 | kfree(winch); |
42a359e3 JD |
698 | cleanup: |
699 | os_kill_process(pid, 1); | |
700 | os_close_file(fd); | |
701 | if (stack != 0) | |
702 | free_stack(stack, 0); | |
e464bf2b JD |
703 | } |
704 | ||
cd2ee4a3 JD |
705 | static void unregister_winch(struct tty_struct *tty) |
706 | { | |
48a0b740 | 707 | struct list_head *ele, *next; |
e464bf2b | 708 | struct winch *winch; |
2116bda6 | 709 | struct tty_struct *wtty; |
cd2ee4a3 | 710 | |
605a69ac | 711 | spin_lock(&winch_handler_lock); |
e464bf2b | 712 | |
48a0b740 | 713 | list_for_each_safe(ele, next, &winch_handlers) { |
cd2ee4a3 | 714 | winch = list_entry(ele, struct winch, list); |
2116bda6 RW |
715 | wtty = tty_port_tty_get(winch->port); |
716 | if (wtty == tty) { | |
f4ab7818 JB |
717 | list_del(&winch->list); |
718 | spin_unlock(&winch_handler_lock); | |
7cf3cf21 | 719 | free_winch(winch); |
e464bf2b | 720 | break; |
2f8a2dc2 | 721 | } |
2116bda6 | 722 | tty_kref_put(wtty); |
2f8a2dc2 | 723 | } |
605a69ac | 724 | spin_unlock(&winch_handler_lock); |
cd2ee4a3 JD |
725 | } |
726 | ||
1da177e4 LT |
727 | static void winch_cleanup(void) |
728 | { | |
1da177e4 LT |
729 | struct winch *winch; |
730 | ||
e464bf2b | 731 | spin_lock(&winch_handler_lock); |
f4ab7818 JB |
732 | while ((winch = list_first_entry_or_null(&winch_handlers, |
733 | struct winch, list))) { | |
734 | list_del(&winch->list); | |
735 | spin_unlock(&winch_handler_lock); | |
e464bf2b | 736 | |
7cf3cf21 | 737 | free_winch(winch); |
f4ab7818 JB |
738 | |
739 | spin_lock(&winch_handler_lock); | |
1da177e4 | 740 | } |
e464bf2b JD |
741 | |
742 | spin_unlock(&winch_handler_lock); | |
1da177e4 LT |
743 | } |
744 | __uml_exitcall(winch_cleanup); | |
745 | ||
746 | char *add_xterm_umid(char *base) | |
747 | { | |
748 | char *umid, *title; | |
749 | int len; | |
750 | ||
7eebe8a9 | 751 | umid = get_umid(); |
2f8a2dc2 | 752 | if (*umid == '\0') |
b97b77cc | 753 | return base; |
165dc591 | 754 | |
1da177e4 LT |
755 | len = strlen(base) + strlen(" ()") + strlen(umid) + 1; |
756 | title = kmalloc(len, GFP_KERNEL); | |
2f8a2dc2 JD |
757 | if (title == NULL) { |
758 | printk(KERN_ERR "Failed to allocate buffer for xterm title\n"); | |
b97b77cc | 759 | return base; |
1da177e4 LT |
760 | } |
761 | ||
762 | snprintf(title, len, "%s (%s)", base, umid); | |
b97b77cc | 763 | return title; |
1da177e4 | 764 | } |