ehci-timer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2012 by Alan Stern
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. /* This file is part of ehci-hcd.c */
  15. /*-------------------------------------------------------------------------*/
  16. /*
  17. * EHCI timer support... Now using hrtimers.
  18. *
  19. * Lots of different events are triggered from ehci->hrtimer. Whenever
  20. * the timer routine runs, it checks each possible event; events that are
  21. * currently enabled and whose expiration time has passed get handled.
  22. * The set of enabled events is stored as a collection of bitflags in
  23. * ehci->enabled_hrtimer_events, and they are numbered in order of
  24. * increasing delay values (ranging between 1 ms and 100 ms).
  25. *
  26. * Rather than implementing a sorted list or tree of all pending events,
  27. * we keep track only of the lowest-numbered pending event, in
  28. * ehci->next_hrtimer_event. Whenever ehci->hrtimer gets restarted, its
  29. * expiration time is set to the timeout value for this event.
  30. *
  31. * As a result, events might not get handled right away; the actual delay
  32. * could be anywhere up to twice the requested delay. This doesn't
  33. * matter, because none of the events are especially time-critical. The
  34. * ones that matter most all have a delay of 1 ms, so they will be
  35. * handled after 2 ms at most, which is okay. In addition to this, we
  36. * allow for an expiration range of 1 ms.
  37. */
  38. /*
  39. * Delay lengths for the hrtimer event types.
  40. * Keep this list sorted by delay length, in the same order as
  41. * the event types indexed by enum ehci_hrtimer_event in ehci.h.
  42. */
  43. static unsigned event_delays_ns[] = {
  44. };
  45. /* Enable a pending hrtimer event */
  46. static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
  47. bool resched)
  48. {
  49. ktime_t *timeout = &ehci->hr_timeouts[event];
  50. if (resched)
  51. *timeout = ktime_add(ktime_get(),
  52. ktime_set(0, event_delays_ns[event]));
  53. ehci->enabled_hrtimer_events |= (1 << event);
  54. /* Track only the lowest-numbered pending event */
  55. if (event < ehci->next_hrtimer_event) {
  56. ehci->next_hrtimer_event = event;
  57. hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
  58. NSEC_PER_MSEC, HRTIMER_MODE_ABS);
  59. }
  60. }
  61. /*
  62. * Handler functions for the hrtimer event types.
  63. * Keep this array in the same order as the event types indexed by
  64. * enum ehci_hrtimer_event in ehci.h.
  65. */
  66. static void (*event_handlers[])(struct ehci_hcd *) = {
  67. };
  68. static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
  69. {
  70. struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer);
  71. ktime_t now;
  72. unsigned long events;
  73. unsigned long flags;
  74. unsigned e;
  75. spin_lock_irqsave(&ehci->lock, flags);
  76. events = ehci->enabled_hrtimer_events;
  77. ehci->enabled_hrtimer_events = 0;
  78. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  79. /*
  80. * Check each pending event. If its time has expired, handle
  81. * the event; otherwise re-enable it.
  82. */
  83. now = ktime_get();
  84. for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
  85. if (now.tv64 >= ehci->hr_timeouts[e].tv64)
  86. event_handlers[e](ehci);
  87. else
  88. ehci_enable_event(ehci, e, false);
  89. }
  90. spin_unlock_irqrestore(&ehci->lock, flags);
  91. return HRTIMER_NORESTART;
  92. }