114 #include <linux/module.h>
117 #include <linux/errno.h>
118 #include <linux/kernel.h>
120 #include <linux/sched.h>
121 #include <linux/slab.h>
122 #include <linux/fcntl.h>
124 #include <linux/poll.h>
126 #include <linux/device.h>
127 #include <linux/wait.h>
132 #include <linux/parport.h>
134 #include <linux/lp.h>
137 #include <asm/uaccess.h>
145 static unsigned int lp_count = 0;
146 static struct class *lp_class;
148 #ifdef CONFIG_LP_CONSOLE
149 static struct parport *console_registered;
155 #define LP_PREEMPT_REQUEST 1
156 #define LP_PARPORT_CLAIMED 2
160 #define r_dtr(x) (parport_read_data(lp_table[(x)].dev->port))
161 #define r_str(x) (parport_read_status(lp_table[(x)].dev->port))
162 #define w_ctr(x,y) do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
163 #define w_dtr(x,y) do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
166 static void lp_claim_parport_or_block(
struct lp_struct *this_lp)
174 static void lp_release_parport(
struct lp_struct *this_lp)
183 static int lp_preempt(
void *
handle)
205 static int lp_reset(
int minor)
208 lp_claim_parport_or_block (&lp_table[minor]);
212 retval =
r_str(minor);
213 lp_release_parport (&lp_table[minor]);
217 static void lp_error (
int minor)
226 if (polling) lp_release_parport (&lp_table[minor]);
230 if (polling) lp_claim_parport_or_block (&lp_table[minor]);
231 else parport_yield_blocking (lp_table[minor].
dev);
234 static int lp_check_status(
int minor)
237 unsigned int last = lp_table[minor].last_error;
243 if (last != LP_POUTPA) {
249 if (last != LP_PSELECD) {
254 }
else if (!(status & LP_PERRORP)) {
255 if (last != LP_PERRORP) {
265 lp_table[minor].last_error = last;
273 static int lp_wait_ready(
int minor,
int nonblock)
283 error = lp_check_status (minor);
284 if (error && (nonblock || (
LP_F(minor) & LP_ABORT)))
286 if (signal_pending (
current)) {
295 size_t count, loff_t *ppos)
297 unsigned int minor = iminor(file->
f_path.dentry->d_inode);
298 struct parport *port = lp_table[minor].dev->port;
299 char *kbuf = lp_table[minor].lp_buffer;
302 size_t copy_size =
count;
308 lp_table[minor].runchars = 0;
310 lp_table[minor].lastcall =
jiffies;
327 lp_claim_parport_or_block (&lp_table[minor]);
329 lp_table[minor].current_mode = lp_negotiate (port,
330 lp_table[minor].best_mode);
334 : lp_table[minor].timeout));
336 if ((retv = lp_wait_ready (minor, nonblock)) == 0)
341 copy_size -= written;
347 if (signal_pending (
current)) {
362 error = lp_wait_ready (minor, nonblock);
368 }
else if (nonblock) {
374 parport_yield_blocking (lp_table[minor].dev);
375 lp_table[minor].current_mode
376 = lp_negotiate (port,
377 lp_table[minor].best_mode);
379 }
else if (need_resched())
396 &lp_table[minor].
bits)) {
401 lp_release_parport (&lp_table[minor]);
409 #ifdef CONFIG_PARPORT_1284
412 static ssize_t lp_read(
struct file * file,
char __user * buf,
413 size_t count, loff_t *ppos)
416 unsigned int minor=iminor(file->
f_path.dentry->d_inode);
417 struct parport *port = lp_table[minor].dev->port;
419 char *kbuf = lp_table[minor].lp_buffer;
429 lp_claim_parport_or_block (&lp_table[minor]);
433 : lp_table[minor].timeout));
442 while (retval == 0) {
470 if (signal_pending (
current)) {
479 lp_release_parport (&lp_table[minor]);
491 static int lp_open(
struct inode *
inode,
struct file * file)
493 unsigned int minor = iminor(inode);
497 if (minor >=
LP_NO) {
516 lp_claim_parport_or_block (&lp_table[minor]);
517 status =
r_str(minor);
518 lp_release_parport (&lp_table[minor]);
519 if (status & LP_POUTPA) {
524 }
else if (!(status & LP_PSELECD)) {
529 }
else if (!(status & LP_PERRORP)) {
537 if (!lp_table[minor].lp_buffer) {
543 lp_claim_parport_or_block (&lp_table[minor]);
554 lp_release_parport (&lp_table[minor]);
561 static int lp_release(
struct inode * inode,
struct file * file)
563 unsigned int minor = iminor(inode);
565 lp_claim_parport_or_block (&lp_table[minor]);
568 lp_release_parport (&lp_table[minor]);
569 kfree(lp_table[minor].lp_buffer);
570 lp_table[minor].lp_buffer =
NULL;
575 static int lp_do_ioctl(
unsigned int minor,
unsigned int cmd,
576 unsigned long arg,
void __user *
argp)
586 if ((
LP_F(minor) & LP_EXIST) == 0)
599 LP_F(minor) &= ~LP_ABORT;
605 LP_F(minor) &= ~LP_ABORTOPEN;
611 LP_F(minor) &= ~LP_CAREFUL;
625 lp_claim_parport_or_block (&lp_table[minor]);
626 status =
r_str(minor);
627 lp_release_parport (&lp_table[minor]);
638 sizeof(
struct lp_stats)))
641 memset(&LP_STAT(minor), 0,
642 sizeof(
struct lp_stats));
646 status =
LP_F(minor);
657 static int lp_set_timeout(
unsigned int minor,
struct timeval *par_timeout)
662 if ((par_timeout->
tv_sec < 0) ||
668 if (to_jiffies <= 0) {
671 lp_table[minor].timeout = to_jiffies;
675 static long lp_ioctl(
struct file *file,
unsigned int cmd,
682 minor = iminor(file->
f_path.dentry->d_inode);
691 ret = lp_set_timeout(minor, &par_timeout);
694 ret = lp_do_ioctl(minor, cmd, arg, (
void __user *)arg);
703 static long lp_compat_ioctl(
struct file *file,
unsigned int cmd,
710 minor = iminor(file->
f_path.dentry->d_inode);
718 ret = lp_set_timeout(minor, &par_timeout);
727 ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg));
739 .unlocked_ioctl = lp_ioctl,
741 .compat_ioctl = lp_compat_ioctl,
744 .release = lp_release,
745 #ifdef CONFIG_PARPORT_1284
753 #ifdef CONFIG_LP_CONSOLE
760 #define CONSOLE_LP_STRICT 1
764 static void lp_console_write (
struct console *co,
const char *
s,
767 struct pardevice *dev = lp_table[CONSOLE_LP].dev;
783 char *lf =
memchr (s,
'\n', count);
798 if (lf && canwrite <= 0) {
799 const char *crlf =
"\r\n";
808 i -= written, crlf += written;
809 }
while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
811 }
while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
816 static struct console lpcons = {
818 .write = lp_console_write,
836 static int parport_ptr;
847 }
else if (!
strncmp(str,
"parport", 7)) {
849 if (parport_ptr <
LP_NO)
850 parport_nr[parport_ptr++] =
n;
854 }
else if (!
strcmp(str,
"auto")) {
856 }
else if (!
strcmp(str,
"none")) {
858 }
else if (!
strcmp(str,
"reset")) {
865 static int lp_register(
int nr,
struct parport *port)
869 (
void *) &lp_table[nr]);
870 if (lp_table[nr].dev ==
NULL)
883 #ifdef CONFIG_LP_CONSOLE
887 console_registered =
port;
891 CONSOLE_LP, port->
name);
898 static void lp_attach (
struct parport *port)
902 switch (parport_nr[0]) {
908 if (lp_count ==
LP_NO) {
912 if (!lp_register(lp_count, port))
917 for (i = 0; i <
LP_NO; i++) {
918 if (port->
number == parport_nr[i]) {
919 if (!lp_register(i, port))
928 static void lp_detach (
struct parport *port)
931 #ifdef CONFIG_LP_CONSOLE
932 if (console_registered == port) {
934 console_registered =
NULL;
945 static int __init lp_init (
void)
952 for (i = 0; i <
LP_NO; i++) {
953 lp_table[
i].dev =
NULL;
954 lp_table[
i].flags = 0;
958 lp_table[
i].lp_buffer =
NULL;
960 lp_table[
i].lastcall = 0;
961 lp_table[
i].runchars = 0;
962 memset (&lp_table[i].
stats, 0,
sizeof (
struct lp_stats));
964 lp_table[
i].last_error = 0;
968 lp_table[
i].timeout = 10 *
HZ;
971 if (register_chrdev (
LP_MAJOR,
"lp", &lp_fops)) {
977 if (IS_ERR(lp_class)) {
978 err = PTR_ERR(lp_class);
990 #ifndef CONFIG_PARPORT_1284
1005 static int __init lp_init_module (
void)
1009 if (!
strncmp(parport[0],
"auto", 4))
1013 for (n = 0; n < LP_NO && parport[
n]; n++) {
1014 if (!
strncmp(parport[n],
"none", 4))
1019 if (ep != parport[n])
1033 static void lp_cleanup_module (
void)
1039 #ifdef CONFIG_LP_CONSOLE
1044 for (offset = 0; offset <
LP_NO; offset++) {
1045 if (lp_table[offset].dev ==
NULL)