drivers/hid/hid-mcp2221.c
Source file repositories/reference/linux-study-clean/drivers/hid/hid-mcp2221.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hid/hid-mcp2221.c- Extension
.c- Size
- 31032 bytes
- Lines
- 1366
- Domain
- Driver Families
- Bucket
- drivers/hid
- 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.
- 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/module.hlinux/err.hlinux/mutex.hlinux/bitfield.hlinux/completion.hlinux/delay.hlinux/hid.hlinux/hidraw.hlinux/i2c.hlinux/gpio/driver.hlinux/iio/iio.hlinux/minmax.hlinux/moduleparam.hhid-ids.h
Detected Declarations
struct mcp_set_gpiostruct mcp_get_gpiostruct mcp2221struct mcp2221_iiofunction mcp_send_reportfunction mcp_send_data_req_statusfunction mcp_chk_last_cmd_statusfunction mcp_cancel_last_cmdfunction mcp_chk_last_cmd_status_free_busfunction mcp_set_i2c_speedfunction mcp_i2c_writefunction datafunction mcp_i2c_xferfunction mcp_smbus_writefunction mcp_smbus_xferfunction mcp_i2c_funcfunction mcp_gpio_read_sramfunction mcp2221_check_gpio_pinfuncfunction mcp_gpio_getfunction mcp_gpio_setfunction mcp_gpio_dir_setfunction mcp_gpio_direction_inputfunction mcp_gpio_direction_outputfunction mcp_gpio_get_directionfunction mcp_get_i2c_eng_statefunction mcp2221_raw_eventfunction mcp2221_hid_unregisterfunction mcp2221_removefunction mcp2221_read_rawfunction mcp2221_write_rawfunction mcp_iio_channelsfunction mcp_init_workfunction mcp2221_probe
Annotated Snippet
struct mcp_set_gpio {
u8 cmd;
u8 dummy;
struct {
u8 change_value;
u8 value;
u8 change_direction;
u8 direction;
} gpio[MCP_NGPIO];
} __packed;
/* MCP GPIO get command layout */
struct mcp_get_gpio {
u8 cmd;
u8 dummy;
struct {
u8 value;
u8 direction;
} gpio[MCP_NGPIO];
} __packed;
/*
* There is no way to distinguish responses. Therefore next command
* is sent only after response to previous has been received. Mutex
* lock is used for this purpose mainly.
*/
struct mcp2221 {
struct hid_device *hdev;
struct i2c_adapter adapter;
struct mutex lock;
struct completion wait_in_report;
struct delayed_work init_work;
u8 *rxbuf;
u8 txbuf[64];
int rxbuf_idx;
int rxbuf_size;
int status;
u8 cur_i2c_clk_div;
struct gpio_chip *gc;
u8 gp_idx;
u8 gpio_dir;
u8 mode[4];
#if IS_REACHABLE(CONFIG_IIO)
struct iio_chan_spec iio_channels[3];
u16 adc_values[3];
u8 adc_scale;
u8 dac_value;
u16 dac_scale;
#endif
};
struct mcp2221_iio {
struct mcp2221 *mcp;
};
/*
* Default i2c bus clock frequency 400 kHz. Modify this if you
* want to set some other frequency (min 50 kHz - max 400 kHz).
*/
static uint i2c_clk_freq = 400;
/* Synchronously send output report to the device */
static int mcp_send_report(struct mcp2221 *mcp,
u8 *out_report, size_t len)
{
u8 *buf;
int ret;
buf = kmemdup(out_report, len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
/* mcp2221 uses interrupt endpoint for out reports */
ret = hid_hw_output_report(mcp->hdev, buf, len);
kfree(buf);
if (ret < 0)
return ret;
return 0;
}
/*
* Send o/p report to the device and wait for i/p report to be
* received from the device. If the device does not respond,
* we timeout.
*/
static int mcp_send_data_req_status(struct mcp2221 *mcp,
u8 *out_report, int len)
{
int ret;
Annotation
- Immediate include surface: `linux/module.h`, `linux/err.h`, `linux/mutex.h`, `linux/bitfield.h`, `linux/completion.h`, `linux/delay.h`, `linux/hid.h`, `linux/hidraw.h`.
- Detected declarations: `struct mcp_set_gpio`, `struct mcp_get_gpio`, `struct mcp2221`, `struct mcp2221_iio`, `function mcp_send_report`, `function mcp_send_data_req_status`, `function mcp_chk_last_cmd_status`, `function mcp_cancel_last_cmd`, `function mcp_chk_last_cmd_status_free_bus`, `function mcp_set_i2c_speed`.
- Atlas domain: Driver Families / drivers/hid.
- Implementation status: source 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.