2019-05-27 09:55:01 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
2005-04-17 02:20:36 +04:00
/*
* linux / drivers / char / ppdev . c
*
* This is the code behind / dev / parport * - - it allows a user - space
* application to use the parport subsystem .
*
* Copyright ( C ) 1998 - 2000 , 2002 Tim Waugh < tim @ cyberelk . net >
*
* A / dev / parportx device node represents an arbitrary device
* on port ' x ' . The following operations are possible :
*
* open do nothing , set up default IEEE 1284 protocol to be COMPAT
* close release port and unregister device ( if necessary )
* ioctl
* EXCL register device exclusively ( may fail )
* CLAIM ( register device first time ) parport_claim_or_block
* RELEASE parport_release
* SETMODE set the IEEE 1284 protocol to use for read / write
* SETPHASE set the IEEE 1284 phase of a particular mode . Not to be
* confused with ioctl ( fd , SETPHASER , & stun ) . ; - )
* DATADIR data_forward / data_reverse
* WDATA write_data
* RDATA read_data
* WCONTROL write_control
* RCONTROL read_control
* FCONTROL frob_control
* RSTATUS read_status
* NEGOT parport_negotiate
* YIELD parport_yield_blocking
* WCTLONIRQ on interrupt , set control lines
* CLRIRQ clear ( and return ) interrupt count
* SETTIME sets device timeout ( struct timeval )
* GETTIME gets device timeout ( struct timeval )
* GETMODES gets hardware supported modes ( unsigned int )
* GETMODE gets the current IEEE1284 mode
* GETPHASE gets the current IEEE1284 phase
* GETFLAGS gets current ( user - visible ) flags
* SETFLAGS sets current ( user - visible ) flags
* read / write read or write in current IEEE 1284 protocol
* select wait for interrupt ( in readfds )
*
* Changes :
* Added SETTIME / GETTIME ioctl , Fred Barnes , 1999.
*
* Arnaldo Carvalho de Melo < acme @ conectiva . com . br > 2000 / 08 / 25
* - On error , copy_from_user and copy_to_user do not return - EFAULT ,
* They return the positive number of bytes * not * copied due to address
* space errors .
*
* Added GETMODES / GETMODE / GETPHASE ioctls , Fred Barnes < frmb2 @ ukc . ac . uk > , 03 / 01 / 2001.
* Added GETFLAGS / SETFLAGS ioctls , Fred Barnes , 04 / 2001
*/
# include <linux/module.h>
# include <linux/init.h>
2017-02-02 21:15:33 +03:00
# include <linux/sched/signal.h>
2005-04-17 02:20:36 +04:00
# include <linux/device.h>
# include <linux/ioctl.h>
# include <linux/parport.h>
# include <linux/ctype.h>
# include <linux/poll.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2006-03-25 14:07:13 +03:00
# include <linux/major.h>
2005-04-17 02:20:36 +04:00
# include <linux/ppdev.h>
2010-06-02 16:28:52 +04:00
# include <linux/mutex.h>
2008-07-25 12:48:16 +04:00
# include <linux/uaccess.h>
2016-01-08 10:50:49 +03:00
# include <linux/compat.h>
2005-04-17 02:20:36 +04:00
# define PP_VERSION "ppdev: user-space parallel port driver"
# define CHRDEV "ppdev"
struct pp_struct {
2016-02-12 16:03:40 +03:00
struct pardevice * pdev ;
2005-04-17 02:20:36 +04:00
wait_queue_head_t irq_wait ;
atomic_t irqc ;
unsigned int flags ;
int irqresponse ;
unsigned char irqctl ;
struct ieee1284_info state ;
struct ieee1284_info saved_state ;
long default_inactivity ;
2017-03-07 02:23:43 +03:00
int index ;
2005-04-17 02:20:36 +04:00
} ;
2016-11-13 00:22:12 +03:00
/* should we use PARDEVICE_MAX here? */
static struct device * devices [ PARPORT_MAX ] ;
2017-03-07 02:23:43 +03:00
static DEFINE_IDA ( ida_index ) ;
2005-04-17 02:20:36 +04:00
/* pp_struct.flags bitfields */
# define PP_CLAIMED (1<<0)
# define PP_EXCL (1<<1)
/* Other constants */
# define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
# define PP_BUFFER_SIZE 1024
# define PARDEVICE_MAX 8
2010-06-02 16:28:52 +04:00
static DEFINE_MUTEX ( pp_do_mutex ) ;
2016-01-08 10:50:48 +03:00
/* define fixed sized ioctl cmd for y2038 migration */
# define PPGETTIME32 _IOR(PP_IOCTL, 0x95, s32[2])
# define PPSETTIME32 _IOW(PP_IOCTL, 0x96, s32[2])
# define PPGETTIME64 _IOR(PP_IOCTL, 0x95, s64[2])
# define PPSETTIME64 _IOW(PP_IOCTL, 0x96, s64[2])
2016-02-12 16:03:39 +03:00
static inline void pp_enable_irq ( struct pp_struct * pp )
2005-04-17 02:20:36 +04:00
{
struct parport * port = pp - > pdev - > port ;
2016-02-12 16:03:41 +03:00
2016-02-12 16:03:39 +03:00
port - > ops - > enable_irq ( port ) ;
2005-04-17 02:20:36 +04:00
}
2016-02-12 16:03:40 +03:00
static ssize_t pp_read ( struct file * file , char __user * buf , size_t count ,
2016-02-12 16:03:43 +03:00
loff_t * ppos )
2005-04-17 02:20:36 +04:00
{
2013-01-24 02:07:38 +04:00
unsigned int minor = iminor ( file_inode ( file ) ) ;
2005-04-17 02:20:36 +04:00
struct pp_struct * pp = file - > private_data ;
2016-02-12 16:03:40 +03:00
char * kbuffer ;
2005-04-17 02:20:36 +04:00
ssize_t bytes_read = 0 ;
struct parport * pport ;
int mode ;
if ( ! ( pp - > flags & PP_CLAIMED ) ) {
/* Don't have the port claimed */
2009-06-18 03:27:49 +04:00
pr_debug ( CHRDEV " %x: claim the port first \n " , minor ) ;
2005-04-17 02:20:36 +04:00
return - EINVAL ;
}
/* Trivial case. */
if ( count = = 0 )
return 0 ;
kbuffer = kmalloc ( min_t ( size_t , count , PP_BUFFER_SIZE ) , GFP_KERNEL ) ;
2016-02-12 16:03:42 +03:00
if ( ! kbuffer )
2005-04-17 02:20:36 +04:00
return - ENOMEM ;
pport = pp - > pdev - > port ;
mode = pport - > ieee1284 . mode & ~ ( IEEE1284_DEVICEID | IEEE1284_ADDR ) ;
2016-02-12 16:03:39 +03:00
parport_set_timeout ( pp - > pdev ,
2016-02-12 16:03:43 +03:00
( file - > f_flags & O_NONBLOCK ) ?
PARPORT_INACTIVITY_O_NONBLOCK :
pp - > default_inactivity ) ;
2005-04-17 02:20:36 +04:00
while ( bytes_read = = 0 ) {
ssize_t need = min_t ( unsigned long , count , PP_BUFFER_SIZE ) ;
if ( mode = = IEEE1284_MODE_EPP ) {
/* various specials for EPP mode */
int flags = 0 ;
size_t ( * fn ) ( struct parport * , void * , size_t , int ) ;
2016-02-12 16:03:42 +03:00
if ( pp - > flags & PP_W91284PIC )
2005-04-17 02:20:36 +04:00
flags | = PARPORT_W91284PIC ;
2016-02-12 16:03:42 +03:00
if ( pp - > flags & PP_FASTREAD )
2005-04-17 02:20:36 +04:00
flags | = PARPORT_EPP_FAST ;
2016-02-12 16:03:42 +03:00
if ( pport - > ieee1284 . mode & IEEE1284_ADDR )
2005-04-17 02:20:36 +04:00
fn = pport - > ops - > epp_read_addr ;
2016-02-12 16:03:42 +03:00
else
2005-04-17 02:20:36 +04:00
fn = pport - > ops - > epp_read_data ;
bytes_read = ( * fn ) ( pport , kbuffer , need , flags ) ;
} else {
2016-02-12 16:03:39 +03:00
bytes_read = parport_read ( pport , kbuffer , need ) ;
2005-04-17 02:20:36 +04:00
}
if ( bytes_read ! = 0 )
break ;
if ( file - > f_flags & O_NONBLOCK ) {
bytes_read = - EAGAIN ;
break ;
}
2016-02-12 16:03:39 +03:00
if ( signal_pending ( current ) ) {
2005-04-17 02:20:36 +04:00
bytes_read = - ERESTARTSYS ;
break ;
}
cond_resched ( ) ;
}
2016-02-12 16:03:39 +03:00
parport_set_timeout ( pp - > pdev , pp - > default_inactivity ) ;
2005-04-17 02:20:36 +04:00
2016-02-12 16:03:39 +03:00
if ( bytes_read > 0 & & copy_to_user ( buf , kbuffer , bytes_read ) )
2005-04-17 02:20:36 +04:00
bytes_read = - EFAULT ;
2016-02-12 16:03:39 +03:00
kfree ( kbuffer ) ;
pp_enable_irq ( pp ) ;
2005-04-17 02:20:36 +04:00
return bytes_read ;
}
2016-02-12 16:03:40 +03:00
static ssize_t pp_write ( struct file * file , const char __user * buf ,
2016-02-12 16:03:43 +03:00
size_t count , loff_t * ppos )
2005-04-17 02:20:36 +04:00
{
2013-01-24 02:07:38 +04:00
unsigned int minor = iminor ( file_inode ( file ) ) ;
2005-04-17 02:20:36 +04:00
struct pp_struct * pp = file - > private_data ;
2016-02-12 16:03:40 +03:00
char * kbuffer ;
2005-04-17 02:20:36 +04:00
ssize_t bytes_written = 0 ;
ssize_t wrote ;
int mode ;
struct parport * pport ;
if ( ! ( pp - > flags & PP_CLAIMED ) ) {
/* Don't have the port claimed */
2009-06-18 03:27:49 +04:00
pr_debug ( CHRDEV " %x: claim the port first \n " , minor ) ;
2005-04-17 02:20:36 +04:00
return - EINVAL ;
}
kbuffer = kmalloc ( min_t ( size_t , count , PP_BUFFER_SIZE ) , GFP_KERNEL ) ;
2016-02-12 16:03:42 +03:00
if ( ! kbuffer )
2005-04-17 02:20:36 +04:00
return - ENOMEM ;
2016-02-12 16:03:42 +03:00
2005-04-17 02:20:36 +04:00
pport = pp - > pdev - > port ;
mode = pport - > ieee1284 . mode & ~ ( IEEE1284_DEVICEID | IEEE1284_ADDR ) ;
2016-02-12 16:03:39 +03:00
parport_set_timeout ( pp - > pdev ,
2016-02-12 16:03:43 +03:00
( file - > f_flags & O_NONBLOCK ) ?
PARPORT_INACTIVITY_O_NONBLOCK :
pp - > default_inactivity ) ;
2005-04-17 02:20:36 +04:00
while ( bytes_written < count ) {
ssize_t n = min_t ( unsigned long , count - bytes_written , PP_BUFFER_SIZE ) ;
2016-02-12 16:03:39 +03:00
if ( copy_from_user ( kbuffer , buf + bytes_written , n ) ) {
2005-04-17 02:20:36 +04:00
bytes_written = - EFAULT ;
break ;
}
if ( ( pp - > flags & PP_FASTWRITE ) & & ( mode = = IEEE1284_MODE_EPP ) ) {
/* do a fast EPP write */
if ( pport - > ieee1284 . mode & IEEE1284_ADDR ) {
2016-02-12 16:03:39 +03:00
wrote = pport - > ops - > epp_write_addr ( pport ,
2005-04-17 02:20:36 +04:00
kbuffer , n , PARPORT_EPP_FAST ) ;
} else {
2016-02-12 16:03:39 +03:00
wrote = pport - > ops - > epp_write_data ( pport ,
2005-04-17 02:20:36 +04:00
kbuffer , n , PARPORT_EPP_FAST ) ;
}
} else {
2016-02-12 16:03:39 +03:00
wrote = parport_write ( pp - > pdev - > port , kbuffer , n ) ;
2005-04-17 02:20:36 +04:00
}
if ( wrote < = 0 ) {
2016-02-12 16:03:42 +03:00
if ( ! bytes_written )
2005-04-17 02:20:36 +04:00
bytes_written = wrote ;
break ;
}
bytes_written + = wrote ;
if ( file - > f_flags & O_NONBLOCK ) {
if ( ! bytes_written )
bytes_written = - EAGAIN ;
break ;
}
2016-02-12 16:03:39 +03:00
if ( signal_pending ( current ) )
2005-04-17 02:20:36 +04:00
break ;
cond_resched ( ) ;
}
2016-02-12 16:03:39 +03:00
parport_set_timeout ( pp - > pdev , pp - > default_inactivity ) ;
2005-04-17 02:20:36 +04:00
2016-02-12 16:03:39 +03:00
kfree ( kbuffer ) ;
pp_enable_irq ( pp ) ;
2005-04-17 02:20:36 +04:00
return bytes_written ;
}
2016-02-12 16:03:39 +03:00
static void pp_irq ( void * private )
2005-04-17 02:20:36 +04:00
{
2007-10-19 10:54:26 +04:00
struct pp_struct * pp = private ;
2005-04-17 02:20:36 +04:00
if ( pp - > irqresponse ) {
2016-02-12 16:03:39 +03:00
parport_write_control ( pp - > pdev - > port , pp - > irqctl ) ;
2005-04-17 02:20:36 +04:00
pp - > irqresponse = 0 ;
}
2016-02-12 16:03:39 +03:00
atomic_inc ( & pp - > irqc ) ;
wake_up_interruptible ( & pp - > irq_wait ) ;
2005-04-17 02:20:36 +04:00
}
2016-02-12 16:03:39 +03:00
static int register_device ( int minor , struct pp_struct * pp )
2005-04-17 02:20:36 +04:00
{
struct parport * port ;
2016-02-12 16:03:40 +03:00
struct pardevice * pdev = NULL ;
2005-04-17 02:20:36 +04:00
char * name ;
2016-06-01 15:20:08 +03:00
struct pardev_cb ppdev_cb ;
2017-03-07 02:23:43 +03:00
int rc = 0 , index ;
2005-04-17 02:20:36 +04:00
2010-05-27 01:43:55 +04:00
name = kasprintf ( GFP_KERNEL , CHRDEV " %x " , minor ) ;
2005-04-17 02:20:36 +04:00
if ( name = = NULL )
return - ENOMEM ;
2016-02-12 16:03:39 +03:00
port = parport_find_number ( minor ) ;
2005-04-17 02:20:36 +04:00
if ( ! port ) {
2016-11-11 01:12:01 +03:00
pr_warn ( " %s: no associated port! \n " , name ) ;
2016-12-02 19:23:55 +03:00
rc = - ENXIO ;
goto err ;
2005-04-17 02:20:36 +04:00
}
2017-03-07 02:23:43 +03:00
index = ida_simple_get ( & ida_index , 0 , 0 , GFP_KERNEL ) ;
2016-06-01 15:20:08 +03:00
memset ( & ppdev_cb , 0 , sizeof ( ppdev_cb ) ) ;
ppdev_cb . irq_func = pp_irq ;
ppdev_cb . flags = ( pp - > flags & PP_EXCL ) ? PARPORT_FLAG_EXCL : 0 ;
ppdev_cb . private = pp ;
2017-03-07 02:23:43 +03:00
pdev = parport_register_dev_model ( port , name , & ppdev_cb , index ) ;
2016-02-12 16:03:39 +03:00
parport_put_port ( port ) ;
2005-04-17 02:20:36 +04:00
if ( ! pdev ) {
2016-11-11 01:12:01 +03:00
pr_warn ( " %s: failed to register device! \n " , name ) ;
2016-12-02 19:23:55 +03:00
rc = - ENXIO ;
2017-03-07 02:23:43 +03:00
ida_simple_remove ( & ida_index , index ) ;
2016-12-02 19:23:55 +03:00
goto err ;
2005-04-17 02:20:36 +04:00
}
pp - > pdev = pdev ;
2017-03-07 02:23:43 +03:00
pp - > index = index ;
2016-02-12 16:03:46 +03:00
dev_dbg ( & pdev - > dev , " registered pardevice \n " ) ;
2016-12-02 19:23:55 +03:00
err :
kfree ( name ) ;
return rc ;
2005-04-17 02:20:36 +04:00
}
2016-02-12 16:03:39 +03:00
static enum ieee1284_phase init_phase ( int mode )
2005-04-17 02:20:36 +04:00
{
switch ( mode & ~ ( IEEE1284_DEVICEID
| IEEE1284_ADDR ) ) {
case IEEE1284_MODE_NIBBLE :
case IEEE1284_MODE_BYTE :
return IEEE1284_PH_REV_IDLE ;
}
return IEEE1284_PH_FWD_IDLE ;
}
2016-01-08 10:50:48 +03:00
static int pp_set_timeout ( struct pardevice * pdev , long tv_sec , int tv_usec )
{
long to_jiffies ;
if ( ( tv_sec < 0 ) | | ( tv_usec < 0 ) )
return - EINVAL ;
to_jiffies = usecs_to_jiffies ( tv_usec ) ;
to_jiffies + = tv_sec * HZ ;
if ( to_jiffies < = 0 )
return - EINVAL ;
pdev - > timeout = to_jiffies ;
return 0 ;
}
2008-07-25 12:48:16 +04:00
static int pp_do_ioctl ( struct file * file , unsigned int cmd , unsigned long arg )
2005-04-17 02:20:36 +04:00
{
2013-01-24 02:07:38 +04:00
unsigned int minor = iminor ( file_inode ( file ) ) ;
2005-04-17 02:20:36 +04:00
struct pp_struct * pp = file - > private_data ;
2016-02-12 16:03:40 +03:00
struct parport * port ;
2005-04-17 02:20:36 +04:00
void __user * argp = ( void __user * ) arg ;
2020-02-20 09:23:11 +03:00
struct ieee1284_info * info ;
unsigned char reg ;
unsigned char mask ;
int mode ;
s32 time32 [ 2 ] ;
s64 time64 [ 2 ] ;
struct timespec64 ts ;
int ret ;
2005-04-17 02:20:36 +04:00
/* First handle the cases that don't take arguments. */
switch ( cmd ) {
case PPCLAIM :
{
if ( pp - > flags & PP_CLAIMED ) {
2016-02-12 16:03:46 +03:00
dev_dbg ( & pp - > pdev - > dev , " you've already got it! \n " ) ;
2005-04-17 02:20:36 +04:00
return - EINVAL ;
}
/* Deferred device registration. */
if ( ! pp - > pdev ) {
2016-02-12 16:03:39 +03:00
int err = register_device ( minor , pp ) ;
2016-02-12 16:03:41 +03:00
2016-02-12 16:03:42 +03:00
if ( err )
2005-04-17 02:20:36 +04:00
return err ;
}
2016-02-12 16:03:39 +03:00
ret = parport_claim_or_block ( pp - > pdev ) ;
2005-04-17 02:20:36 +04:00
if ( ret < 0 )
return ret ;
pp - > flags | = PP_CLAIMED ;
/* For interrupt-reporting to work, we need to be
* informed of each interrupt . */
2016-02-12 16:03:39 +03:00
pp_enable_irq ( pp ) ;
2005-04-17 02:20:36 +04:00
/* We may need to fix up the state machine. */
info = & pp - > pdev - > port - > ieee1284 ;
pp - > saved_state . mode = info - > mode ;
pp - > saved_state . phase = info - > phase ;
info - > mode = pp - > state . mode ;
info - > phase = pp - > state . phase ;
2016-02-12 16:03:39 +03:00
pp - > default_inactivity = parport_set_timeout ( pp - > pdev , 0 ) ;
parport_set_timeout ( pp - > pdev , pp - > default_inactivity ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
}
case PPEXCL :
if ( pp - > pdev ) {
2016-02-12 16:03:46 +03:00
dev_dbg ( & pp - > pdev - > dev ,
" too late for PPEXCL; already registered \n " ) ;
2005-04-17 02:20:36 +04:00
if ( pp - > flags & PP_EXCL )
/* But it's not really an error. */
return 0 ;
/* There's no chance of making the driver happy. */
return - EINVAL ;
}
/* Just remember to register the device exclusively
* when we finally do the registration . */
pp - > flags | = PP_EXCL ;
return 0 ;
case PPSETMODE :
{
int mode ;
2016-02-12 16:03:41 +03:00
2016-02-12 16:03:39 +03:00
if ( copy_from_user ( & mode , argp , sizeof ( mode ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
/* FIXME: validate mode */
pp - > state . mode = mode ;
2016-02-12 16:03:39 +03:00
pp - > state . phase = init_phase ( mode ) ;
2005-04-17 02:20:36 +04:00
if ( pp - > flags & PP_CLAIMED ) {
pp - > pdev - > port - > ieee1284 . mode = mode ;
pp - > pdev - > port - > ieee1284 . phase = pp - > state . phase ;
}
return 0 ;
}
case PPGETMODE :
{
int mode ;
2016-02-12 16:03:42 +03:00
if ( pp - > flags & PP_CLAIMED )
2005-04-17 02:20:36 +04:00
mode = pp - > pdev - > port - > ieee1284 . mode ;
2016-02-12 16:03:42 +03:00
else
2005-04-17 02:20:36 +04:00
mode = pp - > state . mode ;
2016-02-12 16:03:42 +03:00
if ( copy_to_user ( argp , & mode , sizeof ( mode ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
return 0 ;
}
case PPSETPHASE :
{
int phase ;
2016-02-12 16:03:41 +03:00
2016-02-12 16:03:42 +03:00
if ( copy_from_user ( & phase , argp , sizeof ( phase ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
2016-02-12 16:03:42 +03:00
2005-04-17 02:20:36 +04:00
/* FIXME: validate phase */
pp - > state . phase = phase ;
2016-02-12 16:03:42 +03:00
if ( pp - > flags & PP_CLAIMED )
2005-04-17 02:20:36 +04:00
pp - > pdev - > port - > ieee1284 . phase = phase ;
return 0 ;
}
case PPGETPHASE :
{
int phase ;
2016-02-12 16:03:42 +03:00
if ( pp - > flags & PP_CLAIMED )
2005-04-17 02:20:36 +04:00
phase = pp - > pdev - > port - > ieee1284 . phase ;
2016-02-12 16:03:42 +03:00
else
2005-04-17 02:20:36 +04:00
phase = pp - > state . phase ;
2016-02-12 16:03:42 +03:00
if ( copy_to_user ( argp , & phase , sizeof ( phase ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
return 0 ;
}
case PPGETMODES :
{
unsigned int modes ;
2016-02-12 16:03:39 +03:00
port = parport_find_number ( minor ) ;
2005-04-17 02:20:36 +04:00
if ( ! port )
return - ENODEV ;
modes = port - > modes ;
2011-05-27 03:25:59 +04:00
parport_put_port ( port ) ;
2016-02-12 16:03:42 +03:00
if ( copy_to_user ( argp , & modes , sizeof ( modes ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
return 0 ;
}
case PPSETFLAGS :
{
int uflags ;
2016-02-12 16:03:42 +03:00
if ( copy_from_user ( & uflags , argp , sizeof ( uflags ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
pp - > flags & = ~ PP_FLAGMASK ;
pp - > flags | = ( uflags & PP_FLAGMASK ) ;
return 0 ;
}
case PPGETFLAGS :
{
int uflags ;
uflags = pp - > flags & PP_FLAGMASK ;
2016-02-12 16:03:42 +03:00
if ( copy_to_user ( argp , & uflags , sizeof ( uflags ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
return 0 ;
}
} /* end switch() */
/* Everything else requires the port to be claimed, so check
* that now . */
if ( ( pp - > flags & PP_CLAIMED ) = = 0 ) {
2009-06-18 03:27:49 +04:00
pr_debug ( CHRDEV " %x: claim the port first \n " , minor ) ;
2005-04-17 02:20:36 +04:00
return - EINVAL ;
}
port = pp - > pdev - > port ;
switch ( cmd ) {
case PPRSTATUS :
2016-02-12 16:03:39 +03:00
reg = parport_read_status ( port ) ;
if ( copy_to_user ( argp , & reg , sizeof ( reg ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
return 0 ;
case PPRDATA :
2016-02-12 16:03:39 +03:00
reg = parport_read_data ( port ) ;
if ( copy_to_user ( argp , & reg , sizeof ( reg ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
return 0 ;
case PPRCONTROL :
2016-02-12 16:03:39 +03:00
reg = parport_read_control ( port ) ;
if ( copy_to_user ( argp , & reg , sizeof ( reg ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
return 0 ;
case PPYIELD :
2016-02-12 16:03:39 +03:00
parport_yield_blocking ( pp - > pdev ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
case PPRELEASE :
/* Save the state machine's state. */
info = & pp - > pdev - > port - > ieee1284 ;
pp - > state . mode = info - > mode ;
pp - > state . phase = info - > phase ;
info - > mode = pp - > saved_state . mode ;
info - > phase = pp - > saved_state . phase ;
2016-02-12 16:03:39 +03:00
parport_release ( pp - > pdev ) ;
2005-04-17 02:20:36 +04:00
pp - > flags & = ~ PP_CLAIMED ;
return 0 ;
case PPWCONTROL :
2016-02-12 16:03:39 +03:00
if ( copy_from_user ( & reg , argp , sizeof ( reg ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
2016-02-12 16:03:39 +03:00
parport_write_control ( port , reg ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
case PPWDATA :
2016-02-12 16:03:39 +03:00
if ( copy_from_user ( & reg , argp , sizeof ( reg ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
2016-02-12 16:03:39 +03:00
parport_write_data ( port , reg ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
case PPFCONTROL :
2016-02-12 16:03:39 +03:00
if ( copy_from_user ( & mask , argp ,
2016-02-12 16:03:43 +03:00
sizeof ( mask ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
2016-02-12 16:03:39 +03:00
if ( copy_from_user ( & reg , 1 + ( unsigned char __user * ) arg ,
2016-02-12 16:03:43 +03:00
sizeof ( reg ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
2016-02-12 16:03:39 +03:00
parport_frob_control ( port , mask , reg ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
case PPDATADIR :
2016-02-12 16:03:39 +03:00
if ( copy_from_user ( & mode , argp , sizeof ( mode ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
if ( mode )
2016-02-12 16:03:39 +03:00
port - > ops - > data_reverse ( port ) ;
2005-04-17 02:20:36 +04:00
else
2016-02-12 16:03:39 +03:00
port - > ops - > data_forward ( port ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
case PPNEGOT :
2016-02-12 16:03:39 +03:00
if ( copy_from_user ( & mode , argp , sizeof ( mode ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
2016-02-12 16:03:39 +03:00
switch ( ( ret = parport_negotiate ( port , mode ) ) ) {
2005-04-17 02:20:36 +04:00
case 0 : break ;
case - 1 : /* handshake failed, peripheral not IEEE 1284 */
ret = - EIO ;
break ;
case 1 : /* handshake succeeded, peripheral rejected mode */
ret = - ENXIO ;
break ;
}
2016-02-12 16:03:39 +03:00
pp_enable_irq ( pp ) ;
2005-04-17 02:20:36 +04:00
return ret ;
case PPWCTLONIRQ :
2016-02-12 16:03:39 +03:00
if ( copy_from_user ( & reg , argp , sizeof ( reg ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
/* Remember what to set the control lines to, for next
* time we get an interrupt . */
pp - > irqctl = reg ;
pp - > irqresponse = 1 ;
return 0 ;
case PPCLRIRQ :
2016-02-12 16:03:39 +03:00
ret = atomic_read ( & pp - > irqc ) ;
if ( copy_to_user ( argp , & ret , sizeof ( ret ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
2016-02-12 16:03:39 +03:00
atomic_sub ( ret , & pp - > irqc ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
2016-01-08 10:50:48 +03:00
case PPSETTIME32 :
if ( copy_from_user ( time32 , argp , sizeof ( time32 ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
2016-01-08 10:50:48 +03:00
2019-11-08 23:34:30 +03:00
if ( ( time32 [ 0 ] < 0 ) | | ( time32 [ 1 ] < 0 ) )
return - EINVAL ;
2016-01-08 10:50:48 +03:00
return pp_set_timeout ( pp - > pdev , time32 [ 0 ] , time32 [ 1 ] ) ;
case PPSETTIME64 :
if ( copy_from_user ( time64 , argp , sizeof ( time64 ) ) )
return - EFAULT ;
2019-11-08 23:34:30 +03:00
if ( ( time64 [ 0 ] < 0 ) | | ( time64 [ 1 ] < 0 ) )
return - EINVAL ;
if ( IS_ENABLED ( CONFIG_SPARC64 ) & & ! in_compat_syscall ( ) )
time64 [ 1 ] > > = 32 ;
2016-01-08 10:50:48 +03:00
return pp_set_timeout ( pp - > pdev , time64 [ 0 ] , time64 [ 1 ] ) ;
case PPGETTIME32 :
jiffies_to_timespec64 ( pp - > pdev - > timeout , & ts ) ;
time32 [ 0 ] = ts . tv_sec ;
time32 [ 1 ] = ts . tv_nsec / NSEC_PER_USEC ;
if ( copy_to_user ( argp , time32 , sizeof ( time32 ) ) )
return - EFAULT ;
2005-04-17 02:20:36 +04:00
return 0 ;
2016-01-08 10:50:48 +03:00
case PPGETTIME64 :
jiffies_to_timespec64 ( pp - > pdev - > timeout , & ts ) ;
time64 [ 0 ] = ts . tv_sec ;
time64 [ 1 ] = ts . tv_nsec / NSEC_PER_USEC ;
2019-11-08 23:34:30 +03:00
if ( IS_ENABLED ( CONFIG_SPARC64 ) & & ! in_compat_syscall ( ) )
time64 [ 1 ] < < = 32 ;
2016-01-08 10:50:48 +03:00
if ( copy_to_user ( argp , time64 , sizeof ( time64 ) ) )
2005-04-17 02:20:36 +04:00
return - EFAULT ;
2016-01-08 10:50:48 +03:00
2005-04-17 02:20:36 +04:00
return 0 ;
default :
2016-02-12 16:03:46 +03:00
dev_dbg ( & pp - > pdev - > dev , " What? (cmd=0x%x) \n " , cmd ) ;
2005-04-17 02:20:36 +04:00
return - EINVAL ;
}
/* Keep the compiler happy */
return 0 ;
}
2008-07-25 12:48:16 +04:00
static long pp_ioctl ( struct file * file , unsigned int cmd , unsigned long arg )
{
long ret ;
2016-02-12 16:03:41 +03:00
2010-06-02 16:28:52 +04:00
mutex_lock ( & pp_do_mutex ) ;
2008-07-25 12:48:16 +04:00
ret = pp_do_ioctl ( file , cmd , arg ) ;
2010-06-02 16:28:52 +04:00
mutex_unlock ( & pp_do_mutex ) ;
2008-07-25 12:48:16 +04:00
return ret ;
}
2016-02-12 16:03:40 +03:00
static int pp_open ( struct inode * inode , struct file * file )
2005-04-17 02:20:36 +04:00
{
unsigned int minor = iminor ( inode ) ;
struct pp_struct * pp ;
if ( minor > = PARPORT_MAX )
return - ENXIO ;
2016-02-12 16:03:39 +03:00
pp = kmalloc ( sizeof ( struct pp_struct ) , GFP_KERNEL ) ;
2005-04-17 02:20:36 +04:00
if ( ! pp )
return - ENOMEM ;
pp - > state . mode = IEEE1284_MODE_COMPAT ;
2016-02-12 16:03:39 +03:00
pp - > state . phase = init_phase ( pp - > state . mode ) ;
2005-04-17 02:20:36 +04:00
pp - > flags = 0 ;
pp - > irqresponse = 0 ;
2016-02-12 16:03:39 +03:00
atomic_set ( & pp - > irqc , 0 ) ;
init_waitqueue_head ( & pp - > irq_wait ) ;
2005-04-17 02:20:36 +04:00
/* Defer the actual device registration until the first claim.
* That way , we know whether or not the driver wants to have
* exclusive access to the port ( PPEXCL ) .
*/
pp - > pdev = NULL ;
file - > private_data = pp ;
return 0 ;
}
2016-02-12 16:03:40 +03:00
static int pp_release ( struct inode * inode , struct file * file )
2005-04-17 02:20:36 +04:00
{
unsigned int minor = iminor ( inode ) ;
struct pp_struct * pp = file - > private_data ;
int compat_negot ;
compat_negot = 0 ;
if ( ! ( pp - > flags & PP_CLAIMED ) & & pp - > pdev & &
( pp - > state . mode ! = IEEE1284_MODE_COMPAT ) ) {
2016-02-12 16:03:44 +03:00
struct ieee1284_info * info ;
2005-04-17 02:20:36 +04:00
/* parport released, but not in compatibility mode */
2016-02-12 16:03:39 +03:00
parport_claim_or_block ( pp - > pdev ) ;
2005-04-17 02:20:36 +04:00
pp - > flags | = PP_CLAIMED ;
info = & pp - > pdev - > port - > ieee1284 ;
pp - > saved_state . mode = info - > mode ;
pp - > saved_state . phase = info - > phase ;
info - > mode = pp - > state . mode ;
info - > phase = pp - > state . phase ;
compat_negot = 1 ;
} else if ( ( pp - > flags & PP_CLAIMED ) & & pp - > pdev & &
( pp - > pdev - > port - > ieee1284 . mode ! = IEEE1284_MODE_COMPAT ) ) {
compat_negot = 2 ;
}
if ( compat_negot ) {
2016-02-12 16:03:39 +03:00
parport_negotiate ( pp - > pdev - > port , IEEE1284_MODE_COMPAT ) ;
2016-02-12 16:03:46 +03:00
dev_dbg ( & pp - > pdev - > dev ,
" negotiated back to compatibility mode because user-space forgot \n " ) ;
2005-04-17 02:20:36 +04:00
}
2019-07-24 12:04:26 +03:00
if ( ( pp - > flags & PP_CLAIMED ) & & pp - > pdev ) {
2005-04-17 02:20:36 +04:00
struct ieee1284_info * info ;
info = & pp - > pdev - > port - > ieee1284 ;
pp - > state . mode = info - > mode ;
pp - > state . phase = info - > phase ;
info - > mode = pp - > saved_state . mode ;
info - > phase = pp - > saved_state . phase ;
2016-02-12 16:03:39 +03:00
parport_release ( pp - > pdev ) ;
2005-04-17 02:20:36 +04:00
if ( compat_negot ! = 1 ) {
2009-06-18 03:27:49 +04:00
pr_debug ( CHRDEV " %x: released pardevice "
2005-04-17 02:20:36 +04:00
" because user-space forgot \n " , minor ) ;
}
}
if ( pp - > pdev ) {
2016-02-12 16:03:39 +03:00
parport_unregister_device ( pp - > pdev ) ;
2017-03-07 02:23:43 +03:00
ida_simple_remove ( & ida_index , pp - > index ) ;
2005-04-17 02:20:36 +04:00
pp - > pdev = NULL ;
2009-06-18 03:27:49 +04:00
pr_debug ( CHRDEV " %x: unregistered pardevice \n " , minor ) ;
2005-04-17 02:20:36 +04:00
}
2016-02-12 16:03:39 +03:00
kfree ( pp ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
}
/* No kernel lock held - fine */
2017-07-03 13:39:46 +03:00
static __poll_t pp_poll ( struct file * file , poll_table * wait )
2005-04-17 02:20:36 +04:00
{
struct pp_struct * pp = file - > private_data ;
2017-07-03 13:39:46 +03:00
__poll_t mask = 0 ;
2005-04-17 02:20:36 +04:00
2016-02-12 16:03:39 +03:00
poll_wait ( file , & pp - > irq_wait , wait ) ;
if ( atomic_read ( & pp - > irqc ) )
2018-02-12 01:34:03 +03:00
mask | = EPOLLIN | EPOLLRDNORM ;
2005-04-17 02:20:36 +04:00
return mask ;
}
2005-03-23 20:53:09 +03:00
static struct class * ppdev_class ;
2005-04-17 02:20:36 +04:00
2006-07-03 11:24:21 +04:00
static const struct file_operations pp_fops = {
2005-04-17 02:20:36 +04:00
. owner = THIS_MODULE ,
. llseek = no_llseek ,
. read = pp_read ,
. write = pp_write ,
. poll = pp_poll ,
2008-07-25 12:48:16 +04:00
. unlocked_ioctl = pp_ioctl ,
2018-09-11 18:23:00 +03:00
. compat_ioctl = compat_ptr_ioctl ,
2005-04-17 02:20:36 +04:00
. open = pp_open ,
. release = pp_release ,
} ;
static void pp_attach ( struct parport * port )
{
2016-11-13 00:22:12 +03:00
struct device * ret ;
if ( devices [ port - > number ] )
return ;
ret = device_create ( ppdev_class , port - > dev ,
MKDEV ( PP_MAJOR , port - > number ) , NULL ,
" parport%d " , port - > number ) ;
if ( IS_ERR ( ret ) ) {
pr_err ( " Failed to create device parport%d \n " ,
port - > number ) ;
return ;
}
devices [ port - > number ] = ret ;
2005-04-17 02:20:36 +04:00
}
static void pp_detach ( struct parport * port )
{
2016-11-13 00:22:12 +03:00
if ( ! devices [ port - > number ] )
return ;
2006-09-12 19:00:10 +04:00
device_destroy ( ppdev_class , MKDEV ( PP_MAJOR , port - > number ) ) ;
2016-11-13 00:22:12 +03:00
devices [ port - > number ] = NULL ;
2005-04-17 02:20:36 +04:00
}
2016-06-01 15:20:08 +03:00
static int pp_probe ( struct pardevice * par_dev )
{
struct device_driver * drv = par_dev - > dev . driver ;
int len = strlen ( drv - > name ) ;
if ( strncmp ( par_dev - > name , drv - > name , len ) )
return - ENODEV ;
return 0 ;
}
2005-04-17 02:20:36 +04:00
static struct parport_driver pp_driver = {
. name = CHRDEV ,
2016-06-01 15:20:08 +03:00
. probe = pp_probe ,
. match_port = pp_attach ,
2005-04-17 02:20:36 +04:00
. detach = pp_detach ,
2016-06-01 15:20:08 +03:00
. devmodel = true ,
2005-04-17 02:20:36 +04:00
} ;
2016-02-12 16:03:39 +03:00
static int __init ppdev_init ( void )
2005-04-17 02:20:36 +04:00
{
2005-06-21 08:15:16 +04:00
int err = 0 ;
2005-04-17 02:20:36 +04:00
2016-02-12 16:03:39 +03:00
if ( register_chrdev ( PP_MAJOR , CHRDEV , & pp_fops ) ) {
2016-11-11 01:12:01 +03:00
pr_warn ( CHRDEV " : unable to get major %d \n " , PP_MAJOR ) ;
2005-04-17 02:20:36 +04:00
return - EIO ;
}
2005-03-23 20:53:09 +03:00
ppdev_class = class_create ( THIS_MODULE , CHRDEV ) ;
2005-04-17 02:20:36 +04:00
if ( IS_ERR ( ppdev_class ) ) {
err = PTR_ERR ( ppdev_class ) ;
goto out_chrdev ;
}
2012-09-02 00:31:58 +04:00
err = parport_register_driver ( & pp_driver ) ;
if ( err < 0 ) {
2016-11-11 01:12:01 +03:00
pr_warn ( CHRDEV " : unable to register with parport \n " ) ;
2005-04-17 02:20:36 +04:00
goto out_class ;
}
2016-11-11 01:12:01 +03:00
pr_info ( PP_VERSION " \n " ) ;
2005-04-17 02:20:36 +04:00
goto out ;
out_class :
2005-03-23 20:53:09 +03:00
class_destroy ( ppdev_class ) ;
2005-04-17 02:20:36 +04:00
out_chrdev :
unregister_chrdev ( PP_MAJOR , CHRDEV ) ;
out :
return err ;
}
2016-02-12 16:03:39 +03:00
static void __exit ppdev_cleanup ( void )
2005-04-17 02:20:36 +04:00
{
/* Clean up all parport stuff */
parport_unregister_driver ( & pp_driver ) ;
2005-03-23 20:53:09 +03:00
class_destroy ( ppdev_class ) ;
2016-02-12 16:03:39 +03:00
unregister_chrdev ( PP_MAJOR , CHRDEV ) ;
2005-04-17 02:20:36 +04:00
}
module_init ( ppdev_init ) ;
module_exit ( ppdev_cleanup ) ;
MODULE_LICENSE ( " GPL " ) ;
MODULE_ALIAS_CHARDEV_MAJOR ( PP_MAJOR ) ;