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_WORDS * 4);
106 out += sprintf(out, "%04x%c",
107 le16_to_cpup(val), (++i & 7) ? ' ' : '\n');
109 } while (i < (SECTOR_WORDS * 2));
113 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
117 * ide_find_setting - find a specific setting
118 * @st: setting table pointer
119 * @name: setting name
121 * Scan's the setting table for a matching entry and returns
122 * this or NULL if no entry is found. The caller must hold the
126 static const struct ide_devset *ide_find_setting(const struct ide_devset **st,
130 if (strcmp((*st)->name, name) == 0)
138 * ide_read_setting - read an IDE setting
139 * @drive: drive to read from
140 * @setting: drive setting
142 * Read a drive setting and return the value. The caller
143 * must hold the ide_setting_mtx when making this call.
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
150 static int ide_read_setting(ide_drive_t *drive,
151 const struct ide_devset *setting)
155 if ((setting->flags & S_READ)) {
158 spin_lock_irqsave(&ide_lock, flags);
159 val = setting->get(drive);
160 spin_unlock_irqrestore(&ide_lock, flags);
167 * ide_write_setting - read an IDE setting
168 * @drive: drive to read from
169 * @setting: drive setting
172 * Write a drive setting if it is possible. The caller
173 * must hold the ide_setting_mtx when making this call.
175 * BUGS: the data return and error are the same return value
176 * so an error -EINVAL and true return of the same value cannot
179 * FIXME: This should be changed to enqueue a special request
180 * to the driver to change settings, and then wait on a sema for completion.
181 * The current scheme of polling is kludgy, though safe enough.
184 static int ide_write_setting(ide_drive_t *drive,
185 const struct ide_devset *setting, int val)
187 if (!capable(CAP_SYS_ADMIN))
189 if (setting->set && (setting->flags & S_NOLOCK))
190 return setting->set(drive, val);
191 if (!(setting->flags & S_WRITE))
193 if (val < setting->min || val > setting->max)
195 if (ide_spin_wait_hwgroup(drive))
197 setting->set(drive, val);
198 spin_unlock_irq(&ide_lock);
202 static 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_nolock(current_speed, 0, 70, xfer_rate);
229 ide_devset_rw_nolock(io_32bit, 0, 1 + (SUPPORT_VLB_SYNC << 1), io_32bit);
230 ide_devset_rw_nolock(keepsettings, 0, 1, ksettings);
231 ide_devset_rw_nolock(unmaskirq, 0, 1, unmaskirq);
232 ide_devset_rw_nolock(using_dma, 0, 1, using_dma);
234 ide_devset_w_nolock(pio_mode, 0, 255, pio_mode);
236 ide_devset_rw(init_speed, 0, 70, init_speed);
237 ide_devset_rw(nice1, 0, 1, nice1);
238 ide_devset_rw(number, 0, 3, dn);
240 static const struct ide_devset *ide_generic_settings[] = {
241 &ide_devset_current_speed,
242 &ide_devset_init_speed,
243 &ide_devset_io_32bit,
244 &ide_devset_keepsettings,
247 &ide_devset_pio_mode,
248 &ide_devset_unmaskirq,
249 &ide_devset_using_dma,
253 static void proc_ide_settings_warn(void)
260 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
261 "obsolete, and will be removed soon!\n");
265 static int proc_ide_read_settings
266 (char *page, char **start, off_t off, int count, int *eof, void *data)
268 const struct ide_devset *setting, **g, **d;
269 ide_drive_t *drive = (ide_drive_t *) data;
271 int len, rc, mul_factor, div_factor;
273 proc_ide_settings_warn();
275 mutex_lock(&ide_setting_mtx);
276 g = ide_generic_settings;
278 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
279 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
280 while (*g || (d && *d)) {
281 /* read settings in the alphabetical order */
283 if (strcmp((*d)->name, (*g)->name) < 0)
287 } else if (d && *d) {
291 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
292 div_factor = setting->divf ? setting->divf(drive) : 1;
293 out += sprintf(out, "%-24s", setting->name);
294 rc = ide_read_setting(drive, setting);
296 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
298 out += sprintf(out, "%-16s", "write-only");
299 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
300 if (setting->flags & S_READ)
301 out += sprintf(out, "r");
302 if (setting->flags & S_WRITE)
303 out += sprintf(out, "w");
304 out += sprintf(out, "\n");
307 mutex_unlock(&ide_setting_mtx);
308 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
313 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
314 unsigned long count, void *data)
316 ide_drive_t *drive = (ide_drive_t *) data;
317 char name[MAX_LEN + 1];
318 int for_real = 0, mul_factor, div_factor;
321 const struct ide_devset *setting;
324 if (!capable(CAP_SYS_ADMIN))
327 proc_ide_settings_warn();
329 if (count >= PAGE_SIZE)
332 s = buf = (char *)__get_free_page(GFP_USER);
336 if (copy_from_user(buf, buffer, count)) {
337 free_page((unsigned long)buf);
344 * Skip over leading whitespace
346 while (count && isspace(*s)) {
351 * Do one full pass to verify all parameters,
352 * then do another to actually write the new settings.
361 while (n > 0 && *p != ':') {
369 memcpy(name, q, p - q);
378 val = simple_strtoul(p, &q, 10);
381 if (n > 0 && !isspace(*p))
383 while (n > 0 && isspace(*p)) {
388 mutex_lock(&ide_setting_mtx);
389 /* generic settings first, then driver specific ones */
390 setting = ide_find_setting(ide_generic_settings, name);
393 setting = ide_find_setting(drive->settings, name);
395 mutex_unlock(&ide_setting_mtx);
400 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
401 div_factor = setting->divf ? setting->divf(drive) : 1;
402 ide_write_setting(drive, setting, val * div_factor / mul_factor);
404 mutex_unlock(&ide_setting_mtx);
406 } while (!for_real++);
407 free_page((unsigned long)buf);
410 free_page((unsigned long)buf);
411 printk("proc_ide_write_settings(): parse error\n");
415 int proc_ide_read_capacity
416 (char *page, char **start, off_t off, int count, int *eof, void *data)
418 int len = sprintf(page, "%llu\n", (long long)0x7fffffff);
419 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
422 EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
424 int proc_ide_read_geometry
425 (char *page, char **start, off_t off, int count, int *eof, void *data)
427 ide_drive_t *drive = (ide_drive_t *) data;
431 out += sprintf(out, "physical %d/%d/%d\n",
432 drive->cyl, drive->head, drive->sect);
433 out += sprintf(out, "logical %d/%d/%d\n",
434 drive->bios_cyl, drive->bios_head, drive->bios_sect);
437 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
440 EXPORT_SYMBOL(proc_ide_read_geometry);
442 static int proc_ide_read_dmodel
443 (char *page, char **start, off_t off, int count, int *eof, void *data)
445 ide_drive_t *drive = (ide_drive_t *) data;
446 char *m = (char *)&drive->id[ATA_ID_PROD];
449 len = sprintf(page, "%.40s\n", m[0] ? m : "(none)");
450 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
453 static int proc_ide_read_driver
454 (char *page, char **start, off_t off, int count, int *eof, void *data)
456 ide_drive_t *drive = (ide_drive_t *) data;
457 struct device *dev = &drive->gendev;
458 ide_driver_t *ide_drv;
462 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
463 len = sprintf(page, "%s version %s\n",
464 dev->driver->name, ide_drv->version);
466 len = sprintf(page, "ide-default version 0.9.newide\n");
467 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
470 static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
472 struct device *dev = &drive->gendev;
476 device_release_driver(dev);
477 /* FIXME: device can still be in use by previous driver */
478 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
479 err = device_attach(dev);
481 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
483 drive->driver_req[0] = 0;
484 if (dev->driver == NULL) {
485 err = device_attach(dev);
488 "IDE: %s: device_attach(2) error: %d\n",
491 if (dev->driver && !strcmp(dev->driver->name, driver))
497 static int proc_ide_write_driver
498 (struct file *file, const char __user *buffer, unsigned long count, void *data)
500 ide_drive_t *drive = (ide_drive_t *) data;
503 if (!capable(CAP_SYS_ADMIN))
507 if (copy_from_user(name, buffer, count))
510 if (ide_replace_subdriver(drive, name))
515 static int proc_ide_read_media
516 (char *page, char **start, off_t off, int count, int *eof, void *data)
518 ide_drive_t *drive = (ide_drive_t *) data;
522 switch (drive->media) {
523 case ide_disk: media = "disk\n"; break;
524 case ide_cdrom: media = "cdrom\n"; break;
525 case ide_tape: media = "tape\n"; break;
526 case ide_floppy: media = "floppy\n"; break;
527 case ide_optical: media = "optical\n"; break;
528 default: media = "UNKNOWN\n"; break;
532 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
535 static ide_proc_entry_t generic_drive_entries[] = {
536 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver,
537 proc_ide_write_driver },
538 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
539 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
540 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
541 { "settings", S_IFREG|S_IRUSR|S_IWUSR, proc_ide_read_settings,
542 proc_ide_write_settings },
543 { NULL, 0, NULL, NULL }
546 static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
548 struct proc_dir_entry *ent;
552 while (p->name != NULL) {
553 ent = create_proc_entry(p->name, p->mode, dir);
556 ent->read_proc = p->read_proc;
557 ent->write_proc = p->write_proc;
562 static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
566 while (p->name != NULL) {
567 remove_proc_entry(p->name, dir);
572 void ide_proc_register_driver(ide_drive_t *drive, ide_driver_t *driver)
574 mutex_lock(&ide_setting_mtx);
575 drive->settings = driver->settings;
576 mutex_unlock(&ide_setting_mtx);
578 ide_add_proc_entries(drive->proc, driver->proc, drive);
581 EXPORT_SYMBOL(ide_proc_register_driver);
584 * ide_proc_unregister_driver - remove driver specific data
588 * Clean up the driver specific /proc files and IDE settings
591 * Takes ide_setting_mtx and ide_lock.
592 * Caller must hold none of the locks.
595 void ide_proc_unregister_driver(ide_drive_t *drive, ide_driver_t *driver)
599 ide_remove_proc_entries(drive->proc, driver->proc);
601 mutex_lock(&ide_setting_mtx);
602 spin_lock_irqsave(&ide_lock, flags);
604 * ide_setting_mtx protects the settings list
605 * ide_lock protects the use of settings
607 * so we need to hold both, ide_settings_sem because we want to
608 * modify the settings list, and ide_lock because we cannot take
609 * a setting out that is being used.
611 * OTOH both ide_{read,write}_setting are only ever used under
614 drive->settings = NULL;
615 spin_unlock_irqrestore(&ide_lock, flags);
616 mutex_unlock(&ide_setting_mtx);
618 EXPORT_SYMBOL(ide_proc_unregister_driver);
620 void ide_proc_port_register_devices(ide_hwif_t *hwif)
623 struct proc_dir_entry *ent;
624 struct proc_dir_entry *parent = hwif->proc;
627 for (d = 0; d < MAX_DRIVES; d++) {
628 ide_drive_t *drive = &hwif->drives[d];
635 drive->proc = proc_mkdir(drive->name, parent);
637 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
638 sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name);
639 ent = proc_symlink(drive->name, proc_ide_root, name);
644 void ide_proc_unregister_device(ide_drive_t *drive)
647 ide_remove_proc_entries(drive->proc, generic_drive_entries);
648 remove_proc_entry(drive->name, proc_ide_root);
649 remove_proc_entry(drive->name, drive->hwif->proc);
654 static ide_proc_entry_t hwif_entries[] = {
655 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
656 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
657 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
658 { NULL, 0, NULL, NULL }
661 void ide_proc_register_port(ide_hwif_t *hwif)
664 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
669 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
673 void ide_proc_unregister_port(ide_hwif_t *hwif)
676 ide_remove_proc_entries(hwif->proc, hwif_entries);
677 remove_proc_entry(hwif->name, proc_ide_root);
682 static int proc_print_driver(struct device_driver *drv, void *data)
684 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
685 struct seq_file *s = data;
687 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
692 static int ide_drivers_show(struct seq_file *s, void *p)
696 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
698 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
703 static int ide_drivers_open(struct inode *inode, struct file *file)
705 return single_open(file, &ide_drivers_show, NULL);
708 static const struct file_operations ide_drivers_operations = {
709 .owner = THIS_MODULE,
710 .open = ide_drivers_open,
713 .release = single_release,
716 void proc_ide_create(void)
718 proc_ide_root = proc_mkdir("ide", NULL);
723 proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations);
726 void proc_ide_destroy(void)
728 remove_proc_entry("drivers", proc_ide_root);
729 remove_proc_entry("ide", NULL);