port.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 show_port_connect_type(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(connect_type, S_IRUGO, show_port_connect_type,
  44. NULL);
  45. static struct attribute *port_dev_attrs[] = {
  46. &dev_attr_connect_type.attr,
  47. NULL,
  48. };
  49. static struct attribute_group port_dev_attr_grp = {
  50. .attrs = port_dev_attrs,
  51. };
  52. static const struct attribute_group *port_dev_group[] = {
  53. &port_dev_attr_grp,
  54. NULL,
  55. };
  56. static void usb_port_device_release(struct device *dev)
  57. {
  58. struct usb_port *port_dev = to_usb_port(dev);
  59. kfree(port_dev);
  60. }
  61. #ifdef CONFIG_PM_RUNTIME
  62. static int usb_port_runtime_resume(struct device *dev)
  63. {
  64. struct usb_port *port_dev = to_usb_port(dev);
  65. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  66. struct usb_interface *intf = to_usb_interface(dev->parent);
  67. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  68. int port1 = port_dev->portnum;
  69. int retval;
  70. if (!hub)
  71. return -EINVAL;
  72. usb_autopm_get_interface(intf);
  73. set_bit(port1, hub->busy_bits);
  74. retval = usb_hub_set_port_power(hdev, hub, port1, true);
  75. if (port_dev->child && !retval) {
  76. /*
  77. * Attempt to wait for usb hub port to be reconnected in order
  78. * to make the resume procedure successful. The device may have
  79. * disconnected while the port was powered off, so ignore the
  80. * return status.
  81. */
  82. retval = hub_port_debounce_be_connected(hub, port1);
  83. if (retval < 0)
  84. dev_dbg(&port_dev->dev, "can't get reconnection after setting port power on, status %d\n",
  85. retval);
  86. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  87. retval = 0;
  88. }
  89. clear_bit(port1, hub->busy_bits);
  90. usb_autopm_put_interface(intf);
  91. return retval;
  92. }
  93. static int usb_port_runtime_suspend(struct device *dev)
  94. {
  95. struct usb_port *port_dev = to_usb_port(dev);
  96. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  97. struct usb_interface *intf = to_usb_interface(dev->parent);
  98. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  99. int port1 = port_dev->portnum;
  100. int retval;
  101. if (!hub)
  102. return -EINVAL;
  103. if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
  104. == PM_QOS_FLAGS_ALL)
  105. return -EAGAIN;
  106. usb_autopm_get_interface(intf);
  107. set_bit(port1, hub->busy_bits);
  108. retval = usb_hub_set_port_power(hdev, hub, port1, false);
  109. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
  110. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  111. clear_bit(port1, hub->busy_bits);
  112. usb_autopm_put_interface(intf);
  113. return retval;
  114. }
  115. #endif
  116. static const struct dev_pm_ops usb_port_pm_ops = {
  117. #ifdef CONFIG_PM_RUNTIME
  118. .runtime_suspend = usb_port_runtime_suspend,
  119. .runtime_resume = usb_port_runtime_resume,
  120. #endif
  121. };
  122. struct device_type usb_port_device_type = {
  123. .name = "usb_port",
  124. .release = usb_port_device_release,
  125. .pm = &usb_port_pm_ops,
  126. };
  127. int usb_hub_create_port_device(struct usb_hub *hub, int port1)
  128. {
  129. struct usb_port *port_dev = NULL;
  130. int retval;
  131. port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
  132. if (!port_dev) {
  133. retval = -ENOMEM;
  134. goto exit;
  135. }
  136. hub->ports[port1 - 1] = port_dev;
  137. port_dev->portnum = port1;
  138. port_dev->power_is_on = true;
  139. port_dev->dev.parent = hub->intfdev;
  140. port_dev->dev.groups = port_dev_group;
  141. port_dev->dev.type = &usb_port_device_type;
  142. dev_set_name(&port_dev->dev, "port%d", port1);
  143. retval = device_register(&port_dev->dev);
  144. if (retval)
  145. goto error_register;
  146. pm_runtime_set_active(&port_dev->dev);
  147. /* It would be dangerous if user space couldn't
  148. * prevent usb device from being powered off. So don't
  149. * enable port runtime pm if failed to expose port's pm qos.
  150. */
  151. if (!dev_pm_qos_expose_flags(&port_dev->dev,
  152. PM_QOS_FLAG_NO_POWER_OFF))
  153. pm_runtime_enable(&port_dev->dev);
  154. device_enable_async_suspend(&port_dev->dev);
  155. return 0;
  156. error_register:
  157. put_device(&port_dev->dev);
  158. exit:
  159. return retval;
  160. }
  161. void usb_hub_remove_port_device(struct usb_hub *hub,
  162. int port1)
  163. {
  164. device_unregister(&hub->ports[port1 - 1]->dev);
  165. }