Skip to content

sensor: sc2315e for hi3516ev200 from source, without the write past its state - #306

Merged
openipc-ai merged 3 commits into
mainfrom
sensor/sc2315e-ev200
Nov 17, 2026
Merged

openipc-ai merged 3 commits into
mainfrom
sensor/sc2315e-ev200

Conversation

@openipc-ai

@openipc-ai openipc-ai commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

OpenIPC/firmware ships libsns_sc2315e.so for hi3516ev200 as a prebuilt binary (general/package/hisilicon-osdrv-hi3516ev200/files/sensor/). Its source -- OpenIPC's own "SC2315E Universal driver" from 2021-22 -- never reached this repository, and the binary has a heap overrun.

Commits

  1. Import. The driver at its last commit, ported to the T5 headers the other EV200 drivers build with (comm_sns.h/sns_ctrl.h/gk_api_*.h through hicompat.h, HI_* types and constants as GK_*). Logic unchanged, including the exported sensor_driver() entry point the shipped binary has, so the library is a drop-in for the blob. README sensor table gains the row.
  2. Fix. cmos_fps_set() stored the frame length to pstSnsState[1].astRegsInfo[0].astI2cData[19].{u32RegAddr,bUpdate} -- lines carried over from a disassembly of the vendor's build, whose ISP_SNS_STATE_S is 0x200 bytes longer, so its u32FLStd and au32FL[0] sit at those offsets (0xa58, 0xa50). Here a pipe has one 2156-byte state, so every frame-rate set wrote two words ~490 bytes past the allocation. musl's allocator catches the damage when the context is freed on a pipeline restart (SIGILL in free(), reported from the field on hi3516ev200 + SC2315E cameras); whether it does depends only on heap layout, so it comes and goes with unrelated builds. The stores now go to u32FLStd and au32FL[0], as the stock driver's version of the function does (kept under #if 0 beside it).

Testing

Built for hi3516ev200 (SDK_CODE 0x3516E200), gk7205v200 and gk7205v500 (XMedia compat flags): no errors or warnings.

On a hi3516ev200 + SC2315E camera, with an LD_PRELOAD shim that places the 2156-byte context flush against a PROT_NONE page:

