STRUCT COMEDI_ASYNC(9)
π struct comedi_async β the Kernel API Manual moment
okay so bestie, we're in the Kernel API Manual today, and the star of the show is struct comedi_async, which is literally the control data for asynchronous COMEDI commands β¨ like that's its whole job, that's the vibe.
β¨ NAME β¨
struct comedi_async β control data for asynchronous COMEDI commands π
πΈ SYNOPSIS πΈ
struct comedi_async {
unsigned int prealloc_bufsz;
struct comedi_buf_map *buf_map;
unsigned int max_bufsize;
unsigned int buf_write_count;
unsigned int buf_write_alloc_count;
unsigned int buf_read_count;
unsigned int buf_read_alloc_count;
unsigned int buf_write_ptr;
unsigned int buf_read_ptr;
unsigned int cur_chan;
unsigned int scans_done;
unsigned int scan_progress;
unsigned int munge_chan;
unsigned int munge_count;
unsigned int munge_ptr;
unsigned int events;
struct comedi_cmd cmd;
wait_queue_head_t wait_head;
struct completion run_complete;
refcount_t run_active;
unsigned int cb_mask;
int (*inttrig)(struct comedi_device *dev, struct comedi_subdevice *s, unsigned int x);
};
π«Ά Members π«Ά
like, every single one, no skips, periodt:
prealloc_bufszβ the buffer size, in bytes πbuf_mapβ map of the buffer pages πΊοΈmax_bufsizeβ the max buffer size you're allowed, in bytes πbuf_write_countβ the "write completed" count (in bytes, modulo 2**32)buf_write_alloc_countβ the "allocated for writing" count (in bytes, modulo 2**32)buf_read_countβ the "read completed" count (in bytes, modulo 2**32)buf_read_alloc_countβ the "allocated for reading" count (in bytes, modulo 2**32)buf_write_ptrβ buffer position for the writer βοΈbuf_read_ptrβ buffer position for the reader πcur_chanβ current position in the chanlist for the scan (for those drivers that actually use it, iykyk)scans_doneβ how many scans have been completed π₯scan_progressβ amount received or sent for the current scan, in bytesmunge_chanβ current position in the chanlist for the "munging" π§munge_countβ the "munge" count (in bytes, modulo 2**32)munge_ptrβ buffer position for the "munging" πeventsβ bit-vector of the events that have occurred βcmdβ all the details of the comedi command currently in progresswait_headβ task wait queue for the file reader or writer π΄run_completeβ the "run complete" completion event βrun_activeβ the "run active" reference countercb_maskβ bit-vector of events that should wake the waiting tasks β°inttrigβ software trigger function for the command, orNULLπ
π Description π
okay so first, a note about the ..._count and ..._ptr members, because this is the part where it gets iconic:
Think of the _Count values as being integers of unlimited size, indexing into a buffer of infinite length (even though only an advancing portion of the buffer of fixed length prealloc_bufsz is accessible at any time). Then:
Buf_Read_Count <= Buf_Read_Alloc_Count <= Munge_Count <= Buf_Write_Count
<= Buf_Write_Alloc_Count <= (Buf_Read_Count + prealloc_bufsz)
(and no, those aren't the actual member names, apart from prealloc_bufsz β don't come for me π). When the buffer gets reset, those _Count values start at 0 and only ever increase, maintaining the inequalities above until the next time the buffer is reset. The buffer gets divided into these regions by the inequalities:
[0, Buf_Read_Count): old region, no longer accessible β[Buf_Read_Count, Buf_Read_Alloc_Count): filled and munged region, allocated for reading but not yet read[Buf_Read_Alloc_Count, Munge_Count): filled and munged region, not yet allocated for reading[Munge_Count, Buf_Write_Count): filled region, not yet munged[Buf_Write_Count, Buf_Write_Alloc_Count): unfilled region, allocated for writing but not yet written[Buf_Write_Alloc_Count, Buf_Read_Count + prealloc_bufsz): unfilled region, not yet allocated for writing[Buf_Read_Count + prealloc_bufsz, infinity): unfilled region, not yet accessible π
Data needs to be written into the buffer before it can be read out, and it may need to be converted (or "munged") between those two operations. Extra unfilled buffer space may need to be allocated for writing (advancing Buf_Write_Alloc_Count) before new data is written. After writing new data, the newly filled space needs to be released (advancing Buf_Write_Count). This also results in the new data being "munged" (advancing Munge_Count). Before data is read out of the buffer, extra space may need to be allocated for reading (advancing Buf_Read_Alloc_Count). After the data has been read out, the space needs to be released (advancing Buf_Read_Count) π«Ά
The actual members β buf_read_count, buf_read_alloc_count, munge_count, buf_write_count, and buf_write_alloc_count β take the value of the corresponding capitalized _Count values modulo 2^32 (UINT_MAX+1). Subtracting a "higher" _count value from a "lower" _count value gives the same answer as subtracting a "higher" _Count value from a lower _Count value, because prealloc_bufsz < UINT_MAX+1. The modulo operation is done implicitly, she's just like that π
The buf_read_ptr, munge_ptr, and buf_write_ptr members take the value of the corresponding capitalized _Count values modulo prealloc_bufsz. These correspond to byte indices in the physical buffer. The modulo operation is done by subtracting prealloc_bufsz when the value exceeds prealloc_bufsz (assuming prealloc_bufsz plus the increment is less than or equal to UINT_MAX).
π¦ SEE ALSO π¦
Kernel file ./include/linux/comedi/comedidev.h struct:
comedi_subdevice(9)struct comedi_buf_page(9)struct comedi_buf_map(9)enum comedi_cb(9)struct comedi_driver(9)struct comedi_device(9)struct comedi_lrange(9)comedi_range_is_bipolar(9)comedi_range_is_unipolar(9)comedi_range_is_external(9)comedi_chan_range_is_bipolar(9)comedi_chan_range_is_unipolar(9)comedi_chan_range_is_external(9)comedi_offset_munge(9)comedi_bytes_per_sample(9)comedi_sample_shift(9)comedi_bytes_to_samples(9)comedi_samples_to_bytes(9)comedi_check_trigger_src(9)comedi_check_trigger_is_unique(9)comedi_check_trigger_arg_is(9)comedi_check_trigger_arg_min(9)comedi_check_trigger_arg_max(9)comedi_buf_n_bytes_ready(9)__comedi_request_region(9)comedi_request_region(9)module_comedi_driver(9)
and that's the whole page bestie!! π this lil icon lives at ./include/linux/comedi, it's dated October 2026, and yes it's still called struct comedi_async(9) β go read it, slay, bye πβ¨
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms