static long i2c_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
		return -EINVAL;
	}

	switch (_IOC_NR(cmd)) {
		case I2C_WRITEREG:
			/* write to an i2c slave */
			D(printk(KERN_DEBUG "i2cw %d %d %d\n",
				 I2C_ARGSLAVE(arg),
				 I2C_ARGREG(arg),
				 I2C_ARGVALUE(arg)));

			return i2c_writereg(I2C_ARGSLAVE(arg),
					    I2C_ARGREG(arg),
					    I2C_ARGVALUE(arg));
		case I2C_READREG:
		{
			unsigned char val;
			/* read from an i2c slave */
			D(printk(KERN_DEBUG "i2cr %d %d ",
				I2C_ARGSLAVE(arg),
				I2C_ARGREG(arg)));
			val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
			D(printk(KERN_DEBUG "= %d\n", val));
			return val;
		}
		default:
			return -EINVAL;

	}
	return 0;
}
Beispiel #2
0
static long
i2c_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	int ret;
	if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
		return -ENOTTY;
	}

	switch (_IOC_NR(cmd)) {
		case I2C_WRITEREG:
			
			D(printk("i2cw %d %d %d\n",
				 I2C_ARGSLAVE(arg),
				 I2C_ARGREG(arg),
				 I2C_ARGVALUE(arg)));

			mutex_lock(&i2c_mutex);
			ret = i2c_writereg(I2C_ARGSLAVE(arg),
					    I2C_ARGREG(arg),
					    I2C_ARGVALUE(arg));
			mutex_unlock(&i2c_mutex);
			return ret;

		case I2C_READREG:
		{
			unsigned char val;
			
			D(printk("i2cr %d %d ",
				I2C_ARGSLAVE(arg),
				I2C_ARGREG(arg)));
			mutex_lock(&i2c_mutex);
			val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
			mutex_unlock(&i2c_mutex);
			D(printk("= %d\n", val));
			return val;
		}
		default:
			return -EINVAL;

	}

	return 0;
}
Beispiel #3
0
/*#---------------------------------------------------------------------------
 *#
 *# FUNCTION NAME: i2c_ioctl
 *#
 *# DESCRIPTION  : Main device API: ioctl's to write/read
 *#                to/from i2c registers
 *#
 *# PARAMETERS   : *inode: reference to inode
 *#                *filp : reference to file pointer
 *#                cmd   : command to be executed during the ioctl call
 *#                arg   : pointer to a structure with the data???
 *#
 *# RETURN       : result of the ioctl call
 *#
 *#---------------------------------------------------------------------------
 */
static int i2c_ioctl( struct inode *inode
                    , struct file *file
                    , unsigned int cmd
                    , unsigned long arg
                    )
{
    /* the acme ioctls */
    I2C_DATA i2cdata;
    int RetVal = EI2CNOERRORS;

    if ( _IOC_TYPE( cmd ) != ETRAXI2C_IOCTYPE )
    {
        return ( -EINVAL );
    }

    switch ( _IOC_NR( cmd ) )
    {
    case I2C_WRITEREG:
        /* write to an i2c slave */
        RetVal = i2c_writereg( I2C_ARGSLAVE( arg )
                             , I2C_ARGREG( arg )
                             , I2C_ARGVALUE( arg )
                             );
        break;

    case I2C_READREG:
        RetVal = i2c_readreg( I2C_ARGSLAVE( arg ), I2C_ARGREG( arg ) );
        break;

    /* New functions added by GVC */
    case I2C_READ:
        copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
        {
            int RetryCntr = MAXRETRIES;

            do
            {
                RetVal = i2c_command( i2cdata.slave
                                    , NULL
                                    , 0
                                    , i2cdata.rbuf
                                    , i2cdata.rlen
                                    );
             } while ( ( EI2CNOERRORS != RetVal )
                     &&( --RetryCntr )
                     );
        }
        copy_to_user( (char*)arg, (char*)&i2cdata, sizeof( I2C_DATA ) );
        break;

    case I2C_WRITE:
        copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
        {
            int RetryCntr = MAXRETRIES;

            do
            {
                RetVal = i2c_command( i2cdata.slave
                                    , i2cdata.wbuf
                                    , i2cdata.wlen
                                    , NULL
                                    , 0
                                    );
             } while ( ( EI2CNOERRORS != RetVal )
                     &&( --RetryCntr )
                     );
        }
        break;

    case I2C_WRITEREAD:
        copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
        {
            int RetryCntr = MAXRETRIES;

            do
            {
                RetVal = i2c_command( i2cdata.slave
                                    , i2cdata.wbuf
                                    , i2cdata.wlen
                                    , i2cdata.rbuf
                                    , i2cdata.rlen
                                    );
             } while ( ( EI2CNOERRORS != RetVal )
                     &&( --RetryCntr )
                     );
        }
        copy_to_user( (char*)arg, (char*)&i2cdata, sizeof( I2C_DATA ) );
        break;

    default:
        RetVal = -EINVAL;
    }

    return ( -RetVal );
}   /* i2c_ioctl */