1 /* 2 * Generic parallel printer driver 3 * 4 * Copyright (C) 1992 by Jim Weigand and Linus Torvalds 5 * Copyright (C) 1992,1993 by Michael K. Johnson 6 * - Thanks much to Gunter Windau for pointing out to me where the error 7 * checking ought to be. 8 * Copyright (C) 1993 by Nigel Gamble (added interrupt code) 9 * Copyright (C) 1994 by Alan Cox (Modularised it) 10 * LPCAREFUL, LPABORT, LPGETSTATUS added by Chris Metcalf, metcalf@lcs.mit.edu 11 * Statistics and support for slow printers by Rob Janssen, rob@knoware.nl 12 * "lp=" command line parameters added by Grant Guenther, grant@torque.net 13 * lp_read (Status readback) support added by Carsten Gross, 14 * carsten@sol.wohnheim.uni-ulm.de 15 * Support for parport by Philip Blundell <philb@gnu.org> 16 * Parport sharing hacking by Andrea Arcangeli 17 * Fixed kernel_(to/from)_user memory copy to check for errors 18 * by Riccardo Facchetti <fizban@tin.it> 19 * 22-JAN-1998 Added support for devfs Richard Gooch <rgooch@atnf.csiro.au> 20 * Redesigned interrupt handling for handle printers with buggy handshake 21 * by Andrea Arcangeli, 11 May 1998 22 * Full efficient handling of printer with buggy irq handshake (now I have 23 * understood the meaning of the strange handshake). This is done sending new 24 * characters if the interrupt is just happened, even if the printer say to 25 * be still BUSY. This is needed at least with Epson Stylus Color. To enable 26 * the new TRUST_IRQ mode read the `LP OPTIMIZATION' section below... 27 * Fixed the irq on the rising edge of the strobe case. 28 * Obsoleted the CAREFUL flag since a printer that doesn' t work with 29 * CAREFUL will block a bit after in lp_check_status(). 30 * Andrea Arcangeli, 15 Oct 1998 31 * Obsoleted and removed all the lowlevel stuff implemented in the last 32 * month to use the IEEE1284 functions (that handle the _new_ compatibilty 33 * mode fine). 34 */ 35 36 /* This driver should, in theory, work with any parallel port that has an 37 * appropriate low-level driver; all I/O is done through the parport 38 * abstraction layer. 39 * 40 * If this driver is built into the kernel, you can configure it using the 41 * kernel command-line. For example: 42 * 43 * lp=parport1,none,parport2 (bind lp0 to parport1, disable lp1 and 44 * bind lp2 to parport2) 45 * 46 * lp=auto (assign lp devices to all ports that 47 * have printers attached, as determined 48 * by the IEEE-1284 autoprobe) 49 * 50 * lp=reset (reset the printer during 51 * initialisation) 52 * 53 * lp=off (disable the printer driver entirely) 54 * 55 * If the driver is loaded as a module, similar functionality is available 56 * using module parameters. The equivalent of the above commands would be: 57 * 58 * # insmod lp.o parport=1,none,2 59 * 60 * # insmod lp.o parport=auto 61 * 62 * # insmod lp.o reset=1 63 */ 64 65 /* COMPATIBILITY WITH OLD KERNELS 66 * 67 * Under Linux 2.0 and previous versions, lp devices were bound to ports at 68 * particular I/O addresses, as follows: 69 * 70 * lp0 0x3bc 71 * lp1 0x378 72 * lp2 0x278 73 * 74 * The new driver, by default, binds lp devices to parport devices as it 75 * finds them. This means that if you only have one port, it will be bound 76 * to lp0 regardless of its I/O address. If you need the old behaviour, you 77 * can force it using the parameters described above. 78 */ 79 80 /* 81 * The new interrupt handling code take care of the buggy handshake 82 * of some HP and Epson printer: 83 * ___ 84 * ACK _______________ ___________ 85 * |__| 86 * ____ 87 * BUSY _________ _______ 88 * |____________| 89 * 90 * I discovered this using the printer scanner that you can find at: 91 * 92 * ftp://e-mind.com/pub/linux/pscan/ 93 * 94 * 11 May 98, Andrea Arcangeli 95 * 96 * My printer scanner run on an Epson Stylus Color show that such printer 97 * generates the irq on the _rising_ edge of the STROBE. Now lp handle 98 * this case fine too. 99 * 100 * 15 Oct 1998, Andrea Arcangeli 101 * 102 * The so called `buggy' handshake is really the well documented 103 * compatibility mode IEEE1284 handshake. They changed the well known 104 * Centronics handshake acking in the middle of busy expecting to not 105 * break drivers or legacy application, while they broken linux lp 106 * until I fixed it reverse engineering the protocol by hand some 107 * month ago... 108 * 109 * 14 Dec 1998, Andrea Arcangeli 110 * 111 * Copyright (C) 2000 by Tim Waugh (added LPSETTIMEOUT ioctl) 112 */ 113 114 #include <linux/module.h> 115 #include <linux/init.h> 116 117 #include <linux/errno.h> 118 #include <linux/kernel.h> 119 #include <linux/major.h> 120 #include <linux/sched/signal.h> 121 #include <linux/slab.h> 122 #include <linux/fcntl.h> 123 #include <linux/delay.h> 124 #include <linux/poll.h> 125 #include <linux/console.h> 126 #include <linux/device.h> 127 #include <linux/wait.h> 128 #include <linux/jiffies.h> 129 #include <linux/mutex.h> 130 #include <linux/compat.h> 131 132 #include <linux/parport.h> 133 #undef LP_STATS 134 #include <linux/lp.h> 135 136 #include <asm/irq.h> 137 #include <linux/uaccess.h> 138 139 /* if you have more than 8 printers, remember to increase LP_NO */ 140 #define LP_NO 8 141 142 static DEFINE_MUTEX(lp_mutex); 143 static struct lp_struct lp_table[LP_NO]; 144 145 static unsigned int lp_count = 0; 146 static struct class *lp_class; 147 148 #ifdef CONFIG_LP_CONSOLE 149 static struct parport *console_registered; 150 #endif /* CONFIG_LP_CONSOLE */ 151 152 #undef LP_DEBUG 153 154 /* Bits used to manage claiming the parport device */ 155 #define LP_PREEMPT_REQUEST 1 156 #define LP_PARPORT_CLAIMED 2 157 158 /* --- low-level port access ----------------------------------- */ 159 160 #define r_dtr(x) (parport_read_data(lp_table[(x)].dev->port)) 161 #define r_str(x) (parport_read_status(lp_table[(x)].dev->port)) 162 #define w_ctr(x,y) do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0) 163 #define w_dtr(x,y) do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0) 164 165 /* Claim the parport or block trying unless we've already claimed it */ 166 static void lp_claim_parport_or_block(struct lp_struct *this_lp) 167 { 168 if (!test_and_set_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) { 169 parport_claim_or_block(this_lp->dev); 170 } 171 } 172 173 /* Claim the parport or block trying unless we've already claimed it */ 174 static void lp_release_parport(struct lp_struct *this_lp) 175 { 176 if (test_and_clear_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) { 177 parport_release(this_lp->dev); 178 } 179 } 180 181 182 183 static int lp_preempt(void *handle) 184 { 185 struct lp_struct *this_lp = (struct lp_struct *)handle; 186 set_bit(LP_PREEMPT_REQUEST, &this_lp->bits); 187 return 1; 188 } 189 190 191 /* 192 * Try to negotiate to a new mode; if unsuccessful negotiate to 193 * compatibility mode. Return the mode we ended up in. 194 */ 195 static int lp_negotiate(struct parport *port, int mode) 196 { 197 if (parport_negotiate(port, mode) != 0) { 198 mode = IEEE1284_MODE_COMPAT; 199 parport_negotiate(port, mode); 200 } 201 202 return mode; 203 } 204 205 static int lp_reset(int minor) 206 { 207 int retval; 208 lp_claim_parport_or_block(&lp_table[minor]); 209 w_ctr(minor, LP_PSELECP); 210 udelay(LP_DELAY); 211 w_ctr(minor, LP_PSELECP | LP_PINITP); 212 retval = r_str(minor); 213 lp_release_parport(&lp_table[minor]); 214 return retval; 215 } 216 217 static void lp_error(int minor) 218 { 219 DEFINE_WAIT(wait); 220 int polling; 221 222 if (LP_F(minor) & LP_ABORT) 223 return; 224 225 polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE; 226 if (polling) 227 lp_release_parport(&lp_table[minor]); 228 prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE); 229 schedule_timeout(LP_TIMEOUT_POLLED); 230 finish_wait(&lp_table[minor].waitq, &wait); 231 if (polling) 232 lp_claim_parport_or_block(&lp_table[minor]); 233 else 234 parport_yield_blocking(lp_table[minor].dev); 235 } 236 237 static int lp_check_status(int minor) 238 { 239 int error = 0; 240 unsigned int last = lp_table[minor].last_error; 241 unsigned char status = r_str(minor); 242 if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL)) 243 /* No error. */ 244 last = 0; 245 else if ((status & LP_POUTPA)) { 246 if (last != LP_POUTPA) { 247 last = LP_POUTPA; 248 printk(KERN_INFO "lp%d out of paper\n", minor); 249 } 250 error = -ENOSPC; 251 } else if (!(status & LP_PSELECD)) { 252 if (last != LP_PSELECD) { 253 last = LP_PSELECD; 254 printk(KERN_INFO "lp%d off-line\n", minor); 255 } 256 error = -EIO; 257 } else if (!(status & LP_PERRORP)) { 258 if (last != LP_PERRORP) { 259 last = LP_PERRORP; 260 printk(KERN_INFO "lp%d on fire\n", minor); 261 } 262 error = -EIO; 263 } else { 264 last = 0; /* Come here if LP_CAREFUL is set and no 265 errors are reported. */ 266 } 267 268 lp_table[minor].last_error = last; 269 270 if (last != 0) 271 lp_error(minor); 272 273 return error; 274 } 275 276 static int lp_wait_ready(int minor, int nonblock) 277 { 278 int error = 0; 279 280 /* If we're not in compatibility mode, we're ready now! */ 281 if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) { 282 return 0; 283 } 284 285 do { 286 error = lp_check_status(minor); 287 if (error && (nonblock || (LP_F(minor) & LP_ABORT))) 288 break; 289 if (signal_pending(current)) { 290 error = -EINTR; 291 break; 292 } 293 } while (error); 294 return error; 295 } 296 297 static ssize_t lp_write(struct file *file, const char __user *buf, 298 size_t count, loff_t *ppos) 299 { 300 unsigned int minor = iminor(file_inode(file)); 301 struct parport *port = lp_table[minor].dev->port; 302 char *kbuf = lp_table[minor].lp_buffer; 303 ssize_t retv = 0; 304 ssize_t written; 305 size_t copy_size = count; 306 int nonblock = ((file->f_flags & O_NONBLOCK) || 307 (LP_F(minor) & LP_ABORT)); 308 309 #ifdef LP_STATS 310 if (time_after(jiffies, lp_table[minor].lastcall + LP_TIME(minor))) 311 lp_table[minor].runchars = 0; 312 313 lp_table[minor].lastcall = jiffies; 314 #endif 315 316 /* Need to copy the data from user-space. */ 317 if (copy_size > LP_BUFFER_SIZE) 318 copy_size = LP_BUFFER_SIZE; 319 320 if (mutex_lock_interruptible(&lp_table[minor].port_mutex)) 321 return -EINTR; 322 323 if (copy_from_user(kbuf, buf, copy_size)) { 324 retv = -EFAULT; 325 goto out_unlock; 326 } 327 328 /* Claim Parport or sleep until it becomes available 329 */ 330 lp_claim_parport_or_block(&lp_table[minor]); 331 /* Go to the proper mode. */ 332 lp_table[minor].current_mode = lp_negotiate(port, 333 lp_table[minor].best_mode); 334 335 parport_set_timeout(lp_table[minor].dev, 336 (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK 337 : lp_table[minor].timeout)); 338 339 if ((retv = lp_wait_ready(minor, nonblock)) == 0) 340 do { 341 /* Write the data. */ 342 written = parport_write(port, kbuf, copy_size); 343 if (written > 0) { 344 copy_size -= written; 345 count -= written; 346 buf += written; 347 retv += written; 348 } 349 350 if (signal_pending(current)) { 351 if (retv == 0) 352 retv = -EINTR; 353 354 break; 355 } 356 357 if (copy_size > 0) { 358 /* incomplete write -> check error ! */ 359 int error; 360 361 parport_negotiate(lp_table[minor].dev->port, 362 IEEE1284_MODE_COMPAT); 363 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT; 364 365 error = lp_wait_ready(minor, nonblock); 366 367 if (error) { 368 if (retv == 0) 369 retv = error; 370 break; 371 } else if (nonblock) { 372 if (retv == 0) 373 retv = -EAGAIN; 374 break; 375 } 376 377 parport_yield_blocking(lp_table[minor].dev); 378 lp_table[minor].current_mode 379 = lp_negotiate(port, 380 lp_table[minor].best_mode); 381 382 } else if (need_resched()) 383 schedule(); 384 385 if (count) { 386 copy_size = count; 387 if (copy_size > LP_BUFFER_SIZE) 388 copy_size = LP_BUFFER_SIZE; 389 390 if (copy_from_user(kbuf, buf, copy_size)) { 391 if (retv == 0) 392 retv = -EFAULT; 393 break; 394 } 395 } 396 } while (count > 0); 397 398 if (test_and_clear_bit(LP_PREEMPT_REQUEST, 399 &lp_table[minor].bits)) { 400 printk(KERN_INFO "lp%d releasing parport\n", minor); 401 parport_negotiate(lp_table[minor].dev->port, 402 IEEE1284_MODE_COMPAT); 403 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT; 404 lp_release_parport(&lp_table[minor]); 405 } 406 out_unlock: 407 mutex_unlock(&lp_table[minor].port_mutex); 408 409 return retv; 410 } 411 412 #ifdef CONFIG_PARPORT_1284 413 414 /* Status readback conforming to ieee1284 */ 415 static ssize_t lp_read(struct file *file, char __user *buf, 416 size_t count, loff_t *ppos) 417 { 418 DEFINE_WAIT(wait); 419 unsigned int minor=iminor(file_inode(file)); 420 struct parport *port = lp_table[minor].dev->port; 421 ssize_t retval = 0; 422 char *kbuf = lp_table[minor].lp_buffer; 423 int nonblock = ((file->f_flags & O_NONBLOCK) || 424 (LP_F(minor) & LP_ABORT)); 425 426 if (count > LP_BUFFER_SIZE) 427 count = LP_BUFFER_SIZE; 428 429 if (mutex_lock_interruptible(&lp_table[minor].port_mutex)) 430 return -EINTR; 431 432 lp_claim_parport_or_block(&lp_table[minor]); 433 434 parport_set_timeout(lp_table[minor].dev, 435 (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK 436 : lp_table[minor].timeout)); 437 438 parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT); 439 if (parport_negotiate(lp_table[minor].dev->port, 440 IEEE1284_MODE_NIBBLE)) { 441 retval = -EIO; 442 goto out; 443 } 444 445 while (retval == 0) { 446 retval = parport_read(port, kbuf, count); 447 448 if (retval > 0) 449 break; 450 451 if (nonblock) { 452 retval = -EAGAIN; 453 break; 454 } 455 456 /* Wait for data. */ 457 458 if (lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE) { 459 parport_negotiate(lp_table[minor].dev->port, 460 IEEE1284_MODE_COMPAT); 461 lp_error(minor); 462 if (parport_negotiate(lp_table[minor].dev->port, 463 IEEE1284_MODE_NIBBLE)) { 464 retval = -EIO; 465 goto out; 466 } 467 } else { 468 prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE); 469 schedule_timeout(LP_TIMEOUT_POLLED); 470 finish_wait(&lp_table[minor].waitq, &wait); 471 } 472 473 if (signal_pending(current)) { 474 retval = -ERESTARTSYS; 475 break; 476 } 477 478 cond_resched(); 479 } 480 parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT); 481 out: 482 lp_release_parport(&lp_table[minor]); 483 484 if (retval > 0 && copy_to_user(buf, kbuf, retval)) 485 retval = -EFAULT; 486 487 mutex_unlock(&lp_table[minor].port_mutex); 488 489 return retval; 490 } 491 492 #endif /* IEEE 1284 support */ 493 494 static int lp_open(struct inode *inode, struct file *file) 495 { 496 unsigned int minor = iminor(inode); 497 int ret = 0; 498 499 mutex_lock(&lp_mutex); 500 if (minor >= LP_NO) { 501 ret = -ENXIO; 502 goto out; 503 } 504 if ((LP_F(minor) & LP_EXIST) == 0) { 505 ret = -ENXIO; 506 goto out; 507 } 508 if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor))) { 509 ret = -EBUSY; 510 goto out; 511 } 512 /* If ABORTOPEN is set and the printer is offline or out of paper, 513 we may still want to open it to perform ioctl()s. Therefore we 514 have commandeered O_NONBLOCK, even though it is being used in 515 a non-standard manner. This is strictly a Linux hack, and 516 should most likely only ever be used by the tunelp application. */ 517 if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) { 518 int status; 519 lp_claim_parport_or_block(&lp_table[minor]); 520 status = r_str(minor); 521 lp_release_parport(&lp_table[minor]); 522 if (status & LP_POUTPA) { 523 printk(KERN_INFO "lp%d out of paper\n", minor); 524 LP_F(minor) &= ~LP_BUSY; 525 ret = -ENOSPC; 526 goto out; 527 } else if (!(status & LP_PSELECD)) { 528 printk(KERN_INFO "lp%d off-line\n", minor); 529 LP_F(minor) &= ~LP_BUSY; 530 ret = -EIO; 531 goto out; 532 } else if (!(status & LP_PERRORP)) { 533 printk(KERN_ERR "lp%d printer error\n", minor); 534 LP_F(minor) &= ~LP_BUSY; 535 ret = -EIO; 536 goto out; 537 } 538 } 539 lp_table[minor].lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL); 540 if (!lp_table[minor].lp_buffer) { 541 LP_F(minor) &= ~LP_BUSY; 542 ret = -ENOMEM; 543 goto out; 544 } 545 /* Determine if the peripheral supports ECP mode */ 546 lp_claim_parport_or_block(&lp_table[minor]); 547 if ( (lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) && 548 !parport_negotiate(lp_table[minor].dev->port, 549 IEEE1284_MODE_ECP)) { 550 printk(KERN_INFO "lp%d: ECP mode\n", minor); 551 lp_table[minor].best_mode = IEEE1284_MODE_ECP; 552 } else { 553 lp_table[minor].best_mode = IEEE1284_MODE_COMPAT; 554 } 555 /* Leave peripheral in compatibility mode */ 556 parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT); 557 lp_release_parport(&lp_table[minor]); 558 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT; 559 out: 560 mutex_unlock(&lp_mutex); 561 return ret; 562 } 563 564 static int lp_release(struct inode *inode, struct file *file) 565 { 566 unsigned int minor = iminor(inode); 567 568 lp_claim_parport_or_block(&lp_table[minor]); 569 parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT); 570 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT; 571 lp_release_parport(&lp_table[minor]); 572 kfree(lp_table[minor].lp_buffer); 573 lp_table[minor].lp_buffer = NULL; 574 LP_F(minor) &= ~LP_BUSY; 575 return 0; 576 } 577 578 static int lp_do_ioctl(unsigned int minor, unsigned int cmd, 579 unsigned long arg, void __user *argp) 580 { 581 int status; 582 int retval = 0; 583 584 #ifdef LP_DEBUG 585 printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg); 586 #endif 587 if (minor >= LP_NO) 588 return -ENODEV; 589 if ((LP_F(minor) & LP_EXIST) == 0) 590 return -ENODEV; 591 switch ( cmd ) { 592 case LPTIME: 593 if (arg > UINT_MAX / HZ) 594 return -EINVAL; 595 LP_TIME(minor) = arg * HZ/100; 596 break; 597 case LPCHAR: 598 LP_CHAR(minor) = arg; 599 break; 600 case LPABORT: 601 if (arg) 602 LP_F(minor) |= LP_ABORT; 603 else 604 LP_F(minor) &= ~LP_ABORT; 605 break; 606 case LPABORTOPEN: 607 if (arg) 608 LP_F(minor) |= LP_ABORTOPEN; 609 else 610 LP_F(minor) &= ~LP_ABORTOPEN; 611 break; 612 case LPCAREFUL: 613 if (arg) 614 LP_F(minor) |= LP_CAREFUL; 615 else 616 LP_F(minor) &= ~LP_CAREFUL; 617 break; 618 case LPWAIT: 619 LP_WAIT(minor) = arg; 620 break; 621 case LPSETIRQ: 622 return -EINVAL; 623 break; 624 case LPGETIRQ: 625 if (copy_to_user(argp, &LP_IRQ(minor), 626 sizeof(int))) 627 return -EFAULT; 628 break; 629 case LPGETSTATUS: 630 if (mutex_lock_interruptible(&lp_table[minor].port_mutex)) 631 return -EINTR; 632 lp_claim_parport_or_block(&lp_table[minor]); 633 status = r_str(minor); 634 lp_release_parport(&lp_table[minor]); 635 mutex_unlock(&lp_table[minor].port_mutex); 636 637 if (copy_to_user(argp, &status, sizeof(int))) 638 return -EFAULT; 639 break; 640 case LPRESET: 641 lp_reset(minor); 642 break; 643 #ifdef LP_STATS 644 case LPGETSTATS: 645 if (copy_to_user(argp, &LP_STAT(minor), 646 sizeof(struct lp_stats))) 647 return -EFAULT; 648 if (capable(CAP_SYS_ADMIN)) 649 memset(&LP_STAT(minor), 0, 650 sizeof(struct lp_stats)); 651 break; 652 #endif 653 case LPGETFLAGS: 654 status = LP_F(minor); 655 if (copy_to_user(argp, &status, sizeof(int))) 656 return -EFAULT; 657 break; 658 659 default: 660 retval = -EINVAL; 661 } 662 return retval; 663 } 664 665 static int lp_set_timeout(unsigned int minor, s64 tv_sec, long tv_usec) 666 { 667 long to_jiffies; 668 669 /* Convert to jiffies, place in lp_table */ 670 if (tv_sec < 0 || tv_usec < 0) 671 return -EINVAL; 672 673 /* 674 * we used to not check, so let's not make this fatal, 675 * but deal with user space passing a 32-bit tv_nsec in 676 * a 64-bit field, capping the timeout to 1 second 677 * worth of microseconds, and capping the total at 678 * MAX_JIFFY_OFFSET. 679 */ 680 if (tv_usec > 999999) 681 tv_usec = 999999; 682 683 if (tv_sec >= MAX_SEC_IN_JIFFIES - 1) { 684 to_jiffies = MAX_JIFFY_OFFSET; 685 } else { 686 to_jiffies = DIV_ROUND_UP(tv_usec, 1000000/HZ); 687 to_jiffies += tv_sec * (long) HZ; 688 } 689 690 if (to_jiffies <= 0) { 691 return -EINVAL; 692 } 693 lp_table[minor].timeout = to_jiffies; 694 return 0; 695 } 696 697 static int lp_set_timeout32(unsigned int minor, void __user *arg) 698 { 699 s32 karg[2]; 700 701 if (copy_from_user(karg, arg, sizeof(karg))) 702 return -EFAULT; 703 704 return lp_set_timeout(minor, karg[0], karg[1]); 705 } 706 707 static int lp_set_timeout64(unsigned int minor, void __user *arg) 708 { 709 s64 karg[2]; 710 711 if (copy_from_user(karg, arg, sizeof(karg))) 712 return -EFAULT; 713 714 return lp_set_timeout(minor, karg[0], karg[1]); 715 } 716 717 static long lp_ioctl(struct file *file, unsigned int cmd, 718 unsigned long arg) 719 { 720 unsigned int minor; 721 int ret; 722 723 minor = iminor(file_inode(file)); 724 mutex_lock(&lp_mutex); 725 switch (cmd) { 726 case LPSETTIMEOUT_OLD: 727 if (BITS_PER_LONG == 32) { 728 ret = lp_set_timeout32(minor, (void __user *)arg); 729 break; 730 } 731 /* fallthrough for 64-bit */ 732 case LPSETTIMEOUT_NEW: 733 ret = lp_set_timeout64(minor, (void __user *)arg); 734 break; 735 default: 736 ret = lp_do_ioctl(minor, cmd, arg, (void __user *)arg); 737 break; 738 } 739 mutex_unlock(&lp_mutex); 740 741 return ret; 742 } 743 744 #ifdef CONFIG_COMPAT 745 static long lp_compat_ioctl(struct file *file, unsigned int cmd, 746 unsigned long arg) 747 { 748 unsigned int minor; 749 int ret; 750 751 minor = iminor(file_inode(file)); 752 mutex_lock(&lp_mutex); 753 switch (cmd) { 754 case LPSETTIMEOUT_OLD: 755 if (!COMPAT_USE_64BIT_TIME) { 756 ret = lp_set_timeout32(minor, (void __user *)arg); 757 break; 758 } 759 /* fallthrough for x32 mode */ 760 case LPSETTIMEOUT_NEW: 761 ret = lp_set_timeout64(minor, (void __user *)arg); 762 break; 763 #ifdef LP_STATS 764 case LPGETSTATS: 765 /* FIXME: add an implementation if you set LP_STATS */ 766 ret = -EINVAL; 767 break; 768 #endif 769 default: 770 ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg)); 771 break; 772 } 773 mutex_unlock(&lp_mutex); 774 775 return ret; 776 } 777 #endif 778 779 static const struct file_operations lp_fops = { 780 .owner = THIS_MODULE, 781 .write = lp_write, 782 .unlocked_ioctl = lp_ioctl, 783 #ifdef CONFIG_COMPAT 784 .compat_ioctl = lp_compat_ioctl, 785 #endif 786 .open = lp_open, 787 .release = lp_release, 788 #ifdef CONFIG_PARPORT_1284 789 .read = lp_read, 790 #endif 791 .llseek = noop_llseek, 792 }; 793 794 /* --- support for console on the line printer ----------------- */ 795 796 #ifdef CONFIG_LP_CONSOLE 797 798 #define CONSOLE_LP 0 799 800 /* If the printer is out of paper, we can either lose the messages or 801 * stall until the printer is happy again. Define CONSOLE_LP_STRICT 802 * non-zero to get the latter behaviour. */ 803 #define CONSOLE_LP_STRICT 1 804 805 /* The console must be locked when we get here. */ 806 807 static void lp_console_write(struct console *co, const char *s, 808 unsigned count) 809 { 810 struct pardevice *dev = lp_table[CONSOLE_LP].dev; 811 struct parport *port = dev->port; 812 ssize_t written; 813 814 if (parport_claim(dev)) 815 /* Nothing we can do. */ 816 return; 817 818 parport_set_timeout(dev, 0); 819 820 /* Go to compatibility mode. */ 821 parport_negotiate(port, IEEE1284_MODE_COMPAT); 822 823 do { 824 /* Write the data, converting LF->CRLF as we go. */ 825 ssize_t canwrite = count; 826 char *lf = memchr(s, '\n', count); 827 if (lf) 828 canwrite = lf - s; 829 830 if (canwrite > 0) { 831 written = parport_write(port, s, canwrite); 832 833 if (written <= 0) 834 continue; 835 836 s += written; 837 count -= written; 838 canwrite -= written; 839 } 840 841 if (lf && canwrite <= 0) { 842 const char *crlf = "\r\n"; 843 int i = 2; 844 845 /* Dodge the original '\n', and put '\r\n' instead. */ 846 s++; 847 count--; 848 do { 849 written = parport_write(port, crlf, i); 850 if (written > 0) 851 i -= written, crlf += written; 852 } while (i > 0 && (CONSOLE_LP_STRICT || written > 0)); 853 } 854 } while (count > 0 && (CONSOLE_LP_STRICT || written > 0)); 855 856 parport_release(dev); 857 } 858 859 static struct console lpcons = { 860 .name = "lp", 861 .write = lp_console_write, 862 .flags = CON_PRINTBUFFER, 863 }; 864 865 #endif /* console on line printer */ 866 867 /* --- initialisation code ------------------------------------- */ 868 869 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC }; 870 static char *parport[LP_NO]; 871 static bool reset; 872 873 module_param_array(parport, charp, NULL, 0); 874 module_param(reset, bool, 0); 875 876 #ifndef MODULE 877 static int __init lp_setup(char *str) 878 { 879 static int parport_ptr; 880 int x; 881 882 if (get_option(&str, &x)) { 883 if (x == 0) { 884 /* disable driver on "lp=" or "lp=0" */ 885 parport_nr[0] = LP_PARPORT_OFF; 886 } else { 887 printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x); 888 return 0; 889 } 890 } else if (!strncmp(str, "parport", 7)) { 891 int n = simple_strtoul(str+7, NULL, 10); 892 if (parport_ptr < LP_NO) 893 parport_nr[parport_ptr++] = n; 894 else 895 printk(KERN_INFO "lp: too many ports, %s ignored.\n", 896 str); 897 } else if (!strcmp(str, "auto")) { 898 parport_nr[0] = LP_PARPORT_AUTO; 899 } else if (!strcmp(str, "none")) { 900 if (parport_ptr < LP_NO) 901 parport_nr[parport_ptr++] = LP_PARPORT_NONE; 902 else 903 printk(KERN_INFO "lp: too many ports, %s ignored.\n", 904 str); 905 } else if (!strcmp(str, "reset")) { 906 reset = true; 907 } 908 return 1; 909 } 910 #endif 911 912 static int lp_register(int nr, struct parport *port) 913 { 914 lp_table[nr].dev = parport_register_device(port, "lp", 915 lp_preempt, NULL, NULL, 0, 916 (void *) &lp_table[nr]); 917 if (lp_table[nr].dev == NULL) 918 return 1; 919 lp_table[nr].flags |= LP_EXIST; 920 921 if (reset) 922 lp_reset(nr); 923 924 device_create(lp_class, port->dev, MKDEV(LP_MAJOR, nr), NULL, 925 "lp%d", nr); 926 927 printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name, 928 (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven"); 929 930 #ifdef CONFIG_LP_CONSOLE 931 if (!nr) { 932 if (port->modes & PARPORT_MODE_SAFEININT) { 933 register_console(&lpcons); 934 console_registered = port; 935 printk(KERN_INFO "lp%d: console ready\n", CONSOLE_LP); 936 } else 937 printk(KERN_ERR "lp%d: cannot run console on %s\n", 938 CONSOLE_LP, port->name); 939 } 940 #endif 941 942 return 0; 943 } 944 945 static void lp_attach(struct parport *port) 946 { 947 unsigned int i; 948 949 switch (parport_nr[0]) { 950 case LP_PARPORT_UNSPEC: 951 case LP_PARPORT_AUTO: 952 if (parport_nr[0] == LP_PARPORT_AUTO && 953 port->probe_info[0].class != PARPORT_CLASS_PRINTER) 954 return; 955 if (lp_count == LP_NO) { 956 printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO); 957 return; 958 } 959 if (!lp_register(lp_count, port)) 960 lp_count++; 961 break; 962 963 default: 964 for (i = 0; i < LP_NO; i++) { 965 if (port->number == parport_nr[i]) { 966 if (!lp_register(i, port)) 967 lp_count++; 968 break; 969 } 970 } 971 break; 972 } 973 } 974 975 static void lp_detach(struct parport *port) 976 { 977 /* Write this some day. */ 978 #ifdef CONFIG_LP_CONSOLE 979 if (console_registered == port) { 980 unregister_console(&lpcons); 981 console_registered = NULL; 982 } 983 #endif /* CONFIG_LP_CONSOLE */ 984 } 985 986 static struct parport_driver lp_driver = { 987 .name = "lp", 988 .attach = lp_attach, 989 .detach = lp_detach, 990 }; 991 992 static int __init lp_init(void) 993 { 994 int i, err = 0; 995 996 if (parport_nr[0] == LP_PARPORT_OFF) 997 return 0; 998 999 for (i = 0; i < LP_NO; i++) { 1000 lp_table[i].dev = NULL; 1001 lp_table[i].flags = 0; 1002 lp_table[i].chars = LP_INIT_CHAR; 1003 lp_table[i].time = LP_INIT_TIME; 1004 lp_table[i].wait = LP_INIT_WAIT; 1005 lp_table[i].lp_buffer = NULL; 1006 #ifdef LP_STATS 1007 lp_table[i].lastcall = 0; 1008 lp_table[i].runchars = 0; 1009 memset(&lp_table[i].stats, 0, sizeof(struct lp_stats)); 1010 #endif 1011 lp_table[i].last_error = 0; 1012 init_waitqueue_head(&lp_table[i].waitq); 1013 init_waitqueue_head(&lp_table[i].dataq); 1014 mutex_init(&lp_table[i].port_mutex); 1015 lp_table[i].timeout = 10 * HZ; 1016 } 1017 1018 if (register_chrdev(LP_MAJOR, "lp", &lp_fops)) { 1019 printk(KERN_ERR "lp: unable to get major %d\n", LP_MAJOR); 1020 return -EIO; 1021 } 1022 1023 lp_class = class_create(THIS_MODULE, "printer"); 1024 if (IS_ERR(lp_class)) { 1025 err = PTR_ERR(lp_class); 1026 goto out_reg; 1027 } 1028 1029 if (parport_register_driver(&lp_driver)) { 1030 printk(KERN_ERR "lp: unable to register with parport\n"); 1031 err = -EIO; 1032 goto out_class; 1033 } 1034 1035 if (!lp_count) { 1036 printk(KERN_INFO "lp: driver loaded but no devices found\n"); 1037 #ifndef CONFIG_PARPORT_1284 1038 if (parport_nr[0] == LP_PARPORT_AUTO) 1039 printk(KERN_INFO "lp: (is IEEE 1284 support enabled?)\n"); 1040 #endif 1041 } 1042 1043 return 0; 1044 1045 out_class: 1046 class_destroy(lp_class); 1047 out_reg: 1048 unregister_chrdev(LP_MAJOR, "lp"); 1049 return err; 1050 } 1051 1052 static int __init lp_init_module(void) 1053 { 1054 if (parport[0]) { 1055 /* The user gave some parameters. Let's see what they were. */ 1056 if (!strncmp(parport[0], "auto", 4)) 1057 parport_nr[0] = LP_PARPORT_AUTO; 1058 else { 1059 int n; 1060 for (n = 0; n < LP_NO && parport[n]; n++) { 1061 if (!strncmp(parport[n], "none", 4)) 1062 parport_nr[n] = LP_PARPORT_NONE; 1063 else { 1064 char *ep; 1065 unsigned long r = simple_strtoul(parport[n], &ep, 0); 1066 if (ep != parport[n]) 1067 parport_nr[n] = r; 1068 else { 1069 printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]); 1070 return -ENODEV; 1071 } 1072 } 1073 } 1074 } 1075 } 1076 1077 return lp_init(); 1078 } 1079 1080 static void lp_cleanup_module(void) 1081 { 1082 unsigned int offset; 1083 1084 parport_unregister_driver(&lp_driver); 1085 1086 #ifdef CONFIG_LP_CONSOLE 1087 unregister_console(&lpcons); 1088 #endif 1089 1090 unregister_chrdev(LP_MAJOR, "lp"); 1091 for (offset = 0; offset < LP_NO; offset++) { 1092 if (lp_table[offset].dev == NULL) 1093 continue; 1094 parport_unregister_device(lp_table[offset].dev); 1095 device_destroy(lp_class, MKDEV(LP_MAJOR, offset)); 1096 } 1097 class_destroy(lp_class); 1098 } 1099 1100 __setup("lp=", lp_setup); 1101 module_init(lp_init_module); 1102 module_exit(lp_cleanup_module); 1103 1104 MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR); 1105 MODULE_LICENSE("GPL"); 1106