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>
28 #include <linux/slab.h>
32 static struct proc_dir_entry
*proc_ide_root
;
34 static int ide_imodel_proc_show(struct seq_file
*m
, void *v
)
36 ide_hwif_t
*hwif
= (ide_hwif_t
*) m
->private;
39 switch (hwif
->chipset
) {
40 case ide_generic
: name
= "generic"; break;
41 case ide_pci
: name
= "pci"; break;
42 case ide_cmd640
: name
= "cmd640"; break;
43 case ide_dtc2278
: name
= "dtc2278"; break;
44 case ide_ali14xx
: name
= "ali14xx"; break;
45 case ide_qd65xx
: name
= "qd65xx"; break;
46 case ide_umc8672
: name
= "umc8672"; break;
47 case ide_ht6560b
: name
= "ht6560b"; break;
48 case ide_4drives
: name
= "4drives"; break;
49 case ide_pmac
: name
= "mac-io"; break;
50 case ide_au1xxx
: name
= "au1xxx"; break;
51 case ide_palm3710
: name
= "palm3710"; break;
52 case ide_acorn
: name
= "acorn"; break;
53 default: name
= "(unknown)"; break;
55 seq_printf(m
, "%s\n", name
);
59 static int ide_imodel_proc_open(struct inode
*inode
, struct file
*file
)
61 return single_open(file
, ide_imodel_proc_show
, PDE(inode
)->data
);
64 static const struct file_operations ide_imodel_proc_fops
= {
66 .open
= ide_imodel_proc_open
,
69 .release
= single_release
,
72 static int ide_mate_proc_show(struct seq_file
*m
, void *v
)
74 ide_hwif_t
*hwif
= (ide_hwif_t
*) m
->private;
76 if (hwif
&& hwif
->mate
)
77 seq_printf(m
, "%s\n", hwif
->mate
->name
);
79 seq_printf(m
, "(none)\n");
83 static int ide_mate_proc_open(struct inode
*inode
, struct file
*file
)
85 return single_open(file
, ide_mate_proc_show
, PDE(inode
)->data
);
88 static const struct file_operations ide_mate_proc_fops
= {
90 .open
= ide_mate_proc_open
,
93 .release
= single_release
,
96 static int ide_channel_proc_show(struct seq_file
*m
, void *v
)
98 ide_hwif_t
*hwif
= (ide_hwif_t
*) m
->private;
100 seq_printf(m
, "%c\n", hwif
->channel
? '1' : '0');
104 static int ide_channel_proc_open(struct inode
*inode
, struct file
*file
)
106 return single_open(file
, ide_channel_proc_show
, PDE(inode
)->data
);
109 static const struct file_operations ide_channel_proc_fops
= {
110 .owner
= THIS_MODULE
,
111 .open
= ide_channel_proc_open
,
114 .release
= single_release
,
117 static int ide_identify_proc_show(struct seq_file
*m
, void *v
)
119 ide_drive_t
*drive
= (ide_drive_t
*)m
->private;
127 buf
= kmalloc(SECTOR_SIZE
, GFP_KERNEL
);
130 if (taskfile_lib_get_identify(drive
, buf
) == 0) {
131 __le16
*val
= (__le16
*)buf
;
134 for (i
= 0; i
< SECTOR_SIZE
/ 2; i
++) {
135 seq_printf(m
, "%04x%c", le16_to_cpu(val
[i
]),
136 (i
% 8) == 7 ? '\n' : ' ');
144 static int ide_identify_proc_open(struct inode
*inode
, struct file
*file
)
146 return single_open(file
, ide_identify_proc_show
, PDE(inode
)->data
);
149 static const struct file_operations ide_identify_proc_fops
= {
150 .owner
= THIS_MODULE
,
151 .open
= ide_identify_proc_open
,
154 .release
= single_release
,
158 * ide_find_setting - find a specific setting
159 * @st: setting table pointer
160 * @name: setting name
162 * Scan's the setting table for a matching entry and returns
163 * this or NULL if no entry is found. The caller must hold the
168 const struct ide_proc_devset
*ide_find_setting(const struct ide_proc_devset
*st
,
172 if (strcmp(st
->name
, name
) == 0)
176 return st
->name
? st
: NULL
;
180 * ide_read_setting - read an IDE setting
181 * @drive: drive to read from
182 * @setting: drive setting
184 * Read a drive setting and return the value. The caller
185 * must hold the ide_setting_mtx when making this call.
187 * BUGS: the data return and error are the same return value
188 * so an error -EINVAL and true return of the same value cannot
192 static int ide_read_setting(ide_drive_t
*drive
,
193 const struct ide_proc_devset
*setting
)
195 const struct ide_devset
*ds
= setting
->setting
;
199 val
= ds
->get(drive
);
205 static int ide_write_setting(ide_drive_t
*drive
,
206 const struct ide_proc_devset
*setting
, int val
)
208 const struct ide_devset
*ds
= setting
->setting
;
210 if (!capable(CAP_SYS_ADMIN
))
214 if ((ds
->flags
& DS_SYNC
)
215 && (val
< setting
->min
|| val
> setting
->max
))
217 return ide_devset_execute(drive
, ds
, val
);
220 ide_devset_get(xfer_rate
, current_speed
);
222 static int set_xfer_rate (ide_drive_t
*drive
, int arg
)
226 if (arg
< XFER_PIO_0
|| arg
> XFER_UDMA_6
)
229 memset(&cmd
, 0, sizeof(cmd
));
230 cmd
.tf
.command
= ATA_CMD_SET_FEATURES
;
231 cmd
.tf
.feature
= SETFEATURES_XFER
;
232 cmd
.tf
.nsect
= (u8
)arg
;
233 cmd
.valid
.out
.tf
= IDE_VALID_FEATURE
| IDE_VALID_NSECT
;
234 cmd
.valid
.in
.tf
= IDE_VALID_NSECT
;
235 cmd
.tf_flags
= IDE_TFLAG_SET_XFER
;
237 return ide_no_data_taskfile(drive
, &cmd
);
240 ide_devset_rw(current_speed
, xfer_rate
);
241 ide_devset_rw_field(init_speed
, init_speed
);
242 ide_devset_rw_flag(nice1
, IDE_DFLAG_NICE1
);
243 ide_devset_rw_field(number
, dn
);
245 static const struct ide_proc_devset ide_generic_settings
[] = {
246 IDE_PROC_DEVSET(current_speed
, 0, 70),
247 IDE_PROC_DEVSET(init_speed
, 0, 70),
248 IDE_PROC_DEVSET(io_32bit
, 0, 1 + (SUPPORT_VLB_SYNC
<< 1)),
249 IDE_PROC_DEVSET(keepsettings
, 0, 1),
250 IDE_PROC_DEVSET(nice1
, 0, 1),
251 IDE_PROC_DEVSET(number
, 0, 3),
252 IDE_PROC_DEVSET(pio_mode
, 0, 255),
253 IDE_PROC_DEVSET(unmaskirq
, 0, 1),
254 IDE_PROC_DEVSET(using_dma
, 0, 1),
258 static void proc_ide_settings_warn(void)
260 printk_once(KERN_WARNING
"Warning: /proc/ide/hd?/settings interface is "
261 "obsolete, and will be removed soon!\n");
264 static int ide_settings_proc_show(struct seq_file
*m
, void *v
)
266 const struct ide_proc_devset
*setting
, *g
, *d
;
267 const struct ide_devset
*ds
;
268 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
269 int rc
, mul_factor
, div_factor
;
271 proc_ide_settings_warn();
273 mutex_lock(&ide_setting_mtx
);
274 g
= ide_generic_settings
;
276 seq_printf(m
, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
277 seq_printf(m
, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
278 while (g
->name
|| (d
&& d
->name
)) {
279 /* read settings in the alphabetical order */
280 if (g
->name
&& d
&& d
->name
) {
281 if (strcmp(d
->name
, g
->name
) < 0)
285 } else if (d
&& d
->name
) {
289 mul_factor
= setting
->mulf
? setting
->mulf(drive
) : 1;
290 div_factor
= setting
->divf
? setting
->divf(drive
) : 1;
291 seq_printf(m
, "%-24s", setting
->name
);
292 rc
= ide_read_setting(drive
, setting
);
294 seq_printf(m
, "%-16d", rc
* mul_factor
/ div_factor
);
296 seq_printf(m
, "%-16s", "write-only");
297 seq_printf(m
, "%-16d%-16d", (setting
->min
* mul_factor
+ div_factor
- 1) / div_factor
, setting
->max
* mul_factor
/ div_factor
);
298 ds
= setting
->setting
;
305 mutex_unlock(&ide_setting_mtx
);
309 static int ide_settings_proc_open(struct inode
*inode
, struct file
*file
)
311 return single_open(file
, ide_settings_proc_show
, PDE(inode
)->data
);
316 static ssize_t
ide_settings_proc_write(struct file
*file
, const char __user
*buffer
,
317 size_t count
, loff_t
*pos
)
319 ide_drive_t
*drive
= (ide_drive_t
*) PDE(file
->f_path
.dentry
->d_inode
)->data
;
320 char name
[MAX_LEN
+ 1];
321 int for_real
= 0, mul_factor
, div_factor
;
324 const struct ide_proc_devset
*setting
;
327 if (!capable(CAP_SYS_ADMIN
))
330 proc_ide_settings_warn();
332 if (count
>= PAGE_SIZE
)
335 s
= buf
= (char *)__get_free_page(GFP_USER
);
339 if (copy_from_user(buf
, buffer
, count
)) {
340 free_page((unsigned long)buf
);
347 * Skip over leading whitespace
349 while (count
&& isspace(*s
)) {
354 * Do one full pass to verify all parameters,
355 * then do another to actually write the new settings.
364 while (n
> 0 && *p
!= ':') {
372 memcpy(name
, q
, p
- q
);
381 val
= simple_strtoul(p
, &q
, 10);
384 if (n
> 0 && !isspace(*p
))
386 while (n
> 0 && isspace(*p
)) {
391 mutex_lock(&ide_setting_mtx
);
392 /* generic settings first, then driver specific ones */
393 setting
= ide_find_setting(ide_generic_settings
, name
);
396 setting
= ide_find_setting(drive
->settings
, name
);
398 mutex_unlock(&ide_setting_mtx
);
403 mul_factor
= setting
->mulf
? setting
->mulf(drive
) : 1;
404 div_factor
= setting
->divf
? setting
->divf(drive
) : 1;
405 ide_write_setting(drive
, setting
, val
* div_factor
/ mul_factor
);
407 mutex_unlock(&ide_setting_mtx
);
409 } while (!for_real
++);
410 free_page((unsigned long)buf
);
413 free_page((unsigned long)buf
);
414 printk("%s(): parse error\n", __func__
);
418 static const struct file_operations ide_settings_proc_fops
= {
419 .owner
= THIS_MODULE
,
420 .open
= ide_settings_proc_open
,
423 .release
= single_release
,
424 .write
= ide_settings_proc_write
,
427 static int ide_capacity_proc_show(struct seq_file
*m
, void *v
)
429 seq_printf(m
, "%llu\n", (long long)0x7fffffff);
433 static int ide_capacity_proc_open(struct inode
*inode
, struct file
*file
)
435 return single_open(file
, ide_capacity_proc_show
, NULL
);
438 const struct file_operations ide_capacity_proc_fops
= {
439 .owner
= THIS_MODULE
,
440 .open
= ide_capacity_proc_open
,
443 .release
= single_release
,
445 EXPORT_SYMBOL_GPL(ide_capacity_proc_fops
);
447 static int ide_geometry_proc_show(struct seq_file
*m
, void *v
)
449 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
451 seq_printf(m
, "physical %d/%d/%d\n",
452 drive
->cyl
, drive
->head
, drive
->sect
);
453 seq_printf(m
, "logical %d/%d/%d\n",
454 drive
->bios_cyl
, drive
->bios_head
, drive
->bios_sect
);
458 static int ide_geometry_proc_open(struct inode
*inode
, struct file
*file
)
460 return single_open(file
, ide_geometry_proc_show
, PDE(inode
)->data
);
463 const struct file_operations ide_geometry_proc_fops
= {
464 .owner
= THIS_MODULE
,
465 .open
= ide_geometry_proc_open
,
468 .release
= single_release
,
470 EXPORT_SYMBOL(ide_geometry_proc_fops
);
472 static int ide_dmodel_proc_show(struct seq_file
*seq
, void *v
)
474 ide_drive_t
*drive
= (ide_drive_t
*) seq
->private;
475 char *m
= (char *)&drive
->id
[ATA_ID_PROD
];
477 seq_printf(seq
, "%.40s\n", m
[0] ? m
: "(none)");
481 static int ide_dmodel_proc_open(struct inode
*inode
, struct file
*file
)
483 return single_open(file
, ide_dmodel_proc_show
, PDE(inode
)->data
);
486 static const struct file_operations ide_dmodel_proc_fops
= {
487 .owner
= THIS_MODULE
,
488 .open
= ide_dmodel_proc_open
,
491 .release
= single_release
,
494 static int ide_driver_proc_show(struct seq_file
*m
, void *v
)
496 ide_drive_t
*drive
= (ide_drive_t
*)m
->private;
497 struct device
*dev
= &drive
->gendev
;
498 struct ide_driver
*ide_drv
;
501 ide_drv
= to_ide_driver(dev
->driver
);
502 seq_printf(m
, "%s version %s\n",
503 dev
->driver
->name
, ide_drv
->version
);
505 seq_printf(m
, "ide-default version 0.9.newide\n");
509 static int ide_driver_proc_open(struct inode
*inode
, struct file
*file
)
511 return single_open(file
, ide_driver_proc_show
, PDE(inode
)->data
);
514 static int ide_replace_subdriver(ide_drive_t
*drive
, const char *driver
)
516 struct device
*dev
= &drive
->gendev
;
520 device_release_driver(dev
);
521 strlcpy(drive
->driver_req
, driver
, sizeof(drive
->driver_req
));
522 err
= device_attach(dev
);
524 printk(KERN_WARNING
"IDE: %s: device_attach error: %d\n",
526 drive
->driver_req
[0] = 0;
527 if (dev
->driver
== NULL
) {
528 err
= device_attach(dev
);
531 "IDE: %s: device_attach(2) error: %d\n",
534 if (dev
->driver
&& !strcmp(dev
->driver
->name
, driver
))
540 static ssize_t
ide_driver_proc_write(struct file
*file
, const char __user
*buffer
,
541 size_t count
, loff_t
*pos
)
543 ide_drive_t
*drive
= (ide_drive_t
*) PDE(file
->f_path
.dentry
->d_inode
)->data
;
546 if (!capable(CAP_SYS_ADMIN
))
550 if (copy_from_user(name
, buffer
, count
))
553 if (ide_replace_subdriver(drive
, name
))
558 static const struct file_operations ide_driver_proc_fops
= {
559 .owner
= THIS_MODULE
,
560 .open
= ide_driver_proc_open
,
563 .release
= single_release
,
564 .write
= ide_driver_proc_write
,
567 static int ide_media_proc_show(struct seq_file
*m
, void *v
)
569 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
572 switch (drive
->media
) {
573 case ide_disk
: media
= "disk\n"; break;
574 case ide_cdrom
: media
= "cdrom\n"; break;
575 case ide_tape
: media
= "tape\n"; break;
576 case ide_floppy
: media
= "floppy\n"; break;
577 case ide_optical
: media
= "optical\n"; break;
578 default: media
= "UNKNOWN\n"; break;
584 static int ide_media_proc_open(struct inode
*inode
, struct file
*file
)
586 return single_open(file
, ide_media_proc_show
, PDE(inode
)->data
);
589 static const struct file_operations ide_media_proc_fops
= {
590 .owner
= THIS_MODULE
,
591 .open
= ide_media_proc_open
,
594 .release
= single_release
,
597 static ide_proc_entry_t generic_drive_entries
[] = {
598 { "driver", S_IFREG
|S_IRUGO
, &ide_driver_proc_fops
},
599 { "identify", S_IFREG
|S_IRUSR
, &ide_identify_proc_fops
},
600 { "media", S_IFREG
|S_IRUGO
, &ide_media_proc_fops
},
601 { "model", S_IFREG
|S_IRUGO
, &ide_dmodel_proc_fops
},
602 { "settings", S_IFREG
|S_IRUSR
|S_IWUSR
, &ide_settings_proc_fops
},
606 static void ide_add_proc_entries(struct proc_dir_entry
*dir
, ide_proc_entry_t
*p
, void *data
)
608 struct proc_dir_entry
*ent
;
612 while (p
->name
!= NULL
) {
613 ent
= proc_create_data(p
->name
, p
->mode
, dir
, p
->proc_fops
, data
);
619 static void ide_remove_proc_entries(struct proc_dir_entry
*dir
, ide_proc_entry_t
*p
)
623 while (p
->name
!= NULL
) {
624 remove_proc_entry(p
->name
, dir
);
629 void ide_proc_register_driver(ide_drive_t
*drive
, struct ide_driver
*driver
)
631 mutex_lock(&ide_setting_mtx
);
632 drive
->settings
= driver
->proc_devsets(drive
);
633 mutex_unlock(&ide_setting_mtx
);
635 ide_add_proc_entries(drive
->proc
, driver
->proc_entries(drive
), drive
);
638 EXPORT_SYMBOL(ide_proc_register_driver
);
641 * ide_proc_unregister_driver - remove driver specific data
645 * Clean up the driver specific /proc files and IDE settings
648 * Takes ide_setting_mtx.
651 void ide_proc_unregister_driver(ide_drive_t
*drive
, struct ide_driver
*driver
)
653 ide_remove_proc_entries(drive
->proc
, driver
->proc_entries(drive
));
655 mutex_lock(&ide_setting_mtx
);
657 * ide_setting_mtx protects both the settings list and the use
658 * of settings (we cannot take a setting out that is being used).
660 drive
->settings
= NULL
;
661 mutex_unlock(&ide_setting_mtx
);
663 EXPORT_SYMBOL(ide_proc_unregister_driver
);
665 void ide_proc_port_register_devices(ide_hwif_t
*hwif
)
667 struct proc_dir_entry
*ent
;
668 struct proc_dir_entry
*parent
= hwif
->proc
;
673 ide_port_for_each_dev(i
, drive
, hwif
) {
674 if ((drive
->dev_flags
& IDE_DFLAG_PRESENT
) == 0)
677 drive
->proc
= proc_mkdir(drive
->name
, parent
);
679 ide_add_proc_entries(drive
->proc
, generic_drive_entries
, drive
);
680 sprintf(name
, "ide%d/%s", (drive
->name
[2]-'a')/2, drive
->name
);
681 ent
= proc_symlink(drive
->name
, proc_ide_root
, name
);
686 void ide_proc_unregister_device(ide_drive_t
*drive
)
689 ide_remove_proc_entries(drive
->proc
, generic_drive_entries
);
690 remove_proc_entry(drive
->name
, proc_ide_root
);
691 remove_proc_entry(drive
->name
, drive
->hwif
->proc
);
696 static ide_proc_entry_t hwif_entries
[] = {
697 { "channel", S_IFREG
|S_IRUGO
, &ide_channel_proc_fops
},
698 { "mate", S_IFREG
|S_IRUGO
, &ide_mate_proc_fops
},
699 { "model", S_IFREG
|S_IRUGO
, &ide_imodel_proc_fops
},
703 void ide_proc_register_port(ide_hwif_t
*hwif
)
706 hwif
->proc
= proc_mkdir(hwif
->name
, proc_ide_root
);
711 ide_add_proc_entries(hwif
->proc
, hwif_entries
, hwif
);
715 void ide_proc_unregister_port(ide_hwif_t
*hwif
)
718 ide_remove_proc_entries(hwif
->proc
, hwif_entries
);
719 remove_proc_entry(hwif
->name
, proc_ide_root
);
724 static int proc_print_driver(struct device_driver
*drv
, void *data
)
726 struct ide_driver
*ide_drv
= to_ide_driver(drv
);
727 struct seq_file
*s
= data
;
729 seq_printf(s
, "%s version %s\n", drv
->name
, ide_drv
->version
);
734 static int ide_drivers_show(struct seq_file
*s
, void *p
)
738 err
= bus_for_each_drv(&ide_bus_type
, NULL
, s
, proc_print_driver
);
740 printk(KERN_WARNING
"IDE: %s: bus_for_each_drv error: %d\n",
745 static int ide_drivers_open(struct inode
*inode
, struct file
*file
)
747 return single_open(file
, &ide_drivers_show
, NULL
);
750 static const struct file_operations ide_drivers_operations
= {
751 .owner
= THIS_MODULE
,
752 .open
= ide_drivers_open
,
755 .release
= single_release
,
758 void proc_ide_create(void)
760 proc_ide_root
= proc_mkdir("ide", NULL
);
765 proc_create("drivers", 0, proc_ide_root
, &ide_drivers_operations
);
768 void proc_ide_destroy(void)
770 remove_proc_entry("drivers", proc_ide_root
);
771 remove_proc_entry("ide", NULL
);