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 * PARPORT_BASE_* is the size of the known parts of the sysctl path
37 * in dev/partport/%s/devices/%s. "dev/parport/"(12), "/devices/"(9
40 #define PARPORT_BASE_PATH_SIZE 13
41 #define PARPORT_BASE_DEVICES_PATH_SIZE 22
43 static int do_active_device(struct ctl_table *table, int write,
44 void *result, size_t *lenp, loff_t *ppos)
46 struct parport *port = (struct parport *)table->extra1;
48 struct pardevice *dev;
51 if (write) /* can't happen anyway */
59 for (dev = port->devices; dev ; dev = dev->next) {
60 if(dev == port->cad) {
61 len += sprintf(buffer, "%s\n", dev->name);
66 len += sprintf(buffer, "%s\n", "none");
75 memcpy(result, buffer, len);
79 #ifdef CONFIG_PARPORT_1284
80 static int do_autoprobe(struct ctl_table *table, int write,
81 void *result, size_t *lenp, loff_t *ppos)
83 struct parport_device_info *info = table->extra2;
88 if (write) /* permissions stop this */
96 if ((str = info->class_name) != NULL)
97 len += sprintf (buffer + len, "CLASS:%s;\n", str);
99 if ((str = info->model) != NULL)
100 len += sprintf (buffer + len, "MODEL:%s;\n", str);
102 if ((str = info->mfr) != NULL)
103 len += sprintf (buffer + len, "MANUFACTURER:%s;\n", str);
105 if ((str = info->description) != NULL)
106 len += sprintf (buffer + len, "DESCRIPTION:%s;\n", str);
108 if ((str = info->cmdset) != NULL)
109 len += sprintf (buffer + len, "COMMAND SET:%s;\n", str);
118 memcpy(result, buffer, len);
121 #endif /* IEEE1284.3 support. */
123 static int do_hardware_base_addr(struct ctl_table *table, int write,
124 void *result, size_t *lenp, loff_t *ppos)
126 struct parport *port = (struct parport *)table->extra1;
135 if (write) /* permissions prevent this anyway */
138 len += sprintf (buffer, "%lu\t%lu\n", port->base, port->base_hi);
146 memcpy(result, buffer, len);
150 static int do_hardware_irq(struct ctl_table *table, int write,
151 void *result, size_t *lenp, loff_t *ppos)
153 struct parport *port = (struct parport *)table->extra1;
162 if (write) /* permissions prevent this anyway */
165 len += sprintf (buffer, "%d\n", port->irq);
173 memcpy(result, buffer, len);
177 static int do_hardware_dma(struct ctl_table *table, int write,
178 void *result, size_t *lenp, loff_t *ppos)
180 struct parport *port = (struct parport *)table->extra1;
189 if (write) /* permissions prevent this anyway */
192 len += sprintf (buffer, "%d\n", port->dma);
200 memcpy(result, buffer, len);
204 static int do_hardware_modes(struct ctl_table *table, int write,
205 void *result, size_t *lenp, loff_t *ppos)
207 struct parport *port = (struct parport *)table->extra1;
216 if (write) /* permissions prevent this anyway */
220 #define printmode(x) \
222 if (port->modes & PARPORT_MODE_##x) \
223 len += sprintf(buffer + len, "%s%s", f++ ? "," : "", #x); \
234 buffer[len++] = '\n';
242 memcpy(result, buffer, len);
246 static const unsigned long parport_min_timeslice_value =
247 PARPORT_MIN_TIMESLICE_VALUE;
249 static const unsigned long parport_max_timeslice_value =
250 PARPORT_MAX_TIMESLICE_VALUE;
252 static const int parport_min_spintime_value =
253 PARPORT_MIN_SPINTIME_VALUE;
255 static const int parport_max_spintime_value =
256 PARPORT_MAX_SPINTIME_VALUE;
259 struct parport_sysctl_table {
260 struct ctl_table_header *port_header;
261 struct ctl_table_header *devices_header;
262 #ifdef CONFIG_PARPORT_1284
263 struct ctl_table vars[10];
265 struct ctl_table vars[5];
266 #endif /* IEEE 1284 support */
267 struct ctl_table device_dir[1];
270 static const struct parport_sysctl_table parport_sysctl_template = {
272 .devices_header = NULL,
275 .procname = "spintime",
277 .maxlen = sizeof(int),
279 .proc_handler = proc_dointvec_minmax,
280 .extra1 = (void*) &parport_min_spintime_value,
281 .extra2 = (void*) &parport_max_spintime_value
284 .procname = "base-addr",
288 .proc_handler = do_hardware_base_addr
295 .proc_handler = do_hardware_irq
302 .proc_handler = do_hardware_dma
309 .proc_handler = do_hardware_modes
311 #ifdef CONFIG_PARPORT_1284
313 .procname = "autoprobe",
317 .proc_handler = do_autoprobe
320 .procname = "autoprobe0",
324 .proc_handler = do_autoprobe
327 .procname = "autoprobe1",
331 .proc_handler = do_autoprobe
334 .procname = "autoprobe2",
338 .proc_handler = do_autoprobe
341 .procname = "autoprobe3",
345 .proc_handler = do_autoprobe
347 #endif /* IEEE 1284 support */
351 .procname = "active",
355 .proc_handler = do_active_device
360 struct parport_device_sysctl_table
362 struct ctl_table_header *sysctl_header;
363 struct ctl_table vars[1];
364 struct ctl_table device_dir[1];
367 static const struct parport_device_sysctl_table
368 parport_device_sysctl_template = {
369 .sysctl_header = NULL,
372 .procname = "timeslice",
374 .maxlen = sizeof(unsigned long),
376 .proc_handler = proc_doulongvec_ms_jiffies_minmax,
377 .extra1 = (void*) &parport_min_timeslice_value,
378 .extra2 = (void*) &parport_max_timeslice_value
391 struct parport_default_sysctl_table
393 struct ctl_table_header *sysctl_header;
394 struct ctl_table vars[2];
397 static struct parport_default_sysctl_table
398 parport_default_sysctl_table = {
399 .sysctl_header = NULL,
402 .procname = "timeslice",
403 .data = &parport_default_timeslice,
404 .maxlen = sizeof(parport_default_timeslice),
406 .proc_handler = proc_doulongvec_ms_jiffies_minmax,
407 .extra1 = (void*) &parport_min_timeslice_value,
408 .extra2 = (void*) &parport_max_timeslice_value
411 .procname = "spintime",
412 .data = &parport_default_spintime,
413 .maxlen = sizeof(parport_default_spintime),
415 .proc_handler = proc_dointvec_minmax,
416 .extra1 = (void*) &parport_min_spintime_value,
417 .extra2 = (void*) &parport_max_spintime_value
422 int parport_proc_register(struct parport *port)
424 struct parport_sysctl_table *t;
426 size_t tmp_path_len, port_name_len;
427 int bytes_written, i, err = 0;
429 t = kmemdup(&parport_sysctl_template, sizeof(*t), GFP_KERNEL);
433 t->device_dir[0].extra1 = port;
435 t->vars[0].data = &port->spintime;
436 for (i = 0; i < 5; i++) {
437 t->vars[i].extra1 = port;
438 #ifdef CONFIG_PARPORT_1284
439 t->vars[5 + i].extra2 = &port->probe_info[i];
440 #endif /* IEEE 1284 support */
443 port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN);
445 * Allocate a buffer for two paths: dev/parport/PORT and dev/parport/PORT/devices.
446 * We calculate for the second as that will give us enough for the first.
448 tmp_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len;
449 tmp_dir_path = kzalloc(tmp_path_len, GFP_KERNEL);
455 bytes_written = snprintf(tmp_dir_path, tmp_path_len,
456 "dev/parport/%s/devices", port->name);
457 if (tmp_path_len <= bytes_written) {
459 goto exit_free_tmp_dir_path;
461 t->devices_header = register_sysctl(tmp_dir_path, t->device_dir);
462 if (t->devices_header == NULL) {
464 goto exit_free_tmp_dir_path;
467 tmp_path_len = PARPORT_BASE_PATH_SIZE + port_name_len;
468 bytes_written = snprintf(tmp_dir_path, tmp_path_len,
469 "dev/parport/%s", port->name);
470 if (tmp_path_len <= bytes_written) {
472 goto unregister_devices_h;
475 t->port_header = register_sysctl(tmp_dir_path, t->vars);
476 if (t->port_header == NULL) {
478 goto unregister_devices_h;
481 port->sysctl_table = t;
486 unregister_devices_h:
487 unregister_sysctl_table(t->devices_header);
489 exit_free_tmp_dir_path:
497 int parport_proc_unregister(struct parport *port)
499 if (port->sysctl_table) {
500 struct parport_sysctl_table *t = port->sysctl_table;
501 port->sysctl_table = NULL;
502 unregister_sysctl_table(t->devices_header);
503 unregister_sysctl_table(t->port_header);
509 int parport_device_proc_register(struct pardevice *device)
511 int bytes_written, err = 0;
512 struct parport_device_sysctl_table *t;
513 struct parport * port = device->port;
514 size_t port_name_len, device_name_len, tmp_dir_path_len;
517 t = kmemdup(&parport_device_sysctl_template, sizeof(*t), GFP_KERNEL);
521 port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN);
522 device_name_len = strnlen(device->name, PATH_MAX);
524 /* Allocate a buffer for two paths: dev/parport/PORT/devices/DEVICE. */
525 tmp_dir_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len + device_name_len;
526 tmp_dir_path = kzalloc(tmp_dir_path_len, GFP_KERNEL);
532 bytes_written = snprintf(tmp_dir_path, tmp_dir_path_len, "dev/parport/%s/devices/%s",
533 port->name, device->name);
534 if (tmp_dir_path_len <= bytes_written) {
539 t->vars[0].data = &device->timeslice;
541 t->sysctl_header = register_sysctl(tmp_dir_path, t->vars);
542 if (t->sysctl_header == NULL) {
546 device->sysctl_table = t;
560 int parport_device_proc_unregister(struct pardevice *device)
562 if (device->sysctl_table) {
563 struct parport_device_sysctl_table *t = device->sysctl_table;
564 device->sysctl_table = NULL;
565 unregister_sysctl_table(t->sysctl_header);
571 static int __init parport_default_proc_register(void)
575 parport_default_sysctl_table.sysctl_header =
576 register_sysctl("dev/parport/default", parport_default_sysctl_table.vars);
577 if (!parport_default_sysctl_table.sysctl_header)
579 ret = parport_bus_init();
581 unregister_sysctl_table(parport_default_sysctl_table.
588 static void __exit parport_default_proc_unregister(void)
590 if (parport_default_sysctl_table.sysctl_header) {
591 unregister_sysctl_table(parport_default_sysctl_table.
593 parport_default_sysctl_table.sysctl_header = NULL;
598 #else /* no sysctl or no procfs*/
600 int parport_proc_register(struct parport *pp)
605 int parport_proc_unregister(struct parport *pp)
610 int parport_device_proc_register(struct pardevice *device)
615 int parport_device_proc_unregister(struct pardevice *device)
620 static int __init parport_default_proc_register (void)
622 return parport_bus_init();
625 static void __exit parport_default_proc_unregister (void)
631 subsys_initcall(parport_default_proc_register)
632 module_exit(parport_default_proc_unregister)