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