2019-05-19 15:08:20 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2005-05-10 12:59:13 +04:00
/*
* SCSI Media Changer device driver for Linux 2.6
*
* ( c ) 1996 - 2003 Gerd Knorr < kraxel @ bytesex . org >
*
*/
# define VERSION "0.25"
# include <linux/module.h>
# include <linux/init.h>
# include <linux/fs.h>
# include <linux/kernel.h>
# include <linux/mm.h>
# include <linux/major.h>
# include <linux/string.h>
# include <linux/errno.h>
# include <linux/interrupt.h>
# include <linux/blkdev.h>
# include <linux/completion.h>
# include <linux/compat.h>
# include <linux/chio.h> /* here are all the ioctls */
2006-01-11 15:16:10 +03:00
# include <linux/mutex.h>
2008-01-24 11:24:50 +03:00
# include <linux/idr.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>
2005-05-10 12:59:13 +04:00
# include <scsi/scsi.h>
# include <scsi/scsi_cmnd.h>
# include <scsi/scsi_driver.h>
# include <scsi/scsi_ioctl.h>
# include <scsi/scsi_host.h>
# include <scsi/scsi_device.h>
2005-06-13 07:37:10 +04:00
# include <scsi/scsi_eh.h>
2005-05-10 12:59:13 +04:00
# include <scsi/scsi_dbg.h>
# define CH_DT_MAX 16
# define CH_TYPES 8
2008-01-24 11:24:50 +03:00
# define CH_MAX_DEVS 128
2005-05-10 12:59:13 +04:00
MODULE_DESCRIPTION ( " device driver for scsi media changer devices " ) ;
MODULE_AUTHOR ( " Gerd Knorr <kraxel@bytesex.org> " ) ;
MODULE_LICENSE ( " GPL " ) ;
2006-03-08 11:14:20 +03:00
MODULE_ALIAS_CHARDEV_MAJOR ( SCSI_CHANGER_MAJOR ) ;
2009-03-10 19:19:44 +03:00
MODULE_ALIAS_SCSI_DEVICE ( TYPE_MEDIUM_CHANGER ) ;
2005-05-10 12:59:13 +04:00
static int init = 1 ;
module_param ( init , int , 0444 ) ;
MODULE_PARM_DESC ( init , \
" initialize element status on driver load (default: on) " ) ;
static int timeout_move = 300 ;
module_param ( timeout_move , int , 0644 ) ;
MODULE_PARM_DESC ( timeout_move , " timeout for move commands "
" (default: 300 seconds) " ) ;
static int timeout_init = 3600 ;
module_param ( timeout_init , int , 0644 ) ;
MODULE_PARM_DESC ( timeout_init , " timeout for INITIALIZE ELEMENT STATUS "
" (default: 3600 seconds) " ) ;
static int verbose = 1 ;
module_param ( verbose , int , 0644 ) ;
MODULE_PARM_DESC ( verbose , " be verbose (default: on) " ) ;
static int debug = 0 ;
module_param ( debug , int , 0644 ) ;
MODULE_PARM_DESC ( debug , " enable/disable debug messages, also prints more "
" detailed sense codes on scsi errors (default: off) " ) ;
static int dt_id [ CH_DT_MAX ] = { [ 0 . . . ( CH_DT_MAX - 1 ) ] = - 1 } ;
static int dt_lun [ CH_DT_MAX ] ;
module_param_array ( dt_id , int , NULL , 0444 ) ;
module_param_array ( dt_lun , int , NULL , 0444 ) ;
/* tell the driver about vendor-specific slots */
static int vendor_firsts [ CH_TYPES - 4 ] ;
static int vendor_counts [ CH_TYPES - 4 ] ;
module_param_array ( vendor_firsts , int , NULL , 0444 ) ;
module_param_array ( vendor_counts , int , NULL , 0444 ) ;
2005-11-28 18:22:25 +03:00
static const char * vendor_labels [ CH_TYPES - 4 ] = {
2005-05-10 12:59:13 +04:00
" v0 " , " v1 " , " v2 " , " v3 "
} ;
// module_param_string_array(vendor_labels, NULL, 0444);
2014-06-25 18:39:56 +04:00
# define ch_printk(prefix, ch, fmt, a...) \
2014-10-24 16:26:44 +04:00
sdev_prefix_printk ( prefix , ( ch ) - > device , ( ch ) - > name , fmt , # # a )
2014-06-25 18:39:56 +04:00
2010-08-11 05:01:22 +04:00
# define DPRINTK(fmt, arg...) \
do { \
if ( debug ) \
2014-06-25 18:39:56 +04:00
ch_printk ( KERN_DEBUG , ch , fmt , # # arg ) ; \
2010-08-11 05:01:22 +04:00
} while ( 0 )
# define VPRINTK(level, fmt, arg...) \
do { \
if ( verbose ) \
2014-06-25 18:39:56 +04:00
ch_printk ( level , ch , fmt , # # arg ) ; \
2010-08-11 05:01:22 +04:00
} while ( 0 )
2005-05-10 12:59:13 +04:00
/* ------------------------------------------------------------------- */
# define MAX_RETRIES 1
2005-05-12 12:25:26 +04:00
static struct class * ch_sysfs_class ;
2005-05-10 12:59:13 +04:00
typedef struct {
2017-08-15 17:28:49 +03:00
struct kref ref ;
2005-05-10 12:59:13 +04:00
struct list_head list ;
int minor ;
char name [ 8 ] ;
struct scsi_device * device ;
struct scsi_device * * dt ; /* ptrs to data transfer elements */
u_int firsts [ CH_TYPES ] ;
u_int counts [ CH_TYPES ] ;
u_int unit_attention ;
u_int voltags ;
2006-01-11 15:16:10 +03:00
struct mutex lock ;
2005-05-10 12:59:13 +04:00
} scsi_changer ;
2008-01-24 11:24:50 +03:00
static DEFINE_IDR ( ch_index_idr ) ;
static DEFINE_SPINLOCK ( ch_index_lock ) ;
2005-05-10 12:59:13 +04:00
2005-11-28 18:22:25 +03:00
static const struct {
2005-05-10 12:59:13 +04:00
unsigned char sense ;
unsigned char asc ;
unsigned char ascq ;
int errno ;
2008-04-01 09:05:30 +04:00
} ch_err [ ] = {
2005-05-10 12:59:13 +04:00
/* Just filled in what looks right. Hav'nt checked any standard paper for
these errno assignments , so they may be wrong . . . */
{
. sense = ILLEGAL_REQUEST ,
. asc = 0x21 ,
. ascq = 0x01 ,
. errno = EBADSLT , /* Invalid element address */
} , {
. sense = ILLEGAL_REQUEST ,
. asc = 0x28 ,
. ascq = 0x01 ,
. errno = EBADE , /* Import or export element accessed */
} , {
. sense = ILLEGAL_REQUEST ,
. asc = 0x3B ,
. ascq = 0x0D ,
. errno = EXFULL , /* Medium destination element full */
} , {
. sense = ILLEGAL_REQUEST ,
. asc = 0x3B ,
. ascq = 0x0E ,
. errno = EBADE , /* Medium source element empty */
} , {
. sense = ILLEGAL_REQUEST ,
. asc = 0x20 ,
. ascq = 0x00 ,
. errno = EBADRQC , /* Invalid command operation code */
} , {
/* end of list */
}
} ;
/* ------------------------------------------------------------------- */
2005-06-13 07:37:10 +04:00
static int ch_find_errno ( struct scsi_sense_hdr * sshdr )
2005-05-10 12:59:13 +04:00
{
int i , errno = 0 ;
/* Check to see if additional sense information is available */
2005-06-13 07:37:10 +04:00
if ( scsi_sense_valid ( sshdr ) & &
sshdr - > asc ! = 0 ) {
2008-04-01 09:05:30 +04:00
for ( i = 0 ; ch_err [ i ] . errno ! = 0 ; i + + ) {
if ( ch_err [ i ] . sense = = sshdr - > sense_key & &
ch_err [ i ] . asc = = sshdr - > asc & &
ch_err [ i ] . ascq = = sshdr - > ascq ) {
errno = - ch_err [ i ] . errno ;
2005-05-10 12:59:13 +04:00
break ;
}
}
}
if ( errno = = 0 )
errno = - EIO ;
return errno ;
}
static int
2014-10-24 16:26:57 +04:00
ch_do_scsi ( scsi_changer * ch , unsigned char * cmd , int cmd_len ,
2005-05-10 12:59:13 +04:00
void * buffer , unsigned buflength ,
enum dma_data_direction direction )
{
2005-06-13 07:37:10 +04:00
int errno , retries = 0 , timeout , result ;
struct scsi_sense_hdr sshdr ;
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
timeout = ( cmd [ 0 ] = = INITIALIZE_ELEMENT_STATUS )
? timeout_init : timeout_move ;
retry :
errno = 0 ;
2014-06-25 18:39:56 +04:00
result = scsi_execute_req ( ch - > device , cmd , direction , buffer ,
2005-06-13 07:37:10 +04:00
buflength , & sshdr , timeout * HZ ,
2008-12-04 08:24:39 +03:00
MAX_RETRIES , NULL ) ;
2005-05-10 12:59:13 +04:00
2018-06-25 14:20:58 +03:00
if ( driver_byte ( result ) = = DRIVER_SENSE ) {
2005-05-10 12:59:13 +04:00
if ( debug )
2014-10-24 16:26:45 +04:00
scsi_print_sense_hdr ( ch - > device , ch - > name , & sshdr ) ;
2005-06-13 07:37:10 +04:00
errno = ch_find_errno ( & sshdr ) ;
2005-05-10 12:59:13 +04:00
2005-06-13 07:37:10 +04:00
switch ( sshdr . sense_key ) {
2005-05-10 12:59:13 +04:00
case UNIT_ATTENTION :
ch - > unit_attention = 1 ;
if ( retries + + < 3 )
goto retry ;
break ;
}
}
return errno ;
}
/* ------------------------------------------------------------------------ */
static int
ch_elem_to_typecode ( scsi_changer * ch , u_int elem )
{
int i ;
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
for ( i = 0 ; i < CH_TYPES ; i + + ) {
if ( elem > = ch - > firsts [ i ] & &
elem < ch - > firsts [ i ] +
ch - > counts [ i ] )
return i + 1 ;
}
return 0 ;
}
static int
ch_read_element_status ( scsi_changer * ch , u_int elem , char * data )
{
u_char cmd [ 12 ] ;
u_char * buffer ;
int result ;
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
buffer = kmalloc ( 512 , GFP_KERNEL | GFP_DMA ) ;
if ( ! buffer )
return - ENOMEM ;
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
retry :
memset ( cmd , 0 , sizeof ( cmd ) ) ;
cmd [ 0 ] = READ_ELEMENT_STATUS ;
2014-06-25 17:27:36 +04:00
cmd [ 1 ] = ( ( ch - > device - > lun & 0x7 ) < < 5 ) |
2005-05-10 12:59:13 +04:00
( ch - > voltags ? 0x10 : 0 ) |
ch_elem_to_typecode ( ch , elem ) ;
cmd [ 2 ] = ( elem > > 8 ) & 0xff ;
cmd [ 3 ] = elem & 0xff ;
cmd [ 5 ] = 1 ;
cmd [ 9 ] = 255 ;
2014-10-24 16:26:57 +04:00
if ( 0 = = ( result = ch_do_scsi ( ch , cmd , 12 ,
buffer , 256 , DMA_FROM_DEVICE ) ) ) {
2005-05-10 12:59:13 +04:00
if ( ( ( buffer [ 16 ] < < 8 ) | buffer [ 17 ] ) ! = elem ) {
2010-08-11 05:01:22 +04:00
DPRINTK ( " asked for element 0x%02x, got 0x%02x \n " ,
2005-05-10 12:59:13 +04:00
elem , ( buffer [ 16 ] < < 8 ) | buffer [ 17 ] ) ;
kfree ( buffer ) ;
return - EIO ;
}
memcpy ( data , buffer + 16 , 16 ) ;
} else {
if ( ch - > voltags ) {
ch - > voltags = 0 ;
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " device has no volume tag support \n " ) ;
2005-05-10 12:59:13 +04:00
goto retry ;
}
2010-08-11 05:01:22 +04:00
DPRINTK ( " READ ELEMENT STATUS for element 0x%x failed \n " , elem ) ;
2005-05-10 12:59:13 +04:00
}
kfree ( buffer ) ;
return result ;
}
2008-01-24 11:24:52 +03:00
static int
2005-05-10 12:59:13 +04:00
ch_init_elem ( scsi_changer * ch )
{
int err ;
u_char cmd [ 6 ] ;
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " INITIALIZE ELEMENT STATUS, may take some time ... \n " ) ;
2005-05-10 12:59:13 +04:00
memset ( cmd , 0 , sizeof ( cmd ) ) ;
cmd [ 0 ] = INITIALIZE_ELEMENT_STATUS ;
2014-06-25 17:27:36 +04:00
cmd [ 1 ] = ( ch - > device - > lun & 0x7 ) < < 5 ;
2014-10-24 16:26:57 +04:00
err = ch_do_scsi ( ch , cmd , 6 , NULL , 0 , DMA_NONE ) ;
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " ... finished \n " ) ;
2005-05-10 12:59:13 +04:00
return err ;
}
static int
ch_readconfig ( scsi_changer * ch )
{
u_char cmd [ 10 ] , data [ 16 ] ;
u_char * buffer ;
int result , id , lun , i ;
u_int elem ;
2007-04-16 10:05:33 +04:00
buffer = kzalloc ( 512 , GFP_KERNEL | GFP_DMA ) ;
2005-05-10 12:59:13 +04:00
if ( ! buffer )
return - ENOMEM ;
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
memset ( cmd , 0 , sizeof ( cmd ) ) ;
cmd [ 0 ] = MODE_SENSE ;
2014-06-25 17:27:36 +04:00
cmd [ 1 ] = ( ch - > device - > lun & 0x7 ) < < 5 ;
2005-05-10 12:59:13 +04:00
cmd [ 2 ] = 0x1d ;
cmd [ 4 ] = 255 ;
2014-10-24 16:26:57 +04:00
result = ch_do_scsi ( ch , cmd , 10 , buffer , 255 , DMA_FROM_DEVICE ) ;
2005-05-10 12:59:13 +04:00
if ( 0 ! = result ) {
cmd [ 1 ] | = ( 1 < < 3 ) ;
2014-10-24 16:26:57 +04:00
result = ch_do_scsi ( ch , cmd , 10 , buffer , 255 , DMA_FROM_DEVICE ) ;
2005-05-10 12:59:13 +04:00
}
if ( 0 = = result ) {
ch - > firsts [ CHET_MT ] =
( buffer [ buffer [ 3 ] + 6 ] < < 8 ) | buffer [ buffer [ 3 ] + 7 ] ;
ch - > counts [ CHET_MT ] =
( buffer [ buffer [ 3 ] + 8 ] < < 8 ) | buffer [ buffer [ 3 ] + 9 ] ;
ch - > firsts [ CHET_ST ] =
( buffer [ buffer [ 3 ] + 10 ] < < 8 ) | buffer [ buffer [ 3 ] + 11 ] ;
ch - > counts [ CHET_ST ] =
( buffer [ buffer [ 3 ] + 12 ] < < 8 ) | buffer [ buffer [ 3 ] + 13 ] ;
ch - > firsts [ CHET_IE ] =
( buffer [ buffer [ 3 ] + 14 ] < < 8 ) | buffer [ buffer [ 3 ] + 15 ] ;
ch - > counts [ CHET_IE ] =
( buffer [ buffer [ 3 ] + 16 ] < < 8 ) | buffer [ buffer [ 3 ] + 17 ] ;
ch - > firsts [ CHET_DT ] =
( buffer [ buffer [ 3 ] + 18 ] < < 8 ) | buffer [ buffer [ 3 ] + 19 ] ;
ch - > counts [ CHET_DT ] =
( buffer [ buffer [ 3 ] + 20 ] < < 8 ) | buffer [ buffer [ 3 ] + 21 ] ;
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " type #1 (mt): 0x%x+%d [medium transport] \n " ,
2005-05-10 12:59:13 +04:00
ch - > firsts [ CHET_MT ] ,
ch - > counts [ CHET_MT ] ) ;
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " type #2 (st): 0x%x+%d [storage] \n " ,
2005-05-10 12:59:13 +04:00
ch - > firsts [ CHET_ST ] ,
ch - > counts [ CHET_ST ] ) ;
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " type #3 (ie): 0x%x+%d [import/export] \n " ,
2005-05-10 12:59:13 +04:00
ch - > firsts [ CHET_IE ] ,
ch - > counts [ CHET_IE ] ) ;
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " type #4 (dt): 0x%x+%d [data transfer] \n " ,
2005-05-10 12:59:13 +04:00
ch - > firsts [ CHET_DT ] ,
ch - > counts [ CHET_DT ] ) ;
} else {
2015-02-24 17:11:26 +03:00
VPRINTK ( KERN_INFO , " reading element address assignment page failed! \n " ) ;
2005-05-10 12:59:13 +04:00
}
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
/* vendor specific element types */
for ( i = 0 ; i < 4 ; i + + ) {
if ( 0 = = vendor_counts [ i ] )
continue ;
if ( NULL = = vendor_labels [ i ] )
continue ;
ch - > firsts [ CHET_V1 + i ] = vendor_firsts [ i ] ;
ch - > counts [ CHET_V1 + i ] = vendor_counts [ i ] ;
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " type #%d (v%d): 0x%x+%d [%s, vendor specific] \n " ,
2005-05-10 12:59:13 +04:00
i + 5 , i + 1 , vendor_firsts [ i ] , vendor_counts [ i ] ,
vendor_labels [ i ] ) ;
}
/* look up the devices of the data transfer elements */
2010-08-11 05:01:18 +04:00
ch - > dt = kcalloc ( ch - > counts [ CHET_DT ] , sizeof ( * ch - > dt ) ,
2005-05-10 12:59:13 +04:00
GFP_KERNEL ) ;
2009-08-08 00:42:21 +04:00
if ( ! ch - > dt ) {
kfree ( buffer ) ;
return - ENOMEM ;
}
2005-05-10 12:59:13 +04:00
for ( elem = 0 ; elem < ch - > counts [ CHET_DT ] ; elem + + ) {
id = - 1 ;
lun = 0 ;
if ( elem < CH_DT_MAX & & - 1 ! = dt_id [ elem ] ) {
id = dt_id [ elem ] ;
lun = dt_lun [ elem ] ;
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " dt 0x%x: [insmod option] " ,
2005-05-10 12:59:13 +04:00
elem + ch - > firsts [ CHET_DT ] ) ;
} else if ( 0 ! = ch_read_element_status
( ch , elem + ch - > firsts [ CHET_DT ] , data ) ) {
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " dt 0x%x: READ ELEMENT STATUS failed \n " ,
2005-05-10 12:59:13 +04:00
elem + ch - > firsts [ CHET_DT ] ) ;
} else {
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " dt 0x%x: " , elem + ch - > firsts [ CHET_DT ] ) ;
2005-05-10 12:59:13 +04:00
if ( data [ 6 ] & 0x80 ) {
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_CONT , " not this SCSI bus \n " ) ;
2005-05-10 12:59:13 +04:00
ch - > dt [ elem ] = NULL ;
} else if ( 0 = = ( data [ 6 ] & 0x30 ) ) {
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_CONT , " ID/LUN unknown \n " ) ;
2005-05-10 12:59:13 +04:00
ch - > dt [ elem ] = NULL ;
} else {
id = ch - > device - > id ;
lun = 0 ;
if ( data [ 6 ] & 0x20 ) id = data [ 7 ] ;
if ( data [ 6 ] & 0x10 ) lun = data [ 6 ] & 7 ;
}
}
if ( - 1 ! = id ) {
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_CONT , " ID %i, LUN %i, " , id , lun ) ;
2005-05-10 12:59:13 +04:00
ch - > dt [ elem ] =
scsi_device_lookup ( ch - > device - > host ,
ch - > device - > channel ,
id , lun ) ;
if ( ! ch - > dt [ elem ] ) {
/* should not happen */
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_CONT , " Huh? device not found! \n " ) ;
2005-05-10 12:59:13 +04:00
} else {
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_CONT , " name: %8.8s %16.16s %4.4s \n " ,
ch - > dt [ elem ] - > vendor ,
ch - > dt [ elem ] - > model ,
ch - > dt [ elem ] - > rev ) ;
2005-05-10 12:59:13 +04:00
}
}
}
ch - > voltags = 1 ;
kfree ( buffer ) ;
return 0 ;
}
/* ------------------------------------------------------------------------ */
static int
ch_position ( scsi_changer * ch , u_int trans , u_int elem , int rotate )
{
u_char cmd [ 10 ] ;
2008-01-24 11:24:52 +03:00
2010-08-11 05:01:22 +04:00
DPRINTK ( " position: 0x%x \n " , elem ) ;
2005-05-10 12:59:13 +04:00
if ( 0 = = trans )
trans = ch - > firsts [ CHET_MT ] ;
memset ( cmd , 0 , sizeof ( cmd ) ) ;
cmd [ 0 ] = POSITION_TO_ELEMENT ;
2014-06-25 17:27:36 +04:00
cmd [ 1 ] = ( ch - > device - > lun & 0x7 ) < < 5 ;
2005-05-10 12:59:13 +04:00
cmd [ 2 ] = ( trans > > 8 ) & 0xff ;
cmd [ 3 ] = trans & 0xff ;
cmd [ 4 ] = ( elem > > 8 ) & 0xff ;
cmd [ 5 ] = elem & 0xff ;
cmd [ 8 ] = rotate ? 1 : 0 ;
2014-10-24 16:26:57 +04:00
return ch_do_scsi ( ch , cmd , 10 , NULL , 0 , DMA_NONE ) ;
2005-05-10 12:59:13 +04:00
}
static int
ch_move ( scsi_changer * ch , u_int trans , u_int src , u_int dest , int rotate )
{
u_char cmd [ 12 ] ;
2008-01-24 11:24:52 +03:00
2010-08-11 05:01:22 +04:00
DPRINTK ( " move: 0x%x => 0x%x \n " , src , dest ) ;
2005-05-10 12:59:13 +04:00
if ( 0 = = trans )
trans = ch - > firsts [ CHET_MT ] ;
memset ( cmd , 0 , sizeof ( cmd ) ) ;
cmd [ 0 ] = MOVE_MEDIUM ;
2014-06-25 17:27:36 +04:00
cmd [ 1 ] = ( ch - > device - > lun & 0x7 ) < < 5 ;
2005-05-10 12:59:13 +04:00
cmd [ 2 ] = ( trans > > 8 ) & 0xff ;
cmd [ 3 ] = trans & 0xff ;
cmd [ 4 ] = ( src > > 8 ) & 0xff ;
cmd [ 5 ] = src & 0xff ;
cmd [ 6 ] = ( dest > > 8 ) & 0xff ;
cmd [ 7 ] = dest & 0xff ;
cmd [ 10 ] = rotate ? 1 : 0 ;
2014-10-24 16:26:57 +04:00
return ch_do_scsi ( ch , cmd , 12 , NULL , 0 , DMA_NONE ) ;
2005-05-10 12:59:13 +04:00
}
static int
ch_exchange ( scsi_changer * ch , u_int trans , u_int src ,
u_int dest1 , u_int dest2 , int rotate1 , int rotate2 )
{
u_char cmd [ 12 ] ;
2008-01-24 11:24:52 +03:00
2010-08-11 05:01:22 +04:00
DPRINTK ( " exchange: 0x%x => 0x%x => 0x%x \n " ,
2005-05-10 12:59:13 +04:00
src , dest1 , dest2 ) ;
if ( 0 = = trans )
trans = ch - > firsts [ CHET_MT ] ;
memset ( cmd , 0 , sizeof ( cmd ) ) ;
cmd [ 0 ] = EXCHANGE_MEDIUM ;
2014-06-25 17:27:36 +04:00
cmd [ 1 ] = ( ch - > device - > lun & 0x7 ) < < 5 ;
2005-05-10 12:59:13 +04:00
cmd [ 2 ] = ( trans > > 8 ) & 0xff ;
cmd [ 3 ] = trans & 0xff ;
cmd [ 4 ] = ( src > > 8 ) & 0xff ;
cmd [ 5 ] = src & 0xff ;
cmd [ 6 ] = ( dest1 > > 8 ) & 0xff ;
cmd [ 7 ] = dest1 & 0xff ;
cmd [ 8 ] = ( dest2 > > 8 ) & 0xff ;
cmd [ 9 ] = dest2 & 0xff ;
cmd [ 10 ] = ( rotate1 ? 1 : 0 ) | ( rotate2 ? 2 : 0 ) ;
2008-01-24 11:24:52 +03:00
2014-10-24 16:26:57 +04:00
return ch_do_scsi ( ch , cmd , 12 , NULL , 0 , DMA_NONE ) ;
2005-05-10 12:59:13 +04:00
}
static void
ch_check_voltag ( char * tag )
{
int i ;
for ( i = 0 ; i < 32 ; i + + ) {
/* restrict to ascii */
if ( tag [ i ] > = 0x7f | | tag [ i ] < 0x20 )
tag [ i ] = ' ' ;
/* don't allow search wildcards */
if ( tag [ i ] = = ' ? ' | |
tag [ i ] = = ' * ' )
tag [ i ] = ' ' ;
}
}
static int
ch_set_voltag ( scsi_changer * ch , u_int elem ,
int alternate , int clear , u_char * tag )
{
u_char cmd [ 12 ] ;
u_char * buffer ;
int result ;
2007-04-16 10:05:33 +04:00
buffer = kzalloc ( 512 , GFP_KERNEL ) ;
2005-05-10 12:59:13 +04:00
if ( ! buffer )
return - ENOMEM ;
2010-08-11 05:01:22 +04:00
DPRINTK ( " %s %s voltag: 0x%x => \" %s \" \n " ,
2005-05-10 12:59:13 +04:00
clear ? " clear " : " set " ,
alternate ? " alternate " : " primary " ,
elem , tag ) ;
memset ( cmd , 0 , sizeof ( cmd ) ) ;
cmd [ 0 ] = SEND_VOLUME_TAG ;
2014-06-25 17:27:36 +04:00
cmd [ 1 ] = ( ( ch - > device - > lun & 0x7 ) < < 5 ) |
2005-05-10 12:59:13 +04:00
ch_elem_to_typecode ( ch , elem ) ;
cmd [ 2 ] = ( elem > > 8 ) & 0xff ;
cmd [ 3 ] = elem & 0xff ;
cmd [ 5 ] = clear
? ( alternate ? 0x0d : 0x0c )
: ( alternate ? 0x0b : 0x0a ) ;
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
cmd [ 9 ] = 255 ;
memcpy ( buffer , tag , 32 ) ;
ch_check_voltag ( buffer ) ;
2014-10-24 16:26:57 +04:00
result = ch_do_scsi ( ch , cmd , 12 , buffer , 256 , DMA_TO_DEVICE ) ;
2005-05-10 12:59:13 +04:00
kfree ( buffer ) ;
return result ;
}
2005-09-10 01:03:44 +04:00
static int ch_gstatus ( scsi_changer * ch , int type , unsigned char __user * dest )
2005-05-10 12:59:13 +04:00
{
int retval = 0 ;
u_char data [ 16 ] ;
unsigned int i ;
2008-01-24 11:24:52 +03:00
2006-01-11 15:16:10 +03:00
mutex_lock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
for ( i = 0 ; i < ch - > counts [ type ] ; i + + ) {
if ( 0 ! = ch_read_element_status
( ch , ch - > firsts [ type ] + i , data ) ) {
retval = - EIO ;
break ;
}
put_user ( data [ 2 ] , dest + i ) ;
if ( data [ 2 ] & CESTATUS_EXCEPT )
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " element 0x%x: asc=0x%x, ascq=0x%x \n " ,
2005-05-10 12:59:13 +04:00
ch - > firsts [ type ] + i ,
( int ) data [ 4 ] , ( int ) data [ 5 ] ) ;
retval = ch_read_element_status
( ch , ch - > firsts [ type ] + i , data ) ;
if ( 0 ! = retval )
break ;
}
2006-01-11 15:16:10 +03:00
mutex_unlock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
return retval ;
}
/* ------------------------------------------------------------------------ */
2017-08-15 17:28:49 +03:00
static void ch_destroy ( struct kref * ref )
{
scsi_changer * ch = container_of ( ref , scsi_changer , ref ) ;
2020-02-13 18:32:05 +03:00
ch - > device = NULL ;
2017-08-15 17:28:49 +03:00
kfree ( ch - > dt ) ;
kfree ( ch ) ;
}
2005-05-10 12:59:13 +04:00
static int
ch_release ( struct inode * inode , struct file * file )
{
scsi_changer * ch = file - > private_data ;
scsi_device_put ( ch - > device ) ;
file - > private_data = NULL ;
2017-08-15 17:28:49 +03:00
kref_put ( & ch - > ref , ch_destroy ) ;
2005-05-10 12:59:13 +04:00
return 0 ;
}
static int
ch_open ( struct inode * inode , struct file * file )
{
2008-01-24 11:24:50 +03:00
scsi_changer * ch ;
2005-05-10 12:59:13 +04:00
int minor = iminor ( inode ) ;
2008-01-24 11:24:50 +03:00
spin_lock ( & ch_index_lock ) ;
ch = idr_find ( & ch_index_idr , minor ) ;
2020-02-13 18:32:05 +03:00
if ( ch = = NULL | | ! kref_get_unless_zero ( & ch - > ref ) ) {
2008-01-24 11:24:50 +03:00
spin_unlock ( & ch_index_lock ) ;
2005-05-10 12:59:13 +04:00
return - ENXIO ;
}
2008-01-24 11:24:50 +03:00
spin_unlock ( & ch_index_lock ) ;
2020-02-13 18:32:05 +03:00
if ( scsi_device_get ( ch - > device ) ) {
kref_put ( & ch - > ref , ch_destroy ) ;
return - ENXIO ;
}
2020-02-13 18:32:06 +03:00
/* Synchronize with ch_probe() */
mutex_lock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
file - > private_data = ch ;
2020-02-13 18:32:06 +03:00
mutex_unlock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
return 0 ;
}
static int
ch_checkrange ( scsi_changer * ch , unsigned int type , unsigned int unit )
{
if ( type > = CH_TYPES | | unit > = ch - > counts [ type ] )
return - 1 ;
return 0 ;
}
2008-01-14 17:43:18 +03:00
static long ch_ioctl ( struct file * file ,
2005-05-10 12:59:13 +04:00
unsigned int cmd , unsigned long arg )
{
scsi_changer * ch = file - > private_data ;
int retval ;
2005-09-10 01:03:44 +04:00
void __user * argp = ( void __user * ) arg ;
2008-01-24 11:24:52 +03:00
2014-10-11 18:25:31 +04:00
retval = scsi_ioctl_block_when_processing_errors ( ch - > device , cmd ,
file - > f_flags & O_NDELAY ) ;
if ( retval )
return retval ;
2005-05-10 12:59:13 +04:00
switch ( cmd ) {
case CHIOGPARAMS :
{
struct changer_params params ;
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
params . cp_curpicker = 0 ;
params . cp_npickers = ch - > counts [ CHET_MT ] ;
params . cp_nslots = ch - > counts [ CHET_ST ] ;
params . cp_nportals = ch - > counts [ CHET_IE ] ;
params . cp_ndrives = ch - > counts [ CHET_DT ] ;
2008-01-24 11:24:52 +03:00
2005-09-10 01:03:44 +04:00
if ( copy_to_user ( argp , & params , sizeof ( params ) ) )
2005-05-10 12:59:13 +04:00
return - EFAULT ;
return 0 ;
}
case CHIOGVPARAMS :
{
struct changer_vendor_params vparams ;
memset ( & vparams , 0 , sizeof ( vparams ) ) ;
if ( ch - > counts [ CHET_V1 ] ) {
vparams . cvp_n1 = ch - > counts [ CHET_V1 ] ;
strncpy ( vparams . cvp_label1 , vendor_labels [ 0 ] , 16 ) ;
}
if ( ch - > counts [ CHET_V2 ] ) {
vparams . cvp_n2 = ch - > counts [ CHET_V2 ] ;
strncpy ( vparams . cvp_label2 , vendor_labels [ 1 ] , 16 ) ;
}
if ( ch - > counts [ CHET_V3 ] ) {
vparams . cvp_n3 = ch - > counts [ CHET_V3 ] ;
strncpy ( vparams . cvp_label3 , vendor_labels [ 2 ] , 16 ) ;
}
if ( ch - > counts [ CHET_V4 ] ) {
vparams . cvp_n4 = ch - > counts [ CHET_V4 ] ;
strncpy ( vparams . cvp_label4 , vendor_labels [ 3 ] , 16 ) ;
}
2005-09-10 01:03:44 +04:00
if ( copy_to_user ( argp , & vparams , sizeof ( vparams ) ) )
2005-05-10 12:59:13 +04:00
return - EFAULT ;
return 0 ;
}
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
case CHIOPOSITION :
{
struct changer_position pos ;
2008-01-24 11:24:52 +03:00
2005-09-10 01:03:44 +04:00
if ( copy_from_user ( & pos , argp , sizeof ( pos ) ) )
2005-05-10 12:59:13 +04:00
return - EFAULT ;
if ( 0 ! = ch_checkrange ( ch , pos . cp_type , pos . cp_unit ) ) {
2010-08-11 05:01:22 +04:00
DPRINTK ( " CHIOPOSITION: invalid parameter \n " ) ;
2005-05-10 12:59:13 +04:00
return - EBADSLT ;
}
2006-01-11 15:16:10 +03:00
mutex_lock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
retval = ch_position ( ch , 0 ,
ch - > firsts [ pos . cp_type ] + pos . cp_unit ,
pos . cp_flags & CP_INVERT ) ;
2006-01-11 15:16:10 +03:00
mutex_unlock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
return retval ;
}
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
case CHIOMOVE :
{
struct changer_move mv ;
2005-09-10 01:03:44 +04:00
if ( copy_from_user ( & mv , argp , sizeof ( mv ) ) )
2005-05-10 12:59:13 +04:00
return - EFAULT ;
if ( 0 ! = ch_checkrange ( ch , mv . cm_fromtype , mv . cm_fromunit ) | |
0 ! = ch_checkrange ( ch , mv . cm_totype , mv . cm_tounit ) ) {
2010-08-11 05:01:22 +04:00
DPRINTK ( " CHIOMOVE: invalid parameter \n " ) ;
2005-05-10 12:59:13 +04:00
return - EBADSLT ;
}
2008-01-24 11:24:52 +03:00
2006-01-11 15:16:10 +03:00
mutex_lock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
retval = ch_move ( ch , 0 ,
ch - > firsts [ mv . cm_fromtype ] + mv . cm_fromunit ,
ch - > firsts [ mv . cm_totype ] + mv . cm_tounit ,
mv . cm_flags & CM_INVERT ) ;
2006-01-11 15:16:10 +03:00
mutex_unlock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
return retval ;
}
case CHIOEXCHANGE :
{
struct changer_exchange mv ;
2008-01-24 11:24:52 +03:00
2005-09-10 01:03:44 +04:00
if ( copy_from_user ( & mv , argp , sizeof ( mv ) ) )
2005-05-10 12:59:13 +04:00
return - EFAULT ;
if ( 0 ! = ch_checkrange ( ch , mv . ce_srctype , mv . ce_srcunit ) | |
0 ! = ch_checkrange ( ch , mv . ce_fdsttype , mv . ce_fdstunit ) | |
0 ! = ch_checkrange ( ch , mv . ce_sdsttype , mv . ce_sdstunit ) ) {
2010-08-11 05:01:22 +04:00
DPRINTK ( " CHIOEXCHANGE: invalid parameter \n " ) ;
2005-05-10 12:59:13 +04:00
return - EBADSLT ;
}
2008-01-24 11:24:52 +03:00
2006-01-11 15:16:10 +03:00
mutex_lock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
retval = ch_exchange
( ch , 0 ,
ch - > firsts [ mv . ce_srctype ] + mv . ce_srcunit ,
ch - > firsts [ mv . ce_fdsttype ] + mv . ce_fdstunit ,
ch - > firsts [ mv . ce_sdsttype ] + mv . ce_sdstunit ,
mv . ce_flags & CE_INVERT1 , mv . ce_flags & CE_INVERT2 ) ;
2006-01-11 15:16:10 +03:00
mutex_unlock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
return retval ;
}
case CHIOGSTATUS :
{
struct changer_element_status ces ;
2008-01-24 11:24:52 +03:00
2005-09-10 01:03:44 +04:00
if ( copy_from_user ( & ces , argp , sizeof ( ces ) ) )
2005-05-10 12:59:13 +04:00
return - EFAULT ;
if ( ces . ces_type < 0 | | ces . ces_type > = CH_TYPES )
return - EINVAL ;
return ch_gstatus ( ch , ces . ces_type , ces . ces_data ) ;
}
case CHIOGELEM :
{
struct changer_get_element cge ;
2008-04-01 09:05:30 +04:00
u_char ch_cmd [ 12 ] ;
u_char * buffer ;
2005-05-10 12:59:13 +04:00
unsigned int elem ;
int result , i ;
2008-01-24 11:24:52 +03:00
2005-09-10 01:03:44 +04:00
if ( copy_from_user ( & cge , argp , sizeof ( cge ) ) )
2005-05-10 12:59:13 +04:00
return - EFAULT ;
if ( 0 ! = ch_checkrange ( ch , cge . cge_type , cge . cge_unit ) )
return - EINVAL ;
elem = ch - > firsts [ cge . cge_type ] + cge . cge_unit ;
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
buffer = kmalloc ( 512 , GFP_KERNEL | GFP_DMA ) ;
if ( ! buffer )
return - ENOMEM ;
2006-01-11 15:16:10 +03:00
mutex_lock ( & ch - > lock ) ;
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
voltag_retry :
2008-04-01 09:05:30 +04:00
memset ( ch_cmd , 0 , sizeof ( ch_cmd ) ) ;
ch_cmd [ 0 ] = READ_ELEMENT_STATUS ;
2014-06-25 17:27:36 +04:00
ch_cmd [ 1 ] = ( ( ch - > device - > lun & 0x7 ) < < 5 ) |
2005-05-10 12:59:13 +04:00
( ch - > voltags ? 0x10 : 0 ) |
ch_elem_to_typecode ( ch , elem ) ;
2008-04-01 09:05:30 +04:00
ch_cmd [ 2 ] = ( elem > > 8 ) & 0xff ;
ch_cmd [ 3 ] = elem & 0xff ;
ch_cmd [ 5 ] = 1 ;
ch_cmd [ 9 ] = 255 ;
2008-01-24 11:24:52 +03:00
2014-10-24 16:26:57 +04:00
result = ch_do_scsi ( ch , ch_cmd , 12 ,
buffer , 256 , DMA_FROM_DEVICE ) ;
2008-04-01 09:05:30 +04:00
if ( ! result ) {
2005-05-10 12:59:13 +04:00
cge . cge_status = buffer [ 18 ] ;
cge . cge_flags = 0 ;
if ( buffer [ 18 ] & CESTATUS_EXCEPT ) {
cge . cge_errno = EIO ;
}
if ( buffer [ 25 ] & 0x80 ) {
cge . cge_flags | = CGE_SRC ;
if ( buffer [ 25 ] & 0x40 )
cge . cge_flags | = CGE_INVERT ;
elem = ( buffer [ 26 ] < < 8 ) | buffer [ 27 ] ;
for ( i = 0 ; i < 4 ; i + + ) {
if ( elem > = ch - > firsts [ i ] & &
elem < ch - > firsts [ i ] + ch - > counts [ i ] ) {
cge . cge_srctype = i ;
cge . cge_srcunit = elem - ch - > firsts [ i ] ;
}
}
}
if ( ( buffer [ 22 ] & 0x30 ) = = 0x30 ) {
cge . cge_flags | = CGE_IDLUN ;
cge . cge_id = buffer [ 23 ] ;
cge . cge_lun = buffer [ 22 ] & 7 ;
}
if ( buffer [ 9 ] & 0x80 ) {
cge . cge_flags | = CGE_PVOLTAG ;
memcpy ( cge . cge_pvoltag , buffer + 28 , 36 ) ;
}
if ( buffer [ 9 ] & 0x40 ) {
cge . cge_flags | = CGE_AVOLTAG ;
memcpy ( cge . cge_avoltag , buffer + 64 , 36 ) ;
}
} else if ( ch - > voltags ) {
ch - > voltags = 0 ;
2010-08-11 05:01:22 +04:00
VPRINTK ( KERN_INFO , " device has no volume tag support \n " ) ;
2005-05-10 12:59:13 +04:00
goto voltag_retry ;
}
kfree ( buffer ) ;
2006-01-11 15:16:10 +03:00
mutex_unlock ( & ch - > lock ) ;
2008-01-24 11:24:52 +03:00
2005-09-10 01:03:44 +04:00
if ( copy_to_user ( argp , & cge , sizeof ( cge ) ) )
2005-05-10 12:59:13 +04:00
return - EFAULT ;
return result ;
}
case CHIOINITELEM :
{
2006-01-11 15:16:10 +03:00
mutex_lock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
retval = ch_init_elem ( ch ) ;
2006-01-11 15:16:10 +03:00
mutex_unlock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
return retval ;
}
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
case CHIOSVOLTAG :
{
struct changer_set_voltag csv ;
int elem ;
2005-09-10 01:03:44 +04:00
if ( copy_from_user ( & csv , argp , sizeof ( csv ) ) )
2005-05-10 12:59:13 +04:00
return - EFAULT ;
if ( 0 ! = ch_checkrange ( ch , csv . csv_type , csv . csv_unit ) ) {
2010-08-11 05:01:22 +04:00
DPRINTK ( " CHIOSVOLTAG: invalid parameter \n " ) ;
2005-05-10 12:59:13 +04:00
return - EBADSLT ;
}
elem = ch - > firsts [ csv . csv_type ] + csv . csv_unit ;
2006-01-11 15:16:10 +03:00
mutex_lock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
retval = ch_set_voltag ( ch , elem ,
csv . csv_flags & CSV_AVOLTAG ,
csv . csv_flags & CSV_CLEARTAG ,
csv . csv_voltag ) ;
2006-01-11 15:16:10 +03:00
mutex_unlock ( & ch - > lock ) ;
2005-05-10 12:59:13 +04:00
return retval ;
}
default :
2005-09-10 01:03:44 +04:00
return scsi_ioctl ( ch - > device , cmd , argp ) ;
2005-05-10 12:59:13 +04:00
}
}
# ifdef CONFIG_COMPAT
struct changer_element_status32 {
int ces_type ;
compat_uptr_t ces_data ;
} ;
# define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
static long ch_ioctl_compat ( struct file * file ,
unsigned int cmd , unsigned long arg )
{
scsi_changer * ch = file - > private_data ;
2019-03-15 19:39:44 +03:00
int retval = scsi_ioctl_block_when_processing_errors ( ch - > device , cmd ,
file - > f_flags & O_NDELAY ) ;
if ( retval )
return retval ;
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
switch ( cmd ) {
case CHIOGPARAMS :
case CHIOGVPARAMS :
case CHIOPOSITION :
case CHIOMOVE :
case CHIOEXCHANGE :
case CHIOGELEM :
case CHIOINITELEM :
case CHIOSVOLTAG :
/* compatible */
2019-03-15 19:39:44 +03:00
return ch_ioctl ( file , cmd , ( unsigned long ) compat_ptr ( arg ) ) ;
2005-05-10 12:59:13 +04:00
case CHIOGSTATUS32 :
{
struct changer_element_status32 ces32 ;
2005-09-10 01:03:44 +04:00
unsigned char __user * data ;
2008-01-24 11:24:52 +03:00
2005-09-10 01:03:44 +04:00
if ( copy_from_user ( & ces32 , ( void __user * ) arg , sizeof ( ces32 ) ) )
2005-05-10 12:59:13 +04:00
return - EFAULT ;
if ( ces32 . ces_type < 0 | | ces32 . ces_type > = CH_TYPES )
return - EINVAL ;
data = compat_ptr ( ces32 . ces_data ) ;
return ch_gstatus ( ch , ces32 . ces_type , data ) ;
}
default :
2019-03-15 19:39:44 +03:00
return scsi_compat_ioctl ( ch - > device , cmd , compat_ptr ( arg ) ) ;
2005-05-10 12:59:13 +04:00
}
}
# endif
/* ------------------------------------------------------------------------ */
static int ch_probe ( struct device * dev )
{
struct scsi_device * sd = to_scsi_device ( dev ) ;
2008-02-22 02:13:36 +03:00
struct device * class_dev ;
2013-02-28 05:04:42 +04:00
int ret ;
2005-05-10 12:59:13 +04:00
scsi_changer * ch ;
2008-01-23 17:34:35 +03:00
2005-05-10 12:59:13 +04:00
if ( sd - > type ! = TYPE_MEDIUM_CHANGER )
return - ENODEV ;
2008-01-23 17:34:35 +03:00
2007-04-16 10:05:33 +04:00
ch = kzalloc ( sizeof ( * ch ) , GFP_KERNEL ) ;
2005-05-10 12:59:13 +04:00
if ( NULL = = ch )
return - ENOMEM ;
2013-02-28 05:04:42 +04:00
idr_preload ( GFP_KERNEL ) ;
2008-01-24 11:24:50 +03:00
spin_lock ( & ch_index_lock ) ;
2013-02-28 05:04:42 +04:00
ret = idr_alloc ( & ch_index_idr , ch , 0 , CH_MAX_DEVS + 1 , GFP_NOWAIT ) ;
2008-01-24 11:24:50 +03:00
spin_unlock ( & ch_index_lock ) ;
2013-02-28 05:04:42 +04:00
idr_preload_end ( ) ;
2008-01-24 11:24:50 +03:00
2013-02-28 05:04:42 +04:00
if ( ret < 0 ) {
if ( ret = = - ENOSPC )
ret = - ENODEV ;
2008-01-24 11:24:50 +03:00
goto free_ch ;
}
2013-02-28 05:04:42 +04:00
ch - > minor = ret ;
2005-05-10 12:59:13 +04:00
sprintf ( ch - > name , " ch%d " , ch - > minor ) ;
2020-02-13 18:32:05 +03:00
ret = scsi_device_get ( sd ) ;
if ( ret ) {
sdev_printk ( KERN_WARNING , sd , " ch%d: failed to get device \n " ,
ch - > minor ) ;
goto remove_idr ;
}
2008-01-23 17:34:35 +03:00
2020-02-13 18:32:06 +03:00
mutex_init ( & ch - > lock ) ;
kref_init ( & ch - > ref ) ;
ch - > device = sd ;
2008-07-22 07:03:34 +04:00
class_dev = device_create ( ch_sysfs_class , dev ,
MKDEV ( SCSI_CHANGER_MAJOR , ch - > minor ) , ch ,
" s%s " , ch - > name ) ;
2008-01-23 17:34:35 +03:00
if ( IS_ERR ( class_dev ) ) {
2014-06-25 18:39:56 +04:00
sdev_printk ( KERN_WARNING , sd , " ch%d: device_create failed \n " ,
ch - > minor ) ;
2008-01-24 11:24:50 +03:00
ret = PTR_ERR ( class_dev ) ;
2020-02-13 18:32:05 +03:00
goto put_device ;
2008-01-23 17:34:35 +03:00
}
2020-02-13 18:32:06 +03:00
mutex_lock ( & ch - > lock ) ;
2017-08-15 17:28:49 +03:00
ret = ch_readconfig ( ch ) ;
2020-02-13 18:32:06 +03:00
if ( ret ) {
mutex_unlock ( & ch - > lock ) ;
2017-08-15 17:28:49 +03:00
goto destroy_dev ;
2020-02-13 18:32:06 +03:00
}
2005-05-10 12:59:13 +04:00
if ( init )
ch_init_elem ( ch ) ;
2020-02-13 18:32:06 +03:00
mutex_unlock ( & ch - > lock ) ;
2008-07-26 18:25:43 +04:00
dev_set_drvdata ( dev , ch ) ;
2005-10-25 02:04:36 +04:00
sdev_printk ( KERN_INFO , sd , " Attached scsi changer %s \n " , ch - > name ) ;
2008-01-23 17:34:35 +03:00
2005-05-10 12:59:13 +04:00
return 0 ;
2017-08-15 17:28:49 +03:00
destroy_dev :
device_destroy ( ch_sysfs_class , MKDEV ( SCSI_CHANGER_MAJOR , ch - > minor ) ) ;
2020-02-13 18:32:05 +03:00
put_device :
scsi_device_put ( sd ) ;
2008-01-24 11:24:50 +03:00
remove_idr :
2013-02-28 05:04:42 +04:00
idr_remove ( & ch_index_idr , ch - > minor ) ;
2008-01-24 11:24:50 +03:00
free_ch :
kfree ( ch ) ;
return ret ;
2005-05-10 12:59:13 +04:00
}
static int ch_remove ( struct device * dev )
{
2008-01-24 11:24:50 +03:00
scsi_changer * ch = dev_get_drvdata ( dev ) ;
2005-05-10 12:59:13 +04:00
2008-01-24 11:24:50 +03:00
spin_lock ( & ch_index_lock ) ;
idr_remove ( & ch_index_idr , ch - > minor ) ;
2020-02-13 18:32:05 +03:00
dev_set_drvdata ( dev , NULL ) ;
2008-01-24 11:24:50 +03:00
spin_unlock ( & ch_index_lock ) ;
2005-05-10 12:59:13 +04:00
2008-02-22 02:13:36 +03:00
device_destroy ( ch_sysfs_class , MKDEV ( SCSI_CHANGER_MAJOR , ch - > minor ) ) ;
2020-02-13 18:32:05 +03:00
scsi_device_put ( ch - > device ) ;
2017-08-15 17:28:49 +03:00
kref_put ( & ch - > ref , ch_destroy ) ;
2005-05-10 12:59:13 +04:00
return 0 ;
}
2008-01-24 11:24:52 +03:00
static struct scsi_driver ch_template = {
. gendrv = {
. name = " ch " ,
2014-11-12 20:34:51 +03:00
. owner = THIS_MODULE ,
2008-01-24 11:24:52 +03:00
. probe = ch_probe ,
. remove = ch_remove ,
} ,
} ;
static const struct file_operations changer_fops = {
. owner = THIS_MODULE ,
. open = ch_open ,
. release = ch_release ,
. unlocked_ioctl = ch_ioctl ,
# ifdef CONFIG_COMPAT
. compat_ioctl = ch_ioctl_compat ,
# endif
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 = noop_llseek ,
2008-01-24 11:24:52 +03:00
} ;
2005-05-10 12:59:13 +04:00
static int __init init_ch_module ( void )
{
int rc ;
2008-01-24 11:24:52 +03:00
2005-05-10 12:59:13 +04:00
printk ( KERN_INFO " SCSI Media Changer driver v " VERSION " \n " ) ;
2005-05-12 12:25:26 +04:00
ch_sysfs_class = class_create ( THIS_MODULE , " scsi_changer " ) ;
2005-05-10 12:59:13 +04:00
if ( IS_ERR ( ch_sysfs_class ) ) {
rc = PTR_ERR ( ch_sysfs_class ) ;
return rc ;
}
rc = register_chrdev ( SCSI_CHANGER_MAJOR , " ch " , & changer_fops ) ;
if ( rc < 0 ) {
printk ( " Unable to get major %d for SCSI-Changer \n " ,
SCSI_CHANGER_MAJOR ) ;
goto fail1 ;
}
rc = scsi_register_driver ( & ch_template . gendrv ) ;
if ( rc < 0 )
goto fail2 ;
return 0 ;
fail2 :
unregister_chrdev ( SCSI_CHANGER_MAJOR , " ch " ) ;
fail1 :
2005-05-12 12:25:26 +04:00
class_destroy ( ch_sysfs_class ) ;
2005-05-10 12:59:13 +04:00
return rc ;
}
2008-01-24 11:24:52 +03:00
static void __exit exit_ch_module ( void )
2005-05-10 12:59:13 +04:00
{
scsi_unregister_driver ( & ch_template . gendrv ) ;
unregister_chrdev ( SCSI_CHANGER_MAJOR , " ch " ) ;
2005-05-12 12:25:26 +04:00
class_destroy ( ch_sysfs_class ) ;
2008-01-24 11:24:50 +03:00
idr_destroy ( & ch_index_idr ) ;
2005-05-10 12:59:13 +04:00
}
module_init ( init_ch_module ) ;
module_exit ( exit_ch_module ) ;