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 ide_imodel_proc_show(struct seq_file
*m
, void *v
)
35 ide_hwif_t
*hwif
= (ide_hwif_t
*) m
->private;
38 switch (hwif
->chipset
) {
39 case ide_generic
: name
= "generic"; break;
40 case ide_pci
: name
= "pci"; break;
41 case ide_cmd640
: name
= "cmd640"; break;
42 case ide_dtc2278
: name
= "dtc2278"; break;
43 case ide_ali14xx
: name
= "ali14xx"; break;
44 case ide_qd65xx
: name
= "qd65xx"; break;
45 case ide_umc8672
: name
= "umc8672"; break;
46 case ide_ht6560b
: name
= "ht6560b"; break;
47 case ide_4drives
: name
= "4drives"; break;
48 case ide_pmac
: name
= "mac-io"; break;
49 case ide_au1xxx
: name
= "au1xxx"; break;
50 case ide_palm3710
: name
= "palm3710"; break;
51 case ide_acorn
: name
= "acorn"; break;
52 default: name
= "(unknown)"; break;
54 seq_printf(m
, "%s\n", name
);
58 static int ide_imodel_proc_open(struct inode
*inode
, struct file
*file
)
60 return single_open(file
, ide_imodel_proc_show
, PDE(inode
)->data
);
63 static const struct file_operations ide_imodel_proc_fops
= {
65 .open
= ide_imodel_proc_open
,
68 .release
= single_release
,
71 static int ide_mate_proc_show(struct seq_file
*m
, void *v
)
73 ide_hwif_t
*hwif
= (ide_hwif_t
*) m
->private;
75 if (hwif
&& hwif
->mate
)
76 seq_printf(m
, "%s\n", hwif
->mate
->name
);
78 seq_printf(m
, "(none)\n");
82 static int ide_mate_proc_open(struct inode
*inode
, struct file
*file
)
84 return single_open(file
, ide_mate_proc_show
, PDE(inode
)->data
);
87 static const struct file_operations ide_mate_proc_fops
= {
89 .open
= ide_mate_proc_open
,
92 .release
= single_release
,
95 static int ide_channel_proc_show(struct seq_file
*m
, void *v
)
97 ide_hwif_t
*hwif
= (ide_hwif_t
*) m
->private;
99 seq_printf(m
, "%c\n", hwif
->channel
? '1' : '0');
103 static int ide_channel_proc_open(struct inode
*inode
, struct file
*file
)
105 return single_open(file
, ide_channel_proc_show
, PDE(inode
)->data
);
108 static const struct file_operations ide_channel_proc_fops
= {
109 .owner
= THIS_MODULE
,
110 .open
= ide_channel_proc_open
,
113 .release
= single_release
,
116 static int ide_identify_proc_show(struct seq_file
*m
, void *v
)
118 ide_drive_t
*drive
= (ide_drive_t
*)m
->private;
126 buf
= kmalloc(SECTOR_SIZE
, GFP_KERNEL
);
129 if (taskfile_lib_get_identify(drive
, buf
) == 0) {
130 __le16
*val
= (__le16
*)buf
;
133 for (i
= 0; i
< SECTOR_SIZE
/ 2; i
++) {
134 seq_printf(m
, "%04x%c", le16_to_cpu(val
[i
]),
135 (i
% 8) == 7 ? '\n' : ' ');
143 static int ide_identify_proc_open(struct inode
*inode
, struct file
*file
)
145 return single_open(file
, ide_identify_proc_show
, PDE(inode
)->data
);
148 static const struct file_operations ide_identify_proc_fops
= {
149 .owner
= THIS_MODULE
,
150 .open
= ide_identify_proc_open
,
153 .release
= single_release
,
157 * ide_find_setting - find a specific setting
158 * @st: setting table pointer
159 * @name: setting name
161 * Scan's the setting table for a matching entry and returns
162 * this or NULL if no entry is found. The caller must hold the
167 const struct ide_proc_devset
*ide_find_setting(const struct ide_proc_devset
*st
,
171 if (strcmp(st
->name
, name
) == 0)
175 return st
->name
? st
: NULL
;
179 * ide_read_setting - read an IDE setting
180 * @drive: drive to read from
181 * @setting: drive setting
183 * Read a drive setting and return the value. The caller
184 * must hold the ide_setting_mtx when making this call.
186 * BUGS: the data return and error are the same return value
187 * so an error -EINVAL and true return of the same value cannot
191 static int ide_read_setting(ide_drive_t
*drive
,
192 const struct ide_proc_devset
*setting
)
194 const struct ide_devset
*ds
= setting
->setting
;
198 val
= ds
->get(drive
);
204 * ide_write_setting - read an IDE setting
205 * @drive: drive to read from
206 * @setting: drive setting
209 * Write a drive setting if it is possible. The caller
210 * must hold the ide_setting_mtx when making this call.
212 * BUGS: the data return and error are the same return value
213 * so an error -EINVAL and true return of the same value cannot
216 * FIXME: This should be changed to enqueue a special request
217 * to the driver to change settings, and then wait on a sema for completion.
218 * The current scheme of polling is kludgy, though safe enough.
221 static int ide_write_setting(ide_drive_t
*drive
,
222 const struct ide_proc_devset
*setting
, int val
)
224 const struct ide_devset
*ds
= setting
->setting
;
226 if (!capable(CAP_SYS_ADMIN
))
230 if ((ds
->flags
& DS_SYNC
)
231 && (val
< setting
->min
|| val
> setting
->max
))
233 return ide_devset_execute(drive
, ds
, val
);
236 ide_devset_get(xfer_rate
, current_speed
);
238 static int set_xfer_rate (ide_drive_t
*drive
, int arg
)
242 if (arg
< XFER_PIO_0
|| arg
> XFER_UDMA_6
)
245 memset(&cmd
, 0, sizeof(cmd
));
246 cmd
.tf
.command
= ATA_CMD_SET_FEATURES
;
247 cmd
.tf
.feature
= SETFEATURES_XFER
;
248 cmd
.tf
.nsect
= (u8
)arg
;
249 cmd
.valid
.out
.tf
= IDE_VALID_FEATURE
| IDE_VALID_NSECT
;
250 cmd
.valid
.in
.tf
= IDE_VALID_NSECT
;
251 cmd
.tf_flags
= IDE_TFLAG_SET_XFER
;
253 return ide_no_data_taskfile(drive
, &cmd
);
256 ide_devset_rw(current_speed
, xfer_rate
);
257 ide_devset_rw_field(init_speed
, init_speed
);
258 ide_devset_rw_flag(nice1
, IDE_DFLAG_NICE1
);
259 ide_devset_rw_field(number
, dn
);
261 static const struct ide_proc_devset ide_generic_settings
[] = {
262 IDE_PROC_DEVSET(current_speed
, 0, 70),
263 IDE_PROC_DEVSET(init_speed
, 0, 70),
264 IDE_PROC_DEVSET(io_32bit
, 0, 1 + (SUPPORT_VLB_SYNC
<< 1)),
265 IDE_PROC_DEVSET(keepsettings
, 0, 1),
266 IDE_PROC_DEVSET(nice1
, 0, 1),
267 IDE_PROC_DEVSET(number
, 0, 3),
268 IDE_PROC_DEVSET(pio_mode
, 0, 255),
269 IDE_PROC_DEVSET(unmaskirq
, 0, 1),
270 IDE_PROC_DEVSET(using_dma
, 0, 1),
274 static void proc_ide_settings_warn(void)
281 printk(KERN_WARNING
"Warning: /proc/ide/hd?/settings interface is "
282 "obsolete, and will be removed soon!\n");
286 static int ide_settings_proc_show(struct seq_file
*m
, void *v
)
288 const struct ide_proc_devset
*setting
, *g
, *d
;
289 const struct ide_devset
*ds
;
290 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
291 int rc
, mul_factor
, div_factor
;
293 proc_ide_settings_warn();
295 mutex_lock(&ide_setting_mtx
);
296 g
= ide_generic_settings
;
298 seq_printf(m
, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
299 seq_printf(m
, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
300 while (g
->name
|| (d
&& d
->name
)) {
301 /* read settings in the alphabetical order */
302 if (g
->name
&& d
&& d
->name
) {
303 if (strcmp(d
->name
, g
->name
) < 0)
307 } else if (d
&& d
->name
) {
311 mul_factor
= setting
->mulf
? setting
->mulf(drive
) : 1;
312 div_factor
= setting
->divf
? setting
->divf(drive
) : 1;
313 seq_printf(m
, "%-24s", setting
->name
);
314 rc
= ide_read_setting(drive
, setting
);
316 seq_printf(m
, "%-16d", rc
* mul_factor
/ div_factor
);
318 seq_printf(m
, "%-16s", "write-only");
319 seq_printf(m
, "%-16d%-16d", (setting
->min
* mul_factor
+ div_factor
- 1) / div_factor
, setting
->max
* mul_factor
/ div_factor
);
320 ds
= setting
->setting
;
327 mutex_unlock(&ide_setting_mtx
);
331 static int ide_settings_proc_open(struct inode
*inode
, struct file
*file
)
333 return single_open(file
, ide_settings_proc_show
, PDE(inode
)->data
);
338 static ssize_t
ide_settings_proc_write(struct file
*file
, const char __user
*buffer
,
339 size_t count
, loff_t
*pos
)
341 ide_drive_t
*drive
= (ide_drive_t
*) PDE(file
->f_path
.dentry
->d_inode
)->data
;
342 char name
[MAX_LEN
+ 1];
343 int for_real
= 0, mul_factor
, div_factor
;
346 const struct ide_proc_devset
*setting
;
349 if (!capable(CAP_SYS_ADMIN
))
352 proc_ide_settings_warn();
354 if (count
>= PAGE_SIZE
)
357 s
= buf
= (char *)__get_free_page(GFP_USER
);
361 if (copy_from_user(buf
, buffer
, count
)) {
362 free_page((unsigned long)buf
);
369 * Skip over leading whitespace
371 while (count
&& isspace(*s
)) {
376 * Do one full pass to verify all parameters,
377 * then do another to actually write the new settings.
386 while (n
> 0 && *p
!= ':') {
394 memcpy(name
, q
, p
- q
);
403 val
= simple_strtoul(p
, &q
, 10);
406 if (n
> 0 && !isspace(*p
))
408 while (n
> 0 && isspace(*p
)) {
413 mutex_lock(&ide_setting_mtx
);
414 /* generic settings first, then driver specific ones */
415 setting
= ide_find_setting(ide_generic_settings
, name
);
418 setting
= ide_find_setting(drive
->settings
, name
);
420 mutex_unlock(&ide_setting_mtx
);
425 mul_factor
= setting
->mulf
? setting
->mulf(drive
) : 1;
426 div_factor
= setting
->divf
? setting
->divf(drive
) : 1;
427 ide_write_setting(drive
, setting
, val
* div_factor
/ mul_factor
);
429 mutex_unlock(&ide_setting_mtx
);
431 } while (!for_real
++);
432 free_page((unsigned long)buf
);
435 free_page((unsigned long)buf
);
436 printk("%s(): parse error\n", __func__
);
440 static const struct file_operations ide_settings_proc_fops
= {
441 .owner
= THIS_MODULE
,
442 .open
= ide_settings_proc_open
,
445 .release
= single_release
,
446 .write
= ide_settings_proc_write
,
449 static int ide_capacity_proc_show(struct seq_file
*m
, void *v
)
451 seq_printf(m
, "%llu\n", (long long)0x7fffffff);
455 static int ide_capacity_proc_open(struct inode
*inode
, struct file
*file
)
457 return single_open(file
, ide_capacity_proc_show
, NULL
);
460 const struct file_operations ide_capacity_proc_fops
= {
461 .owner
= THIS_MODULE
,
462 .open
= ide_capacity_proc_open
,
465 .release
= single_release
,
467 EXPORT_SYMBOL_GPL(ide_capacity_proc_fops
);
469 static int ide_geometry_proc_show(struct seq_file
*m
, void *v
)
471 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
473 seq_printf(m
, "physical %d/%d/%d\n",
474 drive
->cyl
, drive
->head
, drive
->sect
);
475 seq_printf(m
, "logical %d/%d/%d\n",
476 drive
->bios_cyl
, drive
->bios_head
, drive
->bios_sect
);
480 static int ide_geometry_proc_open(struct inode
*inode
, struct file
*file
)
482 return single_open(file
, ide_geometry_proc_show
, PDE(inode
)->data
);
485 const struct file_operations ide_geometry_proc_fops
= {
486 .owner
= THIS_MODULE
,
487 .open
= ide_geometry_proc_open
,
490 .release
= single_release
,
492 EXPORT_SYMBOL(ide_geometry_proc_fops
);
494 static int ide_dmodel_proc_show(struct seq_file
*seq
, void *v
)
496 ide_drive_t
*drive
= (ide_drive_t
*) seq
->private;
497 char *m
= (char *)&drive
->id
[ATA_ID_PROD
];
499 seq_printf(seq
, "%.40s\n", m
[0] ? m
: "(none)");
503 static int ide_dmodel_proc_open(struct inode
*inode
, struct file
*file
)
505 return single_open(file
, ide_dmodel_proc_show
, PDE(inode
)->data
);
508 static const struct file_operations ide_dmodel_proc_fops
= {
509 .owner
= THIS_MODULE
,
510 .open
= ide_dmodel_proc_open
,
513 .release
= single_release
,
516 static int ide_driver_proc_show(struct seq_file
*m
, void *v
)
518 ide_drive_t
*drive
= (ide_drive_t
*)m
->private;
519 struct device
*dev
= &drive
->gendev
;
520 struct ide_driver
*ide_drv
;
523 ide_drv
= to_ide_driver(dev
->driver
);
524 seq_printf(m
, "%s version %s\n",
525 dev
->driver
->name
, ide_drv
->version
);
527 seq_printf(m
, "ide-default version 0.9.newide\n");
531 static int ide_driver_proc_open(struct inode
*inode
, struct file
*file
)
533 return single_open(file
, ide_driver_proc_show
, PDE(inode
)->data
);
536 static int ide_replace_subdriver(ide_drive_t
*drive
, const char *driver
)
538 struct device
*dev
= &drive
->gendev
;
542 device_release_driver(dev
);
543 /* FIXME: device can still be in use by previous driver */
544 strlcpy(drive
->driver_req
, driver
, sizeof(drive
->driver_req
));
545 err
= device_attach(dev
);
547 printk(KERN_WARNING
"IDE: %s: device_attach error: %d\n",
549 drive
->driver_req
[0] = 0;
550 if (dev
->driver
== NULL
) {
551 err
= device_attach(dev
);
554 "IDE: %s: device_attach(2) error: %d\n",
557 if (dev
->driver
&& !strcmp(dev
->driver
->name
, driver
))
563 static ssize_t
ide_driver_proc_write(struct file
*file
, const char __user
*buffer
,
564 size_t count
, loff_t
*pos
)
566 ide_drive_t
*drive
= (ide_drive_t
*) PDE(file
->f_path
.dentry
->d_inode
)->data
;
569 if (!capable(CAP_SYS_ADMIN
))
573 if (copy_from_user(name
, buffer
, count
))
576 if (ide_replace_subdriver(drive
, name
))
581 static const struct file_operations ide_driver_proc_fops
= {
582 .owner
= THIS_MODULE
,
583 .open
= ide_driver_proc_open
,
586 .release
= single_release
,
587 .write
= ide_driver_proc_write
,
590 static int ide_media_proc_show(struct seq_file
*m
, void *v
)
592 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
595 switch (drive
->media
) {
596 case ide_disk
: media
= "disk\n"; break;
597 case ide_cdrom
: media
= "cdrom\n"; break;
598 case ide_tape
: media
= "tape\n"; break;
599 case ide_floppy
: media
= "floppy\n"; break;
600 case ide_optical
: media
= "optical\n"; break;
601 default: media
= "UNKNOWN\n"; break;
607 static int ide_media_proc_open(struct inode
*inode
, struct file
*file
)
609 return single_open(file
, ide_media_proc_show
, PDE(inode
)->data
);
612 static const struct file_operations ide_media_proc_fops
= {
613 .owner
= THIS_MODULE
,
614 .open
= ide_media_proc_open
,
617 .release
= single_release
,
620 static ide_proc_entry_t generic_drive_entries
[] = {
621 { "driver", S_IFREG
|S_IRUGO
, &ide_driver_proc_fops
},
622 { "identify", S_IFREG
|S_IRUSR
, &ide_identify_proc_fops
},
623 { "media", S_IFREG
|S_IRUGO
, &ide_media_proc_fops
},
624 { "model", S_IFREG
|S_IRUGO
, &ide_dmodel_proc_fops
},
625 { "settings", S_IFREG
|S_IRUSR
|S_IWUSR
, &ide_settings_proc_fops
},
629 static void ide_add_proc_entries(struct proc_dir_entry
*dir
, ide_proc_entry_t
*p
, void *data
)
631 struct proc_dir_entry
*ent
;
635 while (p
->name
!= NULL
) {
636 ent
= proc_create_data(p
->name
, p
->mode
, dir
, p
->proc_fops
, data
);
642 static void ide_remove_proc_entries(struct proc_dir_entry
*dir
, ide_proc_entry_t
*p
)
646 while (p
->name
!= NULL
) {
647 remove_proc_entry(p
->name
, dir
);
652 void ide_proc_register_driver(ide_drive_t
*drive
, struct ide_driver
*driver
)
654 mutex_lock(&ide_setting_mtx
);
655 drive
->settings
= driver
->proc_devsets(drive
);
656 mutex_unlock(&ide_setting_mtx
);
658 ide_add_proc_entries(drive
->proc
, driver
->proc_entries(drive
), drive
);
661 EXPORT_SYMBOL(ide_proc_register_driver
);
664 * ide_proc_unregister_driver - remove driver specific data
668 * Clean up the driver specific /proc files and IDE settings
671 * Takes ide_setting_mtx.
674 void ide_proc_unregister_driver(ide_drive_t
*drive
, struct ide_driver
*driver
)
676 ide_remove_proc_entries(drive
->proc
, driver
->proc_entries(drive
));
678 mutex_lock(&ide_setting_mtx
);
680 * ide_setting_mtx protects both the settings list and the use
681 * of settings (we cannot take a setting out that is being used).
683 drive
->settings
= NULL
;
684 mutex_unlock(&ide_setting_mtx
);
686 EXPORT_SYMBOL(ide_proc_unregister_driver
);
688 void ide_proc_port_register_devices(ide_hwif_t
*hwif
)
690 struct proc_dir_entry
*ent
;
691 struct proc_dir_entry
*parent
= hwif
->proc
;
696 ide_port_for_each_dev(i
, drive
, hwif
) {
697 if ((drive
->dev_flags
& IDE_DFLAG_PRESENT
) == 0)
700 drive
->proc
= proc_mkdir(drive
->name
, parent
);
702 ide_add_proc_entries(drive
->proc
, generic_drive_entries
, drive
);
703 sprintf(name
, "ide%d/%s", (drive
->name
[2]-'a')/2, drive
->name
);
704 ent
= proc_symlink(drive
->name
, proc_ide_root
, name
);
709 void ide_proc_unregister_device(ide_drive_t
*drive
)
712 ide_remove_proc_entries(drive
->proc
, generic_drive_entries
);
713 remove_proc_entry(drive
->name
, proc_ide_root
);
714 remove_proc_entry(drive
->name
, drive
->hwif
->proc
);
719 static ide_proc_entry_t hwif_entries
[] = {
720 { "channel", S_IFREG
|S_IRUGO
, &ide_channel_proc_fops
},
721 { "mate", S_IFREG
|S_IRUGO
, &ide_mate_proc_fops
},
722 { "model", S_IFREG
|S_IRUGO
, &ide_imodel_proc_fops
},
726 void ide_proc_register_port(ide_hwif_t
*hwif
)
729 hwif
->proc
= proc_mkdir(hwif
->name
, proc_ide_root
);
734 ide_add_proc_entries(hwif
->proc
, hwif_entries
, hwif
);
738 void ide_proc_unregister_port(ide_hwif_t
*hwif
)
741 ide_remove_proc_entries(hwif
->proc
, hwif_entries
);
742 remove_proc_entry(hwif
->name
, proc_ide_root
);
747 static int proc_print_driver(struct device_driver
*drv
, void *data
)
749 struct ide_driver
*ide_drv
= to_ide_driver(drv
);
750 struct seq_file
*s
= data
;
752 seq_printf(s
, "%s version %s\n", drv
->name
, ide_drv
->version
);
757 static int ide_drivers_show(struct seq_file
*s
, void *p
)
761 err
= bus_for_each_drv(&ide_bus_type
, NULL
, s
, proc_print_driver
);
763 printk(KERN_WARNING
"IDE: %s: bus_for_each_drv error: %d\n",
768 static int ide_drivers_open(struct inode
*inode
, struct file
*file
)
770 return single_open(file
, &ide_drivers_show
, NULL
);
773 static const struct file_operations ide_drivers_operations
= {
774 .owner
= THIS_MODULE
,
775 .open
= ide_drivers_open
,
778 .release
= single_release
,
781 void proc_ide_create(void)
783 proc_ide_root
= proc_mkdir("ide", NULL
);
788 proc_create("drivers", 0, proc_ide_root
, &ide_drivers_operations
);
791 void proc_ide_destroy(void)
793 remove_proc_entry("drivers", proc_ide_root
);
794 remove_proc_entry("ide", NULL
);