staging: comedi: add a 'readback' member to comedi_subdevice

The analog output hardware in most comedi drivers does not provide a
way to readback to last values written to the channels. In order to
provide an (*insn_read) for the analog output subdevice, the comedi
drivers save the last values for each channel in the private data.

Add a new member, 'readback', to the comedi_subdevice definition to
provide a common way to save these values.

Introduce a comedi core function, comedi_alloc_subdev_readback(), to
allocate the memory needed to save the values. This memory will be
automatically kfree'd when the driver is detached.

Introduce a comedi core function, comedi_readback_insn_read(), that
the comedi drivers can use for the (*insn_read) of a subdevice to
return the saved values for each channel.

This will allow removing the boilerplate in the comedi drivers to
return the saved values. In some drivers it will also allow removing
the private data completely.

Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Reviewed-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
H Hartley Sweeten 2014-08-25 16:03:54 -07:00 committed by Greg Kroah-Hartman
parent d0be943042
commit d276206692
2 changed files with 48 additions and 0 deletions

View File

@ -88,6 +88,8 @@ struct comedi_subdevice {
struct device *class_dev; struct device *class_dev;
int minor; int minor;
unsigned int *readback;
}; };
struct comedi_buf_page { struct comedi_buf_page {
@ -448,6 +450,10 @@ unsigned int comedi_dio_update_state(struct comedi_subdevice *,
void *comedi_alloc_devpriv(struct comedi_device *, size_t); void *comedi_alloc_devpriv(struct comedi_device *, size_t);
int comedi_alloc_subdevices(struct comedi_device *, int); int comedi_alloc_subdevices(struct comedi_device *, int);
int comedi_alloc_subdev_readback(struct comedi_subdevice *);
int comedi_readback_insn_read(struct comedi_device *, struct comedi_subdevice *,
struct comedi_insn *, unsigned int *data);
int comedi_load_firmware(struct comedi_device *, struct device *, int comedi_load_firmware(struct comedi_device *, struct device *,
const char *name, const char *name,

View File

@ -96,6 +96,22 @@ int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
} }
EXPORT_SYMBOL_GPL(comedi_alloc_subdevices); EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
/**
* comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback.
* @s: comedi_subdevice struct
*/
int comedi_alloc_subdev_readback(struct comedi_subdevice *s)
{
if (!s->n_chan)
return -EINVAL;
s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL);
if (!s->readback)
return -ENOMEM;
return 0;
}
EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback);
static void comedi_device_detach_cleanup(struct comedi_device *dev) static void comedi_device_detach_cleanup(struct comedi_device *dev)
{ {
int i; int i;
@ -111,6 +127,7 @@ static void comedi_device_detach_cleanup(struct comedi_device *dev)
comedi_buf_alloc(dev, s, 0); comedi_buf_alloc(dev, s, 0);
kfree(s->async); kfree(s->async);
} }
kfree(s->readback);
} }
kfree(dev->subdevices); kfree(dev->subdevices);
dev->subdevices = NULL; dev->subdevices = NULL;
@ -156,6 +173,31 @@ int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
return -EINVAL; return -EINVAL;
} }
/**
* comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback.
* @dev: comedi_device struct
* @s: comedi_subdevice struct
* @insn: comedi_insn struct
* @data: pointer to return the readback data
*/
int comedi_readback_insn_read(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn,
unsigned int *data)
{
unsigned int chan = CR_CHAN(insn->chanspec);
int i;
if (!s->readback)
return -EINVAL;
for (i = 0; i < insn->n; i++)
data[i] = s->readback[chan];
return insn->n;
}
EXPORT_SYMBOL_GPL(comedi_readback_insn_read);
/** /**
* comedi_timeout() - busy-wait for a driver condition to occur. * comedi_timeout() - busy-wait for a driver condition to occur.
* @dev: comedi_device struct * @dev: comedi_device struct