Driver Result
shipped binary SIGSEGV at start: pc = libsns_sc2315e.so+0x1050 (str r1, [r3, #2648], r3 = the context), the write landing in the guard page
this PR starts; eight pipeline rebuilds (frame rate changed between reloads), the context freed and reallocated each time, no fault

Sensor timing is the same as with the shipped binary (40 ms frame interval; encoder at the configured 15/20/25 fps), and the picture is normal.

Wiring it into the firmware (building it here and dropping the blob) follows as an OpenIPC/firmware PR once this is in.

OpenIPC/firmware ships libsns_sc2315e.so for hi3516ev200 as a prebuilt
binary; its source is OpenIPC's own "SC2315E Universal driver" (2021-22),
which never reached this repository. This is that source, at its last
commit (fa7c697, "Use new driver versioning model"), ported to the T5
headers the other EV200 drivers build with: comm_sns.h / sns_ctrl.h /
gk_api_*.h through hicompat.h, HI_ types and constants as GK_,
HI_DBG_ERR as MODULE_DBG_ERR. The logic is unchanged, including the
exported sensor_driver() entry point the shipped binary has, so the
library is a drop-in for the blob.
cmos_fps_set() wrote the frame length it computed to
pstSnsState[1].astRegsInfo[0].astI2cData[19].u32RegAddr and .bUpdate.
Those two lines were carried over from a disassembly of the vendor's
build, whose ISP_SNS_STATE_S is 0x200 bytes longer: at its offsets
0xa58 and 0xa50 sit u32FLStd and au32FL[0]. Here each pipe has one
2156-byte ISP_SNS_STATE_S, so pstSnsState[1] is past the allocation and
every frame-rate set wrote two words ~490 bytes beyond it, into whatever
the heap held next. musl's allocator found the damage when the context
was freed, as SIGILL in free() on a pipeline restart.

They now store to u32FLStd and au32FL[0], as the stock driver's version
of the function does (kept under #if 0 below it).

Tested on a hi3516ev200 + SC2315E camera, with an LD_PRELOAD shim that
puts the 2156-byte context flush against a PROT_NONE page:
- the shipped libsns_sc2315e.so faults on start, in cmos_fps_set
  (str r1, [r3, #2648], r3 = the context);
- this build starts, survives eight pipeline rebuilds (frame rate
  changed between reloads; the context freed and allocated each time),
  and gives the same sensor timing as the shipped one (40 ms frame
  interval, the encoder at the configured 15/20/25 fps) and a normal
  picture.
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Add source-built SC2315E driver and fix frame-rate heap overrun

✨ Enhancement 🐞 Bug fix 📝 Documentation 🕐 40+ Minutes

Grey Divider

AI Description

• Add the source-built SC2315E driver for hi3516ev200 while retaining its existing streamer entry
 point.
• Store frame length in the allocated pipe state to prevent heap corruption during frame-rate
 changes.
• Document sensor support; firmware integration to replace the shipped binary remains a separate
 change.
Diagram

sequenceDiagram
    actor Streamer
    participant Driver as SC2315E driver
    participant State as Pipe state
    participant ISP as ISP API
    participant AE as AE engine
    participant Sensor as SC2315E sensor
    Streamer->>Driver: load sensor_driver
    Streamer->>Driver: register callbacks
    Driver->>State: allocate context
    Driver->>ISP: register callbacks
    Driver->>AE: register AE callbacks
    AE->>Driver: set frame rate
    Driver->>State: save frame length
    Driver->>ISP: provide VMAX registers
    ISP->>Sensor: write over I2C
Loading
High-Level Assessment

Importing the existing driver preserves its sensor behavior and streamer interface; writing frame length to u32FLStd and au32FL[0] fixes the overrun at its source. Padding the allocation would only conceal the invalid access, while replacing the driver logic would introduce unnecessary timing risk.

Files changed (6) +4304 / -0

Enhancement (3) +3121 / -0
sc2315e_cmos_ex.hProvide SC2315E ISP and AWB tuning data +2657/-0

Provide SC2315E ISP and AWB tuning data

• Imports the calibration and tuning tables used by the driver for ISP processing and white balance.

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos_ex.h

sc2315e_sensor_ctl.cAdd SC2315E I2C control and initialization +428/-0

Add SC2315E I2C control and initialization

• Implements per-pipe I2C access, sensor initialization and exit, and the 1080p register sequence. Uses the compatible ISP API for quick-start handling.

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c

universal.hDefine the streamer driver parameters and logging interface +36/-0

Define the streamer driver parameters and logging interface

• Defines the parameters accepted by sensor_driver and the logging callback used by the imported driver.

libraries/sensor/hi3516ev200/smart_sc2315e/universal.h

Bug fix (1) +1165 / -0
sc2315e_cmos.cImport callbacks and correct frame-length state writes +1165/-0

Import callbacks and correct frame-length state writes

• Adds ISP, AE, and AWB callbacks plus the streamer-facing sensor_driver entry point. Frame-rate handling writes u32FLStd and au32FL[0] in the current pipe's allocated state instead of writing beyond it.

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c

Documentation (1) +1 / -0
README.mdList SC2315E sensor support +1/-0

List SC2315E sensor support

• Adds SC2315E to the sensor support table for hi3516ev200.

README.md

Other (1) +17 / -0
MakefileBuild SC2315E shared and static libraries +17/-0

Build SC2315E shared and static libraries

• Adds the sensor build target using the repository's EV200 include path and SDK_CODE setting. Produces libsns_sc2315e as shared and static libraries.

libraries/sensor/hi3516ev200/smart_sc2315e/Makefile

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (6) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Action required

1. Calibration exit halves the camera rate ✓ Resolved
Description
cmos_set_pixel_detect() writes u32FLStd directly to the sensor’s VMAX registers, although
cmos_fps_set() stores twice the VMAX value there. After a frame-rate change, leaving pixel
calibration programs twice the intended VMAX—for example, 2250 instead of 1125 at 30 fps.

2. Sensor startup succeeds without an I²C bus 🐞 Bug ☼ Reliability
Description
sc2315e_init() discards the result of sc2315e_i2c_init() and unconditionally marks the sensor
initialized. When opening the bus or selecting the slave fails, the initialization writes are
silently treated as successful despite no registers being programmed.
Code

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c[R223-224]

+  sc2315e_GetQuickStartFlag(ViPipe, &quickStartFlag);
+  sc2315e_i2c_init(ViPipe);
Evidence
I²C setup has failure returns, while writes with a negative descriptor return success and
initialization sets bInit regardless.

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c[71-87]
libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c[168-171]
libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c[220-236]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A failed I²C setup still leads to a sensor state marked initialized.
## Fix Focus Areas
- libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c[220-236]
- libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c[168-171]
## Recommended Fix
Stop initialization when I²C setup fails, keep `bInit` false, and do not report writes to an unopened descriptor as successful.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

3. Camera register reads return wrong data 🐞 Bug ≡ Correctness
Description
The non-HIBVT branch of sc2315e_read_register() places address bytes in the opposite order from
writes, then calls read() without sending the requested register address. On the Linux I²C path
used by this driver, calls through the exposed register-read callback therefore cannot reliably read
the requested register.
Code

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c[R148-151]

+  if (u32RegWidth == 2) {
+    aRecvbuf[0] = addr & 0xff;
+    aRecvbuf[1] = (addr >> 8) & 0xff;
+  } else {
Evidence
The sensor uses two-byte addresses; writes send the high byte first, whereas the read branch only
prepares a reversed address in a buffer overwritten by read(). The read function is installed in
the sensor object.

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c[33-35]
libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c[139-165]
libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c[190-200]
libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[1138-1147]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The Linux register-read path never transmits its requested address to the sensor.
## Fix Focus Areas
- libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_sensor_ctl.c[139-165]
## Recommended Fix
Send the two-byte register address high byte first, then read the data using an appropriate combined I²C transaction; check the transaction length and return the received data byte.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


4. Unsupported frame rates are accepted 🐞 Bug ≡ Correctness
Description
cmos_set_image_mode() has an empty branch for rates above 30 fps and returns success without
rejecting rates below the supported minimum either. Such requests reach cmos_fps_set(), which only
changes timing for 5–30 fps, so the previously configured sensor timing remains active.
Code

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[R939-940]

+  } else {
+  }
Evidence
Image-mode selection returns success after an empty greater-than-30 branch; its registered FPS
callback updates timing only within 5–30 fps.

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[234-254]
libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[920-949]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Image-mode selection reports success for rates its FPS callback does not support.
## Fix Focus Areas
- libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[917-949]
## Recommended Fix
Validate the requested rate against the supported 5–30 fps range in image-mode selection and return failure for unsupported requests.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


5. Digital gain gets the wrong limit 🐞 Bug ≡ Correctness
Description
cmos_get_ae_default() copies u32MaxDgain into u32MaxDgainTarget before assigning the maximum
its supported value of 0x880. The AE defaults consequently receive the caller’s prior
value—typically zero—as their digital-gain target limit.
Code

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[R173-176]

+  pstAeSnsDft->u32MinDgain = 1024;
+  pstAeSnsDft->u32MaxDgainTarget = pstAeSnsDft->u32MaxDgain;
+
+  pstAeSnsDft->u32MaxDgain = 0x880;
Evidence
The assignment order directly copies the old maximum; the active AE function table registers
digital-gain callbacks.

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[167-177]
libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[497-510]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The digital-gain target limit is copied before its source value is initialized.
## Fix Focus Areas
- libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[170-177]
## Recommended Fix
Assign `u32MaxDgain` before setting `u32MaxDgainTarget`, so both fields advertise the same supported maximum.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


View review recommended (3)
6. Initial exposure uses half the frame ✓ Resolved
Description
sensor_global_init() initializes u32FLStd and au32FL to the 1125-line sensor VMAX, while
cmos_fps_set() defines software full lines as twice VMAX. Before the first FPS update,
cmos_get_ae_default() derives its maximum integration time from 1125 rather than the 2250 full
lines subsequently used at the same 30 fps timing.

7. Calibration exposure stays too short 🐞 Bug ≡ Correctness
Description
cmos_set_pixel_detect() calculates its calibration exposure from the prior u32FLStd rather than
from the 5 fps frame length it programs. When calibration is enabled from normal timing, the sensor
gets a 6750-line VMAX but an exposure limit derived from the shorter preceding frame.
Code

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[R753-754]

+  u32FullLines_5Fps = (SC2315E_VMAX_1080P30_LINEAR * 30) / 5;
+  u32MaxIntTime_5Fps = pstSnsState->u32FLStd - 8;
Evidence
The callback computes a new 5 fps VMAX but derives the exposure separately from existing state. The
sibling sensor derives calibration exposure from its calibration frame length.

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[746-767]
libraries/sensor/hi3516ev200/smart_sc223a/sc223a_cmos.c[871-900]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Pixel calibration pairs its 5 fps VMAX with an exposure calculated from normal timing.
## Fix Focus Areas
- libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[746-767]
## Recommended Fix
Calculate the calibration integration limit from the 5 fps VMAX using the driver's full-line representation, then program all exposure bytes needed for that value.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


8. Failed registration leaves callbacks behind 🐞 Bug ☼ Reliability
Description
sensor_register_callback() returns directly when ISP, AE, or AWB registration fails, without
releasing the context allocated first or undoing earlier successful registrations. An AE or AWB
registration failure therefore leaves a partially registered sensor, and a later attempt cannot rely
on a clean starting state.
Code

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[R1063-1066]

+  if (s32Ret != GK_SUCCESS) {
+    ISP_TRACE(MODULE_DBG_ERR,
+              "sensor register callback function to ae lib failed!\n");
+    return s32Ret;
Evidence
Context allocation precedes three sequential registrations, each with a direct failure return;
context cleanup is reached only through successful unregistration.

libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[1002-1028]
libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[1030-1079]
libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[1082-1111]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Registration failures retain allocated state and any callbacks registered earlier in the sequence.
## Fix Focus Areas
- libraries/sensor/hi3516ev200/smart_sc2315e/sc2315e_cmos.c[1030-1079]
## Recommended Fix
On each failure, unregister only the callbacks that succeeded earlier, release the sensor context, and return the registration error.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Tip of the day
💡 Did you know, you can enable the Remediation agent and Qodo fixes findings in a dedicated fix PR

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

With the previous commit u32FLStd and au32FL[0] are live: cmos_fps_set()
keeps them in full lines, twice VMAX, as the stock template does. Two
places still took them for VMAX itself, harmless while the fps path was
writing elsewhere:

- sensor_global_init() started them at VMAX (1125), so the AE defaults
  read before the first frame-rate set had half the frame; the stock
  template starts them at VMAX * 2.
- leaving pixel-calibration mode wrote u32FLStd straight to the VMAX
  registers, which after a frame-rate set would be twice the frame.
  It now writes u32FLStd >> 1.

Re-tested on the hi3516ev200 + SC2315E camera under the guard-page shim:
eight pipeline rebuilds at 25/20/15 fps, no fault, 40 ms sensor frame
interval, encoder at the configured rate, normal exposure and picture.
@openipc-ai

Copy link
Copy Markdown
Contributor Author

Qodo review, finding by finding:

  1. Calibration exit halves the rate -- fixed in db44d78. This one the fix itself exposed: before it, u32FLStd stayed at its initial VMAX, so the write was right by accident. It now writes u32FLStd >> 1.
  2. Initial frame length is half -- fixed in db44d78: sensor_global_init() starts at VMAX * 2, the unit cmos_fps_set() uses, as the stock template does.
  3. Calibration exposure from the previous frame length -- left as is. That is how the stock template computes it too, and pixel calibration is a separate tuning mode; changing its exposure without a calibration run to check it against would be a guess.
    2, 3, 4, 5, 8 (I2C init result ignored, register read path, unsupported rates accepted, digital-gain target order, registration not unwound) -- left as is in this PR. They describe the driver exactly as the binary in the field behaves, and Fix build when isp excluded #5 in particular would change the AE's digital-gain limit, i.e. the picture in low light. This PR's purpose is to put the shipped driver's source here and remove the heap overrun with no other change in behaviour (the camera test shows the same sensor timing and picture as the binary). They are worth separate changes, each checked on the camera.

Re-tested db44d78 on the hi3516ev200 + SC2315E camera under the guard-page shim: eight pipeline rebuilds at 25/20/15 fps, no fault, 40 ms sensor frame interval, encoder at the configured rate, normal exposure and picture.

@openipc-ai
openipc-ai merged commit 443c5d5 into main Nov 17, 2026
37 checks passed
@openipc-ai
openipc-ai deleted the sensor/sc2315e-ev200 branch November 17, 2026 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant