2 * Copyright (C) 1997-1998 Mark Lord
3 * Copyright (C) 2003 Red Hat
5 * Some code was moved here from ide.c, see it for original copyrights.
9 * This is the /proc/ide/ filesystem implementation.
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
17 #include <linux/module.h>
19 #include <asm/uaccess.h>
20 #include <linux/errno.h>
21 #include <linux/proc_fs.h>
22 #include <linux/stat.h>
24 #include <linux/pci.h>
25 #include <linux/ctype.h>
26 #include <linux/ide.h>
27 #include <linux/seq_file.h>
31 static struct proc_dir_entry *proc_ide_root;
33 static int proc_ide_read_imodel
34 (char *page, char **start, off_t off, int count, int *eof, void *data)
36 ide_hwif_t *hwif = (ide_hwif_t *) data;
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;
60 len = sprintf(page, "%s\n", name);
61 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
64 static int proc_ide_read_mate
65 (char *page, char **start, off_t off, int count, int *eof, void *data)
67 ide_hwif_t *hwif = (ide_hwif_t *) data;
70 if (hwif && hwif->mate)
71 len = sprintf(page, "%s\n", hwif->mate->name);
73 len = sprintf(page, "(none)\n");
74 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
77 static int proc_ide_read_channel
78 (char *page, char **start, off_t off, int count, int *eof, void *data)
80 ide_hwif_t *hwif = (ide_hwif_t *) data;
83 page[0] = hwif->channel ? '1' : '0';
86 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
89 static int proc_ide_read_identify
90 (char *page, char **start, off_t off, int count, int *eof, void *data)
92 ide_drive_t *drive = (ide_drive_t *)data;
96 len = sprintf(page, "\n");
99 __le16 *val = (__le16 *)page;
101 err = taskfile_lib_get_identify(drive, page);
103 char *out = (char *)page + SECTOR_SIZE;
107 out += sprintf(out, "%04x%c",
108 le16_to_cpup(val), (++i & 7) ? ' ' : '\n');
110 } while (i < SECTOR_SIZE / 2);
114 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
118 * ide_find_setting - find a specific setting
119 * @st: setting table pointer
120 * @name: setting name
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
128 const struct ide_proc_devset *ide_find_setting(const struct ide_proc_devset *st,
132 if (strcmp(st->name, name) == 0)
136 return st->name ? st : NULL;
140 * ide_read_setting - read an IDE setting
141 * @drive: drive to read from
142 * @setting: drive setting
144 * Read a drive setting and return the value. The caller
145 * must hold the ide_setting_mtx when making this call.
147 * BUGS: the data return and error are the same return value
148 * so an error -EINVAL and true return of the same value cannot
152 static int ide_read_setting(ide_drive_t *drive,
153 const struct ide_proc_devset *setting)
155 const struct ide_devset *ds = setting->setting;
159 val = ds->get(drive);
165 * ide_write_setting - read an IDE setting
166 * @drive: drive to read from
167 * @setting: drive setting
170 * Write a drive setting if it is possible. The caller
171 * must hold the ide_setting_mtx when making this call.
173 * BUGS: the data return and error are the same return value
174 * so an error -EINVAL and true return of the same value cannot
177 * FIXME: This should be changed to enqueue a special request
178 * to the driver to change settings, and then wait on a sema for completion.
179 * The current scheme of polling is kludgy, though safe enough.
182 static int ide_write_setting(ide_drive_t *drive,
183 const struct ide_proc_devset *setting, int val)
185 const struct ide_devset *ds = setting->setting;
187 if (!capable(CAP_SYS_ADMIN))
191 if ((ds->flags & DS_SYNC)
192 && (val < setting->min || val > setting->max))
194 return ide_devset_execute(drive, ds, val);
197 ide_devset_get(xfer_rate, current_speed);
199 static int set_xfer_rate (ide_drive_t *drive, int arg)
204 if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
207 memset(&task, 0, sizeof(task));
208 task.tf.command = ATA_CMD_SET_FEATURES;
209 task.tf.feature = SETFEATURES_XFER;
210 task.tf.nsect = (u8)arg;
211 task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT |
214 err = ide_no_data_taskfile(drive, &task);
217 ide_set_xfer_rate(drive, (u8) arg);
218 ide_driveid_update(drive);
223 ide_devset_rw(current_speed, xfer_rate);
224 ide_devset_rw_field(init_speed, init_speed);
225 ide_devset_rw_flag(nice1, IDE_DFLAG_NICE1);
226 ide_devset_rw_field(number, dn);
228 static const struct ide_proc_devset ide_generic_settings[] = {
229 IDE_PROC_DEVSET(current_speed, 0, 70),
230 IDE_PROC_DEVSET(init_speed, 0, 70),
231 IDE_PROC_DEVSET(io_32bit, 0, 1 + (SUPPORT_VLB_SYNC << 1)),
232 IDE_PROC_DEVSET(keepsettings, 0, 1),
233 IDE_PROC_DEVSET(nice1, 0, 1),
234 IDE_PROC_DEVSET(number, 0, 3),
235 IDE_PROC_DEVSET(pio_mode, 0, 255),
236 IDE_PROC_DEVSET(unmaskirq, 0, 1),
237 IDE_PROC_DEVSET(using_dma, 0, 1),
241 static void proc_ide_settings_warn(void)
248 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
249 "obsolete, and will be removed soon!\n");
253 static int proc_ide_read_settings
254 (char *page, char **start, off_t off, int count, int *eof, void *data)
256 const struct ide_proc_devset *setting, *g, *d;
257 const struct ide_devset *ds;
258 ide_drive_t *drive = (ide_drive_t *) data;
260 int len, rc, mul_factor, div_factor;
262 proc_ide_settings_warn();
264 mutex_lock(&ide_setting_mtx);
265 g = ide_generic_settings;
267 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
268 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
269 while (g->name || (d && d->name)) {
270 /* read settings in the alphabetical order */
271 if (g->name && d && d->name) {
272 if (strcmp(d->name, g->name) < 0)
276 } else if (d && d->name) {
280 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
281 div_factor = setting->divf ? setting->divf(drive) : 1;
282 out += sprintf(out, "%-24s", setting->name);
283 rc = ide_read_setting(drive, setting);
285 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
287 out += sprintf(out, "%-16s", "write-only");
288 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
289 ds = setting->setting;
291 out += sprintf(out, "r");
293 out += sprintf(out, "w");
294 out += sprintf(out, "\n");
297 mutex_unlock(&ide_setting_mtx);
298 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
303 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
304 unsigned long count, void *data)
306 ide_drive_t *drive = (ide_drive_t *) data;
307 char name[MAX_LEN + 1];
308 int for_real = 0, mul_factor, div_factor;
311 const struct ide_proc_devset *setting;
314 if (!capable(CAP_SYS_ADMIN))
317 proc_ide_settings_warn();
319 if (count >= PAGE_SIZE)
322 s = buf = (char *)__get_free_page(GFP_USER);
326 if (copy_from_user(buf, buffer, count)) {
327 free_page((unsigned long)buf);
334 * Skip over leading whitespace
336 while (count && isspace(*s)) {
341 * Do one full pass to verify all parameters,
342 * then do another to actually write the new settings.
351 while (n > 0 && *p != ':') {
359 memcpy(name, q, p - q);
368 val = simple_strtoul(p, &q, 10);
371 if (n > 0 && !isspace(*p))
373 while (n > 0 && isspace(*p)) {
378 mutex_lock(&ide_setting_mtx);
379 /* generic settings first, then driver specific ones */
380 setting = ide_find_setting(ide_generic_settings, name);
383 setting = ide_find_setting(drive->settings, name);
385 mutex_unlock(&ide_setting_mtx);
390 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
391 div_factor = setting->divf ? setting->divf(drive) : 1;
392 ide_write_setting(drive, setting, val * div_factor / mul_factor);
394 mutex_unlock(&ide_setting_mtx);
396 } while (!for_real++);
397 free_page((unsigned long)buf);
400 free_page((unsigned long)buf);
401 printk("proc_ide_write_settings(): parse error\n");
405 int proc_ide_read_capacity
406 (char *page, char **start, off_t off, int count, int *eof, void *data)
408 int len = sprintf(page, "%llu\n", (long long)0x7fffffff);
409 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
412 EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
414 int proc_ide_read_geometry
415 (char *page, char **start, off_t off, int count, int *eof, void *data)
417 ide_drive_t *drive = (ide_drive_t *) data;
421 out += sprintf(out, "physical %d/%d/%d\n",
422 drive->cyl, drive->head, drive->sect);
423 out += sprintf(out, "logical %d/%d/%d\n",
424 drive->bios_cyl, drive->bios_head, drive->bios_sect);
427 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
430 EXPORT_SYMBOL(proc_ide_read_geometry);
432 static int proc_ide_read_dmodel
433 (char *page, char **start, off_t off, int count, int *eof, void *data)
435 ide_drive_t *drive = (ide_drive_t *) data;
436 char *m = (char *)&drive->id[ATA_ID_PROD];
439 len = sprintf(page, "%.40s\n", m[0] ? m : "(none)");
440 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
443 static int proc_ide_read_driver
444 (char *page, char **start, off_t off, int count, int *eof, void *data)
446 ide_drive_t *drive = (ide_drive_t *) data;
447 struct device *dev = &drive->gendev;
448 ide_driver_t *ide_drv;
452 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
453 len = sprintf(page, "%s version %s\n",
454 dev->driver->name, ide_drv->version);
456 len = sprintf(page, "ide-default version 0.9.newide\n");
457 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
460 static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
462 struct device *dev = &drive->gendev;
466 device_release_driver(dev);
467 /* FIXME: device can still be in use by previous driver */
468 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
469 err = device_attach(dev);
471 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
473 drive->driver_req[0] = 0;
474 if (dev->driver == NULL) {
475 err = device_attach(dev);
478 "IDE: %s: device_attach(2) error: %d\n",
481 if (dev->driver && !strcmp(dev->driver->name, driver))
487 static int proc_ide_write_driver
488 (struct file *file, const char __user *buffer, unsigned long count, void *data)
490 ide_drive_t *drive = (ide_drive_t *) data;
493 if (!capable(CAP_SYS_ADMIN))
497 if (copy_from_user(name, buffer, count))
500 if (ide_replace_subdriver(drive, name))
505 static int proc_ide_read_media
506 (char *page, char **start, off_t off, int count, int *eof, void *data)
508 ide_drive_t *drive = (ide_drive_t *) data;
512 switch (drive->media) {
513 case ide_disk: media = "disk\n"; break;
514 case ide_cdrom: media = "cdrom\n"; break;
515 case ide_tape: media = "tape\n"; break;
516 case ide_floppy: media = "floppy\n"; break;
517 case ide_optical: media = "optical\n"; break;
518 default: media = "UNKNOWN\n"; break;
522 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
525 static ide_proc_entry_t generic_drive_entries[] = {
526 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver,
527 proc_ide_write_driver },
528 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
529 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
530 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
531 { "settings", S_IFREG|S_IRUSR|S_IWUSR, proc_ide_read_settings,
532 proc_ide_write_settings },
533 { NULL, 0, NULL, NULL }
536 static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
538 struct proc_dir_entry *ent;
542 while (p->name != NULL) {
543 ent = create_proc_entry(p->name, p->mode, dir);
546 ent->read_proc = p->read_proc;
547 ent->write_proc = p->write_proc;
552 static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
556 while (p->name != NULL) {
557 remove_proc_entry(p->name, dir);
562 void ide_proc_register_driver(ide_drive_t *drive, ide_driver_t *driver)
564 mutex_lock(&ide_setting_mtx);
565 drive->settings = driver->proc_devsets(drive);
566 mutex_unlock(&ide_setting_mtx);
568 ide_add_proc_entries(drive->proc, driver->proc_entries(drive), drive);
571 EXPORT_SYMBOL(ide_proc_register_driver);
574 * ide_proc_unregister_driver - remove driver specific data
578 * Clean up the driver specific /proc files and IDE settings
581 * Takes ide_setting_mtx.
584 void ide_proc_unregister_driver(ide_drive_t *drive, ide_driver_t *driver)
586 ide_remove_proc_entries(drive->proc, driver->proc_entries(drive));
588 mutex_lock(&ide_setting_mtx);
590 * ide_setting_mtx protects both the settings list and the use
591 * of settings (we cannot take a setting out that is being used).
593 drive->settings = NULL;
594 mutex_unlock(&ide_setting_mtx);
596 EXPORT_SYMBOL(ide_proc_unregister_driver);
598 void ide_proc_port_register_devices(ide_hwif_t *hwif)
601 struct proc_dir_entry *ent;
602 struct proc_dir_entry *parent = hwif->proc;
605 for (d = 0; d < MAX_DRIVES; d++) {
606 ide_drive_t *drive = &hwif->drives[d];
608 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0 || drive->proc)
611 drive->proc = proc_mkdir(drive->name, parent);
613 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
614 sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name);
615 ent = proc_symlink(drive->name, proc_ide_root, name);
620 void ide_proc_unregister_device(ide_drive_t *drive)
623 ide_remove_proc_entries(drive->proc, generic_drive_entries);
624 remove_proc_entry(drive->name, proc_ide_root);
625 remove_proc_entry(drive->name, drive->hwif->proc);
630 static ide_proc_entry_t hwif_entries[] = {
631 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
632 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
633 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
634 { NULL, 0, NULL, NULL }
637 void ide_proc_register_port(ide_hwif_t *hwif)
640 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
645 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
649 void ide_proc_unregister_port(ide_hwif_t *hwif)
652 ide_remove_proc_entries(hwif->proc, hwif_entries);
653 remove_proc_entry(hwif->name, proc_ide_root);
658 static int proc_print_driver(struct device_driver *drv, void *data)
660 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
661 struct seq_file *s = data;
663 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
668 static int ide_drivers_show(struct seq_file *s, void *p)
672 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
674 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
679 static int ide_drivers_open(struct inode *inode, struct file *file)
681 return single_open(file, &ide_drivers_show, NULL);
684 static const struct file_operations ide_drivers_operations = {
685 .owner = THIS_MODULE,
686 .open = ide_drivers_open,
689 .release = single_release,
692 void proc_ide_create(void)
694 proc_ide_root = proc_mkdir("ide", NULL);
699 proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations);
702 void proc_ide_destroy(void)
704 remove_proc_entry("drivers", proc_ide_root);
705 remove_proc_entry("ide", NULL);