Commit f8f76e90 authored by H Hartley Sweeten's avatar H Hartley Sweeten Committed by Greg Kroah-Hartman

staging: comedi: comedi_buf: factor out common comedi_buf_write_alloc_* code

The only difference between comedi_buf_write_alloc() and the *_strict()
version is that the *_strict() one will only allocate the chunk if it
can completely fulfill the request.

Factor out the common code and add a flag parameter to indicate the 'strict'
usage. Change the exported functions so they are just wrappers around the
common function.

Cleanup the common function a bit and use the comedi_buf_write_n_available()
helper to determine the number of bytes available.

comedi_buf_write_n_available() is not used outside this module so make it
static. Since the only caller is __comedi_buf_write_alloc(), which will
always have a valid async pointer and already has a memory barrier, we
can remove the unnecessary (async == NULL) test as well as the smp_mb().
Also, the rounding of the sample size can be removed since the caller
does not need it.
Signed-off-by: default avatarH Hartley Sweeten <hsweeten@visionengravers.com>
Signed-off-by: default avatarIan Abbott <abbotti@mev.co.uk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 61c9fb0e
...@@ -159,22 +159,30 @@ void comedi_buf_reset(struct comedi_async *async) ...@@ -159,22 +159,30 @@ void comedi_buf_reset(struct comedi_async *async)
async->events = 0; async->events = 0;
} }
unsigned int comedi_buf_write_n_available(struct comedi_async *async) static unsigned int comedi_buf_write_n_available(struct comedi_async *async)
{ {
unsigned int free_end; unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
unsigned int nbytes;
if (async == NULL) return free_end - async->buf_write_alloc_count;
return 0; }
free_end = async->buf_read_count + async->prealloc_bufsz; static unsigned int __comedi_buf_write_alloc(struct comedi_async *async,
nbytes = free_end - async->buf_write_alloc_count; unsigned int nbytes,
nbytes -= nbytes % bytes_per_sample(async->subdevice); int strict)
/* barrier insures the read of buf_read_count in this {
query occurs before any following writes to the buffer which unsigned int available = comedi_buf_write_n_available(async);
might be based on the return value from this query.
if (nbytes > available)
nbytes = strict ? 0 : available;
async->buf_write_alloc_count += nbytes;
/*
* ensure the async buffer 'counts' are read and updated
* before we write data to the write-alloc'ed buffer space
*/ */
smp_mb(); smp_mb();
return nbytes; return nbytes;
} }
...@@ -182,16 +190,7 @@ unsigned int comedi_buf_write_n_available(struct comedi_async *async) ...@@ -182,16 +190,7 @@ unsigned int comedi_buf_write_n_available(struct comedi_async *async)
unsigned int comedi_buf_write_alloc(struct comedi_async *async, unsigned int comedi_buf_write_alloc(struct comedi_async *async,
unsigned int nbytes) unsigned int nbytes)
{ {
unsigned int free_end = async->buf_read_count + async->prealloc_bufsz; return __comedi_buf_write_alloc(async, nbytes, 0);
if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
nbytes = free_end - async->buf_write_alloc_count;
async->buf_write_alloc_count += nbytes;
/* barrier insures the read of buf_read_count above occurs before
we write data to the write-alloc'ed buffer space */
smp_mb();
return nbytes;
} }
EXPORT_SYMBOL(comedi_buf_write_alloc); EXPORT_SYMBOL(comedi_buf_write_alloc);
...@@ -199,16 +198,7 @@ EXPORT_SYMBOL(comedi_buf_write_alloc); ...@@ -199,16 +198,7 @@ EXPORT_SYMBOL(comedi_buf_write_alloc);
unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async, unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
unsigned int nbytes) unsigned int nbytes)
{ {
unsigned int free_end = async->buf_read_count + async->prealloc_bufsz; return __comedi_buf_write_alloc(async, nbytes, 1);
if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
nbytes = 0;
async->buf_write_alloc_count += nbytes;
/* barrier insures the read of buf_read_count above occurs before
we write data to the write-alloc'ed buffer space */
smp_mb();
return nbytes;
} }
/* munging is applied to data by core as it passes between user /* munging is applied to data by core as it passes between user
......
...@@ -438,7 +438,6 @@ comedi_to_usb_interface(struct comedi_device *dev) ...@@ -438,7 +438,6 @@ comedi_to_usb_interface(struct comedi_device *dev)
int comedi_buf_put(struct comedi_async *async, short x); int comedi_buf_put(struct comedi_async *async, short x);
int comedi_buf_get(struct comedi_async *async, short *x); int comedi_buf_get(struct comedi_async *async, short *x);
unsigned int comedi_buf_write_n_available(struct comedi_async *async);
unsigned int comedi_buf_write_alloc(struct comedi_async *async, unsigned int comedi_buf_write_alloc(struct comedi_async *async,
unsigned int nbytes); unsigned int nbytes);
unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async, unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment