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_cy82c693: name = "cy82c693"; break;
50 case ide_4drives: name = "4drives"; break;
51 case ide_pmac: name = "mac-io"; break;
52 case ide_au1xxx: name = "au1xxx"; break;
53 case ide_palm3710: name = "palm3710"; break;
54 case ide_acorn: name = "acorn"; break;
55 default: name = "(unknown)"; break;
57 len = sprintf(page, "%s\n", name);
58 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
61 static int proc_ide_read_mate
62 (char *page, char **start, off_t off, int count, int *eof, void *data)
64 ide_hwif_t *hwif = (ide_hwif_t *) data;
67 if (hwif && hwif->mate)
68 len = sprintf(page, "%s\n", hwif->mate->name);
70 len = sprintf(page, "(none)\n");
71 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
74 static int proc_ide_read_channel
75 (char *page, char **start, off_t off, int count, int *eof, void *data)
77 ide_hwif_t *hwif = (ide_hwif_t *) data;
80 page[0] = hwif->channel ? '1' : '0';
83 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
86 static int proc_ide_read_identify
87 (char *page, char **start, off_t off, int count, int *eof, void *data)
89 ide_drive_t *drive = (ide_drive_t *)data;
93 len = sprintf(page, "\n");
96 __le16 *val = (__le16 *)page;
98 err = taskfile_lib_get_identify(drive, page);
100 char *out = (char *)page + SECTOR_SIZE;
104 out += sprintf(out, "%04x%c",
105 le16_to_cpup(val), (++i & 7) ? ' ' : '\n');
107 } while (i < SECTOR_SIZE / 2);
111 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
115 * ide_find_setting - find a specific setting
116 * @st: setting table pointer
117 * @name: setting name
119 * Scan's the setting table for a matching entry and returns
120 * this or NULL if no entry is found. The caller must hold the
125 const struct ide_proc_devset *ide_find_setting(const struct ide_proc_devset *st,
129 if (strcmp(st->name, name) == 0)
133 return st->name ? st : NULL;
137 * ide_read_setting - read an IDE setting
138 * @drive: drive to read from
139 * @setting: drive setting
141 * Read a drive setting and return the value. The caller
142 * must hold the ide_setting_mtx when making this call.
144 * BUGS: the data return and error are the same return value
145 * so an error -EINVAL and true return of the same value cannot
149 static int ide_read_setting(ide_drive_t *drive,
150 const struct ide_proc_devset *setting)
152 const struct ide_devset *ds = setting->setting;
156 val = ds->get(drive);
162 * ide_write_setting - read an IDE setting
163 * @drive: drive to read from
164 * @setting: drive setting
167 * Write a drive setting if it is possible. The caller
168 * must hold the ide_setting_mtx when making this call.
170 * BUGS: the data return and error are the same return value
171 * so an error -EINVAL and true return of the same value cannot
174 * FIXME: This should be changed to enqueue a special request
175 * to the driver to change settings, and then wait on a sema for completion.
176 * The current scheme of polling is kludgy, though safe enough.
179 static int ide_write_setting(ide_drive_t *drive,
180 const struct ide_proc_devset *setting, int val)
182 const struct ide_devset *ds = setting->setting;
184 if (!capable(CAP_SYS_ADMIN))
188 if ((ds->flags & DS_SYNC)
189 && (val < setting->min || val > setting->max))
191 return ide_devset_execute(drive, ds, val);
194 ide_devset_get(xfer_rate, current_speed);
196 static int set_xfer_rate (ide_drive_t *drive, int arg)
201 if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
204 memset(&task, 0, sizeof(task));
205 task.tf.command = ATA_CMD_SET_FEATURES;
206 task.tf.feature = SETFEATURES_XFER;
207 task.tf.nsect = (u8)arg;
208 task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT |
211 err = ide_no_data_taskfile(drive, &task);
214 ide_set_xfer_rate(drive, (u8) arg);
215 ide_driveid_update(drive);
220 ide_devset_rw(current_speed, xfer_rate);
221 ide_devset_rw_field(init_speed, init_speed);
222 ide_devset_rw_flag(nice1, IDE_DFLAG_NICE1);
223 ide_devset_rw_field(number, dn);
225 static const struct ide_proc_devset ide_generic_settings[] = {
226 IDE_PROC_DEVSET(current_speed, 0, 70),
227 IDE_PROC_DEVSET(init_speed, 0, 70),
228 IDE_PROC_DEVSET(io_32bit, 0, 1 + (SUPPORT_VLB_SYNC << 1)),
229 IDE_PROC_DEVSET(keepsettings, 0, 1),
230 IDE_PROC_DEVSET(nice1, 0, 1),
231 IDE_PROC_DEVSET(number, 0, 3),
232 IDE_PROC_DEVSET(pio_mode, 0, 255),
233 IDE_PROC_DEVSET(unmaskirq, 0, 1),
234 IDE_PROC_DEVSET(using_dma, 0, 1),
238 static void proc_ide_settings_warn(void)
245 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
246 "obsolete, and will be removed soon!\n");
250 static int proc_ide_read_settings
251 (char *page, char **start, off_t off, int count, int *eof, void *data)
253 const struct ide_proc_devset *setting, *g, *d;
254 const struct ide_devset *ds;
255 ide_drive_t *drive = (ide_drive_t *) data;
257 int len, rc, mul_factor, div_factor;
259 proc_ide_settings_warn();
261 mutex_lock(&ide_setting_mtx);
262 g = ide_generic_settings;
264 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
265 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
266 while (g->name || (d && d->name)) {
267 /* read settings in the alphabetical order */
268 if (g->name && d && d->name) {
269 if (strcmp(d->name, g->name) < 0)
273 } else if (d && d->name) {
277 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
278 div_factor = setting->divf ? setting->divf(drive) : 1;
279 out += sprintf(out, "%-24s", setting->name);
280 rc = ide_read_setting(drive, setting);
282 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
284 out += sprintf(out, "%-16s", "write-only");
285 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
286 ds = setting->setting;
288 out += sprintf(out, "r");
290 out += sprintf(out, "w");
291 out += sprintf(out, "\n");
294 mutex_unlock(&ide_setting_mtx);
295 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
300 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
301 unsigned long count, void *data)
303 ide_drive_t *drive = (ide_drive_t *) data;
304 char name[MAX_LEN + 1];
305 int for_real = 0, mul_factor, div_factor;
308 const struct ide_proc_devset *setting;
311 if (!capable(CAP_SYS_ADMIN))
314 proc_ide_settings_warn();
316 if (count >= PAGE_SIZE)
319 s = buf = (char *)__get_free_page(GFP_USER);
323 if (copy_from_user(buf, buffer, count)) {
324 free_page((unsigned long)buf);
331 * Skip over leading whitespace
333 while (count && isspace(*s)) {
338 * Do one full pass to verify all parameters,
339 * then do another to actually write the new settings.
348 while (n > 0 && *p != ':') {
356 memcpy(name, q, p - q);
365 val = simple_strtoul(p, &q, 10);
368 if (n > 0 && !isspace(*p))
370 while (n > 0 && isspace(*p)) {
375 mutex_lock(&ide_setting_mtx);
376 /* generic settings first, then driver specific ones */
377 setting = ide_find_setting(ide_generic_settings, name);
380 setting = ide_find_setting(drive->settings, name);
382 mutex_unlock(&ide_setting_mtx);
387 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
388 div_factor = setting->divf ? setting->divf(drive) : 1;
389 ide_write_setting(drive, setting, val * div_factor / mul_factor);
391 mutex_unlock(&ide_setting_mtx);
393 } while (!for_real++);
394 free_page((unsigned long)buf);
397 free_page((unsigned long)buf);
398 printk("proc_ide_write_settings(): parse error\n");
402 int proc_ide_read_capacity
403 (char *page, char **start, off_t off, int count, int *eof, void *data)
405 int len = sprintf(page, "%llu\n", (long long)0x7fffffff);
406 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
409 EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
411 int proc_ide_read_geometry
412 (char *page, char **start, off_t off, int count, int *eof, void *data)
414 ide_drive_t *drive = (ide_drive_t *) data;
418 out += sprintf(out, "physical %d/%d/%d\n",
419 drive->cyl, drive->head, drive->sect);
420 out += sprintf(out, "logical %d/%d/%d\n",
421 drive->bios_cyl, drive->bios_head, drive->bios_sect);
424 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
427 EXPORT_SYMBOL(proc_ide_read_geometry);
429 static int proc_ide_read_dmodel
430 (char *page, char **start, off_t off, int count, int *eof, void *data)
432 ide_drive_t *drive = (ide_drive_t *) data;
433 char *m = (char *)&drive->id[ATA_ID_PROD];
436 len = sprintf(page, "%.40s\n", m[0] ? m : "(none)");
437 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
440 static int proc_ide_read_driver
441 (char *page, char **start, off_t off, int count, int *eof, void *data)
443 ide_drive_t *drive = (ide_drive_t *) data;
444 struct device *dev = &drive->gendev;
445 ide_driver_t *ide_drv;
449 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
450 len = sprintf(page, "%s version %s\n",
451 dev->driver->name, ide_drv->version);
453 len = sprintf(page, "ide-default version 0.9.newide\n");
454 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
457 static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
459 struct device *dev = &drive->gendev;
463 device_release_driver(dev);
464 /* FIXME: device can still be in use by previous driver */
465 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
466 err = device_attach(dev);
468 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
470 drive->driver_req[0] = 0;
471 if (dev->driver == NULL) {
472 err = device_attach(dev);
475 "IDE: %s: device_attach(2) error: %d\n",
478 if (dev->driver && !strcmp(dev->driver->name, driver))
484 static int proc_ide_write_driver
485 (struct file *file, const char __user *buffer, unsigned long count, void *data)
487 ide_drive_t *drive = (ide_drive_t *) data;
490 if (!capable(CAP_SYS_ADMIN))
494 if (copy_from_user(name, buffer, count))
497 if (ide_replace_subdriver(drive, name))
502 static int proc_ide_read_media
503 (char *page, char **start, off_t off, int count, int *eof, void *data)
505 ide_drive_t *drive = (ide_drive_t *) data;
509 switch (drive->media) {
510 case ide_disk: media = "disk\n"; break;
511 case ide_cdrom: media = "cdrom\n"; break;
512 case ide_tape: media = "tape\n"; break;
513 case ide_floppy: media = "floppy\n"; break;
514 case ide_optical: media = "optical\n"; break;
515 default: media = "UNKNOWN\n"; break;
519 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
522 static ide_proc_entry_t generic_drive_entries[] = {
523 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver,
524 proc_ide_write_driver },
525 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
526 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
527 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
528 { "settings", S_IFREG|S_IRUSR|S_IWUSR, proc_ide_read_settings,
529 proc_ide_write_settings },
530 { NULL, 0, NULL, NULL }
533 static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
535 struct proc_dir_entry *ent;
539 while (p->name != NULL) {
540 ent = create_proc_entry(p->name, p->mode, dir);
543 ent->read_proc = p->read_proc;
544 ent->write_proc = p->write_proc;
549 static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
553 while (p->name != NULL) {
554 remove_proc_entry(p->name, dir);
559 void ide_proc_register_driver(ide_drive_t *drive, ide_driver_t *driver)
561 mutex_lock(&ide_setting_mtx);
562 drive->settings = driver->proc_devsets(drive);
563 mutex_unlock(&ide_setting_mtx);
565 ide_add_proc_entries(drive->proc, driver->proc_entries(drive), drive);
568 EXPORT_SYMBOL(ide_proc_register_driver);
571 * ide_proc_unregister_driver - remove driver specific data
575 * Clean up the driver specific /proc files and IDE settings
578 * Takes ide_setting_mtx.
581 void ide_proc_unregister_driver(ide_drive_t *drive, ide_driver_t *driver)
583 ide_remove_proc_entries(drive->proc, driver->proc_entries(drive));
585 mutex_lock(&ide_setting_mtx);
587 * ide_setting_mtx protects both the settings list and the use
588 * of settings (we cannot take a setting out that is being used).
590 drive->settings = NULL;
591 mutex_unlock(&ide_setting_mtx);
593 EXPORT_SYMBOL(ide_proc_unregister_driver);
595 void ide_proc_port_register_devices(ide_hwif_t *hwif)
598 struct proc_dir_entry *ent;
599 struct proc_dir_entry *parent = hwif->proc;
602 for (d = 0; d < MAX_DRIVES; d++) {
603 ide_drive_t *drive = &hwif->drives[d];
605 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0 || drive->proc)
608 drive->proc = proc_mkdir(drive->name, parent);
610 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
611 sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name);
612 ent = proc_symlink(drive->name, proc_ide_root, name);
617 void ide_proc_unregister_device(ide_drive_t *drive)
620 ide_remove_proc_entries(drive->proc, generic_drive_entries);
621 remove_proc_entry(drive->name, proc_ide_root);
622 remove_proc_entry(drive->name, drive->hwif->proc);
627 static ide_proc_entry_t hwif_entries[] = {
628 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
629 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
630 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
631 { NULL, 0, NULL, NULL }
634 void ide_proc_register_port(ide_hwif_t *hwif)
637 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
642 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
646 void ide_proc_unregister_port(ide_hwif_t *hwif)
649 ide_remove_proc_entries(hwif->proc, hwif_entries);
650 remove_proc_entry(hwif->name, proc_ide_root);
655 static int proc_print_driver(struct device_driver *drv, void *data)
657 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
658 struct seq_file *s = data;
660 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
665 static int ide_drivers_show(struct seq_file *s, void *p)
669 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
671 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
676 static int ide_drivers_open(struct inode *inode, struct file *file)
678 return single_open(file, &ide_drivers_show, NULL);
681 static const struct file_operations ide_drivers_operations = {
682 .owner = THIS_MODULE,
683 .open = ide_drivers_open,
686 .release = single_release,
689 void proc_ide_create(void)
691 proc_ide_root = proc_mkdir("ide", NULL);
696 proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations);
699 void proc_ide_destroy(void)
701 remove_proc_entry("drivers", proc_ide_root);
702 remove_proc_entry("ide", NULL);