Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
| 8 | * |
| 9 | * based on work by Grant Guenther <grant@torque.net> |
| 10 | * and Philip Blundell |
| 11 | * |
| 12 | * Cleaned up include files - Russell King <linux@arm.uk.linux.org> |
| 13 | */ |
| 14 | |
| 15 | #include <linux/string.h> |
| 16 | #include <linux/config.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 | |
| 26 | #include <asm/uaccess.h> |
| 27 | |
| 28 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS) |
| 29 | |
| 30 | #define PARPORT_MIN_TIMESLICE_VALUE 1ul |
| 31 | #define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ) |
| 32 | #define PARPORT_MIN_SPINTIME_VALUE 1 |
| 33 | #define PARPORT_MAX_SPINTIME_VALUE 1000 |
| 34 | |
| 35 | static int do_active_device(ctl_table *table, int write, struct file *filp, |
| 36 | void __user *result, size_t *lenp, loff_t *ppos) |
| 37 | { |
| 38 | struct parport *port = (struct parport *)table->extra1; |
| 39 | char buffer[256]; |
| 40 | struct pardevice *dev; |
| 41 | int len = 0; |
| 42 | |
| 43 | if (write) /* can't happen anyway */ |
| 44 | return -EACCES; |
| 45 | |
| 46 | if (*ppos) { |
| 47 | *lenp = 0; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | for (dev = port->devices; dev ; dev = dev->next) { |
| 52 | if(dev == port->cad) { |
| 53 | len += sprintf(buffer, "%s\n", dev->name); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if(!len) { |
| 58 | len += sprintf(buffer, "%s\n", "none"); |
| 59 | } |
| 60 | |
| 61 | if (len > *lenp) |
| 62 | len = *lenp; |
| 63 | else |
| 64 | *lenp = len; |
| 65 | |
| 66 | *ppos += len; |
| 67 | |
| 68 | return copy_to_user(result, buffer, len) ? -EFAULT : 0; |
| 69 | } |
| 70 | |
| 71 | #ifdef CONFIG_PARPORT_1284 |
| 72 | static int do_autoprobe(ctl_table *table, int write, struct file *filp, |
| 73 | void __user *result, size_t *lenp, loff_t *ppos) |
| 74 | { |
| 75 | struct parport_device_info *info = table->extra2; |
| 76 | const char *str; |
| 77 | char buffer[256]; |
| 78 | int len = 0; |
| 79 | |
| 80 | if (write) /* permissions stop this */ |
| 81 | return -EACCES; |
| 82 | |
| 83 | if (*ppos) { |
| 84 | *lenp = 0; |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | if ((str = info->class_name) != NULL) |
| 89 | len += sprintf (buffer + len, "CLASS:%s;\n", str); |
| 90 | |
| 91 | if ((str = info->model) != NULL) |
| 92 | len += sprintf (buffer + len, "MODEL:%s;\n", str); |
| 93 | |
| 94 | if ((str = info->mfr) != NULL) |
| 95 | len += sprintf (buffer + len, "MANUFACTURER:%s;\n", str); |
| 96 | |
| 97 | if ((str = info->description) != NULL) |
| 98 | len += sprintf (buffer + len, "DESCRIPTION:%s;\n", str); |
| 99 | |
| 100 | if ((str = info->cmdset) != NULL) |
| 101 | len += sprintf (buffer + len, "COMMAND SET:%s;\n", str); |
| 102 | |
| 103 | if (len > *lenp) |
| 104 | len = *lenp; |
| 105 | else |
| 106 | *lenp = len; |
| 107 | |
| 108 | *ppos += len; |
| 109 | |
| 110 | return copy_to_user (result, buffer, len) ? -EFAULT : 0; |
| 111 | } |
| 112 | #endif /* IEEE1284.3 support. */ |
| 113 | |
| 114 | static int do_hardware_base_addr (ctl_table *table, int write, |
| 115 | struct file *filp, void __user *result, |
| 116 | size_t *lenp, loff_t *ppos) |
| 117 | { |
| 118 | struct parport *port = (struct parport *)table->extra1; |
| 119 | char buffer[20]; |
| 120 | int len = 0; |
| 121 | |
| 122 | if (*ppos) { |
| 123 | *lenp = 0; |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | if (write) /* permissions prevent this anyway */ |
| 128 | return -EACCES; |
| 129 | |
| 130 | len += sprintf (buffer, "%lu\t%lu\n", port->base, port->base_hi); |
| 131 | |
| 132 | if (len > *lenp) |
| 133 | len = *lenp; |
| 134 | else |
| 135 | *lenp = len; |
| 136 | |
| 137 | *ppos += len; |
| 138 | |
| 139 | return copy_to_user(result, buffer, len) ? -EFAULT : 0; |
| 140 | } |
| 141 | |
| 142 | static int do_hardware_irq (ctl_table *table, int write, |
| 143 | struct file *filp, void __user *result, |
| 144 | size_t *lenp, loff_t *ppos) |
| 145 | { |
| 146 | struct parport *port = (struct parport *)table->extra1; |
| 147 | char buffer[20]; |
| 148 | int len = 0; |
| 149 | |
| 150 | if (*ppos) { |
| 151 | *lenp = 0; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | if (write) /* permissions prevent this anyway */ |
| 156 | return -EACCES; |
| 157 | |
| 158 | len += sprintf (buffer, "%d\n", port->irq); |
| 159 | |
| 160 | if (len > *lenp) |
| 161 | len = *lenp; |
| 162 | else |
| 163 | *lenp = len; |
| 164 | |
| 165 | *ppos += len; |
| 166 | |
| 167 | return copy_to_user(result, buffer, len) ? -EFAULT : 0; |
| 168 | } |
| 169 | |
| 170 | static int do_hardware_dma (ctl_table *table, int write, |
| 171 | struct file *filp, void __user *result, |
| 172 | size_t *lenp, loff_t *ppos) |
| 173 | { |
| 174 | struct parport *port = (struct parport *)table->extra1; |
| 175 | char buffer[20]; |
| 176 | int len = 0; |
| 177 | |
| 178 | if (*ppos) { |
| 179 | *lenp = 0; |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | if (write) /* permissions prevent this anyway */ |
| 184 | return -EACCES; |
| 185 | |
| 186 | len += sprintf (buffer, "%d\n", port->dma); |
| 187 | |
| 188 | if (len > *lenp) |
| 189 | len = *lenp; |
| 190 | else |
| 191 | *lenp = len; |
| 192 | |
| 193 | *ppos += len; |
| 194 | |
| 195 | return copy_to_user(result, buffer, len) ? -EFAULT : 0; |
| 196 | } |
| 197 | |
| 198 | static int do_hardware_modes (ctl_table *table, int write, |
| 199 | struct file *filp, void __user *result, |
| 200 | size_t *lenp, loff_t *ppos) |
| 201 | { |
| 202 | struct parport *port = (struct parport *)table->extra1; |
| 203 | char buffer[40]; |
| 204 | int len = 0; |
| 205 | |
| 206 | if (*ppos) { |
| 207 | *lenp = 0; |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | if (write) /* permissions prevent this anyway */ |
| 212 | return -EACCES; |
| 213 | |
| 214 | { |
| 215 | #define printmode(x) {if(port->modes&PARPORT_MODE_##x){len+=sprintf(buffer+len,"%s%s",f?",":"",#x);f++;}} |
| 216 | int f = 0; |
| 217 | printmode(PCSPP); |
| 218 | printmode(TRISTATE); |
| 219 | printmode(COMPAT); |
| 220 | printmode(EPP); |
| 221 | printmode(ECP); |
| 222 | printmode(DMA); |
| 223 | #undef printmode |
| 224 | } |
| 225 | buffer[len++] = '\n'; |
| 226 | |
| 227 | if (len > *lenp) |
| 228 | len = *lenp; |
| 229 | else |
| 230 | *lenp = len; |
| 231 | |
| 232 | *ppos += len; |
| 233 | |
| 234 | return copy_to_user(result, buffer, len) ? -EFAULT : 0; |
| 235 | } |
| 236 | |
| 237 | #define PARPORT_PORT_DIR(child) { 0, NULL, NULL, 0, 0555, child } |
| 238 | #define PARPORT_PARPORT_DIR(child) { DEV_PARPORT, "parport", \ |
| 239 | NULL, 0, 0555, child } |
| 240 | #define PARPORT_DEV_DIR(child) { CTL_DEV, "dev", NULL, 0, 0555, child } |
| 241 | #define PARPORT_DEVICES_ROOT_DIR { DEV_PARPORT_DEVICES, "devices", \ |
| 242 | NULL, 0, 0555, NULL } |
| 243 | |
| 244 | static const unsigned long parport_min_timeslice_value = |
| 245 | PARPORT_MIN_TIMESLICE_VALUE; |
| 246 | |
| 247 | static const unsigned long parport_max_timeslice_value = |
| 248 | PARPORT_MAX_TIMESLICE_VALUE; |
| 249 | |
| 250 | static const int parport_min_spintime_value = |
| 251 | PARPORT_MIN_SPINTIME_VALUE; |
| 252 | |
| 253 | static const int parport_max_spintime_value = |
| 254 | PARPORT_MAX_SPINTIME_VALUE; |
| 255 | |
| 256 | |
| 257 | struct parport_sysctl_table { |
| 258 | struct ctl_table_header *sysctl_header; |
| 259 | ctl_table vars[12]; |
| 260 | ctl_table device_dir[2]; |
| 261 | ctl_table port_dir[2]; |
| 262 | ctl_table parport_dir[2]; |
| 263 | ctl_table dev_dir[2]; |
| 264 | }; |
| 265 | |
| 266 | static const struct parport_sysctl_table parport_sysctl_template = { |
| 267 | NULL, |
| 268 | { |
| 269 | { DEV_PARPORT_SPINTIME, "spintime", |
| 270 | NULL, sizeof(int), 0644, NULL, |
| 271 | &proc_dointvec_minmax, NULL, NULL, |
| 272 | (void*) &parport_min_spintime_value, |
| 273 | (void*) &parport_max_spintime_value }, |
| 274 | { DEV_PARPORT_BASE_ADDR, "base-addr", |
| 275 | NULL, 0, 0444, NULL, |
| 276 | &do_hardware_base_addr }, |
| 277 | { DEV_PARPORT_IRQ, "irq", |
| 278 | NULL, 0, 0444, NULL, |
| 279 | &do_hardware_irq }, |
| 280 | { DEV_PARPORT_DMA, "dma", |
| 281 | NULL, 0, 0444, NULL, |
| 282 | &do_hardware_dma }, |
| 283 | { DEV_PARPORT_MODES, "modes", |
| 284 | NULL, 0, 0444, NULL, |
| 285 | &do_hardware_modes }, |
| 286 | PARPORT_DEVICES_ROOT_DIR, |
| 287 | #ifdef CONFIG_PARPORT_1284 |
| 288 | { DEV_PARPORT_AUTOPROBE, "autoprobe", |
| 289 | NULL, 0, 0444, NULL, |
| 290 | &do_autoprobe }, |
| 291 | { DEV_PARPORT_AUTOPROBE + 1, "autoprobe0", |
| 292 | NULL, 0, 0444, NULL, |
| 293 | &do_autoprobe }, |
| 294 | { DEV_PARPORT_AUTOPROBE + 2, "autoprobe1", |
| 295 | NULL, 0, 0444, NULL, |
| 296 | &do_autoprobe }, |
| 297 | { DEV_PARPORT_AUTOPROBE + 3, "autoprobe2", |
| 298 | NULL, 0, 0444, NULL, |
| 299 | &do_autoprobe }, |
| 300 | { DEV_PARPORT_AUTOPROBE + 4, "autoprobe3", |
| 301 | NULL, 0, 0444, NULL, |
| 302 | &do_autoprobe }, |
| 303 | #endif /* IEEE 1284 support */ |
| 304 | {0} |
| 305 | }, |
| 306 | { {DEV_PARPORT_DEVICES_ACTIVE, "active", NULL, 0, 0444, NULL, |
| 307 | &do_active_device }, {0}}, |
| 308 | { PARPORT_PORT_DIR(NULL), {0}}, |
| 309 | { PARPORT_PARPORT_DIR(NULL), {0}}, |
| 310 | { PARPORT_DEV_DIR(NULL), {0}} |
| 311 | }; |
| 312 | |
| 313 | struct parport_device_sysctl_table |
| 314 | { |
| 315 | struct ctl_table_header *sysctl_header; |
| 316 | ctl_table vars[2]; |
| 317 | ctl_table device_dir[2]; |
| 318 | ctl_table devices_root_dir[2]; |
| 319 | ctl_table port_dir[2]; |
| 320 | ctl_table parport_dir[2]; |
| 321 | ctl_table dev_dir[2]; |
| 322 | }; |
| 323 | |
| 324 | static const struct parport_device_sysctl_table |
| 325 | parport_device_sysctl_template = { |
| 326 | NULL, |
| 327 | { |
| 328 | { DEV_PARPORT_DEVICE_TIMESLICE, "timeslice", |
| 329 | NULL, sizeof(int), 0644, NULL, |
| 330 | &proc_doulongvec_ms_jiffies_minmax, NULL, NULL, |
| 331 | (void*) &parport_min_timeslice_value, |
| 332 | (void*) &parport_max_timeslice_value }, |
| 333 | }, |
| 334 | { {0, NULL, NULL, 0, 0555, NULL}, {0}}, |
| 335 | { PARPORT_DEVICES_ROOT_DIR, {0}}, |
| 336 | { PARPORT_PORT_DIR(NULL), {0}}, |
| 337 | { PARPORT_PARPORT_DIR(NULL), {0}}, |
| 338 | { PARPORT_DEV_DIR(NULL), {0}} |
| 339 | }; |
| 340 | |
| 341 | struct parport_default_sysctl_table |
| 342 | { |
| 343 | struct ctl_table_header *sysctl_header; |
| 344 | ctl_table vars[3]; |
| 345 | ctl_table default_dir[2]; |
| 346 | ctl_table parport_dir[2]; |
| 347 | ctl_table dev_dir[2]; |
| 348 | }; |
| 349 | |
| 350 | extern unsigned long parport_default_timeslice; |
| 351 | extern int parport_default_spintime; |
| 352 | |
| 353 | static struct parport_default_sysctl_table |
| 354 | parport_default_sysctl_table = { |
| 355 | NULL, |
| 356 | { |
| 357 | { DEV_PARPORT_DEFAULT_TIMESLICE, "timeslice", |
| 358 | &parport_default_timeslice, |
| 359 | sizeof(parport_default_timeslice), 0644, NULL, |
| 360 | &proc_doulongvec_ms_jiffies_minmax, NULL, NULL, |
| 361 | (void*) &parport_min_timeslice_value, |
| 362 | (void*) &parport_max_timeslice_value }, |
| 363 | { DEV_PARPORT_DEFAULT_SPINTIME, "spintime", |
| 364 | &parport_default_spintime, |
| 365 | sizeof(parport_default_spintime), 0644, NULL, |
| 366 | &proc_dointvec_minmax, NULL, NULL, |
| 367 | (void*) &parport_min_spintime_value, |
| 368 | (void*) &parport_max_spintime_value }, |
| 369 | {0} |
| 370 | }, |
| 371 | { { DEV_PARPORT_DEFAULT, "default", NULL, 0, 0555, |
| 372 | parport_default_sysctl_table.vars },{0}}, |
| 373 | { |
| 374 | PARPORT_PARPORT_DIR(parport_default_sysctl_table.default_dir), |
| 375 | {0}}, |
| 376 | { PARPORT_DEV_DIR(parport_default_sysctl_table.parport_dir), {0}} |
| 377 | }; |
| 378 | |
| 379 | |
| 380 | int parport_proc_register(struct parport *port) |
| 381 | { |
| 382 | struct parport_sysctl_table *t; |
| 383 | int i; |
| 384 | |
| 385 | t = kmalloc(sizeof(*t), GFP_KERNEL); |
| 386 | if (t == NULL) |
| 387 | return -ENOMEM; |
| 388 | memcpy(t, &parport_sysctl_template, sizeof(*t)); |
| 389 | |
| 390 | t->device_dir[0].extra1 = port; |
| 391 | |
| 392 | for (i = 0; i < 8; i++) |
| 393 | t->vars[i].extra1 = port; |
| 394 | |
| 395 | t->vars[0].data = &port->spintime; |
| 396 | t->vars[5].child = t->device_dir; |
| 397 | |
| 398 | for (i = 0; i < 5; i++) |
| 399 | t->vars[6 + i].extra2 = &port->probe_info[i]; |
| 400 | |
| 401 | t->port_dir[0].procname = port->name; |
| 402 | t->port_dir[0].ctl_name = port->number + 1; /* nb 0 isn't legal here */ |
| 403 | |
| 404 | t->port_dir[0].child = t->vars; |
| 405 | t->parport_dir[0].child = t->port_dir; |
| 406 | t->dev_dir[0].child = t->parport_dir; |
| 407 | |
| 408 | t->sysctl_header = register_sysctl_table(t->dev_dir, 0); |
| 409 | if (t->sysctl_header == NULL) { |
| 410 | kfree(t); |
| 411 | t = NULL; |
| 412 | } |
| 413 | port->sysctl_table = t; |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | int parport_proc_unregister(struct parport *port) |
| 418 | { |
| 419 | if (port->sysctl_table) { |
| 420 | struct parport_sysctl_table *t = port->sysctl_table; |
| 421 | port->sysctl_table = NULL; |
| 422 | unregister_sysctl_table(t->sysctl_header); |
| 423 | kfree(t); |
| 424 | } |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | int parport_device_proc_register(struct pardevice *device) |
| 429 | { |
| 430 | struct parport_device_sysctl_table *t; |
| 431 | struct parport * port = device->port; |
| 432 | |
| 433 | t = kmalloc(sizeof(*t), GFP_KERNEL); |
| 434 | if (t == NULL) |
| 435 | return -ENOMEM; |
| 436 | memcpy(t, &parport_device_sysctl_template, sizeof(*t)); |
| 437 | |
| 438 | t->dev_dir[0].child = t->parport_dir; |
| 439 | t->parport_dir[0].child = t->port_dir; |
| 440 | t->port_dir[0].procname = port->name; |
| 441 | t->port_dir[0].ctl_name = port->number + 1; /* nb 0 isn't legal here */ |
| 442 | t->port_dir[0].child = t->devices_root_dir; |
| 443 | t->devices_root_dir[0].child = t->device_dir; |
| 444 | |
| 445 | #ifdef CONFIG_PARPORT_1284 |
| 446 | |
| 447 | t->device_dir[0].ctl_name = |
| 448 | parport_device_num(port->number, port->muxport, |
| 449 | device->daisy) |
| 450 | + 1; /* nb 0 isn't legal here */ |
| 451 | |
| 452 | #else /* No IEEE 1284 support */ |
| 453 | |
| 454 | /* parport_device_num isn't available. */ |
| 455 | t->device_dir[0].ctl_name = 1; |
| 456 | |
| 457 | #endif /* IEEE 1284 support or not */ |
| 458 | |
| 459 | t->device_dir[0].procname = device->name; |
| 460 | t->device_dir[0].extra1 = device; |
| 461 | t->device_dir[0].child = t->vars; |
| 462 | t->vars[0].data = &device->timeslice; |
| 463 | |
| 464 | t->sysctl_header = register_sysctl_table(t->dev_dir, 0); |
| 465 | if (t->sysctl_header == NULL) { |
| 466 | kfree(t); |
| 467 | t = NULL; |
| 468 | } |
| 469 | device->sysctl_table = t; |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | int parport_device_proc_unregister(struct pardevice *device) |
| 474 | { |
| 475 | if (device->sysctl_table) { |
| 476 | struct parport_device_sysctl_table *t = device->sysctl_table; |
| 477 | device->sysctl_table = NULL; |
| 478 | unregister_sysctl_table(t->sysctl_header); |
| 479 | kfree(t); |
| 480 | } |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static int __init parport_default_proc_register(void) |
| 485 | { |
| 486 | parport_default_sysctl_table.sysctl_header = |
| 487 | register_sysctl_table(parport_default_sysctl_table.dev_dir, 0); |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static void __exit parport_default_proc_unregister(void) |
| 492 | { |
| 493 | if (parport_default_sysctl_table.sysctl_header) { |
| 494 | unregister_sysctl_table(parport_default_sysctl_table. |
| 495 | sysctl_header); |
| 496 | parport_default_sysctl_table.sysctl_header = NULL; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | #else /* no sysctl or no procfs*/ |
| 501 | |
| 502 | int parport_proc_register(struct parport *pp) |
| 503 | { |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | int parport_proc_unregister(struct parport *pp) |
| 508 | { |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | int parport_device_proc_register(struct pardevice *device) |
| 513 | { |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | int parport_device_proc_unregister(struct pardevice *device) |
| 518 | { |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static int __init parport_default_proc_register (void) |
| 523 | { |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static void __exit parport_default_proc_unregister (void) |
| 528 | { |
| 529 | } |
| 530 | #endif |
| 531 | |
| 532 | module_init(parport_default_proc_register) |
| 533 | module_exit(parport_default_proc_unregister) |