1 /*-
2  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 /*
30  * IEEE 802.11 power save support.
31  */
32 #include "opt_wlan.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 
38 #include <sys/socket.h>
39 
40 #include <net/if.h>
41 #include <net/if_var.h>
42 #include <net/if_media.h>
43 #include <net/ethernet.h>
44 
45 #include <netproto/802_11/ieee80211_var.h>
46 
47 #include <net/bpf.h>
48 
49 static void ieee80211_update_ps(struct ieee80211vap *, int);
50 static int ieee80211_set_tim(struct ieee80211_node *, int);
51 
52 static MALLOC_DEFINE(M_80211_POWER, "80211power", "802.11 power save state");
53 
54 void
55 ieee80211_power_attach(struct ieee80211com *ic)
56 {
57 }
58 
59 void
60 ieee80211_power_detach(struct ieee80211com *ic)
61 {
62 }
63 
64 void
65 ieee80211_power_vattach(struct ieee80211vap *vap)
66 {
67 	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
68 	    vap->iv_opmode == IEEE80211_M_IBSS) {
69 		/* NB: driver should override */
70 		vap->iv_update_ps = ieee80211_update_ps;
71 		vap->iv_set_tim = ieee80211_set_tim;
72 	}
73 	vap->iv_node_ps = ieee80211_node_pwrsave;
74 	vap->iv_sta_ps = ieee80211_sta_pwrsave;
75 }
76 
77 void
78 ieee80211_power_latevattach(struct ieee80211vap *vap)
79 {
80 	/*
81 	 * Allocate these only if needed.  Beware that we
82 	 * know adhoc mode doesn't support ATIM yet...
83 	 */
84 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
85 		vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t);
86 		vap->iv_tim_bitmap = (uint8_t *) kmalloc(vap->iv_tim_len,
87 			M_80211_POWER, M_INTWAIT | M_ZERO);
88 		if (vap->iv_tim_bitmap == NULL) {
89 			kprintf("%s: no memory for TIM bitmap!\n", __func__);
90 			/* XXX good enough to keep from crashing? */
91 			vap->iv_tim_len = 0;
92 		}
93 	}
94 }
95 
96 void
97 ieee80211_power_vdetach(struct ieee80211vap *vap)
98 {
99 	if (vap->iv_tim_bitmap != NULL) {
100 		kfree(vap->iv_tim_bitmap, M_80211_POWER);
101 		vap->iv_tim_bitmap = NULL;
102 	}
103 }
104 
105 void
106 ieee80211_psq_init(struct ieee80211_psq *psq, const char *name)
107 {
108 	memset(psq, 0, sizeof(*psq));
109 	psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE;
110 	IEEE80211_PSQ_INIT(psq, name);		/* OS-dependent setup */
111 }
112 
113 void
114 ieee80211_psq_cleanup(struct ieee80211_psq *psq)
115 {
116 #if 0
117 	psq_drain(psq);				/* XXX should not be needed? */
118 #else
119 	KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len));
120 #endif
121 	IEEE80211_PSQ_DESTROY(psq);		/* OS-dependent cleanup */
122 }
123 
124 /*
125  * Return the highest priority frame in the ps queue.
126  */
127 struct mbuf *
128 ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen)
129 {
130 	struct ieee80211_psq *psq = &ni->ni_psq;
131 	struct ieee80211_psq_head *qhead;
132 	struct mbuf *m;
133 
134 	IEEE80211_PSQ_LOCK(psq);
135 	qhead = &psq->psq_head[0];
136 again:
137 	if ((m = qhead->head) != NULL) {
138 		if ((qhead->head = m->m_nextpkt) == NULL)
139 			qhead->tail = NULL;
140 		KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
141 		qhead->len--;
142 		KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
143 		psq->psq_len--;
144 		m->m_nextpkt = NULL;
145 	}
146 	if (m == NULL && qhead == &psq->psq_head[0]) {
147 		/* Algol-68 style for loop */
148 		qhead = &psq->psq_head[1];
149 		goto again;
150 	}
151 	if (qlen != NULL)
152 		*qlen = psq->psq_len;
153 	IEEE80211_PSQ_UNLOCK(psq);
154 	return m;
155 }
156 
157 /*
158  * Reclaim an mbuf from the ps q.  If marked with M_ENCAP
159  * we assume there is a node reference that must be relcaimed.
160  */
161 static void
162 psq_mfree(struct mbuf *m)
163 {
164 	if (m->m_flags & M_ENCAP) {
165 		struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif;
166 		ieee80211_free_node(ni);
167 	}
168 	m->m_nextpkt = NULL;
169 	m_freem(m);
170 }
171 
172 /*
173  * Clear any frames queued in the power save queue.
174  * The number of frames that were present is returned.
175  */
176 static int
177 psq_drain(struct ieee80211_psq *psq)
178 {
179 	struct ieee80211_psq_head *qhead;
180 	struct mbuf *m;
181 	int qlen;
182 
183 	IEEE80211_PSQ_LOCK(psq);
184 	qlen = psq->psq_len;
185 	qhead = &psq->psq_head[0];
186 again:
187 	while ((m = qhead->head) != NULL) {
188 		qhead->head = m->m_nextpkt;
189 		psq_mfree(m);
190 	}
191 	qhead->tail = NULL;
192 	qhead->len = 0;
193 	if (qhead == &psq->psq_head[0]) {	/* Algol-68 style for loop */
194 		qhead = &psq->psq_head[1];
195 		goto again;
196 	}
197 	psq->psq_len = 0;
198 	IEEE80211_PSQ_UNLOCK(psq);
199 
200 	return qlen;
201 }
202 
203 /*
204  * Clear any frames queued in the power save queue.
205  * The number of frames that were present is returned.
206  */
207 int
208 ieee80211_node_psq_drain(struct ieee80211_node *ni)
209 {
210 	return psq_drain(&ni->ni_psq);
211 }
212 
213 /*
214  * Age frames on the power save queue. The aging interval is
215  * 4 times the listen interval specified by the station.  This
216  * number is factored into the age calculations when the frame
217  * is placed on the queue.  We store ages as time differences
218  * so we can check and/or adjust only the head of the list.
219  * If a frame's age exceeds the threshold then discard it.
220  * The number of frames discarded is returned so the caller
221  * can check if it needs to adjust the tim.
222  */
223 int
224 ieee80211_node_psq_age(struct ieee80211_node *ni)
225 {
226 	struct ieee80211_psq *psq = &ni->ni_psq;
227 	int discard = 0;
228 
229 	if (psq->psq_len != 0) {
230 #ifdef IEEE80211_DEBUG
231 		struct ieee80211vap *vap = ni->ni_vap;
232 #endif
233 		struct ieee80211_psq_head *qhead;
234 		struct mbuf *m;
235 
236 		IEEE80211_PSQ_LOCK(psq);
237 		qhead = &psq->psq_head[0];
238 	again:
239 		while ((m = qhead->head) != NULL &&
240 		    M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
241 			IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
242 			     "discard frame, age %u", M_AGE_GET(m));
243 			if ((qhead->head = m->m_nextpkt) == NULL)
244 				qhead->tail = NULL;
245 			KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
246 			qhead->len--;
247 			KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
248 			psq->psq_len--;
249 			psq_mfree(m);
250 			discard++;
251 		}
252 		if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
253 			qhead = &psq->psq_head[1];
254 			goto again;
255 		}
256 		if (m != NULL)
257 			M_AGE_SUB(m, IEEE80211_INACT_WAIT);
258 		IEEE80211_PSQ_UNLOCK(psq);
259 
260 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
261 		    "discard %u frames for age", discard);
262 		IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
263 	}
264 	return discard;
265 }
266 
267 /*
268  * Handle a change in the PS station occupancy.
269  */
270 static void
271 ieee80211_update_ps(struct ieee80211vap *vap, int nsta)
272 {
273 
274 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
275 		vap->iv_opmode == IEEE80211_M_IBSS,
276 		("operating mode %u", vap->iv_opmode));
277 }
278 
279 /*
280  * Indicate whether there are frames queued for a station in power-save mode.
281  */
282 static int
283 ieee80211_set_tim(struct ieee80211_node *ni, int set)
284 {
285 	struct ieee80211vap *vap = ni->ni_vap;
286 	struct ieee80211com *ic = ni->ni_ic;
287 	uint16_t aid;
288 	int changed;
289 
290 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
291 		vap->iv_opmode == IEEE80211_M_IBSS,
292 		("operating mode %u", vap->iv_opmode));
293 
294 	aid = IEEE80211_AID(ni->ni_associd);
295 	KASSERT(aid < vap->iv_max_aid,
296 		("bogus aid %u, max %u", aid, vap->iv_max_aid));
297 
298 	IEEE80211_LOCK(ic);
299 	changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0));
300 	if (changed) {
301 		if (set) {
302 			setbit(vap->iv_tim_bitmap, aid);
303 			vap->iv_ps_pending++;
304 		} else {
305 			clrbit(vap->iv_tim_bitmap, aid);
306 			vap->iv_ps_pending--;
307 		}
308 		/* NB: we know vap is in RUN state so no need to check */
309 		vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM);
310 	}
311 	IEEE80211_UNLOCK(ic);
312 
313 	return changed;
314 }
315 
316 /*
317  * Save an outbound packet for a node in power-save sleep state.
318  * The new packet is placed on the node's saved queue, and the TIM
319  * is changed, if necessary.
320  */
321 int
322 ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
323 {
324 	struct ieee80211_psq *psq = &ni->ni_psq;
325 	struct ieee80211vap *vap = ni->ni_vap;
326 	struct ieee80211com *ic = ni->ni_ic;
327 	struct ieee80211_psq_head *qhead;
328 	int qlen, age;
329 
330 	IEEE80211_PSQ_LOCK(psq);
331 	if (psq->psq_len >= psq->psq_maxlen) {
332 		psq->psq_drops++;
333 		IEEE80211_PSQ_UNLOCK(psq);
334 		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
335 		    "pwr save q overflow, drops %d (size %d)",
336 		    psq->psq_drops, psq->psq_len);
337 #ifdef IEEE80211_DEBUG
338 		if (ieee80211_msg_dumppkts(vap))
339 			ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t),
340 			    m->m_len, -1, -1);
341 #endif
342 		psq_mfree(m);
343 		return ENOSPC;
344 	}
345 	/*
346 	 * Tag the frame with it's expiry time and insert it in
347 	 * the appropriate queue.  The aging interval is 4 times
348 	 * the listen interval specified by the station. Frames
349 	 * that sit around too long are reclaimed using this
350 	 * information.
351 	 */
352 	/* TU -> secs.  XXX handle overflow? */
353 	age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
354 	/*
355 	 * Encapsulated frames go on the high priority queue,
356 	 * other stuff goes on the low priority queue.  We use
357 	 * this to order frames returned out of the driver
358 	 * ahead of frames we collect in ieee80211_start.
359 	 */
360 	if (m->m_flags & M_ENCAP)
361 		qhead = &psq->psq_head[0];
362 	else
363 		qhead = &psq->psq_head[1];
364 	if (qhead->tail == NULL) {
365 		struct mbuf *mh;
366 
367 		qhead->head = m;
368 		/*
369 		 * Take care to adjust age when inserting the first
370 		 * frame of a queue and the other queue already has
371 		 * frames.  We need to preserve the age difference
372 		 * relationship so ieee80211_node_psq_age works.
373 		 */
374 		if (qhead == &psq->psq_head[1]) {
375 			mh = psq->psq_head[0].head;
376 			if (mh != NULL)
377 				age-= M_AGE_GET(mh);
378 		} else {
379 			mh = psq->psq_head[1].head;
380 			if (mh != NULL) {
381 				int nage = M_AGE_GET(mh) - age;
382 				/* XXX is clamping to zero good 'nuf? */
383 				M_AGE_SET(mh, nage < 0 ? 0 : nage);
384 			}
385 		}
386 	} else {
387 		qhead->tail->m_nextpkt = m;
388 		age -= M_AGE_GET(qhead->head);
389 	}
390 	KASSERT(age >= 0, ("age %d", age));
391 	M_AGE_SET(m, age);
392 	m->m_nextpkt = NULL;
393 	qhead->tail = m;
394 	qhead->len++;
395 	qlen = ++(psq->psq_len);
396 	IEEE80211_PSQ_UNLOCK(psq);
397 
398 	IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
399 	    "save frame with age %d, %u now queued", age, qlen);
400 
401 	if (qlen == 1 && vap->iv_set_tim != NULL)
402 		vap->iv_set_tim(ni, 1);
403 
404 	return 0;
405 }
406 
407 /*
408  * Move frames from the ps q to the vap's send queue
409  * and/or the driver's send queue; and kick the start
410  * method for each, as appropriate.  Note we're careful
411  * to preserve packet ordering here.
412  */
413 static void
414 pwrsave_flushq(struct ieee80211_node *ni)
415 {
416 	struct ieee80211_psq *psq = &ni->ni_psq;
417 	struct ieee80211com *ic = ni->ni_ic;
418 	struct ieee80211vap *vap = ni->ni_vap;
419 	struct ieee80211_psq_head *qhead;
420 	struct ifnet *parent, *ifp;
421 	struct mbuf *parent_q = NULL, *ifp_q = NULL;
422 	struct mbuf *m;
423 
424 	IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
425 	    "flush ps queue, %u packets queued", psq->psq_len);
426 
427 	IEEE80211_PSQ_LOCK(psq);
428 	qhead = &psq->psq_head[0];	/* 802.11 frames */
429 	if (qhead->head != NULL) {
430 		/* XXX could dispatch through vap and check M_ENCAP */
431 		parent = vap->iv_ic->ic_ifp;
432 		/* XXX need different driver interface */
433 		/* XXX bypasses q max and OACTIVE */
434 		parent_q = qhead->head;
435 		qhead->head = qhead->tail = NULL;
436 		qhead->len = 0;
437 	} else
438 		parent = NULL;
439 
440 	qhead = &psq->psq_head[1];	/* 802.3 frames */
441 	if (qhead->head != NULL) {
442 		ifp = vap->iv_ifp;
443 		/* XXX need different driver interface */
444 		/* XXX bypasses q max and OACTIVE */
445 		ifp_q = qhead->head;
446 		qhead->head = qhead->tail = NULL;
447 		qhead->len = 0;
448 	} else
449 		ifp = NULL;
450 	psq->psq_len = 0;
451 	IEEE80211_PSQ_UNLOCK(psq);
452 
453 	/* NB: do this outside the psq lock */
454 	/* XXX packets might get reordered if parent is OACTIVE */
455 	/* parent frames, should be encapsulated */
456 	if (parent != NULL) {
457 		while (parent_q != NULL) {
458 			m = parent_q;
459 			parent_q = m->m_nextpkt;
460 			m->m_nextpkt = NULL;
461 			/* must be encapsulated */
462 			KASSERT((m->m_flags & M_ENCAP),
463 			    ("%s: parentq with non-M_ENCAP frame!\n",
464 			    __func__));
465 			/*
466 			 * For encaped frames, we need to free the node
467 			 * reference upon failure.
468 			 */
469 			if (ieee80211_parent_xmitpkt(ic, m) != 0)
470 				ieee80211_free_node(ni);
471 		}
472 	}
473 
474 	/* VAP frames, aren't encapsulated */
475 	if (ifp != NULL) {
476 		while (ifp_q != NULL) {
477 			m = ifp_q;
478 			ifp_q = m->m_nextpkt;
479 			m->m_nextpkt = NULL;
480 			KASSERT((!(m->m_flags & M_ENCAP)),
481 			    ("%s: vapq with M_ENCAP frame!\n", __func__));
482 			(void) ieee80211_vap_xmitpkt(vap, m);
483 		}
484 	}
485 }
486 
487 /*
488  * Handle station power-save state change.
489  */
490 void
491 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
492 {
493 	struct ieee80211vap *vap = ni->ni_vap;
494 	int update;
495 
496 	update = 0;
497 	if (enable) {
498 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
499 			vap->iv_ps_sta++;
500 			update = 1;
501 		}
502 		ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
503 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
504 		    "power save mode on, %u sta's in ps mode", vap->iv_ps_sta);
505 
506 		if (update)
507 			vap->iv_update_ps(vap, vap->iv_ps_sta);
508 	} else {
509 		if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
510 			vap->iv_ps_sta--;
511 			update = 1;
512 		}
513 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
514 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
515 		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
516 
517 		/* NB: order here is intentional so TIM is clear before flush */
518 		if (vap->iv_set_tim != NULL)
519 			vap->iv_set_tim(ni, 0);
520 		if (update) {
521 			/* NB if no sta's in ps, driver should flush mc q */
522 			vap->iv_update_ps(vap, vap->iv_ps_sta);
523 		}
524 		if (ni->ni_psq.psq_len != 0)
525 			pwrsave_flushq(ni);
526 	}
527 }
528 
529 /*
530  * Handle power-save state change in station mode.
531  */
532 void
533 ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
534 {
535 	struct ieee80211_node *ni = vap->iv_bss;
536 
537 	if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
538 		return;
539 
540 	IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
541 	    "sta power save mode %s", enable ? "on" : "off");
542 	if (!enable) {
543 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
544 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
545 		/*
546 		 * Flush any queued frames; we can do this immediately
547 		 * because we know they'll be queued behind the null
548 		 * data frame we send the ap.
549 		 * XXX can we use a data frame to take us out of ps?
550 		 */
551 		if (ni->ni_psq.psq_len != 0)
552 			pwrsave_flushq(ni);
553 	} else {
554 		ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
555 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
556 	}
557 }
558 
559 /*
560  * Handle being notified that we have data available for us in a TIM/ATIM.
561  *
562  * This may schedule a transition from _SLEEP -> _RUN if it's appropriate.
563  */
564 void
565 ieee80211_sta_tim_notify(struct ieee80211vap *vap, int set)
566 {
567 	/*
568 	 * Schedule the driver state change.  It'll happen at some point soon.
569 	 * Since the hardware shouldn't know that we're running just yet
570 	 * (and thus tell the peer that we're awake before we actually wake
571 	 * up said hardware), we leave the actual node state transition
572 	 * up to the transition to RUN.
573 	 *
574 	 * XXX TODO: verify that the transition to RUN will wake up the
575 	 * BSS node!
576 	 */
577 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER, "%s: TIM=%d\n", __func__, set);
578 	IEEE80211_LOCK(vap->iv_ic);
579 	if (set == 1 && vap->iv_state == IEEE80211_S_SLEEP) {
580 		ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
581 	}
582 	IEEE80211_UNLOCK(vap->iv_ic);
583 }
584 
585 /*
586  * Timer check on whether the VAP has had any transmit activity.
587  *
588  * This may schedule a transition from _RUN -> _SLEEP if it's appropriate.
589  */
590 void
591 ieee80211_sta_ps_timer_check(struct ieee80211vap *vap)
592 {
593 	struct ieee80211com *ic = vap->iv_ic;
594 
595 	/* XXX lock assert */
596 
597 	/* For no, only do this in STA mode */
598 	if (! (vap->iv_caps & IEEE80211_C_SWSLEEP))
599 		goto out;
600 
601 	if (vap->iv_opmode != IEEE80211_M_STA)
602 		goto out;
603 
604 	/* If we're not at run state, bail */
605 	if (vap->iv_state != IEEE80211_S_RUN)
606 		goto out;
607 
608 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
609 	    "%s: lastdata=%llu, ticks=%llu\n",
610 	    __func__, (unsigned long long) ic->ic_lastdata,
611 	    (unsigned long long) ticks);
612 
613 	/* If powersave is disabled on the VAP, don't bother */
614 	if (! (vap->iv_flags & IEEE80211_F_PMGTON))
615 		goto out;
616 
617 	/* If we've done any data within our idle interval, bail */
618 	/* XXX hard-coded to one second for now, ew! */
619 	if (time_after(ic->ic_lastdata + 500, ticks))
620 		goto out;
621 
622 	/*
623 	 * Signify we're going into power save and transition the
624 	 * node to powersave.
625 	 */
626 	if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
627 		vap->iv_sta_ps(vap, 1);
628 
629 	/*
630 	 * XXX The driver has to handle the fact that we're going
631 	 * to sleep but frames may still be transmitted;
632 	 * hopefully it and/or us will do the right thing and mark any
633 	 * transmitted frames with PWRMGT set to 1.
634 	 */
635 	ieee80211_new_state_locked(vap, IEEE80211_S_SLEEP, 0);
636 
637 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
638 	    "%s: time delta=%d msec\n", __func__,
639 	    (int) ticks_to_msecs(ticks - ic->ic_lastdata));
640 
641 out:
642 	return;
643 }
644