On 22/10/14 23:36, H Hartley Sweeten wrote:
Introduce a generic method to write samples to the async buffer. The number of samples is first checked against the number of samples that would fill the async buffer. The size of each sample is determined using the bytes_per_sample() helper. If all the samples will fit in the async buffer they are written to the buffer using comedi_write_array_to_buffer(). This will allow converting all the comedi drivers to use a common method to write data to the async buffer. Since comedi_write_array_to_buffer() sets the COMEDI_CB_BLOCK event after writing the data, those events can be removed from the drivers. In addition, comedi_inc_scan_progress() will automatically detect the end of scan and set the COMEDI_CB_EOS event. Those events can also be removed from the drivers. Signed-off-by: H Hartley Sweeten <hsweeten@xxxxxxxxxxxxxxxxxxx> Cc: Ian Abbott <abbotti@xxxxxxxxx> Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- drivers/staging/comedi/comedi_buf.c | 31 +++++++++++++++++++++++++++++++ drivers/staging/comedi/comedidev.h | 2 ++ 2 files changed, 33 insertions(+) diff --git a/drivers/staging/comedi/comedi_buf.c b/drivers/staging/comedi/comedi_buf.c index 1f91607..a41a414 100644 --- a/drivers/staging/comedi/comedi_buf.c +++ b/drivers/staging/comedi/comedi_buf.c @@ -528,6 +528,37 @@ unsigned int comedi_write_array_to_buffer(struct comedi_subdevice *s, EXPORT_SYMBOL_GPL(comedi_write_array_to_buffer); /** + * comedi_buf_write_samples - write sample data to comedi buffer + * @s: comedi_subdevice struct + * @data: samples + * @nsamples: number of samples + * + * Writes nsamples to the comedi buffer associated with the subdevice, marks + * it as written and updates the acquisition scan progress. + * + * Returns the amount of data written in bytes. + */ +unsigned int comedi_buf_write_samples(struct comedi_subdevice *s, + const void *data, unsigned int nsamples) +{ + unsigned int max_samples; + unsigned int nbytes; + + /* make sure there is enought room in the buffer for all the samples */ + max_samples = comedi_buf_write_n_available(s) / bytes_per_sample(s); + if (nsamples > max_samples) { + dev_warn(s->device->class_dev, "buffer overrun\n"); + s->async->events |= COMEDI_CB_OVERFLOW; + return 0;
Maybe instead of returning 0 here, it could set nsamples to max_samples and carry on. Then at least it might write some samples to the buffer. For example, in your patch 08, the 'for' loop you replaced might have written some samples to the buffer before flagging the overrun, but the replacement wouldn't.
+ }
You could return early here if nsamples == 0.
+ + nbytes = nsamples * bytes_per_sample(s); + + return comedi_write_array_to_buffer(s, data, nbytes); +} +EXPORT_SYMBOL_GPL(comedi_buf_write_samples); + +/** * comedi_buf_read_samples - read sample data from comedi buffer * @s: comedi_subdevice struct * @data: destination diff --git a/drivers/staging/comedi/comedidev.h b/drivers/staging/comedi/comedidev.h index 9f4f9ef..eee4dd4 100644 --- a/drivers/staging/comedi/comedidev.h +++ b/drivers/staging/comedi/comedidev.h @@ -446,6 +446,8 @@ int comedi_buf_put(struct comedi_subdevice *s, unsigned short x); unsigned int comedi_write_array_to_buffer(struct comedi_subdevice *s, const void *data, unsigned int num_bytes); +unsigned int comedi_buf_write_samples(struct comedi_subdevice *s, + const void *data, unsigned int nsamples); unsigned int comedi_buf_read_samples(struct comedi_subdevice *s, void *data, unsigned int nsamples);
-- -=( Ian Abbott @ MEV Ltd. E-mail: <abbotti@xxxxxxxxx> )=- -=( Web: http://www.mev.co.uk/ )=- _______________________________________________ devel mailing list devel@xxxxxxxxxxxxxxxxxxxxxx http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel