drivers/iio/chemical/scd4x.c
Source file repositories/reference/linux-study-clean/drivers/iio/chemical/scd4x.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/iio/chemical/scd4x.c- Extension
.c- Size
- 17405 bytes
- Lines
- 769
- Domain
- Driver Families
- Bucket
- drivers/iio
- Inferred role
- Driver Families: implementation source
- Status
- source 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/unaligned.hlinux/crc8.hlinux/delay.hlinux/device.hlinux/i2c.hlinux/iio/buffer.hlinux/iio/iio.hlinux/iio/sysfs.hlinux/iio/trigger.hlinux/iio/trigger_consumer.hlinux/iio/triggered_buffer.hlinux/iio/types.hlinux/kernel.hlinux/mutex.hlinux/string.hlinux/sysfs.hlinux/types.h
Detected Declarations
struct scd4x_stateenum scd4x_cmdenum scd4x_channel_idxfunction scd4x_i2c_xferfunction scd4x_send_commandfunction scd4x_readfunction scd4x_writefunction scd4x_write_and_fetchfunction scd4x_read_measfunction scd4x_wait_meas_pollfunction scd4x_read_pollfunction scd4x_read_channelfunction scd4x_read_rawfunction scd4x_read_availfunction scd4x_write_rawfunction calibration_auto_enable_showfunction calibration_auto_enable_storefunction calibration_forced_value_storefunction scd4x_suspendfunction scd4x_resumefunction scd4x_stop_measfunction scd4x_disable_regulatorfunction scd4x_trigger_handlerfunction scd4x_probe
Annotated Snippet
struct scd4x_state {
struct i2c_client *client;
/* maintain access to device, to prevent concurrent reads/writes */
struct mutex lock;
struct regulator *vdd;
};
DECLARE_CRC8_TABLE(scd4x_crc8_table);
static int scd4x_i2c_xfer(struct scd4x_state *state, char *txbuf, int txsize,
char *rxbuf, int rxsize)
{
struct i2c_client *client = state->client;
int ret;
ret = i2c_master_send(client, txbuf, txsize);
if (ret < 0)
return ret;
if (ret != txsize)
return -EIO;
if (rxsize == 0)
return 0;
ret = i2c_master_recv(client, rxbuf, rxsize);
if (ret < 0)
return ret;
if (ret != rxsize)
return -EIO;
return 0;
}
static int scd4x_send_command(struct scd4x_state *state, enum scd4x_cmd cmd)
{
char buf[SCD4X_COMMAND_BUF_SIZE];
int ret;
/*
* Measurement needs to be stopped before sending commands.
* Except stop and start command.
*/
if ((cmd != CMD_STOP_MEAS) && (cmd != CMD_START_MEAS)) {
ret = scd4x_send_command(state, CMD_STOP_MEAS);
if (ret)
return ret;
/* execution time for stopping measurement */
msleep_interruptible(500);
}
put_unaligned_be16(cmd, buf);
ret = scd4x_i2c_xfer(state, buf, 2, buf, 0);
if (ret)
return ret;
if ((cmd != CMD_STOP_MEAS) && (cmd != CMD_START_MEAS)) {
ret = scd4x_send_command(state, CMD_START_MEAS);
if (ret)
return ret;
}
return 0;
}
static int scd4x_read(struct scd4x_state *state, enum scd4x_cmd cmd,
void *response, int response_sz)
{
struct i2c_client *client = state->client;
char buf[SCD4X_READ_BUF_SIZE];
char *rsp = response;
int i, ret;
char crc;
/*
* Measurement needs to be stopped before sending commands.
* Except for reading measurement and data ready command.
*/
if ((cmd != CMD_GET_DATA_READY) && (cmd != CMD_READ_MEAS) &&
(cmd != CMD_GET_AMB_PRESSURE)) {
ret = scd4x_send_command(state, CMD_STOP_MEAS);
if (ret)
return ret;
/* execution time for stopping measurement */
msleep_interruptible(500);
}
Annotation
- Immediate include surface: `linux/unaligned.h`, `linux/crc8.h`, `linux/delay.h`, `linux/device.h`, `linux/i2c.h`, `linux/iio/buffer.h`, `linux/iio/iio.h`, `linux/iio/sysfs.h`.
- Detected declarations: `struct scd4x_state`, `enum scd4x_cmd`, `enum scd4x_channel_idx`, `function scd4x_i2c_xfer`, `function scd4x_send_command`, `function scd4x_read`, `function scd4x_write`, `function scd4x_write_and_fetch`, `function scd4x_read_meas`, `function scd4x_wait_meas_poll`.
- Atlas domain: Driver Families / drivers/iio.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
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.