devcon.c 3.5 KB

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