port.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. * Wait for usb hub port to be reconnected in order to make
  78. * the resume procedure successful.
  79. */
  80. retval = hub_port_debounce_be_connected(hub, port1);
  81. if (retval < 0) {
  82. dev_dbg(&port_dev->dev, "can't get reconnection after setting port power on, status %d\n",
  83. retval);
  84. goto out;
  85. }
  86. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  87. /* Set return value to 0 if debounce successful */
  88. retval = 0;
  89. }
  90. out:
  91. clear_bit(port1, hub->busy_bits);
  92. usb_autopm_put_interface(intf);
  93. return retval;
  94. }
  95. static int usb_port_runtime_suspend(struct device *dev)
  96. {
  97. struct usb_port *port_dev = to_usb_port(dev);
  98. struct usb_device *hdev = to_usb_device(dev->parent->parent);
  99. struct usb_interface *intf = to_usb_interface(dev->parent);
  100. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  101. int port1 = port_dev->portnum;
  102. int retval;
  103. if (!hub)
  104. return -EINVAL;
  105. if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
  106. == PM_QOS_FLAGS_ALL)
  107. return -EAGAIN;
  108. usb_autopm_get_interface(intf);
  109. set_bit(port1, hub->busy_bits);
  110. retval = usb_hub_set_port_power(hdev, hub, port1, false);
  111. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
  112. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  113. clear_bit(port1, hub->busy_bits);
  114. usb_autopm_put_interface(intf);
  115. return retval;
  116. }
  117. #endif
  118. static const struct dev_pm_ops usb_port_pm_ops = {
  119. #ifdef CONFIG_PM_RUNTIME
  120. .runtime_suspend = usb_port_runtime_suspend,
  121. .runtime_resume = usb_port_runtime_resume,
  122. #endif
  123. };
  124. struct device_type usb_port_device_type = {
  125. .name = "usb_port",
  126. .release = usb_port_device_release,
  127. .pm = &usb_port_pm_ops,
  128. };
  129. int usb_hub_create_port_device(struct usb_hub *hub, int port1)
  130. {
  131. struct usb_port *port_dev = NULL;
  132. int retval;
  133. port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
  134. if (!port_dev) {
  135. retval = -ENOMEM;
  136. goto exit;
  137. }
  138. hub->ports[port1 - 1] = port_dev;
  139. port_dev->portnum = port1;
  140. port_dev->power_is_on = true;
  141. port_dev->dev.parent = hub->intfdev;
  142. port_dev->dev.groups = port_dev_group;
  143. port_dev->dev.type = &usb_port_device_type;
  144. dev_set_name(&port_dev->dev, "port%d", port1);
  145. retval = device_register(&port_dev->dev);
  146. if (retval)
  147. goto error_register;
  148. pm_runtime_set_active(&port_dev->dev);
  149. /* It would be dangerous if user space couldn't
  150. * prevent usb device from being powered off. So don't
  151. * enable port runtime pm if failed to expose port's pm qos.
  152. */
  153. if (!dev_pm_qos_expose_flags(&port_dev->dev,
  154. PM_QOS_FLAG_NO_POWER_OFF))
  155. pm_runtime_enable(&port_dev->dev);
  156. device_enable_async_suspend(&port_dev->dev);
  157. return 0;
  158. error_register:
  159. put_device(&port_dev->dev);
  160. exit:
  161. return retval;
  162. }
  163. void usb_hub_remove_port_device(struct usb_hub *hub,
  164. int port1)
  165. {
  166. device_unregister(&hub->ports[port1 - 1]->dev);
  167. }