summaryrefslogtreecommitdiffstats
path: root/isys/vio.c
blob: be4062315090f5d7d31052501548277a1190efaa (plain)
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
/*
 * vio.c - probing for vio devices on the iSeries (viocd and viodasd)
 *
 * Jeremy Katz <katzj@redhat.com>
 *
 * Copyright 2003  Red Hat, Inc.
 *
 */

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <kudzu/kudzu.h>
#include "probe.h"

int vioGetCdDevs(struct knownDevices * devices) {
#if !defined(__powerpc__)
    return 0;
#else
    int fd, i;
    char * buf, * start, * end, * chptr, * next, * model, * ptr;
    int ctlNum = 0;
    char ctl[64];
    struct kddevice newDevice;

    if (access("/proc/iSeries/viocd", R_OK))
	return 0;

    /* Read from /proc/iSeries/viocd */
    fd = open("/proc/iSeries/viocd", O_RDONLY);
    if (fd < 0) {
	fprintf(stderr, "failed to open /proc/iSeries/viocd!\n");
	return 1;
    }

    i = readFD(fd, &buf);
    if (i < 1) {
        close(fd);
	fprintf(stderr, "error reading /proc/iSeries/viocd!\n");
        return 1;
    }
    close(fd);
    buf[i] = '\0';

    start = buf;
    end = start + strlen(start);
    while (*start && start < end) {
	/* parse till end of line and store the start of next line. */
	chptr = start;
	while (*chptr != '\n') chptr++;
	*chptr = '\0';
	next = chptr + 1;

	/* get rid of anything which is not alpha */
	while (!(isalpha(*start))) start++;

	model = NULL;
	if (!strncmp("viocd", start, 5))
	    model = "IBM Virtual CDROM";

	if (model) {
	    start += 13;
	    ptr = strchr(start, ' ');
	    *ptr++ = '\0';
	    
	    ctlNum = atoi(start);

	    chptr = strstr(ptr, "type ") + 5;
	    ptr = strchr(chptr, ',');
	    *ptr = '\0';

	    model = alloca((20 + strlen(chptr)) * sizeof(char *));
	    sprintf(model, "IBM Virtual CD-ROM Model %s", chptr);
	    snprintf(ctl, 63, "iseries/vcd%c", ctlNum + 'a');

	    if (!deviceKnown(devices, ctl)) {
	      newDevice.name = strdup(ctl);
	      newDevice.model = strdup(model);
	      newDevice.class = CLASS_CDROM;
	      addDevice(devices, newDevice);
	    }
	    //	    printf("model is %s, ctl is %s\n", model, ctl);
	}

	start = next;
	end = start + strlen(start);
    } /* end of while */

    free (buf);
    return 0;
#endif
}

int vioGetDasdDevs(struct knownDevices * devices) {
#if !defined(__powerpc__)
    return 0;
#else
    int fd, i;
    char * buf, * start, * end, * chptr, * next, * model, * ptr;
    int ctlNum = 0;
    char ctl[64];
    struct kddevice newDevice;

    if (access("/proc/iSeries/viodasd", R_OK))
	return 0;

    /* Read from /proc/iSeries/viodasd */
    fd = open("/proc/iSeries/viodasd", O_RDONLY);
    if (fd < 0) {
	fprintf(stderr, "failed to open /proc/iSeries/viodasd!\n");
	return 1;
    }

    i = readFD(fd, &buf);
    if (i < 1) {
        close(fd);
	fprintf(stderr, "error reading /proc/iSeries/viodasd!\n");
        return 1;
    }
    close(fd);
    buf[i] = '\0';

    start = buf;
    end = start + strlen(start);
    while (*start && start < end) {
	/* parse till end of line and store the start of next line. */
	chptr = start;
	while (*chptr != '\n') chptr++;
	*chptr = '\0';
	next = chptr + 1;

	/* get rid of anything which is not alpha */
	while (!(isalpha(*start))) start++;

	model = NULL;
	if (!strncmp("DISK ", start, 5))
	    model = "IBM Virtual DASD";

	if (model) {
	    chptr = start += 5;
	    ptr = strchr(chptr, ' ');
	    *ptr = '\0';
	    ctlNum = atoi(chptr);

	    if (ctlNum <= 26) {
		snprintf(ctl, 63, "iseries/vd%c", 'a' + ctlNum);
	    } else {
		snprintf(ctl, 63, "iseries/vda%c", 'a' + ctlNum - 26);
	    }
	   
	    if (!deviceKnown(devices, ctl)) {
	      newDevice.name = strdup(ctl);
	      newDevice.model = strdup(model);
	      newDevice.class = CLASS_HD;
	      addDevice(devices, newDevice);
	    }
	    //	    printf("model is %s, ctl is %s\n", model, ctl);
	}

	start = next;
	end = start + strlen(start);
    } /* end of while */

    free (buf);
    return 0;
#endif
}

int isVioConsole(void) {
#if !defined(__powerpc__)
    return 0;
#else
    int fd, i;
    char *buf, *start;
    char driver[50], device[50];
    static int isviocons = -1;

    if (isviocons != -1)
	return isviocons;
    
    fd = open("/proc/tty/drivers", O_RDONLY);
    if (fd < 0) {
	fprintf(stderr, "failed to open /proc/tty/drivers!\n");
	return 0;
    }
    i = readFD(fd, &buf);
    if (i < 1) {
        close(fd);
	fprintf(stderr, "error reading /proc/tty/drivers!\n");
        return 0;
    }
    close(fd);
    buf[i] = '\0';

    isviocons = 0;
    start = buf;
    while (start && *start) {
	if (sscanf(start, "%s %s", &driver, &device) == 2) {
	    if (!strcmp(driver, "vioconsole") && !strcmp(device, "/dev/tty")) {
		isviocons = 1;
		break;
	    }
	}		
        start = strchr(start, '\n');
        if (start)
	    start++;
    }
    free(buf);
    return isviocons;
#endif
}