proc.c 7.25 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
Dave Jones's avatar
Dave Jones committed
2
 * /proc/bus/pnp interface for Plug and Play devices
Linus Torvalds's avatar
Linus Torvalds committed
3 4
 *
 * Written by David Hinds, dahinds@users.sourceforge.net
Dave Jones's avatar
Dave Jones committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Modified by Thomas Hood, jdthood@mail.com
 *
 * The .../devices and .../<node> and .../boot/<node> files are
 * utilized by the lspnp and setpnp utilities, supplied with the
 * pcmcia-cs package.
 *     http://pcmcia-cs.sourceforge.net
 *
 * The .../escd file is utilized by the lsescd utility written by
 * Gunther Mayer.
 *     http://home.t-online.de/home/gunther.mayer/lsescd
 *
 * The .../legacy_device_resources file is not used yet.
 *
 * The other files are human-readable.
Linus Torvalds's avatar
Linus Torvalds committed
19 20 21 22 23 24 25 26 27 28 29
 */

//#include <pcmcia/config.h>
//#include <pcmcia/k_compat.h>

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/pnpbios.h>
30
#include <linux/init.h>
Linus Torvalds's avatar
Linus Torvalds committed
31

32 33
#include <asm/uaccess.h>

Linus Torvalds's avatar
Linus Torvalds committed
34 35 36
static struct proc_dir_entry *proc_pnp = NULL;
static struct proc_dir_entry *proc_pnp_boot = NULL;

Dave Jones's avatar
Dave Jones committed
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
static int proc_read_pnpconfig(char *buf, char **start, off_t pos,
                               int count, int *eof, void *data)
{
	struct pnp_isa_config_struc pnps;

	if (pnp_bios_isapnp_config(&pnps))
		return -EIO;
	return snprintf(buf, count,
		"structure_revision %d\n"
		"number_of_CSNs %d\n"
		"ISA_read_data_port 0x%x\n",
		pnps.revision,
		pnps.no_csns,
		pnps.isa_rd_data_port
	);
}

static int proc_read_escdinfo(char *buf, char **start, off_t pos,
                              int count, int *eof, void *data)
{
	struct escd_info_struc escd;

	if (pnp_bios_escd_info(&escd))
		return -EIO;
	return snprintf(buf, count,
		"min_ESCD_write_size %d\n"
		"ESCD_size %d\n"
		"NVRAM_base 0x%x\n",
		escd.min_escd_write_size,
		escd.escd_size,
		escd.nv_storage_base
	);
}

71
#define MAX_SANE_ESCD_SIZE (32*1024)
Dave Jones's avatar
Dave Jones committed
72 73 74 75 76 77 78 79 80 81 82
static int proc_read_escd(char *buf, char **start, off_t pos,
                          int count, int *eof, void *data)
{
	struct escd_info_struc escd;
	char *tmpbuf;
	int escd_size, escd_left_to_read, n;

	if (pnp_bios_escd_info(&escd))
		return -EIO;

	/* sanity check */
83 84
	if (escd.escd_size > MAX_SANE_ESCD_SIZE) {
		printk(KERN_ERR "PnPBIOS: proc_read_escd: ESCD size reported by BIOS escd_info call is too great\n");
Dave Jones's avatar
Dave Jones committed
85 86 87 88 89 90 91 92 93
		return -EFBIG;
	}

	tmpbuf = pnpbios_kmalloc(escd.escd_size, GFP_KERNEL);
	if (!tmpbuf) return -ENOMEM;

	if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base))
		return -EIO;

94 95 96 97 98 99 100 101
	escd_size = (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1])*256;

	/* sanity check */
	if (escd_size > MAX_SANE_ESCD_SIZE) {
		printk(KERN_ERR "PnPBIOS: proc_read_escd: ESCD size reported by BIOS read_escd call is too great\n");
		return -EFBIG;
	}

