2007-05-08 15:31:45 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005-2007 Jiri Slaby <jirislaby@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2008-12-30 05:14:56 +08:00
|
|
|
* You need a userspace library to cooperate with this driver. It (and other
|
2007-05-08 15:31:45 +08:00
|
|
|
* info) may be obtained here:
|
|
|
|
* http://www.fi.muni.cz/~xslaby/phantom.html
|
2007-10-19 14:40:24 +08:00
|
|
|
* or alternatively, you might use OpenHaptics provided by Sensable.
|
2007-05-08 15:31:45 +08:00
|
|
|
*/
|
|
|
|
|
2008-04-29 15:59:13 +08:00
|
|
|
#include <linux/compat.h>
|
2007-05-08 15:31:45 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/cdev.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 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2007-05-08 15:31:45 +08:00
|
|
|
#include <linux/phantom.h>
|
2009-10-07 21:09:06 +08:00
|
|
|
#include <linux/sched.h>
|
2010-06-02 20:28:52 +08:00
|
|
|
#include <linux/mutex.h>
|
2007-05-08 15:31:45 +08:00
|
|
|
|
2011-07-27 07:09:06 +08:00
|
|
|
#include <linux/atomic.h>
|
2007-05-08 15:31:45 +08:00
|
|
|
#include <asm/io.h>
|
|
|
|
|
2008-02-06 17:38:03 +08:00
|
|
|
#define PHANTOM_VERSION "n0.9.8"
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
#define PHANTOM_MAX_MINORS 8
|
|
|
|
|
|
|
|
#define PHN_IRQCTL 0x4c /* irq control in caddr space */
|
|
|
|
|
|
|
|
#define PHB_RUNNING 1
|
2007-10-19 14:40:24 +08:00
|
|
|
#define PHB_NOT_OH 2
|
2007-05-08 15:31:45 +08:00
|
|
|
|
2010-06-02 20:28:52 +08:00
|
|
|
static DEFINE_MUTEX(phantom_mutex);
|
2007-05-08 15:31:45 +08:00
|
|
|
static struct class *phantom_class;
|
|
|
|
static int phantom_major;
|
|
|
|
|
|
|
|
struct phantom_device {
|
|
|
|
unsigned int opened;
|
|
|
|
void __iomem *caddr;
|
|
|
|
u32 __iomem *iaddr;
|
|
|
|
u32 __iomem *oaddr;
|
|
|
|
unsigned long status;
|
|
|
|
atomic_t counter;
|
|
|
|
|
|
|
|
wait_queue_head_t wait;
|
|
|
|
struct cdev cdev;
|
|
|
|
|
|
|
|
struct mutex open_lock;
|
2007-10-19 14:40:24 +08:00
|
|
|
spinlock_t regs_lock;
|
|
|
|
|
|
|
|
/* used in NOT_OH mode */
|
|
|
|
struct phm_regs oregs;
|
|
|
|
u32 ctl_reg;
|
2007-05-08 15:31:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned char phantom_devices[PHANTOM_MAX_MINORS];
|
|
|
|
|
|
|
|
static int phantom_status(struct phantom_device *dev, unsigned long newstat)
|
|
|
|
{
|
|
|
|
pr_debug("phantom_status %lx %lx\n", dev->status, newstat);
|
|
|
|
|
|
|
|
if (!(dev->status & PHB_RUNNING) && (newstat & PHB_RUNNING)) {
|
|
|
|
atomic_set(&dev->counter, 0);
|
|
|
|
iowrite32(PHN_CTL_IRQ, dev->iaddr + PHN_CONTROL);
|
|
|
|
iowrite32(0x43, dev->caddr + PHN_IRQCTL);
|
2007-05-24 04:57:59 +08:00
|
|
|
ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
|
|
|
|
} else if ((dev->status & PHB_RUNNING) && !(newstat & PHB_RUNNING)) {
|
2007-05-08 15:31:45 +08:00
|
|
|
iowrite32(0, dev->caddr + PHN_IRQCTL);
|
2007-05-24 04:57:59 +08:00
|
|
|
ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
|
|
|
|
}
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
dev->status = newstat;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File ops
|
|
|
|
*/
|
|
|
|
|
2007-05-24 04:57:58 +08:00
|
|
|
static long phantom_ioctl(struct file *file, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
2007-05-08 15:31:45 +08:00
|
|
|
{
|
|
|
|
struct phantom_device *dev = file->private_data;
|
|
|
|
struct phm_regs rs;
|
|
|
|
struct phm_reg r;
|
|
|
|
void __user *argp = (void __user *)arg;
|
2007-10-19 14:40:24 +08:00
|
|
|
unsigned long flags;
|
2007-05-08 15:31:45 +08:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
switch (cmd) {
|
2008-04-29 15:59:13 +08:00
|
|
|
case PHN_SETREG:
|
2007-05-08 15:31:45 +08:00
|
|
|
case PHN_SET_REG:
|
|
|
|
if (copy_from_user(&r, argp, sizeof(r)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (r.reg > 7)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2007-10-19 14:40:24 +08:00
|
|
|
spin_lock_irqsave(&dev->regs_lock, flags);
|
2007-05-08 15:31:45 +08:00
|
|
|
if (r.reg == PHN_CONTROL && (r.value & PHN_CTL_IRQ) &&
|
2007-05-24 04:57:58 +08:00
|
|
|
phantom_status(dev, dev->status | PHB_RUNNING)){
|
2007-10-19 14:40:24 +08:00
|
|
|
spin_unlock_irqrestore(&dev->regs_lock, flags);
|
2007-05-08 15:31:45 +08:00
|
|
|
return -ENODEV;
|
2007-05-24 04:57:58 +08:00
|
|
|
}
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
pr_debug("phantom: writing %x to %u\n", r.value, r.reg);
|
2007-10-19 14:40:24 +08:00
|
|
|
|
|
|
|
/* preserve amp bit (don't allow to change it when in NOT_OH) */
|
|
|
|
if (r.reg == PHN_CONTROL && (dev->status & PHB_NOT_OH)) {
|
|
|
|
r.value &= ~PHN_CTL_AMP;
|
|
|
|
r.value |= dev->ctl_reg & PHN_CTL_AMP;
|
|
|
|
dev->ctl_reg = r.value;
|
|
|
|
}
|
|
|
|
|
2007-05-08 15:31:45 +08:00
|
|
|
iowrite32(r.value, dev->iaddr + r.reg);
|
2007-05-24 04:57:59 +08:00
|
|
|
ioread32(dev->iaddr); /* PCI posting */
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
if (r.reg == PHN_CONTROL && !(r.value & PHN_CTL_IRQ))
|
|
|
|
phantom_status(dev, dev->status & ~PHB_RUNNING);
|
2007-10-19 14:40:24 +08:00
|
|
|
spin_unlock_irqrestore(&dev->regs_lock, flags);
|
2007-05-08 15:31:45 +08:00
|
|
|
break;
|
2008-04-29 15:59:13 +08:00
|
|
|
case PHN_SETREGS:
|
2007-05-08 15:31:45 +08:00
|
|
|
case PHN_SET_REGS:
|
|
|
|
if (copy_from_user(&rs, argp, sizeof(rs)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
pr_debug("phantom: SRS %u regs %x\n", rs.count, rs.mask);
|
2007-10-19 14:40:24 +08:00
|
|
|
spin_lock_irqsave(&dev->regs_lock, flags);
|
|
|
|
if (dev->status & PHB_NOT_OH)
|
|
|
|
memcpy(&dev->oregs, &rs, sizeof(rs));
|
|
|
|
else {
|
|
|
|
u32 m = min(rs.count, 8U);
|
|
|
|
for (i = 0; i < m; i++)
|
|
|
|
if (rs.mask & BIT(i))
|
|
|
|
iowrite32(rs.values[i], dev->oaddr + i);
|
|
|
|
ioread32(dev->iaddr); /* PCI posting */
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&dev->regs_lock, flags);
|
2007-05-08 15:31:45 +08:00
|
|
|
break;
|
2008-04-29 15:59:13 +08:00
|
|
|
case PHN_GETREG:
|
2007-05-08 15:31:45 +08:00
|
|
|
case PHN_GET_REG:
|
|
|
|
if (copy_from_user(&r, argp, sizeof(r)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (r.reg > 7)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
r.value = ioread32(dev->iaddr + r.reg);
|
|
|
|
|
|
|
|
if (copy_to_user(argp, &r, sizeof(r)))
|
|
|
|
return -EFAULT;
|
|
|
|
break;
|
2008-04-29 15:59:13 +08:00
|
|
|
case PHN_GETREGS:
|
2007-10-19 14:40:24 +08:00
|
|
|
case PHN_GET_REGS: {
|
|
|
|
u32 m;
|
|
|
|
|
2007-05-08 15:31:45 +08:00
|
|
|
if (copy_from_user(&rs, argp, sizeof(rs)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2007-10-19 14:40:24 +08:00
|
|
|
m = min(rs.count, 8U);
|
|
|
|
|
2007-05-08 15:31:45 +08:00
|
|
|
pr_debug("phantom: GRS %u regs %x\n", rs.count, rs.mask);
|
2007-10-19 14:40:24 +08:00
|
|
|
spin_lock_irqsave(&dev->regs_lock, flags);
|
|
|
|
for (i = 0; i < m; i++)
|
|
|
|
if (rs.mask & BIT(i))
|
2007-05-08 15:31:45 +08:00
|
|
|
rs.values[i] = ioread32(dev->iaddr + i);
|
2008-04-29 15:59:14 +08:00
|
|
|
atomic_set(&dev->counter, 0);
|
2007-10-19 14:40:24 +08:00
|
|
|
spin_unlock_irqrestore(&dev->regs_lock, flags);
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
if (copy_to_user(argp, &rs, sizeof(rs)))
|
|
|
|
return -EFAULT;
|
|
|
|
break;
|
2007-10-19 14:40:24 +08:00
|
|
|
} case PHN_NOT_OH:
|
|
|
|
spin_lock_irqsave(&dev->regs_lock, flags);
|
|
|
|
if (dev->status & PHB_RUNNING) {
|
|
|
|
printk(KERN_ERR "phantom: you need to set NOT_OH "
|
|
|
|
"before you start the device!\n");
|
|
|
|
spin_unlock_irqrestore(&dev->regs_lock, flags);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
dev->status |= PHB_NOT_OH;
|
|
|
|
spin_unlock_irqrestore(&dev->regs_lock, flags);
|
|
|
|
break;
|
2007-05-08 15:31:45 +08:00
|
|
|
default:
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-29 15:59:13 +08:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
static long phantom_compat_ioctl(struct file *filp, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
if (_IOC_NR(cmd) <= 3 && _IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
|
|
|
|
cmd &= ~(_IOC_SIZEMASK << _IOC_SIZESHIFT);
|
|
|
|
cmd |= sizeof(void *) << _IOC_SIZESHIFT;
|
|
|
|
}
|
|
|
|
return phantom_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define phantom_compat_ioctl NULL
|
|
|
|
#endif
|
|
|
|
|
2007-05-08 15:31:45 +08:00
|
|
|
static int phantom_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct phantom_device *dev = container_of(inode->i_cdev,
|
|
|
|
struct phantom_device, cdev);
|
|
|
|
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_lock(&phantom_mutex);
|
2007-05-08 15:31:45 +08:00
|
|
|
nonseekable_open(inode, file);
|
|
|
|
|
2008-05-17 04:03:05 +08:00
|
|
|
if (mutex_lock_interruptible(&dev->open_lock)) {
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_unlock(&phantom_mutex);
|
2007-05-08 15:31:45 +08:00
|
|
|
return -ERESTARTSYS;
|
2008-05-17 04:03:05 +08:00
|
|
|
}
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
if (dev->opened) {
|
|
|
|
mutex_unlock(&dev->open_lock);
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_unlock(&phantom_mutex);
|
2007-05-08 15:31:45 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2007-10-19 14:40:24 +08:00
|
|
|
WARN_ON(dev->status & PHB_NOT_OH);
|
|
|
|
|
2007-05-08 15:31:45 +08:00
|
|
|
file->private_data = dev;
|
|
|
|
|
2007-10-19 14:40:24 +08:00
|
|
|
atomic_set(&dev->counter, 0);
|
2007-05-08 15:31:45 +08:00
|
|
|
dev->opened++;
|
|
|
|
mutex_unlock(&dev->open_lock);
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_unlock(&phantom_mutex);
|
2007-05-08 15:31:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int phantom_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct phantom_device *dev = file->private_data;
|
|
|
|
|
|
|
|
mutex_lock(&dev->open_lock);
|
|
|
|
|
|
|
|
dev->opened = 0;
|
|
|
|
phantom_status(dev, dev->status & ~PHB_RUNNING);
|
2007-10-19 14:40:24 +08:00
|
|
|
dev->status &= ~PHB_NOT_OH;
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
mutex_unlock(&dev->open_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int phantom_poll(struct file *file, poll_table *wait)
|
|
|
|
{
|
|
|
|
struct phantom_device *dev = file->private_data;
|
|
|
|
unsigned int mask = 0;
|
|
|
|
|
|
|
|
pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
|
|
|
|
poll_wait(file, &dev->wait, wait);
|
2008-04-29 15:59:14 +08:00
|
|
|
|
|
|
|
if (!(dev->status & PHB_RUNNING))
|
|
|
|
mask = POLLERR;
|
|
|
|
else if (atomic_read(&dev->counter))
|
2007-05-08 15:31:45 +08:00
|
|
|
mask = POLLIN | POLLRDNORM;
|
2008-04-29 15:59:14 +08:00
|
|
|
|
2007-05-08 15:31:45 +08:00
|
|
|
pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2009-10-02 06:43:56 +08:00
|
|
|
static const struct file_operations phantom_file_ops = {
|
2007-05-08 15:31:45 +08:00
|
|
|
.open = phantom_open,
|
|
|
|
.release = phantom_release,
|
2007-05-24 04:57:58 +08:00
|
|
|
.unlocked_ioctl = phantom_ioctl,
|
2008-04-29 15:59:13 +08:00
|
|
|
.compat_ioctl = phantom_compat_ioctl,
|
2007-05-08 15:31:45 +08:00
|
|
|
.poll = phantom_poll,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.llseek = no_llseek,
|
2007-05-08 15:31:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static irqreturn_t phantom_isr(int irq, void *data)
|
|
|
|
{
|
|
|
|
struct phantom_device *dev = data;
|
2007-10-19 14:40:24 +08:00
|
|
|
unsigned int i;
|
|
|
|
u32 ctl;
|
2007-05-08 15:31:45 +08:00
|
|
|
|
2007-10-19 14:40:24 +08:00
|
|
|
spin_lock(&dev->regs_lock);
|
|
|
|
ctl = ioread32(dev->iaddr + PHN_CONTROL);
|
|
|
|
if (!(ctl & PHN_CTL_IRQ)) {
|
|
|
|
spin_unlock(&dev->regs_lock);
|
2007-05-08 15:31:45 +08:00
|
|
|
return IRQ_NONE;
|
2007-10-19 14:40:24 +08:00
|
|
|
}
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
iowrite32(0, dev->iaddr);
|
|
|
|
iowrite32(0xc0, dev->iaddr);
|
2007-10-19 14:40:24 +08:00
|
|
|
|
|
|
|
if (dev->status & PHB_NOT_OH) {
|
|
|
|
struct phm_regs *r = &dev->oregs;
|
|
|
|
u32 m = min(r->count, 8U);
|
|
|
|
|
|
|
|
for (i = 0; i < m; i++)
|
|
|
|
if (r->mask & BIT(i))
|
|
|
|
iowrite32(r->values[i], dev->oaddr + i);
|
|
|
|
|
|
|
|
dev->ctl_reg ^= PHN_CTL_AMP;
|
|
|
|
iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL);
|
|
|
|
}
|
|
|
|
spin_unlock(&dev->regs_lock);
|
|
|
|
|
2007-05-24 04:57:59 +08:00
|
|
|
ioread32(dev->iaddr); /* PCI posting */
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
atomic_inc(&dev->counter);
|
|
|
|
wake_up_interruptible(&dev->wait);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init and deinit driver
|
|
|
|
*/
|
|
|
|
|
|
|
|
static unsigned int __devinit phantom_get_free(void)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PHANTOM_MAX_MINORS; i++)
|
|
|
|
if (phantom_devices[i] == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __devinit phantom_probe(struct pci_dev *pdev,
|
|
|
|
const struct pci_device_id *pci_id)
|
|
|
|
{
|
|
|
|
struct phantom_device *pht;
|
|
|
|
unsigned int minor;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = pci_enable_device(pdev);
|
2010-10-27 05:22:35 +08:00
|
|
|
if (retval) {
|
|
|
|
dev_err(&pdev->dev, "pci_enable_device failed!\n");
|
2007-05-08 15:31:45 +08:00
|
|
|
goto err;
|
2010-10-27 05:22:35 +08:00
|
|
|
}
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
minor = phantom_get_free();
|
|
|
|
if (minor == PHANTOM_MAX_MINORS) {
|
|
|
|
dev_err(&pdev->dev, "too many devices found!\n");
|
|
|
|
retval = -EIO;
|
|
|
|
goto err_dis;
|
|
|
|
}
|
|
|
|
|
|
|
|
phantom_devices[minor] = 1;
|
|
|
|
|
|
|
|
retval = pci_request_regions(pdev, "phantom");
|
2010-10-27 05:22:35 +08:00
|
|
|
if (retval) {
|
|
|
|
dev_err(&pdev->dev, "pci_request_regions failed!\n");
|
2007-05-08 15:31:45 +08:00
|
|
|
goto err_null;
|
2010-10-27 05:22:35 +08:00
|
|
|
}
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
retval = -ENOMEM;
|
|
|
|
pht = kzalloc(sizeof(*pht), GFP_KERNEL);
|
|
|
|
if (pht == NULL) {
|
|
|
|
dev_err(&pdev->dev, "unable to allocate device\n");
|
|
|
|
goto err_reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
pht->caddr = pci_iomap(pdev, 0, 0);
|
|
|
|
if (pht->caddr == NULL) {
|
|
|
|
dev_err(&pdev->dev, "can't remap conf space\n");
|
|
|
|
goto err_fr;
|
|
|
|
}
|
|
|
|
pht->iaddr = pci_iomap(pdev, 2, 0);
|
|
|
|
if (pht->iaddr == NULL) {
|
|
|
|
dev_err(&pdev->dev, "can't remap input space\n");
|
|
|
|
goto err_unmc;
|
|
|
|
}
|
|
|
|
pht->oaddr = pci_iomap(pdev, 3, 0);
|
|
|
|
if (pht->oaddr == NULL) {
|
|
|
|
dev_err(&pdev->dev, "can't remap output space\n");
|
|
|
|
goto err_unmi;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_init(&pht->open_lock);
|
2007-10-19 14:40:24 +08:00
|
|
|
spin_lock_init(&pht->regs_lock);
|
2007-05-08 15:31:45 +08:00
|
|
|
init_waitqueue_head(&pht->wait);
|
|
|
|
cdev_init(&pht->cdev, &phantom_file_ops);
|
|
|
|
pht->cdev.owner = THIS_MODULE;
|
|
|
|
|
|
|
|
iowrite32(0, pht->caddr + PHN_IRQCTL);
|
2007-05-24 04:57:59 +08:00
|
|
|
ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
|
2007-05-08 15:31:45 +08:00
|
|
|
retval = request_irq(pdev->irq, phantom_isr,
|
|
|
|
IRQF_SHARED | IRQF_DISABLED, "phantom", pht);
|
|
|
|
if (retval) {
|
|
|
|
dev_err(&pdev->dev, "can't establish ISR\n");
|
|
|
|
goto err_unmo;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
|
|
|
|
if (retval) {
|
|
|
|
dev_err(&pdev->dev, "chardev registration failed\n");
|
|
|
|
goto err_irq;
|
|
|
|
}
|
|
|
|
|
2008-07-22 11:03:34 +08:00
|
|
|
if (IS_ERR(device_create(phantom_class, &pdev->dev,
|
|
|
|
MKDEV(phantom_major, minor), NULL,
|
|
|
|
"phantom%u", minor)))
|
2007-05-08 15:31:45 +08:00
|
|
|
dev_err(&pdev->dev, "can't create device\n");
|
|
|
|
|
|
|
|
pci_set_drvdata(pdev, pht);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err_irq:
|
|
|
|
free_irq(pdev->irq, pht);
|
|
|
|
err_unmo:
|
|
|
|
pci_iounmap(pdev, pht->oaddr);
|
|
|
|
err_unmi:
|
|
|
|
pci_iounmap(pdev, pht->iaddr);
|
|
|
|
err_unmc:
|
|
|
|
pci_iounmap(pdev, pht->caddr);
|
|
|
|
err_fr:
|
|
|
|
kfree(pht);
|
|
|
|
err_reg:
|
|
|
|
pci_release_regions(pdev);
|
|
|
|
err_null:
|
|
|
|
phantom_devices[minor] = 0;
|
|
|
|
err_dis:
|
|
|
|
pci_disable_device(pdev);
|
|
|
|
err:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __devexit phantom_remove(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
struct phantom_device *pht = pci_get_drvdata(pdev);
|
|
|
|
unsigned int minor = MINOR(pht->cdev.dev);
|
|
|
|
|
|
|
|
device_destroy(phantom_class, MKDEV(phantom_major, minor));
|
|
|
|
|
|
|
|
cdev_del(&pht->cdev);
|
|
|
|
|
|
|
|
iowrite32(0, pht->caddr + PHN_IRQCTL);
|
2007-05-24 04:57:59 +08:00
|
|
|
ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
|
2007-05-08 15:31:45 +08:00
|
|
|
free_irq(pdev->irq, pht);
|
|
|
|
|
|
|
|
pci_iounmap(pdev, pht->oaddr);
|
|
|
|
pci_iounmap(pdev, pht->iaddr);
|
|
|
|
pci_iounmap(pdev, pht->caddr);
|
|
|
|
|
|
|
|
kfree(pht);
|
|
|
|
|
|
|
|
pci_release_regions(pdev);
|
|
|
|
|
|
|
|
phantom_devices[minor] = 0;
|
|
|
|
|
|
|
|
pci_disable_device(pdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PM
|
|
|
|
static int phantom_suspend(struct pci_dev *pdev, pm_message_t state)
|
|
|
|
{
|
|
|
|
struct phantom_device *dev = pci_get_drvdata(pdev);
|
|
|
|
|
|
|
|
iowrite32(0, dev->caddr + PHN_IRQCTL);
|
2007-05-24 04:57:59 +08:00
|
|
|
ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
|
2007-05-08 15:31:45 +08:00
|
|
|
|
2007-10-19 14:40:23 +08:00
|
|
|
synchronize_irq(pdev->irq);
|
|
|
|
|
2007-05-08 15:31:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int phantom_resume(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
struct phantom_device *dev = pci_get_drvdata(pdev);
|
|
|
|
|
|
|
|
iowrite32(0, dev->caddr + PHN_IRQCTL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define phantom_suspend NULL
|
|
|
|
#define phantom_resume NULL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct pci_device_id phantom_pci_tbl[] __devinitdata = {
|
2008-02-06 17:38:03 +08:00
|
|
|
{ .vendor = PCI_VENDOR_ID_PLX, .device = PCI_DEVICE_ID_PLX_9050,
|
|
|
|
.subvendor = PCI_VENDOR_ID_PLX, .subdevice = PCI_DEVICE_ID_PLX_9050,
|
|
|
|
.class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
|
2007-05-08 15:31:45 +08:00
|
|
|
{ 0, }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
|
|
|
|
|
|
|
|
static struct pci_driver phantom_pci_driver = {
|
|
|
|
.name = "phantom",
|
|
|
|
.id_table = phantom_pci_tbl,
|
|
|
|
.probe = phantom_probe,
|
2012-11-20 02:21:23 +08:00
|
|
|
.remove = phantom_remove,
|
2007-05-08 15:31:45 +08:00
|
|
|
.suspend = phantom_suspend,
|
|
|
|
.resume = phantom_resume
|
|
|
|
};
|
|
|
|
|
2010-01-05 19:48:09 +08:00
|
|
|
static CLASS_ATTR_STRING(version, 0444, PHANTOM_VERSION);
|
2007-05-08 15:31:45 +08:00
|
|
|
|
|
|
|
static int __init phantom_init(void)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
dev_t dev;
|
|
|
|
|
|
|
|
phantom_class = class_create(THIS_MODULE, "phantom");
|
|
|
|
if (IS_ERR(phantom_class)) {
|
|
|
|
retval = PTR_ERR(phantom_class);
|
|
|
|
printk(KERN_ERR "phantom: can't register phantom class\n");
|
|
|
|
goto err;
|
|
|
|
}
|
2010-01-05 19:48:09 +08:00
|
|
|
retval = class_create_file(phantom_class, &class_attr_version.attr);
|
2007-05-08 15:31:45 +08:00
|
|
|
if (retval) {
|
|
|
|
printk(KERN_ERR "phantom: can't create sysfs version file\n");
|
|
|
|
goto err_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
|
|
|
|
if (retval) {
|
|
|
|
printk(KERN_ERR "phantom: can't register character device\n");
|
|
|
|
goto err_attr;
|
|
|
|
}
|
|
|
|
phantom_major = MAJOR(dev);
|
|
|
|
|
|
|
|
retval = pci_register_driver(&phantom_pci_driver);
|
|
|
|
if (retval) {
|
|
|
|
printk(KERN_ERR "phantom: can't register pci driver\n");
|
|
|
|
goto err_unchr;
|
|
|
|
}
|
|
|
|
|
|
|
|
printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
|
|
|
|
"init OK\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err_unchr:
|
|
|
|
unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
|
|
|
|
err_attr:
|
2010-01-05 19:48:09 +08:00
|
|
|
class_remove_file(phantom_class, &class_attr_version.attr);
|
2007-05-08 15:31:45 +08:00
|
|
|
err_class:
|
|
|
|
class_destroy(phantom_class);
|
|
|
|
err:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit phantom_exit(void)
|
|
|
|
{
|
|
|
|
pci_unregister_driver(&phantom_pci_driver);
|
|
|
|
|
|
|
|
unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
|
|
|
|
|
2010-01-05 19:48:09 +08:00
|
|
|
class_remove_file(phantom_class, &class_attr_version.attr);
|
2007-05-08 15:31:45 +08:00
|
|
|
class_destroy(phantom_class);
|
|
|
|
|
|
|
|
pr_debug("phantom: module successfully removed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(phantom_init);
|
|
|
|
module_exit(phantom_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
|
2008-07-25 16:48:23 +08:00
|
|
|
MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)");
|
2007-05-08 15:31:45 +08:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_VERSION(PHANTOM_VERSION);
|