drivers/i2c/i2c-dev.c
Source file repositories/reference/linux-study-clean/drivers/i2c/i2c-dev.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/i2c/i2c-dev.c- Extension
.c- Size
- 20616 bytes
- Lines
- 807
- Domain
- Driver Families
- Bucket
- drivers/i2c
- 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/cdev.hlinux/compat.hlinux/device.hlinux/fs.hlinux/i2c-dev.hlinux/i2c.hlinux/init.hlinux/jiffies.hlinux/kernel.hlinux/list.hlinux/module.hlinux/notifier.hlinux/slab.hlinux/uaccess.h
Detected Declarations
struct i2c_devstruct i2c_smbus_ioctl_data32struct i2c_msg32struct i2c_rdwr_ioctl_data32function put_i2c_devfunction name_showfunction i2c_adapterfunction i2cdev_writefunction i2cdev_checkfunction i2cdev_check_mux_parentsfunction i2cdev_check_mux_childrenfunction i2cdev_check_addrfunction i2cdev_ioctl_rdwrfunction slavefunction i2cdev_ioctl_smbusfunction i2cdev_ioctlfunction compat_i2cdev_ioctlfunction i2cdev_openfunction i2cdev_releasefunction i2cdev_dev_releasefunction i2cdev_attach_adapterfunction i2cdev_detach_adapterfunction i2cdev_notifier_callfunction i2c_dev_attach_adapterfunction i2c_dev_detach_adapterfunction i2c_dev_initfunction i2c_dev_exitmodule init i2c_dev_init
Annotated Snippet
static const struct file_operations i2cdev_fops = {
.owner = THIS_MODULE,
.read = i2cdev_read,
.write = i2cdev_write,
.unlocked_ioctl = i2cdev_ioctl,
.compat_ioctl = compat_i2cdev_ioctl,
.open = i2cdev_open,
.release = i2cdev_release,
};
/* ------------------------------------------------------------------------- */
static const struct class i2c_dev_class = {
.name = "i2c-dev",
.dev_groups = i2c_groups,
};
static void i2cdev_dev_release(struct device *dev)
{
struct i2c_dev *i2c_dev;
i2c_dev = container_of(dev, struct i2c_dev, dev);
kfree(i2c_dev);
}
static int i2cdev_attach_adapter(struct device *dev)
{
struct i2c_adapter *adap;
struct i2c_dev *i2c_dev;
int res;
if (dev->type != &i2c_adapter_type)
return NOTIFY_DONE;
adap = to_i2c_adapter(dev);
i2c_dev = get_free_i2c_dev(adap);
if (IS_ERR(i2c_dev))
return NOTIFY_DONE;
cdev_init(&i2c_dev->cdev, &i2cdev_fops);
i2c_dev->cdev.owner = THIS_MODULE;
device_initialize(&i2c_dev->dev);
i2c_dev->dev.devt = MKDEV(I2C_MAJOR, adap->nr);
i2c_dev->dev.class = &i2c_dev_class;
i2c_dev->dev.parent = &adap->dev;
i2c_dev->dev.release = i2cdev_dev_release;
res = dev_set_name(&i2c_dev->dev, "i2c-%d", adap->nr);
if (res)
goto err_put_i2c_dev;
res = cdev_device_add(&i2c_dev->cdev, &i2c_dev->dev);
if (res)
goto err_put_i2c_dev;
pr_debug("adapter [%s] registered as minor %d\n", adap->name, adap->nr);
return NOTIFY_OK;
err_put_i2c_dev:
put_i2c_dev(i2c_dev, false);
return NOTIFY_DONE;
}
static int i2cdev_detach_adapter(struct device *dev)
{
struct i2c_adapter *adap;
struct i2c_dev *i2c_dev;
if (dev->type != &i2c_adapter_type)
return NOTIFY_DONE;
adap = to_i2c_adapter(dev);
i2c_dev = i2c_dev_get_by_minor(adap->nr);
if (!i2c_dev) /* attach_adapter must have failed */
return NOTIFY_DONE;
put_i2c_dev(i2c_dev, true);
pr_debug("adapter [%s] unregistered\n", adap->name);
return NOTIFY_OK;
}
static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
void *data)
{
struct device *dev = data;
switch (action) {
case BUS_NOTIFY_ADD_DEVICE:
Annotation
- Immediate include surface: `linux/cdev.h`, `linux/compat.h`, `linux/device.h`, `linux/fs.h`, `linux/i2c-dev.h`, `linux/i2c.h`, `linux/init.h`, `linux/jiffies.h`.
- Detected declarations: `struct i2c_dev`, `struct i2c_smbus_ioctl_data32`, `struct i2c_msg32`, `struct i2c_rdwr_ioctl_data32`, `function put_i2c_dev`, `function name_show`, `function i2c_adapter`, `function i2cdev_write`, `function i2cdev_check`, `function i2cdev_check_mux_parents`.
- Atlas domain: Driver Families / drivers/i2c.
- 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.