2006-08-09 05:42:06 +08:00
|
|
|
/*
|
|
|
|
* adutux - driver for ADU devices from Ontrak Control Systems
|
|
|
|
* This is an experimental driver. Use at your own risk.
|
|
|
|
* This driver is not supported by Ontrak Control Systems.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003 John Homppi (SCO, leave this notice here)
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* derived from the Lego USB Tower driver 0.56:
|
|
|
|
* Copyright (c) 2003 David Glance <davidgsf@sourceforge.net>
|
|
|
|
* 2001 Juergen Stuber <stuber@loria.fr>
|
|
|
|
* that was derived from USB Skeleton driver - 0.5
|
|
|
|
* Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-06-27 07:30:46 +08:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2006-08-09 05:42:06 +08:00
|
|
|
#include <linux/kernel.h>
|
2017-02-03 02:15:33 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2006-08-09 05:42:06 +08:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/usb.h>
|
2007-07-14 03:28:31 +08:00
|
|
|
#include <linux/mutex.h>
|
2013-05-14 03:40:47 +08:00
|
|
|
#include <linux/uaccess.h>
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
/* Version Information */
|
|
|
|
#define DRIVER_VERSION "v0.0.13"
|
|
|
|
#define DRIVER_AUTHOR "John Homppi"
|
|
|
|
#define DRIVER_DESC "adutux (see www.ontrak.net)"
|
|
|
|
|
|
|
|
/* Define these values to match your device */
|
|
|
|
#define ADU_VENDOR_ID 0x0a07
|
|
|
|
#define ADU_PRODUCT_ID 0x0064
|
|
|
|
|
|
|
|
/* table of devices that work with this driver */
|
2010-01-10 22:34:45 +08:00
|
|
|
static const struct usb_device_id device_table[] = {
|
2006-08-09 05:42:06 +08:00
|
|
|
{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */
|
2013-05-14 03:41:10 +08:00
|
|
|
{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */
|
|
|
|
{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */
|
2006-08-09 05:42:06 +08:00
|
|
|
{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */
|
|
|
|
{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */
|
|
|
|
{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */
|
2013-05-14 03:42:21 +08:00
|
|
|
{ } /* Terminating entry */
|
2006-08-09 05:42:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(usb, device_table);
|
|
|
|
|
|
|
|
#ifdef CONFIG_USB_DYNAMIC_MINORS
|
|
|
|
#define ADU_MINOR_BASE 0
|
|
|
|
#else
|
|
|
|
#define ADU_MINOR_BASE 67
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* we can have up to this number of device plugged in at once */
|
|
|
|
#define MAX_DEVICES 16
|
|
|
|
|
|
|
|
#define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */
|
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
/*
|
|
|
|
* The locking scheme is a vanilla 3-lock:
|
|
|
|
* adu_device.buflock: A spinlock, covers what IRQs touch.
|
|
|
|
* adutux_mutex: A Static lock to cover open_count. It would also cover
|
|
|
|
* any globals, but we don't have them in 2.6.
|
|
|
|
* adu_device.mtx: A mutex to hold across sleepers like copy_from_user.
|
|
|
|
* It covers all of adu_device, except the open_count
|
|
|
|
* and what .buflock covers.
|
|
|
|
*/
|
|
|
|
|
2006-08-09 05:42:06 +08:00
|
|
|
/* Structure to hold all of our device specific stuff */
|
|
|
|
struct adu_device {
|
2007-11-01 06:59:30 +08:00
|
|
|
struct mutex mtx;
|
2013-05-14 03:41:34 +08:00
|
|
|
struct usb_device *udev; /* save off the usb device pointer */
|
|
|
|
struct usb_interface *interface;
|
2007-11-01 06:59:30 +08:00
|
|
|
unsigned int minor; /* the starting minor number for this device */
|
2006-08-09 05:42:06 +08:00
|
|
|
char serial_number[8];
|
|
|
|
|
|
|
|
int open_count; /* number of times this port has been opened */
|
|
|
|
|
2013-05-14 03:41:34 +08:00
|
|
|
char *read_buffer_primary;
|
2006-08-09 05:42:06 +08:00
|
|
|
int read_buffer_length;
|
2013-05-14 03:41:34 +08:00
|
|
|
char *read_buffer_secondary;
|
2006-08-09 05:42:06 +08:00
|
|
|
int secondary_head;
|
|
|
|
int secondary_tail;
|
|
|
|
spinlock_t buflock;
|
|
|
|
|
|
|
|
wait_queue_head_t read_wait;
|
|
|
|
wait_queue_head_t write_wait;
|
|
|
|
|
2013-05-14 03:41:34 +08:00
|
|
|
char *interrupt_in_buffer;
|
|
|
|
struct usb_endpoint_descriptor *interrupt_in_endpoint;
|
|
|
|
struct urb *interrupt_in_urb;
|
2006-08-09 05:42:06 +08:00
|
|
|
int read_urb_finished;
|
|
|
|
|
2013-05-14 03:41:34 +08:00
|
|
|
char *interrupt_out_buffer;
|
|
|
|
struct usb_endpoint_descriptor *interrupt_out_endpoint;
|
|
|
|
struct urb *interrupt_out_urb;
|
2007-11-01 06:59:30 +08:00
|
|
|
int out_urb_finished;
|
2006-08-09 05:42:06 +08:00
|
|
|
};
|
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
static DEFINE_MUTEX(adutux_mutex);
|
|
|
|
|
2006-08-09 05:42:06 +08:00
|
|
|
static struct usb_driver adu_driver;
|
|
|
|
|
2013-06-27 07:30:45 +08:00
|
|
|
static inline void adu_debug_data(struct device *dev, const char *function,
|
|
|
|
int size, const unsigned char *data)
|
2006-08-09 05:42:06 +08:00
|
|
|
{
|
2013-06-27 07:30:45 +08:00
|
|
|
dev_dbg(dev, "%s - length = %d, data = %*ph\n",
|
|
|
|
function, size, size, data);
|
2006-08-09 05:42:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* adu_abort_transfers
|
|
|
|
* aborts transfers and frees associated data structures
|
|
|
|
*/
|
|
|
|
static void adu_abort_transfers(struct adu_device *dev)
|
|
|
|
{
|
2007-11-01 06:59:30 +08:00
|
|
|
unsigned long flags;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
2013-06-27 07:30:44 +08:00
|
|
|
if (dev->udev == NULL)
|
2013-06-27 07:30:43 +08:00
|
|
|
return;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
/* shutdown transfer */
|
2007-11-01 06:59:30 +08:00
|
|
|
|
|
|
|
/* XXX Anchor these instead */
|
|
|
|
spin_lock_irqsave(&dev->buflock, flags);
|
|
|
|
if (!dev->read_urb_finished) {
|
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
|
|
|
usb_kill_urb(dev->interrupt_in_urb);
|
|
|
|
} else
|
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dev->buflock, flags);
|
|
|
|
if (!dev->out_urb_finished) {
|
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
|
|
|
usb_kill_urb(dev->interrupt_out_urb);
|
|
|
|
} else
|
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
2006-08-09 05:42:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void adu_delete(struct adu_device *dev)
|
|
|
|
{
|
|
|
|
/* free data structures */
|
|
|
|
usb_free_urb(dev->interrupt_in_urb);
|
|
|
|
usb_free_urb(dev->interrupt_out_urb);
|
|
|
|
kfree(dev->read_buffer_primary);
|
|
|
|
kfree(dev->read_buffer_secondary);
|
|
|
|
kfree(dev->interrupt_in_buffer);
|
|
|
|
kfree(dev->interrupt_out_buffer);
|
|
|
|
kfree(dev);
|
|
|
|
}
|
|
|
|
|
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
Maintain a per-CPU global "struct pt_regs *" variable which can be used instead
of passing regs around manually through all ~1800 interrupt handlers in the
Linux kernel.
The regs pointer is used in few places, but it potentially costs both stack
space and code to pass it around. On the FRV arch, removing the regs parameter
from all the genirq function results in a 20% speed up of the IRQ exit path
(ie: from leaving timer_interrupt() to leaving do_IRQ()).
Where appropriate, an arch may override the generic storage facility and do
something different with the variable. On FRV, for instance, the address is
maintained in GR28 at all times inside the kernel as part of general exception
handling.
Having looked over the code, it appears that the parameter may be handed down
through up to twenty or so layers of functions. Consider a USB character
device attached to a USB hub, attached to a USB controller that posts its
interrupts through a cascaded auxiliary interrupt controller. A character
device driver may want to pass regs to the sysrq handler through the input
layer which adds another few layers of parameter passing.
I've build this code with allyesconfig for x86_64 and i386. I've runtested the
main part of the code on FRV and i386, though I can't test most of the drivers.
I've also done partial conversion for powerpc and MIPS - these at least compile
with minimal configurations.
This will affect all archs. Mostly the changes should be relatively easy.
Take do_IRQ(), store the regs pointer at the beginning, saving the old one:
struct pt_regs *old_regs = set_irq_regs(regs);
And put the old one back at the end:
set_irq_regs(old_regs);
Don't pass regs through to generic_handle_irq() or __do_IRQ().
In timer_interrupt(), this sort of change will be necessary:
- update_process_times(user_mode(regs));
- profile_tick(CPU_PROFILING, regs);
+ update_process_times(user_mode(get_irq_regs()));
+ profile_tick(CPU_PROFILING);
I'd like to move update_process_times()'s use of get_irq_regs() into itself,
except that i386, alone of the archs, uses something other than user_mode().
Some notes on the interrupt handling in the drivers:
(*) input_dev() is now gone entirely. The regs pointer is no longer stored in
the input_dev struct.
(*) finish_unlinks() in drivers/usb/host/ohci-q.c needs checking. It does
something different depending on whether it's been supplied with a regs
pointer or not.
(*) Various IRQ handler function pointers have been moved to type
irq_handler_t.
Signed-Off-By: David Howells <dhowells@redhat.com>
(cherry picked from 1b16e7ac850969f38b375e511e3fa2f474a33867 commit)
2006-10-05 21:55:46 +08:00
|
|
|
static void adu_interrupt_in_callback(struct urb *urb)
|
2006-08-09 05:42:06 +08:00
|
|
|
{
|
|
|
|
struct adu_device *dev = urb->context;
|
2007-07-19 01:58:02 +08:00
|
|
|
int status = urb->status;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
2013-06-27 07:30:45 +08:00
|
|
|
adu_debug_data(&dev->udev->dev, __func__,
|
|
|
|
urb->actual_length, urb->transfer_buffer);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
spin_lock(&dev->buflock);
|
|
|
|
|
2007-07-19 01:58:02 +08:00
|
|
|
if (status != 0) {
|
2007-08-16 22:02:08 +08:00
|
|
|
if ((status != -ENOENT) && (status != -ECONNRESET) &&
|
|
|
|
(status != -ESHUTDOWN)) {
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
|
|
|
"%s : nonzero status received: %d\n",
|
|
|
|
__func__, status);
|
2006-08-09 05:42:06 +08:00
|
|
|
}
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
|
|
|
|
if (dev->read_buffer_length <
|
2011-08-23 18:12:03 +08:00
|
|
|
(4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
|
2006-08-09 05:42:06 +08:00
|
|
|
(urb->actual_length)) {
|
|
|
|
memcpy (dev->read_buffer_primary +
|
|
|
|
dev->read_buffer_length,
|
|
|
|
dev->interrupt_in_buffer, urb->actual_length);
|
|
|
|
|
|
|
|
dev->read_buffer_length += urb->actual_length;
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__,
|
|
|
|
urb->actual_length);
|
2006-08-09 05:42:06 +08:00
|
|
|
} else {
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
|
|
|
|
__func__);
|
2006-08-09 05:42:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
dev->read_urb_finished = 1;
|
|
|
|
spin_unlock(&dev->buflock);
|
|
|
|
/* always wake up so we recover from errors */
|
|
|
|
wake_up_interruptible(&dev->read_wait);
|
|
|
|
}
|
|
|
|
|
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
Maintain a per-CPU global "struct pt_regs *" variable which can be used instead
of passing regs around manually through all ~1800 interrupt handlers in the
Linux kernel.
The regs pointer is used in few places, but it potentially costs both stack
space and code to pass it around. On the FRV arch, removing the regs parameter
from all the genirq function results in a 20% speed up of the IRQ exit path
(ie: from leaving timer_interrupt() to leaving do_IRQ()).
Where appropriate, an arch may override the generic storage facility and do
something different with the variable. On FRV, for instance, the address is
maintained in GR28 at all times inside the kernel as part of general exception
handling.
Having looked over the code, it appears that the parameter may be handed down
through up to twenty or so layers of functions. Consider a USB character
device attached to a USB hub, attached to a USB controller that posts its
interrupts through a cascaded auxiliary interrupt controller. A character
device driver may want to pass regs to the sysrq handler through the input
layer which adds another few layers of parameter passing.
I've build this code with allyesconfig for x86_64 and i386. I've runtested the
main part of the code on FRV and i386, though I can't test most of the drivers.
I've also done partial conversion for powerpc and MIPS - these at least compile
with minimal configurations.
This will affect all archs. Mostly the changes should be relatively easy.
Take do_IRQ(), store the regs pointer at the beginning, saving the old one:
struct pt_regs *old_regs = set_irq_regs(regs);
And put the old one back at the end:
set_irq_regs(old_regs);
Don't pass regs through to generic_handle_irq() or __do_IRQ().
In timer_interrupt(), this sort of change will be necessary:
- update_process_times(user_mode(regs));
- profile_tick(CPU_PROFILING, regs);
+ update_process_times(user_mode(get_irq_regs()));
+ profile_tick(CPU_PROFILING);
I'd like to move update_process_times()'s use of get_irq_regs() into itself,
except that i386, alone of the archs, uses something other than user_mode().
Some notes on the interrupt handling in the drivers:
(*) input_dev() is now gone entirely. The regs pointer is no longer stored in
the input_dev struct.
(*) finish_unlinks() in drivers/usb/host/ohci-q.c needs checking. It does
something different depending on whether it's been supplied with a regs
pointer or not.
(*) Various IRQ handler function pointers have been moved to type
irq_handler_t.
Signed-Off-By: David Howells <dhowells@redhat.com>
(cherry picked from 1b16e7ac850969f38b375e511e3fa2f474a33867 commit)
2006-10-05 21:55:46 +08:00
|
|
|
static void adu_interrupt_out_callback(struct urb *urb)
|
2006-08-09 05:42:06 +08:00
|
|
|
{
|
|
|
|
struct adu_device *dev = urb->context;
|
2007-07-19 01:58:02 +08:00
|
|
|
int status = urb->status;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
2013-06-27 07:30:45 +08:00
|
|
|
adu_debug_data(&dev->udev->dev, __func__,
|
|
|
|
urb->actual_length, urb->transfer_buffer);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
2007-07-19 01:58:02 +08:00
|
|
|
if (status != 0) {
|
|
|
|
if ((status != -ENOENT) &&
|
|
|
|
(status != -ECONNRESET)) {
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
|
|
|
"%s :nonzero status received: %d\n", __func__,
|
|
|
|
status);
|
2006-08-09 05:42:06 +08:00
|
|
|
}
|
2013-06-27 07:30:45 +08:00
|
|
|
return;
|
2006-08-09 05:42:06 +08:00
|
|
|
}
|
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
spin_lock(&dev->buflock);
|
|
|
|
dev->out_urb_finished = 1;
|
|
|
|
wake_up(&dev->write_wait);
|
|
|
|
spin_unlock(&dev->buflock);
|
2006-08-09 05:42:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int adu_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct adu_device *dev = NULL;
|
|
|
|
struct usb_interface *interface;
|
|
|
|
int subminor;
|
2007-11-01 06:59:30 +08:00
|
|
|
int retval;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
subminor = iminor(inode);
|
|
|
|
|
2013-05-16 06:21:07 +08:00
|
|
|
retval = mutex_lock_interruptible(&adutux_mutex);
|
2013-06-27 07:30:44 +08:00
|
|
|
if (retval)
|
2007-11-01 06:59:30 +08:00
|
|
|
goto exit_no_lock;
|
|
|
|
|
2006-08-09 05:42:06 +08:00
|
|
|
interface = usb_find_interface(&adu_driver, subminor);
|
|
|
|
if (!interface) {
|
2013-06-27 07:30:46 +08:00
|
|
|
pr_err("%s - error, can't find device for minor %d\n",
|
|
|
|
__func__, subminor);
|
2006-08-09 05:42:06 +08:00
|
|
|
retval = -ENODEV;
|
|
|
|
goto exit_no_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = usb_get_intfdata(interface);
|
2007-11-01 06:59:30 +08:00
|
|
|
if (!dev || !dev->udev) {
|
2006-08-09 05:42:06 +08:00
|
|
|
retval = -ENODEV;
|
|
|
|
goto exit_no_device;
|
|
|
|
}
|
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
/* check that nobody else is using the device */
|
|
|
|
if (dev->open_count) {
|
|
|
|
retval = -EBUSY;
|
2006-08-09 05:42:06 +08:00
|
|
|
goto exit_no_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
++dev->open_count;
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
|
|
|
|
dev->open_count);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
/* save device in the file's private structure */
|
|
|
|
file->private_data = dev;
|
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
/* initialize in direction */
|
|
|
|
dev->read_buffer_length = 0;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
/* fixup first read by having urb waiting for it */
|
2013-05-14 03:41:54 +08:00
|
|
|
usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
|
2007-11-01 06:59:30 +08:00
|
|
|
usb_rcvintpipe(dev->udev,
|
|
|
|
dev->interrupt_in_endpoint->bEndpointAddress),
|
|
|
|
dev->interrupt_in_buffer,
|
2011-08-23 18:12:03 +08:00
|
|
|
usb_endpoint_maxp(dev->interrupt_in_endpoint),
|
2007-11-01 06:59:30 +08:00
|
|
|
adu_interrupt_in_callback, dev,
|
|
|
|
dev->interrupt_in_endpoint->bInterval);
|
|
|
|
dev->read_urb_finished = 0;
|
|
|
|
if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
|
|
|
|
dev->read_urb_finished = 1;
|
|
|
|
/* we ignore failure */
|
|
|
|
/* end of fixup for first read */
|
|
|
|
|
|
|
|
/* initialize out direction */
|
|
|
|
dev->out_urb_finished = 1;
|
|
|
|
|
|
|
|
retval = 0;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
exit_no_device:
|
2007-11-01 06:59:30 +08:00
|
|
|
mutex_unlock(&adutux_mutex);
|
|
|
|
exit_no_lock:
|
2006-08-09 05:42:06 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
static void adu_release_internal(struct adu_device *dev)
|
2006-08-09 05:42:06 +08:00
|
|
|
{
|
|
|
|
/* decrement our usage count for the device */
|
|
|
|
--dev->open_count;
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
|
|
|
|
dev->open_count);
|
2006-08-09 05:42:06 +08:00
|
|
|
if (dev->open_count <= 0) {
|
|
|
|
adu_abort_transfers(dev);
|
|
|
|
dev->open_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int adu_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
2007-11-01 06:59:30 +08:00
|
|
|
struct adu_device *dev;
|
2006-08-09 05:42:06 +08:00
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
if (file == NULL) {
|
|
|
|
retval = -ENODEV;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = file->private_data;
|
|
|
|
if (dev == NULL) {
|
|
|
|
retval = -ENODEV;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
mutex_lock(&adutux_mutex); /* not interruptible */
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
if (dev->open_count <= 0) {
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__);
|
2006-08-09 05:42:06 +08:00
|
|
|
retval = -ENODEV;
|
2009-03-12 04:47:38 +08:00
|
|
|
goto unlock;
|
2006-08-09 05:42:06 +08:00
|
|
|
}
|
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
adu_release_internal(dev);
|
USB: prevent char device open/deregister race
This patch (as908) adds central protection in usbcore for the
prototypical race between opening and unregistering a char device.
The spinlock used to protect the minor-numbers array is replaced with
an rwsem, which can remain locked across a call to a driver's open()
method. This guarantees that open() and deregister() will be mutually
exclusive.
The private locks currently used in several individual drivers for
this purpose are no longer necessary, and the patch removes them. The
following USB drivers are affected: usblcd, idmouse, auerswald,
legousbtower, sisusbvga/sisusb, ldusb, adutux, iowarrior, and
usb-skeleton.
As a side effect of this change, usb_deregister_dev() must not be
called while holding a lock that is acquired by open(). Unfortunately
a number of drivers do this, but luckily the solution is simple: call
usb_deregister_dev() before acquiring the lock.
In addition to these changes (and their consequent code
simplifications), the patch fixes a use-after-free bug in adutux and a
race between open() and release() in iowarrior.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2007-05-22 23:46:41 +08:00
|
|
|
if (dev->udev == NULL) {
|
|
|
|
/* the device was unplugged before the file was released */
|
2007-11-01 06:59:30 +08:00
|
|
|
if (!dev->open_count) /* ... and we're the last user */
|
|
|
|
adu_delete(dev);
|
USB: prevent char device open/deregister race
This patch (as908) adds central protection in usbcore for the
prototypical race between opening and unregistering a char device.
The spinlock used to protect the minor-numbers array is replaced with
an rwsem, which can remain locked across a call to a driver's open()
method. This guarantees that open() and deregister() will be mutually
exclusive.
The private locks currently used in several individual drivers for
this purpose are no longer necessary, and the patch removes them. The
following USB drivers are affected: usblcd, idmouse, auerswald,
legousbtower, sisusbvga/sisusb, ldusb, adutux, iowarrior, and
usb-skeleton.
As a side effect of this change, usb_deregister_dev() must not be
called while holding a lock that is acquired by open(). Unfortunately
a number of drivers do this, but luckily the solution is simple: call
usb_deregister_dev() before acquiring the lock.
In addition to these changes (and their consequent code
simplifications), the patch fixes a use-after-free bug in adutux and a
race between open() and release() in iowarrior.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2007-05-22 23:46:41 +08:00
|
|
|
}
|
2009-03-12 04:47:38 +08:00
|
|
|
unlock:
|
2007-11-01 06:59:30 +08:00
|
|
|
mutex_unlock(&adutux_mutex);
|
2009-03-12 04:47:38 +08:00
|
|
|
exit:
|
2006-08-09 05:42:06 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
|
|
|
|
loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct adu_device *dev;
|
|
|
|
size_t bytes_read = 0;
|
|
|
|
size_t bytes_to_read = count;
|
|
|
|
int i;
|
|
|
|
int retval = 0;
|
|
|
|
int timeout = 0;
|
|
|
|
int should_submit = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
|
|
|
|
|
|
|
dev = file->private_data;
|
2007-07-14 03:28:31 +08:00
|
|
|
if (mutex_lock_interruptible(&dev->mtx))
|
2006-08-09 05:42:06 +08:00
|
|
|
return -ERESTARTSYS;
|
|
|
|
|
|
|
|
/* verify that the device wasn't unplugged */
|
2007-11-01 06:59:30 +08:00
|
|
|
if (dev->udev == NULL) {
|
2006-08-09 05:42:06 +08:00
|
|
|
retval = -ENODEV;
|
2013-06-27 07:30:46 +08:00
|
|
|
pr_err("No device or device unplugged %d\n", retval);
|
2006-08-09 05:42:06 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify that some data was requested */
|
|
|
|
if (count == 0) {
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
|
|
|
|
__func__);
|
2006-08-09 05:42:06 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = COMMAND_TIMEOUT;
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
|
2006-08-09 05:42:06 +08:00
|
|
|
while (bytes_to_read) {
|
|
|
|
int data_in_secondary = dev->secondary_tail - dev->secondary_head;
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
|
|
|
"%s : while, data_in_secondary=%d, status=%d\n",
|
|
|
|
__func__, data_in_secondary,
|
|
|
|
dev->interrupt_in_urb->status);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
if (data_in_secondary) {
|
|
|
|
/* drain secondary buffer */
|
|
|
|
int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
|
|
|
|
i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
|
2010-08-01 01:40:07 +08:00
|
|
|
if (i) {
|
2006-08-09 05:42:06 +08:00
|
|
|
retval = -EFAULT;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
dev->secondary_head += (amount - i);
|
|
|
|
bytes_read += (amount - i);
|
|
|
|
bytes_to_read -= (amount - i);
|
|
|
|
} else {
|
|
|
|
/* we check the primary buffer */
|
|
|
|
spin_lock_irqsave (&dev->buflock, flags);
|
|
|
|
if (dev->read_buffer_length) {
|
|
|
|
/* we secure access to the primary */
|
|
|
|
char *tmp;
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
|
|
|
"%s : swap, read_buffer_length = %d\n",
|
|
|
|
__func__, dev->read_buffer_length);
|
2006-08-09 05:42:06 +08:00
|
|
|
tmp = dev->read_buffer_secondary;
|
|
|
|
dev->read_buffer_secondary = dev->read_buffer_primary;
|
|
|
|
dev->read_buffer_primary = tmp;
|
|
|
|
dev->secondary_head = 0;
|
|
|
|
dev->secondary_tail = dev->read_buffer_length;
|
|
|
|
dev->read_buffer_length = 0;
|
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
|
|
|
/* we have a free buffer so use it */
|
|
|
|
should_submit = 1;
|
|
|
|
} else {
|
|
|
|
/* even the primary was empty - we may need to do IO */
|
2007-11-01 06:59:30 +08:00
|
|
|
if (!dev->read_urb_finished) {
|
2006-08-09 05:42:06 +08:00
|
|
|
/* somebody is doing IO */
|
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
|
|
|
"%s : submitted already\n",
|
|
|
|
__func__);
|
2006-08-09 05:42:06 +08:00
|
|
|
} else {
|
|
|
|
/* we must initiate input */
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
|
|
|
"%s : initiate input\n",
|
|
|
|
__func__);
|
2006-08-09 05:42:06 +08:00
|
|
|
dev->read_urb_finished = 0;
|
2007-11-01 06:59:30 +08:00
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
2013-05-14 03:41:54 +08:00
|
|
|
usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
|
2013-05-14 03:41:10 +08:00
|
|
|
usb_rcvintpipe(dev->udev,
|
|
|
|
dev->interrupt_in_endpoint->bEndpointAddress),
|
2006-08-09 05:42:06 +08:00
|
|
|
dev->interrupt_in_buffer,
|
2011-08-23 18:12:03 +08:00
|
|
|
usb_endpoint_maxp(dev->interrupt_in_endpoint),
|
2006-08-09 05:42:06 +08:00
|
|
|
adu_interrupt_in_callback,
|
|
|
|
dev,
|
|
|
|
dev->interrupt_in_endpoint->bInterval);
|
2007-11-01 06:59:30 +08:00
|
|
|
retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
|
|
|
|
if (retval) {
|
|
|
|
dev->read_urb_finished = 1;
|
2006-08-09 05:42:06 +08:00
|
|
|
if (retval == -ENOMEM) {
|
|
|
|
retval = bytes_read ? bytes_read : -ENOMEM;
|
|
|
|
}
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
|
|
|
"%s : submit failed\n",
|
|
|
|
__func__);
|
2006-08-09 05:42:06 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we wait for I/O to complete */
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
add_wait_queue(&dev->read_wait, &wait);
|
2007-11-01 06:59:30 +08:00
|
|
|
spin_lock_irqsave(&dev->buflock, flags);
|
|
|
|
if (!dev->read_urb_finished) {
|
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
2006-08-09 05:42:06 +08:00
|
|
|
timeout = schedule_timeout(COMMAND_TIMEOUT);
|
2007-11-01 06:59:30 +08:00
|
|
|
} else {
|
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
2006-08-09 05:42:06 +08:00
|
|
|
set_current_state(TASK_RUNNING);
|
2007-11-01 06:59:30 +08:00
|
|
|
}
|
2006-08-09 05:42:06 +08:00
|
|
|
remove_wait_queue(&dev->read_wait, &wait);
|
|
|
|
|
|
|
|
if (timeout <= 0) {
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
|
|
|
"%s : timeout\n", __func__);
|
2006-08-09 05:42:06 +08:00
|
|
|
retval = bytes_read ? bytes_read : -ETIMEDOUT;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (signal_pending(current)) {
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
|
|
|
"%s : signal pending\n",
|
|
|
|
__func__);
|
2006-08-09 05:42:06 +08:00
|
|
|
retval = bytes_read ? bytes_read : -EINTR;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = bytes_read;
|
|
|
|
/* if the primary buffer is empty then use it */
|
2007-11-01 06:59:30 +08:00
|
|
|
spin_lock_irqsave(&dev->buflock, flags);
|
|
|
|
if (should_submit && dev->read_urb_finished) {
|
|
|
|
dev->read_urb_finished = 0;
|
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
2013-05-14 03:41:54 +08:00
|
|
|
usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
|
2006-08-09 05:42:06 +08:00
|
|
|
usb_rcvintpipe(dev->udev,
|
2013-05-14 03:41:10 +08:00
|
|
|
dev->interrupt_in_endpoint->bEndpointAddress),
|
2007-11-01 06:59:30 +08:00
|
|
|
dev->interrupt_in_buffer,
|
2011-08-23 18:12:03 +08:00
|
|
|
usb_endpoint_maxp(dev->interrupt_in_endpoint),
|
2007-11-01 06:59:30 +08:00
|
|
|
adu_interrupt_in_callback,
|
|
|
|
dev,
|
|
|
|
dev->interrupt_in_endpoint->bInterval);
|
|
|
|
if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
|
|
|
|
dev->read_urb_finished = 1;
|
2006-08-09 05:42:06 +08:00
|
|
|
/* we ignore failure */
|
2007-11-01 06:59:30 +08:00
|
|
|
} else {
|
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
2006-08-09 05:42:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
/* unlock the device */
|
2007-07-14 03:28:31 +08:00
|
|
|
mutex_unlock(&dev->mtx);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t adu_write(struct file *file, const __user char *buffer,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2007-11-01 06:59:30 +08:00
|
|
|
DECLARE_WAITQUEUE(waita, current);
|
2006-08-09 05:42:06 +08:00
|
|
|
struct adu_device *dev;
|
|
|
|
size_t bytes_written = 0;
|
|
|
|
size_t bytes_to_write;
|
|
|
|
size_t buffer_size;
|
2007-11-01 06:59:30 +08:00
|
|
|
unsigned long flags;
|
2007-04-02 21:16:36 +08:00
|
|
|
int retval;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
dev = file->private_data;
|
|
|
|
|
2007-07-14 03:28:31 +08:00
|
|
|
retval = mutex_lock_interruptible(&dev->mtx);
|
2007-04-02 21:16:36 +08:00
|
|
|
if (retval)
|
|
|
|
goto exit_nolock;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
/* verify that the device wasn't unplugged */
|
2007-11-01 06:59:30 +08:00
|
|
|
if (dev->udev == NULL) {
|
2006-08-09 05:42:06 +08:00
|
|
|
retval = -ENODEV;
|
2013-06-27 07:30:46 +08:00
|
|
|
pr_err("No device or device unplugged %d\n", retval);
|
2006-08-09 05:42:06 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify that we actually have some data to write */
|
|
|
|
if (count == 0) {
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
|
|
|
|
__func__);
|
2006-08-09 05:42:06 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (count > 0) {
|
2007-11-01 06:59:30 +08:00
|
|
|
add_wait_queue(&dev->write_wait, &waita);
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
spin_lock_irqsave(&dev->buflock, flags);
|
|
|
|
if (!dev->out_urb_finished) {
|
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
mutex_unlock(&dev->mtx);
|
|
|
|
if (signal_pending(current)) {
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev, "%s : interrupted\n",
|
|
|
|
__func__);
|
2007-11-01 06:59:30 +08:00
|
|
|
set_current_state(TASK_RUNNING);
|
2006-08-09 05:42:06 +08:00
|
|
|
retval = -EINTR;
|
2007-11-01 06:59:30 +08:00
|
|
|
goto exit_onqueue;
|
2006-08-09 05:42:06 +08:00
|
|
|
}
|
2007-11-01 06:59:30 +08:00
|
|
|
if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
|
|
|
"%s - command timed out.\n", __func__);
|
2007-11-01 06:59:30 +08:00
|
|
|
retval = -ETIMEDOUT;
|
|
|
|
goto exit_onqueue;
|
|
|
|
}
|
|
|
|
remove_wait_queue(&dev->write_wait, &waita);
|
2007-07-14 03:28:31 +08:00
|
|
|
retval = mutex_lock_interruptible(&dev->mtx);
|
2007-04-02 21:16:36 +08:00
|
|
|
if (retval) {
|
|
|
|
retval = bytes_written ? bytes_written : retval;
|
|
|
|
goto exit_nolock;
|
|
|
|
}
|
2006-08-09 05:42:06 +08:00
|
|
|
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
2017-02-28 06:30:02 +08:00
|
|
|
"%s : in progress, count = %zd\n",
|
2013-06-27 07:30:44 +08:00
|
|
|
__func__, count);
|
2006-08-09 05:42:06 +08:00
|
|
|
} else {
|
2007-11-01 06:59:30 +08:00
|
|
|
spin_unlock_irqrestore(&dev->buflock, flags);
|
|
|
|
set_current_state(TASK_RUNNING);
|
|
|
|
remove_wait_queue(&dev->write_wait, &waita);
|
2017-02-28 06:30:02 +08:00
|
|
|
dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n",
|
2013-06-27 07:30:44 +08:00
|
|
|
__func__, count);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
/* write the data into interrupt_out_buffer from userspace */
|
2011-08-23 18:12:03 +08:00
|
|
|
buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
|
2006-08-09 05:42:06 +08:00
|
|
|
bytes_to_write = count > buffer_size ? buffer_size : count;
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&dev->udev->dev,
|
2017-02-28 06:30:02 +08:00
|
|
|
"%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n",
|
2013-06-27 07:30:44 +08:00
|
|
|
__func__, buffer_size, count, bytes_to_write);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
|
|
|
|
retval = -EFAULT;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send off the urb */
|
|
|
|
usb_fill_int_urb(
|
|
|
|
dev->interrupt_out_urb,
|
|
|
|
dev->udev,
|
|
|
|
usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
|
|
|
|
dev->interrupt_out_buffer,
|
|
|
|
bytes_to_write,
|
|
|
|
adu_interrupt_out_callback,
|
|
|
|
dev,
|
2007-11-01 06:59:30 +08:00
|
|
|
dev->interrupt_out_endpoint->bInterval);
|
2006-08-09 05:42:06 +08:00
|
|
|
dev->interrupt_out_urb->actual_length = bytes_to_write;
|
2007-11-01 06:59:30 +08:00
|
|
|
dev->out_urb_finished = 0;
|
2006-08-09 05:42:06 +08:00
|
|
|
retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
|
|
|
|
if (retval < 0) {
|
2007-11-01 06:59:30 +08:00
|
|
|
dev->out_urb_finished = 1;
|
2008-08-15 00:37:34 +08:00
|
|
|
dev_err(&dev->udev->dev, "Couldn't submit "
|
|
|
|
"interrupt_out_urb %d\n", retval);
|
2006-08-09 05:42:06 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer += bytes_to_write;
|
|
|
|
count -= bytes_to_write;
|
|
|
|
|
|
|
|
bytes_written += bytes_to_write;
|
|
|
|
}
|
|
|
|
}
|
2007-11-01 06:59:30 +08:00
|
|
|
mutex_unlock(&dev->mtx);
|
|
|
|
return bytes_written;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
exit:
|
2007-07-14 03:28:31 +08:00
|
|
|
mutex_unlock(&dev->mtx);
|
2007-04-02 21:16:36 +08:00
|
|
|
exit_nolock:
|
2007-11-01 06:59:30 +08:00
|
|
|
return retval;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
exit_onqueue:
|
|
|
|
remove_wait_queue(&dev->write_wait, &waita);
|
2006-08-09 05:42:06 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* file operations needed when we register this driver */
|
2007-02-12 16:55:34 +08:00
|
|
|
static const struct file_operations adu_fops = {
|
2006-08-09 05:42:06 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.read = adu_read,
|
|
|
|
.write = adu_write,
|
|
|
|
.open = adu_open,
|
|
|
|
.release = adu_release,
|
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 = noop_llseek,
|
2006-08-09 05:42:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* usb class driver info in order to get a minor number from the usb core,
|
|
|
|
* and to have the device registered with devfs and the driver core
|
|
|
|
*/
|
|
|
|
static struct usb_class_driver adu_class = {
|
|
|
|
.name = "usb/adutux%d",
|
|
|
|
.fops = &adu_fops,
|
|
|
|
.minor_base = ADU_MINOR_BASE,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* adu_probe
|
|
|
|
*
|
|
|
|
* Called by the usb core when a new device is connected that it thinks
|
|
|
|
* this driver might be interested in.
|
|
|
|
*/
|
|
|
|
static int adu_probe(struct usb_interface *interface,
|
|
|
|
const struct usb_device_id *id)
|
|
|
|
{
|
|
|
|
struct usb_device *udev = interface_to_usbdev(interface);
|
|
|
|
struct adu_device *dev = NULL;
|
|
|
|
struct usb_host_interface *iface_desc;
|
|
|
|
struct usb_endpoint_descriptor *endpoint;
|
2017-03-17 18:35:33 +08:00
|
|
|
int retval = -ENOMEM;
|
2006-08-09 05:42:06 +08:00
|
|
|
int in_end_size;
|
|
|
|
int out_end_size;
|
|
|
|
int i;
|
|
|
|
|
tree-wide: fix comment/printk typos
"gadget", "through", "command", "maintain", "maintain", "controller", "address",
"between", "initiali[zs]e", "instead", "function", "select", "already",
"equal", "access", "management", "hierarchy", "registration", "interest",
"relative", "memory", "offset", "already",
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2010-11-02 03:38:34 +08:00
|
|
|
/* allocate memory for our device state and initialize it */
|
2006-08-09 05:42:06 +08:00
|
|
|
dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
|
2017-03-17 18:35:33 +08:00
|
|
|
if (!dev)
|
|
|
|
return -ENOMEM;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
2007-07-14 03:28:31 +08:00
|
|
|
mutex_init(&dev->mtx);
|
2006-08-09 05:42:06 +08:00
|
|
|
spin_lock_init(&dev->buflock);
|
|
|
|
dev->udev = udev;
|
|
|
|
init_waitqueue_head(&dev->read_wait);
|
|
|
|
init_waitqueue_head(&dev->write_wait);
|
|
|
|
|
|
|
|
iface_desc = &interface->altsetting[0];
|
|
|
|
|
|
|
|
/* set up the endpoint information */
|
|
|
|
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
|
|
|
|
endpoint = &iface_desc->endpoint[i].desc;
|
|
|
|
|
|
|
|
if (usb_endpoint_is_int_in(endpoint))
|
|
|
|
dev->interrupt_in_endpoint = endpoint;
|
|
|
|
|
|
|
|
if (usb_endpoint_is_int_out(endpoint))
|
|
|
|
dev->interrupt_out_endpoint = endpoint;
|
|
|
|
}
|
|
|
|
if (dev->interrupt_in_endpoint == NULL) {
|
|
|
|
dev_err(&interface->dev, "interrupt in endpoint not found\n");
|
2017-03-17 18:35:33 +08:00
|
|
|
retval = -ENODEV;
|
2006-08-09 05:42:06 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (dev->interrupt_out_endpoint == NULL) {
|
|
|
|
dev_err(&interface->dev, "interrupt out endpoint not found\n");
|
2017-03-17 18:35:33 +08:00
|
|
|
retval = -ENODEV;
|
2006-08-09 05:42:06 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-08-23 18:12:03 +08:00
|
|
|
in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
|
|
|
|
out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
|
2017-03-17 18:35:33 +08:00
|
|
|
if (!dev->read_buffer_primary)
|
2006-08-09 05:42:06 +08:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* debug code prime the buffer */
|
|
|
|
memset(dev->read_buffer_primary, 'a', in_end_size);
|
|
|
|
memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
|
|
|
|
memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
|
|
|
|
memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
|
|
|
|
|
|
|
|
dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
|
2017-03-17 18:35:33 +08:00
|
|
|
if (!dev->read_buffer_secondary)
|
2006-08-09 05:42:06 +08:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* debug code prime the buffer */
|
|
|
|
memset(dev->read_buffer_secondary, 'e', in_end_size);
|
|
|
|
memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
|
|
|
|
memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
|
|
|
|
memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
|
|
|
|
|
|
|
|
dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
|
2016-08-26 01:39:11 +08:00
|
|
|
if (!dev->interrupt_in_buffer)
|
2006-08-09 05:42:06 +08:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* debug code prime the buffer */
|
|
|
|
memset(dev->interrupt_in_buffer, 'i', in_end_size);
|
|
|
|
|
|
|
|
dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
|
2016-08-12 05:14:36 +08:00
|
|
|
if (!dev->interrupt_in_urb)
|
2006-08-09 05:42:06 +08:00
|
|
|
goto error;
|
|
|
|
dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
|
2016-08-26 01:39:11 +08:00
|
|
|
if (!dev->interrupt_out_buffer)
|
2006-08-09 05:42:06 +08:00
|
|
|
goto error;
|
|
|
|
dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
|
2016-08-12 05:14:36 +08:00
|
|
|
if (!dev->interrupt_out_urb)
|
2006-08-09 05:42:06 +08:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
|
|
|
|
sizeof(dev->serial_number))) {
|
|
|
|
dev_err(&interface->dev, "Could not retrieve serial number\n");
|
2017-03-17 18:35:33 +08:00
|
|
|
retval = -EIO;
|
2006-08-09 05:42:06 +08:00
|
|
|
goto error;
|
|
|
|
}
|
2013-06-27 07:30:44 +08:00
|
|
|
dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
/* we can register the device now, as it is ready */
|
|
|
|
usb_set_intfdata(interface, dev);
|
|
|
|
|
|
|
|
retval = usb_register_dev(interface, &adu_class);
|
|
|
|
|
|
|
|
if (retval) {
|
|
|
|
/* something prevented us from registering this driver */
|
|
|
|
dev_err(&interface->dev, "Not able to get a minor for this device.\n");
|
|
|
|
usb_set_intfdata(interface, NULL);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->minor = interface->minor;
|
|
|
|
|
|
|
|
/* let the user know what node this device is now attached to */
|
2007-10-18 18:06:30 +08:00
|
|
|
dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
|
2013-08-11 22:49:22 +08:00
|
|
|
le16_to_cpu(udev->descriptor.idProduct), dev->serial_number,
|
2006-08-09 05:42:06 +08:00
|
|
|
(dev->minor - ADU_MINOR_BASE));
|
2017-03-17 18:35:33 +08:00
|
|
|
|
|
|
|
return 0;
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
error:
|
|
|
|
adu_delete(dev);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* adu_disconnect
|
|
|
|
*
|
|
|
|
* Called by the usb core when the device is removed from the system.
|
|
|
|
*/
|
|
|
|
static void adu_disconnect(struct usb_interface *interface)
|
|
|
|
{
|
|
|
|
struct adu_device *dev;
|
|
|
|
int minor;
|
|
|
|
|
|
|
|
dev = usb_get_intfdata(interface);
|
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
mutex_lock(&dev->mtx); /* not interruptible */
|
|
|
|
dev->udev = NULL; /* poison */
|
2006-08-09 05:42:06 +08:00
|
|
|
minor = dev->minor;
|
|
|
|
usb_deregister_dev(interface, &adu_class);
|
2007-11-01 06:59:30 +08:00
|
|
|
mutex_unlock(&dev->mtx);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
2007-11-01 06:59:30 +08:00
|
|
|
mutex_lock(&adutux_mutex);
|
|
|
|
usb_set_intfdata(interface, NULL);
|
USB: prevent char device open/deregister race
This patch (as908) adds central protection in usbcore for the
prototypical race between opening and unregistering a char device.
The spinlock used to protect the minor-numbers array is replaced with
an rwsem, which can remain locked across a call to a driver's open()
method. This guarantees that open() and deregister() will be mutually
exclusive.
The private locks currently used in several individual drivers for
this purpose are no longer necessary, and the patch removes them. The
following USB drivers are affected: usblcd, idmouse, auerswald,
legousbtower, sisusbvga/sisusb, ldusb, adutux, iowarrior, and
usb-skeleton.
As a side effect of this change, usb_deregister_dev() must not be
called while holding a lock that is acquired by open(). Unfortunately
a number of drivers do this, but luckily the solution is simple: call
usb_deregister_dev() before acquiring the lock.
In addition to these changes (and their consequent code
simplifications), the patch fixes a use-after-free bug in adutux and a
race between open() and release() in iowarrior.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2007-05-22 23:46:41 +08:00
|
|
|
|
2006-08-09 05:42:06 +08:00
|
|
|
/* if the device is not opened, then we clean up right now */
|
2007-11-01 06:59:30 +08:00
|
|
|
if (!dev->open_count)
|
2006-08-09 05:42:06 +08:00
|
|
|
adu_delete(dev);
|
2007-11-01 06:59:30 +08:00
|
|
|
|
|
|
|
mutex_unlock(&adutux_mutex);
|
2006-08-09 05:42:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* usb specific object needed to register this driver with the usb subsystem */
|
|
|
|
static struct usb_driver adu_driver = {
|
|
|
|
.name = "adutux",
|
|
|
|
.probe = adu_probe,
|
|
|
|
.disconnect = adu_disconnect,
|
|
|
|
.id_table = device_table,
|
|
|
|
};
|
|
|
|
|
2011-11-19 01:34:02 +08:00
|
|
|
module_usb_driver(adu_driver);
|
2006-08-09 05:42:06 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR(DRIVER_AUTHOR);
|
|
|
|
MODULE_DESCRIPTION(DRIVER_DESC);
|
|
|
|
MODULE_LICENSE("GPL");
|