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/proc_fs.h>
30 #include <linux/stat.h>
32 #include <linux/pci.h>
33 #include <linux/ctype.h>
34 #include <linux/hdreg.h>
35 #include <linux/ide.h>
36 #include <linux/seq_file.h>
40 static int proc_ide_read_imodel
41 (char *page, char **start, off_t off, int count, int *eof, void *data)
43 ide_hwif_t *hwif = (ide_hwif_t *) data;
48 * Neither ide_unknown nor ide_forced should be set at this point.
50 switch (hwif->chipset) {
51 case ide_generic: name = "generic"; break;
52 case ide_pci: name = "pci"; break;
53 case ide_cmd640: name = "cmd640"; break;
54 case ide_dtc2278: name = "dtc2278"; break;
55 case ide_ali14xx: name = "ali14xx"; break;
56 case ide_qd65xx: name = "qd65xx"; break;
57 case ide_umc8672: name = "umc8672"; break;
58 case ide_ht6560b: name = "ht6560b"; break;
59 case ide_rz1000: name = "rz1000"; break;
60 case ide_trm290: name = "trm290"; break;
61 case ide_cmd646: name = "cmd646"; break;
62 case ide_cy82c693: name = "cy82c693"; break;
63 case ide_4drives: name = "4drives"; break;
64 case ide_pmac: name = "mac-io"; break;
65 case ide_au1xxx: name = "au1xxx"; break;
66 default: name = "(unknown)"; break;
68 len = sprintf(page, "%s\n", name);
69 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
72 static int proc_ide_read_mate
73 (char *page, char **start, off_t off, int count, int *eof, void *data)
75 ide_hwif_t *hwif = (ide_hwif_t *) data;
78 if (hwif && hwif->mate && hwif->mate->present)
79 len = sprintf(page, "%s\n", hwif->mate->name);
81 len = sprintf(page, "(none)\n");
82 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
85 static int proc_ide_read_channel
86 (char *page, char **start, off_t off, int count, int *eof, void *data)
88 ide_hwif_t *hwif = (ide_hwif_t *) data;
91 page[0] = hwif->channel ? '1' : '0';
94 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
97 static int proc_ide_read_identify
98 (char *page, char **start, off_t off, int count, int *eof, void *data)
100 ide_drive_t *drive = (ide_drive_t *)data;
104 len = sprintf(page, "\n");
107 unsigned short *val = (unsigned short *) page;
109 err = taskfile_lib_get_identify(drive, page);
111 char *out = ((char *)page) + (SECTOR_WORDS * 4);
114 out += sprintf(out, "%04x%c",
115 le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
117 } while (i < (SECTOR_WORDS * 2));
121 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
124 static void proc_ide_settings_warn(void)
126 static int warned = 0;
131 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
132 "obsolete, and will be removed soon!\n");
136 static int proc_ide_read_settings
137 (char *page, char **start, off_t off, int count, int *eof, void *data)
139 ide_drive_t *drive = (ide_drive_t *) data;
140 ide_settings_t *setting = (ide_settings_t *) drive->settings;
142 int len, rc, mul_factor, div_factor;
144 proc_ide_settings_warn();
146 down(&ide_setting_sem);
147 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
148 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
150 mul_factor = setting->mul_factor;
151 div_factor = setting->div_factor;
152 out += sprintf(out, "%-24s", setting->name);
153 if ((rc = ide_read_setting(drive, setting)) >= 0)
154 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
156 out += sprintf(out, "%-16s", "write-only");
157 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
158 if (setting->rw & SETTING_READ)
159 out += sprintf(out, "r");
160 if (setting->rw & SETTING_WRITE)
161 out += sprintf(out, "w");
162 out += sprintf(out, "\n");
163 setting = setting->next;
166 up(&ide_setting_sem);
167 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
172 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
173 unsigned long count, void *data)
175 ide_drive_t *drive = (ide_drive_t *) data;
176 char name[MAX_LEN + 1];
179 ide_settings_t *setting;
182 if (!capable(CAP_SYS_ADMIN))
185 proc_ide_settings_warn();
187 if (count >= PAGE_SIZE)
190 s = buf = (char *)__get_free_page(GFP_USER);
194 if (copy_from_user(buf, buffer, count)) {
195 free_page((unsigned long)buf);
202 * Skip over leading whitespace
204 while (count && isspace(*s)) {
209 * Do one full pass to verify all parameters,
210 * then do another to actually write the new settings.
219 while (n > 0 && *p != ':') {
227 memcpy(name, q, p - q);
236 val = simple_strtoul(p, &q, 10);
239 if (n > 0 && !isspace(*p))
241 while (n > 0 && isspace(*p)) {
246 down(&ide_setting_sem);
247 setting = ide_find_setting_by_name(drive, name);
250 up(&ide_setting_sem);
254 ide_write_setting(drive, setting, val * setting->div_factor / setting->mul_factor);
255 up(&ide_setting_sem);
257 } while (!for_real++);
258 free_page((unsigned long)buf);
261 free_page((unsigned long)buf);
262 printk("proc_ide_write_settings(): parse error\n");
266 int proc_ide_read_capacity
267 (char *page, char **start, off_t off, int count, int *eof, void *data)
269 int len = sprintf(page,"%llu\n", (long long)0x7fffffff);
270 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
273 EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
275 int proc_ide_read_geometry
276 (char *page, char **start, off_t off, int count, int *eof, void *data)
278 ide_drive_t *drive = (ide_drive_t *) data;
282 out += sprintf(out,"physical %d/%d/%d\n",
283 drive->cyl, drive->head, drive->sect);
284 out += sprintf(out,"logical %d/%d/%d\n",
285 drive->bios_cyl, drive->bios_head, drive->bios_sect);
288 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
291 EXPORT_SYMBOL(proc_ide_read_geometry);
293 static int proc_ide_read_dmodel
294 (char *page, char **start, off_t off, int count, int *eof, void *data)
296 ide_drive_t *drive = (ide_drive_t *) data;
297 struct hd_driveid *id = drive->id;
300 len = sprintf(page, "%.40s\n",
301 (id && id->model[0]) ? (char *)id->model : "(none)");
302 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
305 static int proc_ide_read_driver
306 (char *page, char **start, off_t off, int count, int *eof, void *data)
308 ide_drive_t *drive = (ide_drive_t *) data;
309 struct device *dev = &drive->gendev;
310 ide_driver_t *ide_drv;
314 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
315 len = sprintf(page, "%s version %s\n",
316 dev->driver->name, ide_drv->version);
318 len = sprintf(page, "ide-default version 0.9.newide\n");
319 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
322 static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
324 struct device *dev = &drive->gendev;
328 device_release_driver(dev);
329 /* FIXME: device can still be in use by previous driver */
330 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
331 err = device_attach(dev);
333 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
335 drive->driver_req[0] = 0;
336 if (dev->driver == NULL) {
337 err = device_attach(dev);
340 "IDE: %s: device_attach(2) error: %d\n",
343 if (dev->driver && !strcmp(dev->driver->name, driver))
349 static int proc_ide_write_driver
350 (struct file *file, const char __user *buffer, unsigned long count, void *data)
352 ide_drive_t *drive = (ide_drive_t *) data;
355 if (!capable(CAP_SYS_ADMIN))
359 if (copy_from_user(name, buffer, count))
362 if (ide_replace_subdriver(drive, name))
367 static int proc_ide_read_media
368 (char *page, char **start, off_t off, int count, int *eof, void *data)
370 ide_drive_t *drive = (ide_drive_t *) data;
374 switch (drive->media) {
375 case ide_disk: media = "disk\n";
377 case ide_cdrom: media = "cdrom\n";
379 case ide_tape: media = "tape\n";
381 case ide_floppy:media = "floppy\n";
383 case ide_optical:media = "optical\n";
385 default: media = "UNKNOWN\n";
390 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
393 static ide_proc_entry_t generic_drive_entries[] = {
394 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver, proc_ide_write_driver },
395 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
396 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
397 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
398 { "settings", S_IFREG|S_IRUSR|S_IWUSR,proc_ide_read_settings, proc_ide_write_settings },
399 { NULL, 0, NULL, NULL }
402 void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
404 struct proc_dir_entry *ent;
408 while (p->name != NULL) {
409 ent = create_proc_entry(p->name, p->mode, dir);
412 ent->read_proc = p->read_proc;
413 ent->write_proc = p->write_proc;
418 void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
422 while (p->name != NULL) {
423 remove_proc_entry(p->name, dir);
428 static void create_proc_ide_drives(ide_hwif_t *hwif)
431 struct proc_dir_entry *ent;
432 struct proc_dir_entry *parent = hwif->proc;
435 for (d = 0; d < MAX_DRIVES; d++) {
436 ide_drive_t *drive = &hwif->drives[d];
443 drive->proc = proc_mkdir(drive->name, parent);
445 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
446 sprintf(name,"ide%d/%s", (drive->name[2]-'a')/2, drive->name);
447 ent = proc_symlink(drive->name, proc_ide_root, name);
452 static void destroy_proc_ide_device(ide_hwif_t *hwif, ide_drive_t *drive)
455 ide_remove_proc_entries(drive->proc, generic_drive_entries);
456 remove_proc_entry(drive->name, proc_ide_root);
457 remove_proc_entry(drive->name, hwif->proc);
462 static void destroy_proc_ide_drives(ide_hwif_t *hwif)
466 for (d = 0; d < MAX_DRIVES; d++) {
467 ide_drive_t *drive = &hwif->drives[d];
469 destroy_proc_ide_device(hwif, drive);
473 static ide_proc_entry_t hwif_entries[] = {
474 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
475 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
476 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
477 { NULL, 0, NULL, NULL }
480 void create_proc_ide_interfaces(void)
484 for (h = 0; h < MAX_HWIFS; h++) {
485 ide_hwif_t *hwif = &ide_hwifs[h];
490 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
493 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
495 create_proc_ide_drives(hwif);
499 EXPORT_SYMBOL(create_proc_ide_interfaces);
501 #ifdef CONFIG_BLK_DEV_IDEPCI
502 void ide_pci_create_host_proc(const char *name, get_info_t *get_info)
504 create_proc_info_entry(name, 0, proc_ide_root, get_info);
507 EXPORT_SYMBOL_GPL(ide_pci_create_host_proc);
510 void destroy_proc_ide_interface(ide_hwif_t *hwif)
513 destroy_proc_ide_drives(hwif);
514 ide_remove_proc_entries(hwif->proc, hwif_entries);
515 remove_proc_entry(hwif->name, proc_ide_root);
520 static int proc_print_driver(struct device_driver *drv, void *data)
522 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
523 struct seq_file *s = data;
525 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
530 static int ide_drivers_show(struct seq_file *s, void *p)
534 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
536 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
541 static int ide_drivers_open(struct inode *inode, struct file *file)
543 return single_open(file, &ide_drivers_show, NULL);
546 static const struct file_operations ide_drivers_operations = {
547 .open = ide_drivers_open,
550 .release = single_release,
553 void proc_ide_create(void)
555 struct proc_dir_entry *entry;
560 create_proc_ide_interfaces();
562 entry = create_proc_entry("drivers", 0, proc_ide_root);
564 entry->proc_fops = &ide_drivers_operations;
567 void proc_ide_destroy(void)
569 remove_proc_entry("drivers", proc_ide_root);
570 remove_proc_entry("ide", NULL);