ide-proc.c revision 7f1ac8c4b9dadc55ec656b368f5f470f2cbe3083
1/* 2 * Copyright (C) 1997-1998 Mark Lord 3 * Copyright (C) 2003 Red Hat 4 * 5 * Some code was moved here from ide.c, see it for original copyrights. 6 */ 7 8/* 9 * This is the /proc/ide/ filesystem implementation. 10 * 11 * Drive/Driver settings can be retrieved by reading the drive's 12 * "settings" files. e.g. "cat /proc/ide0/hda/settings" 13 * To write a new value "val" into a specific setting "name", use: 14 * echo "name:val" >/proc/ide/ide0/hda/settings 15 */ 16 17#include <linux/module.h> 18 19#include <asm/uaccess.h> 20#include <linux/errno.h> 21#include <linux/proc_fs.h> 22#include <linux/stat.h> 23#include <linux/mm.h> 24#include <linux/pci.h> 25#include <linux/ctype.h> 26#include <linux/ide.h> 27#include <linux/seq_file.h> 28 29#include <asm/io.h> 30 31static struct proc_dir_entry *proc_ide_root; 32 33static int proc_ide_read_imodel 34 (char *page, char **start, off_t off, int count, int *eof, void *data) 35{ 36 ide_hwif_t *hwif = (ide_hwif_t *) data; 37 int len; 38 const char *name; 39 40 switch (hwif->chipset) { 41 case ide_generic: name = "generic"; break; 42 case ide_pci: name = "pci"; break; 43 case ide_cmd640: name = "cmd640"; break; 44 case ide_dtc2278: name = "dtc2278"; break; 45 case ide_ali14xx: name = "ali14xx"; break; 46 case ide_qd65xx: name = "qd65xx"; break; 47 case ide_umc8672: name = "umc8672"; break; 48 case ide_ht6560b: name = "ht6560b"; break; 49 case ide_trm290: name = "trm290"; break; 50 case ide_cy82c693: name = "cy82c693"; break; 51 case ide_4drives: name = "4drives"; break; 52 case ide_pmac: name = "mac-io"; break; 53 case ide_au1xxx: name = "au1xxx"; break; 54 case ide_palm3710: name = "palm3710"; break; 55 case ide_acorn: name = "acorn"; break; 56 default: name = "(unknown)"; break; 57 } 58 len = sprintf(page, "%s\n", name); 59 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 60} 61 62static int proc_ide_read_mate 63 (char *page, char **start, off_t off, int count, int *eof, void *data) 64{ 65 ide_hwif_t *hwif = (ide_hwif_t *) data; 66 int len; 67 68 if (hwif && hwif->mate) 69 len = sprintf(page, "%s\n", hwif->mate->name); 70 else 71 len = sprintf(page, "(none)\n"); 72 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 73} 74 75static int proc_ide_read_channel 76 (char *page, char **start, off_t off, int count, int *eof, void *data) 77{ 78 ide_hwif_t *hwif = (ide_hwif_t *) data; 79 int len; 80 81 page[0] = hwif->channel ? '1' : '0'; 82 page[1] = '\n'; 83 len = 2; 84 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 85} 86 87static int proc_ide_read_identify 88 (char *page, char **start, off_t off, int count, int *eof, void *data) 89{ 90 ide_drive_t *drive = (ide_drive_t *)data; 91 int len = 0, i = 0; 92 int err = 0; 93 94 len = sprintf(page, "\n"); 95 96 if (drive) { 97 __le16 *val = (__le16 *)page; 98 99 err = taskfile_lib_get_identify(drive, page); 100 if (!err) { 101 char *out = (char *)page + SECTOR_SIZE; 102 103 page = out; 104 do { 105 out += sprintf(out, "%04x%c", 106 le16_to_cpup(val), (++i & 7) ? ' ' : '\n'); 107 val += 1; 108 } while (i < SECTOR_SIZE / 2); 109 len = out - page; 110 } 111 } 112 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 113} 114 115/** 116 * ide_find_setting - find a specific setting 117 * @st: setting table pointer 118 * @name: setting name 119 * 120 * Scan's the setting table for a matching entry and returns 121 * this or NULL if no entry is found. The caller must hold the 122 * setting semaphore 123 */ 124 125static 126const struct ide_proc_devset *ide_find_setting(const struct ide_proc_devset *st, 127 char *name) 128{ 129 while (st->name) { 130 if (strcmp(st->name, name) == 0) 131 break; 132 st++; 133 } 134 return st->name ? st : NULL; 135} 136 137/** 138 * ide_read_setting - read an IDE setting 139 * @drive: drive to read from 140 * @setting: drive setting 141 * 142 * Read a drive setting and return the value. The caller 143 * must hold the ide_setting_mtx when making this call. 144 * 145 * BUGS: the data return and error are the same return value 146 * so an error -EINVAL and true return of the same value cannot 147 * be told apart 148 */ 149 150static int ide_read_setting(ide_drive_t *drive, 151 const struct ide_proc_devset *setting) 152{ 153 const struct ide_devset *ds = setting->setting; 154 int val = -EINVAL; 155 156 if (ds->get) 157 val = ds->get(drive); 158 159 return val; 160} 161 162/** 163 * ide_write_setting - read an IDE setting 164 * @drive: drive to read from 165 * @setting: drive setting 166 * @val: value 167 * 168 * Write a drive setting if it is possible. The caller 169 * must hold the ide_setting_mtx when making this call. 170 * 171 * BUGS: the data return and error are the same return value 172 * so an error -EINVAL and true return of the same value cannot 173 * be told apart 174 * 175 * FIXME: This should be changed to enqueue a special request 176 * to the driver to change settings, and then wait on a sema for completion. 177 * The current scheme of polling is kludgy, though safe enough. 178 */ 179 180static int ide_write_setting(ide_drive_t *drive, 181 const struct ide_proc_devset *setting, int val) 182{ 183 const struct ide_devset *ds = setting->setting; 184 185 if (!capable(CAP_SYS_ADMIN)) 186 return -EACCES; 187 if (!ds->set) 188 return -EPERM; 189 if ((ds->flags & DS_SYNC) 190 && (val < setting->min || val > setting->max)) 191 return -EINVAL; 192 return ide_devset_execute(drive, ds, val); 193} 194 195ide_devset_get(xfer_rate, current_speed); 196 197static int set_xfer_rate (ide_drive_t *drive, int arg) 198{ 199 ide_task_t task; 200 int err; 201 202 if (arg < XFER_PIO_0 || arg > XFER_UDMA_6) 203 return -EINVAL; 204 205 memset(&task, 0, sizeof(task)); 206 task.tf.command = ATA_CMD_SET_FEATURES; 207 task.tf.feature = SETFEATURES_XFER; 208 task.tf.nsect = (u8)arg; 209 task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT | 210 IDE_TFLAG_IN_NSECT; 211 212 err = ide_no_data_taskfile(drive, &task); 213 214 if (!err) { 215 ide_set_xfer_rate(drive, (u8) arg); 216 ide_driveid_update(drive); 217 } 218 return err; 219} 220 221ide_devset_rw(current_speed, xfer_rate); 222ide_devset_rw_field(init_speed, init_speed); 223ide_devset_rw_flag(nice1, IDE_DFLAG_NICE1); 224ide_devset_rw_field(number, dn); 225 226static const struct ide_proc_devset ide_generic_settings[] = { 227 IDE_PROC_DEVSET(current_speed, 0, 70), 228 IDE_PROC_DEVSET(init_speed, 0, 70), 229 IDE_PROC_DEVSET(io_32bit, 0, 1 + (SUPPORT_VLB_SYNC << 1)), 230 IDE_PROC_DEVSET(keepsettings, 0, 1), 231 IDE_PROC_DEVSET(nice1, 0, 1), 232 IDE_PROC_DEVSET(number, 0, 3), 233 IDE_PROC_DEVSET(pio_mode, 0, 255), 234 IDE_PROC_DEVSET(unmaskirq, 0, 1), 235 IDE_PROC_DEVSET(using_dma, 0, 1), 236 { 0 }, 237}; 238 239static void proc_ide_settings_warn(void) 240{ 241 static int warned; 242 243 if (warned) 244 return; 245 246 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is " 247 "obsolete, and will be removed soon!\n"); 248 warned = 1; 249} 250 251static int proc_ide_read_settings 252 (char *page, char **start, off_t off, int count, int *eof, void *data) 253{ 254 const struct ide_proc_devset *setting, *g, *d; 255 const struct ide_devset *ds; 256 ide_drive_t *drive = (ide_drive_t *) data; 257 char *out = page; 258 int len, rc, mul_factor, div_factor; 259 260 proc_ide_settings_warn(); 261 262 mutex_lock(&ide_setting_mtx); 263 g = ide_generic_settings; 264 d = drive->settings; 265 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n"); 266 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n"); 267 while (g->name || (d && d->name)) { 268 /* read settings in the alphabetical order */ 269 if (g->name && d && d->name) { 270 if (strcmp(d->name, g->name) < 0) 271 setting = d++; 272 else 273 setting = g++; 274 } else if (d && d->name) { 275 setting = d++; 276 } else 277 setting = g++; 278 mul_factor = setting->mulf ? setting->mulf(drive) : 1; 279 div_factor = setting->divf ? setting->divf(drive) : 1; 280 out += sprintf(out, "%-24s", setting->name); 281 rc = ide_read_setting(drive, setting); 282 if (rc >= 0) 283 out += sprintf(out, "%-16d", rc * mul_factor / div_factor); 284 else 285 out += sprintf(out, "%-16s", "write-only"); 286 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor); 287 ds = setting->setting; 288 if (ds->get) 289 out += sprintf(out, "r"); 290 if (ds->set) 291 out += sprintf(out, "w"); 292 out += sprintf(out, "\n"); 293 } 294 len = out - page; 295 mutex_unlock(&ide_setting_mtx); 296 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 297} 298 299#define MAX_LEN 30 300 301static int proc_ide_write_settings(struct file *file, const char __user *buffer, 302 unsigned long count, void *data) 303{ 304 ide_drive_t *drive = (ide_drive_t *) data; 305 char name[MAX_LEN + 1]; 306 int for_real = 0, mul_factor, div_factor; 307 unsigned long n; 308 309 const struct ide_proc_devset *setting; 310 char *buf, *s; 311 312 if (!capable(CAP_SYS_ADMIN)) 313 return -EACCES; 314 315 proc_ide_settings_warn(); 316 317 if (count >= PAGE_SIZE) 318 return -EINVAL; 319 320 s = buf = (char *)__get_free_page(GFP_USER); 321 if (!buf) 322 return -ENOMEM; 323 324 if (copy_from_user(buf, buffer, count)) { 325 free_page((unsigned long)buf); 326 return -EFAULT; 327 } 328 329 buf[count] = '\0'; 330 331 /* 332 * Skip over leading whitespace 333 */ 334 while (count && isspace(*s)) { 335 --count; 336 ++s; 337 } 338 /* 339 * Do one full pass to verify all parameters, 340 * then do another to actually write the new settings. 341 */ 342 do { 343 char *p = s; 344 n = count; 345 while (n > 0) { 346 unsigned val; 347 char *q = p; 348 349 while (n > 0 && *p != ':') { 350 --n; 351 p++; 352 } 353 if (*p != ':') 354 goto parse_error; 355 if (p - q > MAX_LEN) 356 goto parse_error; 357 memcpy(name, q, p - q); 358 name[p - q] = 0; 359 360 if (n > 0) { 361 --n; 362 p++; 363 } else 364 goto parse_error; 365 366 val = simple_strtoul(p, &q, 10); 367 n -= q - p; 368 p = q; 369 if (n > 0 && !isspace(*p)) 370 goto parse_error; 371 while (n > 0 && isspace(*p)) { 372 --n; 373 ++p; 374 } 375 376 mutex_lock(&ide_setting_mtx); 377 /* generic settings first, then driver specific ones */ 378 setting = ide_find_setting(ide_generic_settings, name); 379 if (!setting) { 380 if (drive->settings) 381 setting = ide_find_setting(drive->settings, name); 382 if (!setting) { 383 mutex_unlock(&ide_setting_mtx); 384 goto parse_error; 385 } 386 } 387 if (for_real) { 388 mul_factor = setting->mulf ? setting->mulf(drive) : 1; 389 div_factor = setting->divf ? setting->divf(drive) : 1; 390 ide_write_setting(drive, setting, val * div_factor / mul_factor); 391 } 392 mutex_unlock(&ide_setting_mtx); 393 } 394 } while (!for_real++); 395 free_page((unsigned long)buf); 396 return count; 397parse_error: 398 free_page((unsigned long)buf); 399 printk("proc_ide_write_settings(): parse error\n"); 400 return -EINVAL; 401} 402 403int proc_ide_read_capacity 404 (char *page, char **start, off_t off, int count, int *eof, void *data) 405{ 406 int len = sprintf(page, "%llu\n", (long long)0x7fffffff); 407 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 408} 409 410EXPORT_SYMBOL_GPL(proc_ide_read_capacity); 411 412int proc_ide_read_geometry 413 (char *page, char **start, off_t off, int count, int *eof, void *data) 414{ 415 ide_drive_t *drive = (ide_drive_t *) data; 416 char *out = page; 417 int len; 418 419 out += sprintf(out, "physical %d/%d/%d\n", 420 drive->cyl, drive->head, drive->sect); 421 out += sprintf(out, "logical %d/%d/%d\n", 422 drive->bios_cyl, drive->bios_head, drive->bios_sect); 423 424 len = out - page; 425 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 426} 427 428EXPORT_SYMBOL(proc_ide_read_geometry); 429 430static int proc_ide_read_dmodel 431 (char *page, char **start, off_t off, int count, int *eof, void *data) 432{ 433 ide_drive_t *drive = (ide_drive_t *) data; 434 char *m = (char *)&drive->id[ATA_ID_PROD]; 435 int len; 436 437 len = sprintf(page, "%.40s\n", m[0] ? m : "(none)"); 438 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 439} 440 441static int proc_ide_read_driver 442 (char *page, char **start, off_t off, int count, int *eof, void *data) 443{ 444 ide_drive_t *drive = (ide_drive_t *) data; 445 struct device *dev = &drive->gendev; 446 ide_driver_t *ide_drv; 447 int len; 448 449 if (dev->driver) { 450 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver); 451 len = sprintf(page, "%s version %s\n", 452 dev->driver->name, ide_drv->version); 453 } else 454 len = sprintf(page, "ide-default version 0.9.newide\n"); 455 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 456} 457 458static int ide_replace_subdriver(ide_drive_t *drive, const char *driver) 459{ 460 struct device *dev = &drive->gendev; 461 int ret = 1; 462 int err; 463 464 device_release_driver(dev); 465 /* FIXME: device can still be in use by previous driver */ 466 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req)); 467 err = device_attach(dev); 468 if (err < 0) 469 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n", 470 __func__, err); 471 drive->driver_req[0] = 0; 472 if (dev->driver == NULL) { 473 err = device_attach(dev); 474 if (err < 0) 475 printk(KERN_WARNING 476 "IDE: %s: device_attach(2) error: %d\n", 477 __func__, err); 478 } 479 if (dev->driver && !strcmp(dev->driver->name, driver)) 480 ret = 0; 481 482 return ret; 483} 484 485static int proc_ide_write_driver 486 (struct file *file, const char __user *buffer, unsigned long count, void *data) 487{ 488 ide_drive_t *drive = (ide_drive_t *) data; 489 char name[32]; 490 491 if (!capable(CAP_SYS_ADMIN)) 492 return -EACCES; 493 if (count > 31) 494 count = 31; 495 if (copy_from_user(name, buffer, count)) 496 return -EFAULT; 497 name[count] = '\0'; 498 if (ide_replace_subdriver(drive, name)) 499 return -EINVAL; 500 return count; 501} 502 503static int proc_ide_read_media 504 (char *page, char **start, off_t off, int count, int *eof, void *data) 505{ 506 ide_drive_t *drive = (ide_drive_t *) data; 507 const char *media; 508 int len; 509 510 switch (drive->media) { 511 case ide_disk: media = "disk\n"; break; 512 case ide_cdrom: media = "cdrom\n"; break; 513 case ide_tape: media = "tape\n"; break; 514 case ide_floppy: media = "floppy\n"; break; 515 case ide_optical: media = "optical\n"; break; 516 default: media = "UNKNOWN\n"; break; 517 } 518 strcpy(page, media); 519 len = strlen(media); 520 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 521} 522 523static ide_proc_entry_t generic_drive_entries[] = { 524 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver, 525 proc_ide_write_driver }, 526 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL }, 527 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL }, 528 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL }, 529 { "settings", S_IFREG|S_IRUSR|S_IWUSR, proc_ide_read_settings, 530 proc_ide_write_settings }, 531 { NULL, 0, NULL, NULL } 532}; 533 534static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data) 535{ 536 struct proc_dir_entry *ent; 537 538 if (!dir || !p) 539 return; 540 while (p->name != NULL) { 541 ent = create_proc_entry(p->name, p->mode, dir); 542 if (!ent) return; 543 ent->data = data; 544 ent->read_proc = p->read_proc; 545 ent->write_proc = p->write_proc; 546 p++; 547 } 548} 549 550static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p) 551{ 552 if (!dir || !p) 553 return; 554 while (p->name != NULL) { 555 remove_proc_entry(p->name, dir); 556 p++; 557 } 558} 559 560void ide_proc_register_driver(ide_drive_t *drive, ide_driver_t *driver) 561{ 562 mutex_lock(&ide_setting_mtx); 563 drive->settings = driver->proc_devsets(drive); 564 mutex_unlock(&ide_setting_mtx); 565 566 ide_add_proc_entries(drive->proc, driver->proc_entries(drive), drive); 567} 568 569EXPORT_SYMBOL(ide_proc_register_driver); 570 571/** 572 * ide_proc_unregister_driver - remove driver specific data 573 * @drive: drive 574 * @driver: driver 575 * 576 * Clean up the driver specific /proc files and IDE settings 577 * for a given drive. 578 * 579 * Takes ide_setting_mtx. 580 */ 581 582void ide_proc_unregister_driver(ide_drive_t *drive, ide_driver_t *driver) 583{ 584 ide_remove_proc_entries(drive->proc, driver->proc_entries(drive)); 585 586 mutex_lock(&ide_setting_mtx); 587 /* 588 * ide_setting_mtx protects both the settings list and the use 589 * of settings (we cannot take a setting out that is being used). 590 */ 591 drive->settings = NULL; 592 mutex_unlock(&ide_setting_mtx); 593} 594EXPORT_SYMBOL(ide_proc_unregister_driver); 595 596void ide_proc_port_register_devices(ide_hwif_t *hwif) 597{ 598 int d; 599 struct proc_dir_entry *ent; 600 struct proc_dir_entry *parent = hwif->proc; 601 char name[64]; 602 603 for (d = 0; d < MAX_DRIVES; d++) { 604 ide_drive_t *drive = &hwif->drives[d]; 605 606 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0 || drive->proc) 607 continue; 608 609 drive->proc = proc_mkdir(drive->name, parent); 610 if (drive->proc) 611 ide_add_proc_entries(drive->proc, generic_drive_entries, drive); 612 sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name); 613 ent = proc_symlink(drive->name, proc_ide_root, name); 614 if (!ent) return; 615 } 616} 617 618void ide_proc_unregister_device(ide_drive_t *drive) 619{ 620 if (drive->proc) { 621 ide_remove_proc_entries(drive->proc, generic_drive_entries); 622 remove_proc_entry(drive->name, proc_ide_root); 623 remove_proc_entry(drive->name, drive->hwif->proc); 624 drive->proc = NULL; 625 } 626} 627 628static ide_proc_entry_t hwif_entries[] = { 629 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL }, 630 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL }, 631 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL }, 632 { NULL, 0, NULL, NULL } 633}; 634 635void ide_proc_register_port(ide_hwif_t *hwif) 636{ 637 if (!hwif->proc) { 638 hwif->proc = proc_mkdir(hwif->name, proc_ide_root); 639 640 if (!hwif->proc) 641 return; 642 643 ide_add_proc_entries(hwif->proc, hwif_entries, hwif); 644 } 645} 646 647void ide_proc_unregister_port(ide_hwif_t *hwif) 648{ 649 if (hwif->proc) { 650 ide_remove_proc_entries(hwif->proc, hwif_entries); 651 remove_proc_entry(hwif->name, proc_ide_root); 652 hwif->proc = NULL; 653 } 654} 655 656static int proc_print_driver(struct device_driver *drv, void *data) 657{ 658 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver); 659 struct seq_file *s = data; 660 661 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version); 662 663 return 0; 664} 665 666static int ide_drivers_show(struct seq_file *s, void *p) 667{ 668 int err; 669 670 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver); 671 if (err < 0) 672 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n", 673 __func__, err); 674 return 0; 675} 676 677static int ide_drivers_open(struct inode *inode, struct file *file) 678{ 679 return single_open(file, &ide_drivers_show, NULL); 680} 681 682static const struct file_operations ide_drivers_operations = { 683 .owner = THIS_MODULE, 684 .open = ide_drivers_open, 685 .read = seq_read, 686 .llseek = seq_lseek, 687 .release = single_release, 688}; 689 690void proc_ide_create(void) 691{ 692 proc_ide_root = proc_mkdir("ide", NULL); 693 694 if (!proc_ide_root) 695 return; 696 697 proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations); 698} 699 700void proc_ide_destroy(void) 701{ 702 remove_proc_entry("drivers", proc_ide_root); 703 remove_proc_entry("ide", NULL); 704} 705