blob: 2fb509d57e88c4ee5d1e99b00086ff11ccaec361 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Provide access to virtual console memory.
4 * /dev/vcs0: the screen as it is being viewed right now (possibly scrolled)
5 * /dev/vcsN: the screen of /dev/ttyN (1 <= N <= 63)
6 * [minor: N]
7 *
8 * /dev/vcsaN: idem, but including attributes, and prefixed with
9 * the 4 bytes lines,columns,x,y (as screendump used to give).
10 * Attribute/character pair is in native endianity.
11 * [minor: N+128]
12 *
Nicolas Pitred21b0be2018-06-26 23:56:41 -040013 * /dev/vcsuN: similar to /dev/vcsaN but using 4-byte unicode values
14 * instead of 1-byte screen glyph values.
15 * [minor: N+64]
16 *
17 * /dev/vcsuaN: same idea as /dev/vcsaN for unicode (not yet implemented).
18 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * This replaces screendump and part of selection, so that the system
20 * administrator can control access using file system permissions.
21 *
22 * aeb@cwi.nl - efter Friedas begravelse - 950211
23 *
24 * machek@k332.feld.cvut.cz - modified not to send characters to wrong console
25 * - fixed some fatal off-by-one bugs (0-- no longer == -1 -> looping and looping and looping...)
26 * - making it shorter - scr_readw are macros which expand in PRETTY long code
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
30#include <linux/major.h>
31#include <linux/errno.h>
Paul Gortmaker0e648f42011-05-27 10:46:24 -040032#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/interrupt.h>
35#include <linux/mm.h>
36#include <linux/init.h>
37#include <linux/vt_kern.h>
38#include <linux/selection.h>
39#include <linux/kbd_kern.h>
40#include <linux/console.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/device.h>
Nicolas Pitre47725ac2010-10-05 14:22:37 -040042#include <linux/sched.h>
43#include <linux/fs.h>
44#include <linux/poll.h>
45#include <linux/signal.h>
46#include <linux/slab.h>
47#include <linux/notifier.h>
Matthias Kaehlckec831c332007-05-08 00:39:49 -070048
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080049#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/byteorder.h>
51#include <asm/unaligned.h>
52
53#undef attr
54#undef org
55#undef addr
56#define HEADER_SIZE 4
57
Jiri Olsafcdba072011-02-07 19:31:25 +010058#define CON_BUF_SIZE (CONFIG_BASE_SMALL ? 256 : PAGE_SIZE)
59
Nicolas Pitred21b0be2018-06-26 23:56:41 -040060/*
61 * Our minor space:
62 *
63 * 0 ... 63 glyph mode without attributes
64 * 64 ... 127 unicode mode without attributes
65 * 128 ... 191 glyph mode with attributes
66 * 192 ... 255 unused (reserved for unicode with attributes)
67 *
68 * This relies on MAX_NR_CONSOLES being <= 63, meaning 63 actual consoles
69 * with minors 0, 64, 128 and 192 being proxies for the foreground console.
70 */
71#if MAX_NR_CONSOLES > 63
72#warning "/dev/vcs* devices may not accommodate more than 63 consoles"
73#endif
74
75#define console(inode) (iminor(inode) & 63)
76#define use_unicode(inode) (iminor(inode) & 64)
77#define use_attributes(inode) (iminor(inode) & 128)
78
79
Nicolas Pitre47725ac2010-10-05 14:22:37 -040080struct vcs_poll_data {
81 struct notifier_block notifier;
82 unsigned int cons_num;
83 bool seen_last_update;
84 wait_queue_head_t waitq;
85 struct fasync_struct *fasync;
86};
87
88static int
89vcs_notifier(struct notifier_block *nb, unsigned long code, void *_param)
90{
91 struct vt_notifier_param *param = _param;
92 struct vc_data *vc = param->vc;
93 struct vcs_poll_data *poll =
94 container_of(nb, struct vcs_poll_data, notifier);
95 int currcons = poll->cons_num;
96
97 if (code != VT_UPDATE)
98 return NOTIFY_DONE;
99
100 if (currcons == 0)
101 currcons = fg_console;
102 else
103 currcons--;
104 if (currcons != vc->vc_num)
105 return NOTIFY_DONE;
106
107 poll->seen_last_update = false;
108 wake_up_interruptible(&poll->waitq);
109 kill_fasync(&poll->fasync, SIGIO, POLL_IN);
110 return NOTIFY_OK;
111}
112
113static void
114vcs_poll_data_free(struct vcs_poll_data *poll)
115{
116 unregister_vt_notifier(&poll->notifier);
117 kfree(poll);
118}
119
120static struct vcs_poll_data *
121vcs_poll_data_get(struct file *file)
122{
Al Viroe8cd8162013-03-26 20:30:17 -0400123 struct vcs_poll_data *poll = file->private_data, *kill = NULL;
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400124
125 if (poll)
126 return poll;
127
128 poll = kzalloc(sizeof(*poll), GFP_KERNEL);
129 if (!poll)
130 return NULL;
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400131 poll->cons_num = console(file_inode(file));
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400132 init_waitqueue_head(&poll->waitq);
133 poll->notifier.notifier_call = vcs_notifier;
134 if (register_vt_notifier(&poll->notifier) != 0) {
135 kfree(poll);
136 return NULL;
137 }
138
139 /*
140 * This code may be called either through ->poll() or ->fasync().
141 * If we have two threads using the same file descriptor, they could
142 * both enter this function, both notice that the structure hasn't
143 * been allocated yet and go ahead allocating it in parallel, but
144 * only one of them must survive and be shared otherwise we'd leak
145 * memory with a dangling notifier callback.
146 */
147 spin_lock(&file->f_lock);
148 if (!file->private_data) {
149 file->private_data = poll;
150 } else {
151 /* someone else raced ahead of us */
Al Viroe8cd8162013-03-26 20:30:17 -0400152 kill = poll;
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400153 poll = file->private_data;
154 }
155 spin_unlock(&file->f_lock);
Al Viroe8cd8162013-03-26 20:30:17 -0400156 if (kill)
157 vcs_poll_data_free(kill);
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400158
159 return poll;
160}
161
Jiri Olsafcdba072011-02-07 19:31:25 +0100162/*
163 * Returns VC for inode.
164 * Must be called with console_lock.
165 */
166static struct vc_data*
167vcs_vc(struct inode *inode, int *viewed)
168{
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400169 unsigned int currcons = console(inode);
Jiri Olsafcdba072011-02-07 19:31:25 +0100170
171 WARN_CONSOLE_UNLOCKED();
172
173 if (currcons == 0) {
174 currcons = fg_console;
175 if (viewed)
176 *viewed = 1;
177 } else {
178 currcons--;
179 if (viewed)
180 *viewed = 0;
181 }
182 return vc_cons[currcons].d;
183}
184
185/*
186 * Returns size for VC carried by inode.
187 * Must be called with console_lock.
188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static int
190vcs_size(struct inode *inode)
191{
192 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct vc_data *vc;
194
Jiri Olsafcdba072011-02-07 19:31:25 +0100195 WARN_CONSOLE_UNLOCKED();
196
197 vc = vcs_vc(inode, NULL);
198 if (!vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 size = vc->vc_rows * vc->vc_cols;
202
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400203 if (use_attributes(inode)) {
204 if (use_unicode(inode))
205 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 size = 2*size + HEADER_SIZE;
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400207 } else if (use_unicode(inode))
208 size *= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return size;
210}
211
212static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
213{
214 int size;
215
Jiri Olsadc1892c2011-02-07 19:31:24 +0100216 console_lock();
Al Viro496ad9a2013-01-23 17:07:38 -0500217 size = vcs_size(file_inode(file));
Jiri Olsadc1892c2011-02-07 19:31:24 +0100218 console_unlock();
Jiri Olsafcdba072011-02-07 19:31:25 +0100219 if (size < 0)
Jiri Olsadc1892c2011-02-07 19:31:24 +0100220 return size;
Al Viro65004272013-06-17 15:31:22 +0400221 return fixed_size_llseek(file, offset, orig, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224
225static ssize_t
226vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
227{
Al Viro496ad9a2013-01-23 17:07:38 -0500228 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct vc_data *vc;
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400230 struct vcs_poll_data *poll;
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400231 long pos, read;
232 int attr, uni_mode, row, col, maxcol, viewed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 unsigned short *org = NULL;
234 ssize_t ret;
Jiri Olsafcdba072011-02-07 19:31:25 +0100235 char *con_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jiri Olsafcdba072011-02-07 19:31:25 +0100237 con_buf = (char *) __get_free_page(GFP_KERNEL);
238 if (!con_buf)
239 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 pos = *ppos;
242
243 /* Select the proper current console and verify
244 * sanity of the situation under the console lock.
245 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800246 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400248 uni_mode = use_unicode(inode);
249 attr = use_attributes(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ret = -ENXIO;
Jiri Olsafcdba072011-02-07 19:31:25 +0100251 vc = vcs_vc(inode, &viewed);
252 if (!vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 ret = -EINVAL;
256 if (pos < 0)
257 goto unlock_out;
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400258 /* we enforce 32-bit alignment for pos and count in unicode mode */
259 if (uni_mode && (pos | count) & 3)
260 goto unlock_out;
261
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400262 poll = file->private_data;
263 if (count && poll)
264 poll->seen_last_update = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 read = 0;
266 ret = 0;
267 while (count) {
268 char *con_buf0, *con_buf_start;
269 long this_round, size;
270 ssize_t orig_count;
271 long p = pos;
272
273 /* Check whether we are above size each round,
274 * as copy_to_user at the end of this loop
275 * could sleep.
276 */
277 size = vcs_size(inode);
Jiri Olsadc1892c2011-02-07 19:31:24 +0100278 if (size < 0) {
279 if (read)
280 break;
281 ret = size;
282 goto unlock_out;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (pos >= size)
285 break;
286 if (count > size - pos)
287 count = size - pos;
288
289 this_round = count;
290 if (this_round > CON_BUF_SIZE)
291 this_round = CON_BUF_SIZE;
292
293 /* Perform the whole read into the local con_buf.
294 * Then we can drop the console spinlock and safely
295 * attempt to move it to userspace.
296 */
297
298 con_buf_start = con_buf0 = con_buf;
299 orig_count = this_round;
300 maxcol = vc->vc_cols;
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400301 if (uni_mode) {
302 unsigned int nr;
303
304 ret = vc_uniscr_check(vc);
305 if (ret)
306 break;
307 p /= 4;
308 row = p / vc->vc_cols;
309 col = p % maxcol;
310 nr = maxcol - col;
311 do {
312 if (nr > this_round/4)
313 nr = this_round/4;
Nicolas Pitre708d0bf2018-06-26 23:56:42 -0400314 vc_uniscr_copy_line(vc, con_buf0, viewed,
315 row, col, nr);
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400316 con_buf0 += nr * 4;
317 this_round -= nr * 4;
318 row++;
319 col = 0;
320 nr = maxcol;
321 } while (this_round);
322 } else if (!attr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 org = screen_pos(vc, p, viewed);
324 col = p % maxcol;
325 p += maxcol - col;
326 while (this_round-- > 0) {
327 *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
328 if (++col == maxcol) {
329 org = screen_pos(vc, p, viewed);
330 col = 0;
331 p += maxcol;
332 }
333 }
334 } else {
335 if (p < HEADER_SIZE) {
336 size_t tmp_count;
337
338 con_buf0[0] = (char)vc->vc_rows;
339 con_buf0[1] = (char)vc->vc_cols;
340 getconsxy(vc, con_buf0 + 2);
341
342 con_buf_start += p;
343 this_round += p;
344 if (this_round > CON_BUF_SIZE) {
345 this_round = CON_BUF_SIZE;
346 orig_count = this_round - p;
347 }
348
349 tmp_count = HEADER_SIZE;
350 if (tmp_count > this_round)
351 tmp_count = this_round;
352
353 /* Advance state pointers and move on. */
354 this_round -= tmp_count;
355 p = HEADER_SIZE;
356 con_buf0 = con_buf + HEADER_SIZE;
357 /* If this_round >= 0, then p is even... */
358 } else if (p & 1) {
359 /* Skip first byte for output if start address is odd
360 * Update region sizes up/down depending on free
361 * space in buffer.
362 */
363 con_buf_start++;
364 if (this_round < CON_BUF_SIZE)
365 this_round++;
366 else
367 orig_count--;
368 }
369 if (this_round > 0) {
370 unsigned short *tmp_buf = (unsigned short *)con_buf0;
371
372 p -= HEADER_SIZE;
373 p /= 2;
374 col = p % maxcol;
375
376 org = screen_pos(vc, p, viewed);
377 p += maxcol - col;
378
379 /* Buffer has even length, so we can always copy
380 * character + attribute. We do not copy last byte
381 * to userspace if this_round is odd.
382 */
383 this_round = (this_round + 1) >> 1;
384
385 while (this_round) {
386 *tmp_buf++ = vcs_scr_readw(vc, org++);
387 this_round --;
388 if (++col == maxcol) {
389 org = screen_pos(vc, p, viewed);
390 col = 0;
391 p += maxcol;
392 }
393 }
394 }
395 }
396
397 /* Finally, release the console semaphore while we push
398 * all the data to userspace from our temporary buffer.
399 *
400 * AKPM: Even though it's a semaphore, we should drop it because
401 * the pagefault handling code may want to call printk().
402 */
403
Torben Hohnac751ef2011-01-25 15:07:35 -0800404 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ret = copy_to_user(buf, con_buf_start, orig_count);
Torben Hohnac751ef2011-01-25 15:07:35 -0800406 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 if (ret) {
409 read += (orig_count - ret);
410 ret = -EFAULT;
411 break;
412 }
413 buf += orig_count;
414 pos += orig_count;
415 read += orig_count;
416 count -= orig_count;
417 }
418 *ppos += read;
419 if (read)
420 ret = read;
421unlock_out:
Torben Hohnac751ef2011-01-25 15:07:35 -0800422 console_unlock();
Jiri Olsafcdba072011-02-07 19:31:25 +0100423 free_page((unsigned long) con_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return ret;
425}
426
427static ssize_t
428vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
429{
Al Viro496ad9a2013-01-23 17:07:38 -0500430 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct vc_data *vc;
432 long pos;
Jiri Olsafcdba072011-02-07 19:31:25 +0100433 long attr, size, written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 char *con_buf0;
Jiri Olsafcdba072011-02-07 19:31:25 +0100435 int col, maxcol, viewed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 u16 *org0 = NULL, *org = NULL;
437 size_t ret;
Jiri Olsafcdba072011-02-07 19:31:25 +0100438 char *con_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Nicolas Pitre627f3b92019-11-05 10:33:16 +0100440 if (use_unicode(inode))
441 return -EOPNOTSUPP;
442
Jiri Olsafcdba072011-02-07 19:31:25 +0100443 con_buf = (char *) __get_free_page(GFP_KERNEL);
444 if (!con_buf)
445 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 pos = *ppos;
448
449 /* Select the proper current console and verify
450 * sanity of the situation under the console lock.
451 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800452 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400454 attr = use_attributes(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 ret = -ENXIO;
Jiri Olsafcdba072011-02-07 19:31:25 +0100456 vc = vcs_vc(inode, &viewed);
457 if (!vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 size = vcs_size(inode);
461 ret = -EINVAL;
462 if (pos < 0 || pos > size)
463 goto unlock_out;
464 if (count > size - pos)
465 count = size - pos;
466 written = 0;
467 while (count) {
468 long this_round = count;
469 size_t orig_count;
470 long p;
471
472 if (this_round > CON_BUF_SIZE)
473 this_round = CON_BUF_SIZE;
474
475 /* Temporarily drop the console lock so that we can read
476 * in the write data from userspace safely.
477 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800478 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 ret = copy_from_user(con_buf, buf, this_round);
Torben Hohnac751ef2011-01-25 15:07:35 -0800480 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (ret) {
483 this_round -= ret;
484 if (!this_round) {
485 /* Abort loop if no data were copied. Otherwise
486 * fail with -EFAULT.
487 */
488 if (written)
489 break;
490 ret = -EFAULT;
491 goto unlock_out;
492 }
493 }
494
495 /* The vcs_size might have changed while we slept to grab
496 * the user buffer, so recheck.
497 * Return data written up to now on failure.
498 */
499 size = vcs_size(inode);
Jiri Olsadc1892c2011-02-07 19:31:24 +0100500 if (size < 0) {
501 if (written)
502 break;
503 ret = size;
504 goto unlock_out;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (pos >= size)
507 break;
508 if (this_round > size - pos)
509 this_round = size - pos;
510
511 /* OK, now actually push the write to the console
512 * under the lock using the local kernel buffer.
513 */
514
515 con_buf0 = con_buf;
516 orig_count = this_round;
517 maxcol = vc->vc_cols;
518 p = pos;
519 if (!attr) {
520 org0 = org = screen_pos(vc, p, viewed);
521 col = p % maxcol;
522 p += maxcol - col;
523
524 while (this_round > 0) {
525 unsigned char c = *con_buf0++;
526
527 this_round--;
528 vcs_scr_writew(vc,
529 (vcs_scr_readw(vc, org) & 0xff00) | c, org);
530 org++;
531 if (++col == maxcol) {
532 org = screen_pos(vc, p, viewed);
533 col = 0;
534 p += maxcol;
535 }
536 }
537 } else {
538 if (p < HEADER_SIZE) {
539 char header[HEADER_SIZE];
540
541 getconsxy(vc, header + 2);
542 while (p < HEADER_SIZE && this_round > 0) {
543 this_round--;
544 header[p++] = *con_buf0++;
545 }
546 if (!viewed)
547 putconsxy(vc, header + 2);
548 }
549 p -= HEADER_SIZE;
550 col = (p/2) % maxcol;
551 if (this_round > 0) {
552 org0 = org = screen_pos(vc, p/2, viewed);
553 if ((p & 1) && this_round > 0) {
554 char c;
555
556 this_round--;
557 c = *con_buf0++;
558#ifdef __BIG_ENDIAN
559 vcs_scr_writew(vc, c |
560 (vcs_scr_readw(vc, org) & 0xff00), org);
561#else
562 vcs_scr_writew(vc, (c << 8) |
563 (vcs_scr_readw(vc, org) & 0xff), org);
564#endif
565 org++;
566 p++;
567 if (++col == maxcol) {
568 org = screen_pos(vc, p/2, viewed);
569 col = 0;
570 }
571 }
572 p /= 2;
573 p += maxcol - col;
574 }
575 while (this_round > 1) {
576 unsigned short w;
577
Dave Jonesee025942005-12-28 20:01:04 -0500578 w = get_unaligned(((unsigned short *)con_buf0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 vcs_scr_writew(vc, w, org++);
580 con_buf0 += 2;
581 this_round -= 2;
582 if (++col == maxcol) {
583 org = screen_pos(vc, p, viewed);
584 col = 0;
585 p += maxcol;
586 }
587 }
588 if (this_round > 0) {
589 unsigned char c;
590
591 c = *con_buf0++;
592#ifdef __BIG_ENDIAN
593 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
594#else
595 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
596#endif
597 }
598 }
599 count -= orig_count;
600 written += orig_count;
601 buf += orig_count;
602 pos += orig_count;
603 if (org0)
604 update_region(vc, (unsigned long)(org0), org - org0);
605 }
606 *ppos += written;
607 ret = written;
Nicolas Pitre432c9ed2010-10-01 00:10:44 -0400608 if (written)
609 vcs_scr_updated(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611unlock_out:
Torben Hohnac751ef2011-01-25 15:07:35 -0800612 console_unlock();
Jiri Olsafcdba072011-02-07 19:31:25 +0100613 free_page((unsigned long) con_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return ret;
615}
616
Al Viroafc9a422017-07-03 06:39:46 -0400617static __poll_t
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400618vcs_poll(struct file *file, poll_table *wait)
619{
620 struct vcs_poll_data *poll = vcs_poll_data_get(file);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800621 __poll_t ret = DEFAULT_POLLMASK|EPOLLERR|EPOLLPRI;
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400622
623 if (poll) {
624 poll_wait(file, &poll->waitq, wait);
Nicolas Pitre47c344d2010-11-10 01:33:12 -0500625 if (poll->seen_last_update)
626 ret = DEFAULT_POLLMASK;
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400627 }
628 return ret;
629}
630
631static int
632vcs_fasync(int fd, struct file *file, int on)
633{
634 struct vcs_poll_data *poll = file->private_data;
635
636 if (!poll) {
637 /* don't allocate anything if all we want is disable fasync */
638 if (!on)
639 return 0;
640 poll = vcs_poll_data_get(file);
641 if (!poll)
642 return -ENOMEM;
643 }
644
645 return fasync_helper(fd, file, on, &poll->fasync);
646}
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648static int
649vcs_open(struct inode *inode, struct file *filp)
650{
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400651 unsigned int currcons = console(inode);
652 bool attr = use_attributes(inode);
653 bool uni_mode = use_unicode(inode);
Jonathan Corbetf97259e2008-05-16 13:47:50 -0600654 int ret = 0;
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400655
656 /* we currently don't support attributes in unicode mode */
657 if (attr && uni_mode)
658 return -EOPNOTSUPP;
659
Alan Cox4001d7b2012-03-02 14:59:20 +0000660 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if(currcons && !vc_cons_allocated(currcons-1))
Jonathan Corbetf97259e2008-05-16 13:47:50 -0600662 ret = -ENXIO;
Alan Cox4001d7b2012-03-02 14:59:20 +0000663 console_unlock();
Jonathan Corbetf97259e2008-05-16 13:47:50 -0600664 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400667static int vcs_release(struct inode *inode, struct file *file)
668{
669 struct vcs_poll_data *poll = file->private_data;
670
671 if (poll)
672 vcs_poll_data_free(poll);
673 return 0;
674}
675
Arjan van de Ven62322d22006-07-03 00:24:21 -0700676static const struct file_operations vcs_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 .llseek = vcs_lseek,
678 .read = vcs_read,
679 .write = vcs_write,
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400680 .poll = vcs_poll,
681 .fasync = vcs_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 .open = vcs_open,
Nicolas Pitre47725ac2010-10-05 14:22:37 -0400683 .release = vcs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684};
685
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800686static struct class *vc_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Kay Sievers4995f8e2009-03-09 14:18:52 +0100688void vcs_make_sysfs(int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Kay Sievers4995f8e2009-03-09 14:18:52 +0100690 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL,
691 "vcs%u", index + 1);
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400692 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 65), NULL,
693 "vcsu%u", index + 1);
Kay Sievers4995f8e2009-03-09 14:18:52 +0100694 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL,
695 "vcsa%u", index + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
Alan Coxd09d7dd2006-09-29 01:59:47 -0700697
Kay Sievers4995f8e2009-03-09 14:18:52 +0100698void vcs_remove_sysfs(int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Kay Sievers4995f8e2009-03-09 14:18:52 +0100700 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1));
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400701 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 65));
Kay Sievers4995f8e2009-03-09 14:18:52 +0100702 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705int __init vcs_init(void)
706{
Kay Sieversc46a7ae2009-07-20 16:04:55 +0100707 unsigned int i;
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
710 panic("unable to get major %d for vcs device", VCS_MAJOR);
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800711 vc_class = class_create(THIS_MODULE, "vc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700713 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400714 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 64), NULL, "vcsu");
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700715 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
Kay Sieversc46a7ae2009-07-20 16:04:55 +0100716 for (i = 0; i < MIN_NR_CONSOLES; i++)
717 vcs_make_sysfs(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719}