2017-11-07 14:58:43 +01:00
// SPDX-License-Identifier: GPL-2.0+
2008-11-04 20:29:31 -08:00
/*
2015-01-28 18:41:49 +00:00
* comedi / range . c
* comedi routines for voltage ranges
*
* COMEDI - Linux Control and Measurement Device Interface
* Copyright ( C ) 1997 - 8 David A . Schleef < ds @ schleef . org >
*/
2008-11-04 20:29:31 -08:00
2010-04-30 15:35:37 -07:00
# include <linux/uaccess.h>
2021-11-17 12:05:59 +00:00
# include <linux/comedi/comedidev.h>
2012-06-19 10:17:44 +01:00
# include "comedi_internal.h"
2008-11-04 20:29:31 -08:00
2009-03-16 22:05:31 -04:00
const struct comedi_lrange range_bipolar10 = { 1 , { BIP_RANGE ( 10 ) } } ;
2013-04-12 10:11:54 -07:00
EXPORT_SYMBOL_GPL ( range_bipolar10 ) ;
2010-05-03 17:39:09 +08:00
const struct comedi_lrange range_bipolar5 = { 1 , { BIP_RANGE ( 5 ) } } ;
2013-04-12 10:11:54 -07:00
EXPORT_SYMBOL_GPL ( range_bipolar5 ) ;
2010-05-03 17:39:09 +08:00
const struct comedi_lrange range_bipolar2_5 = { 1 , { BIP_RANGE ( 2.5 ) } } ;
2013-04-12 10:11:54 -07:00
EXPORT_SYMBOL_GPL ( range_bipolar2_5 ) ;
2010-05-03 17:39:09 +08:00
const struct comedi_lrange range_unipolar10 = { 1 , { UNI_RANGE ( 10 ) } } ;
2013-04-12 10:11:54 -07:00
EXPORT_SYMBOL_GPL ( range_unipolar10 ) ;
2010-05-03 17:39:09 +08:00
const struct comedi_lrange range_unipolar5 = { 1 , { UNI_RANGE ( 5 ) } } ;
2013-04-12 10:11:54 -07:00
EXPORT_SYMBOL_GPL ( range_unipolar5 ) ;
2013-04-03 13:38:26 -07:00
const struct comedi_lrange range_unipolar2_5 = { 1 , { UNI_RANGE ( 2.5 ) } } ;
2013-04-12 10:11:54 -07:00
EXPORT_SYMBOL_GPL ( range_unipolar2_5 ) ;
2013-04-03 13:40:13 -07:00
const struct comedi_lrange range_0_20mA = { 1 , { RANGE_mA ( 0 , 20 ) } } ;
2013-04-12 10:11:54 -07:00
EXPORT_SYMBOL_GPL ( range_0_20mA ) ;
2013-04-03 13:40:13 -07:00
const struct comedi_lrange range_4_20mA = { 1 , { RANGE_mA ( 4 , 20 ) } } ;
2013-04-12 10:11:54 -07:00
EXPORT_SYMBOL_GPL ( range_4_20mA ) ;
2013-04-03 13:40:13 -07:00
const struct comedi_lrange range_0_32mA = { 1 , { RANGE_mA ( 0 , 32 ) } } ;
2013-04-12 10:11:54 -07:00
EXPORT_SYMBOL_GPL ( range_0_32mA ) ;
2010-05-03 17:39:09 +08:00
const struct comedi_lrange range_unknown = { 1 , { { 0 , 1000000 , UNIT_none } } } ;
2013-04-12 10:11:54 -07:00
EXPORT_SYMBOL_GPL ( range_unknown ) ;
2008-11-04 20:29:31 -08:00
/*
2015-01-28 18:41:48 +00:00
* COMEDI_RANGEINFO ioctl
* range information
*
* arg :
* pointer to comedi_rangeinfo structure
*
* reads :
* comedi_rangeinfo structure
*
* writes :
* array of comedi_krange structures to rangeinfo - > range_ptr pointer
*/
2010-05-03 15:50:09 -07:00
int do_rangeinfo_ioctl ( struct comedi_device * dev ,
2020-04-25 18:44:30 -04:00
struct comedi_rangeinfo * it )
2008-11-04 20:29:31 -08:00
{
int subd , chan ;
2009-03-16 22:05:31 -04:00
const struct comedi_lrange * lr ;
2009-03-16 22:05:14 -04:00
struct comedi_subdevice * s ;
2008-11-04 20:29:31 -08:00
2020-04-25 18:44:30 -04:00
subd = ( it - > range_type > > 24 ) & 0xf ;
chan = ( it - > range_type > > 16 ) & 0xff ;
2008-11-04 20:29:31 -08:00
if ( ! dev - > attached )
return - EINVAL ;
if ( subd > = dev - > n_subdevices )
return - EINVAL ;
2012-09-05 18:59:44 -07:00
s = & dev - > subdevices [ subd ] ;
2008-11-04 20:29:31 -08:00
if ( s - > range_table ) {
lr = s - > range_table ;
} else if ( s - > range_table_list ) {
if ( chan > = s - > n_chan )
return - EINVAL ;
lr = s - > range_table_list [ chan ] ;
} else {
return - EINVAL ;
}
2020-04-25 18:44:30 -04:00
if ( RANGE_LENGTH ( it - > range_type ) ! = lr - > length ) {
2013-11-22 10:45:58 -07:00
dev_dbg ( dev - > class_dev ,
" wrong length %d should be %d (0x%08x) \n " ,
2020-04-25 18:44:30 -04:00
RANGE_LENGTH ( it - > range_type ) ,
lr - > length , it - > range_type ) ;
2008-11-04 20:29:31 -08:00
return - EINVAL ;
}
2020-04-25 18:44:30 -04:00
if ( copy_to_user ( it - > range_ptr , lr - > range ,
2009-06-08 21:04:41 +05:30
sizeof ( struct comedi_krange ) * lr - > length ) )
2008-11-04 20:29:31 -08:00
return - EFAULT ;
return 0 ;
}
2013-07-24 10:00:45 -07:00
/**
* comedi_check_chanlist ( ) - Validate each element in a chanlist .
* @ s : comedi_subdevice struct
* @ n : number of elements in the chanlist
* @ chanlist : the chanlist to validate
2015-08-05 18:13:26 +01:00
*
* Each element consists of a channel number , a range index , an analog
* reference type and some flags , all packed into an unsigned int .
*
* This checks that the channel number and range index are supported by
* the comedi subdevice . It does not check whether the analog reference
* type and the flags are supported . Drivers that care should check those
* themselves .
*
* Return : % 0 if all @ chanlist elements are valid ( success ) ,
* % - EINVAL if one or more elements are invalid .
*/
2010-05-01 12:33:17 -07:00
int comedi_check_chanlist ( struct comedi_subdevice * s , int n ,
unsigned int * chanlist )
2008-11-04 20:29:31 -08:00
{
2012-08-16 14:38:05 +01:00
struct comedi_device * dev = s - > device ;
2013-07-24 10:00:45 -07:00
unsigned int chanspec ;
int chan , range_len , i ;
2008-11-04 20:29:31 -08:00
2014-03-06 01:57:18 -05:00
for ( i = 0 ; i < n ; i + + ) {
chanspec = chanlist [ i ] ;
chan = CR_CHAN ( chanspec ) ;
if ( s - > range_table )
range_len = s - > range_table - > length ;
else if ( s - > range_table_list & & chan < s - > n_chan )
range_len = s - > range_table_list [ chan ] - > length ;
else
range_len = 0 ;
if ( chan > = s - > n_chan | |
2014-10-07 23:20:24 +05:30
CR_RANGE ( chanspec ) > = range_len ) {
2014-03-06 01:57:18 -05:00
dev_warn ( dev - > class_dev ,
" bad chanlist[%d]=0x%08x chan=%d range length=%d \n " ,
i , chanspec , chan , range_len ) ;
return - EINVAL ;
}
}
2008-11-04 20:29:31 -08:00
return 0 ;
}
2013-04-12 10:11:54 -07:00
EXPORT_SYMBOL_GPL ( comedi_check_chanlist ) ;