1 /*-
2 * Copyright (c) 2002-2003 Taku YAMAMOTO <taku@cent.saitama-u.ac.jp>
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 AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $Id: acpi_vid.c,v 1.4 2003/10/13 10:07:36 taku Exp $
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/power.h>
38 #include <sys/queue.h>
39 #include <sys/sysctl.h>
40
41 #include <contrib/dev/acpica/include/acpi.h>
42
43 #include <dev/acpica/acpivar.h>
44
45 /* ACPI video extension driver. */
46 struct acpi_video_output {
47 ACPI_HANDLE handle;
48 UINT32 adr;
49 STAILQ_ENTRY(acpi_video_output) vo_next;
50 struct {
51 int num;
52 STAILQ_ENTRY(acpi_video_output) next;
53 } vo_unit;
54 int vo_brightness;
55 int vo_fullpower;
56 int vo_economy;
57 int vo_numlevels;
58 int *vo_levels;
59 struct sysctl_ctx_list vo_sysctl_ctx;
60 struct sysctl_oid *vo_sysctl_tree;
61 };
62
63 STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);
64
65 struct acpi_video_softc {
66 device_t device;
67 ACPI_HANDLE handle;
68 struct acpi_video_output_queue vid_outputs;
69 eventhandler_tag vid_pwr_evh;
70 };
71
72 /* interfaces */
73 static int acpi_video_modevent(struct module*, int, void *);
74 static void acpi_video_identify(driver_t *driver, device_t parent);
75 static int acpi_video_probe(device_t);
76 static int acpi_video_attach(device_t);
77 static int acpi_video_detach(device_t);
78 static int acpi_video_shutdown(device_t);
79 static void acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
80 static void acpi_video_power_profile(void *);
81 static void acpi_video_bind_outputs(struct acpi_video_softc *);
82 static struct acpi_video_output *acpi_video_vo_init(UINT32);
83 static void acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
84 static void acpi_video_vo_destroy(struct acpi_video_output *);
85 static int acpi_video_vo_check_level(struct acpi_video_output *, int);
86 static void acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *);
87 static int acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
88 static int acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
89 static int acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
90 static int acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
91
92 /* operations */
93 static void vid_set_switch_policy(ACPI_HANDLE, UINT32);
94 static int vid_enum_outputs(ACPI_HANDLE,
95 void(*)(ACPI_HANDLE, UINT32, void *), void *);
96 static int vo_get_brightness_levels(ACPI_HANDLE, int **);
97 static int vo_get_brightness(ACPI_HANDLE);
98 static void vo_set_brightness(ACPI_HANDLE, int);
99 static UINT32 vo_get_device_status(ACPI_HANDLE);
100 static UINT32 vo_get_graphics_state(ACPI_HANDLE);
101 static void vo_set_device_state(ACPI_HANDLE, UINT32);
102
103 /* events */
104 #define VID_NOTIFY_SWITCHED 0x80
105 #define VID_NOTIFY_REPROBE 0x81
106 #define VID_NOTIFY_CYCLE_BRN 0x85
107 #define VID_NOTIFY_INC_BRN 0x86
108 #define VID_NOTIFY_DEC_BRN 0x87
109 #define VID_NOTIFY_ZERO_BRN 0x88
110
111 /* _DOS (Enable/Disable Output Switching) argument bits */
112 #define DOS_SWITCH_MASK 3
113 #define DOS_SWITCH_BY_OSPM 0
114 #define DOS_SWITCH_BY_BIOS 1
115 #define DOS_SWITCH_LOCKED 2
116 #define DOS_BRIGHTNESS_BY_OSPM (1 << 2)
117
118 /* _DOD and subdev's _ADR */
119 #define DOD_DEVID_MASK 0x0f00
120 #define DOD_DEVID_MASK_FULL 0xffff
121 #define DOD_DEVID_MASK_DISPIDX 0x000f
122 #define DOD_DEVID_MASK_DISPPORT 0x00f0
123 #define DOD_DEVID_MONITOR 0x0100
124 #define DOD_DEVID_LCD 0x0110
125 #define DOD_DEVID_TV 0x0200
126 #define DOD_DEVID_EXT 0x0300
127 #define DOD_DEVID_INTDFP 0x0400
128 #define DOD_BIOS (1 << 16)
129 #define DOD_NONVGA (1 << 17)
130 #define DOD_HEAD_ID_SHIFT 18
131 #define DOD_HEAD_ID_BITS 3
132 #define DOD_HEAD_ID_MASK \
133 (((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
134 #define DOD_DEVID_SCHEME_STD (1 << 31)
135
136 /* _BCL related constants */
137 #define BCL_FULLPOWER 0
138 #define BCL_ECONOMY 1
139
140 /* _DCS (Device Currrent Status) value bits and masks. */
141 #define DCS_EXISTS (1 << 0)
142 #define DCS_ACTIVE (1 << 1)
143 #define DCS_READY (1 << 2)
144 #define DCS_FUNCTIONAL (1 << 3)
145 #define DCS_ATTACHED (1 << 4)
146
147 /* _DSS (Device Set Status) argument bits and masks. */
148 #define DSS_INACTIVE 0
149 #define DSS_ACTIVE (1 << 0)
150 #define DSS_SETNEXT (1 << 30)
151 #define DSS_COMMIT (1 << 31)
152
153 static device_method_t acpi_video_methods[] = {
154 DEVMETHOD(device_identify, acpi_video_identify),
155 DEVMETHOD(device_probe, acpi_video_probe),
156 DEVMETHOD(device_attach, acpi_video_attach),
157 DEVMETHOD(device_detach, acpi_video_detach),
158 DEVMETHOD(device_shutdown, acpi_video_shutdown),
159 { 0, 0 }
160 };
161
162 static driver_t acpi_video_driver = {
163 "acpi_video",
164 acpi_video_methods,
165 sizeof(struct acpi_video_softc),
166 };
167
168 static devclass_t acpi_video_devclass;
169
170 DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass,
171 acpi_video_modevent, NULL);
172 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
173
174 static struct sysctl_ctx_list acpi_video_sysctl_ctx;
175 static struct sysctl_oid *acpi_video_sysctl_tree;
176 static struct acpi_video_output_queue crt_units, tv_units,
177 ext_units, lcd_units, other_units;
178
179 /*
180 * The 'video' lock protects the hierarchy of video output devices
181 * (the video "bus"). The 'video_output' lock protects per-output
182 * data is equivalent to a softc lock for each video output.
183 */
184 ACPI_SERIAL_DECL(video, "ACPI video");
185 ACPI_SERIAL_DECL(video_output, "ACPI video output");
186 MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
187
188 static int
189 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
190 {
191 int err;
192
193 err = 0;
194 switch (evt) {
195 case MOD_LOAD:
196 sysctl_ctx_init(&acpi_video_sysctl_ctx);
197 STAILQ_INIT(&crt_units);
198 STAILQ_INIT(&tv_units);
199 STAILQ_INIT(&ext_units);
200 STAILQ_INIT(&lcd_units);
201 STAILQ_INIT(&other_units);
202 break;
203 case MOD_UNLOAD:
204 sysctl_ctx_free(&acpi_video_sysctl_ctx);
205 acpi_video_sysctl_tree = NULL;
206 break;
207 default:
208 err = EINVAL;
209 }
210
211 return (err);
212 }
213
214 static void
215 acpi_video_identify(driver_t *driver, device_t parent)
216 {
217
218 if (device_find_child(parent, "acpi_video", -1) == NULL)
219 device_add_child(parent, "acpi_video", -1);
220 }
221
222 static int
223 acpi_video_probe(device_t dev)
224 {
225 ACPI_HANDLE devh, h;
226 ACPI_OBJECT_TYPE t_dos;
227
228 devh = acpi_get_handle(dev);
229 if (acpi_disabled("video") ||
230 ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
231 ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) ||
232 ACPI_FAILURE(AcpiGetType(h, &t_dos)) ||
233 t_dos != ACPI_TYPE_METHOD)
234 return (ENXIO);
235
236 device_set_desc(dev, "ACPI video extension");
237 return (0);
238 }
239
240 static int
241 acpi_video_attach(device_t dev)
242 {
243 struct acpi_softc *acpi_sc;
244 struct acpi_video_softc *sc;
245
246 sc = device_get_softc(dev);
247
248 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
249 if (acpi_sc == NULL)
250 return (ENXIO);
251 ACPI_SERIAL_BEGIN(video);
252 if (acpi_video_sysctl_tree == NULL) {
253 acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx,
254 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
255 OID_AUTO, "video", CTLFLAG_RD, 0,
256 "video extension control");
257 }
258 ACPI_SERIAL_END(video);
259
260 sc->device = dev;
261 sc->handle = acpi_get_handle(dev);
262 STAILQ_INIT(&sc->vid_outputs);
263
264 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
265 acpi_video_notify_handler, sc);
266 sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change,
267 acpi_video_power_profile, sc, 0);
268
269 ACPI_SERIAL_BEGIN(video);
270 acpi_video_bind_outputs(sc);
271 ACPI_SERIAL_END(video);
272
273 /*
274 * Notify the BIOS that we want to switch both active outputs and
275 * brightness levels.
276 */
277 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
278 DOS_BRIGHTNESS_BY_OSPM);
279
280 acpi_video_power_profile(sc);
281
282 return (0);
283 }
284
285 static int
286 acpi_video_detach(device_t dev)
287 {
288 struct acpi_video_softc *sc;
289 struct acpi_video_output *vo, *vn;
290
291 sc = device_get_softc(dev);
292
293 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
294 EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh);
295 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
296 acpi_video_notify_handler);
297
298 ACPI_SERIAL_BEGIN(video);
299 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
300 acpi_video_vo_destroy(vo);
301 }
302 ACPI_SERIAL_END(video);
303
304 return (0);
305 }
306
307 static int
308 acpi_video_shutdown(device_t dev)
309 {
310 struct acpi_video_softc *sc;
311
312 sc = device_get_softc(dev);
313 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
314
315 return (0);
316 }
317
318 static void
319 acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
320 {
321 struct acpi_video_softc *sc;
322 struct acpi_video_output *vo, *vo_tmp;
323 ACPI_HANDLE lasthand;
324 UINT32 dcs, dss, dss_p;
325
326 sc = (struct acpi_video_softc *)context;
327
328 switch (notify) {
329 case VID_NOTIFY_SWITCHED:
330 dss_p = 0;
331 lasthand = NULL;
332 ACPI_SERIAL_BEGIN(video);
333 ACPI_SERIAL_BEGIN(video_output);
334 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
335 dss = vo_get_graphics_state(vo->handle);
336 dcs = vo_get_device_status(vo->handle);
337 if (!(dcs & DCS_READY))
338 dss = DSS_INACTIVE;
339 if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
340 (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
341 if (lasthand != NULL)
342 vo_set_device_state(lasthand, dss_p);
343 dss_p = dss;
344 lasthand = vo->handle;
345 }
346 }
347 if (lasthand != NULL)
348 vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
349 ACPI_SERIAL_END(video_output);
350 ACPI_SERIAL_END(video);
351 break;
352 case VID_NOTIFY_REPROBE:
353 ACPI_SERIAL_BEGIN(video);
354 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
355 vo->handle = NULL;
356 acpi_video_bind_outputs(sc);
357 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
358 if (vo->handle == NULL) {
359 STAILQ_REMOVE(&sc->vid_outputs, vo,
360 acpi_video_output, vo_next);
361 acpi_video_vo_destroy(vo);
362 }
363 }
364 ACPI_SERIAL_END(video);
365 break;
366 default:
367 device_printf(sc->device, "unknown notify event 0x%x\n",
368 notify);
369 }
370 }
371
372 static void
373 acpi_video_power_profile(void *context)
374 {
375 int state;
376 struct acpi_video_softc *sc;
377 struct acpi_video_output *vo;
378
379 sc = context;
380 state = power_profile_get_state();
381 if (state != POWER_PROFILE_PERFORMANCE &&
382 state != POWER_PROFILE_ECONOMY)
383 return;
384
385 ACPI_SERIAL_BEGIN(video);
386 ACPI_SERIAL_BEGIN(video_output);
387 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
388 if (vo->vo_levels != NULL && vo->vo_brightness == -1)
389 vo_set_brightness(vo->handle,
390 state == POWER_PROFILE_ECONOMY ?
391 vo->vo_economy : vo->vo_fullpower);
392 }
393 ACPI_SERIAL_END(video_output);
394 ACPI_SERIAL_END(video);
395 }
396
397 static void
398 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
399 {
400 struct acpi_video_softc *sc;
401 struct acpi_video_output *vo;
402
403 ACPI_SERIAL_ASSERT(video);
404 sc = context;
405
406 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
407 if (vo->adr == adr) {
408 acpi_video_vo_bind(vo, handle);
409 return;
410 }
411 }
412 vo = acpi_video_vo_init(adr);
413 if (vo != NULL) {
414 acpi_video_vo_bind(vo, handle);
415 STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
416 }
417 }
418
419 static void
420 acpi_video_bind_outputs(struct acpi_video_softc *sc)
421 {
422
423 ACPI_SERIAL_ASSERT(video);
424 vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
425 }
426
427 static struct acpi_video_output *
428 acpi_video_vo_init(UINT32 adr)
429 {
430 struct acpi_video_output *vn, *vo, *vp;
431 int n, x;
432 char name[8], env[32];
433 const char *type, *desc;
434 struct acpi_video_output_queue *voqh;
435
436 ACPI_SERIAL_ASSERT(video);
437
438 switch (adr & DOD_DEVID_MASK) {
439 case DOD_DEVID_MONITOR:
440 if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
441 /* DOD_DEVID_LCD is a common, backward compatible ID */
442 desc = "Internal/Integrated Digital Flat Panel";
443 type = "lcd";
444 voqh = &lcd_units;
445 } else {
446 desc = "VGA CRT or VESA Compatible Analog Monitor";
447 type = "crt";
448 voqh = &crt_units;
449 }
450 break;
451 case DOD_DEVID_TV:
452 desc = "TV/HDTV or Analog-Video Monitor";
453 type = "tv";
454 voqh = &tv_units;
455 break;
456 case DOD_DEVID_EXT:
457 desc = "External Digital Monitor";
458 type = "ext";
459 voqh = &ext_units;
460 break;
461 case DOD_DEVID_INTDFP:
462 desc = "Internal/Integrated Digital Flat Panel";
463 type = "lcd";
464 voqh = &lcd_units;
465 break;
466 default:
467 desc = "unknown output";
468 type = "out";
469 voqh = &other_units;
470 }
471
472 n = 0;
473 vp = NULL;
474 STAILQ_FOREACH(vn, voqh, vo_unit.next) {
475 if (vn->vo_unit.num != n)
476 break;
477 vp = vn;
478 n++;
479 }
480
481 snprintf(name, sizeof(name), "%s%d", type, n);
482
483 vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
484 if (vo != NULL) {
485 vo->handle = NULL;
486 vo->adr = adr;
487 vo->vo_unit.num = n;
488 vo->vo_brightness = -1;
489 vo->vo_fullpower = -1; /* TODO: override with tunables */
490 vo->vo_economy = -1;
491 vo->vo_numlevels = 0;
492 vo->vo_levels = NULL;
493 snprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
494 if (getenv_int(env, &x))
495 vo->vo_fullpower = x;
496 snprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
497 if (getenv_int(env, &x))
498 vo->vo_economy = x;
499
500 sysctl_ctx_init(&vo->vo_sysctl_ctx);
501 if (vp != NULL)
502 STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
503 else
504 STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
505 if (acpi_video_sysctl_tree != NULL)
506 vo->vo_sysctl_tree =
507 SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
508 SYSCTL_CHILDREN(acpi_video_sysctl_tree),
509 OID_AUTO, name, CTLFLAG_RD, 0, desc);
510 if (vo->vo_sysctl_tree != NULL) {
511 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
512 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
513 OID_AUTO, "active",
514 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
515 acpi_video_vo_active_sysctl, "I",
516 "current activity of this device");
517 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
518 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
519 OID_AUTO, "brightness",
520 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
521 acpi_video_vo_bright_sysctl, "I",
522 "current brightness level");
523 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
524 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
525 OID_AUTO, "fullpower",
526 CTLTYPE_INT|CTLFLAG_RW, vo,
527 POWER_PROFILE_PERFORMANCE,
528 acpi_video_vo_presets_sysctl, "I",
529 "preset level for full power mode");
530 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
531 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
532 OID_AUTO, "economy",
533 CTLTYPE_INT|CTLFLAG_RW, vo,
534 POWER_PROFILE_ECONOMY,
535 acpi_video_vo_presets_sysctl, "I",
536 "preset level for economy mode");
537 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
538 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
539 OID_AUTO, "levels",
540 CTLTYPE_OPAQUE|CTLFLAG_RD, vo, 0,
541 acpi_video_vo_levels_sysctl, "I",
542 "supported brightness levels");
543 } else
544 printf("%s: sysctl node creation failed\n", type);
545 } else
546 printf("%s: softc allocation failed\n", type);
547
548 if (bootverbose) {
549 printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
550 printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
551 printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
552 if (adr & DOD_BIOS)
553 printf(", detectable by BIOS");
554 if (adr & DOD_NONVGA)
555 printf(" (Non-VGA output device whose power "
556 "is related to the VGA device)");
557 printf(", head #%d\n",
558 (adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
559 }
560 return (vo);
561 }
562
563 static void
564 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
565 {
566
567 ACPI_SERIAL_BEGIN(video_output);
568 if (vo->vo_levels != NULL)
569 AcpiOsFree(vo->vo_levels);
570 vo->handle = handle;
571 vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
572 if (vo->vo_numlevels >= 2) {
573 if (vo->vo_fullpower == -1
574 || acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0)
575 /* XXX - can't deal with rebinding... */
576 vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
577 if (vo->vo_economy == -1
578 || acpi_video_vo_check_level(vo, vo->vo_economy) != 0)
579 /* XXX - see above. */
580 vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
581 }
582 if (vo->vo_levels != NULL)
583 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
584 acpi_video_vo_notify_handler, vo);
585 ACPI_SERIAL_END(video_output);
586 }
587
588 static void
589 acpi_video_vo_destroy(struct acpi_video_output *vo)
590 {
591 struct acpi_video_output_queue *voqh;
592
593 ACPI_SERIAL_ASSERT(video);
594 if (vo->vo_sysctl_tree != NULL) {
595 vo->vo_sysctl_tree = NULL;
596 sysctl_ctx_free(&vo->vo_sysctl_ctx);
597 }
598 if (vo->vo_levels != NULL) {
599 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
600 acpi_video_vo_notify_handler);
601 AcpiOsFree(vo->vo_levels);
602 }
603
604 switch (vo->adr & DOD_DEVID_MASK) {
605 case DOD_DEVID_MONITOR:
606 voqh = &crt_units;
607 break;
608 case DOD_DEVID_TV:
609 voqh = &tv_units;
610 break;
611 case DOD_DEVID_EXT:
612 voqh = &ext_units;
613 break;
614 case DOD_DEVID_INTDFP:
615 voqh = &lcd_units;
616 break;
617 default:
618 voqh = &other_units;
619 }
620 STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
621 free(vo, M_ACPIVIDEO);
622 }
623
624 static int
625 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
626 {
627 int i;
628
629 ACPI_SERIAL_ASSERT(video_output);
630 if (vo->vo_levels == NULL)
631 return (ENODEV);
632 for (i = 0; i < vo->vo_numlevels; i++)
633 if (vo->vo_levels[i] == level)
634 return (0);
635 return (EINVAL);
636 }
637
638 static void
639 acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
640 {
641 struct acpi_video_output *vo;
642 int i, j, level, new_level;
643
644 vo = context;
645 ACPI_SERIAL_BEGIN(video_output);
646 if (vo->handle != handle)
647 goto out;
648
649 switch (notify) {
650 case VID_NOTIFY_CYCLE_BRN:
651 if (vo->vo_numlevels <= 3)
652 goto out;
653 /* FALLTHROUGH */
654 case VID_NOTIFY_INC_BRN:
655 case VID_NOTIFY_DEC_BRN:
656 case VID_NOTIFY_ZERO_BRN:
657 if (vo->vo_levels == NULL)
658 goto out;
659 level = vo_get_brightness(handle);
660 if (level < 0)
661 goto out;
662 break;
663 default:
664 printf("unknown notify event 0x%x from %s\n",
665 notify, acpi_name(handle));
666 goto out;
667 }
668
669 new_level = level;
670 switch (notify) {
671 case VID_NOTIFY_CYCLE_BRN:
672 for (i = 2; i < vo->vo_numlevels; i++)
673 if (vo->vo_levels[i] == level) {
674 new_level = vo->vo_numlevels > i + 1 ?
675 vo->vo_levels[i + 1] : vo->vo_levels[2];
676 break;
677 }
678 break;
679 case VID_NOTIFY_INC_BRN:
680 case VID_NOTIFY_DEC_BRN:
681 for (i = 0; i < vo->vo_numlevels; i++) {
682 j = vo->vo_levels[i];
683 if (notify == VID_NOTIFY_INC_BRN) {
684 if (j > level &&
685 (j < new_level || level == new_level))
686 new_level = j;
687 } else {
688 if (j < level &&
689 (j > new_level || level == new_level))
690 new_level = j;
691 }
692 }
693 break;
694 case VID_NOTIFY_ZERO_BRN:
695 for (i = 0; i < vo->vo_numlevels; i++)
696 if (vo->vo_levels[i] == 0) {
697 new_level = 0;
698 break;
699 }
700 break;
701 }
702 if (new_level != level) {
703 vo_set_brightness(handle, new_level);
704 vo->vo_brightness = new_level;
705 }
706
707 out:
708 ACPI_SERIAL_END(video_output);
709 }
710
711 /* ARGSUSED */
712 static int
713 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
714 {
715 struct acpi_video_output *vo;
716 int state, err;
717
718 vo = (struct acpi_video_output *)arg1;
719 if (vo->handle == NULL)
720 return (ENXIO);
721 ACPI_SERIAL_BEGIN(video_output);
722 state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
723 err = sysctl_handle_int(oidp, &state, 0, req);
724 if (err != 0 || req->newptr == NULL)
725 goto out;
726 vo_set_device_state(vo->handle,
727 DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
728 out:
729 ACPI_SERIAL_END(video_output);
730 return (err);
731 }
732
733 /* ARGSUSED */
734 static int
735 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
736 {
737 struct acpi_video_output *vo;
738 int level, preset, err;
739
740 vo = (struct acpi_video_output *)arg1;
741 ACPI_SERIAL_BEGIN(video_output);
742 if (vo->handle == NULL) {
743 err = ENXIO;
744 goto out;
745 }
746 if (vo->vo_levels == NULL) {
747 err = ENODEV;
748 goto out;
749 }
750
751 preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
752 vo->vo_economy : vo->vo_fullpower;
753 level = vo->vo_brightness;
754 if (level == -1)
755 level = preset;
756
757 err = sysctl_handle_int(oidp, &level, 0, req);
758 if (err != 0 || req->newptr == NULL)
759 goto out;
760 if (level < -1 || level > 100) {
761 err = EINVAL;
762 goto out;
763 }
764
765 if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
766 goto out;
767 vo->vo_brightness = level;
768 vo_set_brightness(vo->handle, (level == -1) ? preset : level);
769
770 out:
771 ACPI_SERIAL_END(video_output);
772 return (err);
773 }
774
775 static int
776 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
777 {
778 struct acpi_video_output *vo;
779 int i, level, *preset, err;
780
781 vo = (struct acpi_video_output *)arg1;
782 ACPI_SERIAL_BEGIN(video_output);
783 if (vo->handle == NULL) {
784 err = ENXIO;
785 goto out;
786 }
787 if (vo->vo_levels == NULL) {
788 err = ENODEV;
789 goto out;
790 }
791 preset = (arg2 == POWER_PROFILE_ECONOMY) ?
792 &vo->vo_economy : &vo->vo_fullpower;
793 level = *preset;
794 err = sysctl_handle_int(oidp, &level, 0, req);
795 if (err != 0 || req->newptr == NULL)
796 goto out;
797 if (level < -1 || level > 100) {
798 err = EINVAL;
799 goto out;
800 }
801 if (level == -1) {
802 i = (arg2 == POWER_PROFILE_ECONOMY) ?
803 BCL_ECONOMY : BCL_FULLPOWER;
804 level = vo->vo_levels[i];
805 } else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
806 goto out;
807
808 if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
809 vo_set_brightness(vo->handle, level);
810 *preset = level;
811
812 out:
813 ACPI_SERIAL_END(video_output);
814 return (err);
815 }
816
817 /* ARGSUSED */
818 static int
819 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
820 {
821 struct acpi_video_output *vo;
822 int err;
823
824 vo = (struct acpi_video_output *)arg1;
825 ACPI_SERIAL_BEGIN(video_output);
826 if (vo->vo_levels == NULL) {
827 err = ENODEV;
828 goto out;
829 }
830 if (req->newptr != NULL) {
831 err = EPERM;
832 goto out;
833 }
834 err = sysctl_handle_opaque(oidp, vo->vo_levels,
835 vo->vo_numlevels * sizeof(*vo->vo_levels), req);
836
837 out:
838 ACPI_SERIAL_END(video_output);
839 return (err);
840 }
841
842 static void
843 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
844 {
845 ACPI_STATUS status;
846
847 status = acpi_SetInteger(handle, "_DOS", policy);
848 if (ACPI_FAILURE(status))
849 printf("can't evaluate %s._DOS - %s\n",
850 acpi_name(handle), AcpiFormatException(status));
851 }
852
853 struct enum_callback_arg {
854 void (*callback)(ACPI_HANDLE, UINT32, void *);
855 void *context;
856 ACPI_OBJECT *dod_pkg;
857 int count;
858 };
859
860 static ACPI_STATUS
861 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
862 void *context, void **retp __unused)
863 {
864 ACPI_STATUS status;
865 UINT32 adr, val;
866 struct enum_callback_arg *argset;
867 size_t i;
868
869 ACPI_SERIAL_ASSERT(video);
870 argset = context;
871 status = acpi_GetInteger(handle, "_ADR", &adr);
872 if (ACPI_FAILURE(status))
873 return (AE_OK);
874
875 for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
876 if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
877 (val & DOD_DEVID_MASK_FULL) ==
878 (adr & DOD_DEVID_MASK_FULL)) {
879 argset->callback(handle, val, argset->context);
880 argset->count++;
881 }
882 }
883
884 return (AE_OK);
885 }
886
887 static int
888 vid_enum_outputs(ACPI_HANDLE handle,
889 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
890 {
891 ACPI_STATUS status;
892 ACPI_BUFFER dod_buf;
893 ACPI_OBJECT *res;
894 struct enum_callback_arg argset;
895
896 ACPI_SERIAL_ASSERT(video);
897 dod_buf.Length = ACPI_ALLOCATE_BUFFER;
898 dod_buf.Pointer = NULL;
899 status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
900 if (ACPI_FAILURE(status)) {
901 if (status != AE_NOT_FOUND)
902 printf("can't evaluate %s._DOD - %s\n",
903 acpi_name(handle), AcpiFormatException(status));
904 argset.count = -1;
905 goto out;
906 }
907 res = (ACPI_OBJECT *)dod_buf.Pointer;
908 if (!ACPI_PKG_VALID(res, 1)) {
909 printf("evaluation of %s._DOD makes no sense\n",
910 acpi_name(handle));
911 argset.count = -1;
912 goto out;
913 }
914 if (callback == NULL) {
915 argset.count = res->Package.Count;
916 goto out;
917 }
918 argset.callback = callback;
919 argset.context = context;
920 argset.dod_pkg = res;
921 argset.count = 0;
922 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
923 vid_enum_outputs_subr, NULL, &argset, NULL);
924 if (ACPI_FAILURE(status))
925 printf("failed walking down %s - %s\n",
926 acpi_name(handle), AcpiFormatException(status));
927 out:
928 if (dod_buf.Pointer != NULL)
929 AcpiOsFree(dod_buf.Pointer);
930 return (argset.count);
931 }
932
933 static int
934 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
935 {
936 ACPI_STATUS status;
937 ACPI_BUFFER bcl_buf;
938 ACPI_OBJECT *res;
939 int num, i, n, *levels;
940
941 bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
942 bcl_buf.Pointer = NULL;
943 status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
944 if (ACPI_FAILURE(status)) {
945 if (status != AE_NOT_FOUND)
946 printf("can't evaluate %s._BCL - %s\n",
947 acpi_name(handle), AcpiFormatException(status));
948 goto out;
949 }
950 res = (ACPI_OBJECT *)bcl_buf.Pointer;
951 if (!ACPI_PKG_VALID(res, 2)) {
952 printf("evaluation of %s._BCL makes no sense\n",
953 acpi_name(handle));
954 goto out;
955 }
956 num = res->Package.Count;
957 if (num < 2 || levelp == NULL)
958 goto out;
959 levels = AcpiOsAllocate(num * sizeof(*levels));
960 if (levels == NULL)
961 goto out;
962 for (i = 0, n = 0; i < num; i++)
963 if (acpi_PkgInt32(res, i, &levels[n]) == 0)
964 n++;
965 if (n < 2) {
966 AcpiOsFree(levels);
967 goto out;
968 }
969 *levelp = levels;
970 return (n);
971
972 out:
973 if (bcl_buf.Pointer != NULL)
974 AcpiOsFree(bcl_buf.Pointer);
975 return (0);
976 }
977
978 static int
979 vo_get_brightness(ACPI_HANDLE handle)
980 {
981 UINT32 level;
982 ACPI_STATUS status;
983
984 ACPI_SERIAL_ASSERT(video_output);
985 status = acpi_GetInteger(handle, "_BQC", &level);
986 if (ACPI_FAILURE(status)) {
987 printf("can't evaluate %s._BQC - %s\n", acpi_name(handle),
988 AcpiFormatException(status));
989 return (-1);
990 }
991 if (level > 100)
992 return (-1);
993
994 return (level);
995 }
996
997 static void
998 vo_set_brightness(ACPI_HANDLE handle, int level)
999 {
1000 ACPI_STATUS status;
1001
1002 ACPI_SERIAL_ASSERT(video_output);
1003 status = acpi_SetInteger(handle, "_BCM", level);
1004 if (ACPI_FAILURE(status))
1005 printf("can't evaluate %s._BCM - %s\n",
1006 acpi_name(handle), AcpiFormatException(status));
1007 }
1008
1009 static UINT32
1010 vo_get_device_status(ACPI_HANDLE handle)
1011 {
1012 UINT32 dcs;
1013 ACPI_STATUS status;
1014
1015 ACPI_SERIAL_ASSERT(video_output);
1016 dcs = 0;
1017 status = acpi_GetInteger(handle, "_DCS", &dcs);
1018 if (ACPI_FAILURE(status))
1019 printf("can't evaluate %s._DCS - %s\n",
1020 acpi_name(handle), AcpiFormatException(status));
1021
1022 return (dcs);
1023 }
1024
1025 static UINT32
1026 vo_get_graphics_state(ACPI_HANDLE handle)
1027 {
1028 UINT32 dgs;
1029 ACPI_STATUS status;
1030
1031 dgs = 0;
1032 status = acpi_GetInteger(handle, "_DGS", &dgs);
1033 if (ACPI_FAILURE(status))
1034 printf("can't evaluate %s._DGS - %s\n",
1035 acpi_name(handle), AcpiFormatException(status));
1036
1037 return (dgs);
1038 }
1039
1040 static void
1041 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1042 {
1043 ACPI_STATUS status;
1044
1045 ACPI_SERIAL_ASSERT(video_output);
1046 status = acpi_SetInteger(handle, "_DSS", state);
1047 if (ACPI_FAILURE(status))
1048 printf("can't evaluate %s._DSS - %s\n",
1049 acpi_name(handle), AcpiFormatException(status));
1050 }
Cache object: 84ca66173ece973b1b4b7014a25f7354
|