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 struct ctl_table vars[12];
263 struct ctl_table device_dir[2];
266 static const struct parport_sysctl_table parport_sysctl_template = {
268 .devices_header = NULL,
271 .procname = "spintime",
273 .maxlen = sizeof(int),
275 .proc_handler = proc_dointvec_minmax,
276 .extra1 = (void*) &parport_min_spintime_value,
277 .extra2 = (void*) &parport_max_spintime_value
280 .procname = "base-addr",
284 .proc_handler = do_hardware_base_addr
291 .proc_handler = do_hardware_irq
298 .proc_handler = do_hardware_dma
305 .proc_handler = do_hardware_modes
307 #ifdef CONFIG_PARPORT_1284
309 .procname = "autoprobe",
313 .proc_handler = do_autoprobe
316 .procname = "autoprobe0",
320 .proc_handler = do_autoprobe
323 .procname = "autoprobe1",
327 .proc_handler = do_autoprobe
330 .procname = "autoprobe2",
334 .proc_handler = do_autoprobe
337 .procname = "autoprobe3",
341 .proc_handler = do_autoprobe
343 #endif /* IEEE 1284 support */
348 .procname = "active",
352 .proc_handler = do_active_device
358 struct parport_device_sysctl_table
360 struct ctl_table_header *sysctl_header;
361 struct ctl_table vars[2];
362 struct ctl_table device_dir[2];
363 struct ctl_table devices_root_dir[2];
364 struct ctl_table port_dir[2];
365 struct ctl_table parport_dir[2];
366 struct ctl_table dev_dir[2];
369 static const struct parport_device_sysctl_table
370 parport_device_sysctl_template = {
371 .sysctl_header = NULL,
374 .procname = "timeslice",
376 .maxlen = sizeof(unsigned long),
378 .proc_handler = proc_doulongvec_ms_jiffies_minmax,
379 .extra1 = (void*) &parport_min_timeslice_value,
380 .extra2 = (void*) &parport_max_timeslice_value
395 struct parport_default_sysctl_table
397 struct ctl_table_header *sysctl_header;
398 struct ctl_table vars[3];
399 struct ctl_table default_dir[2];
400 struct ctl_table parport_dir[2];
401 struct ctl_table dev_dir[2];
404 static struct parport_default_sysctl_table
405 parport_default_sysctl_table = {
406 .sysctl_header = NULL,
409 .procname = "timeslice",
410 .data = &parport_default_timeslice,
411 .maxlen = sizeof(parport_default_timeslice),
413 .proc_handler = proc_doulongvec_ms_jiffies_minmax,
414 .extra1 = (void*) &parport_min_timeslice_value,
415 .extra2 = (void*) &parport_max_timeslice_value
418 .procname = "spintime",
419 .data = &parport_default_spintime,
420 .maxlen = sizeof(parport_default_spintime),
422 .proc_handler = proc_dointvec_minmax,
423 .extra1 = (void*) &parport_min_spintime_value,
424 .extra2 = (void*) &parport_max_spintime_value
430 int parport_proc_register(struct parport *port)
432 struct parport_sysctl_table *t;
434 size_t tmp_path_len, port_name_len;
435 int bytes_written, i, err = 0;
437 t = kmemdup(&parport_sysctl_template, sizeof(*t), GFP_KERNEL);
441 t->device_dir[0].extra1 = port;
443 t->vars[0].data = &port->spintime;
444 for (i = 0; i < 5; i++) {
445 t->vars[i].extra1 = port;
446 t->vars[5 + i].extra2 = &port->probe_info[i];
449 port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN);
451 * Allocate a buffer for two paths: dev/parport/PORT and dev/parport/PORT/devices.
452 * We calculate for the second as that will give us enough for the first.
454 tmp_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len;
455 tmp_dir_path = kzalloc(tmp_path_len, GFP_KERNEL);
461 bytes_written = snprintf(tmp_dir_path, tmp_path_len,
462 "dev/parport/%s/devices", port->name);
463 if (tmp_path_len <= bytes_written) {
465 goto exit_free_tmp_dir_path;
467 t->devices_header = register_sysctl(tmp_dir_path, t->device_dir);
468 if (t->devices_header == NULL) {
470 goto exit_free_tmp_dir_path;
473 tmp_path_len = PARPORT_BASE_PATH_SIZE + port_name_len;
474 bytes_written = snprintf(tmp_dir_path, tmp_path_len,
475 "dev/parport/%s", port->name);
476 if (tmp_path_len <= bytes_written) {
478 goto unregister_devices_h;
481 t->port_header = register_sysctl(tmp_dir_path, t->vars);
482 if (t->port_header == NULL) {
484 goto unregister_devices_h;
487 port->sysctl_table = t;
492 unregister_devices_h:
493 unregister_sysctl_table(t->devices_header);
495 exit_free_tmp_dir_path:
503 int parport_proc_unregister(struct parport *port)
505 if (port->sysctl_table) {
506 struct parport_sysctl_table *t = port->sysctl_table;
507 port->sysctl_table = NULL;
508 unregister_sysctl_table(t->devices_header);
509 unregister_sysctl_table(t->port_header);
515 int parport_device_proc_register(struct pardevice *device)
517 int bytes_written, err = 0;
518 struct parport_device_sysctl_table *t;
519 struct parport * port = device->port;
520 size_t port_name_len, device_name_len, tmp_dir_path_len;
523 t = kmemdup(&parport_device_sysctl_template, sizeof(*t), GFP_KERNEL);
527 port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN);
528 device_name_len = strnlen(device->name, PATH_MAX);
530 /* Allocate a buffer for two paths: dev/parport/PORT/devices/DEVICE. */
531 tmp_dir_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len + device_name_len;
532 tmp_dir_path = kzalloc(tmp_dir_path_len, GFP_KERNEL);
538 bytes_written = snprintf(tmp_dir_path, tmp_dir_path_len, "dev/parport/%s/devices/%s",
539 port->name, device->name);
540 if (tmp_dir_path_len <= bytes_written) {
545 t->vars[0].data = &device->timeslice;
547 t->sysctl_header = register_sysctl(tmp_dir_path, t->vars);
548 if (t->sysctl_header == NULL) {
552 device->sysctl_table = t;
566 int parport_device_proc_unregister(struct pardevice *device)
568 if (device->sysctl_table) {
569 struct parport_device_sysctl_table *t = device->sysctl_table;
570 device->sysctl_table = NULL;
571 unregister_sysctl_table(t->sysctl_header);
577 static int __init parport_default_proc_register(void)
581 parport_default_sysctl_table.sysctl_header =
582 register_sysctl("dev/parport/default", parport_default_sysctl_table.vars);
583 if (!parport_default_sysctl_table.sysctl_header)
585 ret = parport_bus_init();
587 unregister_sysctl_table(parport_default_sysctl_table.
594 static void __exit parport_default_proc_unregister(void)
596 if (parport_default_sysctl_table.sysctl_header) {
597 unregister_sysctl_table(parport_default_sysctl_table.
599 parport_default_sysctl_table.sysctl_header = NULL;
604 #else /* no sysctl or no procfs*/
606 int parport_proc_register(struct parport *pp)
611 int parport_proc_unregister(struct parport *pp)
616 int parport_device_proc_register(struct pardevice *device)
621 int parport_device_proc_unregister(struct pardevice *device)
626 static int __init parport_default_proc_register (void)
628 return parport_bus_init();
631 static void __exit parport_default_proc_unregister (void)
637 subsys_initcall(parport_default_proc_register)
638 module_exit(parport_default_proc_unregister)