drivers/iio/industrialio-buffer.c
Source file repositories/reference/linux-study-clean/drivers/iio/industrialio-buffer.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/iio/industrialio-buffer.c- Extension
.c- Size
- 65736 bytes
- Lines
- 2540
- Domain
- Driver Families
- Bucket
- drivers/iio
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern 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.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- 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/atomic.hlinux/anon_inodes.hlinux/cleanup.hlinux/kernel.hlinux/export.hlinux/device.hlinux/dma-buf.hlinux/dma-fence.hlinux/dma-resv.hlinux/file.hlinux/fs.hlinux/cdev.hlinux/slab.hlinux/mm.hlinux/poll.hlinux/sched/signal.hlinux/iio/iio.hlinux/iio/iio-opaque.hiio_core.hiio_core_trigger.hlinux/iio/sysfs.hlinux/iio/buffer.hlinux/iio/buffer_impl.h
Detected Declarations
struct iio_dmabuf_privstruct iio_dma_fencestruct iio_device_configstruct iio_demux_tablefunction iio_buffer_is_activefunction iio_buffer_data_availablefunction iio_buffer_flush_hwfifofunction iio_buffer_readyfunction iio_buffer_readfunction iio_buffer_space_availablefunction iio_buffer_writefunction iio_buffer_pollfunction iio_buffer_read_wrapperfunction iio_buffer_write_wrapperfunction iio_buffer_poll_wrapperfunction pollfunction iio_pop_from_bufferfunction iio_buffer_initfunction iio_device_detach_buffersfunction iio_show_scan_indexfunction iio_show_fixed_typefunction iio_scan_el_showfunction iio_validate_scan_maskfunction iio_scan_mask_setfunction iio_scan_mask_clearfunction iio_scan_mask_queryfunction iio_scan_el_storefunction iio_scan_el_ts_showfunction iio_scan_el_ts_storefunction iio_buffer_add_channel_sysfsfunction length_showfunction length_storefunction enable_showfunction iio_storage_bytes_for_sifunction iio_storage_bytes_for_timestampfunction iio_compute_scan_bytesfunction iio_buffer_activatefunction iio_buffer_deactivatefunction iio_buffer_deactivate_allfunction iio_buffer_enablefunction iio_buffer_disablefunction iio_buffer_update_bytes_per_datumfunction iio_buffer_request_updatefunction iio_free_scan_maskfunction iio_verify_updatefunction list_for_each_entryfunction list_for_each_entryfunction iio_buffer_demux_free
Annotated Snippet
static const struct file_operations iio_buffer_chrdev_fileops = {
.owner = THIS_MODULE,
.llseek = noop_llseek,
.read = iio_buffer_read,
.write = iio_buffer_write,
.unlocked_ioctl = iio_buffer_chrdev_ioctl,
.compat_ioctl = compat_ptr_ioctl,
.poll = iio_buffer_poll,
.release = iio_buffer_chrdev_release,
};
static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg)
{
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
int __user *ival = (int __user *)arg;
struct iio_dev_buffer_pair *ib;
struct iio_buffer *buffer;
int fd, idx, ret;
if (copy_from_user(&idx, ival, sizeof(idx)))
return -EFAULT;
if (idx >= iio_dev_opaque->attached_buffers_cnt)
return -ENODEV;
iio_device_get(indio_dev);
buffer = iio_dev_opaque->attached_buffers[idx];
if (test_and_set_bit(IIO_BUSY_BIT_POS, &buffer->flags)) {
ret = -EBUSY;
goto error_iio_dev_put;
}
ib = kzalloc_obj(*ib);
if (!ib) {
ret = -ENOMEM;
goto error_clear_busy_bit;
}
ib->indio_dev = indio_dev;
ib->buffer = buffer;
fd = anon_inode_getfd("iio:buffer", &iio_buffer_chrdev_fileops,
ib, O_RDWR | O_CLOEXEC);
if (fd < 0) {
ret = fd;
goto error_free_ib;
}
if (copy_to_user(ival, &fd, sizeof(fd))) {
/*
* "Leak" the fd, as there's not much we can do about this
* anyway. 'fd' might have been closed already, as
* anon_inode_getfd() called fd_install() on it, which made
* it reachable by userland.
*
* Instead of allowing a malicious user to play tricks with
* us, rely on the process exit path to do any necessary
* cleanup, as in releasing the file, if still needed.
*/
return -EFAULT;
}
return 0;
error_free_ib:
kfree(ib);
error_clear_busy_bit:
clear_bit(IIO_BUSY_BIT_POS, &buffer->flags);
error_iio_dev_put:
iio_device_put(indio_dev);
return ret;
}
static long iio_device_buffer_ioctl(struct iio_dev *indio_dev, struct file *filp,
unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case IIO_BUFFER_GET_FD_IOCTL:
return iio_device_buffer_getfd(indio_dev, arg);
default:
return IIO_IOCTL_UNHANDLED;
}
}
static int iio_channel_validate_scan_type(struct device *dev, int ch,
const struct iio_scan_type *scan_type)
{
/* Verify that sample bits fit into storage */
Annotation
- Immediate include surface: `linux/atomic.h`, `linux/anon_inodes.h`, `linux/cleanup.h`, `linux/kernel.h`, `linux/export.h`, `linux/device.h`, `linux/dma-buf.h`, `linux/dma-fence.h`.
- Detected declarations: `struct iio_dmabuf_priv`, `struct iio_dma_fence`, `struct iio_device_config`, `struct iio_demux_table`, `function iio_buffer_is_active`, `function iio_buffer_data_available`, `function iio_buffer_flush_hwfifo`, `function iio_buffer_ready`, `function iio_buffer_read`, `function iio_buffer_space_available`.
- Atlas domain: Driver Families / drivers/iio.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.