Skip to content

Commit

Permalink
driver core: Constify API device_find_child() and adapt for various u…
Browse files Browse the repository at this point in the history
…sages

Constify the following API:
struct device *device_find_child(struct device *dev, void *data,
		int (*match)(struct device *dev, void *data));
To :
struct device *device_find_child(struct device *dev, const void *data,
                                 device_match_t match);
typedef int (*device_match_t)(struct device *dev, const void *data);
with the following reasons:

- Protect caller's match data @*data which is for comparison and lookup
  and the API does not actually need to modify @*data.

- Make the API's parameters (@match)() and @DaTa have the same type as
  all of other device finding APIs (bus|class|driver)_find_device().

- All kinds of existing device match functions can be directly taken
  as the API's argument, they were exported by driver core.

Constify the API and adapt for various existing usages.

BTW, various subsystem changes are squashed into this commit to meet
'git bisect' requirement, and this commit has the minimal and simplest
changes to complement squashing shortcoming, and that may bring extra
code improvement.

Reviewed-by: Alison Schofield <[email protected]>
Reviewed-by: Takashi Sakamoto <[email protected]>
Acked-by: Uwe Kleine-König <[email protected]> # for drivers/pwm
Signed-off-by: Zijun Hu <[email protected]>
Reviewed-by: Jonathan Cameron <[email protected]>
Reviewed-by: Mathieu Poirier <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
zijun-hu authored and gregkh committed Jan 3, 2025
1 parent e9451ab commit f1e8bf5
Show file tree
Hide file tree
Showing 28 changed files with 66 additions and 62 deletions.
6 changes: 3 additions & 3 deletions arch/sparc/kernel/vio.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,13 @@ struct vio_remove_node_data {
u64 node;
};

