Import 2.3.18pre1
[davej-history.git] / drivers / parport / procfs.c
blob29a3a7e2932a6c5b6c2cdc345e087cfb0b72d2e3
1 /* Sysctl interface for parport devices.
2 *
3 * Authors: David Campbell <campbell@torque.net>
4 * Tim Waugh <tim@cyberelk.demon.co.uk>
5 * Philip Blundell <philb@gnu.org>
6 * Andrea Arcangeli
7 * Riccardo Facchetti <fizban@tin.it>
9 * based on work by Grant Guenther <grant@torque.net>
10 * and Philip Blundell
12 * Cleaned up include files - Russell King <linux@arm.uk.linux.org>
15 #include <linux/string.h>
16 #include <linux/config.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/malloc.h>
20 #include <linux/parport.h>
21 #include <linux/ctype.h>
22 #include <linux/sysctl.h>
24 #include <asm/uaccess.h>
26 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
28 #define PARPORT_MIN_TIMESLICE_VALUE 1ul
29 #define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ)
30 #define PARPORT_MIN_SPINTIME_VALUE 1
31 #define PARPORT_MAX_SPINTIME_VALUE 1000
33 static int do_active_device(ctl_table *table, int write, struct file *filp,
34 void *result, size_t *lenp)
36 struct parport *port = (struct parport *)table->extra1;
37 char buffer[256];
38 struct pardevice *dev;
39 int len = 0;
41 if (write) /* can't happen anyway */
42 return -EACCES;
44 if (filp->f_pos) {
45 *lenp = 0;
46 return 0;
49 for (dev = port->devices; dev ; dev = dev->next) {
50 if(dev == port->cad) {
51 len += sprintf(buffer, "%s\n", dev->name);
55 if(!len) {
56 len += sprintf(buffer, "%s\n", "none");
59 if (len > *lenp)
60 len = *lenp;
61 else
62 *lenp = len;
64 filp->f_pos += len;
66 return copy_to_user(result, buffer, len) ? -EFAULT : 0;
69 #ifdef CONFIG_PARPORT_1284
70 static int do_autoprobe(ctl_table *table, int write, struct file *filp,
71 void *result, size_t *lenp)
73 struct parport_device_info *info = table->extra2;
74 const char *str;
75 char buffer[256];
76 int len = 0;
78 if (write) /* permissions stop this */
79 return -EACCES;
81 if (filp->f_pos) {
82 *lenp = 0;
83 return 0;
86 if ((str = info->class_name) != NULL)
87 len += sprintf (buffer + len, "CLASS:%s;\n", str);
89 if ((str = info->model) != NULL)
90 len += sprintf (buffer + len, "MODEL:%s;\n", str);
92 if ((str = info->mfr) != NULL)
93 len += sprintf (buffer + len, "MANUFACTURER:%s;\n", str);
95 if ((str = info->description) != NULL)
96 len += sprintf (buffer + len, "DESCRIPTION:%s;\n", str);
98 if ((str = info->cmdset) != NULL)
99 len += sprintf (buffer + len, "COMMAND SET:%s;\n", str);
101 if (len > *lenp)
102 len = *lenp;
103 else
104 *lenp = len;
106 filp->f_pos += len;
108 return copy_to_user (result, buffer, len) ? -EFAULT : 0;
110 #endif /* IEEE1284.3 support. */
112 static int do_hardware(ctl_table *table, int write, struct file *filp,
113 void *result, size_t *lenp)
115 struct parport *port = (struct parport *)table->extra1;
116 char buffer[256];
117 int len = 0;
119 if (filp->f_pos) {
120 *lenp = 0;
121 return 0;
124 if (write) /* can't happen anyway */
125 return -EACCES;
127 len += sprintf(buffer+len, "base:\t0x%lx", port->base);
128 if (port->base_hi)
129 len += sprintf(buffer+len, " (0x%lx)", port->base_hi);
130 buffer[len++] = '\n';
132 if (port->irq == PARPORT_IRQ_NONE) {
133 len += sprintf(buffer+len, "irq:\tnone\n");
134 } else {
135 len += sprintf(buffer+len, "irq:\t%d\n", port->irq);
138 if (port->dma == PARPORT_DMA_NONE)
139 len += sprintf(buffer+len, "dma:\tnone\n");
140 else
141 len += sprintf(buffer+len, "dma:\t%d\n", port->dma);
143 len += sprintf(buffer+len, "modes:\t");
145 #define printmode(x) {if(port->modes&PARPORT_MODE_##x){len+=sprintf(buffer+len,"%s%s",f?",":"",#x);f++;}}
146 int f = 0;
147 printmode(PCSPP);
148 printmode(TRISTATE);
149 printmode(COMPAT);
150 printmode(EPP);
151 printmode(ECP);
152 printmode(DMA);
153 #undef printmode
155 buffer[len++] = '\n';
157 if (len > *lenp)
158 len = *lenp;
159 else
160 *lenp = len;
162 filp->f_pos += len;
164 return copy_to_user(result, buffer, len) ? -EFAULT : 0;
167 #define PARPORT_PORT_DIR(child) { 0, NULL, NULL, 0, 0555, child }
168 #define PARPORT_PARPORT_DIR(child) { DEV_PARPORT, "parport", \
169 NULL, 0, 0555, child }
170 #define PARPORT_DEV_DIR(child) { CTL_DEV, "dev", NULL, 0, 0555, child }
171 #define PARPORT_DEVICES_ROOT_DIR { DEV_PARPORT_DEVICES, "devices", \
172 NULL, 0, 0555, NULL }
174 static const unsigned long parport_min_timeslice_value =
175 PARPORT_MIN_TIMESLICE_VALUE;
177 static const unsigned long parport_max_timeslice_value =
178 PARPORT_MAX_TIMESLICE_VALUE;
180 static const int parport_min_spintime_value =
181 PARPORT_MIN_SPINTIME_VALUE;
183 static const int parport_max_spintime_value =
184 PARPORT_MAX_SPINTIME_VALUE;
187 struct parport_sysctl_table {
188 struct ctl_table_header *sysctl_header;
189 ctl_table vars[9];
190 ctl_table device_dir[2];
191 ctl_table port_dir[2];
192 ctl_table parport_dir[2];
193 ctl_table dev_dir[2];
196 static const struct parport_sysctl_table parport_sysctl_template = {
197 NULL,
199 { DEV_PARPORT_SPINTIME, "spintime",
200 NULL, sizeof(int), 0644, NULL,
201 &proc_dointvec_minmax, NULL, NULL,
202 (void*) &parport_min_spintime_value,
203 (void*) &parport_max_spintime_value },
204 { DEV_PARPORT_HARDWARE, "hardware",
205 NULL, 0, 0444, NULL,
206 &do_hardware },
207 PARPORT_DEVICES_ROOT_DIR,
208 #ifdef CONFIG_PARPORT_1284
209 { DEV_PARPORT_AUTOPROBE, "autoprobe",
210 NULL, 0, 0444, NULL,
211 &do_autoprobe },
212 { DEV_PARPORT_AUTOPROBE + 1, "autoprobe0",
213 NULL, 0, 0444, NULL,
214 &do_autoprobe },
215 { DEV_PARPORT_AUTOPROBE + 2, "autoprobe1",
216 NULL, 0, 0444, NULL,
217 &do_autoprobe },
218 { DEV_PARPORT_AUTOPROBE + 3, "autoprobe2",
219 NULL, 0, 0444, NULL,
220 &do_autoprobe },
221 { DEV_PARPORT_AUTOPROBE + 4, "autoprobe3",
222 NULL, 0, 0444, NULL,
223 &do_autoprobe },
224 #endif /* IEEE 1284 support */
227 { {DEV_PARPORT_DEVICES_ACTIVE, "active", NULL, 0, 444, NULL,
228 &do_active_device }, {0}},
229 { PARPORT_PORT_DIR(NULL), {0}},
230 { PARPORT_PARPORT_DIR(NULL), {0}},
231 { PARPORT_DEV_DIR(NULL), {0}}
234 struct parport_device_sysctl_table
236 struct ctl_table_header *sysctl_header;
237 ctl_table vars[2];
238 ctl_table device_dir[2];
239 ctl_table devices_root_dir[2];
240 ctl_table port_dir[2];
241 ctl_table parport_dir[2];
242 ctl_table dev_dir[2];
245 static const struct parport_device_sysctl_table
246 parport_device_sysctl_template = {
247 NULL,
249 { DEV_PARPORT_DEVICE_TIMESLICE, "timeslice",
250 NULL, sizeof(int), 0644, NULL,
251 &proc_doulongvec_ms_jiffies_minmax, NULL, NULL,
252 (void*) &parport_min_timeslice_value,
253 (void*) &parport_max_timeslice_value },
255 { {0, NULL, NULL, 0, 0555, NULL}, {0}},
256 { PARPORT_DEVICES_ROOT_DIR, {0}},
257 { PARPORT_PORT_DIR(NULL), {0}},
258 { PARPORT_PARPORT_DIR(NULL), {0}},
259 { PARPORT_DEV_DIR(NULL), {0}}
262 struct parport_default_sysctl_table
264 struct ctl_table_header *sysctl_header;
265 ctl_table vars[3];
266 ctl_table default_dir[2];
267 ctl_table parport_dir[2];
268 ctl_table dev_dir[2];
271 extern unsigned long parport_default_timeslice;
272 extern int parport_default_spintime;
274 static struct parport_default_sysctl_table
275 parport_default_sysctl_table = {
276 NULL,
278 { DEV_PARPORT_DEFAULT_TIMESLICE, "timeslice",
279 &parport_default_timeslice,
280 sizeof(parport_default_timeslice), 0644, NULL,
281 &proc_doulongvec_ms_jiffies_minmax, NULL, NULL,
282 (void*) &parport_min_timeslice_value,
283 (void*) &parport_max_timeslice_value },
284 { DEV_PARPORT_DEFAULT_SPINTIME, "spintime",
285 &parport_default_spintime,
286 sizeof(parport_default_spintime), 0644, NULL,
287 &proc_dointvec_minmax, NULL, NULL,
288 (void*) &parport_min_spintime_value,
289 (void*) &parport_max_spintime_value },
292 { { DEV_PARPORT_DEFAULT, "default", NULL, 0, 0555,
293 parport_default_sysctl_table.vars },{0}},
295 PARPORT_PARPORT_DIR(parport_default_sysctl_table.default_dir),
296 {0}},
297 { PARPORT_DEV_DIR(parport_default_sysctl_table.parport_dir), {0}}
301 int parport_proc_register(struct parport *port)
303 struct parport_sysctl_table *t;
304 int i;
306 t = kmalloc(sizeof(*t), GFP_KERNEL);
307 if (t == NULL)
308 return -ENOMEM;
309 memcpy(t, &parport_sysctl_template, sizeof(*t));
311 t->device_dir[0].extra1 = port;
313 for (i = 0; i < 8; i++)
314 t->vars[i].extra1 = port;
316 t->vars[0].data = &port->spintime;
317 t->vars[2].child = t->device_dir;
319 for (i = 0; i < 5; i++)
320 t->vars[3 + i].extra2 = &port->probe_info[i];
322 t->port_dir[0].procname = port->name;
323 t->port_dir[0].ctl_name = port->number + 1; /* nb 0 isn't legal here */
325 t->port_dir[0].child = t->vars;
326 t->parport_dir[0].child = t->port_dir;
327 t->dev_dir[0].child = t->parport_dir;
329 t->sysctl_header = register_sysctl_table(t->dev_dir, 0);
330 if (t->sysctl_header == NULL) {
331 kfree(t);
332 t = NULL;
334 port->sysctl_table = t;
335 return 0;
338 int parport_proc_unregister(struct parport *port)
340 if (port->sysctl_table) {
341 struct parport_sysctl_table *t = port->sysctl_table;
342 port->sysctl_table = NULL;
343 unregister_sysctl_table(t->sysctl_header);
344 kfree(t);
346 return 0;
349 int parport_device_proc_register(struct pardevice *device)
351 struct parport_device_sysctl_table *t;
352 struct parport * port = device->port;
354 t = kmalloc(sizeof(*t), GFP_KERNEL);
355 if (t == NULL)
356 return -ENOMEM;
357 memcpy(t, &parport_device_sysctl_template, sizeof(*t));
359 t->dev_dir[0].child = t->parport_dir;
360 t->parport_dir[0].child = t->port_dir;
361 t->port_dir[0].procname = port->name;
362 t->port_dir[0].ctl_name = port->number + 1; /* nb 0 isn't legal here */
363 t->port_dir[0].child = t->devices_root_dir;
364 t->devices_root_dir[0].child = t->device_dir;
366 #ifdef CONFIG_PARPORT_1284
368 t->device_dir[0].ctl_name =
369 parport_device_num(port->number, port->muxport,
370 device->daisy)
371 + 1; /* nb 0 isn't legal here */
373 #else /* No IEEE 1284 support */
375 /* parport_device_num isn't available. */
376 t->device_dir[0].ctl_name = 1;
378 #endif /* IEEE 1284 support or not */
380 t->device_dir[0].procname = device->name;
381 t->device_dir[0].extra1 = device;
382 t->device_dir[0].child = t->vars;
383 t->vars[0].data = &device->timeslice;
385 t->sysctl_header = register_sysctl_table(t->dev_dir, 0);
386 if (t->sysctl_header == NULL) {
387 kfree(t);
388 t = NULL;
390 device->sysctl_table = t;
391 return 0;
394 int parport_device_proc_unregister(struct pardevice *device)
396 if (device->sysctl_table) {
397 struct parport_device_sysctl_table *t = device->sysctl_table;
398 device->sysctl_table = NULL;
399 unregister_sysctl_table(t->sysctl_header);
400 kfree(t);
402 return 0;
405 int parport_default_proc_register(void)
407 parport_default_sysctl_table.sysctl_header =
408 register_sysctl_table(parport_default_sysctl_table.dev_dir, 0);
409 return 0;
412 int parport_default_proc_unregister(void)
414 if (parport_default_sysctl_table.sysctl_header) {
415 unregister_sysctl_table(parport_default_sysctl_table.
416 sysctl_header);
417 parport_default_sysctl_table.sysctl_header = NULL;
419 return 0;
422 #else /* no sysctl or no procfs*/
424 int parport_proc_register(struct parport *pp)
426 return 0;
429 int parport_proc_unregister(struct parport *pp)
431 return 0;
434 int parport_device_proc_register(struct pardevice *device)
436 return 0;
439 int parport_device_proc_unregister(struct pardevice *device)
441 return 0;
444 int parport_default_proc_register (void)
446 return 0;
449 int parport_default_proc_unregister (void)
451 return 0;
453 #endif