serdev_helpers.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * In some cases UART attached devices which require an in kernel driver,
  4. * e.g. UART attached Bluetooth HCIs are described in the ACPI tables
  5. * by an ACPI device with a broken or missing UartSerialBusV2() resource.
  6. *
  7. * This causes the kernel to create a /dev/ttyS# char-device for the UART
  8. * instead of creating an in kernel serdev-controller + serdev-device pair
  9. * for the in kernel driver.
  10. *
  11. * The quirk handling in acpi_quirk_skip_serdev_enumeration() makes the kernel
  12. * create a serdev-controller device for these UARTs instead of a /dev/ttyS#.
  13. *
  14. * Instantiating the actual serdev-device to bind to is up to pdx86 code,
  15. * this header provides a helper for getting the serdev-controller device.
  16. */
  17. #include <linux/acpi.h>
  18. #include <linux/device.h>
  19. #include <linux/err.h>
  20. #include <linux/printk.h>
  21. #include <linux/sprintf.h>
  22. #include <linux/string.h>
  23. static inline struct device *
  24. get_serdev_controller(const char *serial_ctrl_hid,
  25. const char *serial_ctrl_uid,
  26. int serial_ctrl_port,
  27. const char *serdev_ctrl_name)
  28. {
  29. struct device *ctrl_dev, *child;
  30. struct acpi_device *ctrl_adev;
  31. char name[32];
  32. int i;
  33. ctrl_adev = acpi_dev_get_first_match_dev(serial_ctrl_hid, serial_ctrl_uid, -1);
  34. if (!ctrl_adev) {
  35. pr_err("error could not get %s/%s serial-ctrl adev\n",
  36. serial_ctrl_hid, serial_ctrl_uid ?: "*");
  37. return ERR_PTR(-ENODEV);
  38. }
  39. /* get_first_physical_node() returns a weak ref */
  40. ctrl_dev = get_device(acpi_get_first_physical_node(ctrl_adev));
  41. if (!ctrl_dev) {
  42. pr_err("error could not get %s/%s serial-ctrl physical node\n",
  43. serial_ctrl_hid, serial_ctrl_uid ?: "*");
  44. ctrl_dev = ERR_PTR(-ENODEV);
  45. goto put_ctrl_adev;
  46. }
  47. /* Walk host -> uart-ctrl -> port -> serdev-ctrl */
  48. for (i = 0; i < 3; i++) {
  49. switch (i) {
  50. case 0:
  51. snprintf(name, sizeof(name), "%s:0", dev_name(ctrl_dev));
  52. break;
  53. case 1:
  54. snprintf(name, sizeof(name), "%s.%d",
  55. dev_name(ctrl_dev), serial_ctrl_port);
  56. break;
  57. case 2:
  58. strscpy(name, serdev_ctrl_name, sizeof(name));
  59. break;
  60. }
  61. child = device_find_child_by_name(ctrl_dev, name);
  62. put_device(ctrl_dev);
  63. if (!child) {
  64. pr_err("error could not find '%s' device\n", name);
  65. ctrl_dev = ERR_PTR(-ENODEV);
  66. goto put_ctrl_adev;
  67. }
  68. ctrl_dev = child;
  69. }
  70. put_ctrl_adev:
  71. acpi_dev_put(ctrl_adev);
  72. return ctrl_dev;
  73. }