static int vio_md_node_match(struct device *dev, void *arg)
static int vio_md_node_match(struct device *dev, const void *arg)
{
struct vio_dev *vdev = to_vio_dev(dev);
struct vio_remove_node_data *node_data;
const struct vio_remove_node_data *node_data;
u64 node;

node_data = (struct vio_remove_node_data *)arg;
node_data = (const struct vio_remove_node_data *)arg;

node = vio_vdev_node(node_data->hp, vdev);

Expand Down
6 changes: 3 additions & 3 deletions drivers/base/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -4079,8 +4079,8 @@ EXPORT_SYMBOL_GPL(device_for_each_child_reverse_from);
*
* NOTE: you will need to drop the reference with put_device() after use.
*/
struct device *device_find_child(struct device *parent, void *data,
int (*match)(struct device *dev, void *data))
struct device *device_find_child(struct device *parent, const void *data,
device_match_t match)
{
struct klist_iter i;
struct device *child;
Expand Down Expand Up @@ -4125,7 +4125,7 @@ struct device *device_find_child_by_name(struct device *parent,
}
EXPORT_SYMBOL_GPL(device_find_child_by_name);

static int match_any(struct device *dev, void *unused)
static int match_any(struct device *dev, const void *unused)
{
return 1;
}
Expand Down
6 changes: 3 additions & 3 deletions drivers/block/sunvdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,12 +918,12 @@ struct vdc_check_port_data {
char *type;
};

static int vdc_device_probed(struct device *dev, void *arg)
static int vdc_device_probed(struct device *dev, const void *arg)
{
struct vio_dev *vdev = to_vio_dev(dev);
struct vdc_check_port_data *port_data;
const struct vdc_check_port_data *port_data;

port_data = (struct vdc_check_port_data *)arg;
port_data = (const struct vdc_check_port_data *)arg;

if ((vdev->dev_no == port_data->dev_no) &&
(!(strcmp((char *)&vdev->type, port_data->type))) &&
Expand Down
4 changes: 2 additions & 2 deletions drivers/bus/fsl-mc/dprc-driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ void dprc_remove_devices(struct fsl_mc_device *mc_bus_dev,
}
EXPORT_SYMBOL_GPL(dprc_remove_devices);

static int __fsl_mc_device_match(struct device *dev, void *data)
static int __fsl_mc_device_match(struct device *dev, const void *data)
{
struct fsl_mc_obj_desc *obj_desc = data;
const struct fsl_mc_obj_desc *obj_desc = data;
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);

return fsl_mc_device_match(mc_dev, obj_desc);
Expand Down
4 changes: 2 additions & 2 deletions drivers/cxl/core/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ static int devm_cxl_enable_mem(struct device *host, struct cxl_dev_state *cxlds)
}

/* require dvsec ranges to be covered by a locked platform window */
static int dvsec_range_allowed(struct device *dev, void *arg)
static int dvsec_range_allowed(struct device *dev, const void *arg)
{
struct range *dev_range = arg;
const struct range *dev_range = arg;
struct cxl_decoder *cxld;

if (!is_root_decoder(dev))
Expand Down
2 changes: 1 addition & 1 deletion drivers/cxl/core/pmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool is_cxl_nvdimm_bridge(struct device *dev)
}
EXPORT_SYMBOL_NS_GPL(is_cxl_nvdimm_bridge, "CXL");

static int match_nvdimm_bridge(struct device *dev, void *data)
static int match_nvdimm_bridge(struct device *dev, const void *data)
{
return is_cxl_nvdimm_bridge(dev);
}
Expand Down
21 changes: 12 additions & 9 deletions drivers/cxl/core/region.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ static int check_commit_order(struct device *dev, const void *data)
return 0;
}

static int match_free_decoder(struct device *dev, void *data)
static int match_free_decoder(struct device *dev, const void *data)
{
struct cxl_port *port = to_cxl_port(dev->parent);
struct cxl_decoder *cxld;
Expand Down Expand Up @@ -824,9 +824,9 @@ static int match_free_decoder(struct device *dev, void *data)
return 1;
}

static int match_auto_decoder(struct device *dev, void *data)
static int match_auto_decoder(struct device *dev, const void *data)
{
struct cxl_region_params *p = data;
const struct cxl_region_params *p = data;
struct cxl_decoder *cxld;
struct range *r;

Expand Down Expand Up @@ -1722,10 +1722,12 @@ static struct cxl_port *next_port(struct cxl_port *port)
return port->parent_dport->port;
}

static int match_switch_decoder_by_range(struct device *dev, void *data)
static int match_switch_decoder_by_range(struct device *dev,
const void *data)
{
struct cxl_switch_decoder *cxlsd;
struct range *r1, *r2 = data;
const struct range *r1, *r2 = data;


if (!is_switch_decoder(dev))
return 0;
Expand Down Expand Up @@ -3176,9 +3178,10 @@ static int devm_cxl_add_dax_region(struct cxl_region *cxlr)
return rc;
}

static int match_root_decoder_by_range(struct device *dev, void *data)
static int match_root_decoder_by_range(struct device *dev,
const void *data)
{
struct range *r1, *r2 = data;
const struct range *r1, *r2 = data;
struct cxl_root_decoder *cxlrd;

if (!is_root_decoder(dev))
Expand All @@ -3189,11 +3192,11 @@ static int match_root_decoder_by_range(struct device *dev, void *data)
return range_contains(r1, r2);
}

static int match_region_by_range(struct device *dev, void *data)
static int match_region_by_range(struct device *dev, const void *data)
{
struct cxl_region_params *p;
struct cxl_region *cxlr;
struct range *r = data;
const struct range *r = data;
int rc = 0;

if (!is_cxl_region(dev))
Expand Down
4 changes: 2 additions & 2 deletions drivers/firewire/core-device.c
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ int fw_device_set_broadcast_channel(struct device *dev, void *gen)
return 0;
}

static int compare_configuration_rom(struct device *dev, void *data)
static int compare_configuration_rom(struct device *dev, const void *data)
{
const struct fw_device *old = fw_device(dev);
const u32 *config_rom = data;
Expand Down Expand Up @@ -1039,7 +1039,7 @@ static void fw_device_init(struct work_struct *work)
//
// serialize config_rom access.
scoped_guard(rwsem_read, &fw_device_rwsem) {
found = device_find_child(card->device, (void *)device->config_rom,
found = device_find_child(card->device, device->config_rom,
compare_configuration_rom);
}
if (found) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/firmware/arm_scmi/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ static int scmi_dev_match(struct device *dev, const struct device_driver *drv)
return 0;
}

static int scmi_match_by_id_table(struct device *dev, void *data)
static int scmi_match_by_id_table(struct device *dev, const void *data)
{
struct scmi_device *sdev = to_scmi_dev(dev);
struct scmi_device_id *id_table = data;
const struct scmi_device_id *id_table = data;

return sdev->protocol_id == id_table->protocol_id &&
(id_table->name && !strcmp(sdev->name, id_table->name));
Expand Down
4 changes: 2 additions & 2 deletions drivers/firmware/efi/dev-path-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ static long __init parse_acpi_path(const struct efi_dev_path *node,
return 0;
}

static int __init match_pci_dev(struct device *dev, void *data)
static int __init match_pci_dev(struct device *dev, const void *data)
{
unsigned int devfn = *(unsigned int *)data;
unsigned int devfn = *(const unsigned int *)data;

return dev_is_pci(dev) && to_pci_dev(dev)->devfn == devfn;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/gpio/gpio-sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ static int gpio_sim_setup_sysfs(struct gpio_sim_chip *chip)
return devm_add_action_or_reset(dev, gpio_sim_sysfs_remove, chip);
}

static int gpio_sim_dev_match_fwnode(struct device *dev, void *data)
static int gpio_sim_dev_match_fwnode(struct device *dev, const void *data)
{
return device_match_fwnode(dev, data);
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/gpu/drm/mediatek/mtk_drm_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ static const struct of_device_id mtk_drm_of_ids[] = {
};
MODULE_DEVICE_TABLE(of, mtk_drm_of_ids);

static int mtk_drm_match(struct device *dev, void *data)
static int mtk_drm_match(struct device *dev, const void *data)
{
if (!strncmp(dev_name(dev), "mediatek-drm", sizeof("mediatek-drm") - 1))
return true;
Expand Down
2 changes: 1 addition & 1 deletion drivers/hwmon/hwmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ static int hwmon_attr_base(enum hwmon_sensor_types type)

static DEFINE_MUTEX(hwmon_pec_mutex);

static int hwmon_match_device(struct device *dev, void *data)
static int hwmon_match_device(struct device *dev, const void *data)
{
return dev->class == &hwmon_class;
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/media/pci/mgb4/mgb4_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static const struct hwmon_chip_info temp_chip_info = {
};
#endif

static int match_i2c_adap(struct device *dev, void *data)
static int match_i2c_adap(struct device *dev, const void *data)
{
return i2c_verify_adapter(dev) ? 1 : 0;
}
Expand All @@ -139,7 +139,7 @@ static struct i2c_adapter *get_i2c_adap(struct platform_device *pdev)
return dev ? to_i2c_adapter(dev) : NULL;
}

static int match_spi_adap(struct device *dev, void *data)
static int match_spi_adap(struct device *dev, const void *data)
{
return to_spi_device(dev) ? 1 : 0;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/nvdimm/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ enum nd_ioctl_mode {
DIMM_IOCTL,
};

static int match_dimm(struct device *dev, void *data)
static int match_dimm(struct device *dev, const void *data)
{
long id = (long) data;

Expand Down
2 changes: 1 addition & 1 deletion drivers/pwm/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm)
return 0;
}

static int pwm_unexport_match(struct device *pwm_dev, void *data)
static int pwm_unexport_match(struct device *pwm_dev, const void *data)
{
return pwm_from_dev(pwm_dev) == data;
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/rpmsg/rpmsg_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,9 @@ EXPORT_SYMBOL(rpmsg_get_mtu);
* this is used to make sure we're not creating rpmsg devices for channels
* that already exist.
*/
static int rpmsg_device_match(struct device *dev, void *data)
static int rpmsg_device_match(struct device *dev, const void *data)
{
struct rpmsg_channel_info *chinfo = data;
const struct rpmsg_channel_info *chinfo = data;
struct rpmsg_device *rpdev = to_rpmsg_device(dev);

if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
Expand Down
3 changes: 2 additions & 1 deletion drivers/scsi/qla4xxx/ql4_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -7189,7 +7189,8 @@ static void qla4xxx_build_new_nt_list(struct scsi_qla_host *ha,
* 1: if flashnode entry is non-persistent
* 0: if flashnode entry is persistent
**/
static int qla4xxx_sysfs_ddb_is_non_persistent(struct device *dev, void *data)
static int qla4xxx_sysfs_ddb_is_non_persistent(struct device *dev,
const void *data)
{
struct iscsi_bus_flash_session *fnode_sess;

Expand Down
10 changes: 5 additions & 5 deletions drivers/scsi/scsi_transport_iscsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ EXPORT_SYMBOL_GPL(iscsi_create_flashnode_conn);
* 1 on success
* 0 on failure
*/
static int iscsi_is_flashnode_conn_dev(struct device *dev, void *data)
static int iscsi_is_flashnode_conn_dev(struct device *dev, const void *data)
{
return dev->bus == &iscsi_flashnode_bus;
}
Expand All @@ -1335,7 +1335,7 @@ static int iscsi_destroy_flashnode_conn(struct iscsi_bus_flash_conn *fnode_conn)
return 0;
}

static int flashnode_match_index(struct device *dev, void *data)
static int flashnode_match_index(struct device *dev, const void *data)
{
struct iscsi_bus_flash_session *fnode_sess = NULL;
int ret = 0;
Expand All @@ -1344,7 +1344,7 @@ static int flashnode_match_index(struct device *dev, void *data)
goto exit_match_index;

fnode_sess = iscsi_dev_to_flash_session(dev);
ret = (fnode_sess->target_id == *((int *)data)) ? 1 : 0;
ret = (fnode_sess->target_id == *((const int *)data)) ? 1 : 0;

exit_match_index:
return ret;
Expand Down Expand Up @@ -1389,8 +1389,8 @@ iscsi_get_flashnode_by_index(struct Scsi_Host *shost, uint32_t idx)
* %NULL on failure
*/
struct device *
iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data,
int (*fn)(struct device *dev, void *data))
iscsi_find_flashnode_sess(struct Scsi_Host *shost, const void *data,
device_match_t fn)
{
return device_find_child(&shost->shost_gendev, data, fn);
}
Expand Down
8 changes: 4 additions & 4 deletions drivers/slimbus/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ static bool slim_eaddr_equal(const struct slim_eaddr *a,
a->instance == b->instance);
}

static int slim_match_dev(struct device *dev, void *data)
static int slim_match_dev(struct device *dev, const void *data)
{
struct slim_eaddr *e_addr = data;
const struct slim_eaddr *e_addr = data;
struct slim_device *sbdev = to_slim_device(dev);

return slim_eaddr_equal(&sbdev->e_addr, e_addr);
Expand Down Expand Up @@ -385,9 +385,9 @@ struct slim_device *slim_get_device(struct slim_controller *ctrl,
}
EXPORT_SYMBOL_GPL(slim_get_device);

static int of_slim_match_dev(struct device *dev, void *data)
static int of_slim_match_dev(struct device *dev, const void *data)
{
struct device_node *np = data;
const struct device_node *np = data;
struct slim_device *sbdev = to_slim_device(dev);

return (sbdev->dev.of_node == np);
Expand Down
2 changes: 1 addition & 1 deletion drivers/thunderbolt/retimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ struct tb_retimer_lookup {
u8 index;
};

static int retimer_match(struct device *dev, void *data)
static int retimer_match(struct device *dev, const void *data)
{
const struct tb_retimer_lookup *lookup = data;
struct tb_retimer *rt = tb_to_retimer(dev);
Expand Down
2 changes: 1 addition & 1 deletion drivers/thunderbolt/xdomain.c
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ static int remove_missing_service(struct device *dev, void *data)
return 0;
}

static int find_service(struct device *dev, void *data)
static int find_service(struct device *dev, const void *data)
{
const struct tb_property *p = data;
struct tb_service *svc;
Expand Down
4 changes: 2 additions & 2 deletions drivers/tty/serial/serial_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2365,9 +2365,9 @@ struct uart_match {
struct uart_driver *driver;
};

static int serial_match_port(struct device *dev, void *data)
static int serial_match_port(struct device *dev, const void *data)
{
struct uart_match *match = data;
const struct uart_match *match = data;
struct tty_driver *tty_drv = match->driver->tty_driver;
dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
match->port->line;
Expand Down
8 changes: 4 additions & 4 deletions drivers/usb/typec/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ static const char * const usb_modes[] = {
/* ------------------------------------------------------------------------- */
/* Alternate Modes */

static int altmode_match(struct device *dev, void *data)
static int altmode_match(struct device *dev, const void *data)
{
struct typec_altmode *adev = to_typec_altmode(dev);
struct typec_device_id *id = data;
const struct typec_device_id *id = data;

if (!is_typec_altmode(dev))
return 0;
Expand Down Expand Up @@ -1282,7 +1282,7 @@ const struct device_type typec_cable_dev_type = {
.release = typec_cable_release,
};

static int cable_match(struct device *dev, void *data)
static int cable_match(struct device *dev, const void *data)
{
return is_typec_cable(dev);
}
Expand Down Expand Up @@ -2028,7 +2028,7 @@ const struct device_type typec_port_dev_type = {
/* --------------------------------------- */
/* Driver callbacks to report role updates */

static int partner_match(struct device *dev, void *data)
static int partner_match(struct device *dev, const void *data)
{
return is_typec_partner(dev);
}
Expand Down
Loading

0 comments on commit f1e8bf5

Please sign in to comment.