summaryrefslogtreecommitdiffstats
path: root/drivers/misc/parport_procfs.c
blob: b7d35eea46e28e7903d77c3758a0d4f8f344603b (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
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
/* Parallel port /proc interface code.
 * 
 * Authors: David Campbell <campbell@torque.net>
 *          Tim Waugh <tim@cyberelk.demon.co.uk>
 *          Philip Blundell <philb@gnu.org>
 *          Andrea Arcangeli
 *          Riccardo Facchetti <fizban@tin.it>
 *
 * based on work by Grant Guenther <grant@torque.net>
 *              and Philip Blundell
 */

#include <linux/stddef.h>
#include <linux/tasks.h>
#include <linux/ctype.h>
#include <asm/ptrace.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <asm/irq.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>

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 retval = -EINVAL;
	int newirq = PARPORT_IRQ_NONE;
	struct parport *pp = (struct parport *)data;
	int oldirq = pp->irq;
	unsigned long flags;

/*
 * We can have these valid cases:
 * 	"none" (count == 4 || count == 5)
 * 	decimal number (count == 2 || count == 3)
 * 	octal number (count == 3 || count == 4)
 * 	hex number (count == 4 || count == 5)
 * all other cases are -EINVAL
 *
 * Note: newirq is alredy set up to NONE.
 *
 * -RF
 */
	if (count > 5  || count < 1)
		goto out;

	if (isdigit(buffer[0]))
		newirq = simple_strtoul(buffer, NULL, 0);
	else if (strncmp(buffer, "none", 4) != 0) {
		if (buffer[0] < 32)
			/* Things like '\n' are harmless */
			retval = count;

		goto out;
	}

	retval = count;

	if (oldirq == newirq)
		goto out;

	spin_lock_irqsave(&pp->lock, flags);
	if (pp->flags & PARPORT_FLAG_COMA)
		goto out_ok;

	retval = -EBUSY;
	if (pp->cad)
		goto out_unlock;

	if (newirq != PARPORT_IRQ_NONE) { 
		retval = request_irq(newirq, parport_null_intr_func,
				     SA_INTERRUPT, pp->name, NULL);
		if (retval)
			goto out_unlock;
		else retval = count;
	}

	if (oldirq != PARPORT_IRQ_NONE)
		free_irq(oldirq, NULL);

out_ok:
	pp->irq = newirq;

out_unlock:
	spin_unlock_irqrestore (&pp->lock, flags);

out:
	return retval;
}

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 {
#ifdef __sparc__
		len = sprintf(page, "%s\n", __irq_itoa(pp->irq));
#else
		len = sprintf(page, "%d\n", pp->irq);
#endif
	}
	
	*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)
			page[len++] = '+';
		else
			page[len++] = ' ';

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

		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 {
#ifdef __sparc__
		len += sprintf(page+len, "irq:\t%s\n",__irq_itoa(pp->irq));
#else
		len += sprintf(page+len, "irq:\t%d\n",pp->irq);
#endif
	}

	if (pp->dma == PARPORT_DMA_NONE)
		len += sprintf(page+len, "dma:\tnone\n");
	else
		len += sprintf(page+len, "dma:\t%d\n",pp->dma);

	len += sprintf(page+len, "modes:\t");
	{
#define printmode(x) {if(pp->modes&PARPORT_MODE_PC##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
	}
	page[len++] = '\n';

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

