- Update to 2.6.25-rc3.
[linux-flexiantxendom0-3.2.10.git] / drivers / char / lp.c
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.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
130 #include <linux/parport.h>
131 #undef LP_STATS
132 #include <linux/lp.h>
133
134 #include <asm/irq.h>
135 #include <asm/uaccess.h>
136 #include <asm/system.h>
137
138 /* if you have more than 8 printers, remember to increase LP_NO */
139 #define LP_NO 8
140
141 static struct lp_struct lp_table[LP_NO];
142
143 static unsigned int lp_count = 0;
144 static struct class *lp_class;
145
146 #ifdef CONFIG_LP_CONSOLE
147 static struct parport *console_registered;
148 #endif /* CONFIG_LP_CONSOLE */
149
150 #undef LP_DEBUG
151
152 /* Bits used to manage claiming the parport device */
153 #define LP_PREEMPT_REQUEST 1
154 #define LP_PARPORT_CLAIMED 2
155
156 /* --- low-level port access ----------------------------------- */
157
158 #define r_dtr(x)        (parport_read_data(lp_table[(x)].dev->port))
159 #define r_str(x)        (parport_read_status(lp_table[(x)].dev->port))
160 #define w_ctr(x,y)      do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
161 #define w_dtr(x,y)      do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
162
163 /* Claim the parport or block trying unless we've already claimed it */
164 static void lp_claim_parport_or_block(struct lp_struct *this_lp)
165 {
166         if (!test_and_set_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
167                 parport_claim_or_block (this_lp->dev);
168         }
169 }
170
171 /* Claim the parport or block trying unless we've already claimed it */
172 static void lp_release_parport(struct lp_struct *this_lp)
173 {
174         if (test_and_clear_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
175                 parport_release (this_lp->dev);
176         }
177 }
178
179
180
181 static int lp_preempt(void *handle)
182 {
183         struct lp_struct *this_lp = (struct lp_struct *)handle;
184         set_bit(LP_PREEMPT_REQUEST, &this_lp->bits);
185         return (1);
186 }
187
188
189 /* 
190  * Try to negotiate to a new mode; if unsuccessful negotiate to
191  * compatibility mode.  Return the mode we ended up in.
192  */
193 static int lp_negotiate(struct parport * port, int mode)
194 {
195         if (parport_negotiate (port, mode) != 0) {
196                 mode = IEEE1284_MODE_COMPAT;
197                 parport_negotiate (port, mode);
198         }
199
200         return (mode);
201 }
202
203 static int lp_reset(int minor)
204 {
205         int retval;
206         lp_claim_parport_or_block (&lp_table[minor]);
207         w_ctr(minor, LP_PSELECP);
208         udelay (LP_DELAY);
209         w_ctr(minor, LP_PSELECP | LP_PINITP);
210         retval = r_str(minor);
211         lp_release_parport (&lp_table[minor]);
212         return retval;
213 }
214
215 static void lp_error (int minor)
216 {
217         DEFINE_WAIT(wait);
218         int polling;
219
220         if (LP_F(minor) & LP_ABORT)
221                 return;
222
223         polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
224         if (polling) lp_release_parport (&lp_table[minor]);
225         prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
226         schedule_timeout(LP_TIMEOUT_POLLED);
227         finish_wait(&lp_table[minor].waitq, &wait);
228         if (polling) lp_claim_parport_or_block (&lp_table[minor]);
229         else parport_yield_blocking (lp_table[minor].dev);
230 }
231
232 static int lp_check_status(int minor)
233 {
234         int error = 0;
235         unsigned int last = lp_table[minor].last_error;
236         unsigned char status = r_str(minor);
237         if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL))
238                 /* No error. */
239                 last = 0;
240         else if ((status & LP_POUTPA)) {
241                 if (last != LP_POUTPA) {
242                         last = LP_POUTPA;
243                         printk(KERN_INFO "lp%d out of paper\n", minor);
244                 }
245                 error = -ENOSPC;
246         } else if (!(status & LP_PSELECD)) {
247                 if (last != LP_PSELECD) {
248                         last = LP_PSELECD;
249                         printk(KERN_INFO "lp%d off-line\n", minor);
250                 }
251                 error = -EIO;
252         } else if (!(status & LP_PERRORP)) {
253                 if (last != LP_PERRORP) {
254                         last = LP_PERRORP;
255                         printk(KERN_INFO "lp%d on fire\n", minor);
256                 }
257                 error = -EIO;
258         } else {
259                 last = 0; /* Come here if LP_CAREFUL is set and no
260                              errors are reported. */
261         }
262
263         lp_table[minor].last_error = last;
264
265         if (last != 0)
266                 lp_error(minor);
267
268         return error;
269 }
270
271 static int lp_wait_ready(int minor, int nonblock)
272 {
273         int error = 0;
274
275         /* If we're not in compatibility mode, we're ready now! */
276         if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) {
277           return (0);
278         }
279
280         do {
281                 error = lp_check_status (minor);
282                 if (error && (nonblock || (LP_F(minor) & LP_ABORT)))
283                         break;
284                 if (signal_pending (current)) {
285                         error = -EINTR;
286                         break;
287                 }
288         } while (error);
289         return error;
290 }
291
292 static ssize_t lp_write(struct file * file, const char __user * buf,
293                         size_t count, loff_t *ppos)
294 {
295         unsigned int minor = iminor(file->f_path.dentry->d_inode);
296         struct parport *port = lp_table[minor].dev->port;
297         char *kbuf = lp_table[minor].lp_buffer;
298         ssize_t retv = 0;
299         ssize_t written;
300         size_t copy_size = count;
301         int nonblock = ((file->f_flags & O_NONBLOCK) ||
302                         (LP_F(minor) & LP_ABORT));
303
304 #ifdef LP_STATS
305         if (time_after(jiffies, lp_table[minor].lastcall + LP_TIME(minor)))
306                 lp_table[minor].runchars = 0;
307
308         lp_table[minor].lastcall = jiffies;
309 #endif
310
311         /* Need to copy the data from user-space. */
312         if (copy_size > LP_BUFFER_SIZE)
313                 copy_size = LP_BUFFER_SIZE;
314
315         if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
316                 return -EINTR;
317
318         if (copy_from_user (kbuf, buf, copy_size)) {
319                 retv = -EFAULT;
320                 goto out_unlock;
321         }
322
323         /* Claim Parport or sleep until it becomes available
324          */
325         lp_claim_parport_or_block (&lp_table[minor]);
326         /* Go to the proper mode. */
327         lp_table[minor].current_mode = lp_negotiate (port, 
328                                                      lp_table[minor].best_mode);
329
330         parport_set_timeout (lp_table[minor].dev,
331                              (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
332                               : lp_table[minor].timeout));
333
334         if ((retv = lp_wait_ready (minor, nonblock)) == 0)
335         do {
336                 /* Write the data. */
337                 written = parport_write (port, kbuf, copy_size);
338                 if (written > 0) {
339                         copy_size -= written;
340                         count -= written;
341                         buf  += written;
342                         retv += written;
343                 }
344
345                 if (signal_pending (current)) {
346                         if (retv == 0)
347                                 retv = -EINTR;
348
349                         break;
350                 }
351
352                 if (copy_size > 0) {
353                         /* incomplete write -> check error ! */
354                         int error;
355
356                         parport_negotiate (lp_table[minor].dev->port, 
357                                            IEEE1284_MODE_COMPAT);
358                         lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
359
360                         error = lp_wait_ready (minor, nonblock);
361
362                         if (error) {
363                                 if (retv == 0)
364                                         retv = error;
365                                 break;
366                         } else if (nonblock) {
367                                 if (retv == 0)
368                                         retv = -EAGAIN;
369                                 break;
370                         }
371
372                         parport_yield_blocking (lp_table[minor].dev);
373                         lp_table[minor].current_mode 
374                           = lp_negotiate (port, 
375                                           lp_table[minor].best_mode);
376
377                 } else if (need_resched())
378                         schedule ();
379
380                 if (count) {
381                         copy_size = count;
382                         if (copy_size > LP_BUFFER_SIZE)
383                                 copy_size = LP_BUFFER_SIZE;
384
385                         if (copy_from_user(kbuf, buf, copy_size)) {
386                                 if (retv == 0)
387                                         retv = -EFAULT;
388                                 break;
389                         }
390                 }       
391         } while (count > 0);
392
393         if (test_and_clear_bit(LP_PREEMPT_REQUEST, 
394                                &lp_table[minor].bits)) {
395                 printk(KERN_INFO "lp%d releasing parport\n", minor);
396                 parport_negotiate (lp_table[minor].dev->port, 
397                                    IEEE1284_MODE_COMPAT);
398                 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
399                 lp_release_parport (&lp_table[minor]);
400         }
401 out_unlock:
402         mutex_unlock(&lp_table[minor].port_mutex);
403
404         return retv;
405 }
406
407 #ifdef CONFIG_PARPORT_1284
408
409 /* Status readback conforming to ieee1284 */
410 static ssize_t lp_read(struct file * file, char __user * buf,
411                        size_t count, loff_t *ppos)
412 {
413         DEFINE_WAIT(wait);
414         unsigned int minor=iminor(file->f_path.dentry->d_inode);
415         struct parport *port = lp_table[minor].dev->port;
416         ssize_t retval = 0;
417         char *kbuf = lp_table[minor].lp_buffer;
418         int nonblock = ((file->f_flags & O_NONBLOCK) ||
419                         (LP_F(minor) & LP_ABORT));
420
421         if (count > LP_BUFFER_SIZE)
422                 count = LP_BUFFER_SIZE;
423
424         if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
425                 return -EINTR;
426
427         lp_claim_parport_or_block (&lp_table[minor]);
428
429         parport_set_timeout (lp_table[minor].dev,
430                              (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
431                               : lp_table[minor].timeout));
432
433         parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
434         if (parport_negotiate (lp_table[minor].dev->port,
435                                IEEE1284_MODE_NIBBLE)) {
436                 retval = -EIO;
437                 goto out;
438         }
439
440         while (retval == 0) {
441                 retval = parport_read (port, kbuf, count);
442
443                 if (retval > 0)
444                         break;
445
446                 if (nonblock) {
447                         retval = -EAGAIN;
448                         break;
449                 }
450
451                 /* Wait for data. */
452
453                 if (lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE) {
454                         parport_negotiate (lp_table[minor].dev->port,
455                                            IEEE1284_MODE_COMPAT);
456                         lp_error (minor);
457                         if (parport_negotiate (lp_table[minor].dev->port,
458                                                IEEE1284_MODE_NIBBLE)) {
459                                 retval = -EIO;
460                                 goto out;
461                         }
462                 } else {
463                         prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
464                         schedule_timeout(LP_TIMEOUT_POLLED);
465                         finish_wait(&lp_table[minor].waitq, &wait);
466                 }
467
468                 if (signal_pending (current)) {
469                         retval = -ERESTARTSYS;
470                         break;
471                 }
472
473                 cond_resched ();
474         }
475         parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
476  out:
477         lp_release_parport (&lp_table[minor]);
478
479         if (retval > 0 && copy_to_user (buf, kbuf, retval))
480                 retval = -EFAULT;
481
482         mutex_unlock(&lp_table[minor].port_mutex);
483
484         return retval;
485 }
486
487 #endif /* IEEE 1284 support */
488
489 static int lp_open(struct inode * inode, struct file * file)
490 {
491         unsigned int minor = iminor(inode);
492
493         if (minor >= LP_NO)
494                 return -ENXIO;
495         if ((LP_F(minor) & LP_EXIST) == 0)
496                 return -ENXIO;
497         if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor)))
498                 return -EBUSY;
499
500         /* If ABORTOPEN is set and the printer is offline or out of paper,
501            we may still want to open it to perform ioctl()s.  Therefore we
502            have commandeered O_NONBLOCK, even though it is being used in
503            a non-standard manner.  This is strictly a Linux hack, and
504            should most likely only ever be used by the tunelp application. */
505         if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
506                 int status;
507                 lp_claim_parport_or_block (&lp_table[minor]);
508                 status = r_str(minor);
509                 lp_release_parport (&lp_table[minor]);
510                 if (status & LP_POUTPA) {
511                         printk(KERN_INFO "lp%d out of paper\n", minor);
512                         LP_F(minor) &= ~LP_BUSY;
513                         return -ENOSPC;
514                 } else if (!(status & LP_PSELECD)) {
515                         printk(KERN_INFO "lp%d off-line\n", minor);
516                         LP_F(minor) &= ~LP_BUSY;
517                         return -EIO;
518                 } else if (!(status & LP_PERRORP)) {
519                         printk(KERN_ERR "lp%d printer error\n", minor);
520                         LP_F(minor) &= ~LP_BUSY;
521                         return -EIO;
522                 }
523         }
524         lp_table[minor].lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
525         if (!lp_table[minor].lp_buffer) {
526                 LP_F(minor) &= ~LP_BUSY;
527                 return -ENOMEM;
528         }
529         /* Determine if the peripheral supports ECP mode */
530         lp_claim_parport_or_block (&lp_table[minor]);
531         if ( (lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) &&
532              !parport_negotiate (lp_table[minor].dev->port, 
533                                  IEEE1284_MODE_ECP)) {
534                 printk (KERN_INFO "lp%d: ECP mode\n", minor);
535                 lp_table[minor].best_mode = IEEE1284_MODE_ECP;
536         } else {
537                 lp_table[minor].best_mode = IEEE1284_MODE_COMPAT;
538         }
539         /* Leave peripheral in compatibility mode */
540         parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
541         lp_release_parport (&lp_table[minor]);
542         lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
543         return 0;
544 }
545
546 static int lp_release(struct inode * inode, struct file * file)
547 {
548         unsigned int minor = iminor(inode);
549
550         lp_claim_parport_or_block (&lp_table[minor]);
551         parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
552         lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
553         lp_release_parport (&lp_table[minor]);
554         kfree(lp_table[minor].lp_buffer);
555         lp_table[minor].lp_buffer = NULL;
556         LP_F(minor) &= ~LP_BUSY;
557         return 0;
558 }
559
560 static int lp_ioctl(struct inode *inode, struct file *file,
561                     unsigned int cmd, unsigned long arg)
562 {
563         unsigned int minor = iminor(inode);
564         int status;
565         int retval = 0;
566         void __user *argp = (void __user *)arg;
567
568 #ifdef LP_DEBUG
569         printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
570 #endif
571         if (minor >= LP_NO)
572                 return -ENODEV;
573         if ((LP_F(minor) & LP_EXIST) == 0)
574                 return -ENODEV;
575         switch ( cmd ) {
576                 struct timeval par_timeout;
577                 long to_jiffies;
578
579                 case LPTIME:
580                         LP_TIME(minor) = arg * HZ/100;
581                         break;
582                 case LPCHAR:
583                         LP_CHAR(minor) = arg;
584                         break;
585                 case LPABORT:
586                         if (arg)
587                                 LP_F(minor) |= LP_ABORT;
588                         else
589                                 LP_F(minor) &= ~LP_ABORT;
590                         break;
591                 case LPABORTOPEN:
592                         if (arg)
593                                 LP_F(minor) |= LP_ABORTOPEN;
594                         else
595                                 LP_F(minor) &= ~LP_ABORTOPEN;
596                         break;
597                 case LPCAREFUL:
598                         if (arg)
599                                 LP_F(minor) |= LP_CAREFUL;
600                         else
601                                 LP_F(minor) &= ~LP_CAREFUL;
602                         break;
603                 case LPWAIT:
604                         LP_WAIT(minor) = arg;
605                         break;
606                 case LPSETIRQ: 
607                         return -EINVAL;
608                         break;
609                 case LPGETIRQ:
610                         if (copy_to_user(argp, &LP_IRQ(minor),
611                                         sizeof(int)))
612                                 return -EFAULT;
613                         break;
614                 case LPGETSTATUS:
615                         if (down_interruptible (&lp_table[minor].port_mutex))
616                                 return -EINTR;
617                         lp_claim_parport_or_block (&lp_table[minor]);
618                         status = r_str(minor);
619                         lp_release_parport (&lp_table[minor]);
620                         up (&lp_table[minor].port_mutex);
621
622                         if (copy_to_user(argp, &status, sizeof(int)))
623                                 return -EFAULT;
624                         break;
625                 case LPRESET:
626                         lp_reset(minor);
627                         break;
628 #ifdef LP_STATS
629                 case LPGETSTATS:
630                         if (copy_to_user(argp, &LP_STAT(minor),
631                                         sizeof(struct lp_stats)))
632                                 return -EFAULT;
633                         if (capable(CAP_SYS_ADMIN))
634                                 memset(&LP_STAT(minor), 0,
635                                                 sizeof(struct lp_stats));
636                         break;
637 #endif
638                 case LPGETFLAGS:
639                         status = LP_F(minor);
640                         if (copy_to_user(argp, &status, sizeof(int)))
641                                 return -EFAULT;
642                         break;
643
644                 case LPSETTIMEOUT:
645                         if (copy_from_user (&par_timeout, argp,
646                                             sizeof (struct timeval))) {
647                                 return -EFAULT;
648                         }
649                         /* Convert to jiffies, place in lp_table */
650                         if ((par_timeout.tv_sec < 0) ||
651                             (par_timeout.tv_usec < 0)) {
652                                 return -EINVAL;
653                         }
654                         to_jiffies = DIV_ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
655                         to_jiffies += par_timeout.tv_sec * (long) HZ;
656                         if (to_jiffies <= 0) {
657                                 return -EINVAL;
658                         }
659                         lp_table[minor].timeout = to_jiffies;
660                         break;
661
662                 default:
663                         retval = -EINVAL;
664         }
665         return retval;
666 }
667
668 static const struct file_operations lp_fops = {
669         .owner          = THIS_MODULE,
670         .write          = lp_write,
671         .ioctl          = lp_ioctl,
672         .open           = lp_open,
673         .release        = lp_release,
674 #ifdef CONFIG_PARPORT_1284
675         .read           = lp_read,
676 #endif
677 };
678
679 /* --- support for console on the line printer ----------------- */
680
681 #ifdef CONFIG_LP_CONSOLE
682
683 #define CONSOLE_LP 0
684
685 /* If the printer is out of paper, we can either lose the messages or
686  * stall until the printer is happy again.  Define CONSOLE_LP_STRICT
687  * non-zero to get the latter behaviour. */
688 #define CONSOLE_LP_STRICT 1
689
690 /* The console must be locked when we get here. */
691
692 static void lp_console_write (struct console *co, const char *s,
693                               unsigned count)
694 {
695         struct pardevice *dev = lp_table[CONSOLE_LP].dev;
696         struct parport *port = dev->port;
697         ssize_t written;
698
699         if (parport_claim (dev))
700                 /* Nothing we can do. */
701                 return;
702
703         parport_set_timeout (dev, 0);
704
705         /* Go to compatibility mode. */
706         parport_negotiate (port, IEEE1284_MODE_COMPAT);
707
708         do {
709                 /* Write the data, converting LF->CRLF as we go. */
710                 ssize_t canwrite = count;
711                 char *lf = memchr (s, '\n', count);
712                 if (lf)
713                         canwrite = lf - s;
714
715                 if (canwrite > 0) {
716                         written = parport_write (port, s, canwrite);
717
718                         if (written <= 0)
719                                 continue;
720
721                         s += written;
722                         count -= written;
723                         canwrite -= written;
724                 }
725
726                 if (lf && canwrite <= 0) {
727                         const char *crlf = "\r\n";
728                         int i = 2;
729
730                         /* Dodge the original '\n', and put '\r\n' instead. */
731                         s++;
732                         count--;
733                         do {
734                                 written = parport_write (port, crlf, i);
735                                 if (written > 0)
736                                         i -= written, crlf += written;
737                         } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
738                 }
739         } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
740
741         parport_release (dev);
742 }
743
744 static struct console lpcons = {
745         .name           = "lp",
746         .write          = lp_console_write,
747         .flags          = CON_PRINTBUFFER,
748 };
749
750 #endif /* console on line printer */
751
752 /* --- initialisation code ------------------------------------- */
753
754 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
755 static char *parport[LP_NO];
756 static int reset;
757
758 module_param_array(parport, charp, NULL, 0);
759 module_param(reset, bool, 0);
760
761 #ifndef MODULE
762 static int __init lp_setup (char *str)
763 {
764         static int parport_ptr;
765         int x;
766
767         if (get_option(&str, &x)) {
768                 if (x == 0) {
769                         /* disable driver on "lp=" or "lp=0" */
770                         parport_nr[0] = LP_PARPORT_OFF;
771                 } else {
772                         printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
773                         return 0;
774                 }
775         } else if (!strncmp(str, "parport", 7)) {
776                 int n = simple_strtoul(str+7, NULL, 10);
777                 if (parport_ptr < LP_NO)
778                         parport_nr[parport_ptr++] = n;
779                 else
780                         printk(KERN_INFO "lp: too many ports, %s ignored.\n",
781                                str);
782         } else if (!strcmp(str, "auto")) {
783                 parport_nr[0] = LP_PARPORT_AUTO;
784         } else if (!strcmp(str, "none")) {
785                 parport_nr[parport_ptr++] = LP_PARPORT_NONE;
786         } else if (!strcmp(str, "reset")) {
787                 reset = 1;
788         }
789         return 1;
790 }
791 #endif
792
793 static int lp_register(int nr, struct parport *port)
794 {
795         lp_table[nr].dev = parport_register_device(port, "lp", 
796                                                    lp_preempt, NULL, NULL, 0,
797                                                    (void *) &lp_table[nr]);
798         if (lp_table[nr].dev == NULL)
799                 return 1;
800         lp_table[nr].flags |= LP_EXIST;
801
802         if (reset)
803                 lp_reset(nr);
804
805         device_create(lp_class, port->dev, MKDEV(LP_MAJOR, nr), "lp%d", nr);
806
807         printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name, 
808                (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
809
810 #ifdef CONFIG_LP_CONSOLE
811         if (!nr) {
812                 if (port->modes & PARPORT_MODE_SAFEININT) {
813                         register_console(&lpcons);
814                         console_registered = port;
815                         printk (KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
816                 } else
817                         printk (KERN_ERR "lp%d: cannot run console on %s\n",
818                                 CONSOLE_LP, port->name);
819         }
820 #endif
821
822         return 0;
823 }
824
825 static void lp_attach (struct parport *port)
826 {
827         unsigned int i;
828
829         switch (parport_nr[0]) {
830         case LP_PARPORT_UNSPEC:
831         case LP_PARPORT_AUTO:
832                 if (parport_nr[0] == LP_PARPORT_AUTO &&
833                     port->probe_info[0].class != PARPORT_CLASS_PRINTER)
834                         return;
835                 if (lp_count == LP_NO) {
836                         printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
837                         return;
838                 }
839                 if (!lp_register(lp_count, port))
840                         lp_count++;
841                 break;
842
843         default:
844                 for (i = 0; i < LP_NO; i++) {
845                         if (port->number == parport_nr[i]) {
846                                 if (!lp_register(i, port))
847                                         lp_count++;
848                                 break;
849                         }
850                 }
851                 break;
852         }
853 }
854
855 static void lp_detach (struct parport *port)
856 {
857         /* Write this some day. */
858 #ifdef CONFIG_LP_CONSOLE
859         if (console_registered == port) {
860                 unregister_console(&lpcons);
861                 console_registered = NULL;
862         }
863 #endif /* CONFIG_LP_CONSOLE */
864 }
865
866 static struct parport_driver lp_driver = {
867         .name = "lp",
868         .attach = lp_attach,
869         .detach = lp_detach,
870 };
871
872 static int __init lp_init (void)
873 {
874         int i, err = 0;
875
876         if (parport_nr[0] == LP_PARPORT_OFF)
877                 return 0;
878
879         for (i = 0; i < LP_NO; i++) {
880                 lp_table[i].dev = NULL;
881                 lp_table[i].flags = 0;
882                 lp_table[i].chars = LP_INIT_CHAR;
883                 lp_table[i].time = LP_INIT_TIME;
884                 lp_table[i].wait = LP_INIT_WAIT;
885                 lp_table[i].lp_buffer = NULL;
886 #ifdef LP_STATS
887                 lp_table[i].lastcall = 0;
888                 lp_table[i].runchars = 0;
889                 memset (&lp_table[i].stats, 0, sizeof (struct lp_stats));
890 #endif
891                 lp_table[i].last_error = 0;
892                 init_waitqueue_head (&lp_table[i].waitq);
893                 init_waitqueue_head (&lp_table[i].dataq);
894                 mutex_init(&lp_table[i].port_mutex);
895                 lp_table[i].timeout = 10 * HZ;
896         }
897
898         if (register_chrdev (LP_MAJOR, "lp", &lp_fops)) {
899                 printk (KERN_ERR "lp: unable to get major %d\n", LP_MAJOR);
900                 return -EIO;
901         }
902
903         lp_class = class_create(THIS_MODULE, "printer");
904         if (IS_ERR(lp_class)) {
905                 err = PTR_ERR(lp_class);
906                 goto out_reg;
907         }
908
909         if (parport_register_driver (&lp_driver)) {
910                 printk (KERN_ERR "lp: unable to register with parport\n");
911                 err = -EIO;
912                 goto out_class;
913         }
914
915         if (!lp_count) {
916                 printk (KERN_INFO "lp: driver loaded but no devices found\n");
917 #ifndef CONFIG_PARPORT_1284
918                 if (parport_nr[0] == LP_PARPORT_AUTO)
919                         printk (KERN_INFO "lp: (is IEEE 1284 support enabled?)\n");
920 #endif
921         }
922
923         return 0;
924
925 out_class:
926         class_destroy(lp_class);
927 out_reg:
928         unregister_chrdev(LP_MAJOR, "lp");
929         return err;
930 }
931
932 static int __init lp_init_module (void)
933 {
934         if (parport[0]) {
935                 /* The user gave some parameters.  Let's see what they were.  */
936                 if (!strncmp(parport[0], "auto", 4))
937                         parport_nr[0] = LP_PARPORT_AUTO;
938                 else {
939                         int n;
940                         for (n = 0; n < LP_NO && parport[n]; n++) {
941                                 if (!strncmp(parport[n], "none", 4))
942                                         parport_nr[n] = LP_PARPORT_NONE;
943                                 else {
944                                         char *ep;
945                                         unsigned long r = simple_strtoul(parport[n], &ep, 0);
946                                         if (ep != parport[n]) 
947                                                 parport_nr[n] = r;
948                                         else {
949                                                 printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
950                                                 return -ENODEV;
951                                         }
952                                 }
953                         }
954                 }
955         }
956
957         return lp_init();
958 }
959
960 static void lp_cleanup_module (void)
961 {
962         unsigned int offset;
963
964         parport_unregister_driver (&lp_driver);
965
966 #ifdef CONFIG_LP_CONSOLE
967         unregister_console (&lpcons);
968 #endif
969
970         unregister_chrdev(LP_MAJOR, "lp");
971         for (offset = 0; offset < LP_NO; offset++) {
972                 if (lp_table[offset].dev == NULL)
973                         continue;
974                 parport_unregister_device(lp_table[offset].dev);
975                 device_destroy(lp_class, MKDEV(LP_MAJOR, offset));
976         }
977         class_destroy(lp_class);
978 }
979
980 __setup("lp=", lp_setup);
981 module_init(lp_init_module);
982 module_exit(lp_cleanup_module);
983
984 MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR);
985 MODULE_LICENSE("GPL");