Dave Jones's avatar
Dave Jones committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	escd_left_to_read = escd_size - pos;
	if (escd_left_to_read < 0) escd_left_to_read = 0;
	if (escd_left_to_read == 0) *eof = 1;
	n = min(count,escd_left_to_read);
	memcpy(buf, tmpbuf + pos, n);
	kfree(tmpbuf);
	*start = buf;
	return n;
}

static int proc_read_legacyres(char *buf, char **start, off_t pos,
                               int count, int *eof, void *data)
{
	/* Assume that the following won't overflow the buffer */
	if (pnp_bios_get_stat_res(buf)) 
		return -EIO;

	return count;  // FIXME: Return actual length
}

Linus Torvalds's avatar
Linus Torvalds committed
122 123 124 125 126 127 128
static int proc_read_devices(char *buf, char **start, off_t pos,
                             int count, int *eof, void *data)
{
	struct pnp_bios_node *node;
	u8 nodenum;
	char *p = buf;

Dave Jones's avatar
Dave Jones committed
129 130 131
	if (pos >= 0xff)
		return 0;

Linus Torvalds's avatar
Linus Torvalds committed
132 133
	node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
	if (!node) return -ENOMEM;
Dave Jones's avatar
Dave Jones committed
134 135 136 137 138 139

	for (nodenum=pos; nodenum<0xff; ) {
		u8 thisnodenum = nodenum;
		/* 26 = the number of characters per line sprintf'ed */
		if ((p - buf + 26) > count)
			break;
Adam Belay's avatar
Adam Belay committed
140
		if (pnp_bios_get_dev_node(&nodenum, PNPMODE_STATIC, node))
Linus Torvalds's avatar
Linus Torvalds committed
141 142 143 144 145
			break;
		p += sprintf(p, "%02x\t%08x\t%02x:%02x:%02x\t%04x\n",
			     node->handle, node->eisa_id,
			     node->type_code[0], node->type_code[1],
			     node->type_code[2], node->flags);
Dave Jones's avatar
Dave Jones committed
146 147 148 149 150
		if (nodenum <= thisnodenum) {
			printk(KERN_ERR "%s Node number 0x%x is out of sequence following node 0x%x. Aborting.\n", "PnPBIOS: proc_read_devices:", (unsigned int)nodenum, (unsigned int)thisnodenum);
			*eof = 1;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
151 152
	}
	kfree(node);
Dave Jones's avatar
Dave Jones committed
153 154 155 156
	if (nodenum == 0xff)
		*eof = 1;
	*start = (char *)((off_t)nodenum - pos);
	return p - buf;
Linus Torvalds's avatar
Linus Torvalds committed
157 158 159 160 161 162 163 164 165 166 167 168
}

static int proc_read_node(char *buf, char **start, off_t pos,
                          int count, int *eof, void *data)
{
	struct pnp_bios_node *node;
	int boot = (long)data >> 8;
	u8 nodenum = (long)data;
	int len;

	node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
	if (!node) return -ENOMEM;
Dave Jones's avatar
Dave Jones committed
169
	if (pnp_bios_get_dev_node(&nodenum, boot, node))
Linus Torvalds's avatar
Linus Torvalds committed
170 171 172 173 174 175 176 177 178 179 180 181 182
		return -EIO;
	len = node->size - sizeof(struct pnp_bios_node);
	memcpy(buf, node->data, len);
	kfree(node);
	return len;
}

static int proc_write_node(struct file *file, const char *buf,
                           unsigned long count, void *data)
{
	struct pnp_bios_node *node;
	int boot = (long)data >> 8;
	u8 nodenum = (long)data;
183
	int ret = count;
Linus Torvalds's avatar
Linus Torvalds committed
184 185

	node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	if (!node)
		return -ENOMEM;
	if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
		ret = -EIO;
		goto out;
	}
	if (count != node->size - sizeof(struct pnp_bios_node)) {
		ret = -EINVAL;
		goto out;
	}
	if (copy_from_user(node->data, buf, count)) {
		ret = -EFAULT;
		goto out;
	}
	if (pnp_bios_set_dev_node(node->handle, boot, node) != 0) {
		ret = -EINVAL;
		goto out;
	}
	ret = count;
out:
Linus Torvalds's avatar
Linus Torvalds committed
206
	kfree(node);
207
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
208 209
}

