Alan Stern | d58b4bc | 2012-07-11 11:21:54 -0400 | [diff] [blame^] | 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 | |
| 15 | /* This file is part of ehci-hcd.c */ |
| 16 | |
| 17 | /*-------------------------------------------------------------------------*/ |
| 18 | |
| 19 | /* |
| 20 | * EHCI timer support... Now using hrtimers. |
| 21 | * |
| 22 | * Lots of different events are triggered from ehci->hrtimer. Whenever |
| 23 | * the timer routine runs, it checks each possible event; events that are |
| 24 | * currently enabled and whose expiration time has passed get handled. |
| 25 | * The set of enabled events is stored as a collection of bitflags in |
| 26 | * ehci->enabled_hrtimer_events, and they are numbered in order of |
| 27 | * increasing delay values (ranging between 1 ms and 100 ms). |
| 28 | * |
| 29 | * Rather than implementing a sorted list or tree of all pending events, |
| 30 | * we keep track only of the lowest-numbered pending event, in |
| 31 | * ehci->next_hrtimer_event. Whenever ehci->hrtimer gets restarted, its |
| 32 | * expiration time is set to the timeout value for this event. |
| 33 | * |
| 34 | * As a result, events might not get handled right away; the actual delay |
| 35 | * could be anywhere up to twice the requested delay. This doesn't |
| 36 | * matter, because none of the events are especially time-critical. The |
| 37 | * ones that matter most all have a delay of 1 ms, so they will be |
| 38 | * handled after 2 ms at most, which is okay. In addition to this, we |
| 39 | * allow for an expiration range of 1 ms. |
| 40 | */ |
| 41 | |
| 42 | /* |
| 43 | * Delay lengths for the hrtimer event types. |
| 44 | * Keep this list sorted by delay length, in the same order as |
| 45 | * the event types indexed by enum ehci_hrtimer_event in ehci.h. |
| 46 | */ |
| 47 | static unsigned event_delays_ns[] = { |
| 48 | }; |
| 49 | |
| 50 | /* Enable a pending hrtimer event */ |
| 51 | static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event, |
| 52 | bool resched) |
| 53 | { |
| 54 | ktime_t *timeout = &ehci->hr_timeouts[event]; |
| 55 | |
| 56 | if (resched) |
| 57 | *timeout = ktime_add(ktime_get(), |
| 58 | ktime_set(0, event_delays_ns[event])); |
| 59 | ehci->enabled_hrtimer_events |= (1 << event); |
| 60 | |
| 61 | /* Track only the lowest-numbered pending event */ |
| 62 | if (event < ehci->next_hrtimer_event) { |
| 63 | ehci->next_hrtimer_event = event; |
| 64 | hrtimer_start_range_ns(&ehci->hrtimer, *timeout, |
| 65 | NSEC_PER_MSEC, HRTIMER_MODE_ABS); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | * Handler functions for the hrtimer event types. |
| 72 | * Keep this array in the same order as the event types indexed by |
| 73 | * enum ehci_hrtimer_event in ehci.h. |
| 74 | */ |
| 75 | static void (*event_handlers[])(struct ehci_hcd *) = { |
| 76 | }; |
| 77 | |
| 78 | static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t) |
| 79 | { |
| 80 | struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer); |
| 81 | ktime_t now; |
| 82 | unsigned long events; |
| 83 | unsigned long flags; |
| 84 | unsigned e; |
| 85 | |
| 86 | spin_lock_irqsave(&ehci->lock, flags); |
| 87 | |
| 88 | events = ehci->enabled_hrtimer_events; |
| 89 | ehci->enabled_hrtimer_events = 0; |
| 90 | ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT; |
| 91 | |
| 92 | /* |
| 93 | * Check each pending event. If its time has expired, handle |
| 94 | * the event; otherwise re-enable it. |
| 95 | */ |
| 96 | now = ktime_get(); |
| 97 | for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) { |
| 98 | if (now.tv64 >= ehci->hr_timeouts[e].tv64) |
| 99 | event_handlers[e](ehci); |
| 100 | else |
| 101 | ehci_enable_event(ehci, e, false); |
| 102 | } |
| 103 | |
| 104 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 105 | return HRTIMER_NORESTART; |
| 106 | } |