scsi: qla2xxx: Fix recursive mailbox timeout
[linux/fpc-iii.git] / drivers / base / devcon.c
blobd427e806cd73bf4f5cd6284a6e5126139087a2e3
1 // SPDX-License-Identifier: GPL-2.0
2 /**
3 * Device connections
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 */
9 #include <linux/device.h>
11 static DEFINE_MUTEX(devcon_lock);
12 static LIST_HEAD(devcon_list);
14 /**
15 * device_connection_find_match - Find physical connection to a device
16 * @dev: Device with the connection
17 * @con_id: Identifier for the connection
18 * @data: Data for the match function
19 * @match: Function to check and convert the connection description
21 * Find a connection with unique identifier @con_id between @dev and another
22 * device. @match will be used to convert the connection description to data the
23 * caller is expecting to be returned.
25 void *device_connection_find_match(struct device *dev, const char *con_id,
26 void *data,
27 void *(*match)(struct device_connection *con,
28 int ep, void *data))
30 const char *devname = dev_name(dev);
31 struct device_connection *con;
32 void *ret = NULL;
33 int ep;
35 if (!match)
36 return NULL;
38 mutex_lock(&devcon_lock);
40 list_for_each_entry(con, &devcon_list, list) {
41 ep = match_string(con->endpoint, 2, devname);
42 if (ep < 0)
43 continue;
45 if (con_id && strcmp(con->id, con_id))
46 continue;
48 ret = match(con, !ep, data);
49 if (ret)
50 break;
53 mutex_unlock(&devcon_lock);
55 return ret;
57 EXPORT_SYMBOL_GPL(device_connection_find_match);
59 extern struct bus_type platform_bus_type;
60 extern struct bus_type pci_bus_type;
61 extern struct bus_type i2c_bus_type;
62 extern struct bus_type spi_bus_type;
64 static struct bus_type *generic_match_buses[] = {
65 &platform_bus_type,
66 #ifdef CONFIG_PCI
67 &pci_bus_type,
68 #endif
69 #ifdef CONFIG_I2C
70 &i2c_bus_type,
71 #endif
72 #ifdef CONFIG_SPI_MASTER
73 &spi_bus_type,
74 #endif
75 NULL,
78 /* This tries to find the device from the most common bus types by name. */
79 static void *generic_match(struct device_connection *con, int ep, void *data)
81 struct bus_type *bus;
82 struct device *dev;
84 for (bus = generic_match_buses[0]; bus; bus++) {
85 dev = bus_find_device_by_name(bus, NULL, con->endpoint[ep]);
86 if (dev)
87 return dev;
91 * We only get called if a connection was found, tell the caller to
92 * wait for the other device to show up.
94 return ERR_PTR(-EPROBE_DEFER);
97 /**
98 * device_connection_find - Find two devices connected together
99 * @dev: Device with the connection
100 * @con_id: Identifier for the connection
102 * Find a connection with unique identifier @con_id between @dev and
103 * another device. On success returns handle to the device that is connected
104 * to @dev, with the reference count for the found device incremented. Returns
105 * NULL if no matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a
106 * connection was found but the other device has not been enumerated yet.
108 struct device *device_connection_find(struct device *dev, const char *con_id)
110 return device_connection_find_match(dev, con_id, NULL, generic_match);
112 EXPORT_SYMBOL_GPL(device_connection_find);
115 * device_connection_add - Register a connection description
116 * @con: The connection description to be registered
118 void device_connection_add(struct device_connection *con)
120 mutex_lock(&devcon_lock);
121 list_add_tail(&con->list, &devcon_list);
122 mutex_unlock(&devcon_lock);
124 EXPORT_SYMBOL_GPL(device_connection_add);
127 * device_connections_remove - Unregister connection description
128 * @con: The connection description to be unregistered
130 void device_connection_remove(struct device_connection *con)
132 mutex_lock(&devcon_lock);
133 list_del(&con->list);
134 mutex_unlock(&devcon_lock);
136 EXPORT_SYMBOL_GPL(device_connection_remove);