ehci-sysfs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2007 by Alan Stern
  4. */
  5. /* this file is part of ehci-hcd.c */
  6. /* Display the ports dedicated to the companion controller */
  7. static ssize_t companion_show(struct device *dev,
  8. struct device_attribute *attr,
  9. char *buf)
  10. {
  11. struct ehci_hcd *ehci;
  12. int nports, index, n;
  13. int count = PAGE_SIZE;
  14. char *ptr = buf;
  15. ehci = hcd_to_ehci(dev_get_drvdata(dev));
  16. nports = HCS_N_PORTS(ehci->hcs_params);
  17. for (index = 0; index < nports; ++index) {
  18. if (test_bit(index, &ehci->companion_ports)) {
  19. n = scnprintf(ptr, count, "%d\n", index + 1);
  20. ptr += n;
  21. count -= n;
  22. }
  23. }
  24. return ptr - buf;
  25. }
  26. /*
  27. * Dedicate or undedicate a port to the companion controller.
  28. * Syntax is "[-]portnum", where a leading '-' sign means
  29. * return control of the port to the EHCI controller.
  30. */
  31. static ssize_t companion_store(struct device *dev,
  32. struct device_attribute *attr,
  33. const char *buf, size_t count)
  34. {
  35. struct ehci_hcd *ehci;
  36. int portnum, new_owner;
  37. ehci = hcd_to_ehci(dev_get_drvdata(dev));
  38. new_owner = PORT_OWNER; /* Owned by companion */
  39. if (sscanf(buf, "%d", &portnum) != 1)
  40. return -EINVAL;
  41. if (portnum < 0) {
  42. portnum = - portnum;
  43. new_owner = 0; /* Owned by EHCI */
  44. }
  45. if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
  46. return -ENOENT;
  47. portnum--;
  48. if (new_owner)
  49. set_bit(portnum, &ehci->companion_ports);
  50. else
  51. clear_bit(portnum, &ehci->companion_ports);
  52. set_owner(ehci, portnum, new_owner);
  53. return count;
  54. }
  55. static DEVICE_ATTR_RW(companion);
  56. /*
  57. * Display / Set uframe_periodic_max
  58. */
  59. static ssize_t uframe_periodic_max_show(struct device *dev,
  60. struct device_attribute *attr,
  61. char *buf)
  62. {
  63. struct ehci_hcd *ehci;
  64. int n;
  65. ehci = hcd_to_ehci(dev_get_drvdata(dev));
  66. n = scnprintf(buf, PAGE_SIZE, "%d\n", ehci->uframe_periodic_max);
  67. return n;
  68. }
  69. static ssize_t uframe_periodic_max_store(struct device *dev,
  70. struct device_attribute *attr,
  71. const char *buf, size_t count)
  72. {
  73. struct ehci_hcd *ehci;
  74. unsigned uframe_periodic_max;
  75. unsigned uframe;
  76. unsigned long flags;
  77. ssize_t ret;
  78. ehci = hcd_to_ehci(dev_get_drvdata(dev));
  79. if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
  80. return -EINVAL;
  81. if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) {
  82. ehci_info(ehci, "rejecting invalid request for "
  83. "uframe_periodic_max=%u\n", uframe_periodic_max);
  84. return -EINVAL;
  85. }
  86. ret = -EINVAL;
  87. /*
  88. * lock, so that our checking does not race with possible periodic
  89. * bandwidth allocation through submitting new urbs.
  90. */
  91. spin_lock_irqsave (&ehci->lock, flags);
  92. /*
  93. * for request to decrease max periodic bandwidth, we have to check
  94. * to see whether the decrease is possible.
  95. */
  96. if (uframe_periodic_max < ehci->uframe_periodic_max) {
  97. u8 allocated_max = 0;
  98. for (uframe = 0; uframe < EHCI_BANDWIDTH_SIZE; ++uframe)
  99. allocated_max = max(allocated_max,
  100. ehci->bandwidth[uframe]);
  101. if (allocated_max > uframe_periodic_max) {
  102. ehci_info(ehci,
  103. "cannot decrease uframe_periodic_max because "
  104. "periodic bandwidth is already allocated "
  105. "(%u > %u)\n",
  106. allocated_max, uframe_periodic_max);
  107. goto out_unlock;
  108. }
  109. }
  110. /* increasing is always ok */
  111. ehci_info(ehci, "setting max periodic bandwidth to %u%% "
  112. "(== %u usec/uframe)\n",
  113. 100*uframe_periodic_max/125, uframe_periodic_max);
  114. if (uframe_periodic_max != 100)
  115. ehci_warn(ehci, "max periodic bandwidth set is non-standard\n");
  116. ehci->uframe_periodic_max = uframe_periodic_max;
  117. ret = count;
  118. out_unlock:
  119. spin_unlock_irqrestore (&ehci->lock, flags);
  120. return ret;
  121. }
  122. static DEVICE_ATTR_RW(uframe_periodic_max);
  123. static inline int create_sysfs_files(struct ehci_hcd *ehci)
  124. {
  125. struct device *controller = ehci_to_hcd(ehci)->self.controller;
  126. int i = 0;
  127. /* with integrated TT there is no companion! */
  128. if (!ehci_is_TDI(ehci))
  129. i = device_create_file(controller, &dev_attr_companion);
  130. if (i)
  131. goto out;
  132. i = device_create_file(controller, &dev_attr_uframe_periodic_max);
  133. out:
  134. return i;
  135. }
  136. static inline void remove_sysfs_files(struct ehci_hcd *ehci)
  137. {
  138. struct device *controller = ehci_to_hcd(ehci)->self.controller;
  139. /* with integrated TT there is no companion! */
  140. if (!ehci_is_TDI(ehci))
  141. device_remove_file(controller, &dev_attr_companion);
  142. device_remove_file(controller, &dev_attr_uframe_periodic_max);
  143. }