Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/vc_screen.c |
| 3 | * |
| 4 | * Provide access to virtual console memory. |
| 5 | * /dev/vcs0: the screen as it is being viewed right now (possibly scrolled) |
| 6 | * /dev/vcsN: the screen of /dev/ttyN (1 <= N <= 63) |
| 7 | * [minor: N] |
| 8 | * |
| 9 | * /dev/vcsaN: idem, but including attributes, and prefixed with |
| 10 | * the 4 bytes lines,columns,x,y (as screendump used to give). |
| 11 | * Attribute/character pair is in native endianity. |
| 12 | * [minor: N+128] |
| 13 | * |
| 14 | * This replaces screendump and part of selection, so that the system |
| 15 | * administrator can control access using file system permissions. |
| 16 | * |
| 17 | * aeb@cwi.nl - efter Friedas begravelse - 950211 |
| 18 | * |
| 19 | * machek@k332.feld.cvut.cz - modified not to send characters to wrong console |
| 20 | * - fixed some fatal off-by-one bugs (0-- no longer == -1 -> looping and looping and looping...) |
| 21 | * - making it shorter - scr_readw are macros which expand in PRETTY long code |
| 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/kernel.h> |
| 25 | #include <linux/major.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/tty.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/init.h> |
Matthias Kaehlcke | c831c33 | 2007-05-08 00:39:49 -0700 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/vt_kern.h> |
| 33 | #include <linux/selection.h> |
| 34 | #include <linux/kbd_kern.h> |
| 35 | #include <linux/console.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/device.h> |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 37 | #include <linux/sched.h> |
| 38 | #include <linux/fs.h> |
| 39 | #include <linux/poll.h> |
| 40 | #include <linux/signal.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/notifier.h> |
Matthias Kaehlcke | c831c33 | 2007-05-08 00:39:49 -0700 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/uaccess.h> |
| 45 | #include <asm/byteorder.h> |
| 46 | #include <asm/unaligned.h> |
| 47 | |
| 48 | #undef attr |
| 49 | #undef org |
| 50 | #undef addr |
| 51 | #define HEADER_SIZE 4 |
| 52 | |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 53 | struct vcs_poll_data { |
| 54 | struct notifier_block notifier; |
| 55 | unsigned int cons_num; |
| 56 | bool seen_last_update; |
| 57 | wait_queue_head_t waitq; |
| 58 | struct fasync_struct *fasync; |
| 59 | }; |
| 60 | |
| 61 | static int |
| 62 | vcs_notifier(struct notifier_block *nb, unsigned long code, void *_param) |
| 63 | { |
| 64 | struct vt_notifier_param *param = _param; |
| 65 | struct vc_data *vc = param->vc; |
| 66 | struct vcs_poll_data *poll = |
| 67 | container_of(nb, struct vcs_poll_data, notifier); |
| 68 | int currcons = poll->cons_num; |
| 69 | |
| 70 | if (code != VT_UPDATE) |
| 71 | return NOTIFY_DONE; |
| 72 | |
| 73 | if (currcons == 0) |
| 74 | currcons = fg_console; |
| 75 | else |
| 76 | currcons--; |
| 77 | if (currcons != vc->vc_num) |
| 78 | return NOTIFY_DONE; |
| 79 | |
| 80 | poll->seen_last_update = false; |
| 81 | wake_up_interruptible(&poll->waitq); |
| 82 | kill_fasync(&poll->fasync, SIGIO, POLL_IN); |
| 83 | return NOTIFY_OK; |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | vcs_poll_data_free(struct vcs_poll_data *poll) |
| 88 | { |
| 89 | unregister_vt_notifier(&poll->notifier); |
| 90 | kfree(poll); |
| 91 | } |
| 92 | |
| 93 | static struct vcs_poll_data * |
| 94 | vcs_poll_data_get(struct file *file) |
| 95 | { |
| 96 | struct vcs_poll_data *poll = file->private_data; |
| 97 | |
| 98 | if (poll) |
| 99 | return poll; |
| 100 | |
| 101 | poll = kzalloc(sizeof(*poll), GFP_KERNEL); |
| 102 | if (!poll) |
| 103 | return NULL; |
| 104 | poll->cons_num = iminor(file->f_path.dentry->d_inode) & 127; |
| 105 | init_waitqueue_head(&poll->waitq); |
| 106 | poll->notifier.notifier_call = vcs_notifier; |
| 107 | if (register_vt_notifier(&poll->notifier) != 0) { |
| 108 | kfree(poll); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * This code may be called either through ->poll() or ->fasync(). |
| 114 | * If we have two threads using the same file descriptor, they could |
| 115 | * both enter this function, both notice that the structure hasn't |
| 116 | * been allocated yet and go ahead allocating it in parallel, but |
| 117 | * only one of them must survive and be shared otherwise we'd leak |
| 118 | * memory with a dangling notifier callback. |
| 119 | */ |
| 120 | spin_lock(&file->f_lock); |
| 121 | if (!file->private_data) { |
| 122 | file->private_data = poll; |
| 123 | } else { |
| 124 | /* someone else raced ahead of us */ |
| 125 | vcs_poll_data_free(poll); |
| 126 | poll = file->private_data; |
| 127 | } |
| 128 | spin_unlock(&file->f_lock); |
| 129 | |
| 130 | return poll; |
| 131 | } |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | static int |
| 134 | vcs_size(struct inode *inode) |
| 135 | { |
| 136 | int size; |
| 137 | int minor = iminor(inode); |
| 138 | int currcons = minor & 127; |
| 139 | struct vc_data *vc; |
| 140 | |
| 141 | if (currcons == 0) |
| 142 | currcons = fg_console; |
| 143 | else |
| 144 | currcons--; |
| 145 | if (!vc_cons_allocated(currcons)) |
| 146 | return -ENXIO; |
| 147 | vc = vc_cons[currcons].d; |
| 148 | |
| 149 | size = vc->vc_rows * vc->vc_cols; |
| 150 | |
| 151 | if (minor & 128) |
| 152 | size = 2*size + HEADER_SIZE; |
| 153 | return size; |
| 154 | } |
| 155 | |
| 156 | static loff_t vcs_lseek(struct file *file, loff_t offset, int orig) |
| 157 | { |
| 158 | int size; |
| 159 | |
Matthias Kaehlcke | c831c33 | 2007-05-08 00:39:49 -0700 | [diff] [blame] | 160 | mutex_lock(&con_buf_mtx); |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 161 | size = vcs_size(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | switch (orig) { |
| 163 | default: |
Matthias Kaehlcke | c831c33 | 2007-05-08 00:39:49 -0700 | [diff] [blame] | 164 | mutex_unlock(&con_buf_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return -EINVAL; |
| 166 | case 2: |
| 167 | offset += size; |
| 168 | break; |
| 169 | case 1: |
| 170 | offset += file->f_pos; |
| 171 | case 0: |
| 172 | break; |
| 173 | } |
| 174 | if (offset < 0 || offset > size) { |
Matthias Kaehlcke | c831c33 | 2007-05-08 00:39:49 -0700 | [diff] [blame] | 175 | mutex_unlock(&con_buf_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return -EINVAL; |
| 177 | } |
| 178 | file->f_pos = offset; |
Matthias Kaehlcke | c831c33 | 2007-05-08 00:39:49 -0700 | [diff] [blame] | 179 | mutex_unlock(&con_buf_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return file->f_pos; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | static ssize_t |
| 185 | vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 186 | { |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 187 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | unsigned int currcons = iminor(inode); |
| 189 | struct vc_data *vc; |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 190 | struct vcs_poll_data *poll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | long pos; |
| 192 | long viewed, attr, read; |
| 193 | int col, maxcol; |
| 194 | unsigned short *org = NULL; |
| 195 | ssize_t ret; |
| 196 | |
Matthias Kaehlcke | c831c33 | 2007-05-08 00:39:49 -0700 | [diff] [blame] | 197 | mutex_lock(&con_buf_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | pos = *ppos; |
| 200 | |
| 201 | /* Select the proper current console and verify |
| 202 | * sanity of the situation under the console lock. |
| 203 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 204 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | attr = (currcons & 128); |
| 207 | currcons = (currcons & 127); |
| 208 | if (currcons == 0) { |
| 209 | currcons = fg_console; |
| 210 | viewed = 1; |
| 211 | } else { |
| 212 | currcons--; |
| 213 | viewed = 0; |
| 214 | } |
| 215 | ret = -ENXIO; |
| 216 | if (!vc_cons_allocated(currcons)) |
| 217 | goto unlock_out; |
| 218 | vc = vc_cons[currcons].d; |
| 219 | |
| 220 | ret = -EINVAL; |
| 221 | if (pos < 0) |
| 222 | goto unlock_out; |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 223 | poll = file->private_data; |
| 224 | if (count && poll) |
| 225 | poll->seen_last_update = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | read = 0; |
| 227 | ret = 0; |
| 228 | while (count) { |
| 229 | char *con_buf0, *con_buf_start; |
| 230 | long this_round, size; |
| 231 | ssize_t orig_count; |
| 232 | long p = pos; |
| 233 | |
| 234 | /* Check whether we are above size each round, |
| 235 | * as copy_to_user at the end of this loop |
| 236 | * could sleep. |
| 237 | */ |
| 238 | size = vcs_size(inode); |
| 239 | if (pos >= size) |
| 240 | break; |
| 241 | if (count > size - pos) |
| 242 | count = size - pos; |
| 243 | |
| 244 | this_round = count; |
| 245 | if (this_round > CON_BUF_SIZE) |
| 246 | this_round = CON_BUF_SIZE; |
| 247 | |
| 248 | /* Perform the whole read into the local con_buf. |
| 249 | * Then we can drop the console spinlock and safely |
| 250 | * attempt to move it to userspace. |
| 251 | */ |
| 252 | |
| 253 | con_buf_start = con_buf0 = con_buf; |
| 254 | orig_count = this_round; |
| 255 | maxcol = vc->vc_cols; |
| 256 | if (!attr) { |
| 257 | org = screen_pos(vc, p, viewed); |
| 258 | col = p % maxcol; |
| 259 | p += maxcol - col; |
| 260 | while (this_round-- > 0) { |
| 261 | *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff); |
| 262 | if (++col == maxcol) { |
| 263 | org = screen_pos(vc, p, viewed); |
| 264 | col = 0; |
| 265 | p += maxcol; |
| 266 | } |
| 267 | } |
| 268 | } else { |
| 269 | if (p < HEADER_SIZE) { |
| 270 | size_t tmp_count; |
| 271 | |
| 272 | con_buf0[0] = (char)vc->vc_rows; |
| 273 | con_buf0[1] = (char)vc->vc_cols; |
| 274 | getconsxy(vc, con_buf0 + 2); |
| 275 | |
| 276 | con_buf_start += p; |
| 277 | this_round += p; |
| 278 | if (this_round > CON_BUF_SIZE) { |
| 279 | this_round = CON_BUF_SIZE; |
| 280 | orig_count = this_round - p; |
| 281 | } |
| 282 | |
| 283 | tmp_count = HEADER_SIZE; |
| 284 | if (tmp_count > this_round) |
| 285 | tmp_count = this_round; |
| 286 | |
| 287 | /* Advance state pointers and move on. */ |
| 288 | this_round -= tmp_count; |
| 289 | p = HEADER_SIZE; |
| 290 | con_buf0 = con_buf + HEADER_SIZE; |
| 291 | /* If this_round >= 0, then p is even... */ |
| 292 | } else if (p & 1) { |
| 293 | /* Skip first byte for output if start address is odd |
| 294 | * Update region sizes up/down depending on free |
| 295 | * space in buffer. |
| 296 | */ |
| 297 | con_buf_start++; |
| 298 | if (this_round < CON_BUF_SIZE) |
| 299 | this_round++; |
| 300 | else |
| 301 | orig_count--; |
| 302 | } |
| 303 | if (this_round > 0) { |
| 304 | unsigned short *tmp_buf = (unsigned short *)con_buf0; |
| 305 | |
| 306 | p -= HEADER_SIZE; |
| 307 | p /= 2; |
| 308 | col = p % maxcol; |
| 309 | |
| 310 | org = screen_pos(vc, p, viewed); |
| 311 | p += maxcol - col; |
| 312 | |
| 313 | /* Buffer has even length, so we can always copy |
| 314 | * character + attribute. We do not copy last byte |
| 315 | * to userspace if this_round is odd. |
| 316 | */ |
| 317 | this_round = (this_round + 1) >> 1; |
| 318 | |
| 319 | while (this_round) { |
| 320 | *tmp_buf++ = vcs_scr_readw(vc, org++); |
| 321 | this_round --; |
| 322 | if (++col == maxcol) { |
| 323 | org = screen_pos(vc, p, viewed); |
| 324 | col = 0; |
| 325 | p += maxcol; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* Finally, release the console semaphore while we push |
| 332 | * all the data to userspace from our temporary buffer. |
| 333 | * |
| 334 | * AKPM: Even though it's a semaphore, we should drop it because |
| 335 | * the pagefault handling code may want to call printk(). |
| 336 | */ |
| 337 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 338 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | ret = copy_to_user(buf, con_buf_start, orig_count); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 340 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
| 342 | if (ret) { |
| 343 | read += (orig_count - ret); |
| 344 | ret = -EFAULT; |
| 345 | break; |
| 346 | } |
| 347 | buf += orig_count; |
| 348 | pos += orig_count; |
| 349 | read += orig_count; |
| 350 | count -= orig_count; |
| 351 | } |
| 352 | *ppos += read; |
| 353 | if (read) |
| 354 | ret = read; |
| 355 | unlock_out: |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 356 | console_unlock(); |
Matthias Kaehlcke | c831c33 | 2007-05-08 00:39:49 -0700 | [diff] [blame] | 357 | mutex_unlock(&con_buf_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | static ssize_t |
| 362 | vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 363 | { |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 364 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | unsigned int currcons = iminor(inode); |
| 366 | struct vc_data *vc; |
| 367 | long pos; |
| 368 | long viewed, attr, size, written; |
| 369 | char *con_buf0; |
| 370 | int col, maxcol; |
| 371 | u16 *org0 = NULL, *org = NULL; |
| 372 | size_t ret; |
| 373 | |
Matthias Kaehlcke | c831c33 | 2007-05-08 00:39:49 -0700 | [diff] [blame] | 374 | mutex_lock(&con_buf_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | pos = *ppos; |
| 377 | |
| 378 | /* Select the proper current console and verify |
| 379 | * sanity of the situation under the console lock. |
| 380 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 381 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
| 383 | attr = (currcons & 128); |
| 384 | currcons = (currcons & 127); |
| 385 | |
| 386 | if (currcons == 0) { |
| 387 | currcons = fg_console; |
| 388 | viewed = 1; |
| 389 | } else { |
| 390 | currcons--; |
| 391 | viewed = 0; |
| 392 | } |
| 393 | ret = -ENXIO; |
| 394 | if (!vc_cons_allocated(currcons)) |
| 395 | goto unlock_out; |
| 396 | vc = vc_cons[currcons].d; |
| 397 | |
| 398 | size = vcs_size(inode); |
| 399 | ret = -EINVAL; |
| 400 | if (pos < 0 || pos > size) |
| 401 | goto unlock_out; |
| 402 | if (count > size - pos) |
| 403 | count = size - pos; |
| 404 | written = 0; |
| 405 | while (count) { |
| 406 | long this_round = count; |
| 407 | size_t orig_count; |
| 408 | long p; |
| 409 | |
| 410 | if (this_round > CON_BUF_SIZE) |
| 411 | this_round = CON_BUF_SIZE; |
| 412 | |
| 413 | /* Temporarily drop the console lock so that we can read |
| 414 | * in the write data from userspace safely. |
| 415 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 416 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | ret = copy_from_user(con_buf, buf, this_round); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 418 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | if (ret) { |
| 421 | this_round -= ret; |
| 422 | if (!this_round) { |
| 423 | /* Abort loop if no data were copied. Otherwise |
| 424 | * fail with -EFAULT. |
| 425 | */ |
| 426 | if (written) |
| 427 | break; |
| 428 | ret = -EFAULT; |
| 429 | goto unlock_out; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* The vcs_size might have changed while we slept to grab |
| 434 | * the user buffer, so recheck. |
| 435 | * Return data written up to now on failure. |
| 436 | */ |
| 437 | size = vcs_size(inode); |
| 438 | if (pos >= size) |
| 439 | break; |
| 440 | if (this_round > size - pos) |
| 441 | this_round = size - pos; |
| 442 | |
| 443 | /* OK, now actually push the write to the console |
| 444 | * under the lock using the local kernel buffer. |
| 445 | */ |
| 446 | |
| 447 | con_buf0 = con_buf; |
| 448 | orig_count = this_round; |
| 449 | maxcol = vc->vc_cols; |
| 450 | p = pos; |
| 451 | if (!attr) { |
| 452 | org0 = org = screen_pos(vc, p, viewed); |
| 453 | col = p % maxcol; |
| 454 | p += maxcol - col; |
| 455 | |
| 456 | while (this_round > 0) { |
| 457 | unsigned char c = *con_buf0++; |
| 458 | |
| 459 | this_round--; |
| 460 | vcs_scr_writew(vc, |
| 461 | (vcs_scr_readw(vc, org) & 0xff00) | c, org); |
| 462 | org++; |
| 463 | if (++col == maxcol) { |
| 464 | org = screen_pos(vc, p, viewed); |
| 465 | col = 0; |
| 466 | p += maxcol; |
| 467 | } |
| 468 | } |
| 469 | } else { |
| 470 | if (p < HEADER_SIZE) { |
| 471 | char header[HEADER_SIZE]; |
| 472 | |
| 473 | getconsxy(vc, header + 2); |
| 474 | while (p < HEADER_SIZE && this_round > 0) { |
| 475 | this_round--; |
| 476 | header[p++] = *con_buf0++; |
| 477 | } |
| 478 | if (!viewed) |
| 479 | putconsxy(vc, header + 2); |
| 480 | } |
| 481 | p -= HEADER_SIZE; |
| 482 | col = (p/2) % maxcol; |
| 483 | if (this_round > 0) { |
| 484 | org0 = org = screen_pos(vc, p/2, viewed); |
| 485 | if ((p & 1) && this_round > 0) { |
| 486 | char c; |
| 487 | |
| 488 | this_round--; |
| 489 | c = *con_buf0++; |
| 490 | #ifdef __BIG_ENDIAN |
| 491 | vcs_scr_writew(vc, c | |
| 492 | (vcs_scr_readw(vc, org) & 0xff00), org); |
| 493 | #else |
| 494 | vcs_scr_writew(vc, (c << 8) | |
| 495 | (vcs_scr_readw(vc, org) & 0xff), org); |
| 496 | #endif |
| 497 | org++; |
| 498 | p++; |
| 499 | if (++col == maxcol) { |
| 500 | org = screen_pos(vc, p/2, viewed); |
| 501 | col = 0; |
| 502 | } |
| 503 | } |
| 504 | p /= 2; |
| 505 | p += maxcol - col; |
| 506 | } |
| 507 | while (this_round > 1) { |
| 508 | unsigned short w; |
| 509 | |
Dave Jones | ee02594 | 2005-12-28 20:01:04 -0500 | [diff] [blame] | 510 | w = get_unaligned(((unsigned short *)con_buf0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | vcs_scr_writew(vc, w, org++); |
| 512 | con_buf0 += 2; |
| 513 | this_round -= 2; |
| 514 | if (++col == maxcol) { |
| 515 | org = screen_pos(vc, p, viewed); |
| 516 | col = 0; |
| 517 | p += maxcol; |
| 518 | } |
| 519 | } |
| 520 | if (this_round > 0) { |
| 521 | unsigned char c; |
| 522 | |
| 523 | c = *con_buf0++; |
| 524 | #ifdef __BIG_ENDIAN |
| 525 | vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org); |
| 526 | #else |
| 527 | vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org); |
| 528 | #endif |
| 529 | } |
| 530 | } |
| 531 | count -= orig_count; |
| 532 | written += orig_count; |
| 533 | buf += orig_count; |
| 534 | pos += orig_count; |
| 535 | if (org0) |
| 536 | update_region(vc, (unsigned long)(org0), org - org0); |
| 537 | } |
| 538 | *ppos += written; |
| 539 | ret = written; |
Nicolas Pitre | 432c9ed | 2010-10-01 00:10:44 -0400 | [diff] [blame] | 540 | if (written) |
| 541 | vcs_scr_updated(vc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
| 543 | unlock_out: |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 544 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Matthias Kaehlcke | c831c33 | 2007-05-08 00:39:49 -0700 | [diff] [blame] | 546 | mutex_unlock(&con_buf_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | return ret; |
| 549 | } |
| 550 | |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 551 | static unsigned int |
| 552 | vcs_poll(struct file *file, poll_table *wait) |
| 553 | { |
| 554 | struct vcs_poll_data *poll = vcs_poll_data_get(file); |
Nicolas Pitre | 47c344d | 2010-11-10 01:33:12 -0500 | [diff] [blame] | 555 | int ret = DEFAULT_POLLMASK|POLLERR|POLLPRI; |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 556 | |
| 557 | if (poll) { |
| 558 | poll_wait(file, &poll->waitq, wait); |
Nicolas Pitre | 47c344d | 2010-11-10 01:33:12 -0500 | [diff] [blame] | 559 | if (poll->seen_last_update) |
| 560 | ret = DEFAULT_POLLMASK; |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 561 | } |
| 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | static int |
| 566 | vcs_fasync(int fd, struct file *file, int on) |
| 567 | { |
| 568 | struct vcs_poll_data *poll = file->private_data; |
| 569 | |
| 570 | if (!poll) { |
| 571 | /* don't allocate anything if all we want is disable fasync */ |
| 572 | if (!on) |
| 573 | return 0; |
| 574 | poll = vcs_poll_data_get(file); |
| 575 | if (!poll) |
| 576 | return -ENOMEM; |
| 577 | } |
| 578 | |
| 579 | return fasync_helper(fd, file, on, &poll->fasync); |
| 580 | } |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | static int |
| 583 | vcs_open(struct inode *inode, struct file *filp) |
| 584 | { |
| 585 | unsigned int currcons = iminor(inode) & 127; |
Jonathan Corbet | f97259e | 2008-05-16 13:47:50 -0600 | [diff] [blame] | 586 | int ret = 0; |
| 587 | |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 588 | tty_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | if(currcons && !vc_cons_allocated(currcons-1)) |
Jonathan Corbet | f97259e | 2008-05-16 13:47:50 -0600 | [diff] [blame] | 590 | ret = -ENXIO; |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 591 | tty_unlock(); |
Jonathan Corbet | f97259e | 2008-05-16 13:47:50 -0600 | [diff] [blame] | 592 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 595 | static int vcs_release(struct inode *inode, struct file *file) |
| 596 | { |
| 597 | struct vcs_poll_data *poll = file->private_data; |
| 598 | |
| 599 | if (poll) |
| 600 | vcs_poll_data_free(poll); |
| 601 | return 0; |
| 602 | } |
| 603 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 604 | static const struct file_operations vcs_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | .llseek = vcs_lseek, |
| 606 | .read = vcs_read, |
| 607 | .write = vcs_write, |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 608 | .poll = vcs_poll, |
| 609 | .fasync = vcs_fasync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | .open = vcs_open, |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 611 | .release = vcs_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | }; |
| 613 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 614 | static struct class *vc_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
Kay Sievers | 4995f8e | 2009-03-09 14:18:52 +0100 | [diff] [blame] | 616 | void vcs_make_sysfs(int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
Kay Sievers | 4995f8e | 2009-03-09 14:18:52 +0100 | [diff] [blame] | 618 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL, |
| 619 | "vcs%u", index + 1); |
| 620 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL, |
| 621 | "vcsa%u", index + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
Alan Cox | d09d7dd | 2006-09-29 01:59:47 -0700 | [diff] [blame] | 623 | |
Kay Sievers | 4995f8e | 2009-03-09 14:18:52 +0100 | [diff] [blame] | 624 | void vcs_remove_sysfs(int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | { |
Kay Sievers | 4995f8e | 2009-03-09 14:18:52 +0100 | [diff] [blame] | 626 | device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1)); |
| 627 | device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | int __init vcs_init(void) |
| 631 | { |
Kay Sievers | c46a7ae | 2009-07-20 16:04:55 +0100 | [diff] [blame] | 632 | unsigned int i; |
| 633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops)) |
| 635 | panic("unable to get major %d for vcs device", VCS_MAJOR); |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 636 | vc_class = class_create(THIS_MODULE, "vc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Greg Kroah-Hartman | 03457cd | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 638 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs"); |
| 639 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa"); |
Kay Sievers | c46a7ae | 2009-07-20 16:04:55 +0100 | [diff] [blame] | 640 | for (i = 0; i < MIN_NR_CONSOLES; i++) |
| 641 | vcs_make_sysfs(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | return 0; |
| 643 | } |