summaryrefslogtreecommitdiffstats
path: root/drivers/misc/parport_procfs.c
blob: e75fba3351f2cf716a382314510a2657e270087f (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
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
/* Parallel port /proc interface code.
 * 
 * Authors: David Campbell <campbell@tirian.che.curtin.edu.au>
 *          Tim Waugh <tmw20@cam.ac.uk>
 *
 * based on work by Grant Guenther <grant@torque.net>
 *              and Philip Blundell <Philip.Blundell@pobox.com>
 */

#include <linux/stddef.h>
#include <linux/tasks.h>
#include <asm/ptrace.h>
#include <asm/io.h>
#include <asm/dma.h>

#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/malloc.h>

#include <linux/proc_fs.h>

#include <linux/parport.h>

#undef PARPORT_INCLUDE_BENCH

struct proc_dir_entry *base=NULL;

extern void parport_null_intr_func(int irq, void *dev_id, struct pt_regs *regs);

static int irq_write_proc(struct file *file, const char *buffer,
					  unsigned long count, void *data)
{
	int newirq;
	struct parport *pp = (struct parport *)data;
	
	if (count > 4 )  /* more than 4 digits for a irq 0x?? 0?? ??  */
		return(-EOVERFLOW);

	if (buffer[0] < 32 || !strncmp(buffer, "none", 4)) {
		newirq = PARPORT_IRQ_NONE;
	} else {
		if (buffer[0] == '0') {
			if( buffer[1] == 'x' )
				newirq = simple_strtoul(&buffer[2],0,16);
			else
				newirq = simple_strtoul(&buffer[1],0,8);
		} else {
			newirq = simple_strtoul(buffer,0,10);
		}
	}

	if (pp->irq != PARPORT_IRQ_NONE && !(pp->flags & PARPORT_FLAG_COMA)) 
		free_irq(pp->irq, pp);

	pp->irq = newirq;

	if (pp->irq != PARPORT_IRQ_NONE && !(pp->flags & PARPORT_FLAG_COMA)) { 
		struct pardevice *pd = pp->cad;

		if (pd == NULL) {
			pd = pp->devices;
			if (pd != NULL) 
				request_irq(pp->irq, pd->irq_func ? 
					    pd->irq_func :
					    parport_null_intr_func,
					    SA_INTERRUPT, pd->name, pd->port);
		} else {
			request_irq(pp->irq, pd->irq_func ? pd->irq_func :
				    parport_null_intr_func,
				    SA_INTERRUPT, pp->name, pd->port);
		}
	}

	return count;
}

static int irq_read_proc(char *page, char **start, off_t off,
					 int count, int *eof, void *data)
{
	struct parport *pp = (struct parport *)data;
	int len;
	
	if (pp->irq == PARPORT_IRQ_NONE)
		len = sprintf(page, "none\n");
	else
		len = sprintf(page, "%d\n", pp->irq);
	
	*start = 0;
	*eof   = 1;
	return len;
}

static int devices_read_proc(char *page, char **start, off_t off,
					 int count, int *eof, void *data)
{
	struct parport *pp = (struct parport *)data;
	struct pardevice *pd1;
	int len=0;

	for (pd1 = pp->devices; pd1 ; pd1 = pd1->next) {
		if (pd1 == pp->cad)
			len += sprintf(page+len, "+");
		else
			len += sprintf(page+len, " ");

		len += sprintf(page+len, "%s",pd1->name);

		if (pd1 == pp->lurker)
			len += sprintf(page+len, " LURK");
		
		len += sprintf(page+len,"\n");
	}
		
	*start = 0;
	*eof   = 1;
	return len;
}

static int hardware_read_proc(char *page, char **start, off_t off,
							  int count, int *eof, void *data)
{
	struct parport *pp = (struct parport *)data;
	int len=0;
	
	len += sprintf(page+len, "base:\t0x%lx\n",pp->base);
	if (pp->irq == PARPORT_IRQ_NONE)
		len += sprintf(page+len, "irq:\tnone\n");
	else
		len += sprintf(page+len, "irq:\t%d\n",pp->irq);
	if (pp->dma == PARPORT_DMA_NONE)
		len += sprintf(page+len, "dma:\tnone\n");
	else
		len += sprintf(page+len, "dma:\t%d\n",pp->dma);


#if 0
	len += sprintf(page+len, "modes:\t");
	{
#define printmode(x) {if(pp->modes&PARPORT_MODE_##x){len+=sprintf(page+len,"%s%s",f?",":"",#x);f++;}}
		int f = 0;
		printmode(SPP);
		printmode(PS2);
		printmode(EPP);
		printmode(ECP);
		printmode(ECPEPP);
		printmode(ECPPS2);
#undef printmode
	}
	len += sprintf(page+len, "\n");

	len += sprintf(page+len, "mode:\t");
	if (pp->modes & PARPORT_MODE_ECR) {
		switch (r_ecr(pp) >> 5) {
		case 0:
			len += sprintf(page+len, "SPP");
			if( pp->modes & PARPORT_MODE_PS2 )
				len += sprintf(page+len, ",PS2");
			if( pp->modes & PARPORT_MODE_EPP )
				len += sprintf(page+len, ",EPP");
			break;
		case 1:
			len += sprintf(page+len, "ECPPS2");
			break;
		case 2:
			len += sprintf(page+len, "DATAFIFO");
			break;
		case 3:
			len += sprintf(page+len, "ECP");
			break;
		case 4:
			len += sprintf(page+len, "ECPEPP");
			break;
		case 5:
			len += sprintf(page+len, "Reserved?");
			break;
		case 6:
			len += sprintf(page+len, "TEST");
			break;
		case 7:
			len += sprintf(page+len, "Configuration");
			break;
		}
	} else {
		len += sprintf(page+len, "SPP");
		if (pp->modes & PARPORT_MODE_PS2)
			len += sprintf(page+len, ",PS2");
		if (pp->modes & PARPORT_MODE_EPP)
			len += sprintf(page+len, ",EPP");
	}
	len += sprintf(page+len, "\n");
#endif	
#if 0
	/* Now no detection, please fix with an external function */
	len += sprintf(page+len, "chipset:\tunknown\n");
#endif
#ifdef PARPORT_INCLUDE_BENCHMARK
	if (pp->speed)
		len += sprintf(page+len, "bench:\t%d Bytes/s\n",pp->speed);
	else
		len += sprintf(page+len, "bench:\tunknown\n");
#endif	

	*start = 0;
	*eof   = 1;
	return len;
}

static struct proc_dir_entry *new_proc_entry(const char *name, mode_t mode,
					     struct proc_dir_entry *parent,
					     unsigned short ino)
{
	struct proc_dir_entry *ent;

	ent = kmalloc(sizeof(struct proc_dir_entry), GFP_KERNEL);
	if (!ent)
		return NULL;
	memset(ent, 0, sizeof(struct proc_dir_entry));
	
	if (mode == S_IFDIR)
		mode |= S_IRUGO | S_IXUGO;
	else if (mode == 0)
		mode = S_IFREG | S_IRUGO;


	ent->low_ino = ino;
	ent->name = name;
	ent->namelen = strlen(name);
	ent->mode = mode;
	if (S_ISDIR(mode)) 
		ent->nlink = 2;
	else
		ent->nlink = 1;

	proc_register(parent, ent);
	
	return ent;
}


int parport_proc_init()
{
	base = new_proc_entry("parport", S_IFDIR, &proc_root,PROC_PARPORT);

	if (base)
		return 1;
	else {
		printk(KERN_ERR "parport: Error creating proc entry /proc/parport\n");
		return 0;
	}
}

int parport_proc_cleanup()
{
	if (base)
		proc_unregister(&proc_root,base->low_ino);

	base = NULL;
	
	return 0;
}

int parport_proc_register(struct parport *pp)
{
	struct proc_dir_entry *ent;
	static int conta=0;
	char *name;

	memset(&pp->pdir,0,sizeof(struct parport_dir));

	if (!base) {
		printk(KERN_ERR "parport: Error entry /proc/parport, not generated?\n");
		return 1;
	}
	
	name = pp->pdir.name;
	sprintf(name,"%d",conta++);

	ent = new_proc_entry(name, S_IFDIR, base,0);
	if (!ent) {
		printk(KERN_ERR "parport: Error registering proc_entry /proc/%s\n",name);
		return 1;
	}
	pp->pdir.entry = ent;

	ent = new_proc_entry("irq", S_IFREG | S_IRUGO | S_IWUSR, pp->pdir.entry,0);
	if (!ent) {
		printk(KERN_ERR "parport: Error registering proc_entry /proc/%s/irq\n",name);
		return 1;
	}
	ent->read_proc = irq_read_proc;
	ent->write_proc= irq_write_proc;
	ent->data      = pp;
	pp->pdir.irq   = ent;
	
	ent = new_proc_entry("devices", 0, pp->pdir.entry,0);
	if (!ent) {
		printk(KERN_ERR "parport: Error registering proc_entry /proc/%s/devices\n",name);
		return 1;
	}
	ent->read_proc   = devices_read_proc;
	ent->data        = pp;
	pp->pdir.devices = ent;
	
	ent = new_proc_entry("hardware", 0, pp->pdir.entry,0);
	if (!ent) {
		printk(KERN_ERR "parport: Error registering proc_entry /proc/%s/hardware\n",name);
		return 1;
	}
	ent->read_proc    = hardware_read_proc;
	ent->data         = pp;
	pp->pdir.hardware = ent;
	return 0;
}

int parport_proc_unregister(struct parport *pp)
{
	if (pp->pdir.entry) {
		if (pp->pdir.irq) {
			proc_unregister(pp->pdir.entry, pp->pdir.irq->low_ino);
			kfree(pp->pdir.irq);
		}
		
		if (pp->pdir.devices) {
			proc_unregister(pp->pdir.entry,
					pp->pdir.devices->low_ino);
			kfree(pp->pdir.devices);
		}
		
		if (pp->pdir.hardware) {
			proc_unregister(pp->pdir.entry,
					pp->pdir.hardware->low_ino);
			kfree(pp->pdir.hardware);
		}
		
		proc_unregister(base, pp->pdir.entry->low_ino);
		kfree(pp->pdir.entry);
	}
	
	return 0;
}