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