Skip to content

Commit

Permalink
camerad: use open_v4l_by_name_and_index on c2 also (commaai#23794)
Browse files Browse the repository at this point in the history
* use open_v4l_by_name_and_index on c2 also

* remove open_v4l_by_name_and_index from qcom2
  • Loading branch information
geohot authored and budney committed Mar 27, 2022
1 parent 0f06ca3 commit 9e5bbd4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
13 changes: 13 additions & 0 deletions selfdrive/camerad/cameras/camera_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,16 @@ void camerad_thread() {

CL_CHECK(clReleaseContext(context));
}

int open_v4l_by_name_and_index(const char name[], int index, int flags) {
for (int v4l_index = 0; /**/; ++v4l_index) {
std::string v4l_name = util::read_file(util::string_format("/sys/class/video4linux/v4l-subdev%d/name", v4l_index));
if (v4l_name.empty()) return -1;
if (v4l_name.find(name) == 0) {
if (index == 0) {
return HANDLE_EINTR(open(util::string_format("/dev/v4l-subdev%d", v4l_index).c_str(), flags));
}
index--;
}
}
}
2 changes: 2 additions & 0 deletions selfdrive/camerad/cameras/camera_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,5 @@ void cameras_run(MultiCameraState *s);
void cameras_close(MultiCameraState *s);
void camera_autoexposure(CameraState *s, float grey_frac);
void camerad_thread();

int open_v4l_by_name_and_index(const char name[], int index = 0, int flags = O_RDWR | O_NONBLOCK);
31 changes: 11 additions & 20 deletions selfdrive/camerad/cameras/camera_qcom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ static void sensors_init(MultiCameraState *s) {
.output_format = MSM_SENSOR_BAYER,
}};

unique_fd sensorinit_fd = HANDLE_EINTR(open("/dev/v4l-subdev11", O_RDWR | O_NONBLOCK));
unique_fd sensorinit_fd = open_v4l_by_name_and_index("msm_sensor_init");
assert(sensorinit_fd >= 0);
for (auto &info : slave_infos) {
info.power_setting_array.power_setting = &info.power_setting_array.power_setting_a[0];
Expand All @@ -417,31 +417,22 @@ static void camera_open(CameraState *s, bool is_road_cam) {
struct msm_actuator_cfg_data actuator_cfg_data = {};

// open devices
const char *sensor_dev;
s->csid_fd = open_v4l_by_name_and_index("msm_csid", is_road_cam ? 0 : 2);
assert(s->csid_fd >= 0);
s->csiphy_fd = open_v4l_by_name_and_index("msm_csiphy", is_road_cam ? 0 : 2);
assert(s->csiphy_fd >= 0);
s->isp_fd = open_v4l_by_name_and_index("vfe", is_road_cam ? 0 : 1);
assert(s->isp_fd >= 0);

if (is_road_cam) {
s->csid_fd = HANDLE_EINTR(open("/dev/v4l-subdev3", O_RDWR | O_NONBLOCK));
assert(s->csid_fd >= 0);
s->csiphy_fd = HANDLE_EINTR(open("/dev/v4l-subdev0", O_RDWR | O_NONBLOCK));
assert(s->csiphy_fd >= 0);
sensor_dev = "/dev/v4l-subdev17";
s->isp_fd = HANDLE_EINTR(open("/dev/v4l-subdev13", O_RDWR | O_NONBLOCK));
assert(s->isp_fd >= 0);
s->actuator_fd = HANDLE_EINTR(open("/dev/v4l-subdev7", O_RDWR | O_NONBLOCK));
s->actuator_fd = open_v4l_by_name_and_index("msm_actuator");
assert(s->actuator_fd >= 0);
} else {
s->csid_fd = HANDLE_EINTR(open("/dev/v4l-subdev5", O_RDWR | O_NONBLOCK));
assert(s->csid_fd >= 0);
s->csiphy_fd = HANDLE_EINTR(open("/dev/v4l-subdev2", O_RDWR | O_NONBLOCK));
assert(s->csiphy_fd >= 0);
sensor_dev = "/dev/v4l-subdev18";
s->isp_fd = HANDLE_EINTR(open("/dev/v4l-subdev14", O_RDWR | O_NONBLOCK));
assert(s->isp_fd >= 0);
}

// wait for sensor device
// on first startup, these devices aren't present yet
for (int i = 0; i < 10; i++) {
s->sensor_fd = HANDLE_EINTR(open(sensor_dev, O_RDWR | O_NONBLOCK));
s->sensor_fd = open_v4l_by_name_and_index(is_road_cam ? "imx298" : "ov8865_sunny");
if (s->sensor_fd >= 0) break;
LOGW("waiting for sensors...");
util::sleep_for(1000); // sleep one second
Expand Down Expand Up @@ -908,7 +899,7 @@ void cameras_open(MultiCameraState *s) {
s->v4l_fd = HANDLE_EINTR(open("/dev/video0", O_RDWR | O_NONBLOCK));
assert(s->v4l_fd >= 0);

s->ispif_fd = HANDLE_EINTR(open("/dev/v4l-subdev15", O_RDWR | O_NONBLOCK));
s->ispif_fd = open_v4l_by_name_and_index("msm_ispif");
assert(s->ispif_fd >= 0);

// ISPIF: stop
Expand Down
13 changes: 0 additions & 13 deletions selfdrive/camerad/cameras/camera_qcom2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -578,19 +578,6 @@ static void camera_init(MultiCameraState *multi_cam_state, VisionIpcServer * v,
s->buf.init(device_id, ctx, s, v, FRAME_BUF_COUNT, rgb_type, yuv_type);
}

int open_v4l_by_name_and_index(const char name[], int index, int flags = O_RDWR | O_NONBLOCK) {
for (int v4l_index = 0; /**/; ++v4l_index) {
std::string v4l_name = util::read_file(util::string_format("/sys/class/video4linux/v4l-subdev%d/name", v4l_index));
if (v4l_name.empty()) return -1;
if (v4l_name.find(name) == 0) {
if (index == 0) {
return open(util::string_format("/dev/v4l-subdev%d", v4l_index).c_str(), flags);
}
index--;
}
}
}

static void camera_open(CameraState *s) {
s->sensor_fd = open_v4l_by_name_and_index("cam-sensor-driver", s->camera_num);
assert(s->sensor_fd >= 0);
Expand Down

0 comments on commit 9e5bbd4

Please sign in to comment.