1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /* Sysctl interface for parport devices.
31da177e4SLinus Torvalds *
4bdca3f20SAdrian Bunk * Authors: David Campbell
51da177e4SLinus Torvalds * Tim Waugh <tim@cyberelk.demon.co.uk>
61da177e4SLinus Torvalds * Philip Blundell <philb@gnu.org>
71da177e4SLinus Torvalds * Andrea Arcangeli
81da177e4SLinus Torvalds * Riccardo Facchetti <fizban@tin.it>
91da177e4SLinus Torvalds *
101da177e4SLinus Torvalds * based on work by Grant Guenther <grant@torque.net>
111da177e4SLinus Torvalds * and Philip Blundell
121da177e4SLinus Torvalds *
131da177e4SLinus Torvalds * Cleaned up include files - Russell King <linux@arm.uk.linux.org>
141da177e4SLinus Torvalds */
151da177e4SLinus Torvalds
161da177e4SLinus Torvalds #include <linux/string.h>
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/errno.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/slab.h>
221da177e4SLinus Torvalds #include <linux/parport.h>
231da177e4SLinus Torvalds #include <linux/ctype.h>
241da177e4SLinus Torvalds #include <linux/sysctl.h>
256fa45a22SSudip Mukherjee #include <linux/device.h>
261da177e4SLinus Torvalds
277c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds #define PARPORT_MIN_TIMESLICE_VALUE 1ul
321da177e4SLinus Torvalds #define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ)
331da177e4SLinus Torvalds #define PARPORT_MIN_SPINTIME_VALUE 1
341da177e4SLinus Torvalds #define PARPORT_MAX_SPINTIME_VALUE 1000
3593810936SJoel Granados /*
3693810936SJoel Granados * PARPORT_BASE_* is the size of the known parts of the sysctl path
3793810936SJoel Granados * in dev/partport/%s/devices/%s. "dev/parport/"(12), "/devices/"(9
3893810936SJoel Granados * and null char(1).
3993810936SJoel Granados */
4093810936SJoel Granados #define PARPORT_BASE_PATH_SIZE 13
4193810936SJoel Granados #define PARPORT_BASE_DEVICES_PATH_SIZE 22
421da177e4SLinus Torvalds
do_active_device(struct ctl_table * table,int write,void * result,size_t * lenp,loff_t * ppos)43be424c63SJoe Perches static int do_active_device(struct ctl_table *table, int write,
4432927393SChristoph Hellwig void *result, size_t *lenp, loff_t *ppos)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds struct parport *port = (struct parport *)table->extra1;
471da177e4SLinus Torvalds char buffer[256];
481da177e4SLinus Torvalds struct pardevice *dev;
491da177e4SLinus Torvalds int len = 0;
501da177e4SLinus Torvalds
511da177e4SLinus Torvalds if (write) /* can't happen anyway */
521da177e4SLinus Torvalds return -EACCES;
531da177e4SLinus Torvalds
541da177e4SLinus Torvalds if (*ppos) {
551da177e4SLinus Torvalds *lenp = 0;
561da177e4SLinus Torvalds return 0;
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds
591da177e4SLinus Torvalds for (dev = port->devices; dev ; dev = dev->next) {
601da177e4SLinus Torvalds if(dev == port->cad) {
61*66029078STakashi Iwai len += scnprintf(buffer, sizeof(buffer), "%s\n", dev->name);
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds
651da177e4SLinus Torvalds if(!len) {
66*66029078STakashi Iwai len += scnprintf(buffer, sizeof(buffer), "%s\n", "none");
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds
691da177e4SLinus Torvalds if (len > *lenp)
701da177e4SLinus Torvalds len = *lenp;
711da177e4SLinus Torvalds else
721da177e4SLinus Torvalds *lenp = len;
731da177e4SLinus Torvalds
741da177e4SLinus Torvalds *ppos += len;
7532927393SChristoph Hellwig memcpy(result, buffer, len);
7632927393SChristoph Hellwig return 0;
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds
791da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
do_autoprobe(struct ctl_table * table,int write,void * result,size_t * lenp,loff_t * ppos)80be424c63SJoe Perches static int do_autoprobe(struct ctl_table *table, int write,
8132927393SChristoph Hellwig void *result, size_t *lenp, loff_t *ppos)
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds struct parport_device_info *info = table->extra2;
841da177e4SLinus Torvalds const char *str;
851da177e4SLinus Torvalds char buffer[256];
861da177e4SLinus Torvalds int len = 0;
871da177e4SLinus Torvalds
881da177e4SLinus Torvalds if (write) /* permissions stop this */
891da177e4SLinus Torvalds return -EACCES;
901da177e4SLinus Torvalds
911da177e4SLinus Torvalds if (*ppos) {
921da177e4SLinus Torvalds *lenp = 0;
931da177e4SLinus Torvalds return 0;
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds
961da177e4SLinus Torvalds if ((str = info->class_name) != NULL)
97*66029078STakashi Iwai len += scnprintf (buffer + len, sizeof(buffer) - len, "CLASS:%s;\n", str);
981da177e4SLinus Torvalds
991da177e4SLinus Torvalds if ((str = info->model) != NULL)
100*66029078STakashi Iwai len += scnprintf (buffer + len, sizeof(buffer) - len, "MODEL:%s;\n", str);
1011da177e4SLinus Torvalds
1021da177e4SLinus Torvalds if ((str = info->mfr) != NULL)
103*66029078STakashi Iwai len += scnprintf (buffer + len, sizeof(buffer) - len, "MANUFACTURER:%s;\n", str);
1041da177e4SLinus Torvalds
1051da177e4SLinus Torvalds if ((str = info->description) != NULL)
106*66029078STakashi Iwai len += scnprintf (buffer + len, sizeof(buffer) - len, "DESCRIPTION:%s;\n", str);
1071da177e4SLinus Torvalds
1081da177e4SLinus Torvalds if ((str = info->cmdset) != NULL)
109*66029078STakashi Iwai len += scnprintf (buffer + len, sizeof(buffer) - len, "COMMAND SET:%s;\n", str);
1101da177e4SLinus Torvalds
1111da177e4SLinus Torvalds if (len > *lenp)
1121da177e4SLinus Torvalds len = *lenp;
1131da177e4SLinus Torvalds else
1141da177e4SLinus Torvalds *lenp = len;
1151da177e4SLinus Torvalds
1161da177e4SLinus Torvalds *ppos += len;
1171da177e4SLinus Torvalds
11832927393SChristoph Hellwig memcpy(result, buffer, len);
11932927393SChristoph Hellwig return 0;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds #endif /* IEEE1284.3 support. */
1221da177e4SLinus Torvalds
do_hardware_base_addr(struct ctl_table * table,int write,void * result,size_t * lenp,loff_t * ppos)123be424c63SJoe Perches static int do_hardware_base_addr(struct ctl_table *table, int write,
12432927393SChristoph Hellwig void *result, size_t *lenp, loff_t *ppos)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds struct parport *port = (struct parport *)table->extra1;
127b579ea35Stuhaowen char buffer[64];
1281da177e4SLinus Torvalds int len = 0;
1291da177e4SLinus Torvalds
1301da177e4SLinus Torvalds if (*ppos) {
1311da177e4SLinus Torvalds *lenp = 0;
1321da177e4SLinus Torvalds return 0;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds
1351da177e4SLinus Torvalds if (write) /* permissions prevent this anyway */
1361da177e4SLinus Torvalds return -EACCES;
1371da177e4SLinus Torvalds
138*66029078STakashi Iwai len += scnprintf (buffer, sizeof(buffer), "%lu\t%lu\n", port->base, port->base_hi);
1391da177e4SLinus Torvalds
1401da177e4SLinus Torvalds if (len > *lenp)
1411da177e4SLinus Torvalds len = *lenp;
1421da177e4SLinus Torvalds else
1431da177e4SLinus Torvalds *lenp = len;
1441da177e4SLinus Torvalds
1451da177e4SLinus Torvalds *ppos += len;
14632927393SChristoph Hellwig memcpy(result, buffer, len);
14732927393SChristoph Hellwig return 0;
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds
do_hardware_irq(struct ctl_table * table,int write,void * result,size_t * lenp,loff_t * ppos)150be424c63SJoe Perches static int do_hardware_irq(struct ctl_table *table, int write,
15132927393SChristoph Hellwig void *result, size_t *lenp, loff_t *ppos)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds struct parport *port = (struct parport *)table->extra1;
1541da177e4SLinus Torvalds char buffer[20];
1551da177e4SLinus Torvalds int len = 0;
1561da177e4SLinus Torvalds
1571da177e4SLinus Torvalds if (*ppos) {
1581da177e4SLinus Torvalds *lenp = 0;
1591da177e4SLinus Torvalds return 0;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds
1621da177e4SLinus Torvalds if (write) /* permissions prevent this anyway */
1631da177e4SLinus Torvalds return -EACCES;
1641da177e4SLinus Torvalds
165*66029078STakashi Iwai len += scnprintf (buffer, sizeof(buffer), "%d\n", port->irq);
1661da177e4SLinus Torvalds
1671da177e4SLinus Torvalds if (len > *lenp)
1681da177e4SLinus Torvalds len = *lenp;
1691da177e4SLinus Torvalds else
1701da177e4SLinus Torvalds *lenp = len;
1711da177e4SLinus Torvalds
1721da177e4SLinus Torvalds *ppos += len;
17332927393SChristoph Hellwig memcpy(result, buffer, len);
17432927393SChristoph Hellwig return 0;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds
do_hardware_dma(struct ctl_table * table,int write,void * result,size_t * lenp,loff_t * ppos)177be424c63SJoe Perches static int do_hardware_dma(struct ctl_table *table, int write,
17832927393SChristoph Hellwig void *result, size_t *lenp, loff_t *ppos)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds struct parport *port = (struct parport *)table->extra1;
1811da177e4SLinus Torvalds char buffer[20];
1821da177e4SLinus Torvalds int len = 0;
1831da177e4SLinus Torvalds
1841da177e4SLinus Torvalds if (*ppos) {
1851da177e4SLinus Torvalds *lenp = 0;
1861da177e4SLinus Torvalds return 0;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds
1891da177e4SLinus Torvalds if (write) /* permissions prevent this anyway */
1901da177e4SLinus Torvalds return -EACCES;
1911da177e4SLinus Torvalds
192*66029078STakashi Iwai len += scnprintf (buffer, sizeof(buffer), "%d\n", port->dma);
1931da177e4SLinus Torvalds
1941da177e4SLinus Torvalds if (len > *lenp)
1951da177e4SLinus Torvalds len = *lenp;
1961da177e4SLinus Torvalds else
1971da177e4SLinus Torvalds *lenp = len;
1981da177e4SLinus Torvalds
1991da177e4SLinus Torvalds *ppos += len;
20032927393SChristoph Hellwig memcpy(result, buffer, len);
20132927393SChristoph Hellwig return 0;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds
do_hardware_modes(struct ctl_table * table,int write,void * result,size_t * lenp,loff_t * ppos)204be424c63SJoe Perches static int do_hardware_modes(struct ctl_table *table, int write,
20532927393SChristoph Hellwig void *result, size_t *lenp, loff_t *ppos)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds struct parport *port = (struct parport *)table->extra1;
2081da177e4SLinus Torvalds char buffer[40];
2091da177e4SLinus Torvalds int len = 0;
2101da177e4SLinus Torvalds
2111da177e4SLinus Torvalds if (*ppos) {
2121da177e4SLinus Torvalds *lenp = 0;
2131da177e4SLinus Torvalds return 0;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds
2161da177e4SLinus Torvalds if (write) /* permissions prevent this anyway */
2171da177e4SLinus Torvalds return -EACCES;
2181da177e4SLinus Torvalds
2191da177e4SLinus Torvalds {
220a6abfdffSJoe Perches #define printmode(x) \
221a6abfdffSJoe Perches do { \
222a6abfdffSJoe Perches if (port->modes & PARPORT_MODE_##x) \
223*66029078STakashi Iwai len += scnprintf(buffer + len, sizeof(buffer) - len, "%s%s", f++ ? "," : "", #x); \
224a6abfdffSJoe Perches } while (0)
2251da177e4SLinus Torvalds int f = 0;
2261da177e4SLinus Torvalds printmode(PCSPP);
2271da177e4SLinus Torvalds printmode(TRISTATE);
2281da177e4SLinus Torvalds printmode(COMPAT);
2291da177e4SLinus Torvalds printmode(EPP);
2301da177e4SLinus Torvalds printmode(ECP);
2311da177e4SLinus Torvalds printmode(DMA);
2321da177e4SLinus Torvalds #undef printmode
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds buffer[len++] = '\n';
2351da177e4SLinus Torvalds
2361da177e4SLinus Torvalds if (len > *lenp)
2371da177e4SLinus Torvalds len = *lenp;
2381da177e4SLinus Torvalds else
2391da177e4SLinus Torvalds *lenp = len;
2401da177e4SLinus Torvalds
2411da177e4SLinus Torvalds *ppos += len;
24232927393SChristoph Hellwig memcpy(result, buffer, len);
24332927393SChristoph Hellwig return 0;
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds
2461da177e4SLinus Torvalds static const unsigned long parport_min_timeslice_value =
2471da177e4SLinus Torvalds PARPORT_MIN_TIMESLICE_VALUE;
2481da177e4SLinus Torvalds
2491da177e4SLinus Torvalds static const unsigned long parport_max_timeslice_value =
2501da177e4SLinus Torvalds PARPORT_MAX_TIMESLICE_VALUE;
2511da177e4SLinus Torvalds
2521da177e4SLinus Torvalds static const int parport_min_spintime_value =
2531da177e4SLinus Torvalds PARPORT_MIN_SPINTIME_VALUE;
2541da177e4SLinus Torvalds
2551da177e4SLinus Torvalds static const int parport_max_spintime_value =
2561da177e4SLinus Torvalds PARPORT_MAX_SPINTIME_VALUE;
2571da177e4SLinus Torvalds
2581da177e4SLinus Torvalds
2591da177e4SLinus Torvalds struct parport_sysctl_table {
26037e9981eSJoel Granados struct ctl_table_header *port_header;
26137e9981eSJoel Granados struct ctl_table_header *devices_header;
262be424c63SJoe Perches struct ctl_table vars[12];
263be424c63SJoe Perches struct ctl_table device_dir[2];
2641da177e4SLinus Torvalds };
2651da177e4SLinus Torvalds
2661da177e4SLinus Torvalds static const struct parport_sysctl_table parport_sysctl_template = {
26737e9981eSJoel Granados .port_header = NULL,
26837e9981eSJoel Granados .devices_header = NULL,
2691da177e4SLinus Torvalds {
2702564b7bdSEric W. Biederman {
2712564b7bdSEric W. Biederman .procname = "spintime",
2722564b7bdSEric W. Biederman .data = NULL,
2732564b7bdSEric W. Biederman .maxlen = sizeof(int),
2742564b7bdSEric W. Biederman .mode = 0644,
2756d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax,
2762564b7bdSEric W. Biederman .extra1 = (void*) &parport_min_spintime_value,
2772564b7bdSEric W. Biederman .extra2 = (void*) &parport_max_spintime_value
2782564b7bdSEric W. Biederman },
2792564b7bdSEric W. Biederman {
2802564b7bdSEric W. Biederman .procname = "base-addr",
2812564b7bdSEric W. Biederman .data = NULL,
2822564b7bdSEric W. Biederman .maxlen = 0,
2832564b7bdSEric W. Biederman .mode = 0444,
2846d456111SEric W. Biederman .proc_handler = do_hardware_base_addr
2852564b7bdSEric W. Biederman },
2862564b7bdSEric W. Biederman {
2872564b7bdSEric W. Biederman .procname = "irq",
2882564b7bdSEric W. Biederman .data = NULL,
2892564b7bdSEric W. Biederman .maxlen = 0,
2902564b7bdSEric W. Biederman .mode = 0444,
2916d456111SEric W. Biederman .proc_handler = do_hardware_irq
2922564b7bdSEric W. Biederman },
2932564b7bdSEric W. Biederman {
2942564b7bdSEric W. Biederman .procname = "dma",
2952564b7bdSEric W. Biederman .data = NULL,
2962564b7bdSEric W. Biederman .maxlen = 0,
2972564b7bdSEric W. Biederman .mode = 0444,
2986d456111SEric W. Biederman .proc_handler = do_hardware_dma
2992564b7bdSEric W. Biederman },
3002564b7bdSEric W. Biederman {
3012564b7bdSEric W. Biederman .procname = "modes",
3022564b7bdSEric W. Biederman .data = NULL,
3032564b7bdSEric W. Biederman .maxlen = 0,
3042564b7bdSEric W. Biederman .mode = 0444,
3056d456111SEric W. Biederman .proc_handler = do_hardware_modes
3062564b7bdSEric W. Biederman },
3071da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
3082564b7bdSEric W. Biederman {
3092564b7bdSEric W. Biederman .procname = "autoprobe",
3102564b7bdSEric W. Biederman .data = NULL,
3112564b7bdSEric W. Biederman .maxlen = 0,
3122564b7bdSEric W. Biederman .mode = 0444,
3136d456111SEric W. Biederman .proc_handler = do_autoprobe
3141da177e4SLinus Torvalds },
3152564b7bdSEric W. Biederman {
3162564b7bdSEric W. Biederman .procname = "autoprobe0",
3172564b7bdSEric W. Biederman .data = NULL,
3182564b7bdSEric W. Biederman .maxlen = 0,
3192564b7bdSEric W. Biederman .mode = 0444,
3206d456111SEric W. Biederman .proc_handler = do_autoprobe
3212564b7bdSEric W. Biederman },
3222564b7bdSEric W. Biederman {
3232564b7bdSEric W. Biederman .procname = "autoprobe1",
3242564b7bdSEric W. Biederman .data = NULL,
3252564b7bdSEric W. Biederman .maxlen = 0,
3262564b7bdSEric W. Biederman .mode = 0444,
3276d456111SEric W. Biederman .proc_handler = do_autoprobe
3282564b7bdSEric W. Biederman },
3292564b7bdSEric W. Biederman {
3302564b7bdSEric W. Biederman .procname = "autoprobe2",
3312564b7bdSEric W. Biederman .data = NULL,
3322564b7bdSEric W. Biederman .maxlen = 0,
3332564b7bdSEric W. Biederman .mode = 0444,
3346d456111SEric W. Biederman .proc_handler = do_autoprobe
3352564b7bdSEric W. Biederman },
3362564b7bdSEric W. Biederman {
3372564b7bdSEric W. Biederman .procname = "autoprobe3",
3382564b7bdSEric W. Biederman .data = NULL,
3392564b7bdSEric W. Biederman .maxlen = 0,
3402564b7bdSEric W. Biederman .mode = 0444,
3416d456111SEric W. Biederman .proc_handler = do_autoprobe
3422564b7bdSEric W. Biederman },
3432564b7bdSEric W. Biederman #endif /* IEEE 1284 support */
3442564b7bdSEric W. Biederman {}
3452564b7bdSEric W. Biederman },
3462564b7bdSEric W. Biederman {
3472564b7bdSEric W. Biederman {
3482564b7bdSEric W. Biederman .procname = "active",
3492564b7bdSEric W. Biederman .data = NULL,
3502564b7bdSEric W. Biederman .maxlen = 0,
3512564b7bdSEric W. Biederman .mode = 0444,
3526d456111SEric W. Biederman .proc_handler = do_active_device
3532564b7bdSEric W. Biederman },
3542564b7bdSEric W. Biederman {}
3552564b7bdSEric W. Biederman },
3561da177e4SLinus Torvalds };
3571da177e4SLinus Torvalds
3581da177e4SLinus Torvalds struct parport_device_sysctl_table
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds struct ctl_table_header *sysctl_header;
361be424c63SJoe Perches struct ctl_table vars[2];
362be424c63SJoe Perches struct ctl_table device_dir[2];
363be424c63SJoe Perches struct ctl_table devices_root_dir[2];
364be424c63SJoe Perches struct ctl_table port_dir[2];
365be424c63SJoe Perches struct ctl_table parport_dir[2];
366be424c63SJoe Perches struct ctl_table dev_dir[2];
3671da177e4SLinus Torvalds };
3681da177e4SLinus Torvalds
3691da177e4SLinus Torvalds static const struct parport_device_sysctl_table
3701da177e4SLinus Torvalds parport_device_sysctl_template = {
3712564b7bdSEric W. Biederman .sysctl_header = NULL,
3721da177e4SLinus Torvalds {
3732564b7bdSEric W. Biederman {
3742564b7bdSEric W. Biederman .procname = "timeslice",
3752564b7bdSEric W. Biederman .data = NULL,
37660af8803SEric Dumazet .maxlen = sizeof(unsigned long),
3772564b7bdSEric W. Biederman .mode = 0644,
3786d456111SEric W. Biederman .proc_handler = proc_doulongvec_ms_jiffies_minmax,
3792564b7bdSEric W. Biederman .extra1 = (void*) &parport_min_timeslice_value,
3802564b7bdSEric W. Biederman .extra2 = (void*) &parport_max_timeslice_value
3811da177e4SLinus Torvalds },
3824199a64aSJoel Granados {}
3832564b7bdSEric W. Biederman },
3842564b7bdSEric W. Biederman {
3852564b7bdSEric W. Biederman {
3862564b7bdSEric W. Biederman .procname = NULL,
3872564b7bdSEric W. Biederman .data = NULL,
3882564b7bdSEric W. Biederman .maxlen = 0,
3892564b7bdSEric W. Biederman .mode = 0555,
3902564b7bdSEric W. Biederman },
3912564b7bdSEric W. Biederman {}
3922564b7bdSEric W. Biederman }
3931da177e4SLinus Torvalds };
3941da177e4SLinus Torvalds
3951da177e4SLinus Torvalds struct parport_default_sysctl_table
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds struct ctl_table_header *sysctl_header;
398be424c63SJoe Perches struct ctl_table vars[3];
399be424c63SJoe Perches struct ctl_table default_dir[2];
400be424c63SJoe Perches struct ctl_table parport_dir[2];
401be424c63SJoe Perches struct ctl_table dev_dir[2];
4021da177e4SLinus Torvalds };
4031da177e4SLinus Torvalds
4041da177e4SLinus Torvalds static struct parport_default_sysctl_table
4051da177e4SLinus Torvalds parport_default_sysctl_table = {
4062564b7bdSEric W. Biederman .sysctl_header = NULL,
4071da177e4SLinus Torvalds {
4082564b7bdSEric W. Biederman {
4092564b7bdSEric W. Biederman .procname = "timeslice",
4102564b7bdSEric W. Biederman .data = &parport_default_timeslice,
4112564b7bdSEric W. Biederman .maxlen = sizeof(parport_default_timeslice),
4122564b7bdSEric W. Biederman .mode = 0644,
4136d456111SEric W. Biederman .proc_handler = proc_doulongvec_ms_jiffies_minmax,
4142564b7bdSEric W. Biederman .extra1 = (void*) &parport_min_timeslice_value,
4152564b7bdSEric W. Biederman .extra2 = (void*) &parport_max_timeslice_value
4161da177e4SLinus Torvalds },
4172564b7bdSEric W. Biederman {
4182564b7bdSEric W. Biederman .procname = "spintime",
4192564b7bdSEric W. Biederman .data = &parport_default_spintime,
4202564b7bdSEric W. Biederman .maxlen = sizeof(parport_default_spintime),
4212564b7bdSEric W. Biederman .mode = 0644,
4226d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax,
4232564b7bdSEric W. Biederman .extra1 = (void*) &parport_min_spintime_value,
4242564b7bdSEric W. Biederman .extra2 = (void*) &parport_max_spintime_value
4252564b7bdSEric W. Biederman },
4262564b7bdSEric W. Biederman {}
4272564b7bdSEric W. Biederman }
4281da177e4SLinus Torvalds };
4291da177e4SLinus Torvalds
parport_proc_register(struct parport * port)4301da177e4SLinus Torvalds int parport_proc_register(struct parport *port)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds struct parport_sysctl_table *t;
43393810936SJoel Granados char *tmp_dir_path;
43493810936SJoel Granados size_t tmp_path_len, port_name_len;
43593810936SJoel Granados int bytes_written, i, err = 0;
4361da177e4SLinus Torvalds
4372451a848SSilviu-Mihai Popescu t = kmemdup(&parport_sysctl_template, sizeof(*t), GFP_KERNEL);
4381da177e4SLinus Torvalds if (t == NULL)
4391da177e4SLinus Torvalds return -ENOMEM;
4401da177e4SLinus Torvalds
4411da177e4SLinus Torvalds t->device_dir[0].extra1 = port;
4421da177e4SLinus Torvalds
4431da177e4SLinus Torvalds t->vars[0].data = &port->spintime;
44493810936SJoel Granados for (i = 0; i < 5; i++) {
44593810936SJoel Granados t->vars[i].extra1 = port;
44693810936SJoel Granados t->vars[5 + i].extra2 = &port->probe_info[i];
4471da177e4SLinus Torvalds }
44893810936SJoel Granados
44993810936SJoel Granados port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN);
45093810936SJoel Granados /*
45193810936SJoel Granados * Allocate a buffer for two paths: dev/parport/PORT and dev/parport/PORT/devices.
45293810936SJoel Granados * We calculate for the second as that will give us enough for the first.
45393810936SJoel Granados */
45493810936SJoel Granados tmp_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len;
45593810936SJoel Granados tmp_dir_path = kzalloc(tmp_path_len, GFP_KERNEL);
45693810936SJoel Granados if (!tmp_dir_path) {
45793810936SJoel Granados err = -ENOMEM;
45893810936SJoel Granados goto exit_free_t;
45993810936SJoel Granados }
46093810936SJoel Granados
46193810936SJoel Granados bytes_written = snprintf(tmp_dir_path, tmp_path_len,
46293810936SJoel Granados "dev/parport/%s/devices", port->name);
46393810936SJoel Granados if (tmp_path_len <= bytes_written) {
46493810936SJoel Granados err = -ENOENT;
46593810936SJoel Granados goto exit_free_tmp_dir_path;
46693810936SJoel Granados }
46737e9981eSJoel Granados t->devices_header = register_sysctl(tmp_dir_path, t->device_dir);
46837e9981eSJoel Granados if (t->devices_header == NULL) {
46993810936SJoel Granados err = -ENOENT;
47093810936SJoel Granados goto exit_free_tmp_dir_path;
47193810936SJoel Granados }
47293810936SJoel Granados
47393810936SJoel Granados tmp_path_len = PARPORT_BASE_PATH_SIZE + port_name_len;
47493810936SJoel Granados bytes_written = snprintf(tmp_dir_path, tmp_path_len,
47593810936SJoel Granados "dev/parport/%s", port->name);
47693810936SJoel Granados if (tmp_path_len <= bytes_written) {
47793810936SJoel Granados err = -ENOENT;
47893810936SJoel Granados goto unregister_devices_h;
47993810936SJoel Granados }
48093810936SJoel Granados
48137e9981eSJoel Granados t->port_header = register_sysctl(tmp_dir_path, t->vars);
48237e9981eSJoel Granados if (t->port_header == NULL) {
48393810936SJoel Granados err = -ENOENT;
48493810936SJoel Granados goto unregister_devices_h;
48593810936SJoel Granados }
48693810936SJoel Granados
4871da177e4SLinus Torvalds port->sysctl_table = t;
48893810936SJoel Granados
48993810936SJoel Granados kfree(tmp_dir_path);
4901da177e4SLinus Torvalds return 0;
49193810936SJoel Granados
49293810936SJoel Granados unregister_devices_h:
49337e9981eSJoel Granados unregister_sysctl_table(t->devices_header);
49493810936SJoel Granados
49593810936SJoel Granados exit_free_tmp_dir_path:
49693810936SJoel Granados kfree(tmp_dir_path);
49793810936SJoel Granados
49893810936SJoel Granados exit_free_t:
49993810936SJoel Granados kfree(t);
50093810936SJoel Granados return err;
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds
parport_proc_unregister(struct parport * port)5031da177e4SLinus Torvalds int parport_proc_unregister(struct parport *port)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds if (port->sysctl_table) {
5061da177e4SLinus Torvalds struct parport_sysctl_table *t = port->sysctl_table;
5071da177e4SLinus Torvalds port->sysctl_table = NULL;
50837e9981eSJoel Granados unregister_sysctl_table(t->devices_header);
50937e9981eSJoel Granados unregister_sysctl_table(t->port_header);
5101da177e4SLinus Torvalds kfree(t);
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds return 0;
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds
parport_device_proc_register(struct pardevice * device)5151da177e4SLinus Torvalds int parport_device_proc_register(struct pardevice *device)
5161da177e4SLinus Torvalds {
5174199a64aSJoel Granados int bytes_written, err = 0;
5181da177e4SLinus Torvalds struct parport_device_sysctl_table *t;
5191da177e4SLinus Torvalds struct parport * port = device->port;
5204199a64aSJoel Granados size_t port_name_len, device_name_len, tmp_dir_path_len;
5214199a64aSJoel Granados char *tmp_dir_path;
5221da177e4SLinus Torvalds
5232451a848SSilviu-Mihai Popescu t = kmemdup(&parport_device_sysctl_template, sizeof(*t), GFP_KERNEL);
5241da177e4SLinus Torvalds if (t == NULL)
5251da177e4SLinus Torvalds return -ENOMEM;
5261da177e4SLinus Torvalds
5274199a64aSJoel Granados port_name_len = strnlen(port->name, PARPORT_NAME_MAX_LEN);
5284199a64aSJoel Granados device_name_len = strnlen(device->name, PATH_MAX);
5291da177e4SLinus Torvalds
5304199a64aSJoel Granados /* Allocate a buffer for two paths: dev/parport/PORT/devices/DEVICE. */
5314199a64aSJoel Granados tmp_dir_path_len = PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len + device_name_len;
5324199a64aSJoel Granados tmp_dir_path = kzalloc(tmp_dir_path_len, GFP_KERNEL);
5334199a64aSJoel Granados if (!tmp_dir_path) {
5344199a64aSJoel Granados err = -ENOMEM;
5354199a64aSJoel Granados goto exit_free_t;
5364199a64aSJoel Granados }
5374199a64aSJoel Granados
5384199a64aSJoel Granados bytes_written = snprintf(tmp_dir_path, tmp_dir_path_len, "dev/parport/%s/devices/%s",
5394199a64aSJoel Granados port->name, device->name);
5404199a64aSJoel Granados if (tmp_dir_path_len <= bytes_written) {
5414199a64aSJoel Granados err = -ENOENT;
5424199a64aSJoel Granados goto exit_free_path;
5434199a64aSJoel Granados }
5444199a64aSJoel Granados
5451da177e4SLinus Torvalds t->vars[0].data = &device->timeslice;
5461da177e4SLinus Torvalds
5474199a64aSJoel Granados t->sysctl_header = register_sysctl(tmp_dir_path, t->vars);
5481da177e4SLinus Torvalds if (t->sysctl_header == NULL) {
5491da177e4SLinus Torvalds kfree(t);
5501da177e4SLinus Torvalds t = NULL;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds device->sysctl_table = t;
5534199a64aSJoel Granados
5544199a64aSJoel Granados kfree(tmp_dir_path);
5551da177e4SLinus Torvalds return 0;
5564199a64aSJoel Granados
5574199a64aSJoel Granados exit_free_path:
5584199a64aSJoel Granados kfree(tmp_dir_path);
5594199a64aSJoel Granados
5604199a64aSJoel Granados exit_free_t:
5614199a64aSJoel Granados kfree(t);
5624199a64aSJoel Granados
5634199a64aSJoel Granados return err;
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds
parport_device_proc_unregister(struct pardevice * device)5661da177e4SLinus Torvalds int parport_device_proc_unregister(struct pardevice *device)
5671da177e4SLinus Torvalds {
5681da177e4SLinus Torvalds if (device->sysctl_table) {
5691da177e4SLinus Torvalds struct parport_device_sysctl_table *t = device->sysctl_table;
5701da177e4SLinus Torvalds device->sysctl_table = NULL;
5711da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header);
5721da177e4SLinus Torvalds kfree(t);
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds return 0;
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds
parport_default_proc_register(void)5771da177e4SLinus Torvalds static int __init parport_default_proc_register(void)
5781da177e4SLinus Torvalds {
5796fa45a22SSudip Mukherjee int ret;
5806fa45a22SSudip Mukherjee
5811da177e4SLinus Torvalds parport_default_sysctl_table.sysctl_header =
58202ea1348SJoel Granados register_sysctl("dev/parport/default", parport_default_sysctl_table.vars);
5836fa45a22SSudip Mukherjee if (!parport_default_sysctl_table.sysctl_header)
5846fa45a22SSudip Mukherjee return -ENOMEM;
5856fa45a22SSudip Mukherjee ret = parport_bus_init();
5866fa45a22SSudip Mukherjee if (ret) {
5876fa45a22SSudip Mukherjee unregister_sysctl_table(parport_default_sysctl_table.
5886fa45a22SSudip Mukherjee sysctl_header);
5896fa45a22SSudip Mukherjee return ret;
5906fa45a22SSudip Mukherjee }
5911da177e4SLinus Torvalds return 0;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds
parport_default_proc_unregister(void)5941da177e4SLinus Torvalds static void __exit parport_default_proc_unregister(void)
5951da177e4SLinus Torvalds {
5961da177e4SLinus Torvalds if (parport_default_sysctl_table.sysctl_header) {
5971da177e4SLinus Torvalds unregister_sysctl_table(parport_default_sysctl_table.
5981da177e4SLinus Torvalds sysctl_header);
5991da177e4SLinus Torvalds parport_default_sysctl_table.sysctl_header = NULL;
6001da177e4SLinus Torvalds }
6016fa45a22SSudip Mukherjee parport_bus_exit();
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds
6041da177e4SLinus Torvalds #else /* no sysctl or no procfs*/
6051da177e4SLinus Torvalds
parport_proc_register(struct parport * pp)6061da177e4SLinus Torvalds int parport_proc_register(struct parport *pp)
6071da177e4SLinus Torvalds {
6081da177e4SLinus Torvalds return 0;
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds
parport_proc_unregister(struct parport * pp)6111da177e4SLinus Torvalds int parport_proc_unregister(struct parport *pp)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds return 0;
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds
parport_device_proc_register(struct pardevice * device)6161da177e4SLinus Torvalds int parport_device_proc_register(struct pardevice *device)
6171da177e4SLinus Torvalds {
6181da177e4SLinus Torvalds return 0;
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds
parport_device_proc_unregister(struct pardevice * device)6211da177e4SLinus Torvalds int parport_device_proc_unregister(struct pardevice *device)
6221da177e4SLinus Torvalds {
6231da177e4SLinus Torvalds return 0;
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds
parport_default_proc_register(void)6261da177e4SLinus Torvalds static int __init parport_default_proc_register (void)
6271da177e4SLinus Torvalds {
6286fa45a22SSudip Mukherjee return parport_bus_init();
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds
parport_default_proc_unregister(void)6311da177e4SLinus Torvalds static void __exit parport_default_proc_unregister (void)
6321da177e4SLinus Torvalds {
6336fa45a22SSudip Mukherjee parport_bus_exit();
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds #endif
6361da177e4SLinus Torvalds
637bbca503bSSudip Mukherjee subsys_initcall(parport_default_proc_register)
6381da177e4SLinus Torvalds module_exit(parport_default_proc_unregister)
639