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