imeghar2408-max opened a new pull request, #20972:
URL: https://github.com/apache/nuttx/pull/20972
Increment the open reference count on every successful open in the DAC,
comparator, and opamp drivers while keeping hardware setup limited to the first
open.
Update the count only after setup succeeds, so failed initialization does
not leave a stale reference. This prevents closing one file descriptor from
shutting down hardware while other descriptors remain open.
Tested:
- QEMU rv-virt KSMP64 build with COMP, DAC, and OPAMP enabled.
- Host regression tests covering both close orders and setup-failure retries.
- checkpatch and git diff --check.
Assisted-by: ChatGPT:GPT-6
analog: Fix open reference counting.
## Summary
- Fix reference counting in the DAC, comparator, and operational amplifier
drivers.
- Previously, the open reference count was updated only during the first
successful open. Subsequent opens could leave the count incorrect, allowing a
close to shut down the hardware while other file descriptors remained open.
- Update the open count after each successful open while keeping hardware
setup limited to the first open.
- Ensure failed initialization does not update the open count, allowing a
later retry to succeed.
## Impact
- **New feature:** NO. Fixes existing driver lifecycle behavior.
- **User impact:** NO API changes.
- **Build impact:** NO build-system changes.
- **Hardware impact:** YES. Affects open/close reference counting in DAC,
comparator, and OPAMP drivers.
- **Documentation impact:** NO.
- **Security impact:** No known security impact.
- **Compatibility impact:** No API or ABI changes intended.
## Testing
### 1. QEMU build
**Build host:** Linux. Compiler version was not recorded.
**Target:** QEMU RISC-V `rv-virt:ksmp64`.
Enabled configurations:
```text
CONFIG_COMP=y
CONFIG_DAC=y
CONFIG_OPAMP=y
```
**Command:**
```bash
make -j4 2>&1 | tee /tmp/nuttx-analog-build.log
build_rc=${PIPESTATUS[0]}
echo "BUILD_EXIT_CODE=$build_rc"
```
**Captured build output (relevant lines):**
```text
Create version.h
Make.dep:2: warning: NUL character seen; rest of line ignored
...
LD: nuttx
...
CP: nuttx.hex
BUILD_EXIT_CODE=0
```
The build completed successfully. The non-fatal `Make.dep` warning was
observed in the build output.
Verified the three driver objects were generated:
```text
drivers/comp.o
drivers/dac.o
drivers/opamp.o
```
Also confirmed that `dac.o`, `comp.o`, and `opamp.o` were present in
`drivers/libdrivers.a`.
### 2. Host regression tests
The actual DAC, comparator, and OPAMP driver source files were exercised
using a temporary host-side regression harness with mocked mutex and
critical-section functions.
**Test cases:**
- Open the same device twice and close the first descriptor.
- Open the same device twice and close the second descriptor first.
- Verify hardware shutdown occurs only after the final close.
- Simulate setup failure, retry initialization, and verify the reference
count remains correct.
All cases were exercised for DAC, COMP, and OPAMP.
**Captured output:**
```text
PASS: all analog open-count regression tests
REGRESSION_EXIT_CODE=0
```
### 3. Coding-style and whitespace checks
**Commands:**
```bash
./tools/checkpatch.sh -f \
drivers/analog/dac.c \
drivers/analog/comp.c \
drivers/analog/opamp.c
git diff -- | ./tools/checkpatch.sh -
git diff --check
```
**Captured output:**
```text
✔️ All checks pass.
✔️ All checks pass.
```
`git diff --check` also completed without reporting whitespace errors.
### Testing logs before change
No before-change runtime log was captured. The defect was identified by
examining the open/close reference-counting paths.
### Testing logs after change
The QEMU build completed with exit code `0`, the host regression harness
passed all scenarios, and the coding-style checks passed.
**Real-hardware testing:** A physical-board build and runtime test have not
yet been performed. Hardware-testing evidence remains outstanding.
## PR verification Self-Check
- [x] This PR introduces only one functional change.
- [x] I have updated all required description fields above.
- [x] My PR adheres to the contribution guidelines.
- [x] My PR is still work in progress (not ready for review).
- [ ] My PR is ready for review and can be safely merged into a codebase.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]