1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Power Button Driver
*
* This driver handles interrupt generated by the power button on
* platforms with "power" device node which has "button" property.
* Currently, these platforms are:
*
* Ultra-5_10, Ultra-80, Sun-Blade-100, Sun-Blade-150,
* Sun-Blade-1500, Sun-Blade-2500,
* Sun-Fire-V210, Sun-Fire-V240, Netra-240
*
* Only one instance is allowed to attach. In order to know when
* an application that has opened the device is going away, a new
* minor clone is created for each open(9E) request. There are
* allocations for creating minor clones between 1 and 255. The ioctl
* interface is defined by pbio(7I) and approved as part of
* PSARC/1999/393 case.
*/
#include <sys/types.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ddi_impldefs.h>
#include <sys/cmn_err.h>
#include <sys/errno.h>
#include <sys/modctl.h>
#include <sys/machsystm.h>
#include <sys/open.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <sys/pbio.h>
/*
* Maximum number of clone minors that is allowed. This value
* is defined relatively low to save memory.
*/
#define POWER_MAX_CLONE 256
/*
* Minor number is instance << 8 + clone minor from range 1-255; clone 0
* is reserved for "original" minor.
*/
#define POWER_MINOR_TO_CLONE(minor) ((minor) & (POWER_MAX_CLONE - 1))
/*
* Power Button Abort Delay
*/
#define ABORT_INCREMENT_DELAY 10
/*
* Driver global variables
*/
static void *power_state;
static int power_inst = -1;
static hrtime_t power_button_debounce = NANOSEC/MILLISEC*10;
static hrtime_t power_button_abort_interval = 1.5 * NANOSEC;
static int power_button_abort_presses = 3;
static int power_button_abort_enable = 1;
static int power_button_enable = 1;
static int power_button_pressed = 0;
static int power_button_cancel = 0;
static int power_button_timeouts = 0;
static int timeout_cancel = 0;
static int additional_presses = 0;
/*
* Function prototypes
*/
static int power_attach(dev_info_t *, ddi_attach_cmd_t);
static int power_detach(dev_info_t *, ddi_detach_cmd_t);
static int power_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int power_open(dev_t *, int, int, cred_t *);
static int power_close(dev_t, int, int, cred_t *);
static int power_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static int power_chpoll(dev_t, short, int, short *, struct pollhead **);
static uint_t power_high_intr(caddr_t);
static uint_t power_soft_intr(caddr_t);
static uint_t power_issue_shutdown(caddr_t);
static void power_timeout(caddr_t);
static void power_log_message(void);
/*
* Structure used in the driver
*/
static struct power_soft_state {
dev_info_t *dip; /* device info pointer */
kmutex_t power_mutex; /* mutex lock */
kmutex_t power_intr_mutex; /* interrupt mutex lock */
ddi_iblock_cookie_t soft_iblock_cookie; /* holds interrupt cookie */
ddi_iblock_cookie_t high_iblock_cookie; /* holds interrupt cookie */
ddi_softintr_t softintr_id; /* soft interrupt id */
uchar_t clones[POWER_MAX_CLONE]; /* array of minor clones */
int monitor_on; /* clone monitoring the button event */
/* clone 0 indicates no one is */
/* monitoring the button event */
pollhead_t pollhd; /* poll head struct */
int events; /* bit map of occured events */
int shutdown_pending; /* system shutdown in progress */
ddi_acc_handle_t power_rhandle; /* power button register handle */
uint8_t *power_btn_reg; /* power button register address */
uint8_t power_btn_bit; /* power button register bit */
boolean_t power_regs_mapped; /* flag to tell if regs mapped */
boolean_t power_btn_ioctl; /* flag to specify ioctl request */
};
static int power_setup_regs(struct power_soft_state *softsp);
static void power_free_regs(struct power_soft_state *softsp);
/*
* Configuration data structures
*/
static struct cb_ops power_cb_ops = {
power_open, /* open */
power_close, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
nodev, /* read */
nodev, /* write */
power_ioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
power_chpoll, /* poll */
ddi_prop_op, /* cb_prop_op */
NULL, /* streamtab */
D_MP | D_NEW, /* Driver compatibility flag */
CB_REV, /* rev */
nodev, /* cb_aread */
nodev /* cb_awrite */
};
static struct dev_ops power_ops = {
DEVO_REV, /* devo_rev, */
0, /* refcnt */
power_getinfo, /* getinfo */
nulldev, /* identify */
nulldev, /* probe */
power_attach, /* attach */
power_detach, /* detach */
nodev, /* reset */
&power_cb_ops, /* cb_ops */
(struct bus_ops *)NULL, /* bus_ops */
NULL /* power */
};
static struct modldrv modldrv = {
&mod_driverops, /* Type of module. This one is a driver */
"power button driver v%I%", /* name of module */
&power_ops, /* driver ops */
};
static struct modlinkage modlinkage = {
MODREV_1,
(void *)&modldrv,
NULL
};
/*
* These are the module initialization routines.
*/
int
_init(void)
{
int error;
if ((error = ddi_soft_state_init(&power_state,
sizeof (struct power_soft_state), 0)) != 0)
return (error);
if ((error = mod_install(&modlinkage)) != 0)
ddi_soft_state_fini(&power_state);
return (error);
}
int
_fini(void)
{
int error;
if ((error = mod_remove(&modlinkage)) == 0)
ddi_soft_state_fini(&power_state);
return (error);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
/*ARGSUSED*/
static int
power_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
void **result)
{
struct power_soft_state *softsp;
if (power_inst == -1)
return (DDI_FAILURE);
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
if ((softsp = ddi_get_soft_state(power_state, power_inst))
== NULL)
return (DDI_FAILURE);
*result = (void *)softsp->dip;
return (DDI_SUCCESS);
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)power_inst;
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
static int
power_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
struct power_soft_state *softsp;
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
/*
* If the power node doesn't have "button" property, quietly
* fail to attach.
*/
if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"button") == 0)
return (DDI_FAILURE);
if (power_inst != -1)
return (DDI_FAILURE);
power_inst = ddi_get_instance(dip);
if (ddi_soft_state_zalloc(power_state, power_inst) != DDI_SUCCESS)
return (DDI_FAILURE);
if (ddi_create_minor_node(dip, "power_button", S_IFCHR,
(power_inst << 8) + 0, "ddi_power_button", 0) != DDI_SUCCESS)
return (DDI_FAILURE);
softsp = ddi_get_soft_state(power_state, power_inst);
softsp->dip = dip;
if (power_setup_regs(softsp) != DDI_SUCCESS) {
cmn_err(CE_WARN, "power_attach: failed to setup registers");
goto error;
}
if (ddi_get_iblock_cookie(dip, 0,
&softsp->high_iblock_cookie) != DDI_SUCCESS) {
cmn_err(CE_WARN, "power_attach: ddi_get_soft_iblock_cookie "
"failed.");
goto error;
}
mutex_init(&softsp->power_intr_mutex, NULL, MUTEX_DRIVER,
softsp->high_iblock_cookie);
if (ddi_add_intr(dip, 0, &softsp->high_iblock_cookie, NULL,
power_high_intr, (caddr_t)softsp) != DDI_SUCCESS) {
cmn_err(CE_WARN, "power_attach: failed to add high-level "
" interrupt handler.");
mutex_destroy(&softsp->power_intr_mutex);
goto error;
}
if (ddi_get_soft_iblock_cookie(dip, DDI_SOFTINT_LOW,
&softsp->soft_iblock_cookie) != DDI_SUCCESS) {
cmn_err(CE_WARN, "power_attach: ddi_get_soft_iblock_cookie "
"failed.");
mutex_destroy(&softsp->power_intr_mutex);
ddi_remove_intr(dip, 0, NULL);
goto error;
}
mutex_init(&softsp->power_mutex, NULL, MUTEX_DRIVER,
(void *)softsp->soft_iblock_cookie);
if (ddi_add_softintr(dip, DDI_SOFTINT_LOW, &softsp->softintr_id,
NULL, NULL, power_soft_intr, (caddr_t)softsp) != DDI_SUCCESS) {
cmn_err(CE_WARN, "power_attach: failed to add soft "
"interrupt handler.");
mutex_destroy(&softsp->power_mutex);
mutex_destroy(&softsp->power_intr_mutex);
ddi_remove_intr(dip, 0, NULL);
goto error;
}
ddi_report_dev(dip);
return (DDI_SUCCESS);
error:
power_free_regs(softsp);
ddi_remove_minor_node(dip, "power_button");
ddi_soft_state_free(power_state, power_inst);
return (DDI_FAILURE);
}
/*ARGSUSED*/
/*
* This driver doesn't detach.
*/
static int
power_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
/*
* Since the "power" node has "reg" property, as part of
* the suspend operation, detach(9E) entry point is called.
* There is no state to save, since this register is used
* by OBP to power off the system and the state of the
* power off is preserved by hardware.
*/
return ((cmd == DDI_SUSPEND) ? DDI_SUCCESS :
DDI_FAILURE);
}
/*
* Handler for the high-level interrupt.
*/
static uint_t
power_high_intr(caddr_t arg)
{
struct power_soft_state *softsp = (struct power_soft_state *)arg;
ddi_acc_handle_t hdl = softsp->power_rhandle;
uint8_t reg;
hrtime_t tstamp;
static hrtime_t o_tstamp = 0;
static hrtime_t power_button_tstamp = 0;
static int power_button_cnt;
if (softsp->power_regs_mapped) {
mutex_enter(&softsp->power_intr_mutex);
reg = ddi_get8(hdl, softsp->power_btn_reg);
if (reg & softsp->power_btn_bit) {
reg &= softsp->power_btn_bit;
ddi_put8(hdl, softsp->power_btn_reg, reg);
(void) ddi_get8(hdl, softsp->power_btn_reg);
} else {
if (!softsp->power_btn_ioctl) {
mutex_exit(&softsp->power_intr_mutex);
return (DDI_INTR_CLAIMED);
}
softsp->power_btn_ioctl = B_FALSE;
}
mutex_exit(&softsp->power_intr_mutex);
}
tstamp = gethrtime();
/* need to deal with power button debounce */
if (o_tstamp && (tstamp - o_tstamp) < power_button_debounce) {
o_tstamp = tstamp;
return (DDI_INTR_CLAIMED);
}
o_tstamp = tstamp;
power_button_cnt++;
mutex_enter(&softsp->power_intr_mutex);
power_button_pressed++;
mutex_exit(&softsp->power_intr_mutex);
/*
* If power button abort is enabled and power button was pressed
* power_button_abort_presses times within power_button_abort_interval
* then call abort_sequence_enter();
*/
if (power_button_abort_enable) {
if (power_button_abort_presses == 1 ||
tstamp < (power_button_tstamp +
power_button_abort_interval)) {
if (power_button_cnt == power_button_abort_presses) {
mutex_enter(&softsp->power_intr_mutex);
power_button_cancel += power_button_timeouts;
power_button_pressed = 0;
mutex_exit(&softsp->power_intr_mutex);
power_button_cnt = 0;
abort_sequence_enter("Power Button Abort");
return (DDI_INTR_CLAIMED);
}
} else {
power_button_cnt = 1;
power_button_tstamp = tstamp;
}
}
if (!power_button_enable)
return (DDI_INTR_CLAIMED);
/* post softint to issue timeout for power button action */
if (softsp->softintr_id != NULL)
ddi_trigger_softintr(softsp->softintr_id);
return (DDI_INTR_CLAIMED);
}
/*
* Handle the softints....
*
* If only one softint is posted for several button presses, record
* the number of additional presses just incase this was actually not quite
* an Abort sequence so that we can log this event later.
*
* Issue a timeout with a duration being a fraction larger than
* the specified Abort interval inorder to perform a power down if required.
*/
static uint_t
power_soft_intr(caddr_t arg)
{
struct power_soft_state *softsp = (struct power_soft_state *)arg;
if (!power_button_abort_enable)
return (power_issue_shutdown(arg));
mutex_enter(&softsp->power_intr_mutex);
if (!power_button_pressed) {
mutex_exit(&softsp->power_intr_mutex);
return (DDI_INTR_CLAIMED);
}
/*
* Schedule a timeout to do the necessary
* work for shutdown, only one timeout for
* n presses if power button was pressed
* more than once before softint fired
*/
if (power_button_pressed > 1)
additional_presses += power_button_pressed - 1;
timeout_cancel = 0;
power_button_pressed = 0;
power_button_timeouts++;
mutex_exit(&softsp->power_intr_mutex);
(void) timeout((void(*)(void *))power_timeout,
softsp, NSEC_TO_TICK(power_button_abort_interval) +
ABORT_INCREMENT_DELAY);
return (DDI_INTR_CLAIMED);
}
/*
* Upon receiving a timeout the following is determined:
*
* If an Abort sequence was issued, then we cancel all outstanding timeouts
* and additional presses prior to the Abort sequence.
*
* If we had multiple timeouts issued and the abort sequence was not met,
* then we had more than one button press to power down the machine. We
* were probably trying to issue an abort. So log a message indicating this
* and cancel all outstanding timeouts.
*
* If we had just one timeout and the abort sequence was not met then
* we really did want to power down the machine, so call power_issue_shutdown()
* to do the work and schedule a power down
*/
static void
power_timeout(caddr_t arg)
{
struct power_soft_state *softsp = (struct power_soft_state *)arg;
static int first = 0;
/*
* Abort was generated cancel all outstanding power
* button timeouts
*/
mutex_enter(&softsp->power_intr_mutex);
if (power_button_cancel) {
power_button_cancel--;
power_button_timeouts--;
if (!first) {
first++;
additional_presses = 0;
}
mutex_exit(&softsp->power_intr_mutex);
return;
}
first = 0;
/*
* We get here if the timeout(s) have fired and they were
* not issued prior to an abort.
*
* If we had more than one press in the interval we were
* probably trying to issue an abort, but didnt press the
* required number within the interval. Hence cancel all
* timeouts and do not continue towards shutdown.
*/
if (!timeout_cancel) {
timeout_cancel = power_button_timeouts +
additional_presses;
power_button_timeouts--;
if (!power_button_timeouts)
additional_presses = 0;
if (timeout_cancel > 1) {
mutex_exit(&softsp->power_intr_mutex);
cmn_err(CE_NOTE, "Power Button pressed "
"%d times, cancelling all requests",
timeout_cancel);
return;
}
mutex_exit(&softsp->power_intr_mutex);
/* Go and do the work to request shutdown */
(void) power_issue_shutdown((caddr_t)softsp);
return;
}
power_button_timeouts--;
if (!power_button_timeouts)
additional_presses = 0;
mutex_exit(&softsp->power_intr_mutex);
}
static uint_t
power_issue_shutdown(caddr_t arg)
{
struct power_soft_state *softsp = (struct power_soft_state *)arg;
mutex_enter(&softsp->power_mutex);
softsp->events |= PB_BUTTON_PRESS;
if (softsp->monitor_on != 0) {
mutex_exit(&softsp->power_mutex);
pollwakeup(&softsp->pollhd, POLLRDNORM);
pollwakeup(&softsp->pollhd, POLLIN);
return (DDI_INTR_CLAIMED);
}
if (!softsp->shutdown_pending) {
cmn_err(CE_WARN, "Power off requested from power button or "
"SC, powering down the system!");
softsp->shutdown_pending = 1;
do_shutdown();
/*
* Wait a while for "do_shutdown()" to shut down the system
* before logging an error message.
*/
(void) timeout((void(*)(void *))power_log_message, NULL,
100 * hz);
}
mutex_exit(&softsp->power_mutex);
return (DDI_INTR_CLAIMED);
}
/*
* Open the device.
*/
/*ARGSUSED*/
static int
power_open(dev_t *devp, int openflags, int otyp, cred_t *credp)
{
struct power_soft_state *softsp;
int clone;
if (otyp != OTYP_CHR)
return (EINVAL);
if ((softsp = ddi_get_soft_state(power_state, power_inst)) ==
NULL)
return (ENXIO);
mutex_enter(&softsp->power_mutex);
for (clone = 1; clone < POWER_MAX_CLONE; clone++)
if (!softsp->clones[clone])
break;
if (clone == POWER_MAX_CLONE) {
cmn_err(CE_WARN, "power_open: No more allocation left "
"to create a clone minor.");
mutex_exit(&softsp->power_mutex);
return (ENXIO);
}
*devp = makedevice(getmajor(*devp), (power_inst << 8) + clone);
softsp->clones[clone] = 1;
mutex_exit(&softsp->power_mutex);
return (0);
}
/*
* Close the device.
*/
/*ARGSUSED*/
static int
power_close(dev_t dev, int openflags, int otyp, cred_t *credp)
{
struct power_soft_state *softsp;
int clone;
if (otyp != OTYP_CHR)
return (EINVAL);
if ((softsp = ddi_get_soft_state(power_state, power_inst)) ==
NULL)
return (ENXIO);
clone = POWER_MINOR_TO_CLONE(getminor(dev));
mutex_enter(&softsp->power_mutex);
if (softsp->monitor_on == clone)
softsp->monitor_on = 0;
softsp->clones[clone] = 0;
mutex_exit(&softsp->power_mutex);
return (0);
}
/*ARGSUSED*/
static int
power_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred_p,
int *rval_p)
{
struct power_soft_state *softsp;
int clone;
if ((softsp = ddi_get_soft_state(power_state, power_inst)) ==
NULL)
return (ENXIO);
clone = POWER_MINOR_TO_CLONE(getminor(dev));
switch (cmd) {
case PB_BEGIN_MONITOR:
mutex_enter(&softsp->power_mutex);
if (softsp->monitor_on) {
mutex_exit(&softsp->power_mutex);
return (EBUSY);
}
softsp->monitor_on = clone;
mutex_exit(&softsp->power_mutex);
return (0);
case PB_END_MONITOR:
mutex_enter(&softsp->power_mutex);
/*
* If PB_END_MONITOR is called without first
* calling PB_BEGIN_MONITOR, an error will be
* returned.
*/
if (!softsp->monitor_on) {
mutex_exit(&softsp->power_mutex);
return (ENXIO);
}
/*
* This clone is not monitoring the button.
*/
if (softsp->monitor_on != clone) {
mutex_exit(&softsp->power_mutex);
return (EINVAL);
}
softsp->monitor_on = 0;
mutex_exit(&softsp->power_mutex);
return (0);
case PB_GET_EVENTS:
mutex_enter(&softsp->power_mutex);
if (ddi_copyout((void *)&softsp->events, (void *)arg,
sizeof (int), mode) != 0) {
mutex_exit(&softsp->power_mutex);
return (EFAULT);
}
/*
* This ioctl returned the events detected since last
* call. Note that any application can get the events
* and clear the event register.
*/
softsp->events = 0;
mutex_exit(&softsp->power_mutex);
return (0);
/*
* This ioctl is used by the test suite.
*/
case PB_CREATE_BUTTON_EVENT:
if (softsp->power_regs_mapped) {
mutex_enter(&softsp->power_intr_mutex);
softsp->power_btn_ioctl = B_TRUE;
mutex_exit(&softsp->power_intr_mutex);
}
(void) power_high_intr((caddr_t)softsp);
return (0);
default:
return (ENOTTY);
}
}
/*ARGSUSED*/
static int
power_chpoll(dev_t dev, short events, int anyyet,
short *reventsp, struct pollhead **phpp)
{
struct power_soft_state *softsp;
if ((softsp = ddi_get_soft_state(power_state, power_inst)) == NULL)
return (ENXIO);
mutex_enter(&softsp->power_mutex);
*reventsp = 0;
if (softsp->events)
*reventsp = POLLRDNORM|POLLIN;
else {
if (!anyyet)
*phpp = &softsp->pollhd;
}
mutex_exit(&softsp->power_mutex);
return (0);
}
static void
power_log_message(void)
{
struct power_soft_state *softsp;
if ((softsp = ddi_get_soft_state(power_state, power_inst)) == NULL) {
cmn_err(CE_WARN, "Failed to get internal state!");
return;
}
mutex_enter(&softsp->power_mutex);
softsp->shutdown_pending = 0;
cmn_err(CE_WARN, "Failed to shut down the system!");
mutex_exit(&softsp->power_mutex);
}
/*
* power button register definitions for acpi register on m1535d
*/
#define M1535D_PWR_BTN_REG_01 0x1
#define M1535D_PWR_BTN_EVENT_FLAG 0x1
static int
power_setup_m1535_regs(dev_info_t *dip, struct power_soft_state *softsp)
{
ddi_device_acc_attr_t attr;
uint8_t *reg_base;
attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
if (ddi_regs_map_setup(dip, 0, (caddr_t *)®_base, 0, 0, &attr,
&softsp->power_rhandle) != DDI_SUCCESS) {
return (DDI_FAILURE);
}
softsp->power_btn_reg = ®_base[M1535D_PWR_BTN_REG_01];
softsp->power_btn_bit = M1535D_PWR_BTN_EVENT_FLAG;
softsp->power_regs_mapped = B_TRUE;
return (DDI_SUCCESS);
}
/*
* Setup register map for the power button
* NOTE:- we only map registers for platforms
* binding with the ali1535d+-power compatible
* property.
*/
static int
power_setup_regs(struct power_soft_state *softsp)
{
char *binding_name;
softsp->power_regs_mapped = B_FALSE;
softsp->power_btn_ioctl = B_FALSE;
binding_name = ddi_binding_name(softsp->dip);
if (strcmp(binding_name, "ali1535d+-power") == 0)
return (power_setup_m1535_regs(softsp->dip, softsp));
return (DDI_SUCCESS);
}
static void
power_free_regs(struct power_soft_state *softsp)
{
if (softsp->power_regs_mapped)
ddi_regs_map_free(&softsp->power_rhandle);
}
|