Skip to content

Commit 08e5c36

Browse files
tombaHans Verkuil
authored andcommitted
media: v4l: subdev: Move out subdev state lock macros outside CONFIG_MEDIA_CONTROLLER
The subdev state locking macros and macros to get the active state are currently behind CONFIG_MEDIA_CONTROLLER. This makes sense, as there can be no subdev state without MC. However, we have code paths common to MC and non-MC cases which call subdev operations that have subdev state as a parameter. In the non-MC case the state parameter would always be NULL. Thus it makes sense to allow, e.g.: v4l2_subdev_call_state_active(sd, pad, get_fmt, fmt) which for non-MC case would call the subdev passing NULL as the state. This currently fails: https://lore.kernel.org/oe-kbuild-all/202312061101.PLrz5NnJ-lkp@intel.com/ Fix the issue by moving the related macros to be outside CONFIG_MEDIA_CONTROLLER. The v4l2_subdev_lock_state() and v4l2_subdev_unlock_state() macros will crash if given NULL as the state, but the other macros behave correctly even when there's no active state, and they will only call the lock/unlock macros if there is a state. An alternative fix would be to make another version of v4l2_subdev_call_state_try() with ifdefs, which would not use any state macros and would always pass NULL as the state. But having two version of a macro/function is always more confusing than having just one, so I went this way. So, this fixes the v4l2_subdev_call_state_active() macro. But we also have v4l2_subdev_call_state_try(). It would be possible to fix that macro by additionally creating "no-op" variants of the state alloc and free functions. However, v4l2_subdev_call_state_try() is only used by a single driver (stm32-dcmi), which selects MC, and the macro is supposed to be removed as soon as the users have been converted away from the macro. Thus I have not touched the state alloc/free functions, and I think it makes sense to keep alloc/free functions available only if there's actually something that can be allocated or freed. Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Hans Verkuil <hverkuil-cisco@xs4all.nl> Link: https://lore.kernel.org/r/20231208-v4l2-state-mc-fix-v1-1-a0c8162557c6@ideasonboard.com Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
1 parent e4af84f commit 08e5c36

1 file changed

Lines changed: 83 additions & 83 deletions

File tree

include/media/v4l2-subdev.h

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,89 +1311,6 @@ int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
13111311
*/
13121312
void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
13131313

1314-
/**
1315-
* v4l2_subdev_lock_state() - Locks the subdev state
1316-
* @state: The subdevice state
1317-
*
1318-
* Locks the given subdev state.
1319-
*
1320-
* The state must be unlocked with v4l2_subdev_unlock_state() after use.
1321-
*/
1322-
static inline void v4l2_subdev_lock_state(struct v4l2_subdev_state *state)
1323-
{
1324-
mutex_lock(state->lock);
1325-
}
1326-
1327-
/**
1328-
* v4l2_subdev_unlock_state() - Unlocks the subdev state
1329-
* @state: The subdevice state
1330-
*
1331-
* Unlocks the given subdev state.
1332-
*/
1333-
static inline void v4l2_subdev_unlock_state(struct v4l2_subdev_state *state)
1334-
{
1335-
mutex_unlock(state->lock);
1336-
}
1337-
1338-
/**
1339-
* v4l2_subdev_get_unlocked_active_state() - Checks that the active subdev state
1340-
* is unlocked and returns it
1341-
* @sd: The subdevice
1342-
*
1343-
* Returns the active state for the subdevice, or NULL if the subdev does not
1344-
* support active state. If the state is not NULL, calls
1345-
* lockdep_assert_not_held() to issue a warning if the state is locked.
1346-
*
1347-
* This function is to be used e.g. when getting the active state for the sole
1348-
* purpose of passing it forward, without accessing the state fields.
1349-
*/
1350-
static inline struct v4l2_subdev_state *
1351-
v4l2_subdev_get_unlocked_active_state(struct v4l2_subdev *sd)
1352-
{
1353-
if (sd->active_state)
1354-
lockdep_assert_not_held(sd->active_state->lock);
1355-
return sd->active_state;
1356-
}
1357-
1358-
/**
1359-
* v4l2_subdev_get_locked_active_state() - Checks that the active subdev state
1360-
* is locked and returns it
1361-
*
1362-
* @sd: The subdevice
1363-
*
1364-
* Returns the active state for the subdevice, or NULL if the subdev does not
1365-
* support active state. If the state is not NULL, calls lockdep_assert_held()
1366-
* to issue a warning if the state is not locked.
1367-
*
1368-
* This function is to be used when the caller knows that the active state is
1369-
* already locked.
1370-
*/
1371-
static inline struct v4l2_subdev_state *
1372-
v4l2_subdev_get_locked_active_state(struct v4l2_subdev *sd)
1373-
{
1374-
if (sd->active_state)
1375-
lockdep_assert_held(sd->active_state->lock);
1376-
return sd->active_state;
1377-
}
1378-
1379-
/**
1380-
* v4l2_subdev_lock_and_get_active_state() - Locks and returns the active subdev
1381-
* state for the subdevice
1382-
* @sd: The subdevice
1383-
*
1384-
* Returns the locked active state for the subdevice, or NULL if the subdev
1385-
* does not support active state.
1386-
*
1387-
* The state must be unlocked with v4l2_subdev_unlock_state() after use.
1388-
*/
1389-
static inline struct v4l2_subdev_state *
1390-
v4l2_subdev_lock_and_get_active_state(struct v4l2_subdev *sd)
1391-
{
1392-
if (sd->active_state)
1393-
v4l2_subdev_lock_state(sd->active_state);
1394-
return sd->active_state;
1395-
}
1396-
13971314
/*
13981315
* A macro to generate the macro or function name for sub-devices state access
13991316
* wrapper macros below.
@@ -1738,6 +1655,89 @@ int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable);
17381655

17391656
#endif /* CONFIG_MEDIA_CONTROLLER */
17401657

