2 * Copyright (C) 1997-1998 Mark Lord
3 * Copyright (C) 2003 Red Hat <alan@redhat.com>
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;
161 spin_lock_irqsave(&ide_lock, flags);
162 val = ds->get(drive);
163 spin_unlock_irqrestore(&ide_lock, flags);
170 * ide_write_setting - read an IDE setting
171 * @drive: drive to read from
172 * @setting: drive setting
175 * Write a drive setting if it is possible. The caller
176 * must hold the ide_setting_mtx when making this call.
178 * BUGS: the data return and error are the same return value
179 * so an error -EINVAL and true return of the same value cannot
182 * FIXME: This should be changed to enqueue a special request
183 * to the driver to change settings, and then wait on a sema for completion.
184 * The current scheme of polling is kludgy, though safe enough.
187 static int ide_write_setting(ide_drive_t *drive,
188 const struct ide_proc_devset *setting, int val)
190 const struct ide_devset *ds = setting->setting;
192 if (!capable(CAP_SYS_ADMIN))
196 if ((ds->flags & DS_SYNC)
197 && (val < setting->min || val > setting->max))
199 return ide_devset_execute(drive, ds, val);
202 ide_devset_get(xfer_rate, current_speed);
204 static int set_xfer_rate (ide_drive_t *drive, int arg)
209 if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
212 memset(&task, 0, sizeof(task));
213 task.tf.command = ATA_CMD_SET_FEATURES;
214 task.tf.feature = SETFEATURES_XFER;
215 task.tf.nsect = (u8)arg;
216 task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT |
219 err = ide_no_data_taskfile(drive, &task);
222 ide_set_xfer_rate(drive, (u8) arg);
223 ide_driveid_update(drive);
228 ide_devset_rw(current_speed, xfer_rate);
229 ide_devset_rw_field(init_speed, init_speed);
230 ide_devset_rw_flag(nice1, IDE_DFLAG_NICE1);
231 ide_devset_rw_field(number, dn);
233 static const struct ide_proc_devset ide_generic_settings[] = {
234 IDE_PROC_DEVSET(current_speed, 0, 70),
235 IDE_PROC_DEVSET(init_speed, 0, 70),
236 IDE_PROC_DEVSET(io_32bit, 0, 1 + (SUPPORT_VLB_SYNC << 1)),
237 IDE_PROC_DEVSET(keepsettings, 0, 1),
238 IDE_PROC_DEVSET(nice1, 0, 1),
239 IDE_PROC_DEVSET(number, 0, 3),
240 IDE_PROC_DEVSET(pio_mode, 0, 255),
241 IDE_PROC_DEVSET(unmaskirq, 0, 1),
242 IDE_PROC_DEVSET(using_dma, 0, 1),
246 static void proc_ide_settings_warn(void)
253 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
254 "obsolete, and will be removed soon!\n");
258 static int proc_ide_read_settings
259 (char *page, char **start, off_t off, int count, int *eof, void *data)
261 const struct ide_proc_devset *setting, *g, *d;
262 const struct ide_devset *ds;
263 ide_drive_t *drive = (ide_drive_t *) data;
265 int len, rc, mul_factor, div_factor;
267 proc_ide_settings_warn();
269 mutex_lock(&ide_setting_mtx);
270 g = ide_generic_settings;
272 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
273 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
274 while (g->name || (d && d->name)) {
275 /* read settings in the alphabetical order */
276 if (g->name && d && d->name) {
277 if (strcmp(d->name, g->name) < 0)
281 } else if (d && d->name) {
285 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
286 div_factor = setting->divf ? setting->divf(drive) : 1;
287 out += sprintf(out, "%-24s", setting->name);
288 rc = ide_read_setting(drive, setting);
290 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
292 out += sprintf(out, "%-16s", "write-only");
293 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
294 ds = setting->setting;
296 out += sprintf(out, "r");
298 out += sprintf(out, "w");
299 out += sprintf(out, "\n");
302 mutex_unlock(&ide_setting_mtx);
303 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
308 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
309 unsigned long count, void *data)
311 ide_drive_t *drive = (ide_drive_t *) data;
312 char name[MAX_LEN + 1];
313 int for_real = 0, mul_factor, div_factor;
316 const struct ide_proc_devset *setting;
319 if (!capable(CAP_SYS_ADMIN))
322 proc_ide_settings_warn();
324 if (count >= PAGE_SIZE)
327 s = buf = (char *)__get_free_page(GFP_USER);
331 if (copy_from_user(buf, buffer, count)) {
332 free_page((unsigned long)buf);
339 * Skip over leading whitespace
341 while (count && isspace(*s)) {
346 * Do one full pass to verify all parameters,
347 * then do another to actually write the new settings.
356 while (n > 0 && *p != ':') {
364 memcpy(name, q, p - q);
373 val = simple_strtoul(p, &q, 10);
376 if (n > 0 && !isspace(*p))
378 while (n > 0 && isspace(*p)) {
383 mutex_lock(&ide_setting_mtx);
384 /* generic settings first, then driver specific ones */
385 setting = ide_find_setting(ide_generic_settings, name);
388 setting = ide_find_setting(drive->settings, name);
390 mutex_unlock(&ide_setting_mtx);
395 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
396 div_factor = setting->divf ? setting->divf(drive) : 1;
397 ide_write_setting(drive, setting, val * div_factor / mul_factor);
399 mutex_unlock(&ide_setting_mtx);
401 } while (!for_real++);
402 free_page((unsigned long)buf);
405 free_page((unsigned long)buf);
406 printk("proc_ide_write_settings(): parse error\n");
410 int proc_ide_read_capacity
411 (char *page, char **start, off_t off, int count, int *eof, void *data)
413 int len = sprintf(page, "%llu\n", (long long)0x7fffffff);
414 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
417 EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
419 int proc_ide_read_geometry
420 (char *page, char **start, off_t off, int count, int *eof, void *data)
422 ide_drive_t *drive = (ide_drive_t *) data;
426 out += sprintf(out, "physical %d/%d/%d\n",
427 drive->cyl, drive->head, drive->sect);
428 out += sprintf(out, "logical %d/%d/%d\n",
429 drive->bios_cyl, drive->bios_head, drive->bios_sect);
432 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
435 EXPORT_SYMBOL(proc_ide_read_geometry);
437 static int proc_ide_read_dmodel
438 (char *page, char **start, off_t off, int count, int *eof, void *data)
440 ide_drive_t *drive = (ide_drive_t *) data;
441 char *m = (char *)&drive->id[ATA_ID_PROD];
444 len = sprintf(page, "%.40s\n", m[0] ? m : "(none)");
445 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
448 static int proc_ide_read_driver
449 (char *page, char **start, off_t off, int count, int *eof, void *data)
451 ide_drive_t *drive = (ide_drive_t *) data;
452 struct device *dev = &drive->gendev;
453 ide_driver_t *ide_drv;
457 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
458 len = sprintf(page, "%s version %s\n",
459 dev->driver->name, ide_drv->version);
461 len = sprintf(page, "ide-default version 0.9.newide\n");
462 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
465 static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
467 struct device *dev = &drive->gendev;
471 device_release_driver(dev);
472 /* FIXME: device can still be in use by previous driver */
473 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
474 err = device_attach(dev);
476 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
478 drive->driver_req[0] = 0;
479 if (dev->driver == NULL) {
480 err = device_attach(dev);
483 "IDE: %s: device_attach(2) error: %d\n",
486 if (dev->driver && !strcmp(dev->driver->name, driver))
492 static int proc_ide_write_driver
493 (struct file *file, const char __user *buffer, unsigned long count, void *data)
495 ide_drive_t *drive = (ide_drive_t *) data;
498 if (!capable(CAP_SYS_ADMIN))
502 if (copy_from_user(name, buffer, count))
505 if (ide_replace_subdriver(drive, name))
510 static int proc_ide_read_media
511 (char *page, char **start, off_t off, int count, int *eof, void *data)
513 ide_drive_t *drive = (ide_drive_t *) data;
517 switch (drive->media) {
518 case ide_disk: media = "disk\n"; break;
519 case ide_cdrom: media = "cdrom\n"; break;
520 case ide_tape: media = "tape\n"; break;
521 case ide_floppy: media = "floppy\n"; break;
522 case ide_optical: media = "optical\n"; break;
523 default: media = "UNKNOWN\n"; break;
527 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
530 static ide_proc_entry_t generic_drive_entries[] = {
531 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver,
532 proc_ide_write_driver },
533 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
534 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
535 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
536 { "settings", S_IFREG|S_IRUSR|S_IWUSR, proc_ide_read_settings,
537 proc_ide_write_settings },
538 { NULL, 0, NULL, NULL }
541 static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
543 struct proc_dir_entry *ent;
547 while (p->name != NULL) {
548 ent = create_proc_entry(p->name, p->mode, dir);
551 ent->read_proc = p->read_proc;
552 ent->write_proc = p->write_proc;
557 static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
561 while (p->name != NULL) {
562 remove_proc_entry(p->name, dir);
567 void ide_proc_register_driver(ide_drive_t *drive, ide_driver_t *driver)
569 mutex_lock(&ide_setting_mtx);
570 drive->settings = driver->settings;
571 mutex_unlock(&ide_setting_mtx);
573 ide_add_proc_entries(drive->proc, driver->proc, drive);
576 EXPORT_SYMBOL(ide_proc_register_driver);
579 * ide_proc_unregister_driver - remove driver specific data
583 * Clean up the driver specific /proc files and IDE settings
586 * Takes ide_setting_mtx and ide_lock.
587 * Caller must hold none of the locks.
590 void ide_proc_unregister_driver(ide_drive_t *drive, ide_driver_t *driver)
594 ide_remove_proc_entries(drive->proc, driver->proc);
596 mutex_lock(&ide_setting_mtx);
597 spin_lock_irqsave(&ide_lock, flags);
599 * ide_setting_mtx protects the settings list
600 * ide_lock protects the use of settings
602 * so we need to hold both, ide_settings_sem because we want to
603 * modify the settings list, and ide_lock because we cannot take
604 * a setting out that is being used.
606 * OTOH both ide_{read,write}_setting are only ever used under
609 drive->settings = NULL;
610 spin_unlock_irqrestore(&ide_lock, flags);
611 mutex_unlock(&ide_setting_mtx);
613 EXPORT_SYMBOL(ide_proc_unregister_driver);
615 void ide_proc_port_register_devices(ide_hwif_t *hwif)
618 struct proc_dir_entry *ent;
619 struct proc_dir_entry *parent = hwif->proc;
622 for (d = 0; d < MAX_DRIVES; d++) {
623 ide_drive_t *drive = &hwif->drives[d];
625 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0 || drive->proc)
628 drive->proc = proc_mkdir(drive->name, parent);
630 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
631 sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name);
632 ent = proc_symlink(drive->name, proc_ide_root, name);
637 void ide_proc_unregister_device(ide_drive_t *drive)
640 ide_remove_proc_entries(drive->proc, generic_drive_entries);
641 remove_proc_entry(drive->name, proc_ide_root);
642 remove_proc_entry(drive->name, drive->hwif->proc);
647 static ide_proc_entry_t hwif_entries[] = {
648 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
649 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
650 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
651 { NULL, 0, NULL, NULL }
654 void ide_proc_register_port(ide_hwif_t *hwif)
657 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
662 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
666 void ide_proc_unregister_port(ide_hwif_t *hwif)
669 ide_remove_proc_entries(hwif->proc, hwif_entries);
670 remove_proc_entry(hwif->name, proc_ide_root);
675 static int proc_print_driver(struct device_driver *drv, void *data)
677 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
678 struct seq_file *s = data;
680 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
685 static int ide_drivers_show(struct seq_file *s, void *p)
689 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
691 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
696 static int ide_drivers_open(struct inode *inode, struct file *file)
698 return single_open(file, &ide_drivers_show, NULL);
701 static const struct file_operations ide_drivers_operations = {
702 .owner = THIS_MODULE,
703 .open = ide_drivers_open,
706 .release = single_release,
709 void proc_ide_create(void)
711 proc_ide_root = proc_mkdir("ide", NULL);
716 proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations);
719 void proc_ide_destroy(void)
721 remove_proc_entry("drivers", proc_ide_root);
722 remove_proc_entry("ide", NULL);