1 // SPDX-License-Identifier: GPL-2.0
2 /* Sysctl interface for parport devices.
4 * Authors: David Campbell
5 * Tim Waugh <tim@cyberelk.demon.co.uk>
6 * Philip Blundell <philb@gnu.org>
8 * Riccardo Facchetti <fizban@tin.it>
10 * based on work by Grant Guenther <grant@torque.net>
13 * Cleaned up include files - Russell King <linux@arm.uk.linux.org>
16 #include <linux/string.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/parport.h>
23 #include <linux/ctype.h>
24 #include <linux/sysctl.h>
25 #include <linux/device.h>
27 #include <linux/uaccess.h>
29 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
31 #define PARPORT_MIN_TIMESLICE_VALUE 1ul
32 #define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ)
33 #define PARPORT_MIN_SPINTIME_VALUE 1
34 #define PARPORT_MAX_SPINTIME_VALUE 1000
36 static int do_active_device(struct ctl_table
*table
, int write
,
37 void *result
, size_t *lenp
, loff_t
*ppos
)
39 struct parport
*port
= (struct parport
*)table
->extra1
;
41 struct pardevice
*dev
;
44 if (write
) /* can't happen anyway */
52 for (dev
= port
->devices
; dev
; dev
= dev
->next
) {
53 if(dev
== port
->cad
) {
54 len
+= sprintf(buffer
, "%s\n", dev
->name
);
59 len
+= sprintf(buffer
, "%s\n", "none");
68 memcpy(result
, buffer
, len
);
72 #ifdef CONFIG_PARPORT_1284
73 static int do_autoprobe(struct ctl_table
*table
, int write
,
74 void *result
, size_t *lenp
, loff_t
*ppos
)
76 struct parport_device_info
*info
= table
->extra2
;
81 if (write
) /* permissions stop this */
89 if ((str
= info
->class_name
) != NULL
)
90 len
+= sprintf (buffer
+ len
, "CLASS:%s;\n", str
);
92 if ((str
= info
->model
) != NULL
)
93 len
+= sprintf (buffer
+ len
, "MODEL:%s;\n", str
);
95 if ((str
= info
->mfr
) != NULL
)
96 len
+= sprintf (buffer
+ len
, "MANUFACTURER:%s;\n", str
);
98 if ((str
= info
->description
) != NULL
)
99 len
+= sprintf (buffer
+ len
, "DESCRIPTION:%s;\n", str
);
101 if ((str
= info
->cmdset
) != NULL
)
102 len
+= sprintf (buffer
+ len
, "COMMAND SET:%s;\n", str
);
111 memcpy(result
, buffer
, len
);
114 #endif /* IEEE1284.3 support. */
116 static int do_hardware_base_addr(struct ctl_table
*table
, int write
,
117 void *result
, size_t *lenp
, loff_t
*ppos
)
119 struct parport
*port
= (struct parport
*)table
->extra1
;
128 if (write
) /* permissions prevent this anyway */
131 len
+= sprintf (buffer
, "%lu\t%lu\n", port
->base
, port
->base_hi
);
139 memcpy(result
, buffer
, len
);
143 static int do_hardware_irq(struct ctl_table
*table
, int write
,
144 void *result
, size_t *lenp
, loff_t
*ppos
)
146 struct parport
*port
= (struct parport
*)table
->extra1
;
155 if (write
) /* permissions prevent this anyway */
158 len
+= sprintf (buffer
, "%d\n", port
->irq
);
166 memcpy(result
, buffer
, len
);
170 static int do_hardware_dma(struct ctl_table
*table
, int write
,
171 void *result
, size_t *lenp
, loff_t
*ppos
)
173 struct parport
*port
= (struct parport
*)table
->extra1
;
182 if (write
) /* permissions prevent this anyway */
185 len
+= sprintf (buffer
, "%d\n", port
->dma
);
193 memcpy(result
, buffer
, len
);
197 static int do_hardware_modes(struct ctl_table
*table
, int write
,
198 void *result
, size_t *lenp
, loff_t
*ppos
)
200 struct parport
*port
= (struct parport
*)table
->extra1
;
209 if (write
) /* permissions prevent this anyway */
213 #define printmode(x) {if(port->modes&PARPORT_MODE_##x){len+=sprintf(buffer+len,"%s%s",f?",":"",#x);f++;}}
223 buffer
[len
++] = '\n';
231 memcpy(result
, buffer
, len
);
235 #define PARPORT_PORT_DIR(CHILD) { .procname = NULL, .mode = 0555, .child = CHILD }
236 #define PARPORT_PARPORT_DIR(CHILD) { .procname = "parport", \
237 .mode = 0555, .child = CHILD }
238 #define PARPORT_DEV_DIR(CHILD) { .procname = "dev", .mode = 0555, .child = CHILD }
239 #define PARPORT_DEVICES_ROOT_DIR { .procname = "devices", \
240 .mode = 0555, .child = NULL }
242 static const unsigned long parport_min_timeslice_value
=
243 PARPORT_MIN_TIMESLICE_VALUE
;
245 static const unsigned long parport_max_timeslice_value
=
246 PARPORT_MAX_TIMESLICE_VALUE
;
248 static const int parport_min_spintime_value
=
249 PARPORT_MIN_SPINTIME_VALUE
;
251 static const int parport_max_spintime_value
=
252 PARPORT_MAX_SPINTIME_VALUE
;
255 struct parport_sysctl_table
{
256 struct ctl_table_header
*sysctl_header
;
257 struct ctl_table vars
[12];
258 struct ctl_table device_dir
[2];
259 struct ctl_table port_dir
[2];
260 struct ctl_table parport_dir
[2];
261 struct ctl_table dev_dir
[2];
264 static const struct parport_sysctl_table parport_sysctl_template
= {
265 .sysctl_header
= NULL
,
268 .procname
= "spintime",
270 .maxlen
= sizeof(int),
272 .proc_handler
= proc_dointvec_minmax
,
273 .extra1
= (void*) &parport_min_spintime_value
,
274 .extra2
= (void*) &parport_max_spintime_value
277 .procname
= "base-addr",
281 .proc_handler
= do_hardware_base_addr
288 .proc_handler
= do_hardware_irq
295 .proc_handler
= do_hardware_dma
302 .proc_handler
= do_hardware_modes
304 PARPORT_DEVICES_ROOT_DIR
,
305 #ifdef CONFIG_PARPORT_1284
307 .procname
= "autoprobe",
311 .proc_handler
= do_autoprobe
314 .procname
= "autoprobe0",
318 .proc_handler
= do_autoprobe
321 .procname
= "autoprobe1",
325 .proc_handler
= do_autoprobe
328 .procname
= "autoprobe2",
332 .proc_handler
= do_autoprobe
335 .procname
= "autoprobe3",
339 .proc_handler
= do_autoprobe
341 #endif /* IEEE 1284 support */
346 .procname
= "active",
350 .proc_handler
= do_active_device
355 PARPORT_PORT_DIR(NULL
),
359 PARPORT_PARPORT_DIR(NULL
),
363 PARPORT_DEV_DIR(NULL
),
368 struct parport_device_sysctl_table
370 struct ctl_table_header
*sysctl_header
;
371 struct ctl_table vars
[2];
372 struct ctl_table device_dir
[2];
373 struct ctl_table devices_root_dir
[2];
374 struct ctl_table port_dir
[2];
375 struct ctl_table parport_dir
[2];
376 struct ctl_table dev_dir
[2];
379 static const struct parport_device_sysctl_table
380 parport_device_sysctl_template
= {
381 .sysctl_header
= NULL
,
384 .procname
= "timeslice",
386 .maxlen
= sizeof(unsigned long),
388 .proc_handler
= proc_doulongvec_ms_jiffies_minmax
,
389 .extra1
= (void*) &parport_min_timeslice_value
,
390 .extra2
= (void*) &parport_max_timeslice_value
404 PARPORT_DEVICES_ROOT_DIR
,
408 PARPORT_PORT_DIR(NULL
),
412 PARPORT_PARPORT_DIR(NULL
),
416 PARPORT_DEV_DIR(NULL
),
421 struct parport_default_sysctl_table
423 struct ctl_table_header
*sysctl_header
;
424 struct ctl_table vars
[3];
425 struct ctl_table default_dir
[2];
426 struct ctl_table parport_dir
[2];
427 struct ctl_table dev_dir
[2];
430 static struct parport_default_sysctl_table
431 parport_default_sysctl_table
= {
432 .sysctl_header
= NULL
,
435 .procname
= "timeslice",
436 .data
= &parport_default_timeslice
,
437 .maxlen
= sizeof(parport_default_timeslice
),
439 .proc_handler
= proc_doulongvec_ms_jiffies_minmax
,
440 .extra1
= (void*) &parport_min_timeslice_value
,
441 .extra2
= (void*) &parport_max_timeslice_value
444 .procname
= "spintime",
445 .data
= &parport_default_spintime
,
446 .maxlen
= sizeof(parport_default_spintime
),
448 .proc_handler
= proc_dointvec_minmax
,
449 .extra1
= (void*) &parport_min_spintime_value
,
450 .extra2
= (void*) &parport_max_spintime_value
456 .procname
= "default",
458 .child
= parport_default_sysctl_table
.vars
463 PARPORT_PARPORT_DIR(parport_default_sysctl_table
.default_dir
),
467 PARPORT_DEV_DIR(parport_default_sysctl_table
.parport_dir
),
473 int parport_proc_register(struct parport
*port
)
475 struct parport_sysctl_table
*t
;
478 t
= kmemdup(&parport_sysctl_template
, sizeof(*t
), GFP_KERNEL
);
482 t
->device_dir
[0].extra1
= port
;
484 for (i
= 0; i
< 5; i
++)
485 t
->vars
[i
].extra1
= port
;
487 t
->vars
[0].data
= &port
->spintime
;
488 t
->vars
[5].child
= t
->device_dir
;
490 for (i
= 0; i
< 5; i
++)
491 t
->vars
[6 + i
].extra2
= &port
->probe_info
[i
];
493 t
->port_dir
[0].procname
= port
->name
;
495 t
->port_dir
[0].child
= t
->vars
;
496 t
->parport_dir
[0].child
= t
->port_dir
;
497 t
->dev_dir
[0].child
= t
->parport_dir
;
499 t
->sysctl_header
= register_sysctl_table(t
->dev_dir
);
500 if (t
->sysctl_header
== NULL
) {
504 port
->sysctl_table
= t
;
508 int parport_proc_unregister(struct parport
*port
)
510 if (port
->sysctl_table
) {
511 struct parport_sysctl_table
*t
= port
->sysctl_table
;
512 port
->sysctl_table
= NULL
;
513 unregister_sysctl_table(t
->sysctl_header
);
519 int parport_device_proc_register(struct pardevice
*device
)
521 struct parport_device_sysctl_table
*t
;
522 struct parport
* port
= device
->port
;
524 t
= kmemdup(&parport_device_sysctl_template
, sizeof(*t
), GFP_KERNEL
);
528 t
->dev_dir
[0].child
= t
->parport_dir
;
529 t
->parport_dir
[0].child
= t
->port_dir
;
530 t
->port_dir
[0].procname
= port
->name
;
531 t
->port_dir
[0].child
= t
->devices_root_dir
;
532 t
->devices_root_dir
[0].child
= t
->device_dir
;
534 t
->device_dir
[0].procname
= device
->name
;
535 t
->device_dir
[0].child
= t
->vars
;
536 t
->vars
[0].data
= &device
->timeslice
;
538 t
->sysctl_header
= register_sysctl_table(t
->dev_dir
);
539 if (t
->sysctl_header
== NULL
) {
543 device
->sysctl_table
= t
;
547 int parport_device_proc_unregister(struct pardevice
*device
)
549 if (device
->sysctl_table
) {
550 struct parport_device_sysctl_table
*t
= device
->sysctl_table
;
551 device
->sysctl_table
= NULL
;
552 unregister_sysctl_table(t
->sysctl_header
);
558 static int __init
parport_default_proc_register(void)
562 parport_default_sysctl_table
.sysctl_header
=
563 register_sysctl_table(parport_default_sysctl_table
.dev_dir
);
564 if (!parport_default_sysctl_table
.sysctl_header
)
566 ret
= parport_bus_init();
568 unregister_sysctl_table(parport_default_sysctl_table
.
575 static void __exit
parport_default_proc_unregister(void)
577 if (parport_default_sysctl_table
.sysctl_header
) {
578 unregister_sysctl_table(parport_default_sysctl_table
.
580 parport_default_sysctl_table
.sysctl_header
= NULL
;
585 #else /* no sysctl or no procfs*/
587 int parport_proc_register(struct parport
*pp
)
592 int parport_proc_unregister(struct parport
*pp
)
597 int parport_device_proc_register(struct pardevice
*device
)
602 int parport_device_proc_unregister(struct pardevice
*device
)
607 static int __init
parport_default_proc_register (void)
609 return parport_bus_init();
612 static void __exit
parport_default_proc_unregister (void)
618 subsys_initcall(parport_default_proc_register
)
619 module_exit(parport_default_proc_unregister
)