2017-11-03 13:28:30 +03:00
// SPDX-License-Identifier: GPL-2.0+
2008-08-27 03:22:06 +04:00
/**
tree-wide: fix assorted typos all over the place
That is "success", "unknown", "through", "performance", "[re|un]mapping"
, "access", "default", "reasonable", "[con]currently", "temperature"
, "channel", "[un]used", "application", "example","hierarchy", "therefore"
, "[over|under]flow", "contiguous", "threshold", "enough" and others.
Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2009-11-14 18:09:05 +03:00
* drivers / usb / class / usbtmc . c - USB Test & Measurement class driver
2008-08-27 03:22:06 +04:00
*
* Copyright ( C ) 2007 Stefan Kopp , Gechingen , Germany
* Copyright ( C ) 2008 Novell , Inc .
* Copyright ( C ) 2008 Greg Kroah - Hartman < gregkh @ suse . de >
2018-09-12 11:50:51 +03:00
* Copyright ( C ) 2018 IVI Foundation , Inc .
2008-08-27 03:22:06 +04:00
*/
2013-07-15 12:59:42 +04:00
# define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2008-08-27 03:22:06 +04:00
# include <linux/module.h>
2008-10-30 14:56:47 +03:00
# include <linux/kernel.h>
2008-08-27 03:22:06 +04:00
# include <linux/fs.h>
# include <linux/uaccess.h>
# include <linux/kref.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>
2016-01-27 21:19:14 +03:00
# include <linux/poll.h>
2008-08-27 03:22:06 +04:00
# include <linux/mutex.h>
# include <linux/usb.h>
2018-07-24 12:05:28 +03:00
# include <linux/compat.h>
2008-08-27 03:22:06 +04:00
# include <linux/usb/tmc.h>
2018-09-12 11:51:07 +03:00
/* Increment API VERSION when changing tmc.h with new flags or ioctls
* or when changing a significant behavior of the driver .
*/
# define USBTMC_API_VERSION (2)
2008-08-27 03:22:06 +04:00
2013-04-30 09:51:50 +04:00
# define USBTMC_HEADER_SIZE 12
2008-08-27 03:22:06 +04:00
# define USBTMC_MINOR_BASE 176
2018-07-18 11:45:36 +03:00
/* Minimum USB timeout (in milliseconds) */
# define USBTMC_MIN_TIMEOUT 100
2008-08-27 03:22:06 +04:00
/* Default USB timeout (in milliseconds) */
2009-09-15 12:03:31 +04:00
# define USBTMC_TIMEOUT 5000
2008-08-27 03:22:06 +04:00
2018-09-12 11:50:52 +03:00
/* Max number of urbs used in write transfers */
# define MAX_URBS_IN_FLIGHT 16
2018-09-12 11:50:51 +03:00
/* I/O buffer size used in generic read/write functions */
# define USBTMC_BUFSIZE (4096)
2008-08-27 03:22:06 +04:00
/*
* Maximum number of read cycles to empty bulk in endpoint during CLEAR and
* ABORT_BULK_IN requests . Ends the loop if ( for whatever reason ) a short
* packet is never read .
*/
# define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
2010-01-10 17:33:45 +03:00
static const struct usb_device_id usbtmc_devices [ ] = {
2008-08-27 03:22:06 +04:00
{ USB_INTERFACE_INFO ( USB_CLASS_APP_SPEC , 3 , 0 ) , } ,
2009-03-11 23:51:42 +03:00
{ USB_INTERFACE_INFO ( USB_CLASS_APP_SPEC , 3 , 1 ) , } ,
2008-08-27 03:22:06 +04:00
{ 0 , } /* terminating entry */
} ;
2008-12-04 03:33:09 +03:00
MODULE_DEVICE_TABLE ( usb , usbtmc_devices ) ;
2008-08-27 03:22:06 +04:00
/*
* This structure is the capabilities for the device
2009-09-07 06:47:01 +04:00
* See section 4.2 .1 .8 of the USBTMC specification ,
* and section 4.2 .2 of the USBTMC usb488 subclass
* specification for details .
2008-08-27 03:22:06 +04:00
*/
struct usbtmc_dev_capabilities {
__u8 interface_capabilities ;
__u8 device_capabilities ;
__u8 usb488_interface_capabilities ;
__u8 usb488_device_capabilities ;
} ;
/* This structure holds private data for each USBTMC device. One copy is
* allocated for each USBTMC device in the driver ' s probe function .
*/
struct usbtmc_device_data {
const struct usb_device_id * id ;
struct usb_device * usb_dev ;
struct usb_interface * intf ;
2018-07-18 11:45:34 +03:00
struct list_head file_list ;
2008-08-27 03:22:06 +04:00
unsigned int bulk_in ;
unsigned int bulk_out ;
u8 bTag ;
u8 bTag_last_write ; /* needed for abort */
u8 bTag_last_read ; /* needed for abort */
2018-09-12 11:50:54 +03:00
/* packet size of IN bulk */
u16 wMaxPacketSize ;
2016-01-27 21:09:24 +03:00
/* data for interrupt in endpoint handling */
u8 bNotify1 ;
u8 bNotify2 ;
u16 ifnum ;
u8 iin_bTag ;
u8 * iin_buffer ;
atomic_t iin_data_valid ;
unsigned int iin_ep ;
int iin_ep_present ;
int iin_interval ;
struct urb * iin_urb ;
u16 iin_wMaxPacketSize ;
2016-01-27 21:22:28 +03:00
/* coalesced usb488_caps from usbtmc_dev_capabilities */
__u8 usb488_caps ;
2009-07-02 13:36:30 +04:00
bool zombie ; /* fd of disconnected device */
2008-08-27 03:22:06 +04:00
struct usbtmc_dev_capabilities capabilities ;
struct kref kref ;
struct mutex io_mutex ; /* only one i/o function running at a time */
2016-01-27 21:09:24 +03:00
wait_queue_head_t waitq ;
2016-01-27 21:15:15 +03:00
struct fasync_struct * fasync ;
2018-07-18 11:45:34 +03:00
spinlock_t dev_lock ; /* lock for file_list */
2008-08-27 03:22:06 +04:00
} ;
# define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
2018-07-18 11:45:34 +03:00
/*
* This structure holds private data for each USBTMC file handle .
*/
struct usbtmc_file_data {
struct usbtmc_device_data * data ;
struct list_head file_elem ;
2018-07-18 11:45:36 +03:00
u32 timeout ;
2018-07-18 11:45:34 +03:00
u8 srq_byte ;
atomic_t srq_asserted ;
2018-09-12 11:50:58 +03:00
atomic_t closing ;
2018-09-12 11:50:59 +03:00
u8 bmTransferAttributes ; /* member of DEV_DEP_MSG_IN */
2018-09-12 11:50:52 +03:00
2018-07-18 11:45:38 +03:00
u8 eom_val ;
2018-07-18 11:45:39 +03:00
u8 term_char ;
bool term_char_enabled ;
2018-09-12 11:51:00 +03:00
bool auto_abort ;
2018-09-12 11:50:52 +03:00
spinlock_t err_lock ; /* lock for errors */
struct usb_anchor submitted ;
/* data for generic_write */
struct semaphore limit_write_sem ;
u32 out_transfer_size ;
int out_status ;
2018-09-12 11:50:54 +03:00
/* data for generic_read */
u32 in_transfer_size ;
int in_status ;
int in_urbs_used ;
struct usb_anchor in_anchor ;
wait_queue_head_t wait_bulk_in ;
2018-07-18 11:45:34 +03:00
} ;
2008-08-27 03:22:06 +04:00
/* Forward declarations */
static struct usb_driver usbtmc_driver ;
2018-09-12 11:50:52 +03:00
static void usbtmc_draw_down ( struct usbtmc_file_data * file_data ) ;
2008-08-27 03:22:06 +04:00
static void usbtmc_delete ( struct kref * kref )
{
struct usbtmc_device_data * data = to_usbtmc_data ( kref ) ;
usb_put_dev ( data - > usb_dev ) ;
2016-09-28 12:48:44 +03:00
kfree ( data ) ;
2008-08-27 03:22:06 +04:00
}
static int usbtmc_open ( struct inode * inode , struct file * filp )
{
struct usb_interface * intf ;
struct usbtmc_device_data * data ;
2018-07-18 11:45:34 +03:00
struct usbtmc_file_data * file_data ;
2008-08-27 03:22:06 +04:00
intf = usb_find_interface ( & usbtmc_driver , iminor ( inode ) ) ;
if ( ! intf ) {
2013-07-15 12:59:42 +04:00
pr_err ( " can not find device for minor %d " , iminor ( inode ) ) ;
return - ENODEV ;
2008-08-27 03:22:06 +04:00
}
2018-07-18 11:45:34 +03:00
file_data = kzalloc ( sizeof ( * file_data ) , GFP_KERNEL ) ;
if ( ! file_data )
return - ENOMEM ;
2018-09-12 11:50:52 +03:00
spin_lock_init ( & file_data - > err_lock ) ;
sema_init ( & file_data - > limit_write_sem , MAX_URBS_IN_FLIGHT ) ;
init_usb_anchor ( & file_data - > submitted ) ;
2018-09-12 11:50:54 +03:00
init_usb_anchor ( & file_data - > in_anchor ) ;
init_waitqueue_head ( & file_data - > wait_bulk_in ) ;
2018-09-12 11:50:52 +03:00
2008-08-27 03:22:06 +04:00
data = usb_get_intfdata ( intf ) ;
2016-09-28 21:06:01 +03:00
/* Protect reference to data from file structure until release */
2008-08-27 03:22:06 +04:00
kref_get ( & data - > kref ) ;
2018-07-18 11:45:34 +03:00
mutex_lock ( & data - > io_mutex ) ;
file_data - > data = data ;
2018-09-12 11:50:58 +03:00
atomic_set ( & file_data - > closing , 0 ) ;
2018-07-18 11:45:36 +03:00
file_data - > timeout = USBTMC_TIMEOUT ;
2018-09-12 11:51:12 +03:00
file_data - > term_char = ' \n ' ;
file_data - > term_char_enabled = 0 ;
file_data - > auto_abort = 0 ;
2018-07-18 11:45:38 +03:00
file_data - > eom_val = 1 ;
2018-07-18 11:45:36 +03:00
2018-07-18 11:45:34 +03:00
INIT_LIST_HEAD ( & file_data - > file_elem ) ;
spin_lock_irq ( & data - > dev_lock ) ;
list_add_tail ( & file_data - > file_elem , & data - > file_list ) ;
spin_unlock_irq ( & data - > dev_lock ) ;
mutex_unlock ( & data - > io_mutex ) ;
2008-08-27 03:22:06 +04:00
/* Store pointer in file structure's private data field */
2018-07-18 11:45:34 +03:00
filp - > private_data = file_data ;
2008-08-27 03:22:06 +04:00
2018-07-18 11:45:34 +03:00
return 0 ;
2008-08-27 03:22:06 +04:00
}
2018-09-12 11:50:52 +03:00
/*
* usbtmc_flush - called before file handle is closed
*/
static int usbtmc_flush ( struct file * file , fl_owner_t id )
{
struct usbtmc_file_data * file_data ;
struct usbtmc_device_data * data ;
file_data = file - > private_data ;
if ( file_data = = NULL )
return - ENODEV ;
2018-09-12 11:50:58 +03:00
atomic_set ( & file_data - > closing , 1 ) ;
2018-09-12 11:50:52 +03:00
data = file_data - > data ;
/* wait for io to stop */
mutex_lock ( & data - > io_mutex ) ;
usbtmc_draw_down ( file_data ) ;
spin_lock_irq ( & file_data - > err_lock ) ;
2018-09-12 11:50:54 +03:00
file_data - > in_status = 0 ;
file_data - > in_transfer_size = 0 ;
file_data - > in_urbs_used = 0 ;
2018-09-12 11:50:52 +03:00
file_data - > out_status = 0 ;
file_data - > out_transfer_size = 0 ;
spin_unlock_irq ( & file_data - > err_lock ) ;
wake_up_interruptible_all ( & data - > waitq ) ;
mutex_unlock ( & data - > io_mutex ) ;
return 0 ;
}
2008-08-27 03:22:06 +04:00
static int usbtmc_release ( struct inode * inode , struct file * file )
{
2018-07-18 11:45:34 +03:00
struct usbtmc_file_data * file_data = file - > private_data ;
2008-08-27 03:22:06 +04:00
2018-07-18 11:45:34 +03:00
/* prevent IO _AND_ usbtmc_interrupt */
mutex_lock ( & file_data - > data - > io_mutex ) ;
spin_lock_irq ( & file_data - > data - > dev_lock ) ;
list_del ( & file_data - > file_elem ) ;
spin_unlock_irq ( & file_data - > data - > dev_lock ) ;
mutex_unlock ( & file_data - > data - > io_mutex ) ;
kref_put ( & file_data - > data - > kref , usbtmc_delete ) ;
file_data - > data = NULL ;
kfree ( file_data ) ;
2008-08-27 03:22:06 +04:00
return 0 ;
}
2018-09-12 11:51:04 +03:00
static int usbtmc_ioctl_abort_bulk_in_tag ( struct usbtmc_device_data * data ,
u8 tag )
2008-08-27 03:22:06 +04:00
{
2008-10-26 01:07:32 +04:00
u8 * buffer ;
2008-08-27 03:22:06 +04:00
struct device * dev ;
int rv ;
int n ;
int actual ;
dev = & data - > intf - > dev ;
2018-09-12 11:51:04 +03:00
buffer = kmalloc ( USBTMC_BUFSIZE , GFP_KERNEL ) ;
2008-08-27 03:22:06 +04:00
if ( ! buffer )
return - ENOMEM ;
rv = usb_control_msg ( data - > usb_dev ,
usb_rcvctrlpipe ( data - > usb_dev , 0 ) ,
USBTMC_REQUEST_INITIATE_ABORT_BULK_IN ,
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT ,
2018-09-12 11:51:04 +03:00
tag , data - > bulk_in ,
buffer , 2 , USB_CTRL_GET_TIMEOUT ) ;
2008-08-27 03:22:06 +04:00
if ( rv < 0 ) {
dev_err ( dev , " usb_control_msg returned %d \n " , rv ) ;
goto exit ;
}
2018-09-12 11:51:04 +03:00
dev_dbg ( dev , " INITIATE_ABORT_BULK_IN returned %x with tag %02x \n " ,
buffer [ 0 ] , buffer [ 1 ] ) ;
2008-08-27 03:22:06 +04:00
if ( buffer [ 0 ] = = USBTMC_STATUS_FAILED ) {
2018-09-12 11:51:04 +03:00
/* No transfer in progress and the Bulk-OUT FIFO is empty. */
2008-08-27 03:22:06 +04:00
rv = 0 ;
goto exit ;
}
2018-09-12 11:51:04 +03:00
if ( buffer [ 0 ] = = USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS ) {
/* The device returns this status if either:
* - There is a transfer in progress , but the specified bTag
* does not match .
* - There is no transfer in progress , but the Bulk - OUT FIFO
* is not empty .
*/
rv = - ENOMSG ;
2008-08-27 03:22:06 +04:00
goto exit ;
}
2018-09-12 11:51:04 +03:00
if ( buffer [ 0 ] ! = USBTMC_STATUS_SUCCESS ) {
dev_err ( dev , " INITIATE_ABORT_BULK_IN returned %x \n " ,
buffer [ 0 ] ) ;
2008-08-27 03:22:06 +04:00
rv = - EPERM ;
goto exit ;
}
n = 0 ;
2018-09-12 11:51:04 +03:00
usbtmc_abort_bulk_in_status :
dev_dbg ( dev , " Reading from bulk in EP \n " ) ;
/* Data must be present. So use low timeout 300 ms */
2018-09-25 02:30:32 +03:00
actual = 0 ;
2018-09-12 11:51:04 +03:00
rv = usb_bulk_msg ( data - > usb_dev ,
usb_rcvbulkpipe ( data - > usb_dev ,
data - > bulk_in ) ,
buffer , USBTMC_BUFSIZE ,
& actual , 300 ) ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:04 +03:00
print_hex_dump_debug ( " usbtmc " , DUMP_PREFIX_NONE , 16 , 1 ,
buffer , actual , true ) ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:04 +03:00
n + + ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:04 +03:00
if ( rv < 0 ) {
dev_err ( dev , " usb_bulk_msg returned %d \n " , rv ) ;
if ( rv ! = - ETIMEDOUT )
2008-08-27 03:22:06 +04:00
goto exit ;
2018-09-12 11:51:04 +03:00
}
if ( actual = = USBTMC_BUFSIZE )
goto usbtmc_abort_bulk_in_status ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:04 +03:00
if ( n > = USBTMC_MAX_READS_TO_CLEAR_BULK_IN ) {
2008-08-27 03:22:06 +04:00
dev_err ( dev , " Couldn't clear device buffer within %d cycles \n " ,
USBTMC_MAX_READS_TO_CLEAR_BULK_IN ) ;
rv = - EPERM ;
goto exit ;
}
rv = usb_control_msg ( data - > usb_dev ,
usb_rcvctrlpipe ( data - > usb_dev , 0 ) ,
USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS ,
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT ,
0 , data - > bulk_in , buffer , 0x08 ,
2018-09-12 11:51:04 +03:00
USB_CTRL_GET_TIMEOUT ) ;
2008-08-27 03:22:06 +04:00
if ( rv < 0 ) {
dev_err ( dev , " usb_control_msg returned %d \n " , rv ) ;
goto exit ;
}
2018-09-12 11:51:04 +03:00
dev_dbg ( dev , " CHECK_ABORT_BULK_IN returned %x \n " , buffer [ 0 ] ) ;
2008-08-27 03:22:06 +04:00
if ( buffer [ 0 ] = = USBTMC_STATUS_SUCCESS ) {
rv = 0 ;
goto exit ;
}
if ( buffer [ 0 ] ! = USBTMC_STATUS_PENDING ) {
2018-09-12 11:51:04 +03:00
dev_err ( dev , " CHECK_ABORT_BULK_IN returned %x \n " , buffer [ 0 ] ) ;
2008-08-27 03:22:06 +04:00
rv = - EPERM ;
goto exit ;
}
2018-09-12 11:51:04 +03:00
if ( ( buffer [ 1 ] & 1 ) > 0 ) {
/* The device has 1 or more queued packets the Host can read */
goto usbtmc_abort_bulk_in_status ;
2008-08-27 03:22:06 +04:00
}
2018-09-12 11:51:04 +03:00
/* The Host must send CHECK_ABORT_BULK_IN_STATUS at a later time. */
rv = - EAGAIN ;
2008-08-27 03:22:06 +04:00
exit :
kfree ( buffer ) ;
return rv ;
2018-09-12 11:51:04 +03:00
}
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:04 +03:00
static int usbtmc_ioctl_abort_bulk_in ( struct usbtmc_device_data * data )
{
return usbtmc_ioctl_abort_bulk_in_tag ( data , data - > bTag_last_read ) ;
2008-08-27 03:22:06 +04:00
}
2018-09-12 11:51:05 +03:00
static int usbtmc_ioctl_abort_bulk_out_tag ( struct usbtmc_device_data * data ,
u8 tag )
2008-08-27 03:22:06 +04:00
{
struct device * dev ;
u8 * buffer ;
int rv ;
int n ;
dev = & data - > intf - > dev ;
buffer = kmalloc ( 8 , GFP_KERNEL ) ;
if ( ! buffer )
return - ENOMEM ;
rv = usb_control_msg ( data - > usb_dev ,
usb_rcvctrlpipe ( data - > usb_dev , 0 ) ,
USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT ,
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT ,
2018-09-12 11:51:05 +03:00
tag , data - > bulk_out ,
buffer , 2 , USB_CTRL_GET_TIMEOUT ) ;
2008-08-27 03:22:06 +04:00
if ( rv < 0 ) {
dev_err ( dev , " usb_control_msg returned %d \n " , rv ) ;
goto exit ;
}
dev_dbg ( dev , " INITIATE_ABORT_BULK_OUT returned %x \n " , buffer [ 0 ] ) ;
if ( buffer [ 0 ] ! = USBTMC_STATUS_SUCCESS ) {
dev_err ( dev , " INITIATE_ABORT_BULK_OUT returned %x \n " ,
buffer [ 0 ] ) ;
rv = - EPERM ;
goto exit ;
}
n = 0 ;
usbtmc_abort_bulk_out_check_status :
2018-09-12 11:51:05 +03:00
/* do not stress device with subsequent requests */
msleep ( 50 ) ;
2008-08-27 03:22:06 +04:00
rv = usb_control_msg ( data - > usb_dev ,
usb_rcvctrlpipe ( data - > usb_dev , 0 ) ,
USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS ,
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT ,
0 , data - > bulk_out , buffer , 0x08 ,
2018-09-12 11:51:05 +03:00
USB_CTRL_GET_TIMEOUT ) ;
2008-08-27 03:22:06 +04:00
n + + ;
if ( rv < 0 ) {
dev_err ( dev , " usb_control_msg returned %d \n " , rv ) ;
goto exit ;
}
dev_dbg ( dev , " CHECK_ABORT_BULK_OUT returned %x \n " , buffer [ 0 ] ) ;
if ( buffer [ 0 ] = = USBTMC_STATUS_SUCCESS )
goto usbtmc_abort_bulk_out_clear_halt ;
if ( ( buffer [ 0 ] = = USBTMC_STATUS_PENDING ) & &
( n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN ) )
goto usbtmc_abort_bulk_out_check_status ;
rv = - EPERM ;
goto exit ;
usbtmc_abort_bulk_out_clear_halt :
2009-12-03 22:35:59 +03:00
rv = usb_clear_halt ( data - > usb_dev ,
usb_sndbulkpipe ( data - > usb_dev , data - > bulk_out ) ) ;
2008-08-27 03:22:06 +04:00
if ( rv < 0 ) {
dev_err ( dev , " usb_control_msg returned %d \n " , rv ) ;
goto exit ;
}
rv = 0 ;
exit :
kfree ( buffer ) ;
return rv ;
}
2018-09-12 11:51:05 +03:00
static int usbtmc_ioctl_abort_bulk_out ( struct usbtmc_device_data * data )
{
return usbtmc_ioctl_abort_bulk_out_tag ( data , data - > bTag_last_write ) ;
}
2018-07-18 11:45:34 +03:00
static int usbtmc488_ioctl_read_stb ( struct usbtmc_file_data * file_data ,
2016-01-27 21:09:24 +03:00
void __user * arg )
{
2018-07-18 11:45:34 +03:00
struct usbtmc_device_data * data = file_data - > data ;
2016-01-27 21:09:24 +03:00
struct device * dev = & data - > intf - > dev ;
2018-07-18 11:45:34 +03:00
int srq_asserted = 0 ;
2016-01-27 21:09:24 +03:00
u8 * buffer ;
u8 tag ;
__u8 stb ;
int rv ;
dev_dbg ( dev , " Enter ioctl_read_stb iin_ep_present: %d \n " ,
data - > iin_ep_present ) ;
2018-07-18 11:45:34 +03:00
spin_lock_irq ( & data - > dev_lock ) ;
srq_asserted = atomic_xchg ( & file_data - > srq_asserted , srq_asserted ) ;
if ( srq_asserted ) {
/* a STB with SRQ is already received */
stb = file_data - > srq_byte ;
spin_unlock_irq ( & data - > dev_lock ) ;
rv = put_user ( stb , ( __u8 __user * ) arg ) ;
dev_dbg ( dev , " stb:0x%02x with srq received %d \n " ,
( unsigned int ) stb , rv ) ;
return rv ;
}
spin_unlock_irq ( & data - > dev_lock ) ;
2016-01-27 21:09:24 +03:00
buffer = kmalloc ( 8 , GFP_KERNEL ) ;
if ( ! buffer )
return - ENOMEM ;
atomic_set ( & data - > iin_data_valid , 0 ) ;
rv = usb_control_msg ( data - > usb_dev ,
usb_rcvctrlpipe ( data - > usb_dev , 0 ) ,
USBTMC488_REQUEST_READ_STATUS_BYTE ,
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE ,
data - > iin_bTag ,
data - > ifnum ,
2018-09-12 11:51:06 +03:00
buffer , 0x03 , USB_CTRL_GET_TIMEOUT ) ;
2016-01-27 21:09:24 +03:00
if ( rv < 0 ) {
dev_err ( dev , " stb usb_control_msg returned %d \n " , rv ) ;
goto exit ;
}
if ( buffer [ 0 ] ! = USBTMC_STATUS_SUCCESS ) {
dev_err ( dev , " control status returned %x \n " , buffer [ 0 ] ) ;
rv = - EIO ;
goto exit ;
}
if ( data - > iin_ep_present ) {
rv = wait_event_interruptible_timeout (
data - > waitq ,
atomic_read ( & data - > iin_data_valid ) ! = 0 ,
2018-07-18 11:45:36 +03:00
file_data - > timeout ) ;
2016-01-27 21:09:24 +03:00
if ( rv < 0 ) {
dev_dbg ( dev , " wait interrupted %d \n " , rv ) ;
goto exit ;
}
if ( rv = = 0 ) {
dev_dbg ( dev , " wait timed out \n " ) ;
2018-07-18 11:45:35 +03:00
rv = - ETIMEDOUT ;
2016-01-27 21:09:24 +03:00
goto exit ;
}
tag = data - > bNotify1 & 0x7f ;
if ( tag ! = data - > iin_bTag ) {
dev_err ( dev , " expected bTag %x got %x \n " ,
data - > iin_bTag , tag ) ;
}
stb = data - > bNotify2 ;
} else {
stb = buffer [ 2 ] ;
}
2018-07-18 11:45:34 +03:00
rv = put_user ( stb , ( __u8 __user * ) arg ) ;
dev_dbg ( dev , " stb:0x%02x received %d \n " , ( unsigned int ) stb , rv ) ;
2016-01-27 21:09:24 +03:00
exit :
/* bump interrupt bTag */
data - > iin_bTag + = 1 ;
if ( data - > iin_bTag > 127 )
/* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
data - > iin_bTag = 2 ;
kfree ( buffer ) ;
return rv ;
}
2018-09-12 11:50:58 +03:00
static int usbtmc488_ioctl_wait_srq ( struct usbtmc_file_data * file_data ,
__u32 __user * arg )
{
struct usbtmc_device_data * data = file_data - > data ;
struct device * dev = & data - > intf - > dev ;
int rv ;
u32 timeout ;
unsigned long expire ;
if ( ! data - > iin_ep_present ) {
dev_dbg ( dev , " no interrupt endpoint present \n " ) ;
return - EFAULT ;
}
if ( get_user ( timeout , arg ) )
return - EFAULT ;
expire = msecs_to_jiffies ( timeout ) ;
mutex_unlock ( & data - > io_mutex ) ;
rv = wait_event_interruptible_timeout (
data - > waitq ,
atomic_read ( & file_data - > srq_asserted ) ! = 0 | |
atomic_read ( & file_data - > closing ) ,
expire ) ;
mutex_lock ( & data - > io_mutex ) ;
/* Note! disconnect or close could be called in the meantime */
if ( atomic_read ( & file_data - > closing ) | | data - > zombie )
rv = - ENODEV ;
if ( rv < 0 ) {
/* dev can be invalid now! */
pr_debug ( " %s - wait interrupted %d \n " , __func__ , rv ) ;
return rv ;
}
if ( rv = = 0 ) {
dev_dbg ( dev , " %s - wait timed out \n " , __func__ ) ;
return - ETIMEDOUT ;
}
dev_dbg ( dev , " %s - srq asserted \n " , __func__ ) ;
return 0 ;
}
2016-01-27 21:25:24 +03:00
static int usbtmc488_ioctl_simple ( struct usbtmc_device_data * data ,
void __user * arg , unsigned int cmd )
{
struct device * dev = & data - > intf - > dev ;
__u8 val ;
u8 * buffer ;
u16 wValue ;
int rv ;
if ( ! ( data - > usb488_caps & USBTMC488_CAPABILITY_SIMPLE ) )
return - EINVAL ;
buffer = kmalloc ( 8 , GFP_KERNEL ) ;
if ( ! buffer )
return - ENOMEM ;
if ( cmd = = USBTMC488_REQUEST_REN_CONTROL ) {
rv = copy_from_user ( & val , arg , sizeof ( val ) ) ;
if ( rv ) {
rv = - EFAULT ;
goto exit ;
}
wValue = val ? 1 : 0 ;
} else {
wValue = 0 ;
}
rv = usb_control_msg ( data - > usb_dev ,
usb_rcvctrlpipe ( data - > usb_dev , 0 ) ,
cmd ,
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE ,
wValue ,
data - > ifnum ,
2018-09-12 11:51:06 +03:00
buffer , 0x01 , USB_CTRL_GET_TIMEOUT ) ;
2016-01-27 21:25:24 +03:00
if ( rv < 0 ) {
dev_err ( dev , " simple usb_control_msg failed %d \n " , rv ) ;
goto exit ;
} else if ( rv ! = 1 ) {
dev_warn ( dev , " simple usb_control_msg returned %d \n " , rv ) ;
rv = - EIO ;
goto exit ;
}
if ( buffer [ 0 ] ! = USBTMC_STATUS_SUCCESS ) {
dev_err ( dev , " simple control status returned %x \n " , buffer [ 0 ] ) ;
rv = - EIO ;
goto exit ;
}
rv = 0 ;
exit :
kfree ( buffer ) ;
return rv ;
}
2018-07-18 11:45:37 +03:00
/*
* Sends a TRIGGER Bulk - OUT command message
* See the USBTMC - USB488 specification , Table 2.
*
* Also updates bTag_last_write .
*/
static int usbtmc488_ioctl_trigger ( struct usbtmc_file_data * file_data )
{
struct usbtmc_device_data * data = file_data - > data ;
int retval ;
u8 * buffer ;
int actual ;
buffer = kzalloc ( USBTMC_HEADER_SIZE , GFP_KERNEL ) ;
if ( ! buffer )
return - ENOMEM ;
buffer [ 0 ] = 128 ;
buffer [ 1 ] = data - > bTag ;
buffer [ 2 ] = ~ data - > bTag ;
retval = usb_bulk_msg ( data - > usb_dev ,
usb_sndbulkpipe ( data - > usb_dev ,
data - > bulk_out ) ,
buffer , USBTMC_HEADER_SIZE ,
& actual , file_data - > timeout ) ;
/* Store bTag (in case we need to abort) */
data - > bTag_last_write = data - > bTag ;
/* Increment bTag -- and increment again if zero */
data - > bTag + + ;
if ( ! data - > bTag )
data - > bTag + + ;
kfree ( buffer ) ;
if ( retval < 0 ) {
dev_err ( & data - > intf - > dev , " %s returned %d \n " ,
__func__ , retval ) ;
return retval ;
}
return 0 ;
}
2018-09-12 11:50:52 +03:00
static struct urb * usbtmc_create_urb ( void )
{
const size_t bufsize = USBTMC_BUFSIZE ;
u8 * dmabuf = NULL ;
struct urb * urb = usb_alloc_urb ( 0 , GFP_KERNEL ) ;
if ( ! urb )
return NULL ;
dmabuf = kmalloc ( bufsize , GFP_KERNEL ) ;
if ( ! dmabuf ) {
usb_free_urb ( urb ) ;
return NULL ;
}
urb - > transfer_buffer = dmabuf ;
urb - > transfer_buffer_length = bufsize ;
urb - > transfer_flags | = URB_FREE_BUFFER ;
return urb ;
}
2018-09-12 11:50:54 +03:00
static void usbtmc_read_bulk_cb ( struct urb * urb )
{
struct usbtmc_file_data * file_data = urb - > context ;
int status = urb - > status ;
unsigned long flags ;
/* sync/async unlink faults aren't errors */
if ( status ) {
if ( ! ( /* status == -ENOENT || */
status = = - ECONNRESET | |
status = = - EREMOTEIO | | /* Short packet */
status = = - ESHUTDOWN ) )
dev_err ( & file_data - > data - > intf - > dev ,
" %s - nonzero read bulk status received: %d \n " ,
__func__ , status ) ;
spin_lock_irqsave ( & file_data - > err_lock , flags ) ;
if ( ! file_data - > in_status )
file_data - > in_status = status ;
spin_unlock_irqrestore ( & file_data - > err_lock , flags ) ;
}
spin_lock_irqsave ( & file_data - > err_lock , flags ) ;
file_data - > in_transfer_size + = urb - > actual_length ;
dev_dbg ( & file_data - > data - > intf - > dev ,
" %s - total size: %u current: %d status: %d \n " ,
__func__ , file_data - > in_transfer_size ,
urb - > actual_length , status ) ;
spin_unlock_irqrestore ( & file_data - > err_lock , flags ) ;
usb_anchor_urb ( urb , & file_data - > in_anchor ) ;
wake_up_interruptible ( & file_data - > wait_bulk_in ) ;
wake_up_interruptible ( & file_data - > data - > waitq ) ;
}
static inline bool usbtmc_do_transfer ( struct usbtmc_file_data * file_data )
{
bool data_or_error ;
spin_lock_irq ( & file_data - > err_lock ) ;
data_or_error = ! usb_anchor_empty ( & file_data - > in_anchor )
| | file_data - > in_status ;
spin_unlock_irq ( & file_data - > err_lock ) ;
dev_dbg ( & file_data - > data - > intf - > dev , " %s: returns %d \n " , __func__ ,
data_or_error ) ;
return data_or_error ;
}
static ssize_t usbtmc_generic_read ( struct usbtmc_file_data * file_data ,
void __user * user_buffer ,
u32 transfer_size ,
u32 * transferred ,
u32 flags )
{
struct usbtmc_device_data * data = file_data - > data ;
struct device * dev = & data - > intf - > dev ;
u32 done = 0 ;
u32 remaining ;
const u32 bufsize = USBTMC_BUFSIZE ;
int retval = 0 ;
u32 max_transfer_size ;
unsigned long expire ;
int bufcount = 1 ;
int again = 0 ;
/* mutex already locked */
* transferred = done ;
max_transfer_size = transfer_size ;
if ( flags & USBTMC_FLAG_IGNORE_TRAILER ) {
/* The device may send extra alignment bytes (up to
* wMaxPacketSize – 1 ) to avoid sending a zero - length
* packet
*/
remaining = transfer_size ;
if ( ( max_transfer_size % data - > wMaxPacketSize ) = = 0 )
max_transfer_size + = ( data - > wMaxPacketSize - 1 ) ;
} else {
/* round down to bufsize to avoid truncated data left */
if ( max_transfer_size > bufsize ) {
max_transfer_size =
roundup ( max_transfer_size + 1 - bufsize ,
bufsize ) ;
}
remaining = max_transfer_size ;
}
spin_lock_irq ( & file_data - > err_lock ) ;
if ( file_data - > in_status ) {
/* return the very first error */
retval = file_data - > in_status ;
spin_unlock_irq ( & file_data - > err_lock ) ;
goto error ;
}
if ( flags & USBTMC_FLAG_ASYNC ) {
if ( usb_anchor_empty ( & file_data - > in_anchor ) )
again = 1 ;
if ( file_data - > in_urbs_used = = 0 ) {
file_data - > in_transfer_size = 0 ;
file_data - > in_status = 0 ;
}
} else {
file_data - > in_transfer_size = 0 ;
file_data - > in_status = 0 ;
}
if ( max_transfer_size = = 0 ) {
bufcount = 0 ;
} else {
bufcount = roundup ( max_transfer_size , bufsize ) / bufsize ;
if ( bufcount > file_data - > in_urbs_used )
bufcount - = file_data - > in_urbs_used ;
else
bufcount = 0 ;
if ( bufcount + file_data - > in_urbs_used > MAX_URBS_IN_FLIGHT ) {
bufcount = MAX_URBS_IN_FLIGHT -
file_data - > in_urbs_used ;
}
}
spin_unlock_irq ( & file_data - > err_lock ) ;
dev_dbg ( dev , " %s: requested=%u flags=0x%X size=%u bufs=%d used=%d \n " ,
__func__ , transfer_size , flags ,
max_transfer_size , bufcount , file_data - > in_urbs_used ) ;
while ( bufcount > 0 ) {
u8 * dmabuf = NULL ;
struct urb * urb = usbtmc_create_urb ( ) ;
if ( ! urb ) {
retval = - ENOMEM ;
goto error ;
}
dmabuf = urb - > transfer_buffer ;
usb_fill_bulk_urb ( urb , data - > usb_dev ,
usb_rcvbulkpipe ( data - > usb_dev , data - > bulk_in ) ,
dmabuf , bufsize ,
usbtmc_read_bulk_cb , file_data ) ;
usb_anchor_urb ( urb , & file_data - > submitted ) ;
retval = usb_submit_urb ( urb , GFP_KERNEL ) ;
/* urb is anchored. We can release our reference. */
usb_free_urb ( urb ) ;
if ( unlikely ( retval ) ) {
usb_unanchor_urb ( urb ) ;
goto error ;
}
file_data - > in_urbs_used + + ;
bufcount - - ;
}
if ( again ) {
dev_dbg ( dev , " %s: ret=again \n " , __func__ ) ;
return - EAGAIN ;
}
if ( user_buffer = = NULL )
return - EINVAL ;
expire = msecs_to_jiffies ( file_data - > timeout ) ;
while ( max_transfer_size > 0 ) {
u32 this_part ;
struct urb * urb = NULL ;
if ( ! ( flags & USBTMC_FLAG_ASYNC ) ) {
dev_dbg ( dev , " %s: before wait time %lu \n " ,
__func__ , expire ) ;
retval = wait_event_interruptible_timeout (
file_data - > wait_bulk_in ,
usbtmc_do_transfer ( file_data ) ,
expire ) ;
dev_dbg ( dev , " %s: wait returned %d \n " ,
__func__ , retval ) ;
if ( retval < = 0 ) {
if ( retval = = 0 )
retval = - ETIMEDOUT ;
goto error ;
}
}
urb = usb_get_from_anchor ( & file_data - > in_anchor ) ;
if ( ! urb ) {
if ( ! ( flags & USBTMC_FLAG_ASYNC ) ) {
/* synchronous case: must not happen */
retval = - EFAULT ;
goto error ;
}
/* asynchronous case: ready, do not block or wait */
* transferred = done ;
dev_dbg ( dev , " %s: (async) done=%u ret=0 \n " ,
__func__ , done ) ;
return 0 ;
}
file_data - > in_urbs_used - - ;
if ( max_transfer_size > urb - > actual_length )
max_transfer_size - = urb - > actual_length ;
else
max_transfer_size = 0 ;
if ( remaining > urb - > actual_length )
this_part = urb - > actual_length ;
else
this_part = remaining ;
print_hex_dump_debug ( " usbtmc " , DUMP_PREFIX_NONE , 16 , 1 ,
urb - > transfer_buffer , urb - > actual_length , true ) ;
if ( copy_to_user ( user_buffer + done ,
urb - > transfer_buffer , this_part ) ) {
usb_free_urb ( urb ) ;
retval = - EFAULT ;
goto error ;
}
remaining - = this_part ;
done + = this_part ;
spin_lock_irq ( & file_data - > err_lock ) ;
if ( urb - > status ) {
/* return the very first error */
retval = file_data - > in_status ;
spin_unlock_irq ( & file_data - > err_lock ) ;
usb_free_urb ( urb ) ;
goto error ;
}
spin_unlock_irq ( & file_data - > err_lock ) ;
if ( urb - > actual_length < bufsize ) {
/* short packet or ZLP received => ready */
usb_free_urb ( urb ) ;
retval = 1 ;
break ;
}
if ( ! ( flags & USBTMC_FLAG_ASYNC ) & &
max_transfer_size > ( bufsize * file_data - > in_urbs_used ) ) {
/* resubmit, since other buffers still not enough */
usb_anchor_urb ( urb , & file_data - > submitted ) ;
retval = usb_submit_urb ( urb , GFP_KERNEL ) ;
if ( unlikely ( retval ) ) {
usb_unanchor_urb ( urb ) ;
usb_free_urb ( urb ) ;
goto error ;
}
file_data - > in_urbs_used + + ;
}
usb_free_urb ( urb ) ;
retval = 0 ;
}
error :
* transferred = done ;
dev_dbg ( dev , " %s: before kill \n " , __func__ ) ;
/* Attention: killing urbs can take long time (2 ms) */
usb_kill_anchored_urbs ( & file_data - > submitted ) ;
dev_dbg ( dev , " %s: after kill \n " , __func__ ) ;
usb_scuttle_anchored_urbs ( & file_data - > in_anchor ) ;
file_data - > in_urbs_used = 0 ;
file_data - > in_status = 0 ; /* no spinlock needed here */
dev_dbg ( dev , " %s: done=%u ret=%d \n " , __func__ , done , retval ) ;
return retval ;
}
static ssize_t usbtmc_ioctl_generic_read ( struct usbtmc_file_data * file_data ,
void __user * arg )
{
struct usbtmc_message msg ;
ssize_t retval = 0 ;
/* mutex already locked */
if ( copy_from_user ( & msg , arg , sizeof ( struct usbtmc_message ) ) )
return - EFAULT ;
retval = usbtmc_generic_read ( file_data , msg . message ,
msg . transfer_size , & msg . transferred ,
msg . flags ) ;
if ( put_user ( msg . transferred ,
& ( ( struct usbtmc_message __user * ) arg ) - > transferred ) )
return - EFAULT ;
return retval ;
}
2018-09-12 11:50:52 +03:00
static void usbtmc_write_bulk_cb ( struct urb * urb )
{
struct usbtmc_file_data * file_data = urb - > context ;
int wakeup = 0 ;
unsigned long flags ;
spin_lock_irqsave ( & file_data - > err_lock , flags ) ;
file_data - > out_transfer_size + = urb - > actual_length ;
/* sync/async unlink faults aren't errors */
if ( urb - > status ) {
if ( ! ( urb - > status = = - ENOENT | |
urb - > status = = - ECONNRESET | |
urb - > status = = - ESHUTDOWN ) )
dev_err ( & file_data - > data - > intf - > dev ,
" %s - nonzero write bulk status received: %d \n " ,
__func__ , urb - > status ) ;
if ( ! file_data - > out_status ) {
file_data - > out_status = urb - > status ;
wakeup = 1 ;
}
}
spin_unlock_irqrestore ( & file_data - > err_lock , flags ) ;
dev_dbg ( & file_data - > data - > intf - > dev ,
" %s - write bulk total size: %u \n " ,
__func__ , file_data - > out_transfer_size ) ;
up ( & file_data - > limit_write_sem ) ;
if ( usb_anchor_empty ( & file_data - > submitted ) | | wakeup )
wake_up_interruptible ( & file_data - > data - > waitq ) ;
}
static ssize_t usbtmc_generic_write ( struct usbtmc_file_data * file_data ,
const void __user * user_buffer ,
u32 transfer_size ,
u32 * transferred ,
u32 flags )
{
struct usbtmc_device_data * data = file_data - > data ;
struct device * dev ;
u32 done = 0 ;
u32 remaining ;
unsigned long expire ;
const u32 bufsize = USBTMC_BUFSIZE ;
struct urb * urb = NULL ;
int retval = 0 ;
u32 timeout ;
* transferred = 0 ;
/* Get pointer to private data structure */
dev = & data - > intf - > dev ;
dev_dbg ( dev , " %s: size=%u flags=0x%X sema=%u \n " ,
__func__ , transfer_size , flags ,
file_data - > limit_write_sem . count ) ;
if ( flags & USBTMC_FLAG_APPEND ) {
spin_lock_irq ( & file_data - > err_lock ) ;
retval = file_data - > out_status ;
spin_unlock_irq ( & file_data - > err_lock ) ;
if ( retval < 0 )
return retval ;
} else {
spin_lock_irq ( & file_data - > err_lock ) ;
file_data - > out_transfer_size = 0 ;
file_data - > out_status = 0 ;
spin_unlock_irq ( & file_data - > err_lock ) ;
}
remaining = transfer_size ;
if ( remaining > INT_MAX )
remaining = INT_MAX ;
timeout = file_data - > timeout ;
expire = msecs_to_jiffies ( timeout ) ;
while ( remaining > 0 ) {
u32 this_part , aligned ;
u8 * buffer = NULL ;
if ( flags & USBTMC_FLAG_ASYNC ) {
if ( down_trylock ( & file_data - > limit_write_sem ) ) {
retval = ( done ) ? ( 0 ) : ( - EAGAIN ) ;
goto exit ;
}
} else {
retval = down_timeout ( & file_data - > limit_write_sem ,
expire ) ;
if ( retval < 0 ) {
retval = - ETIMEDOUT ;
goto error ;
}
}
spin_lock_irq ( & file_data - > err_lock ) ;
retval = file_data - > out_status ;
spin_unlock_irq ( & file_data - > err_lock ) ;
if ( retval < 0 ) {
up ( & file_data - > limit_write_sem ) ;
goto error ;
}
/* prepare next urb to send */
urb = usbtmc_create_urb ( ) ;
if ( ! urb ) {
retval = - ENOMEM ;
up ( & file_data - > limit_write_sem ) ;
goto error ;
}
buffer = urb - > transfer_buffer ;
if ( remaining > bufsize )
this_part = bufsize ;
else
this_part = remaining ;
if ( copy_from_user ( buffer , user_buffer + done , this_part ) ) {
retval = - EFAULT ;
up ( & file_data - > limit_write_sem ) ;
goto error ;
}
print_hex_dump_debug ( " usbtmc " , DUMP_PREFIX_NONE ,
16 , 1 , buffer , this_part , true ) ;
/* fill bulk with 32 bit alignment to meet USBTMC specification
* ( size + 3 & ~ 3 ) rounds up and simplifies user code
*/
aligned = ( this_part + 3 ) & ~ 3 ;
dev_dbg ( dev , " write(size:%u align:%u done:%u) \n " ,
( unsigned int ) this_part ,
( unsigned int ) aligned ,
( unsigned int ) done ) ;
usb_fill_bulk_urb ( urb , data - > usb_dev ,
usb_sndbulkpipe ( data - > usb_dev , data - > bulk_out ) ,
urb - > transfer_buffer , aligned ,
usbtmc_write_bulk_cb , file_data ) ;
usb_anchor_urb ( urb , & file_data - > submitted ) ;
retval = usb_submit_urb ( urb , GFP_KERNEL ) ;
if ( unlikely ( retval ) ) {
usb_unanchor_urb ( urb ) ;
up ( & file_data - > limit_write_sem ) ;
goto error ;
}
usb_free_urb ( urb ) ;
urb = NULL ; /* urb will be finally released by usb driver */
remaining - = this_part ;
done + = this_part ;
}
/* All urbs are on the fly */
if ( ! ( flags & USBTMC_FLAG_ASYNC ) ) {
if ( ! usb_wait_anchor_empty_timeout ( & file_data - > submitted ,
timeout ) ) {
retval = - ETIMEDOUT ;
goto error ;
}
}
retval = 0 ;
goto exit ;
error :
usb_kill_anchored_urbs ( & file_data - > submitted ) ;
exit :
usb_free_urb ( urb ) ;
spin_lock_irq ( & file_data - > err_lock ) ;
if ( ! ( flags & USBTMC_FLAG_ASYNC ) )
done = file_data - > out_transfer_size ;
if ( ! retval & & file_data - > out_status )
retval = file_data - > out_status ;
spin_unlock_irq ( & file_data - > err_lock ) ;
* transferred = done ;
dev_dbg ( dev , " %s: done=%u, retval=%d, urbstat=%d \n " ,
__func__ , done , retval , file_data - > out_status ) ;
return retval ;
}
static ssize_t usbtmc_ioctl_generic_write ( struct usbtmc_file_data * file_data ,
void __user * arg )
{
struct usbtmc_message msg ;
ssize_t retval = 0 ;
/* mutex already locked */
if ( copy_from_user ( & msg , arg , sizeof ( struct usbtmc_message ) ) )
return - EFAULT ;
retval = usbtmc_generic_write ( file_data , msg . message ,
msg . transfer_size , & msg . transferred ,
msg . flags ) ;
if ( put_user ( msg . transferred ,
& ( ( struct usbtmc_message __user * ) arg ) - > transferred ) )
return - EFAULT ;
return retval ;
}
2018-09-12 11:50:53 +03:00
/*
* Get the generic write result
*/
static ssize_t usbtmc_ioctl_write_result ( struct usbtmc_file_data * file_data ,
void __user * arg )
{
u32 transferred ;
int retval ;
spin_lock_irq ( & file_data - > err_lock ) ;
transferred = file_data - > out_transfer_size ;
retval = file_data - > out_status ;
spin_unlock_irq ( & file_data - > err_lock ) ;
if ( put_user ( transferred , ( __u32 __user * ) arg ) )
return - EFAULT ;
return retval ;
}
2013-04-30 09:51:51 +04:00
/*
2016-09-28 21:06:01 +03:00
* Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk - OUT endpoint .
2013-04-30 09:51:51 +04:00
* @ transfer_size : number of bytes to request from the device .
*
* See the USBTMC specification , Table 4.
*
* Also updates bTag_last_write .
*/
2018-07-18 11:45:36 +03:00
static int send_request_dev_dep_msg_in ( struct usbtmc_file_data * file_data ,
2018-09-12 11:51:02 +03:00
u32 transfer_size )
2013-04-30 09:51:51 +04:00
{
2018-07-18 11:45:36 +03:00
struct usbtmc_device_data * data = file_data - > data ;
2013-04-30 09:51:51 +04:00
int retval ;
2014-05-19 15:54:57 +04:00
u8 * buffer ;
2013-04-30 09:51:51 +04:00
int actual ;
2014-05-19 15:54:57 +04:00
buffer = kmalloc ( USBTMC_HEADER_SIZE , GFP_KERNEL ) ;
if ( ! buffer )
return - ENOMEM ;
2013-04-30 09:51:51 +04:00
/* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
* Refer to class specs for details
*/
buffer [ 0 ] = 2 ;
buffer [ 1 ] = data - > bTag ;
2013-07-15 12:59:43 +04:00
buffer [ 2 ] = ~ data - > bTag ;
2013-04-30 09:51:51 +04:00
buffer [ 3 ] = 0 ; /* Reserved */
2013-07-15 12:59:43 +04:00
buffer [ 4 ] = transfer_size > > 0 ;
buffer [ 5 ] = transfer_size > > 8 ;
buffer [ 6 ] = transfer_size > > 16 ;
buffer [ 7 ] = transfer_size > > 24 ;
2018-07-18 11:45:39 +03:00
buffer [ 8 ] = file_data - > term_char_enabled * 2 ;
2013-04-30 09:51:51 +04:00
/* Use term character? */
2018-07-18 11:45:39 +03:00
buffer [ 9 ] = file_data - > term_char ;
2013-04-30 09:51:51 +04:00
buffer [ 10 ] = 0 ; /* Reserved */
buffer [ 11 ] = 0 ; /* Reserved */
/* Send bulk URB */
retval = usb_bulk_msg ( data - > usb_dev ,
usb_sndbulkpipe ( data - > usb_dev ,
data - > bulk_out ) ,
2018-07-18 11:45:36 +03:00
buffer , USBTMC_HEADER_SIZE ,
& actual , file_data - > timeout ) ;
2013-04-30 09:51:51 +04:00
/* Store bTag (in case we need to abort) */
data - > bTag_last_write = data - > bTag ;
/* Increment bTag -- and increment again if zero */
data - > bTag + + ;
if ( ! data - > bTag )
2013-07-15 12:59:43 +04:00
data - > bTag + + ;
2013-04-30 09:51:51 +04:00
2014-05-19 15:54:57 +04:00
kfree ( buffer ) ;
2018-09-12 11:51:02 +03:00
if ( retval < 0 )
dev_err ( & data - > intf - > dev , " %s returned %d \n " ,
__func__ , retval ) ;
2013-04-30 09:51:51 +04:00
2018-09-12 11:51:02 +03:00
return retval ;
2013-04-30 09:51:51 +04:00
}
2008-08-27 03:22:06 +04:00
static ssize_t usbtmc_read ( struct file * filp , char __user * buf ,
size_t count , loff_t * f_pos )
{
2018-07-18 11:45:34 +03:00
struct usbtmc_file_data * file_data ;
2008-08-27 03:22:06 +04:00
struct usbtmc_device_data * data ;
struct device * dev ;
2018-09-12 11:51:02 +03:00
const u32 bufsize = USBTMC_BUFSIZE ;
2009-07-22 19:39:42 +04:00
u32 n_characters ;
2008-08-27 03:22:06 +04:00
u8 * buffer ;
int actual ;
2018-09-12 11:51:02 +03:00
u32 done = 0 ;
u32 remaining ;
2008-08-27 03:22:06 +04:00
int retval ;
/* Get pointer to private data structure */
2018-07-18 11:45:34 +03:00
file_data = filp - > private_data ;
data = file_data - > data ;
2008-08-27 03:22:06 +04:00
dev = & data - > intf - > dev ;
2018-09-12 11:51:02 +03:00
buffer = kmalloc ( bufsize , GFP_KERNEL ) ;
2008-08-27 03:22:06 +04:00
if ( ! buffer )
return - ENOMEM ;
mutex_lock ( & data - > io_mutex ) ;
2009-07-02 13:36:30 +04:00
if ( data - > zombie ) {
retval = - ENODEV ;
goto exit ;
}
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:02 +03:00
if ( count > INT_MAX )
count = INT_MAX ;
dev_dbg ( dev , " %s(count:%zu) \n " , __func__ , count ) ;
2013-04-30 09:51:53 +04:00
2018-07-18 11:45:36 +03:00
retval = send_request_dev_dep_msg_in ( file_data , count ) ;
2013-04-30 09:51:53 +04:00
2018-05-17 20:03:25 +03:00
if ( retval < 0 ) {
2018-09-12 11:51:00 +03:00
if ( file_data - > auto_abort )
2018-05-17 20:03:25 +03:00
usbtmc_ioctl_abort_bulk_out ( data ) ;
goto exit ;
2013-04-30 09:51:53 +04:00
}
/* Loop until we have fetched everything we requested */
2008-08-27 03:22:06 +04:00
remaining = count ;
2018-09-25 02:30:30 +03:00
actual = 0 ;
2013-04-30 09:51:53 +04:00
2018-09-12 11:51:02 +03:00
/* Send bulk URB */
retval = usb_bulk_msg ( data - > usb_dev ,
usb_rcvbulkpipe ( data - > usb_dev ,
data - > bulk_in ) ,
buffer , bufsize , & actual ,
file_data - > timeout ) ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:02 +03:00
dev_dbg ( dev , " %s: bulk_msg retval(%u), actual(%d) \n " ,
__func__ , retval , actual ) ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:02 +03:00
/* Store bTag (in case we need to abort) */
data - > bTag_last_read = data - > bTag ;
2009-07-22 19:39:42 +04:00
2018-09-12 11:51:02 +03:00
if ( retval < 0 ) {
if ( file_data - > auto_abort )
usbtmc_ioctl_abort_bulk_in ( data ) ;
goto exit ;
}
2009-07-22 19:39:42 +04:00
2018-09-12 11:51:02 +03:00
/* Sanity checks for the header */
if ( actual < USBTMC_HEADER_SIZE ) {
dev_err ( dev , " Device sent too small first packet: %u < %u \n " ,
actual , USBTMC_HEADER_SIZE ) ;
if ( file_data - > auto_abort )
usbtmc_ioctl_abort_bulk_in ( data ) ;
goto exit ;
}
2009-06-19 02:37:49 +04:00
2018-09-12 11:51:02 +03:00
if ( buffer [ 0 ] ! = 2 ) {
dev_err ( dev , " Device sent reply with wrong MsgID: %u != 2 \n " ,
buffer [ 0 ] ) ;
if ( file_data - > auto_abort )
usbtmc_ioctl_abort_bulk_in ( data ) ;
goto exit ;
}
2018-09-12 11:50:59 +03:00
2018-09-12 11:51:02 +03:00
if ( buffer [ 1 ] ! = data - > bTag_last_write ) {
dev_err ( dev , " Device sent reply with wrong bTag: %u != %u \n " ,
buffer [ 1 ] , data - > bTag_last_write ) ;
if ( file_data - > auto_abort )
usbtmc_ioctl_abort_bulk_in ( data ) ;
goto exit ;
}
2013-04-30 09:51:53 +04:00
2018-09-12 11:51:02 +03:00
/* How many characters did the instrument send? */
n_characters = buffer [ 4 ] +
( buffer [ 5 ] < < 8 ) +
( buffer [ 6 ] < < 16 ) +
( buffer [ 7 ] < < 24 ) ;
2013-04-30 09:51:53 +04:00
2018-09-12 11:51:02 +03:00
file_data - > bmTransferAttributes = buffer [ 8 ] ;
2013-04-30 09:51:53 +04:00
2018-09-12 11:51:02 +03:00
dev_dbg ( dev , " Bulk-IN header: N_characters(%u), bTransAttr(%u) \n " ,
n_characters , buffer [ 8 ] ) ;
2013-04-30 09:51:53 +04:00
2018-09-12 11:51:02 +03:00
if ( n_characters > remaining ) {
dev_err ( dev , " Device wants to return more data than requested: %u > %zu \n " ,
n_characters , count ) ;
if ( file_data - > auto_abort )
usbtmc_ioctl_abort_bulk_in ( data ) ;
goto exit ;
}
2013-04-30 09:51:53 +04:00
2018-09-12 11:51:02 +03:00
print_hex_dump_debug ( " usbtmc " , DUMP_PREFIX_NONE ,
16 , 1 , buffer , actual , true ) ;
2013-04-30 09:51:53 +04:00
2018-09-12 11:51:02 +03:00
remaining = n_characters ;
2013-04-30 09:51:53 +04:00
2018-09-12 11:51:02 +03:00
/* Remove the USBTMC header */
actual - = USBTMC_HEADER_SIZE ;
2013-04-30 09:51:53 +04:00
2018-09-12 11:51:02 +03:00
/* Remove padding if it exists */
if ( actual > remaining )
actual = remaining ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:02 +03:00
remaining - = actual ;
2013-04-30 09:51:53 +04:00
2018-09-12 11:51:02 +03:00
/* Copy buffer to user space */
if ( copy_to_user ( buf , & buffer [ USBTMC_HEADER_SIZE ] , actual ) ) {
/* There must have been an addressing problem */
retval = - EFAULT ;
goto exit ;
}
2013-04-30 09:51:53 +04:00
2018-09-12 11:51:02 +03:00
if ( ( actual + USBTMC_HEADER_SIZE ) = = bufsize ) {
retval = usbtmc_generic_read ( file_data , buf + actual ,
remaining ,
& done ,
USBTMC_FLAG_IGNORE_TRAILER ) ;
if ( retval < 0 )
goto exit ;
2008-08-27 03:22:06 +04:00
}
2018-09-12 11:51:02 +03:00
done + = actual ;
2008-08-27 03:22:06 +04:00
/* Update file position value */
* f_pos = * f_pos + done ;
retval = done ;
exit :
mutex_unlock ( & data - > io_mutex ) ;
kfree ( buffer ) ;
return retval ;
}
static ssize_t usbtmc_write ( struct file * filp , const char __user * buf ,
size_t count , loff_t * f_pos )
{
2018-07-18 11:45:34 +03:00
struct usbtmc_file_data * file_data ;
2008-08-27 03:22:06 +04:00
struct usbtmc_device_data * data ;
2018-09-12 11:51:01 +03:00
struct urb * urb = NULL ;
ssize_t retval = 0 ;
2008-08-27 03:22:06 +04:00
u8 * buffer ;
2018-09-12 11:51:01 +03:00
u32 remaining , done ;
u32 transfersize , aligned , buflen ;
2008-08-27 03:22:06 +04:00
2018-07-18 11:45:34 +03:00
file_data = filp - > private_data ;
data = file_data - > data ;
2008-08-27 03:22:06 +04:00
mutex_lock ( & data - > io_mutex ) ;
2018-09-12 11:51:01 +03:00
2009-07-02 13:36:30 +04:00
if ( data - > zombie ) {
retval = - ENODEV ;
goto exit ;
}
2008-08-27 03:22:06 +04:00
done = 0 ;
2018-09-12 11:51:01 +03:00
spin_lock_irq ( & file_data - > err_lock ) ;
file_data - > out_transfer_size = 0 ;
file_data - > out_status = 0 ;
spin_unlock_irq ( & file_data - > err_lock ) ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:01 +03:00
if ( ! count )
goto exit ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:01 +03:00
if ( down_trylock ( & file_data - > limit_write_sem ) ) {
/* previous calls were async */
retval = - EBUSY ;
goto exit ;
}
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:01 +03:00
urb = usbtmc_create_urb ( ) ;
if ( ! urb ) {
retval = - ENOMEM ;
up ( & file_data - > limit_write_sem ) ;
goto exit ;
}
buffer = urb - > transfer_buffer ;
buflen = urb - > transfer_buffer_length ;
if ( count > INT_MAX ) {
transfersize = INT_MAX ;
buffer [ 8 ] = 0 ;
} else {
transfersize = count ;
buffer [ 8 ] = file_data - > eom_val ;
}
/* Setup IO buffer for DEV_DEP_MSG_OUT message */
buffer [ 0 ] = 1 ;
buffer [ 1 ] = data - > bTag ;
buffer [ 2 ] = ~ data - > bTag ;
buffer [ 3 ] = 0 ; /* Reserved */
buffer [ 4 ] = transfersize > > 0 ;
buffer [ 5 ] = transfersize > > 8 ;
buffer [ 6 ] = transfersize > > 16 ;
buffer [ 7 ] = transfersize > > 24 ;
/* buffer[8] is set above... */
buffer [ 9 ] = 0 ; /* Reserved */
buffer [ 10 ] = 0 ; /* Reserved */
buffer [ 11 ] = 0 ; /* Reserved */
remaining = transfersize ;
if ( transfersize + USBTMC_HEADER_SIZE > buflen ) {
transfersize = buflen - USBTMC_HEADER_SIZE ;
aligned = buflen ;
} else {
aligned = ( transfersize + ( USBTMC_HEADER_SIZE + 3 ) ) & ~ 3 ;
}
if ( copy_from_user ( & buffer [ USBTMC_HEADER_SIZE ] , buf , transfersize ) ) {
retval = - EFAULT ;
up ( & file_data - > limit_write_sem ) ;
goto exit ;
}
dev_dbg ( & data - > intf - > dev , " %s(size:%u align:%u) \n " , __func__ ,
( unsigned int ) transfersize , ( unsigned int ) aligned ) ;
print_hex_dump_debug ( " usbtmc " , DUMP_PREFIX_NONE ,
16 , 1 , buffer , aligned , true ) ;
usb_fill_bulk_urb ( urb , data - > usb_dev ,
usb_sndbulkpipe ( data - > usb_dev , data - > bulk_out ) ,
urb - > transfer_buffer , aligned ,
usbtmc_write_bulk_cb , file_data ) ;
usb_anchor_urb ( urb , & file_data - > submitted ) ;
retval = usb_submit_urb ( urb , GFP_KERNEL ) ;
if ( unlikely ( retval ) ) {
usb_unanchor_urb ( urb ) ;
up ( & file_data - > limit_write_sem ) ;
goto exit ;
}
remaining - = transfersize ;
data - > bTag_last_write = data - > bTag ;
data - > bTag + + ;
if ( ! data - > bTag )
2008-08-27 03:22:06 +04:00
data - > bTag + + ;
2018-09-12 11:51:01 +03:00
/* call generic_write even when remaining = 0 */
retval = usbtmc_generic_write ( file_data , buf + transfersize , remaining ,
& done , USBTMC_FLAG_APPEND ) ;
/* truncate alignment bytes */
if ( done > remaining )
done = remaining ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:01 +03:00
/*add size of first urb*/
done + = transfersize ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:01 +03:00
if ( retval < 0 ) {
usb_kill_anchored_urbs ( & file_data - > submitted ) ;
dev_err ( & data - > intf - > dev ,
" Unable to send data, error %d \n " , ( int ) retval ) ;
if ( file_data - > auto_abort )
usbtmc_ioctl_abort_bulk_out ( data ) ;
goto exit ;
2008-08-27 03:22:06 +04:00
}
2018-09-12 11:51:01 +03:00
retval = done ;
2008-08-27 03:22:06 +04:00
exit :
2018-09-12 11:51:01 +03:00
usb_free_urb ( urb ) ;
2008-08-27 03:22:06 +04:00
mutex_unlock ( & data - > io_mutex ) ;
return retval ;
}
static int usbtmc_ioctl_clear ( struct usbtmc_device_data * data )
{
struct device * dev ;
u8 * buffer ;
int rv ;
int n ;
2014-06-01 17:43:19 +04:00
int actual = 0 ;
2008-08-27 03:22:06 +04:00
dev = & data - > intf - > dev ;
dev_dbg ( dev , " Sending INITIATE_CLEAR request \n " ) ;
2018-09-12 11:51:03 +03:00
buffer = kmalloc ( USBTMC_BUFSIZE , GFP_KERNEL ) ;
2008-08-27 03:22:06 +04:00
if ( ! buffer )
return - ENOMEM ;
rv = usb_control_msg ( data - > usb_dev ,
usb_rcvctrlpipe ( data - > usb_dev , 0 ) ,
USBTMC_REQUEST_INITIATE_CLEAR ,
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE ,
2018-09-12 11:51:03 +03:00
0 , 0 , buffer , 1 , USB_CTRL_GET_TIMEOUT ) ;
2008-08-27 03:22:06 +04:00
if ( rv < 0 ) {
dev_err ( dev , " usb_control_msg returned %d \n " , rv ) ;
goto exit ;
}
dev_dbg ( dev , " INITIATE_CLEAR returned %x \n " , buffer [ 0 ] ) ;
if ( buffer [ 0 ] ! = USBTMC_STATUS_SUCCESS ) {
dev_err ( dev , " INITIATE_CLEAR returned %x \n " , buffer [ 0 ] ) ;
rv = - EPERM ;
goto exit ;
}
n = 0 ;
usbtmc_clear_check_status :
dev_dbg ( dev , " Sending CHECK_CLEAR_STATUS request \n " ) ;
rv = usb_control_msg ( data - > usb_dev ,
usb_rcvctrlpipe ( data - > usb_dev , 0 ) ,
USBTMC_REQUEST_CHECK_CLEAR_STATUS ,
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE ,
2018-09-12 11:51:03 +03:00
0 , 0 , buffer , 2 , USB_CTRL_GET_TIMEOUT ) ;
2008-08-27 03:22:06 +04:00
if ( rv < 0 ) {
dev_err ( dev , " usb_control_msg returned %d \n " , rv ) ;
goto exit ;
}
dev_dbg ( dev , " CHECK_CLEAR_STATUS returned %x \n " , buffer [ 0 ] ) ;
if ( buffer [ 0 ] = = USBTMC_STATUS_SUCCESS )
goto usbtmc_clear_bulk_out_halt ;
if ( buffer [ 0 ] ! = USBTMC_STATUS_PENDING ) {
dev_err ( dev , " CHECK_CLEAR_STATUS returned %x \n " , buffer [ 0 ] ) ;
rv = - EPERM ;
goto exit ;
}
2018-09-12 11:51:03 +03:00
if ( ( buffer [ 1 ] & 1 ) ! = 0 ) {
2008-08-27 03:22:06 +04:00
do {
dev_dbg ( dev , " Reading from bulk in EP \n " ) ;
2018-09-25 02:30:31 +03:00
actual = 0 ;
2008-08-27 03:22:06 +04:00
rv = usb_bulk_msg ( data - > usb_dev ,
usb_rcvbulkpipe ( data - > usb_dev ,
data - > bulk_in ) ,
2018-09-12 11:51:03 +03:00
buffer , USBTMC_BUFSIZE ,
& actual , USB_CTRL_GET_TIMEOUT ) ;
print_hex_dump_debug ( " usbtmc " , DUMP_PREFIX_NONE ,
16 , 1 , buffer , actual , true ) ;
2008-08-27 03:22:06 +04:00
n + + ;
if ( rv < 0 ) {
dev_err ( dev , " usb_control_msg returned %d \n " ,
rv ) ;
goto exit ;
}
2018-09-12 11:51:03 +03:00
} while ( ( actual = = USBTMC_BUFSIZE ) & &
2008-08-27 03:22:06 +04:00
( n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN ) ) ;
2018-09-12 11:51:03 +03:00
} else {
/* do not stress device with subsequent requests */
msleep ( 50 ) ;
n + + ;
}
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:03 +03:00
if ( n > = USBTMC_MAX_READS_TO_CLEAR_BULK_IN ) {
2008-08-27 03:22:06 +04:00
dev_err ( dev , " Couldn't clear device buffer within %d cycles \n " ,
USBTMC_MAX_READS_TO_CLEAR_BULK_IN ) ;
rv = - EPERM ;
goto exit ;
}
goto usbtmc_clear_check_status ;
usbtmc_clear_bulk_out_halt :
2009-12-03 22:35:59 +03:00
rv = usb_clear_halt ( data - > usb_dev ,
usb_sndbulkpipe ( data - > usb_dev , data - > bulk_out ) ) ;
2008-08-27 03:22:06 +04:00
if ( rv < 0 ) {
2018-09-12 11:51:03 +03:00
dev_err ( dev , " usb_clear_halt returned %d \n " , rv ) ;
2008-08-27 03:22:06 +04:00
goto exit ;
}
rv = 0 ;
exit :
kfree ( buffer ) ;
return rv ;
}
static int usbtmc_ioctl_clear_out_halt ( struct usbtmc_device_data * data )
{
int rv ;
2009-12-03 22:35:59 +03:00
rv = usb_clear_halt ( data - > usb_dev ,
usb_sndbulkpipe ( data - > usb_dev , data - > bulk_out ) ) ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:09 +03:00
if ( rv < 0 )
dev_err ( & data - > usb_dev - > dev , " %s returned %d \n " , __func__ , rv ) ;
return rv ;
2008-08-27 03:22:06 +04:00
}
static int usbtmc_ioctl_clear_in_halt ( struct usbtmc_device_data * data )
{
int rv ;
2009-12-03 22:35:59 +03:00
rv = usb_clear_halt ( data - > usb_dev ,
usb_rcvbulkpipe ( data - > usb_dev , data - > bulk_in ) ) ;
2008-08-27 03:22:06 +04:00
2018-09-12 11:51:09 +03:00
if ( rv < 0 )
dev_err ( & data - > usb_dev - > dev , " %s returned %d \n " , __func__ , rv ) ;
return rv ;
2008-08-27 03:22:06 +04:00
}
2018-09-12 11:50:52 +03:00
static int usbtmc_ioctl_cancel_io ( struct usbtmc_file_data * file_data )
{
spin_lock_irq ( & file_data - > err_lock ) ;
2018-09-12 11:50:54 +03:00
file_data - > in_status = - ECANCELED ;
2018-09-12 11:50:52 +03:00
file_data - > out_status = - ECANCELED ;
spin_unlock_irq ( & file_data - > err_lock ) ;
usb_kill_anchored_urbs ( & file_data - > submitted ) ;
return 0 ;
}
2018-09-12 11:50:56 +03:00
static int usbtmc_ioctl_cleanup_io ( struct usbtmc_file_data * file_data )
{
usb_kill_anchored_urbs ( & file_data - > submitted ) ;
usb_scuttle_anchored_urbs ( & file_data - > in_anchor ) ;
spin_lock_irq ( & file_data - > err_lock ) ;
file_data - > in_status = 0 ;
file_data - > in_transfer_size = 0 ;
file_data - > out_status = 0 ;
file_data - > out_transfer_size = 0 ;
spin_unlock_irq ( & file_data - > err_lock ) ;
file_data - > in_urbs_used = 0 ;
return 0 ;
}
2008-08-27 03:22:06 +04:00
static int get_capabilities ( struct usbtmc_device_data * data )
{
struct device * dev = & data - > usb_dev - > dev ;
char * buffer ;
2009-07-02 18:41:39 +04:00
int rv = 0 ;
2008-08-27 03:22:06 +04:00
buffer = kmalloc ( 0x18 , GFP_KERNEL ) ;
if ( ! buffer )
return - ENOMEM ;
rv = usb_control_msg ( data - > usb_dev , usb_rcvctrlpipe ( data - > usb_dev , 0 ) ,
USBTMC_REQUEST_GET_CAPABILITIES ,
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE ,
2018-09-12 11:51:06 +03:00
0 , 0 , buffer , 0x18 , USB_CTRL_GET_TIMEOUT ) ;
2008-08-27 03:22:06 +04:00
if ( rv < 0 ) {
dev_err ( dev , " usb_control_msg returned %d \n " , rv ) ;
2009-07-02 18:41:39 +04:00
goto err_out ;
2008-08-27 03:22:06 +04:00
}
dev_dbg ( dev , " GET_CAPABILITIES returned %x \n " , buffer [ 0 ] ) ;
if ( buffer [ 0 ] ! = USBTMC_STATUS_SUCCESS ) {
dev_err ( dev , " GET_CAPABILITIES returned %x \n " , buffer [ 0 ] ) ;
2009-07-02 18:41:39 +04:00
rv = - EPERM ;
goto err_out ;
2008-08-27 03:22:06 +04:00
}
2009-09-07 06:47:01 +04:00
dev_dbg ( dev , " Interface capabilities are %x \n " , buffer [ 4 ] ) ;
dev_dbg ( dev , " Device capabilities are %x \n " , buffer [ 5 ] ) ;
dev_dbg ( dev , " USB488 interface capabilities are %x \n " , buffer [ 14 ] ) ;
dev_dbg ( dev , " USB488 device capabilities are %x \n " , buffer [ 15 ] ) ;
2008-08-27 03:22:06 +04:00
data - > capabilities . interface_capabilities = buffer [ 4 ] ;
data - > capabilities . device_capabilities = buffer [ 5 ] ;
data - > capabilities . usb488_interface_capabilities = buffer [ 14 ] ;
data - > capabilities . usb488_device_capabilities = buffer [ 15 ] ;
2016-01-27 21:22:28 +03:00
data - > usb488_caps = ( buffer [ 14 ] & 0x07 ) | ( ( buffer [ 15 ] & 0x0f ) < < 4 ) ;
2009-09-07 06:47:01 +04:00
rv = 0 ;
2008-08-27 03:22:06 +04:00
2009-07-02 18:41:39 +04:00
err_out :
2008-08-27 03:22:06 +04:00
kfree ( buffer ) ;
2009-07-02 18:41:39 +04:00
return rv ;
2008-08-27 03:22:06 +04:00
}
# define capability_attribute(name) \
2013-08-24 03:09:33 +04:00
static ssize_t name # # _show ( struct device * dev , \
2008-08-27 03:22:06 +04:00
struct device_attribute * attr , char * buf ) \
{ \
struct usb_interface * intf = to_usb_interface ( dev ) ; \
struct usbtmc_device_data * data = usb_get_intfdata ( intf ) ; \
\
return sprintf ( buf , " %d \n " , data - > capabilities . name ) ; \
} \
2013-08-24 03:09:33 +04:00
static DEVICE_ATTR_RO ( name )
2008-08-27 03:22:06 +04:00
capability_attribute ( interface_capabilities ) ;
capability_attribute ( device_capabilities ) ;
capability_attribute ( usb488_interface_capabilities ) ;
capability_attribute ( usb488_device_capabilities ) ;
2019-08-06 17:44:56 +03:00
static struct attribute * usbtmc_attrs [ ] = {
2008-08-27 03:22:06 +04:00
& dev_attr_interface_capabilities . attr ,
& dev_attr_device_capabilities . attr ,
& dev_attr_usb488_interface_capabilities . attr ,
& dev_attr_usb488_device_capabilities . attr ,
NULL ,
} ;
2019-08-06 17:44:56 +03:00
ATTRIBUTE_GROUPS ( usbtmc ) ;
2008-08-27 03:22:06 +04:00
static int usbtmc_ioctl_indicator_pulse ( struct usbtmc_device_data * data )
{
struct device * dev ;
u8 * buffer ;
int rv ;
dev = & data - > intf - > dev ;
buffer = kmalloc ( 2 , GFP_KERNEL ) ;
if ( ! buffer )
return - ENOMEM ;
rv = usb_control_msg ( data - > usb_dev ,
usb_rcvctrlpipe ( data - > usb_dev , 0 ) ,
USBTMC_REQUEST_INDICATOR_PULSE ,
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE ,
2018-09-12 11:51:06 +03:00
0 , 0 , buffer , 0x01 , USB_CTRL_GET_TIMEOUT ) ;
2008-08-27 03:22:06 +04:00
if ( rv < 0 ) {
dev_err ( dev , " usb_control_msg returned %d \n " , rv ) ;
goto exit ;
}
dev_dbg ( dev , " INDICATOR_PULSE returned %x \n " , buffer [ 0 ] ) ;
if ( buffer [ 0 ] ! = USBTMC_STATUS_SUCCESS ) {
dev_err ( dev , " INDICATOR_PULSE returned %x \n " , buffer [ 0 ] ) ;
rv = - EPERM ;
goto exit ;
}
rv = 0 ;
exit :
kfree ( buffer ) ;
return rv ;
}
2018-09-12 11:50:51 +03:00
static int usbtmc_ioctl_request ( struct usbtmc_device_data * data ,
void __user * arg )
{
struct device * dev = & data - > intf - > dev ;
struct usbtmc_ctrlrequest request ;
u8 * buffer = NULL ;
int rv ;
unsigned long res ;
res = copy_from_user ( & request , arg , sizeof ( struct usbtmc_ctrlrequest ) ) ;
if ( res )
return - EFAULT ;
if ( request . req . wLength > USBTMC_BUFSIZE )
return - EMSGSIZE ;
if ( request . req . wLength ) {
buffer = kmalloc ( request . req . wLength , GFP_KERNEL ) ;
if ( ! buffer )
return - ENOMEM ;
if ( ( request . req . bRequestType & USB_DIR_IN ) = = 0 ) {
/* Send control data to device */
res = copy_from_user ( buffer , request . data ,
request . req . wLength ) ;
if ( res ) {
rv = - EFAULT ;
goto exit ;
}
}
}
rv = usb_control_msg ( data - > usb_dev ,
usb_rcvctrlpipe ( data - > usb_dev , 0 ) ,
request . req . bRequest ,
request . req . bRequestType ,
request . req . wValue ,
request . req . wIndex ,
buffer , request . req . wLength , USB_CTRL_GET_TIMEOUT ) ;
if ( rv < 0 ) {
dev_err ( dev , " %s failed %d \n " , __func__ , rv ) ;
goto exit ;
}
if ( rv & & ( request . req . bRequestType & USB_DIR_IN ) ) {
/* Read control data from device */
res = copy_to_user ( request . data , buffer , rv ) ;
if ( res )
rv = - EFAULT ;
}
exit :
kfree ( buffer ) ;
return rv ;
}
2018-07-18 11:45:36 +03:00
/*
* Get the usb timeout value
*/
static int usbtmc_ioctl_get_timeout ( struct usbtmc_file_data * file_data ,
void __user * arg )
{
u32 timeout ;
timeout = file_data - > timeout ;
return put_user ( timeout , ( __u32 __user * ) arg ) ;
}
/*
* Set the usb timeout value
*/
static int usbtmc_ioctl_set_timeout ( struct usbtmc_file_data * file_data ,
void __user * arg )
{
u32 timeout ;
if ( get_user ( timeout , ( __u32 __user * ) arg ) )
return - EFAULT ;
/* Note that timeout = 0 means
* MAX_SCHEDULE_TIMEOUT in usb_control_msg
*/
if ( timeout < USBTMC_MIN_TIMEOUT )
return - EINVAL ;
file_data - > timeout = timeout ;
return 0 ;
}
2018-07-18 11:45:38 +03:00
/*
* enables / disables sending EOM on write
*/
static int usbtmc_ioctl_eom_enable ( struct usbtmc_file_data * file_data ,
void __user * arg )
{
u8 eom_enable ;
if ( copy_from_user ( & eom_enable , arg , sizeof ( eom_enable ) ) )
return - EFAULT ;
if ( eom_enable > 1 )
return - EINVAL ;
file_data - > eom_val = eom_enable ;
return 0 ;
}
2018-07-18 11:45:39 +03:00
/*
* Configure termination character for read ( )
*/
static int usbtmc_ioctl_config_termc ( struct usbtmc_file_data * file_data ,
void __user * arg )
{
struct usbtmc_termchar termc ;
if ( copy_from_user ( & termc , arg , sizeof ( termc ) ) )
return - EFAULT ;
if ( ( termc . term_char_enabled > 1 ) | |
( termc . term_char_enabled & &
! ( file_data - > data - > capabilities . device_capabilities & 1 ) ) )
return - EINVAL ;
file_data - > term_char = termc . term_char ;
file_data - > term_char_enabled = termc . term_char_enabled ;
return 0 ;
}
2008-08-27 03:22:06 +04:00
static long usbtmc_ioctl ( struct file * file , unsigned int cmd , unsigned long arg )
{
2018-07-18 11:45:34 +03:00
struct usbtmc_file_data * file_data ;
2008-08-27 03:22:06 +04:00
struct usbtmc_device_data * data ;
int retval = - EBADRQC ;
2018-09-12 11:51:00 +03:00
__u8 tmp_byte ;
2008-08-27 03:22:06 +04:00
2018-07-18 11:45:34 +03:00
file_data = file - > private_data ;
data = file_data - > data ;
2008-08-27 03:22:06 +04:00
mutex_lock ( & data - > io_mutex ) ;
2009-07-02 13:36:30 +04:00
if ( data - > zombie ) {
retval = - ENODEV ;
goto skip_io_on_zombie ;
}
2008-08-27 03:22:06 +04:00
switch ( cmd ) {
case USBTMC_IOCTL_CLEAR_OUT_HALT :
retval = usbtmc_ioctl_clear_out_halt ( data ) ;
2009-06-16 00:13:05 +04:00
break ;
2008-08-27 03:22:06 +04:00
case USBTMC_IOCTL_CLEAR_IN_HALT :
retval = usbtmc_ioctl_clear_in_halt ( data ) ;
2009-06-16 00:13:05 +04:00
break ;
2008-08-27 03:22:06 +04:00
case USBTMC_IOCTL_INDICATOR_PULSE :
retval = usbtmc_ioctl_indicator_pulse ( data ) ;
2009-06-16 00:13:05 +04:00
break ;
2008-08-27 03:22:06 +04:00
case USBTMC_IOCTL_CLEAR :
retval = usbtmc_ioctl_clear ( data ) ;
2009-06-16 00:13:05 +04:00
break ;
2008-08-27 03:22:06 +04:00
case USBTMC_IOCTL_ABORT_BULK_OUT :
retval = usbtmc_ioctl_abort_bulk_out ( data ) ;
2009-06-16 00:13:05 +04:00
break ;
2008-08-27 03:22:06 +04:00
case USBTMC_IOCTL_ABORT_BULK_IN :
retval = usbtmc_ioctl_abort_bulk_in ( data ) ;
2009-06-16 00:13:05 +04:00
break ;
2016-01-27 21:09:24 +03:00
2018-09-12 11:50:51 +03:00
case USBTMC_IOCTL_CTRL_REQUEST :
retval = usbtmc_ioctl_request ( data , ( void __user * ) arg ) ;
break ;
2018-07-18 11:45:36 +03:00
case USBTMC_IOCTL_GET_TIMEOUT :
retval = usbtmc_ioctl_get_timeout ( file_data ,
( void __user * ) arg ) ;
break ;
case USBTMC_IOCTL_SET_TIMEOUT :
retval = usbtmc_ioctl_set_timeout ( file_data ,
( void __user * ) arg ) ;
break ;
2018-07-18 11:45:38 +03:00
case USBTMC_IOCTL_EOM_ENABLE :
retval = usbtmc_ioctl_eom_enable ( file_data ,
( void __user * ) arg ) ;
break ;
2018-07-18 11:45:39 +03:00
case USBTMC_IOCTL_CONFIG_TERMCHAR :
retval = usbtmc_ioctl_config_termc ( file_data ,
( void __user * ) arg ) ;
break ;
2018-09-12 11:50:52 +03:00
case USBTMC_IOCTL_WRITE :
retval = usbtmc_ioctl_generic_write ( file_data ,
( void __user * ) arg ) ;
break ;
2018-09-12 11:50:54 +03:00
case USBTMC_IOCTL_READ :
retval = usbtmc_ioctl_generic_read ( file_data ,
( void __user * ) arg ) ;
break ;
2018-09-12 11:50:53 +03:00
case USBTMC_IOCTL_WRITE_RESULT :
retval = usbtmc_ioctl_write_result ( file_data ,
( void __user * ) arg ) ;
break ;
2018-09-12 11:51:07 +03:00
case USBTMC_IOCTL_API_VERSION :
retval = put_user ( USBTMC_API_VERSION ,
( __u32 __user * ) arg ) ;
break ;
2016-01-27 21:22:28 +03:00
case USBTMC488_IOCTL_GET_CAPS :
2018-09-12 11:51:09 +03:00
retval = put_user ( data - > usb488_caps ,
( unsigned char __user * ) arg ) ;
2016-01-27 21:22:28 +03:00
break ;
2016-01-27 21:09:24 +03:00
case USBTMC488_IOCTL_READ_STB :
2018-07-18 11:45:34 +03:00
retval = usbtmc488_ioctl_read_stb ( file_data ,
( void __user * ) arg ) ;
2016-01-27 21:09:24 +03:00
break ;
2016-01-27 21:25:24 +03:00
case USBTMC488_IOCTL_REN_CONTROL :
retval = usbtmc488_ioctl_simple ( data , ( void __user * ) arg ,
USBTMC488_REQUEST_REN_CONTROL ) ;
break ;
case USBTMC488_IOCTL_GOTO_LOCAL :
retval = usbtmc488_ioctl_simple ( data , ( void __user * ) arg ,
USBTMC488_REQUEST_GOTO_LOCAL ) ;
break ;
case USBTMC488_IOCTL_LOCAL_LOCKOUT :
retval = usbtmc488_ioctl_simple ( data , ( void __user * ) arg ,
USBTMC488_REQUEST_LOCAL_LOCKOUT ) ;
break ;
2018-07-18 11:45:37 +03:00
case USBTMC488_IOCTL_TRIGGER :
retval = usbtmc488_ioctl_trigger ( file_data ) ;
break ;
2018-09-12 11:50:55 +03:00
2018-09-12 11:50:58 +03:00
case USBTMC488_IOCTL_WAIT_SRQ :
retval = usbtmc488_ioctl_wait_srq ( file_data ,
( __u32 __user * ) arg ) ;
break ;
2018-09-12 11:50:59 +03:00
case USBTMC_IOCTL_MSG_IN_ATTR :
retval = put_user ( file_data - > bmTransferAttributes ,
( __u8 __user * ) arg ) ;
break ;
2018-09-12 11:51:00 +03:00
case USBTMC_IOCTL_AUTO_ABORT :
retval = get_user ( tmp_byte , ( unsigned char __user * ) arg ) ;
if ( retval = = 0 )
file_data - > auto_abort = ! ! tmp_byte ;
break ;
2018-09-12 11:50:55 +03:00
case USBTMC_IOCTL_CANCEL_IO :
retval = usbtmc_ioctl_cancel_io ( file_data ) ;
break ;
2018-09-12 11:50:56 +03:00
case USBTMC_IOCTL_CLEANUP_IO :
retval = usbtmc_ioctl_cleanup_io ( file_data ) ;
break ;
2008-08-27 03:22:06 +04:00
}
2009-07-02 13:36:30 +04:00
skip_io_on_zombie :
2008-08-27 03:22:06 +04:00
mutex_unlock ( & data - > io_mutex ) ;
return retval ;
}
2016-01-27 21:15:15 +03:00
static int usbtmc_fasync ( int fd , struct file * file , int on )
{
2018-07-18 11:45:34 +03:00
struct usbtmc_file_data * file_data = file - > private_data ;
2016-01-27 21:15:15 +03:00
2018-07-18 11:45:34 +03:00
return fasync_helper ( fd , file , on , & file_data - > data - > fasync ) ;
2016-01-27 21:15:15 +03:00
}
2017-07-03 13:39:46 +03:00
static __poll_t usbtmc_poll ( struct file * file , poll_table * wait )
2016-01-27 21:19:14 +03:00
{
2018-07-18 11:45:34 +03:00
struct usbtmc_file_data * file_data = file - > private_data ;
struct usbtmc_device_data * data = file_data - > data ;
2017-07-03 13:39:46 +03:00
__poll_t mask ;
2016-01-27 21:19:14 +03:00
mutex_lock ( & data - > io_mutex ) ;
if ( data - > zombie ) {
2018-02-12 01:34:03 +03:00
mask = EPOLLHUP | EPOLLERR ;
2016-01-27 21:19:14 +03:00
goto no_poll ;
}
poll_wait ( file , & data - > waitq , wait ) ;
2018-09-12 11:50:54 +03:00
/* Note that EPOLLPRI is now assigned to SRQ, and
* EPOLLIN | EPOLLRDNORM to normal read data .
*/
2018-09-12 11:50:52 +03:00
mask = 0 ;
if ( atomic_read ( & file_data - > srq_asserted ) )
mask | = EPOLLPRI ;
2018-09-12 11:50:54 +03:00
/* Note that the anchor submitted includes all urbs for BULK IN
* and OUT . So EPOLLOUT is signaled when BULK OUT is empty and
* all BULK IN urbs are completed and moved to in_anchor .
*/
2018-09-12 11:50:52 +03:00
if ( usb_anchor_empty ( & file_data - > submitted ) )
mask | = ( EPOLLOUT | EPOLLWRNORM ) ;
2018-09-12 11:50:54 +03:00
if ( ! usb_anchor_empty ( & file_data - > in_anchor ) )
mask | = ( EPOLLIN | EPOLLRDNORM ) ;
2018-09-12 11:50:52 +03:00
spin_lock_irq ( & file_data - > err_lock ) ;
2018-09-12 11:50:54 +03:00
if ( file_data - > in_status | | file_data - > out_status )
2018-09-12 11:50:52 +03:00
mask | = EPOLLERR ;
spin_unlock_irq ( & file_data - > err_lock ) ;
dev_dbg ( & data - > intf - > dev , " poll mask = %x \n " , mask ) ;
2016-01-27 21:19:14 +03:00
no_poll :
mutex_unlock ( & data - > io_mutex ) ;
return mask ;
}
2009-10-02 02:43:56 +04:00
static const struct file_operations fops = {
2008-08-27 03:22:06 +04:00
. owner = THIS_MODULE ,
. read = usbtmc_read ,
. write = usbtmc_write ,
. open = usbtmc_open ,
. release = usbtmc_release ,
2018-09-12 11:50:52 +03:00
. flush = usbtmc_flush ,
2008-08-27 03:22:06 +04:00
. unlocked_ioctl = usbtmc_ioctl ,
2018-07-24 12:05:28 +03:00
# ifdef CONFIG_COMPAT
. compat_ioctl = usbtmc_ioctl ,
# endif
2016-01-27 21:15:15 +03:00
. fasync = usbtmc_fasync ,
2016-01-27 21:19:14 +03:00
. poll = usbtmc_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-15 20:52:59 +04:00
. llseek = default_llseek ,
2008-08-27 03:22:06 +04:00
} ;
static struct usb_class_driver usbtmc_class = {
. name = " usbtmc%d " ,
. fops = & fops ,
. minor_base = USBTMC_MINOR_BASE ,
} ;
2016-01-27 21:09:24 +03:00
static void usbtmc_interrupt ( struct urb * urb )
{
struct usbtmc_device_data * data = urb - > context ;
struct device * dev = & data - > intf - > dev ;
int status = urb - > status ;
int rv ;
dev_dbg ( & data - > intf - > dev , " int status: %d len %d \n " ,
status , urb - > actual_length ) ;
switch ( status ) {
case 0 : /* SUCCESS */
/* check for valid STB notification */
if ( data - > iin_buffer [ 0 ] > 0x81 ) {
data - > bNotify1 = data - > iin_buffer [ 0 ] ;
data - > bNotify2 = data - > iin_buffer [ 1 ] ;
atomic_set ( & data - > iin_data_valid , 1 ) ;
wake_up_interruptible ( & data - > waitq ) ;
goto exit ;
}
2016-01-27 21:15:15 +03:00
/* check for SRQ notification */
if ( data - > iin_buffer [ 0 ] = = 0x81 ) {
2018-07-18 11:45:34 +03:00
unsigned long flags ;
struct list_head * elem ;
2016-01-27 21:15:15 +03:00
if ( data - > fasync )
kill_fasync ( & data - > fasync ,
2018-07-18 11:45:34 +03:00
SIGIO , POLL_PRI ) ;
2016-01-27 21:15:15 +03:00
2018-07-18 11:45:34 +03:00
spin_lock_irqsave ( & data - > dev_lock , flags ) ;
list_for_each ( elem , & data - > file_list ) {
struct usbtmc_file_data * file_data ;
file_data = list_entry ( elem ,
struct usbtmc_file_data ,
file_elem ) ;
file_data - > srq_byte = data - > iin_buffer [ 1 ] ;
atomic_set ( & file_data - > srq_asserted , 1 ) ;
}
spin_unlock_irqrestore ( & data - > dev_lock , flags ) ;
dev_dbg ( dev , " srq received bTag %x stb %x \n " ,
( unsigned int ) data - > iin_buffer [ 0 ] ,
( unsigned int ) data - > iin_buffer [ 1 ] ) ;
wake_up_interruptible_all ( & data - > waitq ) ;
2016-01-27 21:15:15 +03:00
goto exit ;
}
2018-07-18 11:45:34 +03:00
dev_warn ( dev , " invalid notification: %x \n " ,
data - > iin_buffer [ 0 ] ) ;
2016-01-27 21:09:24 +03:00
break ;
case - EOVERFLOW :
dev_err ( dev , " overflow with length %d, actual length is %d \n " ,
data - > iin_wMaxPacketSize , urb - > actual_length ) ;
2017-10-25 05:53:09 +03:00
/* fall through */
2016-01-27 21:09:24 +03:00
case - ECONNRESET :
case - ENOENT :
case - ESHUTDOWN :
case - EILSEQ :
case - ETIME :
2018-07-18 11:45:34 +03:00
case - EPIPE :
2016-01-27 21:09:24 +03:00
/* urb terminated, clean up */
dev_dbg ( dev , " urb terminated, status: %d \n " , status ) ;
return ;
default :
dev_err ( dev , " unknown status received: %d \n " , status ) ;
}
exit :
rv = usb_submit_urb ( urb , GFP_ATOMIC ) ;
if ( rv )
dev_err ( dev , " usb_submit_urb failed: %d \n " , rv ) ;
}
static void usbtmc_free_int ( struct usbtmc_device_data * data )
{
if ( ! data - > iin_ep_present | | ! data - > iin_urb )
return ;
usb_kill_urb ( data - > iin_urb ) ;
kfree ( data - > iin_buffer ) ;
2018-09-12 11:50:57 +03:00
data - > iin_buffer = NULL ;
2016-01-27 21:09:24 +03:00
usb_free_urb ( data - > iin_urb ) ;
2018-09-12 11:50:57 +03:00
data - > iin_urb = NULL ;
2016-01-27 21:09:24 +03:00
kref_put ( & data - > kref , usbtmc_delete ) ;
}
2008-08-27 03:22:06 +04:00
static int usbtmc_probe ( struct usb_interface * intf ,
const struct usb_device_id * id )
{
struct usbtmc_device_data * data ;
struct usb_host_interface * iface_desc ;
2017-03-28 11:33:16 +03:00
struct usb_endpoint_descriptor * bulk_in , * bulk_out , * int_in ;
2008-08-27 03:22:06 +04:00
int retcode ;
dev_dbg ( & intf - > dev , " %s called \n " , __func__ ) ;
2017-03-14 19:55:45 +03:00
data = kzalloc ( sizeof ( * data ) , GFP_KERNEL ) ;
2014-10-14 11:55:56 +04:00
if ( ! data )
2008-08-27 03:22:06 +04:00
return - ENOMEM ;
data - > intf = intf ;
data - > id = id ;
data - > usb_dev = usb_get_dev ( interface_to_usbdev ( intf ) ) ;
usb_set_intfdata ( intf , data ) ;
kref_init ( & data - > kref ) ;
mutex_init ( & data - > io_mutex ) ;
2016-01-27 21:09:24 +03:00
init_waitqueue_head ( & data - > waitq ) ;
atomic_set ( & data - > iin_data_valid , 0 ) ;
2018-07-18 11:45:34 +03:00
INIT_LIST_HEAD ( & data - > file_list ) ;
spin_lock_init ( & data - > dev_lock ) ;
2009-07-02 13:36:30 +04:00
data - > zombie = 0 ;
2008-08-27 03:22:06 +04:00
/* Initialize USBTMC bTag and other fields */
data - > bTag = 1 ;
2016-01-27 21:09:24 +03:00
/* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */
data - > iin_bTag = 2 ;
2008-08-27 03:22:06 +04:00
/* USBTMC devices have only one setting, so use that */
iface_desc = data - > intf - > cur_altsetting ;
2016-01-27 21:09:24 +03:00
data - > ifnum = iface_desc - > desc . bInterfaceNumber ;
2008-08-27 03:22:06 +04:00
2017-03-28 11:33:16 +03:00
/* Find bulk endpoints */
retcode = usb_find_common_endpoints ( iface_desc ,
& bulk_in , & bulk_out , NULL , NULL ) ;
if ( retcode ) {
2017-03-14 19:55:45 +03:00
dev_err ( & intf - > dev , " bulk endpoints not found \n " ) ;
goto err_put ;
}
2019-08-20 12:28:25 +03:00
retcode = - EINVAL ;
2017-03-28 11:33:16 +03:00
data - > bulk_in = bulk_in - > bEndpointAddress ;
2018-09-12 11:50:54 +03:00
data - > wMaxPacketSize = usb_endpoint_maxp ( bulk_in ) ;
2019-08-20 12:28:25 +03:00
if ( ! data - > wMaxPacketSize )
goto err_put ;
2017-03-28 11:33:16 +03:00
dev_dbg ( & intf - > dev , " Found bulk in endpoint at %u \n " , data - > bulk_in ) ;
data - > bulk_out = bulk_out - > bEndpointAddress ;
dev_dbg ( & intf - > dev , " Found Bulk out endpoint at %u \n " , data - > bulk_out ) ;
2016-01-27 21:09:24 +03:00
/* Find int endpoint */
2017-03-28 11:33:16 +03:00
retcode = usb_find_int_in_endpoint ( iface_desc , & int_in ) ;
if ( ! retcode ) {
data - > iin_ep_present = 1 ;
data - > iin_ep = int_in - > bEndpointAddress ;
data - > iin_wMaxPacketSize = usb_endpoint_maxp ( int_in ) ;
data - > iin_interval = int_in - > bInterval ;
dev_dbg ( & intf - > dev , " Found Int in endpoint at %u \n " ,
2016-01-27 21:09:24 +03:00
data - > iin_ep ) ;
}
2008-08-27 03:22:06 +04:00
retcode = get_capabilities ( data ) ;
if ( retcode )
dev_err ( & intf - > dev , " can't read capabilities \n " ) ;
2016-01-27 21:09:24 +03:00
if ( data - > iin_ep_present ) {
/* allocate int urb */
data - > iin_urb = usb_alloc_urb ( 0 , GFP_KERNEL ) ;
2017-03-14 19:55:46 +03:00
if ( ! data - > iin_urb ) {
retcode = - ENOMEM ;
2016-01-27 21:09:24 +03:00
goto error_register ;
2017-03-14 19:55:46 +03:00
}
2016-01-27 21:09:24 +03:00
2016-09-28 21:06:01 +03:00
/* Protect interrupt in endpoint data until iin_urb is freed */
2016-01-27 21:09:24 +03:00
kref_get ( & data - > kref ) ;
/* allocate buffer for interrupt in */
data - > iin_buffer = kmalloc ( data - > iin_wMaxPacketSize ,
GFP_KERNEL ) ;
2017-03-14 19:55:46 +03:00
if ( ! data - > iin_buffer ) {
retcode = - ENOMEM ;
2016-01-27 21:09:24 +03:00
goto error_register ;
2017-03-14 19:55:46 +03:00
}
2016-01-27 21:09:24 +03:00
/* fill interrupt urb */
usb_fill_int_urb ( data - > iin_urb , data - > usb_dev ,
usb_rcvintpipe ( data - > usb_dev , data - > iin_ep ) ,
data - > iin_buffer , data - > iin_wMaxPacketSize ,
usbtmc_interrupt ,
data , data - > iin_interval ) ;
retcode = usb_submit_urb ( data - > iin_urb , GFP_KERNEL ) ;
if ( retcode ) {
dev_err ( & intf - > dev , " Failed to submit iin_urb \n " ) ;
goto error_register ;
}
}
2008-08-27 03:22:06 +04:00
retcode = usb_register_dev ( intf , & usbtmc_class ) ;
if ( retcode ) {
2018-09-12 11:51:11 +03:00
dev_err ( & intf - > dev , " Not able to get a minor (base %u, slice default): %d \n " ,
USBTMC_MINOR_BASE ,
2008-08-27 03:22:06 +04:00
retcode ) ;
goto error_register ;
}
dev_dbg ( & intf - > dev , " Using minor number %d \n " , intf - > minor ) ;
return 0 ;
error_register :
2016-01-27 21:09:24 +03:00
usbtmc_free_int ( data ) ;
2017-03-14 19:55:45 +03:00
err_put :
2008-08-27 03:22:06 +04:00
kref_put ( & data - > kref , usbtmc_delete ) ;
return retcode ;
}
static void usbtmc_disconnect ( struct usb_interface * intf )
{
2018-07-18 11:45:34 +03:00
struct usbtmc_device_data * data = usb_get_intfdata ( intf ) ;
2018-09-12 11:50:52 +03:00
struct list_head * elem ;
2008-08-27 03:22:06 +04:00
usb_deregister_dev ( intf , & usbtmc_class ) ;
2009-07-02 13:36:30 +04:00
mutex_lock ( & data - > io_mutex ) ;
data - > zombie = 1 ;
2018-07-18 11:45:34 +03:00
wake_up_interruptible_all ( & data - > waitq ) ;
2018-09-12 11:50:52 +03:00
list_for_each ( elem , & data - > file_list ) {
struct usbtmc_file_data * file_data ;
file_data = list_entry ( elem ,
struct usbtmc_file_data ,
file_elem ) ;
usb_kill_anchored_urbs ( & file_data - > submitted ) ;
2018-09-12 11:50:54 +03:00
usb_scuttle_anchored_urbs ( & file_data - > in_anchor ) ;
2018-09-12 11:50:52 +03:00
}
2009-07-02 13:36:30 +04:00
mutex_unlock ( & data - > io_mutex ) ;
2016-02-18 12:03:00 +03:00
usbtmc_free_int ( data ) ;
2008-08-27 03:22:06 +04:00
kref_put ( & data - > kref , usbtmc_delete ) ;
}
2018-09-12 11:50:52 +03:00
static void usbtmc_draw_down ( struct usbtmc_file_data * file_data )
{
int time ;
time = usb_wait_anchor_empty_timeout ( & file_data - > submitted , 1000 ) ;
if ( ! time )
usb_kill_anchored_urbs ( & file_data - > submitted ) ;
2018-09-12 11:50:54 +03:00
usb_scuttle_anchored_urbs ( & file_data - > in_anchor ) ;
2018-09-12 11:50:52 +03:00
}
2009-09-24 02:33:45 +04:00
static int usbtmc_suspend ( struct usb_interface * intf , pm_message_t message )
2009-07-02 13:44:33 +04:00
{
2018-09-12 11:50:52 +03:00
struct usbtmc_device_data * data = usb_get_intfdata ( intf ) ;
struct list_head * elem ;
if ( ! data )
return 0 ;
mutex_lock ( & data - > io_mutex ) ;
list_for_each ( elem , & data - > file_list ) {
struct usbtmc_file_data * file_data ;
file_data = list_entry ( elem ,
struct usbtmc_file_data ,
file_elem ) ;
usbtmc_draw_down ( file_data ) ;
}
2018-09-12 11:50:57 +03:00
if ( data - > iin_ep_present & & data - > iin_urb )
usb_kill_urb ( data - > iin_urb ) ;
2018-09-12 11:50:52 +03:00
mutex_unlock ( & data - > io_mutex ) ;
2009-07-02 13:44:33 +04:00
return 0 ;
}
2009-09-24 02:33:45 +04:00
static int usbtmc_resume ( struct usb_interface * intf )
2009-07-02 13:44:33 +04:00
{
2018-09-12 11:50:57 +03:00
struct usbtmc_device_data * data = usb_get_intfdata ( intf ) ;
int retcode = 0 ;
if ( data - > iin_ep_present & & data - > iin_urb )
retcode = usb_submit_urb ( data - > iin_urb , GFP_KERNEL ) ;
if ( retcode )
dev_err ( & intf - > dev , " Failed to submit iin_urb \n " ) ;
return retcode ;
2009-07-02 13:44:33 +04:00
}
2018-09-12 11:50:52 +03:00
static int usbtmc_pre_reset ( struct usb_interface * intf )
{
struct usbtmc_device_data * data = usb_get_intfdata ( intf ) ;
struct list_head * elem ;
if ( ! data )
return 0 ;
mutex_lock ( & data - > io_mutex ) ;
list_for_each ( elem , & data - > file_list ) {
struct usbtmc_file_data * file_data ;
file_data = list_entry ( elem ,
struct usbtmc_file_data ,
file_elem ) ;
usbtmc_ioctl_cancel_io ( file_data ) ;
}
return 0 ;
}
static int usbtmc_post_reset ( struct usb_interface * intf )
{
struct usbtmc_device_data * data = usb_get_intfdata ( intf ) ;
mutex_unlock ( & data - > io_mutex ) ;
return 0 ;
}
2008-08-27 03:22:06 +04:00
static struct usb_driver usbtmc_driver = {
. name = " usbtmc " ,
. id_table = usbtmc_devices ,
. probe = usbtmc_probe ,
2009-07-02 13:44:33 +04:00
. disconnect = usbtmc_disconnect ,
. suspend = usbtmc_suspend ,
. resume = usbtmc_resume ,
2018-09-12 11:50:52 +03:00
. pre_reset = usbtmc_pre_reset ,
. post_reset = usbtmc_post_reset ,
2019-08-06 17:44:56 +03:00
. dev_groups = usbtmc_groups ,
2008-08-27 03:22:06 +04:00
} ;
2011-11-18 21:34:02 +04:00
module_usb_driver ( usbtmc_driver ) ;
2008-08-27 03:22:06 +04:00
MODULE_LICENSE ( " GPL " ) ;