Adam Belay's avatar
Adam Belay committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
int pnpbios_interface_attach_device(struct pnp_bios_node * node)
{
	char name[3];
	struct proc_dir_entry *ent;

	sprintf(name, "%02x", node->handle);
	if ( !pnpbios_dont_use_current_config ) {
		ent = create_proc_entry(name, 0, proc_pnp);
		if (ent) {
			ent->read_proc = proc_read_node;
			ent->write_proc = proc_write_node;
			ent->data = (void *)(long)(node->handle);
		}
	}
	ent = create_proc_entry(name, 0, proc_pnp_boot);
	if (ent) {
		ent->read_proc = proc_read_node;
		ent->write_proc = proc_write_node;
		ent->data = (void *)(long)(node->handle+0x100);
		return 0;
	}
	return -EIO;
}

Linus Torvalds's avatar
Linus Torvalds committed
234 235 236 237 238
/*
 * When this is called, pnpbios functions are assumed to
 * work and the pnpbios_dont_use_current_config flag
 * should already have been set to the appropriate value
 */
Dave Jones's avatar
Dave Jones committed
239
int __init pnpbios_proc_init( void )
Linus Torvalds's avatar
Linus Torvalds committed
240 241
{
	proc_pnp = proc_mkdir("pnp", proc_bus);
Dave Jones's avatar
Dave Jones committed
242 243
	if (!proc_pnp)
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
244
	proc_pnp_boot = proc_mkdir("boot", proc_pnp);
Dave Jones's avatar
Dave Jones committed
245 246
	if (!proc_pnp_boot)
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
247
	create_proc_read_entry("devices", 0, proc_pnp, proc_read_devices, NULL);
Dave Jones's avatar
Dave Jones committed
248 249
	create_proc_read_entry("configuration_info", 0, proc_pnp, proc_read_pnpconfig, NULL);
	create_proc_read_entry("escd_info", 0, proc_pnp, proc_read_escdinfo, NULL);
Dave Jones's avatar
Dave Jones committed
250
	create_proc_read_entry("escd", S_IRUSR, proc_pnp, proc_read_escd, NULL);
Dave Jones's avatar
Dave Jones committed
251 252 253
	create_proc_read_entry("legacy_device_resources", 0, proc_pnp, proc_read_legacyres, NULL);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
254 255
}

Dave Jones's avatar
Dave Jones committed
256
void __exit pnpbios_proc_exit(void)
Linus Torvalds's avatar
Linus Torvalds committed
257 258 259 260 261 262 263 264 265 266 267 268
{
	int i;
	char name[3];
	
	if (!proc_pnp) return;

	for (i=0; i<0xff; i++) {
		sprintf(name, "%02x", i);
		if ( !pnpbios_dont_use_current_config )
			remove_proc_entry(name, proc_pnp);
		remove_proc_entry(name, proc_pnp_boot);
	}
Dave Jones's avatar
Dave Jones committed
269 270 271 272
	remove_proc_entry("legacy_device_resources", proc_pnp);
	remove_proc_entry("escd", proc_pnp);
	remove_proc_entry("escd_info", proc_pnp);
	remove_proc_entry("configuration_info", proc_pnp);
Linus Torvalds's avatar
Linus Torvalds committed
273
	remove_proc_entry("devices", proc_pnp);
Dave Jones's avatar
Dave Jones committed
274
	remove_proc_entry("boot", proc_pnp);
Linus Torvalds's avatar
Linus Torvalds committed
275
	remove_proc_entry("pnp", proc_bus);
Dave Jones's avatar
Dave Jones committed
276 277

	return;
Linus Torvalds's avatar
Linus Torvalds committed
278
}