static int autoprobe_read_proc (char *page, char **start, off_t off,
				int count, int *eof, void *data)
{
	struct parport *pp = (struct parport *) data;
	int len = 0;
	const char *str;

	page[0] = '\0';

	if ((str = pp->probe_info.class_name) != NULL)
		len += sprintf (page+len, "CLASS:%s;\n", str);

	if ((str = pp->probe_info.model) != NULL)
		len += sprintf (page+len, "MODEL:%s;\n", str);

	if ((str = pp->probe_info.mfr) != NULL)
		len += sprintf (page+len, "MANUFACTURER:%s;\n", str);

	if ((str = pp->probe_info.description) != NULL)
		len += sprintf (page+len, "DESCRIPTION:%s;\n", str);

	if ((str = pp->probe_info.cmdset) != NULL)
		len += sprintf (page+len, "COMMAND SET:%s;\n", str);

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

static inline void destroy_proc_entry(struct proc_dir_entry *root, 
				      struct proc_dir_entry **d)
{
	proc_unregister(root, (*d)->low_ino);
	kfree(*d);
	*d = NULL;
}

static void destroy_proc_tree(struct parport *pp) {
	if (pp->pdir.entry) {
		if (pp->pdir.irq) 
			destroy_proc_entry(pp->pdir.entry, &pp->pdir.irq);
		if (pp->pdir.devices) 
			destroy_proc_entry(pp->pdir.entry, &pp->pdir.devices);
		if (pp->pdir.hardware)
			destroy_proc_entry(pp->pdir.entry, &pp->pdir.hardware);
		if (pp->pdir.probe)
			destroy_proc_entry(pp->pdir.entry, &pp->pdir.probe);
		destroy_proc_entry(base, &pp->pdir.entry);
	}
}

static struct proc_dir_entry *new_proc_entry(const char *name, mode_t mode,
					     struct proc_dir_entry *parent,
					     unsigned short ino,
					     struct parport *p)
{
	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))
	{
		if (p && p->ops)
			ent->fill_inode = p->ops->fill_inode;
		ent->nlink = 2;
	} else
		ent->nlink = 1;

	proc_register(parent, ent);
	
	return ent;
}

/*
 * This is called as the fill_inode function when an inode
 * is going into (fill = 1) or out of service (fill = 0).
 * We use it here to manage the module use counts.
 *
 * Note: only the top-level directory needs to do this; if
 * a lower level is referenced, the parent will be as well.
 */
static void parport_modcount(struct inode *inode, int fill)
{
#ifdef MODULE
	if (fill)
		inc_parport_count();
	else
		dec_parport_count();
#endif
}

int parport_proc_init(void)
{
	base = new_proc_entry("parport", S_IFDIR, &proc_root,PROC_PARPORT,
			      NULL);
	base->fill_inode = &parport_modcount;

	if (base == NULL) {
		printk(KERN_ERR "Unable to initialise /proc/parport.\n");
		return 0;
	}

	return 1;
}

void parport_proc_cleanup(void)
{
	if (base) {
		proc_unregister(&proc_root,base->low_ino);
		kfree(base);
		base = NULL;
	}
}

int parport_proc_register(struct parport *pp)
{
	memset(&pp->pdir, 0, sizeof(struct parport_dir));

	if (base == NULL) {
		printk(KERN_ERR "parport_proc not initialised yet.\n");
		return 1;
	}
	
	strncpy(pp->pdir.name, pp->name + strlen("parport"), 
		sizeof(pp->pdir.name));

	pp->pdir.entry = new_proc_entry(pp->pdir.name, S_IFDIR, base, 0, pp);
	if (pp->pdir.entry == NULL)
		goto out_fail;

	pp->pdir.irq = new_proc_entry("irq", S_IFREG | S_IRUGO | S_IWUSR, 
				      pp->pdir.entry, 0, pp);
	if (pp->pdir.irq == NULL)
		goto out_fail;

	pp->pdir.irq->read_proc = irq_read_proc;
	pp->pdir.irq->write_proc = irq_write_proc;
	pp->pdir.irq->data = pp;
	
	pp->pdir.devices = new_proc_entry("devices", 0, pp->pdir.entry, 0, pp);
	if (pp->pdir.devices == NULL)
		goto out_fail;

	pp->pdir.devices->read_proc = devices_read_proc;
	pp->pdir.devices->data = pp;
	
	pp->pdir.hardware = new_proc_entry("hardware", 0, pp->pdir.entry, 0,
					   pp);
	if (pp->pdir.hardware == NULL)
		goto out_fail;

	pp->pdir.hardware->read_proc = hardware_read_proc;
	pp->pdir.hardware->data = pp;

	pp->pdir.probe = new_proc_entry("autoprobe", 0, pp->pdir.entry, 0, pp);
	if (pp->pdir.probe == NULL)
		goto out_fail;

	pp->pdir.probe->read_proc = autoprobe_read_proc;
	pp->pdir.probe->data = pp;

	return 0;

out_fail:

	printk(KERN_ERR "%s: failure registering /proc/ entry.\n", pp->name);
	destroy_proc_tree(pp);
	return 1;
}

int parport_proc_unregister(struct parport *pp)
{
	destroy_proc_tree(pp);
	return 0;
}