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