^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/drivers/char/ppdev.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This is the code behind /dev/parport* -- it allows a user-space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * application to use the parport subsystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 1998-2000, 2002 Tim Waugh <tim@cyberelk.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * A /dev/parportx device node represents an arbitrary device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * on port 'x'. The following operations are possible:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * open do nothing, set up default IEEE 1284 protocol to be COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * close release port and unregister device (if necessary)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * ioctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * EXCL register device exclusively (may fail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * CLAIM (register device first time) parport_claim_or_block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * RELEASE parport_release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * SETMODE set the IEEE 1284 protocol to use for read/write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * SETPHASE set the IEEE 1284 phase of a particular mode. Not to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * confused with ioctl(fd, SETPHASER, &stun). ;-)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * DATADIR data_forward / data_reverse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * WDATA write_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * RDATA read_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * WCONTROL write_control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * RCONTROL read_control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * FCONTROL frob_control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * RSTATUS read_status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * NEGOT parport_negotiate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * YIELD parport_yield_blocking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * WCTLONIRQ on interrupt, set control lines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * CLRIRQ clear (and return) interrupt count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * SETTIME sets device timeout (struct timeval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * GETTIME gets device timeout (struct timeval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * GETMODES gets hardware supported modes (unsigned int)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * GETMODE gets the current IEEE1284 mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * GETPHASE gets the current IEEE1284 phase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * GETFLAGS gets current (user-visible) flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * SETFLAGS sets current (user-visible) flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * read/write read or write in current IEEE 1284 protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * select wait for interrupt (in readfds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Changes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Added SETTIME/GETTIME ioctl, Fred Barnes, 1999.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 2000/08/25
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * - On error, copy_from_user and copy_to_user do not return -EFAULT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * They return the positive number of bytes *not* copied due to address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * space errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Added GETMODES/GETMODE/GETPHASE ioctls, Fred Barnes <frmb2@ukc.ac.uk>, 03/01/2001.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Added GETFLAGS/SETFLAGS ioctls, Fred Barnes, 04/2001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <linux/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <linux/parport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #include <linux/ppdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define PP_VERSION "ppdev: user-space parallel port driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define CHRDEV "ppdev"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct pp_struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct pardevice *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) wait_queue_head_t irq_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) atomic_t irqc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int irqresponse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned char irqctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct ieee1284_info state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct ieee1284_info saved_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) long default_inactivity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* should we use PARDEVICE_MAX here? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static struct device *devices[PARPORT_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static DEFINE_IDA(ida_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* pp_struct.flags bitfields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define PP_CLAIMED (1<<0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define PP_EXCL (1<<1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* Other constants */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #define PP_BUFFER_SIZE 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define PARDEVICE_MAX 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static DEFINE_MUTEX(pp_do_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* define fixed sized ioctl cmd for y2038 migration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define PPGETTIME32 _IOR(PP_IOCTL, 0x95, s32[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define PPSETTIME32 _IOW(PP_IOCTL, 0x96, s32[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define PPGETTIME64 _IOR(PP_IOCTL, 0x95, s64[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #define PPSETTIME64 _IOW(PP_IOCTL, 0x96, s64[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static inline void pp_enable_irq(struct pp_struct *pp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct parport *port = pp->pdev->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) port->ops->enable_irq(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static ssize_t pp_read(struct file *file, char __user *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned int minor = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct pp_struct *pp = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) char *kbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ssize_t bytes_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct parport *pport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!(pp->flags & PP_CLAIMED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* Don't have the port claimed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pr_debug(CHRDEV "%x: claim the port first\n", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* Trivial case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (!kbuffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) pport = pp->pdev->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) parport_set_timeout(pp->pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) (file->f_flags & O_NONBLOCK) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) PARPORT_INACTIVITY_O_NONBLOCK :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) pp->default_inactivity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) while (bytes_read == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (mode == IEEE1284_MODE_EPP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* various specials for EPP mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) size_t (*fn)(struct parport *, void *, size_t, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (pp->flags & PP_W91284PIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) flags |= PARPORT_W91284PIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (pp->flags & PP_FASTREAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) flags |= PARPORT_EPP_FAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (pport->ieee1284.mode & IEEE1284_ADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) fn = pport->ops->epp_read_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) fn = pport->ops->epp_read_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) bytes_read = (*fn)(pport, kbuffer, need, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) bytes_read = parport_read(pport, kbuffer, need);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (bytes_read != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) bytes_read = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) bytes_read = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) parport_set_timeout(pp->pdev, pp->default_inactivity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (bytes_read > 0 && copy_to_user(buf, kbuffer, bytes_read))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) bytes_read = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) kfree(kbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) pp_enable_irq(pp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static ssize_t pp_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) unsigned int minor = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct pp_struct *pp = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) char *kbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) ssize_t bytes_written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ssize_t wrote;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct parport *pport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (!(pp->flags & PP_CLAIMED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* Don't have the port claimed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) pr_debug(CHRDEV "%x: claim the port first\n", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (!kbuffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) pport = pp->pdev->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) parport_set_timeout(pp->pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) (file->f_flags & O_NONBLOCK) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) PARPORT_INACTIVITY_O_NONBLOCK :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) pp->default_inactivity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) while (bytes_written < count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (copy_from_user(kbuffer, buf + bytes_written, n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) bytes_written = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /* do a fast EPP write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (pport->ieee1284.mode & IEEE1284_ADDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) wrote = pport->ops->epp_write_addr(pport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) kbuffer, n, PARPORT_EPP_FAST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) wrote = pport->ops->epp_write_data(pport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) kbuffer, n, PARPORT_EPP_FAST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) wrote = parport_write(pp->pdev->port, kbuffer, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (wrote <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (!bytes_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) bytes_written = wrote;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) bytes_written += wrote;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (!bytes_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) bytes_written = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) parport_set_timeout(pp->pdev, pp->default_inactivity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) kfree(kbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) pp_enable_irq(pp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return bytes_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static void pp_irq(void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct pp_struct *pp = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (pp->irqresponse) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) parport_write_control(pp->pdev->port, pp->irqctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) pp->irqresponse = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) atomic_inc(&pp->irqc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) wake_up_interruptible(&pp->irq_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int register_device(int minor, struct pp_struct *pp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct parport *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct pardevice *pdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct pardev_cb ppdev_cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) int rc = 0, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) name = kasprintf(GFP_KERNEL, CHRDEV "%x", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) port = parport_find_number(minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (!port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) pr_warn("%s: no associated port!\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) rc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) index = ida_simple_get(&ida_index, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) memset(&ppdev_cb, 0, sizeof(ppdev_cb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ppdev_cb.irq_func = pp_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) ppdev_cb.flags = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) ppdev_cb.private = pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) pdev = parport_register_dev_model(port, name, &ppdev_cb, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) parport_put_port(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (!pdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) pr_warn("%s: failed to register device!\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) rc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ida_simple_remove(&ida_index, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) pp->pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) pp->index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) dev_dbg(&pdev->dev, "registered pardevice\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static enum ieee1284_phase init_phase(int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) switch (mode & ~(IEEE1284_DEVICEID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) | IEEE1284_ADDR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) case IEEE1284_MODE_NIBBLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) case IEEE1284_MODE_BYTE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return IEEE1284_PH_REV_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return IEEE1284_PH_FWD_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static int pp_set_timeout(struct pardevice *pdev, long tv_sec, int tv_usec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) long to_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if ((tv_sec < 0) || (tv_usec < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) to_jiffies = usecs_to_jiffies(tv_usec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) to_jiffies += tv_sec * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (to_jiffies <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) pdev->timeout = to_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) unsigned int minor = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct pp_struct *pp = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct parport *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct ieee1284_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) unsigned char reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) unsigned char mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) int mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) s32 time32[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) s64 time64[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct timespec64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /* First handle the cases that don't take arguments. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) case PPCLAIM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (pp->flags & PP_CLAIMED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) dev_dbg(&pp->pdev->dev, "you've already got it!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /* Deferred device registration. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (!pp->pdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int err = register_device(minor, pp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ret = parport_claim_or_block(pp->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) pp->flags |= PP_CLAIMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) /* For interrupt-reporting to work, we need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * informed of each interrupt. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) pp_enable_irq(pp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /* We may need to fix up the state machine. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) info = &pp->pdev->port->ieee1284;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) pp->saved_state.mode = info->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) pp->saved_state.phase = info->phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) info->mode = pp->state.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) info->phase = pp->state.phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) pp->default_inactivity = parport_set_timeout(pp->pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) parport_set_timeout(pp->pdev, pp->default_inactivity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) case PPEXCL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (pp->pdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) dev_dbg(&pp->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) "too late for PPEXCL; already registered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (pp->flags & PP_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /* But it's not really an error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) /* There's no chance of making the driver happy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* Just remember to register the device exclusively
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * when we finally do the registration. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) pp->flags |= PP_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) case PPSETMODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) int mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (copy_from_user(&mode, argp, sizeof(mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /* FIXME: validate mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) pp->state.mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) pp->state.phase = init_phase(mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (pp->flags & PP_CLAIMED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) pp->pdev->port->ieee1284.mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) pp->pdev->port->ieee1284.phase = pp->state.phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) case PPGETMODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) int mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (pp->flags & PP_CLAIMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) mode = pp->pdev->port->ieee1284.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) mode = pp->state.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (copy_to_user(argp, &mode, sizeof(mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) case PPSETPHASE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) int phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (copy_from_user(&phase, argp, sizeof(phase)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* FIXME: validate phase */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) pp->state.phase = phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (pp->flags & PP_CLAIMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) pp->pdev->port->ieee1284.phase = phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) case PPGETPHASE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) int phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (pp->flags & PP_CLAIMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) phase = pp->pdev->port->ieee1284.phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) phase = pp->state.phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (copy_to_user(argp, &phase, sizeof(phase)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) case PPGETMODES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) unsigned int modes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) port = parport_find_number(minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (!port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) modes = port->modes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) parport_put_port(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (copy_to_user(argp, &modes, sizeof(modes)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) case PPSETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int uflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (copy_from_user(&uflags, argp, sizeof(uflags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) pp->flags &= ~PP_FLAGMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) pp->flags |= (uflags & PP_FLAGMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) case PPGETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) int uflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) uflags = pp->flags & PP_FLAGMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (copy_to_user(argp, &uflags, sizeof(uflags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) } /* end switch() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) /* Everything else requires the port to be claimed, so check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * that now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if ((pp->flags & PP_CLAIMED) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) pr_debug(CHRDEV "%x: claim the port first\n", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) port = pp->pdev->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) case PPRSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) reg = parport_read_status(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (copy_to_user(argp, ®, sizeof(reg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) case PPRDATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) reg = parport_read_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (copy_to_user(argp, ®, sizeof(reg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) case PPRCONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) reg = parport_read_control(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (copy_to_user(argp, ®, sizeof(reg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) case PPYIELD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) parport_yield_blocking(pp->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) case PPRELEASE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /* Save the state machine's state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) info = &pp->pdev->port->ieee1284;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) pp->state.mode = info->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) pp->state.phase = info->phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) info->mode = pp->saved_state.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) info->phase = pp->saved_state.phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) parport_release(pp->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) pp->flags &= ~PP_CLAIMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) case PPWCONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (copy_from_user(®, argp, sizeof(reg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) parport_write_control(port, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) case PPWDATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) if (copy_from_user(®, argp, sizeof(reg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) parport_write_data(port, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) case PPFCONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (copy_from_user(&mask, argp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) sizeof(mask)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (copy_from_user(®, 1 + (unsigned char __user *) arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) sizeof(reg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) parport_frob_control(port, mask, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) case PPDATADIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (copy_from_user(&mode, argp, sizeof(mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) port->ops->data_reverse(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) port->ops->data_forward(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) case PPNEGOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (copy_from_user(&mode, argp, sizeof(mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) switch ((ret = parport_negotiate(port, mode))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) case 0: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) case -1: /* handshake failed, peripheral not IEEE 1284 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) case 1: /* handshake succeeded, peripheral rejected mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) ret = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) pp_enable_irq(pp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) case PPWCTLONIRQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (copy_from_user(®, argp, sizeof(reg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) /* Remember what to set the control lines to, for next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * time we get an interrupt. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) pp->irqctl = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) pp->irqresponse = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) case PPCLRIRQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) ret = atomic_read(&pp->irqc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (copy_to_user(argp, &ret, sizeof(ret)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) atomic_sub(ret, &pp->irqc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) case PPSETTIME32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (copy_from_user(time32, argp, sizeof(time32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if ((time32[0] < 0) || (time32[1] < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return pp_set_timeout(pp->pdev, time32[0], time32[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) case PPSETTIME64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (copy_from_user(time64, argp, sizeof(time64)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if ((time64[0] < 0) || (time64[1] < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) time64[1] >>= 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) return pp_set_timeout(pp->pdev, time64[0], time64[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) case PPGETTIME32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) jiffies_to_timespec64(pp->pdev->timeout, &ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) time32[0] = ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) time32[1] = ts.tv_nsec / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (copy_to_user(argp, time32, sizeof(time32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) case PPGETTIME64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) jiffies_to_timespec64(pp->pdev->timeout, &ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) time64[0] = ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) time64[1] = ts.tv_nsec / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) time64[1] <<= 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (copy_to_user(argp, time64, sizeof(time64)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) dev_dbg(&pp->pdev->dev, "What? (cmd=0x%x)\n", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) /* Keep the compiler happy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) mutex_lock(&pp_do_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) ret = pp_do_ioctl(file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) mutex_unlock(&pp_do_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) static int pp_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) unsigned int minor = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) struct pp_struct *pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (minor >= PARPORT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) pp = kmalloc(sizeof(struct pp_struct), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) if (!pp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) pp->state.mode = IEEE1284_MODE_COMPAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) pp->state.phase = init_phase(pp->state.mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) pp->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) pp->irqresponse = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) atomic_set(&pp->irqc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) init_waitqueue_head(&pp->irq_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) /* Defer the actual device registration until the first claim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) * That way, we know whether or not the driver wants to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) * exclusive access to the port (PPEXCL).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) pp->pdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) file->private_data = pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) static int pp_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) unsigned int minor = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) struct pp_struct *pp = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) int compat_negot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) compat_negot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) (pp->state.mode != IEEE1284_MODE_COMPAT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) struct ieee1284_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) /* parport released, but not in compatibility mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) parport_claim_or_block(pp->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) pp->flags |= PP_CLAIMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) info = &pp->pdev->port->ieee1284;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) pp->saved_state.mode = info->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) pp->saved_state.phase = info->phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) info->mode = pp->state.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) info->phase = pp->state.phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) compat_negot = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) compat_negot = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) if (compat_negot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) parport_negotiate(pp->pdev->port, IEEE1284_MODE_COMPAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) dev_dbg(&pp->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) "negotiated back to compatibility mode because user-space forgot\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) if ((pp->flags & PP_CLAIMED) && pp->pdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) struct ieee1284_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) info = &pp->pdev->port->ieee1284;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) pp->state.mode = info->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) pp->state.phase = info->phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) info->mode = pp->saved_state.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) info->phase = pp->saved_state.phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) parport_release(pp->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (compat_negot != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) pr_debug(CHRDEV "%x: released pardevice "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) "because user-space forgot\n", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (pp->pdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) parport_unregister_device(pp->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) ida_simple_remove(&ida_index, pp->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) pp->pdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) kfree(pp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) /* No kernel lock held - fine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) static __poll_t pp_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) struct pp_struct *pp = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) __poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) poll_wait(file, &pp->irq_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) if (atomic_read(&pp->irqc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) static struct class *ppdev_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) static const struct file_operations pp_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) .read = pp_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) .write = pp_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) .poll = pp_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) .unlocked_ioctl = pp_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) .open = pp_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) .release = pp_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) static void pp_attach(struct parport *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) struct device *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) if (devices[port->number])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) ret = device_create(ppdev_class, port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) MKDEV(PP_MAJOR, port->number), NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) "parport%d", port->number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) if (IS_ERR(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) pr_err("Failed to create device parport%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) port->number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) devices[port->number] = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) static void pp_detach(struct parport *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) if (!devices[port->number])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) devices[port->number] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) static int pp_probe(struct pardevice *par_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) struct device_driver *drv = par_dev->dev.driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) int len = strlen(drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if (strncmp(par_dev->name, drv->name, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) static struct parport_driver pp_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) .name = CHRDEV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) .probe = pp_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) .match_port = pp_attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) .detach = pp_detach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) .devmodel = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) static int __init ppdev_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) if (register_chrdev(PP_MAJOR, CHRDEV, &pp_fops)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) pr_warn(CHRDEV ": unable to get major %d\n", PP_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) ppdev_class = class_create(THIS_MODULE, CHRDEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (IS_ERR(ppdev_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) err = PTR_ERR(ppdev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) goto out_chrdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) err = parport_register_driver(&pp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) pr_warn(CHRDEV ": unable to register with parport\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) goto out_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) pr_info(PP_VERSION "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) out_class:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) class_destroy(ppdev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) out_chrdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) unregister_chrdev(PP_MAJOR, CHRDEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) static void __exit ppdev_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) /* Clean up all parport stuff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) parport_unregister_driver(&pp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) class_destroy(ppdev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) unregister_chrdev(PP_MAJOR, CHRDEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) module_init(ppdev_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) module_exit(ppdev_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);