Loading...
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | /* * linux/drivers/char/lp_old98.c * * printer port driver for ancient PC-9800s with no bidirectional port support * * Copyright (C) 1998,99 Kousuke Takai <tak@kmc.kyoto-u.ac.jp>, * Kyoto University Microcomputer Club * * This driver is based on and has compatibility with `lp.c', * generic PC printer port driver. */ #include <linux/module.h> #include <linux/init.h> #include <linux/config.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/major.h> #include <linux/sched.h> #include <linux/slab.h> #include <linux/spinlock.h> #include <linux/ioport.h> #include <linux/fcntl.h> #include <linux/delay.h> #include <linux/console.h> #include <linux/fs.h> #include <asm/io.h> #include <asm/uaccess.h> #include <asm/system.h> #include <linux/lp.h> /* * I/O port numbers */ #define LP_PORT_DATA 0x40 #define LP_PORT_STATUS (LP_PORT_DATA + 2) #define LP_PORT_STROBE (LP_PORT_DATA + 4) #define LP_PORT_CONTROL (LP_PORT_DATA + 6) #define LP_PORT_H98MODE 0x0448 #define LP_PORT_EXTMODE 0x0149 /* * bit mask for I/O */ #define LP_MASK_nBUSY (1 << 2) #define LP_MASK_nSTROBE (1 << 7) #define LP_CONTROL_ASSERT_STROBE (0x0e) #define LP_CONTROL_NEGATE_STROBE (0x0f) /* * Acceptable maximum value for non-privileged user for LPCHARS ioctl. */ #define LP_CHARS_NOPRIV_MAX 65535 #define DC1 '\x11' #define DC3 '\x13' /* PC-9800s have at least and at most one old-style printer port. */ static struct lp_struct lp = { .flags = LP_EXIST | LP_ABORTOPEN, .chars = LP_INIT_CHAR, .time = LP_INIT_TIME, .wait = LP_INIT_WAIT, }; static int dc1_check; static spinlock_t lp_old98_lock = SPIN_LOCK_UNLOCKED; #undef LP_OLD98_DEBUG #ifdef CONFIG_PC9800_OLDLP_CONSOLE static struct console lp_old98_console; /* defined later */ static short saved_console_flags; #endif static DECLARE_WAIT_QUEUE_HEAD (lp_old98_waitq); static void lp_old98_timer_function(unsigned long data) { if (inb(LP_PORT_STATUS) & LP_MASK_nBUSY) wake_up_interruptible(&lp_old98_waitq); else { struct timer_list *t = (struct timer_list *) data; t->expires = jiffies + 1; add_timer(t); } } static inline int lp_old98_wait_ready(void) { struct timer_list timer; init_timer(&timer); timer.function = lp_old98_timer_function; timer.expires = jiffies + 1; timer.data = (unsigned long)&timer; add_timer(&timer); interruptible_sleep_on(&lp_old98_waitq); del_timer(&timer); return signal_pending(current); } static inline int lp_old98_char(char lpchar) { unsigned long count = 0; #ifdef LP_STATS int tmp; #endif while (!(inb(LP_PORT_STATUS) & LP_MASK_nBUSY)) { count++; if (count >= lp.chars) return 0; } outb(lpchar, LP_PORT_DATA); #ifdef LP_STATS /* * Update lp statsistics here (and between next two outb()'s). * Time to compute it is part of storobe delay. */ if (count > lp.stats.maxwait) { #ifdef LP_OLD98_DEBUG printk(KERN_DEBUG "lp_old98: success after %d counts.\n", count); #endif lp.stats.maxwait = count; } count *= 256; tmp = count - lp.stats.meanwait; if (tmp < 0) tmp = -tmp; #endif ndelay(lp.wait); /* negate PSTB# (activate strobe) */ outb(LP_CONTROL_ASSERT_STROBE, LP_PORT_CONTROL); #ifdef LP_STATS lp.stats.meanwait = (255 * lp.stats.meanwait + count + 128) / 256; lp.stats.mdev = (127 * lp.stats.mdev + tmp + 64) / 128; lp.stats.chars ++; #endif ndelay(lp.wait); /* assert PSTB# (deactivate strobe) */ outb(LP_CONTROL_NEGATE_STROBE, LP_PORT_CONTROL); return 1; } static ssize_t lp_old98_write(struct file * file, const char * buf, size_t count, loff_t *dummy) { unsigned long total_bytes_written = 0; if (!access_ok(VERIFY_READ, buf, count)) return -EFAULT; #ifdef LP_STATS if (jiffies - lp.lastcall > lp.time) lp.runchars = 0; lp.lastcall = jiffies; #endif do { unsigned long bytes_written = 0; unsigned long copy_size = (count < LP_BUFFER_SIZE ? count : LP_BUFFER_SIZE); if (__copy_from_user(lp.lp_buffer, buf, copy_size)) return -EFAULT; while (bytes_written < copy_size) { if (lp_old98_char(lp.lp_buffer[bytes_written])) bytes_written ++; else { #ifdef LP_STATS int rc = lp.runchars + bytes_written; if (rc > lp.stats.maxrun) lp.stats.maxrun = rc; lp.stats.sleeps ++; #endif #ifdef LP_OLD98_DEBUG printk(KERN_DEBUG "lp_old98: sleeping at %d characters" " for %d jiffies\n", lp.runchars, lp.time); lp.runchars = 0; #endif if (lp_old98_wait_ready()) return ((total_bytes_written + bytes_written) ? : -EINTR); } } total_bytes_written += bytes_written; buf += bytes_written; #ifdef LP_STATS lp.runchars += bytes_written; #endif count -= bytes_written; } while (count > 0); return total_bytes_written; } static int lp_old98_open(struct inode * inode, struct file * file) { if (iminor(inode) != 0) return -ENXIO; if (lp.flags & LP_BUSY) return -EBUSY; if (dc1_check && (lp.flags & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) { /* * Check whether printer is on-line. * PC-9800's old style port have only BUSY# as status input, * so that it is impossible to distinguish that the printer is * ready and that the printer is off-line or not connected * (in both case BUSY# is in the same state). So: * * (1) output DC1 (0x11) to printer port and do strobe. * (2) watch BUSY# line for a while. If BUSY# is pulled * down, the printer will be ready. Otherwise, * it will be off-line (or not connected, or power-off, * ...). * * The source of this procedure: * Terumasa KODAKA, Kazufumi SHIMIZU, Yu HAYAMI: * `PC-9801 Super Technique', Ascii, 1992. */ int count; unsigned long flags; /* interrupts while check is fairly bad */ spin_lock_irqsave(&lp_old98_lock, flags); if (!lp_old98_char(DC1)) { spin_unlock_irqrestore(&lp_old98_lock, flags); return -EBUSY; } count = (unsigned int)dc1_check > 10000 ? 10000 : dc1_check; while (inb(LP_PORT_STATUS) & LP_MASK_nBUSY) { if (--count == 0) { spin_unlock_irqrestore(&lp_old98_lock, flags); return -ENODEV; } } spin_unlock_irqrestore(&lp_old98_lock, flags); } if ((lp.lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL)) == NULL) return -ENOMEM; lp.flags |= LP_BUSY; #ifdef CONFIG_PC9800_OLDLP_CONSOLE saved_console_flags = lp_old98_console.flags; lp_old98_console.flags &= ~CON_ENABLED; #endif return 0; } static int lp_old98_release(struct inode * inode, struct file * file) { kfree(lp.lp_buffer); lp.lp_buffer = NULL; lp.flags &= ~LP_BUSY; #ifdef CONFIG_PC9800_OLDLP_CONSOLE lp_old98_console.flags = saved_console_flags; #endif return 0; } static int lp_old98_init_device(void) { unsigned char data; if ((data = inb(LP_PORT_EXTMODE)) != 0xFF && (data & 0x10)) { printk(KERN_INFO "lp_old98: shutting down extended parallel port mode...\n"); outb(data & ~0x10, LP_PORT_EXTMODE); } #ifdef PC98_HW_H98 if ((pc98_hw_flags & PC98_HW_H98) && ((data = inb(LP_PORT_H98MODE)) & 0x01)) { printk(KERN_INFO "lp_old98: shutting down H98 full centronics mode...\n"); outb(data & ~0x01, LP_PORT_H98MODE); } #endif return 0; } static int lp_old98_ioctl(struct inode *inode, struct file *file, unsigned int command, unsigned long arg) { int retval = 0; switch (command) { case LPTIME: lp.time = arg * HZ/100; break; case LPCHAR: lp.chars = arg; break; case LPABORT: if (arg) lp.flags |= LP_ABORT; else lp.flags &= ~LP_ABORT; break; case LPABORTOPEN: if (arg) lp.flags |= LP_ABORTOPEN; else lp.flags &= ~LP_ABORTOPEN; break; case LPCAREFUL: /* do nothing */ break; case LPWAIT: lp.wait = arg; break; case LPGETIRQ: retval = put_user(0, (int *)arg); break; case LPGETSTATUS: /* * convert PC-9800's status to IBM PC's one, so that tunelp(8) * works in the same way on this driver. */ retval = put_user((inb(LP_PORT_STATUS) & LP_MASK_nBUSY) ? (LP_PBUSY | LP_PERRORP) : LP_PERRORP, (int *)arg); break; case LPRESET: retval = lp_old98_init_device(); break; #ifdef LP_STATS case LPGETSTATS: if (copy_to_user((struct lp_stats *)arg, &lp.stats, sizeof(struct lp_stats))) retval = -EFAULT; else if (suser()) memset(&lp.stats, 0, sizeof(struct lp_stats)); break; #endif case LPGETFLAGS: retval = put_user(lp.flags, (int *)arg); break; case LPSETIRQ: default: retval = -EINVAL; } return retval; } static struct file_operations lp_old98_fops = { .owner = THIS_MODULE, .write = lp_old98_write, .ioctl = lp_old98_ioctl, .open = lp_old98_open, .release = lp_old98_release, }; /* * Support for console on lp_old98 */ #ifdef CONFIG_PC9800_OLDLP_CONSOLE static inline void io_delay(void) { unsigned char dummy; /* actually not output */ asm volatile ("out%B0 %0,%1" : "=a"(dummy) : "N"(0x5f)); } static void lp_old98_console_write(struct console *console, const char *s, unsigned int count) { int i; static unsigned int timeout_run = 0; while (count) { /* wait approx 1.2 seconds */ for (i = 2000000; !(inb(LP_PORT_STATUS) & LP_MASK_nBUSY); io_delay()) if (!--i) { if (++timeout_run >= 10) /* disable forever... */ console->flags &= ~CON_ENABLED; return; } timeout_run = 0; if (*s == '\n') { outb('\r', LP_PORT_DATA); io_delay(); io_delay(); outb(LP_CONTROL_ASSERT_STROBE, LP_PORT_CONTROL); io_delay(); io_delay(); outb(LP_CONTROL_NEGATE_STROBE, LP_PORT_CONTROL); io_delay(); io_delay(); for (i = 1000000; !(inb(LP_PORT_STATUS) & LP_MASK_nBUSY); io_delay()) if (!--i) return; } outb(*s++, LP_PORT_DATA); io_delay(); io_delay(); outb(LP_CONTROL_ASSERT_STROBE, LP_PORT_CONTROL); io_delay(); io_delay(); outb(LP_CONTROL_NEGATE_STROBE, LP_PORT_CONTROL); io_delay(); io_delay(); --count; } } static struct console lp_old98_console = { .name = "lp_old98", .write = lp_old98_console_write, .flags = CON_PRINTBUFFER, .index = -1, }; #endif /* console on lp_old98 */ static int __init lp_old98_init(void) { char *errmsg = "I/O ports already occupied, giving up."; #ifdef PC98_HW_H98 if (pc98_hw_flags & PC98_HW_H98) if (!request_region(LP_PORT_H98MODE, 1, "lp_old98") goto err1; #endif if (!request_region(LP_PORT_DATA, 1, "lp_old98")) goto err2; if (!request_region(LP_PORT_STATUS, 1, "lp_old98")) goto err3; if (!request_region(LP_PORT_STROBE, 1, "lp_old98")) goto err4; if (!request_region(LP_PORT_EXTMODE, 1, "lp_old98")) goto err5; if (!register_chrdev(LP_MAJOR, "lp", &lp_old98_fops)) { #ifdef CONFIG_PC9800_OLDLP_CONSOLE register_console(&lp_old98_console); printk(KERN_INFO "lp_old98: console ready\n"); #endif /* * rest are not needed by this driver, * but for locking out other printer drivers... */ lp_old98_init_device(); return 0; } else errmsg = "unable to register device"; release_region(LP_PORT_EXTMODE, 1); err5: release_region(LP_PORT_STROBE, 1); err4: release_region(LP_PORT_STATUS, 1); err3: release_region(LP_PORT_DATA, 1); err2: #ifdef PC98_HW_H98 if (pc98_hw_flags & PC98_HW_H98) release_region(LP_PORT_H98MODE, 1); err1: #endif printk(KERN_ERR "lp_old98: %s\n", errmsg); return -EBUSY; } static void __exit lp_old98_exit(void) { #ifdef CONFIG_PC9800_OLDLP_CONSOLE unregister_console(&lp_old98_console); #endif unregister_chrdev(LP_MAJOR, "lp"); release_region(LP_PORT_DATA, 1); release_region(LP_PORT_STATUS, 1); release_region(LP_PORT_STROBE, 1); #ifdef PC98_HW_H98 if (pc98_hw_flags & PC98_HW_H98) release_region(LP_PORT_H98MODE, 1); #endif release_region(LP_PORT_EXTMODE, 1); } #ifndef MODULE static int __init lp_old98_setup(char *str) { int ints[4]; str = get_options(str, ARRAY_SIZE(ints), ints); if (ints[0] > 0) dc1_check = ints[1]; return 1; } __setup("lp_old98_dc1_check=", lp_old98_setup); #endif MODULE_PARM(dc1_check, "i"); MODULE_AUTHOR("Kousuke Takai <tak@kmc.kyoto-u.ac.jp>"); MODULE_DESCRIPTION("PC-9800 old printer port driver"); MODULE_LICENSE("GPL"); module_init(lp_old98_init); module_exit(lp_old98_exit); |