apm.c revision 54017
155682Smarkm/*
2233294Sstas * APM (Advanced Power Management) BIOS Device Driver
355682Smarkm *
455682Smarkm * Copyright (c) 1994 UKAI, Fumitoshi.
5233294Sstas * Copyright (c) 1994-1995 by HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
655682Smarkm * Copyright (c) 1996 Nate Williams <nate@FreeBSD.org>
755682Smarkm * Copyright (c) 1997 Poul-Henning Kamp <phk@FreeBSD.org>
855682Smarkm *
9233294Sstas * This software may be used, modified, copied, and distributed, in
1055682Smarkm * both source and binary form provided that the above copyright and
1155682Smarkm * these terms are retained. Under no circumstances is the author
12233294Sstas * responsible for the proper functioning of this software, nor does
1355682Smarkm * the author assume any responsibility for damages incurred with its
1455682Smarkm * use.
1555682Smarkm *
16233294Sstas * Sep, 1994	Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
1755682Smarkm *
1855682Smarkm * $FreeBSD: head/sys/i386/bios/apm.c 54017 1999-12-02 03:13:11Z jlemon $
1955682Smarkm */
20233294Sstas
2155682Smarkm#include <sys/param.h>
2255682Smarkm#include <sys/systm.h>
2355682Smarkm#include <sys/eventhandler.h>
2455682Smarkm#include <sys/conf.h>
2555682Smarkm#include <sys/kernel.h>
2655682Smarkm#include <sys/time.h>
2755682Smarkm#include <sys/reboot.h>
2855682Smarkm#include <sys/bus.h>
2955682Smarkm#include <sys/select.h>
3055682Smarkm#include <sys/poll.h>
3155682Smarkm#include <sys/fcntl.h>
3255682Smarkm#include <sys/proc.h>
3355682Smarkm#include <sys/uio.h>
3455682Smarkm#include <sys/signalvar.h>
3555682Smarkm#include <sys/sysctl.h>
3655682Smarkm#include <machine/apm_bios.h>
3755682Smarkm#include <machine/segments.h>
38233294Sstas#include <machine/clock.h>
3955682Smarkm#include <vm/vm.h>
4055682Smarkm#include <vm/vm_param.h>
4155682Smarkm#include <vm/pmap.h>
4255682Smarkm#include <sys/syslog.h>
4355682Smarkm
4455682Smarkm#include <machine/pc/bios.h>
4555682Smarkm#include <machine/vm86.h>
4655682Smarkm
4755682Smarkm#include <i386/apm/apm.h>
4855682Smarkm
4955682Smarkm/* Used by the apm_saver screen saver module */
5055682Smarkmint apm_display __P((int newstate));
5155682Smarkmstruct apm_softc apm_softc;
5255682Smarkm
5355682Smarkmstatic void apm_resume __P((void));
5455682Smarkmstatic int apm_bioscall(void);
5555682Smarkmstatic int apm_check_function_supported __P((u_int version, u_int func));
5655682Smarkm
5755682Smarkmstatic u_long	apm_version;
5855682Smarkm
5955682Smarkmint	apm_evindex;
6055682Smarkm
6155682Smarkm#define	SCFLAG_ONORMAL	0x0000001
62102644Snectar#define	SCFLAG_OCTL	0x0000002
6355682Smarkm#define	SCFLAG_OPEN	(SCFLAG_ONORMAL|SCFLAG_OCTL)
64178825Sdfr
65178825Sdfr#define APMDEV(dev)	(minor(dev)&0x0f)
66178825Sdfr#define APMDEV_NORMAL	0
67178825Sdfr#define APMDEV_CTL	8
68102644Snectar
69102644Snectarstatic struct apmhook	*hook[NAPM_HOOK];		/* XXX */
70102644Snectar
71178825Sdfr#define is_enabled(foo) ((foo) ? "enabled" : "disabled")
72178825Sdfr
73178825Sdfr/* Map version number to integer (keeps ordering of version numbers) */
74102644Snectar#define INTVERSION(major, minor)	((major)*100 + (minor))
75102644Snectar
76102644Snectarstatic struct callout_handle apm_timeout_ch =
77178825Sdfr    CALLOUT_HANDLE_INITIALIZER(&apm_timeout_ch);
78178825Sdfr
79178825Sdfrstatic timeout_t apm_timeout;
80102644Snectarstatic d_open_t apmopen;
81102644Snectarstatic d_close_t apmclose;
82102644Snectarstatic d_write_t apmwrite;
83102644Snectarstatic d_ioctl_t apmioctl;
84102644Snectarstatic d_poll_t apmpoll;
85102644Snectar
86102644Snectar#define CDEV_MAJOR 39
87102644Snectarstatic struct cdevsw apm_cdevsw = {
88102644Snectar	/* open */	apmopen,
89102644Snectar	/* close */	apmclose,
90102644Snectar	/* read */	noread,
91102644Snectar	/* write */	apmwrite,
92102644Snectar	/* ioctl */	apmioctl,
9355682Smarkm	/* poll */	apmpoll,
9455682Smarkm	/* mmap */	nommap,
9555682Smarkm	/* strategy */	nostrategy,
9655682Smarkm	/* name */	"apm",
9755682Smarkm	/* maj */	CDEV_MAJOR,
9855682Smarkm	/* dump */	nodump,
9955682Smarkm	/* psize */	nopsize,
100178825Sdfr	/* flags */	0,
101178825Sdfr	/* bmaj */	-1
102178825Sdfr};
10355682Smarkm
104178825Sdfrstatic int apm_suspend_delay = 1;
105178825Sdfrstatic int apm_standby_delay = 1;
106233294Sstas
107178825SdfrSYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
10855682SmarkmSYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
109178825Sdfr
110178825Sdfr/*
111178825Sdfr * return  0 if the function successfull,
112178825Sdfr * return  1 if the function unsuccessfull,
113178825Sdfr * return -1 if the function unsupported.
114178825Sdfr */
115178825Sdfrstatic int
116178825Sdfrapm_bioscall(void)
117178825Sdfr{
118102644Snectar	struct apm_softc *sc = &apm_softc;
119178825Sdfr	int errno = 0;
120178825Sdfr	u_int apm_func = sc->bios.r.eax & 0xff;
121102644Snectar
12255682Smarkm	if (!apm_check_function_supported(sc->intversion, apm_func)) {
123178825Sdfr#ifdef APM_DEBUG
124178825Sdfr		printf("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
125178825Sdfr			apm_func, sc->majorversion, sc->minorversion);
126178825Sdfr#endif
127178825Sdfr		return (-1);
12855682Smarkm	}
129178825Sdfr
130178825Sdfr	sc->bios_busy = 1;
131178825Sdfr	if (sc->connectmode == APM_PROT32CONNECT) {
132178825Sdfr		set_bios_selectors(&sc->bios.seg,
133233294Sstas				   BIOSCODE_FLAG | BIOSDATA_FLAG);
134178825Sdfr		errno = bios32(&sc->bios.r,
135178825Sdfr			       sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
136178825Sdfr	} else {
137178825Sdfr		errno = bios16(&sc->bios, NULL);
138178825Sdfr	}
139178825Sdfr	sc->bios_busy = 0;
140178825Sdfr	return (errno);
141233294Sstas}
142178825Sdfr
143178825Sdfr/* check whether APM function is supported (1)  or not (0). */
144178825Sdfrstatic int
145178825Sdfrapm_check_function_supported(u_int version, u_int func)
146178825Sdfr{
147178825Sdfr	/* except driver version */
148178825Sdfr	if (func == APM_DRVVERSION) {
149178825Sdfr		return (1);
150178825Sdfr	}
15155682Smarkm
15255682Smarkm	switch (version) {
153178825Sdfr	case INTVERSION(1, 0):
154178825Sdfr		if (func > APM_GETPMEVENT) {
155178825Sdfr			return (0); /* not supported */
156178825Sdfr		}
157178825Sdfr		break;
158178825Sdfr	case INTVERSION(1, 1):
159178825Sdfr		if (func > APM_ENGAGEDISENGAGEPM &&
16055682Smarkm		    func < APM_OEMFUNC) {
161178825Sdfr			return (0); /* not supported */
162178825Sdfr		}
163178825Sdfr		break;
164178825Sdfr	case INTVERSION(1, 2):
165178825Sdfr		break;
166178825Sdfr	}
167233294Sstas
168178825Sdfr	return (1); /* supported */
169178825Sdfr}
170178825Sdfr
171178825Sdfr/* enable/disable power management */
172233294Sstasstatic int
173233294Sstasapm_enable_disable_pm(int enable)
174178825Sdfr{
175178825Sdfr	struct apm_softc *sc = &apm_softc;
176178825Sdfr
177178825Sdfr	sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
17855682Smarkm
179178825Sdfr	if (sc->intversion >= INTVERSION(1, 1))
180178825Sdfr		sc->bios.r.ebx  = PMDV_ALLDEV;
181233294Sstas	else
182178825Sdfr		sc->bios.r.ebx  = 0xffff;	/* APM version 1.0 only */
183178825Sdfr	sc->bios.r.ecx  = enable;
184178825Sdfr	sc->bios.r.edx = 0;
185178825Sdfr	return (apm_bioscall());
186178825Sdfr}
18755682Smarkm
188178825Sdfr/* register driver version (APM 1.1 or later) */
189178825Sdfrstatic int
190233294Sstasapm_driver_version(int version)
191178825Sdfr{
192178825Sdfr	struct apm_softc *sc = &apm_softc;
19355682Smarkm
194178825Sdfr	sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
19555682Smarkm	sc->bios.r.ebx  = 0x0;
196178825Sdfr	sc->bios.r.ecx  = version;
197178825Sdfr	sc->bios.r.edx = 0;
19855682Smarkm
199178825Sdfr	if (apm_bioscall() == 0 && sc->bios.r.eax == version)
200178825Sdfr		return (0);
201178825Sdfr
202178825Sdfr	/* Some old BIOSes don't return the connection version in %ax. */
203233294Sstas	if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
20455682Smarkm		return (0);
205178825Sdfr
206178825Sdfr	return (1);
207178825Sdfr}
208178825Sdfr
209178825Sdfr/* engage/disengage power management (APM 1.1 or later) */
210178825Sdfrstatic int
211178825Sdfrapm_engage_disengage_pm(int engage)
212178825Sdfr{
213178825Sdfr	struct apm_softc *sc = &apm_softc;
214178825Sdfr
21555682Smarkm	sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
216	sc->bios.r.ebx = PMDV_ALLDEV;
217	sc->bios.r.ecx = engage;
218	sc->bios.r.edx = 0;
219	return (apm_bioscall());
220}
221
222/* get PM event */
223static u_int
224apm_getevent(void)
225{
226	struct apm_softc *sc = &apm_softc;
227
228	sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
229
230	sc->bios.r.ebx = 0;
231	sc->bios.r.ecx = 0;
232	sc->bios.r.edx = 0;
233	if (apm_bioscall())
234		return (PMEV_NOEVENT);
235	return (sc->bios.r.ebx & 0xffff);
236}
237
238/* suspend entire system */
239static int
240apm_suspend_system(int state)
241{
242	struct apm_softc *sc = &apm_softc;
243
244	sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
245	sc->bios.r.ebx = PMDV_ALLDEV;
246	sc->bios.r.ecx = state;
247	sc->bios.r.edx = 0;
248
249	if (apm_bioscall()) {
250 		printf("Entire system suspend failure: errcode = %d\n",
251		       0xff & (sc->bios.r.eax >> 8));
252 		return 1;
253 	}
254 	return 0;
255}
256
257/* Display control */
258/*
259 * Experimental implementation: My laptop machine can't handle this function
260 * If your laptop can control the display via APM, please inform me.
261 *                            HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
262 */
263int
264apm_display(int newstate)
265{
266	struct apm_softc *sc = &apm_softc;
267
268	sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
269	sc->bios.r.ebx = PMDV_DISP0;
270	sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
271	sc->bios.r.edx = 0;
272	if (apm_bioscall()) {
273 		printf("Display off failure: errcode = %d\n",
274		       0xff & (sc->bios.r.eax >> 8));
275 		return 1;
276 	}
277 	return 0;
278}
279
280/*
281 * Turn off the entire system.
282 */
283static void
284apm_power_off(void *junk, int howto)
285{
286	struct apm_softc *sc = &apm_softc;
287
288	/* Not halting powering off, or not active */
289	if (!(howto & RB_POWEROFF) || !apm_softc.active)
290		return;
291	sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
292	sc->bios.r.ebx = PMDV_ALLDEV;
293	sc->bios.r.ecx = PMST_OFF;
294	sc->bios.r.edx = 0;
295	(void) apm_bioscall();
296}
297
298/* APM Battery low handler */
299static void
300apm_battery_low(void)
301{
302	printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
303}
304
305/* APM hook manager */
306static struct apmhook *
307apm_add_hook(struct apmhook **list, struct apmhook *ah)
308{
309	int s;
310	struct apmhook *p, *prev;
311
312#ifdef APM_DEBUG
313	printf("Add hook \"%s\"\n", ah->ah_name);
314#endif
315
316	s = splhigh();
317	if (ah == NULL)
318		panic("illegal apm_hook!");
319	prev = NULL;
320	for (p = *list; p != NULL; prev = p, p = p->ah_next)
321		if (p->ah_order > ah->ah_order)
322			break;
323
324	if (prev == NULL) {
325		ah->ah_next = *list;
326		*list = ah;
327	} else {
328		ah->ah_next = prev->ah_next;
329		prev->ah_next = ah;
330	}
331	splx(s);
332	return ah;
333}
334
335static void
336apm_del_hook(struct apmhook **list, struct apmhook *ah)
337{
338	int s;
339	struct apmhook *p, *prev;
340
341	s = splhigh();
342	prev = NULL;
343	for (p = *list; p != NULL; prev = p, p = p->ah_next)
344		if (p == ah)
345			goto deleteit;
346	panic("Tried to delete unregistered apm_hook.");
347	goto nosuchnode;
348deleteit:
349	if (prev != NULL)
350		prev->ah_next = p->ah_next;
351	else
352		*list = p->ah_next;
353nosuchnode:
354	splx(s);
355}
356
357
358/* APM driver calls some functions automatically */
359static void
360apm_execute_hook(struct apmhook *list)
361{
362	struct apmhook *p;
363
364	for (p = list; p != NULL; p = p->ah_next) {
365#ifdef APM_DEBUG
366		printf("Execute APM hook \"%s.\"\n", p->ah_name);
367#endif
368		if ((*(p->ah_fun))(p->ah_arg))
369			printf("Warning: APM hook \"%s\" failed", p->ah_name);
370	}
371}
372
373
374/* establish an apm hook */
375struct apmhook *
376apm_hook_establish(int apmh, struct apmhook *ah)
377{
378	if (apmh < 0 || apmh >= NAPM_HOOK)
379		return NULL;
380
381	return apm_add_hook(&hook[apmh], ah);
382}
383
384/* disestablish an apm hook */
385void
386apm_hook_disestablish(int apmh, struct apmhook *ah)
387{
388	if (apmh < 0 || apmh >= NAPM_HOOK)
389		return;
390
391	apm_del_hook(&hook[apmh], ah);
392}
393
394
395static struct timeval suspend_time;
396static struct timeval diff_time;
397
398static int
399apm_default_resume(void *arg)
400{
401	int pl;
402	u_int second, minute, hour;
403	struct timeval resume_time, tmp_time;
404
405	/* modified for adjkerntz */
406	pl = splsoftclock();
407	i8254_restore();		/* restore timer_freq and hz */
408	inittodr(0);			/* adjust time to RTC */
409	microtime(&resume_time);
410	getmicrotime(&tmp_time);
411	timevaladd(&tmp_time, &diff_time);
412
413#ifdef FIXME
414	/* XXX THIS DOESN'T WORK!!! */
415	time = tmp_time;
416#endif
417
418#ifdef APM_FIXUP_CALLTODO
419	/* Calculate the delta time suspended */
420	timevalsub(&resume_time, &suspend_time);
421	/* Fixup the calltodo list with the delta time. */
422	adjust_timeout_calltodo(&resume_time);
423#endif /* APM_FIXUP_CALLTODOK */
424	splx(pl);
425#ifndef APM_FIXUP_CALLTODO
426	second = resume_time.tv_sec - suspend_time.tv_sec;
427#else /* APM_FIXUP_CALLTODO */
428	/*
429	 * We've already calculated resume_time to be the delta between
430	 * the suspend and the resume.
431	 */
432	second = resume_time.tv_sec;
433#endif /* APM_FIXUP_CALLTODO */
434	hour = second / 3600;
435	second %= 3600;
436	minute = second / 60;
437	second %= 60;
438	log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
439		hour, minute, second);
440	return 0;
441}
442
443static int
444apm_default_suspend(void *arg)
445{
446	int	pl;
447
448	pl = splsoftclock();
449	microtime(&diff_time);
450	inittodr(0);
451	microtime(&suspend_time);
452	timevalsub(&diff_time, &suspend_time);
453	splx(pl);
454	return 0;
455}
456
457static int apm_record_event __P((struct apm_softc *, u_int));
458static void apm_processevent(void);
459
460static u_int apm_op_inprog = 0;
461
462static void
463apm_do_suspend(void)
464{
465	struct apm_softc *sc = &apm_softc;
466	int error;
467
468	if (!sc)
469		return;
470
471	apm_op_inprog = 0;
472	sc->suspends = sc->suspend_countdown = 0;
473
474	if (sc->initialized) {
475		error = DEVICE_SUSPEND(root_bus);
476		/*
477		 * XXX Shouldn't ignore the error like this, but should
478		 * instead fix the newbus code.  Until that happens,
479		 * I'm doing this to get suspend working again.
480		 */
481		if (error)
482			printf("DEVICE_SUSPEND error %d, ignored\n", error);
483		apm_execute_hook(hook[APM_HOOK_SUSPEND]);
484		if (apm_suspend_system(PMST_SUSPEND) == 0)
485			apm_processevent();
486		else
487			/* Failure, 'resume' the system again */
488			apm_execute_hook(hook[APM_HOOK_RESUME]);
489	}
490}
491
492static void
493apm_do_standby(void)
494{
495	struct apm_softc *sc = &apm_softc;
496
497	if (!sc)
498		return;
499
500	apm_op_inprog = 0;
501	sc->standbys = sc->standby_countdown = 0;
502
503	if (sc->initialized) {
504		/*
505		 * As far as standby, we don't need to execute
506		 * all of suspend hooks.
507		 */
508		apm_default_suspend(&apm_softc);
509		if (apm_suspend_system(PMST_STANDBY) == 0)
510			apm_processevent();
511	}
512}
513
514static void
515apm_lastreq_notify(void)
516{
517	struct apm_softc *sc = &apm_softc;
518
519	sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
520	sc->bios.r.ebx = PMDV_ALLDEV;
521	sc->bios.r.ecx = PMST_LASTREQNOTIFY;
522	sc->bios.r.edx = 0;
523	apm_bioscall();
524}
525
526static int
527apm_lastreq_rejected(void)
528{
529	struct apm_softc *sc = &apm_softc;
530
531	if (apm_op_inprog == 0) {
532		return 1;	/* no operation in progress */
533	}
534
535	sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
536	sc->bios.r.ebx = PMDV_ALLDEV;
537	sc->bios.r.ecx = PMST_LASTREQREJECT;
538	sc->bios.r.edx = 0;
539
540	if (apm_bioscall()) {
541#ifdef APM_DEBUG
542		printf("apm_lastreq_rejected: failed\n");
543#endif
544		return 1;
545	}
546	apm_op_inprog = 0;
547	return 0;
548}
549
550/*
551 * Public interface to the suspend/resume:
552 *
553 * Execute suspend and resume hook before and after sleep, respectively.
554 *
555 */
556
557void
558apm_suspend(int state)
559{
560	struct apm_softc *sc = &apm_softc;
561
562	if (!sc->initialized)
563		return;
564
565	switch (state) {
566	case PMST_SUSPEND:
567		if (sc->suspends)
568			return;
569		sc->suspends++;
570		sc->suspend_countdown = apm_suspend_delay;
571		break;
572	case PMST_STANDBY:
573		if (sc->standbys)
574			return;
575		sc->standbys++;
576		sc->standby_countdown = apm_standby_delay;
577		break;
578	default:
579		printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
580		return;
581	}
582
583	apm_op_inprog++;
584	apm_lastreq_notify();
585}
586
587void
588apm_resume(void)
589{
590	struct apm_softc *sc = &apm_softc;
591
592	if (!sc)
593		return;
594
595	if (sc->initialized) {
596		DEVICE_RESUME(root_bus);
597		apm_execute_hook(hook[APM_HOOK_RESUME]);
598	}
599}
600
601
602/* get APM information */
603static int
604apm_get_info(apm_info_t aip)
605{
606	struct apm_softc *sc = &apm_softc;
607
608	sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
609	sc->bios.r.ebx = PMDV_ALLDEV;
610	sc->bios.r.ecx = 0;
611	sc->bios.r.edx = 0xffff;		/* default to unknown battery time */
612
613	if (apm_bioscall())
614		return 1;
615
616	aip->ai_infoversion = 1;
617	aip->ai_acline      = (sc->bios.r.ebx >> 8) & 0xff;
618	aip->ai_batt_stat   = sc->bios.r.ebx & 0xff;
619	aip->ai_batt_life   = sc->bios.r.ecx & 0xff;
620	aip->ai_major       = (u_int)sc->majorversion;
621	aip->ai_minor       = (u_int)sc->minorversion;
622	aip->ai_status      = (u_int)sc->active;
623	sc->bios.r.edx &= 0xffff;
624	if (sc->bios.r.edx == 0xffff)	/* Time is unknown */
625		aip->ai_batt_time = -1;
626	else if (sc->bios.r.edx & 0x8000)	/* Time is in minutes */
627		aip->ai_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
628	else			/* Time is in seconds */
629		aip->ai_batt_time = sc->bios.r.edx;
630
631	sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
632	sc->bios.r.ebx = 0;
633	sc->bios.r.ecx = 0;
634	sc->bios.r.edx = 0;
635	if (apm_bioscall()) {
636		aip->ai_batteries = -1;	/* Unknown */
637		aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
638	} else {
639		aip->ai_batteries = sc->bios.r.ebx & 0xff;
640		aip->ai_capabilities = sc->bios.r.ecx & 0xf;
641	}
642
643	bzero(aip->ai_spare, sizeof aip->ai_spare);
644
645	return 0;
646}
647
648
649/* inform APM BIOS that CPU is idle */
650void
651apm_cpu_idle(void)
652{
653	struct apm_softc *sc = &apm_softc;
654
655	if (sc->active) {
656
657		sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
658		sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
659		(void) apm_bioscall();
660	}
661	/*
662	 * Some APM implementation halts CPU in BIOS, whenever
663	 * "CPU-idle" function are invoked, but swtch() of
664	 * FreeBSD halts CPU, therefore, CPU is halted twice
665	 * in the sched loop. It makes the interrupt latency
666	 * terribly long and be able to cause a serious problem
667	 * in interrupt processing. We prevent it by removing
668	 * "hlt" operation from swtch() and managed it under
669	 * APM driver.
670	 */
671	if (!sc->active || sc->always_halt_cpu)
672		__asm("hlt");	/* wait for interrupt */
673}
674
675/* inform APM BIOS that CPU is busy */
676void
677apm_cpu_busy(void)
678{
679	struct apm_softc *sc = &apm_softc;
680
681	/*
682	 * The APM specification says this is only necessary if your BIOS
683	 * slows down the processor in the idle task, otherwise it's not
684	 * necessary.
685	 */
686	if (sc->slow_idle_cpu && sc->active) {
687
688		sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
689		sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
690		apm_bioscall();
691	}
692}
693
694
695/*
696 * APM timeout routine:
697 *
698 * This routine is automatically called by timer once per second.
699 */
700
701static void
702apm_timeout(void *dummy)
703{
704	struct apm_softc *sc = &apm_softc;
705
706	if (apm_op_inprog)
707		apm_lastreq_notify();
708
709	if (sc->standbys && sc->standby_countdown-- <= 0)
710		apm_do_standby();
711
712	if (sc->suspends && sc->suspend_countdown-- <= 0)
713		apm_do_suspend();
714
715	if (!sc->bios_busy)
716		apm_processevent();
717
718	if (sc->active == 1)
719		/* Run slightly more oftan than 1 Hz */
720		apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1 );
721}
722
723/* enable APM BIOS */
724static void
725apm_event_enable(void)
726{
727	struct apm_softc *sc = &apm_softc;
728
729#ifdef APM_DEBUG
730	printf("called apm_event_enable()\n");
731#endif
732	if (sc->initialized) {
733		sc->active = 1;
734		apm_timeout(sc);
735	}
736}
737
738/* disable APM BIOS */
739static void
740apm_event_disable(void)
741{
742	struct apm_softc *sc = &apm_softc;
743
744#ifdef APM_DEBUG
745	printf("called apm_event_disable()\n");
746#endif
747	if (sc->initialized) {
748		untimeout(apm_timeout, NULL, apm_timeout_ch);
749		sc->active = 0;
750	}
751}
752
753/* halt CPU in scheduling loop */
754static void
755apm_halt_cpu(void)
756{
757	struct apm_softc *sc = &apm_softc;
758
759	if (sc->initialized)
760		sc->always_halt_cpu = 1;
761}
762
763/* don't halt CPU in scheduling loop */
764static void
765apm_not_halt_cpu(void)
766{
767	struct apm_softc *sc = &apm_softc;
768
769	if (sc->initialized)
770		sc->always_halt_cpu = 0;
771}
772
773/* device driver definitions */
774
775/*
776 * Create "connection point"
777 */
778static void
779apm_identify(driver_t *driver, device_t parent)
780{
781	device_t child;
782
783	child = BUS_ADD_CHILD(parent, 0, "apm", 0);
784	if (child == NULL)
785		panic("apm_identify");
786}
787
788/*
789 * probe for APM BIOS
790 */
791static int
792apm_probe(device_t dev)
793{
794#define APM_KERNBASE	KERNBASE
795	struct vm86frame	vmf;
796	struct apm_softc	*sc = &apm_softc;
797	int			disabled, flags;
798
799	if (resource_int_value("apm", 0, "disabled", &disabled) == 0
800	    && disabled != 0)
801		return ENXIO;
802
803	device_set_desc(dev, "APM BIOS");
804
805	if ( device_get_unit(dev) > 0 ) {
806		printf("apm: Only one APM driver supported.\n");
807		return ENXIO;
808	}
809
810	if (resource_int_value("apm", 0, "flags", &flags) != 0)
811		flags = 0;
812
813	bzero(&vmf, sizeof(struct vm86frame));		/* safety */
814	bzero(&apm_softc, sizeof(apm_softc));
815	vmf.vmf_ah = APM_BIOS;
816	vmf.vmf_al = APM_INSTCHECK;
817	vmf.vmf_bx = 0;
818	if (vm86_intcall(APM_INT, &vmf))
819		return ENXIO;			/* APM not found */
820	if (vmf.vmf_bx != 0x504d) {
821		printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
822		return ENXIO;
823	}
824	if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
825		printf("apm: protected mode connections are not supported\n");
826		return ENXIO;
827	}
828
829	apm_version = vmf.vmf_ax;
830	sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
831	sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
832	sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
833
834	vmf.vmf_ah = APM_BIOS;
835	vmf.vmf_al = APM_DISCONNECT;
836	vmf.vmf_bx = 0;
837        vm86_intcall(APM_INT, &vmf);		/* disconnect, just in case */
838
839	if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
840		vmf.vmf_ah = APM_BIOS;
841		vmf.vmf_al = APM_PROT32CONNECT;
842		vmf.vmf_bx = 0;
843		if (vm86_intcall(APM_INT, &vmf)) {
844			printf("apm: 32-bit connection error.\n");
845			return (ENXIO);
846 		}
847		sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
848		sc->bios.seg.code32.limit = 0xffff;
849		sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
850		sc->bios.seg.code16.limit = 0xffff;
851		sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
852		sc->bios.seg.data.limit = 0xffff;
853		sc->bios.entry = vmf.vmf_ebx;
854		sc->connectmode = APM_PROT32CONNECT;
855 	} else {
856		/* use 16-bit connection */
857		vmf.vmf_ah = APM_BIOS;
858		vmf.vmf_al = APM_PROT16CONNECT;
859		vmf.vmf_bx = 0;
860		if (vm86_intcall(APM_INT, &vmf)) {
861			printf("apm: 16-bit connection error.\n");
862			return (ENXIO);
863		}
864		sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
865		sc->bios.seg.code16.limit = 0xffff;
866		sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
867		sc->bios.seg.data.limit = 0xffff;
868		sc->bios.entry = vmf.vmf_bx;
869		sc->connectmode = APM_PROT16CONNECT;
870	}
871	return(0);
872}
873
874
875/*
876 * return 0 if the user will notice and handle the event,
877 * return 1 if the kernel driver should do so.
878 */
879static int
880apm_record_event(struct apm_softc *sc, u_int event_type)
881{
882	struct apm_event_info *evp;
883
884	if ((sc->sc_flags & SCFLAG_OPEN) == 0)
885		return 1;		/* no user waiting */
886	if (sc->event_count == APM_NEVENTS)
887		return 1;			/* overflow */
888	if (sc->event_filter[event_type] == 0)
889		return 1;		/* not registered */
890	evp = &sc->event_list[sc->event_ptr];
891	sc->event_count++;
892	sc->event_ptr++;
893	sc->event_ptr %= APM_NEVENTS;
894	evp->type = event_type;
895	evp->index = ++apm_evindex;
896	selwakeup(&sc->sc_rsel);
897	return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
898}
899
900/* Process APM event */
901static void
902apm_processevent(void)
903{
904	int apm_event;
905	struct apm_softc *sc = &apm_softc;
906
907#ifdef APM_DEBUG
908#  define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
909	printf("Received APM Event: " #symbol "\n");
910#else
911#  define OPMEV_DEBUGMESSAGE(symbol) case symbol:
912#endif
913	do {
914		apm_event = apm_getevent();
915		switch (apm_event) {
916		    OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
917			if (apm_op_inprog == 0) {
918			    apm_op_inprog++;
919			    if (apm_record_event(sc, apm_event)) {
920				apm_suspend(PMST_STANDBY);
921			    }
922			}
923			break;
924		    OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
925			if (apm_op_inprog == 0) {
926			    apm_op_inprog++;
927			    if (apm_record_event(sc, apm_event)) {
928				apm_suspend(PMST_STANDBY);
929			    }
930			}
931			break;
932		    OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
933 			apm_lastreq_notify();
934			if (apm_op_inprog == 0) {
935			    apm_op_inprog++;
936			    if (apm_record_event(sc, apm_event)) {
937				apm_do_suspend();
938			    }
939			}
940			return; /* XXX skip the rest */
941		    OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
942 			apm_lastreq_notify();
943			if (apm_op_inprog == 0) {
944			    apm_op_inprog++;
945			    if (apm_record_event(sc, apm_event)) {
946				apm_do_suspend();
947			    }
948			}
949			return; /* XXX skip the rest */
950		    OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
951			apm_do_suspend();
952			break;
953		    OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
954			apm_record_event(sc, apm_event);
955			apm_resume();
956			break;
957		    OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
958			apm_record_event(sc, apm_event);
959			apm_resume();
960			break;
961		    OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
962			apm_record_event(sc, apm_event);
963			apm_resume();
964			break;
965		    OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
966			if (apm_record_event(sc, apm_event)) {
967			    apm_battery_low();
968			    apm_suspend(PMST_SUSPEND);
969			}
970			break;
971		    OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
972			apm_record_event(sc, apm_event);
973			break;
974		    OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
975			apm_record_event(sc, apm_event);
976			inittodr(0);	/* adjust time to RTC */
977			break;
978		    OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
979			apm_record_event(sc, apm_event);
980			break;
981		    case PMEV_NOEVENT:
982			break;
983		    default:
984			printf("Unknown Original APM Event 0x%x\n", apm_event);
985			    break;
986		}
987	} while (apm_event != PMEV_NOEVENT);
988}
989
990/*
991 * Attach APM:
992 *
993 * Initialize APM driver
994 */
995
996static int
997apm_attach(device_t dev)
998{
999	struct apm_softc	*sc = &apm_softc;
1000	int			flags;
1001	int			drv_version;
1002
1003	if (resource_int_value("apm", 0, "flags", &flags) != 0)
1004		flags = 0;
1005
1006	if (flags & 0x20)
1007		statclock_disable = 1;
1008
1009	sc->initialized = 0;
1010
1011	/* Must be externally enabled */
1012	sc->active = 0;
1013
1014	/* Always call HLT in idle loop */
1015	sc->always_halt_cpu = 1;
1016
1017	/* print bootstrap messages */
1018#ifdef APM_DEBUG
1019	printf("apm: APM BIOS version %04x\n",  apm_version);
1020	printf("apm: Code16 0x%08x, Data 0x%08x\n",
1021               sc->bios.seg.code16.base, sc->bios.seg.data.base);
1022	printf("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1023               sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1024	       is_enabled(!sc->disabled));
1025	printf("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1026	      sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1027#endif /* APM_DEBUG */
1028
1029	/*
1030         * In one test, apm bios version was 1.02; an attempt to register
1031         * a 1.04 driver resulted in a 1.00 connection!  Registering a
1032         * 1.02 driver resulted in a 1.02 connection.
1033         */
1034	drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1035	for (; drv_version > 0x100; drv_version--)
1036		if (apm_driver_version(drv_version) == 0)
1037			break;
1038	sc->minorversion = ((drv_version & 0x00f0) >>  4) * 10 +
1039		((drv_version & 0x000f) >> 0);
1040	sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1041		((apm_version & 0x0f00) >> 8);
1042
1043	sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1044
1045#ifdef APM_DEBUG
1046	if (sc->intversion >= INTVERSION(1, 1))
1047		printf("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1048#endif
1049
1050	printf("apm: found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1051	       ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1052	       ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1053	       sc->majorversion, sc->minorversion);
1054
1055#ifdef APM_DEBUG
1056	printf("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1057#endif
1058
1059	/* enable power management */
1060	if (sc->disabled) {
1061		if (apm_enable_disable_pm(1)) {
1062#ifdef APM_DEBUG
1063			printf("apm: *Warning* enable function failed! [%x]\n",
1064				(sc->bios.r.eax >> 8) & 0xff);
1065#endif
1066		}
1067	}
1068
1069	/* engage power managment (APM 1.1 or later) */
1070	if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1071		if (apm_engage_disengage_pm(1)) {
1072#ifdef APM_DEBUG
1073			printf("apm: *Warning* engage function failed err=[%x]",
1074				(sc->bios.r.eax >> 8) & 0xff);
1075			printf(" (Docked or using external power?).\n");
1076#endif
1077		}
1078	}
1079
1080        /* default suspend hook */
1081        sc->sc_suspend.ah_fun = apm_default_suspend;
1082        sc->sc_suspend.ah_arg = sc;
1083        sc->sc_suspend.ah_name = "default suspend";
1084        sc->sc_suspend.ah_order = APM_MAX_ORDER;
1085
1086        /* default resume hook */
1087        sc->sc_resume.ah_fun = apm_default_resume;
1088        sc->sc_resume.ah_arg = sc;
1089        sc->sc_resume.ah_name = "default resume";
1090        sc->sc_resume.ah_order = APM_MIN_ORDER;
1091
1092        apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1093        apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
1094
1095	/* Power the system off using APM */
1096	EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1097			      SHUTDOWN_PRI_LAST);
1098
1099	sc->initialized = 1;
1100
1101	make_dev(&apm_cdevsw, 0, 0, 5, 0660, "apm");
1102	make_dev(&apm_cdevsw, 8, 0, 5, 0660, "apmctl");
1103	return 0;
1104}
1105
1106static int
1107apmopen(dev_t dev, int flag, int fmt, struct proc *p)
1108{
1109	struct apm_softc *sc = &apm_softc;
1110	int ctl = APMDEV(dev);
1111
1112	if (!sc->initialized)
1113		return (ENXIO);
1114
1115	switch (ctl) {
1116	case APMDEV_CTL:
1117		if (!(flag & FWRITE))
1118			return EINVAL;
1119		if (sc->sc_flags & SCFLAG_OCTL)
1120			return EBUSY;
1121		sc->sc_flags |= SCFLAG_OCTL;
1122		bzero(sc->event_filter, sizeof sc->event_filter);
1123		break;
1124	case APMDEV_NORMAL:
1125		sc->sc_flags |= SCFLAG_ONORMAL;
1126		break;
1127	default:
1128		return ENXIO;
1129		break;
1130	}
1131	return 0;
1132}
1133
1134static int
1135apmclose(dev_t dev, int flag, int fmt, struct proc *p)
1136{
1137	struct apm_softc *sc = &apm_softc;
1138	int ctl = APMDEV(dev);
1139
1140	switch (ctl) {
1141	case APMDEV_CTL:
1142		apm_lastreq_rejected();
1143		sc->sc_flags &= ~SCFLAG_OCTL;
1144		bzero(sc->event_filter, sizeof sc->event_filter);
1145		break;
1146	case APMDEV_NORMAL:
1147		sc->sc_flags &= ~SCFLAG_ONORMAL;
1148		break;
1149	}
1150	if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1151		sc->event_count = 0;
1152		sc->event_ptr = 0;
1153	}
1154	return 0;
1155}
1156
1157static int
1158apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1159{
1160	struct apm_softc *sc = &apm_softc;
1161	struct apm_bios_arg *args;
1162	int error = 0;
1163	int ret;
1164	int newstate;
1165
1166	if (!sc->initialized)
1167		return (ENXIO);
1168#ifdef APM_DEBUG
1169	printf("APM ioctl: cmd = 0x%x\n", cmd);
1170#endif
1171	switch (cmd) {
1172	case APMIO_SUSPEND:
1173		if (sc->active)
1174			apm_suspend(PMST_SUSPEND);
1175		else
1176			error = EINVAL;
1177		break;
1178
1179	case APMIO_STANDBY:
1180		if (sc->active)
1181			apm_suspend(PMST_STANDBY);
1182		else
1183			error = EINVAL;
1184		break;
1185
1186	case APMIO_GETINFO_OLD:
1187		{
1188			struct apm_info info;
1189			apm_info_old_t aiop;
1190
1191			if (apm_get_info(&info))
1192				error = ENXIO;
1193			aiop = (apm_info_old_t)addr;
1194			aiop->ai_major = info.ai_major;
1195			aiop->ai_minor = info.ai_minor;
1196			aiop->ai_acline = info.ai_acline;
1197			aiop->ai_batt_stat = info.ai_batt_stat;
1198			aiop->ai_batt_life = info.ai_batt_life;
1199			aiop->ai_status = info.ai_status;
1200		}
1201		break;
1202	case APMIO_GETINFO:
1203		if (apm_get_info((apm_info_t)addr))
1204			error = ENXIO;
1205		break;
1206	case APMIO_ENABLE:
1207		apm_event_enable();
1208		break;
1209	case APMIO_DISABLE:
1210		apm_event_disable();
1211		break;
1212	case APMIO_HALTCPU:
1213		apm_halt_cpu();
1214		break;
1215	case APMIO_NOTHALTCPU:
1216		apm_not_halt_cpu();
1217		break;
1218	case APMIO_DISPLAY:
1219		newstate = *(int *)addr;
1220		if (apm_display(newstate))
1221			error = ENXIO;
1222		break;
1223	case APMIO_BIOS:
1224		/* XXX compatibility with the old interface */
1225		args = (struct apm_bios_arg *)addr;
1226		sc->bios.r.eax = args->eax;
1227		sc->bios.r.ebx = args->ebx;
1228		sc->bios.r.ecx = args->ecx;
1229		sc->bios.r.edx = args->edx;
1230		sc->bios.r.esi = args->esi;
1231		sc->bios.r.edi = args->edi;
1232		if ((ret = apm_bioscall())) {
1233			/*
1234			 * Return code 1 means bios call was unsuccessful.
1235			 * Error code is stored in %ah.
1236			 * Return code -1 means bios call was unsupported
1237			 * in the APM BIOS version.
1238			 */
1239			if (ret == -1) {
1240				error = EINVAL;
1241			}
1242		} else {
1243			/*
1244			 * Return code 0 means bios call was successful.
1245			 * We need only %al and can discard %ah.
1246			 */
1247			sc->bios.r.eax &= 0xff;
1248		}
1249		args->eax = sc->bios.r.eax;
1250		args->ebx = sc->bios.r.ebx;
1251		args->ecx = sc->bios.r.ecx;
1252		args->edx = sc->bios.r.edx;
1253		args->esi = sc->bios.r.esi;
1254		args->edi = sc->bios.r.edi;
1255		break;
1256	default:
1257		error = EINVAL;
1258		break;
1259	}
1260
1261	/* for /dev/apmctl */
1262	if (APMDEV(dev) == APMDEV_CTL) {
1263		struct apm_event_info *evp;
1264		int i;
1265
1266		error = 0;
1267		switch (cmd) {
1268		case APMIO_NEXTEVENT:
1269			if (!sc->event_count) {
1270				error = EAGAIN;
1271			} else {
1272				evp = (struct apm_event_info *)addr;
1273				i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1274				i %= APM_NEVENTS;
1275				*evp = sc->event_list[i];
1276				sc->event_count--;
1277			}
1278			break;
1279		case APMIO_REJECTLASTREQ:
1280			if (apm_lastreq_rejected()) {
1281				error = EINVAL;
1282			}
1283			break;
1284		default:
1285			error = EINVAL;
1286			break;
1287		}
1288	}
1289
1290	return error;
1291}
1292
1293static int
1294apmwrite(dev_t dev, struct uio *uio, int ioflag)
1295{
1296	struct apm_softc *sc = &apm_softc;
1297	u_int event_type;
1298	int error;
1299	u_char enabled;
1300
1301	if (APMDEV(dev) != APMDEV_CTL)
1302		return(ENODEV);
1303	if (uio->uio_resid != sizeof(u_int))
1304		return(E2BIG);
1305
1306	if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1307		return(error);
1308
1309	if (event_type < 0 || event_type >= APM_NPMEV)
1310		return(EINVAL);
1311
1312	if (sc->event_filter[event_type] == 0) {
1313		enabled = 1;
1314	} else {
1315		enabled = 0;
1316	}
1317	sc->event_filter[event_type] = enabled;
1318#ifdef APM_DEBUG
1319	printf("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1320#endif
1321
1322	return uio->uio_resid;
1323}
1324
1325static int
1326apmpoll(dev_t dev, int events, struct proc *p)
1327{
1328	struct apm_softc *sc = &apm_softc;
1329	int revents = 0;
1330
1331	if (events & (POLLIN | POLLRDNORM)) {
1332		if (sc->event_count) {
1333			revents |= events & (POLLIN | POLLRDNORM);
1334		} else {
1335			selrecord(p, &sc->sc_rsel);
1336		}
1337	}
1338
1339	return (revents);
1340}
1341
1342static device_method_t apm_methods[] = {
1343	/* Device interface */
1344	DEVMETHOD(device_identify,	apm_identify),
1345	DEVMETHOD(device_probe,		apm_probe),
1346	DEVMETHOD(device_attach,	apm_attach),
1347
1348	{ 0, 0 }
1349};
1350
1351static driver_t apm_driver = {
1352	"apm",
1353	apm_methods,
1354	1,			/* no softc (XXX) */
1355};
1356
1357static devclass_t apm_devclass;
1358
1359DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);
1360