2 * linux/drivers/ide/ide-proc.c Version 1.05 Mar 05, 2003
4 * Copyright (C) 1997-1998 Mark Lord
5 * Copyright (C) 2003 Red Hat <alan@redhat.com>
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
16 * Also useful, "cat /proc/ide0/hda/[identify, smart_values,
17 * smart_thresholds, capabilities]" will issue an IDENTIFY /
18 * PACKET_IDENTIFY / SMART_READ_VALUES / SMART_READ_THRESHOLDS /
19 * SENSE CAPABILITIES command to /dev/hda, and then dump out the
20 * returned data as 256 16-bit words. The "hdparm" utility will
21 * be updated someday soon to use this mechanism.
25 #include <linux/module.h>
27 #include <asm/uaccess.h>
28 #include <linux/errno.h>
29 #include <linux/sched.h>
30 #include <linux/proc_fs.h>
31 #include <linux/stat.h>
33 #include <linux/pci.h>
34 #include <linux/ctype.h>
35 #include <linux/hdreg.h>
36 #include <linux/ide.h>
37 #include <linux/seq_file.h>
41 static int proc_ide_read_imodel
42 (char *page, char **start, off_t off, int count, int *eof, void *data)
44 ide_hwif_t *hwif = (ide_hwif_t *) data;
49 * Neither ide_unknown nor ide_forced should be set at this point.
51 switch (hwif->chipset) {
52 case ide_generic: name = "generic"; break;
53 case ide_pci: name = "pci"; break;
54 case ide_cmd640: name = "cmd640"; break;
55 case ide_dtc2278: name = "dtc2278"; break;
56 case ide_ali14xx: name = "ali14xx"; break;
57 case ide_qd65xx: name = "qd65xx"; break;
58 case ide_umc8672: name = "umc8672"; break;
59 case ide_ht6560b: name = "ht6560b"; break;
60 case ide_rz1000: name = "rz1000"; break;
61 case ide_trm290: name = "trm290"; break;
62 case ide_cmd646: name = "cmd646"; break;
63 case ide_cy82c693: name = "cy82c693"; break;
64 case ide_4drives: name = "4drives"; break;
65 case ide_pmac: name = "mac-io"; break;
66 case ide_au1xxx: name = "au1xxx"; break;
67 default: name = "(unknown)"; break;
69 len = sprintf(page, "%s\n", name);
70 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
73 static int proc_ide_read_mate
74 (char *page, char **start, off_t off, int count, int *eof, void *data)
76 ide_hwif_t *hwif = (ide_hwif_t *) data;
79 if (hwif && hwif->mate && hwif->mate->present)
80 len = sprintf(page, "%s\n", hwif->mate->name);
82 len = sprintf(page, "(none)\n");
83 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
86 static int proc_ide_read_channel
87 (char *page, char **start, off_t off, int count, int *eof, void *data)
89 ide_hwif_t *hwif = (ide_hwif_t *) data;
92 page[0] = hwif->channel ? '1' : '0';
95 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
98 static int proc_ide_read_identify
99 (char *page, char **start, off_t off, int count, int *eof, void *data)
101 ide_drive_t *drive = (ide_drive_t *)data;
105 len = sprintf(page, "\n");
108 unsigned short *val = (unsigned short *) page;
110 err = taskfile_lib_get_identify(drive, page);
112 char *out = ((char *)page) + (SECTOR_WORDS * 4);
115 out += sprintf(out, "%04x%c",
116 le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
118 } while (i < (SECTOR_WORDS * 2));
122 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
125 static void proc_ide_settings_warn(void)
127 static int warned = 0;
132 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
133 "obsolete, and will be removed soon!\n");
137 static int proc_ide_read_settings
138 (char *page, char **start, off_t off, int count, int *eof, void *data)
140 ide_drive_t *drive = (ide_drive_t *) data;
141 ide_settings_t *setting = (ide_settings_t *) drive->settings;
143 int len, rc, mul_factor, div_factor;
145 proc_ide_settings_warn();
147 down(&ide_setting_sem);
148 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
149 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
151 mul_factor = setting->mul_factor;
152 div_factor = setting->div_factor;
153 out += sprintf(out, "%-24s", setting->name);
154 if ((rc = ide_read_setting(drive, setting)) >= 0)
155 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
157 out += sprintf(out, "%-16s", "write-only");
158 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
159 if (setting->rw & SETTING_READ)
160 out += sprintf(out, "r");
161 if (setting->rw & SETTING_WRITE)
162 out += sprintf(out, "w");
163 out += sprintf(out, "\n");
164 setting = setting->next;
167 up(&ide_setting_sem);
168 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
173 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
174 unsigned long count, void *data)
176 ide_drive_t *drive = (ide_drive_t *) data;
177 char name[MAX_LEN + 1];
180 ide_settings_t *setting;
183 if (!capable(CAP_SYS_ADMIN))
186 proc_ide_settings_warn();
188 if (count >= PAGE_SIZE)
191 s = buf = (char *)__get_free_page(GFP_USER);
195 if (copy_from_user(buf, buffer, count)) {
196 free_page((unsigned long)buf);
203 * Skip over leading whitespace
205 while (count && isspace(*s)) {
210 * Do one full pass to verify all parameters,
211 * then do another to actually write the new settings.
220 while (n > 0 && *p != ':') {
228 memcpy(name, q, p - q);
237 val = simple_strtoul(p, &q, 10);
240 if (n > 0 && !isspace(*p))
242 while (n > 0 && isspace(*p)) {
247 down(&ide_setting_sem);
248 setting = ide_find_setting_by_name(drive, name);
251 up(&ide_setting_sem);
255 ide_write_setting(drive, setting, val * setting->div_factor / setting->mul_factor);
256 up(&ide_setting_sem);
258 } while (!for_real++);
259 free_page((unsigned long)buf);
262 free_page((unsigned long)buf);
263 printk("proc_ide_write_settings(): parse error\n");
267 int proc_ide_read_capacity
268 (char *page, char **start, off_t off, int count, int *eof, void *data)
270 int len = sprintf(page,"%llu\n", (long long)0x7fffffff);
271 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
274 EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
276 int proc_ide_read_geometry
277 (char *page, char **start, off_t off, int count, int *eof, void *data)
279 ide_drive_t *drive = (ide_drive_t *) data;
283 out += sprintf(out,"physical %d/%d/%d\n",
284 drive->cyl, drive->head, drive->sect);
285 out += sprintf(out,"logical %d/%d/%d\n",
286 drive->bios_cyl, drive->bios_head, drive->bios_sect);
289 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
292 EXPORT_SYMBOL(proc_ide_read_geometry);
294 static int proc_ide_read_dmodel
295 (char *page, char **start, off_t off, int count, int *eof, void *data)
297 ide_drive_t *drive = (ide_drive_t *) data;
298 struct hd_driveid *id = drive->id;
301 len = sprintf(page, "%.40s\n",
302 (id && id->model[0]) ? (char *)id->model : "(none)");
303 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
306 static int proc_ide_read_driver
307 (char *page, char **start, off_t off, int count, int *eof, void *data)
309 ide_drive_t *drive = (ide_drive_t *) data;
310 struct device *dev = &drive->gendev;
311 ide_driver_t *ide_drv;
314 down_read(&dev->bus->subsys.rwsem);
316 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
317 len = sprintf(page, "%s version %s\n",
318 dev->driver->name, ide_drv->version);
320 len = sprintf(page, "ide-default version 0.9.newide\n");
321 up_read(&dev->bus->subsys.rwsem);
322 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
325 static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
327 struct device *dev = &drive->gendev;
330 down_write(&dev->bus->subsys.rwsem);
331 device_release_driver(dev);
332 /* FIXME: device can still be in use by previous driver */
333 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
335 drive->driver_req[0] = 0;
336 if (dev->driver == NULL)
338 if (dev->driver && !strcmp(dev->driver->name, driver))
340 up_write(&dev->bus->subsys.rwsem);
345 static int proc_ide_write_driver
346 (struct file *file, const char __user *buffer, unsigned long count, void *data)
348 ide_drive_t *drive = (ide_drive_t *) data;
351 if (!capable(CAP_SYS_ADMIN))
355 if (copy_from_user(name, buffer, count))
358 if (ide_replace_subdriver(drive, name))
363 static int proc_ide_read_media
364 (char *page, char **start, off_t off, int count, int *eof, void *data)
366 ide_drive_t *drive = (ide_drive_t *) data;
370 switch (drive->media) {
371 case ide_disk: media = "disk\n";
373 case ide_cdrom: media = "cdrom\n";
375 case ide_tape: media = "tape\n";
377 case ide_floppy:media = "floppy\n";
379 case ide_optical:media = "optical\n";
381 default: media = "UNKNOWN\n";
386 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
389 static ide_proc_entry_t generic_drive_entries[] = {
390 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver, proc_ide_write_driver },
391 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
392 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
393 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
394 { "settings", S_IFREG|S_IRUSR|S_IWUSR,proc_ide_read_settings, proc_ide_write_settings },
395 { NULL, 0, NULL, NULL }
398 void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
400 struct proc_dir_entry *ent;
404 while (p->name != NULL) {
405 ent = create_proc_entry(p->name, p->mode, dir);
409 ent->read_proc = p->read_proc;
410 ent->write_proc = p->write_proc;
415 void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
419 while (p->name != NULL) {
420 remove_proc_entry(p->name, dir);
425 static void create_proc_ide_drives(ide_hwif_t *hwif)
428 struct proc_dir_entry *ent;
429 struct proc_dir_entry *parent = hwif->proc;
432 for (d = 0; d < MAX_DRIVES; d++) {
433 ide_drive_t *drive = &hwif->drives[d];
440 drive->proc = proc_mkdir(drive->name, parent);
442 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
443 sprintf(name,"ide%d/%s", (drive->name[2]-'a')/2, drive->name);
444 ent = proc_symlink(drive->name, proc_ide_root, name);
449 static void destroy_proc_ide_device(ide_hwif_t *hwif, ide_drive_t *drive)
452 ide_remove_proc_entries(drive->proc, generic_drive_entries);
453 remove_proc_entry(drive->name, proc_ide_root);
454 remove_proc_entry(drive->name, hwif->proc);
459 static void destroy_proc_ide_drives(ide_hwif_t *hwif)
463 for (d = 0; d < MAX_DRIVES; d++) {
464 ide_drive_t *drive = &hwif->drives[d];
466 destroy_proc_ide_device(hwif, drive);
470 static ide_proc_entry_t hwif_entries[] = {
471 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
472 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
473 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
474 { NULL, 0, NULL, NULL }
477 void create_proc_ide_interfaces(void)
481 for (h = 0; h < MAX_HWIFS; h++) {
482 ide_hwif_t *hwif = &ide_hwifs[h];
487 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
490 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
492 create_proc_ide_drives(hwif);
496 EXPORT_SYMBOL(create_proc_ide_interfaces);
498 #ifdef CONFIG_BLK_DEV_IDEPCI
499 void ide_pci_create_host_proc(const char *name, get_info_t *get_info)
501 create_proc_info_entry(name, 0, proc_ide_root, get_info);
504 EXPORT_SYMBOL_GPL(ide_pci_create_host_proc);
507 void destroy_proc_ide_interface(ide_hwif_t *hwif)
510 destroy_proc_ide_drives(hwif);
511 ide_remove_proc_entries(hwif->proc, hwif_entries);
512 remove_proc_entry(hwif->name, proc_ide_root);
517 static int proc_print_driver(struct device_driver *drv, void *data)
519 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
520 struct seq_file *s = data;
522 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
527 static int ide_drivers_show(struct seq_file *s, void *p)
529 bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
533 static int ide_drivers_open(struct inode *inode, struct file *file)
535 return single_open(file, &ide_drivers_show, NULL);
538 static struct file_operations ide_drivers_operations = {
539 .open = ide_drivers_open,
542 .release = single_release,
545 void proc_ide_create(void)
547 struct proc_dir_entry *entry;
552 create_proc_ide_interfaces();
554 entry = create_proc_entry("drivers", 0, proc_ide_root);
556 entry->proc_fops = &ide_drivers_operations;
559 void proc_ide_destroy(void)
561 remove_proc_entry("drivers", proc_ide_root);
562 remove_proc_entry("ide", NULL);