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
127 static const struct ide_devset *ide_find_setting(const struct ide_devset **st,
131 if (strcmp((*st)->name, name) == 0)
139 * ide_read_setting - read an IDE setting
140 * @drive: drive to read from
141 * @setting: drive setting
143 * Read a drive setting and return the value. The caller
144 * must hold the ide_setting_mtx when making this call.
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
151 static int ide_read_setting(ide_drive_t *drive,
152 const struct ide_devset *setting)
156 if ((setting->flags & S_READ)) {
159 spin_lock_irqsave(&ide_lock, flags);
160 val = setting->get(drive);
161 spin_unlock_irqrestore(&ide_lock, flags);
168 * ide_write_setting - read an IDE setting
169 * @drive: drive to read from
170 * @setting: drive setting
173 * Write a drive setting if it is possible. The caller
174 * must hold the ide_setting_mtx when making this call.
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
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.
185 static int ide_write_setting(ide_drive_t *drive,
186 const struct ide_devset *setting, int val)
188 if (!capable(CAP_SYS_ADMIN))
190 if (setting->set && (setting->flags & S_NOLOCK))
191 return setting->set(drive, val);
192 if (!(setting->flags & S_WRITE))
194 if (val < setting->min || val > setting->max)
196 if (ide_spin_wait_hwgroup(drive))
198 setting->set(drive, val);
199 spin_unlock_irq(&ide_lock);
203 static ide_devset_get(xfer_rate, current_speed);
205 static int set_xfer_rate (ide_drive_t *drive, int arg)
210 if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
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 |
220 err = ide_no_data_taskfile(drive, &task);
223 ide_set_xfer_rate(drive, (u8) arg);
224 ide_driveid_update(drive);
229 ide_devset_rw_nolock(current_speed, 0, 70, xfer_rate);
230 ide_devset_rw_nolock(io_32bit, 0, 1 + (SUPPORT_VLB_SYNC << 1), io_32bit);
231 ide_devset_rw_nolock(keepsettings, 0, 1, ksettings);
232 ide_devset_rw_nolock(unmaskirq, 0, 1, unmaskirq);
233 ide_devset_rw_nolock(using_dma, 0, 1, using_dma);
235 ide_devset_w_nolock(pio_mode, 0, 255, pio_mode);
237 ide_devset_rw(init_speed, 0, 70, init_speed);
238 ide_devset_rw(nice1, 0, 1, nice1);
239 ide_devset_rw(number, 0, 3, dn);
241 static 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,
248 &ide_devset_pio_mode,
249 &ide_devset_unmaskirq,
250 &ide_devset_using_dma,
254 static void proc_ide_settings_warn(void)
261 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
262 "obsolete, and will be removed soon!\n");
266 static int proc_ide_read_settings
267 (char *page, char **start, off_t off, int count, int *eof, void *data)
269 const struct ide_devset *setting, **g, **d;
270 ide_drive_t *drive = (ide_drive_t *) data;
272 int len, rc, mul_factor, div_factor;
274 proc_ide_settings_warn();
276 mutex_lock(&ide_setting_mtx);
277 g = ide_generic_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 */
284 if (strcmp((*d)->name, (*g)->name) < 0)
288 } else if (d && *d) {
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);
297 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
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");
308 mutex_unlock(&ide_setting_mtx);
309 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
314 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
315 unsigned long count, void *data)
317 ide_drive_t *drive = (ide_drive_t *) data;
318 char name[MAX_LEN + 1];
319 int for_real = 0, mul_factor, div_factor;
322 const struct ide_devset *setting;
325 if (!capable(CAP_SYS_ADMIN))
328 proc_ide_settings_warn();
330 if (count >= PAGE_SIZE)
333 s = buf = (char *)__get_free_page(GFP_USER);
337 if (copy_from_user(buf, buffer, count)) {
338 free_page((unsigned long)buf);
345 * Skip over leading whitespace
347 while (count && isspace(*s)) {
352 * Do one full pass to verify all parameters,
353 * then do another to actually write the new settings.
362 while (n > 0 && *p != ':') {
370 memcpy(name, q, p - q);
379 val = simple_strtoul(p, &q, 10);
382 if (n > 0 && !isspace(*p))
384 while (n > 0 && isspace(*p)) {
389 mutex_lock(&ide_setting_mtx);
390 /* generic settings first, then driver specific ones */
391 setting = ide_find_setting(ide_generic_settings, name);
394 setting = ide_find_setting(drive->settings, name);
396 mutex_unlock(&ide_setting_mtx);
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);
405 mutex_unlock(&ide_setting_mtx);
407 } while (!for_real++);
408 free_page((unsigned long)buf);
411 free_page((unsigned long)buf);
412 printk("proc_ide_write_settings(): parse error\n");
416 int proc_ide_read_capacity
417 (char *page, char **start, off_t off, int count, int *eof, void *data)
419 int len = sprintf(page, "%llu\n", (long long)0x7fffffff);
420 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
423 EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
425 int proc_ide_read_geometry
426 (char *page, char **start, off_t off, int count, int *eof, void *data)
428 ide_drive_t *drive = (ide_drive_t *) data;
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);
438 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
441 EXPORT_SYMBOL(proc_ide_read_geometry);
443 static int proc_ide_read_dmodel
444 (char *page, char **start, off_t off, int count, int *eof, void *data)
446 ide_drive_t *drive = (ide_drive_t *) data;
447 char *m = (char *)&drive->id[ATA_ID_PROD];
450 len = sprintf(page, "%.40s\n", m[0] ? m : "(none)");
451 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
454 static int proc_ide_read_driver
455 (char *page, char **start, off_t off, int count, int *eof, void *data)
457 ide_drive_t *drive = (ide_drive_t *) data;
458 struct device *dev = &drive->gendev;
459 ide_driver_t *ide_drv;
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);
467 len = sprintf(page, "ide-default version 0.9.newide\n");
468 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
471 static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
473 struct device *dev = &drive->gendev;
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);
482 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
484 drive->driver_req[0] = 0;
485 if (dev->driver == NULL) {
486 err = device_attach(dev);
489 "IDE: %s: device_attach(2) error: %d\n",
492 if (dev->driver && !strcmp(dev->driver->name, driver))
498 static int proc_ide_write_driver
499 (struct file *file, const char __user *buffer, unsigned long count, void *data)
501 ide_drive_t *drive = (ide_drive_t *) data;
504 if (!capable(CAP_SYS_ADMIN))
508 if (copy_from_user(name, buffer, count))
511 if (ide_replace_subdriver(drive, name))
516 static int proc_ide_read_media
517 (char *page, char **start, off_t off, int count, int *eof, void *data)
519 ide_drive_t *drive = (ide_drive_t *) data;
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;
533 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
536 static 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 }
547 static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
549 struct proc_dir_entry *ent;
553 while (p->name != NULL) {
554 ent = create_proc_entry(p->name, p->mode, dir);
557 ent->read_proc = p->read_proc;
558 ent->write_proc = p->write_proc;
563 static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
567 while (p->name != NULL) {
568 remove_proc_entry(p->name, dir);
573 void ide_proc_register_driver(ide_drive_t *drive, ide_driver_t *driver)
575 mutex_lock(&ide_setting_mtx);
576 drive->settings = driver->settings;
577 mutex_unlock(&ide_setting_mtx);
579 ide_add_proc_entries(drive->proc, driver->proc, drive);
582 EXPORT_SYMBOL(ide_proc_register_driver);
585 * ide_proc_unregister_driver - remove driver specific data
589 * Clean up the driver specific /proc files and IDE settings
592 * Takes ide_setting_mtx and ide_lock.
593 * Caller must hold none of the locks.
596 void ide_proc_unregister_driver(ide_drive_t *drive, ide_driver_t *driver)
600 ide_remove_proc_entries(drive->proc, driver->proc);
602 mutex_lock(&ide_setting_mtx);
603 spin_lock_irqsave(&ide_lock, flags);
605 * ide_setting_mtx protects the settings list
606 * ide_lock protects the use of settings
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.
612 * OTOH both ide_{read,write}_setting are only ever used under
615 drive->settings = NULL;
616 spin_unlock_irqrestore(&ide_lock, flags);
617 mutex_unlock(&ide_setting_mtx);
619 EXPORT_SYMBOL(ide_proc_unregister_driver);
621 void ide_proc_port_register_devices(ide_hwif_t *hwif)
624 struct proc_dir_entry *ent;
625 struct proc_dir_entry *parent = hwif->proc;
628 for (d = 0; d < MAX_DRIVES; d++) {
629 ide_drive_t *drive = &hwif->drives[d];
636 drive->proc = proc_mkdir(drive->name, parent);
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);
645 void ide_proc_unregister_device(ide_drive_t *drive)
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);
655 static 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 }
662 void ide_proc_register_port(ide_hwif_t *hwif)
665 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
670 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
674 void ide_proc_unregister_port(ide_hwif_t *hwif)
677 ide_remove_proc_entries(hwif->proc, hwif_entries);
678 remove_proc_entry(hwif->name, proc_ide_root);
683 static int proc_print_driver(struct device_driver *drv, void *data)
685 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
686 struct seq_file *s = data;
688 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
693 static int ide_drivers_show(struct seq_file *s, void *p)
697 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
699 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
704 static int ide_drivers_open(struct inode *inode, struct file *file)
706 return single_open(file, &ide_drivers_show, NULL);
709 static const struct file_operations ide_drivers_operations = {
710 .owner = THIS_MODULE,
711 .open = ide_drivers_open,
714 .release = single_release,
717 void proc_ide_create(void)
719 proc_ide_root = proc_mkdir("ide", NULL);
724 proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations);
727 void proc_ide_destroy(void)
729 remove_proc_entry("drivers", proc_ide_root);
730 remove_proc_entry("ide", NULL);