1658+
/**
1659+
* v4l2_subdev_lock_state() - Locks the subdev state
1660+
* @state: The subdevice state
1661+
*
1662+
* Locks the given subdev state.
1663+
*
1664+
* The state must be unlocked with v4l2_subdev_unlock_state() after use.
1665+
*/
1666+
static inline void v4l2_subdev_lock_state(struct v4l2_subdev_state *state)
1667+
{
1668+
mutex_lock(state->lock);
1669+
}
1670+
1671+
/**
1672+
* v4l2_subdev_unlock_state() - Unlocks the subdev state
1673+
* @state: The subdevice state
1674+
*
1675+
* Unlocks the given subdev state.
1676+
*/
1677+
static inline void v4l2_subdev_unlock_state(struct v4l2_subdev_state *state)
1678+
{
1679+
mutex_unlock(state->lock);
1680+
}
1681+
1682+
/**
1683+
* v4l2_subdev_get_unlocked_active_state() - Checks that the active subdev state
1684+
* is unlocked and returns it
1685+
* @sd: The subdevice
1686+
*
1687+
* Returns the active state for the subdevice, or NULL if the subdev does not
1688+
* support active state. If the state is not NULL, calls
1689+
* lockdep_assert_not_held() to issue a warning if the state is locked.
1690+
*
1691+
* This function is to be used e.g. when getting the active state for the sole
1692+
* purpose of passing it forward, without accessing the state fields.
1693+
*/
1694+
static inline struct v4l2_subdev_state *
1695+
v4l2_subdev_get_unlocked_active_state(struct v4l2_subdev *sd)
1696+
{
1697+
if (sd->active_state)
1698+
lockdep_assert_not_held(sd->active_state->lock);
1699+
return sd->active_state;
1700+
}
1701+
1702+
/**
1703+
* v4l2_subdev_get_locked_active_state() - Checks that the active subdev state
1704+
* is locked and returns it
1705+
*
1706+
* @sd: The subdevice
1707+
*
1708+
* Returns the active state for the subdevice, or NULL if the subdev does not
1709+
* support active state. If the state is not NULL, calls lockdep_assert_held()
1710+
* to issue a warning if the state is not locked.
1711+
*
1712+
* This function is to be used when the caller knows that the active state is
1713+
* already locked.
1714+
*/
1715+
static inline struct v4l2_subdev_state *
1716+
v4l2_subdev_get_locked_active_state(struct v4l2_subdev *sd)
1717+
{
1718+
if (sd->active_state)
1719+
lockdep_assert_held(sd->active_state->lock);
1720+
return sd->active_state;
1721+
}
1722+
1723+
/**
1724+
* v4l2_subdev_lock_and_get_active_state() - Locks and returns the active subdev
1725+
* state for the subdevice
1726+
* @sd: The subdevice
1727+
*
1728+
* Returns the locked active state for the subdevice, or NULL if the subdev
1729+
* does not support active state.
1730+
*
1731+
* The state must be unlocked with v4l2_subdev_unlock_state() after use.
1732+
*/
1733+
static inline struct v4l2_subdev_state *
1734+
v4l2_subdev_lock_and_get_active_state(struct v4l2_subdev *sd)
1735+
{
1736+
if (sd->active_state)
1737+
v4l2_subdev_lock_state(sd->active_state);
1738+
return sd->active_state;
1739+
}
1740+
17411741
/**
17421742
* v4l2_subdev_init - initializes the sub-device struct
17431743
*

0 commit comments

Comments
 (0)