port.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * usb port device code
  3. *
  4. * Copyright (C) 2012 Intel Corp
  5. *
  6. * Author: Lan Tianyu <tianyu.lan@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. *
  17. */
  18. #include <linux/slab.h>
  19. #include <linux/pm_qos.h>
  20. #include "hub.h"
  21. static const struct attribute_group *port_dev_group[];
  22. static ssize_t connect_type_show(struct device *dev,
  23. struct device_attribute *attr, char *buf)
  24. {
  25. struct usb_port *port_dev = to_usb_port(dev);
  26. char *result;
  27. switch (port_dev->connect_type) {
  28. case USB_PORT_CONNECT_TYPE_HOT_PLUG:
  29. result = "hotplug";
  30. break;
  31. case USB_PORT_CONNECT_TYPE_HARD_WIRED:
  32. result = "hardwired";
  33. break;
  34. case USB_PORT_NOT_USED:
  35. result = "not used";
  36. break;
  37. default:
  38. result = "unknown";
  39. break;
  40. }
  41. return sprintf(buf, "%s\n", result);
  42. }
  43. static DEVICE_ATTR_RO(connect_type);
  44. static struct attribute *port_dev_attrs[] = {
  45. &dev_attr_connect_type.attr,
  46. NULL,
  47. };
  48. static struct attribute_group port_dev_attr_grp = {
  49. .attrs = port_dev_attrs,
  50. };
  51. static const struct attribute_group *port_dev_group[] = {
  52. &port_dev_attr_grp,
  53. NULL,
  54. };
  55. static void usb_port_device_release(struct device *dev)
  56. {
  57. struct usb_port *port_dev = to_usb_port(dev);
  58. kfree(port_dev);
  59. }
  60. #ifdef CONFIG_PM_RUNTIME
  61. static int usb_port_runtime_resume(struct device *dev)
  62. {
  63. struct usb_port *port_dev = to_usb_port(dev);
  64. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  65. struct usb_interface *intf = to_usb_interface(dev->parent);
  66. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  67. int port1 = port_dev->portnum;
  68. int retval;
  69. if (!hub)
  70. return -EINVAL;
  71. usb_autopm_get_interface(intf);
  72. set_bit(port1, hub->busy_bits);
  73. retval = usb_hub_set_port_power(hdev, hub, port1, true);
  74. if (port_dev->child && !retval) {
  75. /*
  76. * Attempt to wait for usb hub port to be reconnected in order
  77. * to make the resume procedure successful. The device may have
  78. * disconnected while the port was powered off, so ignore the
  79. * return status.
  80. */
  81. retval = hub_port_debounce_be_connected(hub, port1);
  82. if (retval < 0)
  83. dev_dbg(&port_dev->dev, "can't get reconnection after setting port power on, status %d\n",
  84. retval);
  85. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  86. retval = 0;
  87. }
  88. clear_bit(port1, hub->busy_bits);
  89. usb_autopm_put_interface(intf);
  90. return retval;
  91. }
  92. static int usb_port_runtime_suspend(struct device *dev)
  93. {
  94. struct usb_port *port_dev = to_usb_port(dev);
  95. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  96. struct usb_interface *intf = to_usb_interface(dev->parent);
  97. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  98. int port1 = port_dev->portnum;
  99. int retval;
  100. if (!hub)
  101. return -EINVAL;
  102. if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
  103. == PM_QOS_FLAGS_ALL)
  104. return -EAGAIN;
  105. usb_autopm_get_interface(intf);
  106. set_bit(port1, hub->busy_bits);
  107. retval = usb_hub_set_port_power(hdev, hub, port1, false);
  108. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
  109. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  110. clear_bit(port1, hub->busy_bits);
  111. usb_autopm_put_interface(intf);
  112. return retval;
  113. }
  114. #endif
  115. static const struct dev_pm_ops usb_port_pm_ops = {
  116. #ifdef CONFIG_PM_RUNTIME
  117. .runtime_suspend = usb_port_runtime_suspend,
  118. .runtime_resume = usb_port_runtime_resume,
  119. #endif
  120. };
  121. struct device_type usb_port_device_type = {
  122. .name = "usb_port",
  123. .release = usb_port_device_release,
  124. .pm = &usb_port_pm_ops,
  125. };
  126. int usb_hub_create_port_device(struct usb_hub *hub, int port1)
  127. {
  128. struct usb_port *port_dev = NULL;
  129. int retval;
  130. port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
  131. if (!port_dev) {
  132. retval = -ENOMEM;
  133. goto exit;
  134. }
  135. hub->ports[port1 - 1] = port_dev;
  136. port_dev->portnum = port1;
  137. port_dev->power_is_on = true;
  138. port_dev->dev.parent = hub->intfdev;
  139. port_dev->dev.groups = port_dev_group;
  140. port_dev->dev.type = &usb_port_device_type;
  141. dev_set_name(&port_dev->dev, "port%d", port1);
  142. retval = device_register(&port_dev->dev);
  143. if (retval)
  144. goto error_register;
  145. pm_runtime_set_active(&port_dev->dev);
  146. /* It would be dangerous if user space couldn't
  147. * prevent usb device from being powered off. So don't
  148. * enable port runtime pm if failed to expose port's pm qos.
  149. */
  150. if (!dev_pm_qos_expose_flags(&port_dev->dev,
  151. PM_QOS_FLAG_NO_POWER_OFF))
  152. pm_runtime_enable(&port_dev->dev);
  153. device_enable_async_suspend(&port_dev->dev);
  154. return 0;
  155. error_register:
  156. put_device(&port_dev->dev);
  157. exit:
  158. return retval;
  159. }
  160. void usb_hub_remove_port_device(struct usb_hub *hub,
  161. int port1)
  162. {
  163. device_unregister(&hub->ports[port1 - 1]->dev);
  164. }