drivers/iio/buffer/kfifo_buf.c
Source file repositories/reference/linux-study-clean/drivers/iio/buffer/kfifo_buf.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/iio/buffer/kfifo_buf.c- Extension
.c- Size
- 6490 bytes
- Lines
- 273
- Domain
- Driver Families
- Bucket
- drivers/iio
- Inferred role
- Driver Families: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/slab.hlinux/kernel.hlinux/module.hlinux/device.hlinux/workqueue.hlinux/kfifo.hlinux/mutex.hlinux/iio/iio.hlinux/iio/buffer.hlinux/iio/kfifo_buf.hlinux/iio/buffer_impl.hlinux/sched.hlinux/poll.h
Detected Declarations
struct iio_kfifofunction __iio_allocate_kfifofunction iio_request_update_kfifofunction iio_mark_update_needed_kfifofunction iio_set_bytes_per_datum_kfifofunction iio_set_length_kfifofunction iio_store_to_kfifofunction iio_read_kfifofunction iio_kfifo_buf_data_availablefunction iio_kfifo_buffer_releasefunction iio_kfifo_buf_space_availablefunction iio_kfifo_remove_fromfunction iio_kfifo_writefunction iio_kfifo_freefunction devm_iio_kfifo_releasefunction iio_kfifo_allocateexport iio_kfifo_allocateexport iio_kfifo_freeexport devm_iio_kfifo_buffer_setup_ext
Annotated Snippet
struct iio_kfifo {
struct iio_buffer buffer;
struct kfifo kf;
struct mutex user_lock;
int update_needed;
};
#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
size_t bytes_per_datum, unsigned int length)
{
if ((length == 0) || (bytes_per_datum == 0))
return -EINVAL;
/*
* Make sure we don't overflow an unsigned int after kfifo rounds up to
* the next power of 2.
*/
if (roundup_pow_of_two(length) > UINT_MAX / bytes_per_datum)
return -EINVAL;
return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
bytes_per_datum, GFP_KERNEL);
}
static int iio_request_update_kfifo(struct iio_buffer *r)
{
int ret = 0;
struct iio_kfifo *buf = iio_to_kfifo(r);
mutex_lock(&buf->user_lock);
if (buf->update_needed) {
kfifo_free(&buf->kf);
ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
buf->buffer.length);
if (ret >= 0)
buf->update_needed = false;
} else {
kfifo_reset_out(&buf->kf);
}
mutex_unlock(&buf->user_lock);
return ret;
}
static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
{
struct iio_kfifo *kf = iio_to_kfifo(r);
kf->update_needed = true;
return 0;
}
static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
{
if (r->bytes_per_datum != bpd) {
r->bytes_per_datum = bpd;
iio_mark_update_needed_kfifo(r);
}
return 0;
}
static int iio_set_length_kfifo(struct iio_buffer *r, unsigned int length)
{
/* Avoid an invalid state */
if (length < 2)
length = 2;
if (r->length != length) {
r->length = length;
iio_mark_update_needed_kfifo(r);
}
return 0;
}
static int iio_store_to_kfifo(struct iio_buffer *r,
const void *data)
{
int ret;
struct iio_kfifo *kf = iio_to_kfifo(r);
ret = kfifo_in(&kf->kf, data, 1);
if (ret != 1)
return -EBUSY;
return 0;
}
static int iio_read_kfifo(struct iio_buffer *r, size_t n, char __user *buf)
{
int ret, copied;
struct iio_kfifo *kf = iio_to_kfifo(r);
Annotation
- Immediate include surface: `linux/slab.h`, `linux/kernel.h`, `linux/module.h`, `linux/device.h`, `linux/workqueue.h`, `linux/kfifo.h`, `linux/mutex.h`, `linux/iio/iio.h`.
- Detected declarations: `struct iio_kfifo`, `function __iio_allocate_kfifo`, `function iio_request_update_kfifo`, `function iio_mark_update_needed_kfifo`, `function iio_set_bytes_per_datum_kfifo`, `function iio_set_length_kfifo`, `function iio_store_to_kfifo`, `function iio_read_kfifo`, `function iio_kfifo_buf_data_available`, `function iio_kfifo_buffer_release`.
- Atlas domain: Driver Families / drivers/iio.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.