blob: 62051f8b0910cb97c04fc58e364f0996437c60be [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#include <linux/errno.h>
118#include <linux/kernel.h>
119#include <linux/major.h>
120#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#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>
Marcelo Feitoza Parisi96757702005-09-10 00:26:57 -0700128#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static struct lp_struct lp_table[LP_NO];
142
143static unsigned int lp_count = 0;
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800144static struct class *lp_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146#ifdef CONFIG_LP_CONSOLE
147static struct parport *console_registered; // initially NULL
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 */
164static 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 */
172static 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
181static 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 */
193static 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
203static 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
215static 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
232static 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
271static 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
292static ssize_t lp_write(struct file * file, const char __user * buf,
293 size_t count, loff_t *ppos)
294{
Josef Sipeka7113a92006-12-08 02:36:55 -0800295 unsigned int minor = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 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
Marcelo Feitoza Parisi96757702005-09-10 00:26:57 -0700305 if (time_after(jiffies, lp_table[minor].lastcall + LP_TIME(minor)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 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 (down_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 }
401out_unlock:
402 up (&lp_table[minor].port_mutex);
403
404 return retv;
405}
406
407#ifdef CONFIG_PARPORT_1284
408
409/* Status readback conforming to ieee1284 */
410static ssize_t lp_read(struct file * file, char __user * buf,
411 size_t count, loff_t *ppos)
412{
413 DEFINE_WAIT(wait);
Josef Sipeka7113a92006-12-08 02:36:55 -0800414 unsigned int minor=iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 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 (down_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 up (&lp_table[minor].port_mutex);
483
484 return retval;
485}
486
487#endif /* IEEE 1284 support */
488
489static 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 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800524 lp_table[minor].lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 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
546static 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
560static 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 lp_claim_parport_or_block (&lp_table[minor]);
616 status = r_str(minor);
617 lp_release_parport (&lp_table[minor]);
618
619 if (copy_to_user(argp, &status, sizeof(int)))
620 return -EFAULT;
621 break;
622 case LPRESET:
623 lp_reset(minor);
624 break;
625#ifdef LP_STATS
626 case LPGETSTATS:
627 if (copy_to_user(argp, &LP_STAT(minor),
628 sizeof(struct lp_stats)))
629 return -EFAULT;
630 if (capable(CAP_SYS_ADMIN))
631 memset(&LP_STAT(minor), 0,
632 sizeof(struct lp_stats));
633 break;
634#endif
635 case LPGETFLAGS:
636 status = LP_F(minor);
637 if (copy_to_user(argp, &status, sizeof(int)))
638 return -EFAULT;
639 break;
640
641 case LPSETTIMEOUT:
642 if (copy_from_user (&par_timeout, argp,
643 sizeof (struct timeval))) {
644 return -EFAULT;
645 }
646 /* Convert to jiffies, place in lp_table */
647 if ((par_timeout.tv_sec < 0) ||
648 (par_timeout.tv_usec < 0)) {
649 return -EINVAL;
650 }
Milind Arun Choudharyb259d742007-05-08 00:28:57 -0700651 to_jiffies = DIV_ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 to_jiffies += par_timeout.tv_sec * (long) HZ;
653 if (to_jiffies <= 0) {
654 return -EINVAL;
655 }
656 lp_table[minor].timeout = to_jiffies;
657 break;
658
659 default:
660 retval = -EINVAL;
661 }
662 return retval;
663}
664
Arjan van de Ven62322d22006-07-03 00:24:21 -0700665static const struct file_operations lp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 .owner = THIS_MODULE,
667 .write = lp_write,
668 .ioctl = lp_ioctl,
669 .open = lp_open,
670 .release = lp_release,
671#ifdef CONFIG_PARPORT_1284
672 .read = lp_read,
673#endif
674};
675
676/* --- support for console on the line printer ----------------- */
677
678#ifdef CONFIG_LP_CONSOLE
679
680#define CONSOLE_LP 0
681
682/* If the printer is out of paper, we can either lose the messages or
683 * stall until the printer is happy again. Define CONSOLE_LP_STRICT
684 * non-zero to get the latter behaviour. */
685#define CONSOLE_LP_STRICT 1
686
687/* The console must be locked when we get here. */
688
689static void lp_console_write (struct console *co, const char *s,
690 unsigned count)
691{
692 struct pardevice *dev = lp_table[CONSOLE_LP].dev;
693 struct parport *port = dev->port;
694 ssize_t written;
695
696 if (parport_claim (dev))
697 /* Nothing we can do. */
698 return;
699
700 parport_set_timeout (dev, 0);
701
702 /* Go to compatibility mode. */
703 parport_negotiate (port, IEEE1284_MODE_COMPAT);
704
705 do {
706 /* Write the data, converting LF->CRLF as we go. */
707 ssize_t canwrite = count;
708 char *lf = memchr (s, '\n', count);
709 if (lf)
710 canwrite = lf - s;
711
712 if (canwrite > 0) {
713 written = parport_write (port, s, canwrite);
714
715 if (written <= 0)
716 continue;
717
718 s += written;
719 count -= written;
720 canwrite -= written;
721 }
722
723 if (lf && canwrite <= 0) {
724 const char *crlf = "\r\n";
725 int i = 2;
726
727 /* Dodge the original '\n', and put '\r\n' instead. */
728 s++;
729 count--;
730 do {
731 written = parport_write (port, crlf, i);
732 if (written > 0)
733 i -= written, crlf += written;
734 } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
735 }
736 } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
737
738 parport_release (dev);
739}
740
741static struct console lpcons = {
742 .name = "lp",
743 .write = lp_console_write,
744 .flags = CON_PRINTBUFFER,
745};
746
747#endif /* console on line printer */
748
749/* --- initialisation code ------------------------------------- */
750
751static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
752static char *parport[LP_NO] = { NULL, };
753static int reset = 0;
754
755module_param_array(parport, charp, NULL, 0);
756module_param(reset, bool, 0);
757
758#ifndef MODULE
759static int __init lp_setup (char *str)
760{
761 static int parport_ptr; // initially zero
762 int x;
763
764 if (get_option (&str, &x)) {
765 if (x == 0) {
766 /* disable driver on "lp=" or "lp=0" */
767 parport_nr[0] = LP_PARPORT_OFF;
768 } else {
769 printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
770 return 0;
771 }
772 } else if (!strncmp(str, "parport", 7)) {
773 int n = simple_strtoul(str+7, NULL, 10);
774 if (parport_ptr < LP_NO)
775 parport_nr[parport_ptr++] = n;
776 else
777 printk(KERN_INFO "lp: too many ports, %s ignored.\n",
778 str);
779 } else if (!strcmp(str, "auto")) {
780 parport_nr[0] = LP_PARPORT_AUTO;
781 } else if (!strcmp(str, "none")) {
782 parport_nr[parport_ptr++] = LP_PARPORT_NONE;
783 } else if (!strcmp(str, "reset")) {
784 reset = 1;
785 }
786 return 1;
787}
788#endif
789
790static int lp_register(int nr, struct parport *port)
791{
792 lp_table[nr].dev = parport_register_device(port, "lp",
793 lp_preempt, NULL, NULL, 0,
794 (void *) &lp_table[nr]);
795 if (lp_table[nr].dev == NULL)
796 return 1;
797 lp_table[nr].flags |= LP_EXIST;
798
799 if (reset)
800 lp_reset(nr);
801
David Brownellda675292007-05-08 00:27:42 -0700802 class_device_create(lp_class, NULL, MKDEV(LP_MAJOR, nr), port->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 "lp%d", nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
806 (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
807
808#ifdef CONFIG_LP_CONSOLE
809 if (!nr) {
810 if (port->modes & PARPORT_MODE_SAFEININT) {
811 register_console (&lpcons);
812 console_registered = port;
813 printk (KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
814 } else
815 printk (KERN_ERR "lp%d: cannot run console on %s\n",
816 CONSOLE_LP, port->name);
817 }
818#endif
819
820 return 0;
821}
822
823static void lp_attach (struct parport *port)
824{
825 unsigned int i;
826
827 switch (parport_nr[0])
828 {
829 case LP_PARPORT_UNSPEC:
830 case LP_PARPORT_AUTO:
831 if (parport_nr[0] == LP_PARPORT_AUTO &&
832 port->probe_info[0].class != PARPORT_CLASS_PRINTER)
833 return;
834 if (lp_count == LP_NO) {
835 printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
836 return;
837 }
838 if (!lp_register(lp_count, port))
839 lp_count++;
840 break;
841
842 default:
843 for (i = 0; i < LP_NO; i++) {
844 if (port->number == parport_nr[i]) {
845 if (!lp_register(i, port))
846 lp_count++;
847 break;
848 }
849 }
850 break;
851 }
852}
853
854static void lp_detach (struct parport *port)
855{
856 /* Write this some day. */
857#ifdef CONFIG_LP_CONSOLE
858 if (console_registered == port) {
859 unregister_console (&lpcons);
860 console_registered = NULL;
861 }
862#endif /* CONFIG_LP_CONSOLE */
863}
864
865static struct parport_driver lp_driver = {
866 .name = "lp",
867 .attach = lp_attach,
868 .detach = lp_detach,
869};
870
871static int __init lp_init (void)
872{
873 int i, err = 0;
874
875 if (parport_nr[0] == LP_PARPORT_OFF)
876 return 0;
877
878 for (i = 0; i < LP_NO; i++) {
879 lp_table[i].dev = NULL;
880 lp_table[i].flags = 0;
881 lp_table[i].chars = LP_INIT_CHAR;
882 lp_table[i].time = LP_INIT_TIME;
883 lp_table[i].wait = LP_INIT_WAIT;
884 lp_table[i].lp_buffer = NULL;
885#ifdef LP_STATS
886 lp_table[i].lastcall = 0;
887 lp_table[i].runchars = 0;
888 memset (&lp_table[i].stats, 0, sizeof (struct lp_stats));
889#endif
890 lp_table[i].last_error = 0;
891 init_waitqueue_head (&lp_table[i].waitq);
892 init_waitqueue_head (&lp_table[i].dataq);
893 init_MUTEX (&lp_table[i].port_mutex);
894 lp_table[i].timeout = 10 * HZ;
895 }
896
897 if (register_chrdev (LP_MAJOR, "lp", &lp_fops)) {
898 printk (KERN_ERR "lp: unable to get major %d\n", LP_MAJOR);
899 return -EIO;
900 }
901
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800902 lp_class = class_create(THIS_MODULE, "printer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (IS_ERR(lp_class)) {
904 err = PTR_ERR(lp_class);
Alan Coxd09d7dd2006-09-29 01:59:47 -0700905 goto out_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
908 if (parport_register_driver (&lp_driver)) {
909 printk (KERN_ERR "lp: unable to register with parport\n");
910 err = -EIO;
911 goto out_class;
912 }
913
914 if (!lp_count) {
915 printk (KERN_INFO "lp: driver loaded but no devices found\n");
916#ifndef CONFIG_PARPORT_1284
917 if (parport_nr[0] == LP_PARPORT_AUTO)
918 printk (KERN_INFO "lp: (is IEEE 1284 support enabled?)\n");
919#endif
920 }
921
922 return 0;
923
924out_class:
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800925 class_destroy(lp_class);
Alan Coxd09d7dd2006-09-29 01:59:47 -0700926out_reg:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 unregister_chrdev(LP_MAJOR, "lp");
928 return err;
929}
930
931static int __init lp_init_module (void)
932{
933 if (parport[0]) {
934 /* The user gave some parameters. Let's see what they were. */
935 if (!strncmp(parport[0], "auto", 4))
936 parport_nr[0] = LP_PARPORT_AUTO;
937 else {
938 int n;
939 for (n = 0; n < LP_NO && parport[n]; n++) {
940 if (!strncmp(parport[n], "none", 4))
941 parport_nr[n] = LP_PARPORT_NONE;
942 else {
943 char *ep;
944 unsigned long r = simple_strtoul(parport[n], &ep, 0);
945 if (ep != parport[n])
946 parport_nr[n] = r;
947 else {
948 printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
949 return -ENODEV;
950 }
951 }
952 }
953 }
954 }
955
956 return lp_init();
957}
958
959static void lp_cleanup_module (void)
960{
961 unsigned int offset;
962
963 parport_unregister_driver (&lp_driver);
964
965#ifdef CONFIG_LP_CONSOLE
966 unregister_console (&lpcons);
967#endif
968
969 unregister_chrdev(LP_MAJOR, "lp");
970 for (offset = 0; offset < LP_NO; offset++) {
971 if (lp_table[offset].dev == NULL)
972 continue;
973 parport_unregister_device(lp_table[offset].dev);
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800974 class_device_destroy(lp_class, MKDEV(LP_MAJOR, offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800976 class_destroy(lp_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979__setup("lp=", lp_setup);
980module_init(lp_init_module);
981module_exit(lp_cleanup_module);
982
983MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR);
984MODULE_LICENSE("GPL");