port.c 4.9 KB

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