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
|
/*
* 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 2000, 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/sunddi.h>
#include <sys/stat.h>
#include <sys/i2c/clients/i2c_client.h>
#include <sys/hpc3130_events.h>
#include <values.h> /* for BITSPERBYTE */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <strings.h>
#include <poll.h>
#include <libintl.h>
#include <errno.h>
#include <assert.h>
#include <config_admin.h>
#include <sys/daktari.h>
#include "sf880drd.h"
/*
* Hotplug Controller addresses.
*/
static const unsigned char controller_names[NUM_CONTROLLERS] =
{ 0xe2, 0xe6, 0xe8, 0xec };
#define INDEX2SLOT(INDEX) ((INDEX)%4) /* cf init_poll_events() */
#define INDEX2CONTROLLER(INDEX) ((INDEX)/4) /* cf init_poll_events() */
/*
* Local variables.
*/
static struct pollfd fds[NUM_FDS];
static unsigned int fault_leds[2];
static unsigned int ok2rem_leds[2];
/*
* Local prototypes.
*/
static void init_poll_events(void);
static void process_event(int);
static void report_cfgadm_error(int, char *);
static void set_front_panel_led(uint8_t, boolean_t);
static int i2c_set_bit(int, int, uint8_t);
static void report_syscall_error(char *);
static void pushbutton_event(char *);
static void fault_led_event(hpc3130_event_type_t, int);
static void removable_led_event(hpc3130_event_type_t, int);
/*
* main(): loops forever looking for events.
*/
int
main()
{
int i;
int rv;
init_poll_events();
for (;;) {
/* sleep in poll() waiting an event */
rv = poll(fds, NUM_FDS, -1);
if (rv < 0) {
report_syscall_error("poll");
continue;
}
/* woken up from poll() process the event */
for (i = 0; i < NUM_FDS; ++i) {
if (fds[i].revents == POLLIN)
process_event(i);
}
}
/* NOTREACHED */
return (0);
}
/*
* Set up poll fds.
*/
static void
init_poll_events()
{
int c;
int p;
int i = 0;
char buf[sizeof (HPC3130_DEV_FMT)+8];
for (c = 0; c < NUM_CONTROLLERS; ++c) {
for (p = 0; p < SLOTS_PER_CONTROLLER; ++p) {
(void) sprintf(buf, HPC3130_DEV_FMT,
controller_names[c], p);
fds[i].events = POLLIN;
fds[i].fd = open(buf, O_RDWR);
if (fds[i].fd == -1) {
report_syscall_error(buf);
exit(-1);
}
i++;
}
}
}
/*
* Process poll events.
*/
static void
process_event(int fdi)
{
struct hpc3130_event e;
int rv;
int slot = INDEX2SLOT(fdi);
int cntr = INDEX2CONTROLLER(fdi);
rv = ioctl(fds[fdi].fd, HPC3130_GET_EVENT, &e);
if (rv == -1) {
report_syscall_error("HPC3130_GET_EVENT");
return;
}
switch (e.id) {
case HPC3130_EVENT_INSERTION:
case HPC3130_EVENT_REMOVAL:
case HPC3130_EVENT_POWERON:
case HPC3130_EVENT_POWEROFF:
break;
case HPC3130_EVENT_BUTTON:
DPRINTF(("\nBUTTON EVENT slot (%s)\n", e.name));
pushbutton_event(e.name);
break;
case HPC3130_LED_FAULT_ON:
DPRINTF(("\nFAULT LED ON EVENT\n"));
fault_led_event(e.id, fdi);
break;
case HPC3130_LED_FAULT_OFF:
DPRINTF(("\nFAULT LED OFF EVENT\n"));
fault_led_event(e.id, fdi);
break;
case HPC3130_LED_REMOVABLE_ON:
DPRINTF(("\nREMOVABLE LED ON EVENT\n"));
removable_led_event(e.id, fdi);
break;
case HPC3130_LED_REMOVABLE_OFF:
DPRINTF(("\nREMOVABLE LED OFF EVENT\n"));
removable_led_event(e.id, fdi);
break;
default:
(void) fprintf(stderr, "WARNING: bogus event: %x\n", e.id);
}
}
/*
* Button Event handler.
*/
static void
pushbutton_event(char *ap_id)
{
char *errstr = NULL;
struct cfga_list_data *stat = NULL;
int nlist;
cfga_cmd_t cmd;
cfga_err_t rv;
rv = config_list_ext(1, &ap_id, &stat, &nlist,
NULL, NULL, &errstr, 0);
if (rv != CFGA_OK) {
report_cfgadm_error(rv, errstr);
goto out;
}
assert(nlist == 1);
/* The only types of hotplug with buttons */
assert(!(strcmp(stat->ap_class, "pci")));
switch (stat->ap_o_state) {
case CFGA_STAT_UNCONFIGURED:
cmd = CFGA_CMD_CONFIGURE;
break;
case CFGA_STAT_CONFIGURED:
cmd = CFGA_CMD_DISCONNECT;
break;
default:
/* Should never get here */
assert(0);
}
/*
* confp & msgp are NULL: when using the pushbutton,
* simply fail if prompting is required.
*/
rv = config_change_state(cmd, 1, &ap_id, NULL, NULL, NULL, &errstr, 0);
if (rv != CFGA_OK) {
report_cfgadm_error(rv, errstr);
/* FALLTHRU to "out" */
}
out:
if (errstr)
free(errstr);
if (stat)
free(stat);
}
static void
fault_led_event(hpc3130_event_type_t event, int fdi)
{
int side = 0;
unsigned int old;
if (INDEX2CONTROLLER(fdi) != GPTWO_CONTROLLER) {
/* It's a PCI slot; left side of chassis */
side = 1;
}
old = fault_leds[side];
assert(fdi <= sizeof (fault_leds[side]) * BITSPERBYTE);
switch (event) {
case HPC3130_LED_FAULT_ON:
fault_leds[side] |= (1<<fdi);
DPRINTF(("fault_led_event: HPC3130_LED_FAULT_ON\n"));
break;
case HPC3130_LED_FAULT_OFF:
fault_leds[side] &= ~(1<<fdi);
DPRINTF(("fault_led_event: HPC3130_LED_FAULT_OFF\n"));
break;
}
DPRINTF(("fault_led_event: old(0x%x), fault_leds[%d](0x%x)\n",
old, side, fault_leds[side]));
if ((old == 0) != (fault_leds[side] == 0) && ok2rem_leds[side] == 0) {
/*
* The first FAULT LED has come on, or the last one has gone
* off on this side, and all the OK2REMOVE LEDS are off on this
* side. So we have to update the front panel ARROW LED.
*/
set_front_panel_led(side ? LEFT_DOOR_ATTEN_LED :
RIGHT_DOOR_ATTEN_LED,
fault_leds[side] ? LED_ON : LED_OFF);
}
}
static void
removable_led_event(hpc3130_event_type_t event, int fdi)
{
int side = 0;
unsigned int old;
if (INDEX2CONTROLLER(fdi) != GPTWO_CONTROLLER) {
/* It's a PCI slot; left side of chassis */
side = 1;
}
old = ok2rem_leds[side];
assert(fdi <= sizeof (ok2rem_leds[side]) * BITSPERBYTE);
switch (event) {
case HPC3130_LED_REMOVABLE_ON:
ok2rem_leds[side] |= (1<<fdi);
DPRINTF(("removable_led_event: HPC3130_LED_REMOVABLE_ON\n"));
break;
case HPC3130_LED_REMOVABLE_OFF:
ok2rem_leds[side] &= ~(1<<fdi);
DPRINTF(("removable_led_event: HPC3130_LED_REMOVABLE_OFF\n"));
break;
}
DPRINTF(("removable_led_event: old(0x%x), ok2rem_leds[%d](0x%x)\n",
old, side, ok2rem_leds[side]));
if ((old == 0) != (ok2rem_leds[side] == 0)) {
/*
* The first OKAY2REMOVE LED has come on, or the last
* one has gone off (on this side). We may have to update
* the front panel LEDs.
*/
if (ok2rem_leds[!side] == 0) {
/*
* The OK2REMOVE LEDs are all off on the other
* side of the chassis, so this side determines
* whether the front OK2REMOVE is on or off.
*/
set_front_panel_led(SYS_OK2REMOVE_LED,
ok2rem_leds[side] ? LED_ON : LED_OFF);
}
if (fault_leds[side] == 0) {
/*
* All the FAULT LEDs are off on this side. So the
* OK2REMOVE LEDs determine whether the ARROW LED is on.
*/
set_front_panel_led(side ? LEFT_DOOR_ATTEN_LED :
RIGHT_DOOR_ATTEN_LED,
ok2rem_leds[side] ? LED_ON : LED_OFF);
}
}
}
/*
* Set front panel system leds either on or off.
*/
static void
set_front_panel_led(uint8_t bit_num, boolean_t on_off)
{
int fd;
int rv;
i2c_bit_t arg;
fd = open(SSC050_LED_PORT, O_RDWR);
if (fd == -1) {
report_syscall_error("ssc050");
return;
}
arg.bit_num = bit_num;
arg.bit_value = on_off;
rv = ioctl(fd, I2C_SET_BIT, &arg);
if (rv == -1)
report_syscall_error("LED I2C_SET_BIT");
(void) close(fd);
}
static int
i2c_set_bit(int fp, int bit, uint8_t value)
{
int rv;
i2c_bit_t passin;
passin.bit_num = (uchar_t)bit;
passin.bit_value = value;
rv = ioctl(fp, I2C_SET_BIT, &passin);
return (rv);
}
static void
report_cfgadm_error(int cfgerrnum, char *errstr)
{
const char *ep;
ep = config_strerror(cfgerrnum);
if (ep == NULL)
ep = gettext("configuration administration unknown error");
if (errstr != NULL && *errstr != '\0') {
(void) fprintf(stderr, "%s: %s\n", ep, errstr);
} else {
(void) fprintf(stderr, "%s\n", ep);
}
}
static void
report_syscall_error(char *msg)
{
if (errno != EINTR)
perror(msg);
}
|