File: startup.c

package info (click to toggle)
pcmciautils 018-8
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 448 kB
  • ctags: 1,014
  • sloc: ansic: 3,901; makefile: 230; lex: 222; sh: 169; yacc: 105
file content (256 lines) | stat: -rw-r--r-- 5,162 bytes parent folder | download | duplicates (4)
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
/*
 * Startup tool for non statically mapped PCMCIA sockets
 *
 * (C) 2005		Dominik Brodowski <linux@brodo.de>
 *
 *  The initial developer of the original code is David A. Hinds
 *  <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
 *  are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
 *
 * License: GPL v2
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "startup.h"

/* uncomment for debug output */
#ifdef DEBUG
#define dprintf printf
#else
#define dprintf(...) do { } while (0);
#endif

/* Linked list of resource adjustments */
struct adjust_list_t *root_adjust;

/* path for config file, device scripts */
static char *configpath = "/etc/pcmcia";

enum {
	RESOURCE_MEM,
	RESOURCE_IO,
	MAX_RESOURCE_FILES
};


static const char *resource_files[MAX_RESOURCE_FILES] = {
	[RESOURCE_MEM]	= "available_resources_mem",
	[RESOURCE_IO]	= "available_resources_io",
};

#define PATH_TO_SOCKET "/sys/class/pcmcia_socket/"

#define SYSFS_PATH_MAX 255


static int sysfs_read_file(const char *fname, char *buf, size_t buflen)
{
	ssize_t numread;
	int fd;
	int ret = 0;

	fd = open(fname, O_RDONLY);
	if (fd <= 0)
		return fd;

	numread = read(fd, buf, buflen - 1);
	if (numread < 1)
		ret = -EIO;
	else
		buf[numread] = '\0';

	close(fd);
	return ret;
}

static int sysfs_write_file(const char *fname, const char *value, size_t len)
{
	ssize_t numwrite;
	int fd;
	int ret = 0;

	fd = open(fname, O_WRONLY);
	if (fd <= 0)
		return fd;

	numwrite = write(fd, value, len);
	if ((numwrite < 1) || ((size_t) numwrite != len))
		ret = -EIO;

	close(fd);
	return ret;
}

static int add_available_resource(unsigned int socket_no, unsigned int type,
				  unsigned int action,
				  unsigned long start, unsigned long end)
{
	char file[SYSFS_PATH_MAX];
	char content[SYSFS_PATH_MAX];
	int ret;
	size_t len;

	if (type >= MAX_RESOURCE_FILES)
		return -EINVAL;

	if (end <= start)
		return -EINVAL;

	dprintf("%d %d %d 0x%lx 0x%lx\n", socket_no, type, action, start, end);

	snprintf(file, SYSFS_PATH_MAX, PATH_TO_SOCKET "pcmcia_socket%u/%s",
		socket_no, resource_files[type]);

	switch (action) {
	case ADD_MANAGED_RESOURCE:
		len = snprintf(content, SYSFS_PATH_MAX,
			       "0x%08lx - 0x%08lx", start, end);
		break;

	case REMOVE_MANAGED_RESOURCE:
		len = snprintf(content, SYSFS_PATH_MAX,
			       "- 0x%08lx - 0x%08lx", start, end);
		break;

	default:
		return -EINVAL;
	}

	dprintf("content is %s, file is %s\n", content, file);

	ret = sysfs_write_file(file, content, len);

	dprintf("ret is %d\n", ret);

	return ret;
}

static int setup_done(unsigned int socket_no)
{
	char file[SYSFS_PATH_MAX];

	snprintf(file, SYSFS_PATH_MAX, PATH_TO_SOCKET
		 "pcmcia_socket%u/available_resources_setup_done",
		 socket_no);

	return sysfs_write_file(file, "42", 2);
}


static int disallow_irq(unsigned int socket_no, unsigned int irq)
{
	char file[SYSFS_PATH_MAX];
	char content[SYSFS_PATH_MAX];
	unsigned int mask = 0xfff;
	unsigned int new_mask;
	int ret;
	size_t len;

	if (irq >= 32)
		return -EINVAL;

	len = snprintf(file, SYSFS_PATH_MAX, PATH_TO_SOCKET
		 "pcmcia_socket%u/card_irq_mask",
		 socket_no);
	dprintf("file is %s\n", file);

	ret = sysfs_read_file(file, content, SYSFS_PATH_MAX);
	if (ret)
		return -ENODEV;

	ret = sscanf(content, "0x%x\n", &mask);
	if (ret != 1)
		return -EIO;

	new_mask = 1 << irq;

	mask &= ~new_mask;

	len = snprintf(content, SYSFS_PATH_MAX, "0x%04x", mask);

	dprintf("content is %s\n", content);

	return sysfs_write_file(file, content, len);
}


static void load_config(void)
{
	if (chdir(configpath) != 0) {
		syslog(LOG_ERR, "chdir to %s failed: %m", configpath);
		exit(EXIT_FAILURE);
	}
	if (parse_configfile("config.opts") == -1)
                parse_configfile("/usr/lib/pcmciautils/config.opts");
	return;
}


static void adjust_resources(unsigned int socket_no)
{
	adjust_list_t *al;

	for (al = root_adjust; al; al = al->next) {
		switch (al->adj.Resource) {
		case RES_MEMORY_RANGE:
			add_available_resource(socket_no, RESOURCE_MEM,
					al->adj.Action,
					al->adj.resource.memory.Base,
					al->adj.resource.memory.Base +
					al->adj.resource.memory.Size - 1);
			break;
		case RES_IO_RANGE:
			add_available_resource(socket_no, RESOURCE_IO,
					al->adj.Action,
					al->adj.resource.io.BasePort,
					al->adj.resource.io.BasePort +
					al->adj.resource.io.NumPorts - 1);
			break;
		case RES_IRQ:
			if (al->adj.Action == REMOVE_MANAGED_RESOURCE)
				disallow_irq(socket_no,
					al->adj.resource.irq.IRQ);
			break;
		}
	}
}


int main(int argc, char *argv[])
{
	char *socket_no;
	unsigned long socket, i;
	unsigned int all_sockets = 0;

	socket_no = getenv("SOCKET_NO");
	if (socket_no)
		socket = strtoul(socket_no, NULL, 0);
	else if (argc == 2)
		socket = strtoul(argv[1], NULL, 0);
	else if (argc == 1) {
		socket = 0;
		all_sockets = 1;
	} else
		return -EINVAL;

	load_config();

	for (i = 0; i < MAX_SOCKS; i++) {
		if ((socket != i) && (!all_sockets))
			continue;

		adjust_resources(i);
		setup_done(i);
	}

	return 0;
}