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 <linux/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_mate_proc_show(struct seq_file
*m
, void *v
)
61 ide_hwif_t
*hwif
= (ide_hwif_t
*) m
->private;
63 if (hwif
&& hwif
->mate
)
64 seq_printf(m
, "%s\n", hwif
->mate
->name
);
66 seq_printf(m
, "(none)\n");
70 static int ide_channel_proc_show(struct seq_file
*m
, void *v
)
72 ide_hwif_t
*hwif
= (ide_hwif_t
*) m
->private;
74 seq_printf(m
, "%c\n", hwif
->channel
? '1' : '0');
78 static int ide_identify_proc_show(struct seq_file
*m
, void *v
)
80 ide_drive_t
*drive
= (ide_drive_t
*)m
->private;
88 buf
= kmalloc(SECTOR_SIZE
, GFP_KERNEL
);
91 if (taskfile_lib_get_identify(drive
, buf
) == 0) {
92 __le16
*val
= (__le16
*)buf
;
95 for (i
= 0; i
< SECTOR_SIZE
/ 2; i
++) {
96 seq_printf(m
, "%04x%c", le16_to_cpu(val
[i
]),
97 (i
% 8) == 7 ? '\n' : ' ');
106 * ide_find_setting - find a specific setting
107 * @st: setting table pointer
108 * @name: setting name
110 * Scan's the setting table for a matching entry and returns
111 * this or NULL if no entry is found. The caller must hold the
116 const struct ide_proc_devset
*ide_find_setting(const struct ide_proc_devset
*st
,
120 if (strcmp(st
->name
, name
) == 0)
124 return st
->name
? st
: NULL
;
128 * ide_read_setting - read an IDE setting
129 * @drive: drive to read from
130 * @setting: drive setting
132 * Read a drive setting and return the value. The caller
133 * must hold the ide_setting_mtx when making this call.
135 * BUGS: the data return and error are the same return value
136 * so an error -EINVAL and true return of the same value cannot
140 static int ide_read_setting(ide_drive_t
*drive
,
141 const struct ide_proc_devset
*setting
)
143 const struct ide_devset
*ds
= setting
->setting
;
147 val
= ds
->get(drive
);
153 * ide_write_setting - read an IDE setting
154 * @drive: drive to read from
155 * @setting: drive setting
158 * Write a drive setting if it is possible. The caller
159 * must hold the ide_setting_mtx when making this call.
161 * BUGS: the data return and error are the same return value
162 * so an error -EINVAL and true return of the same value cannot
165 * FIXME: This should be changed to enqueue a special request
166 * to the driver to change settings, and then wait on a sema for completion.
167 * The current scheme of polling is kludgy, though safe enough.
170 static int ide_write_setting(ide_drive_t
*drive
,
171 const struct ide_proc_devset
*setting
, int val
)
173 const struct ide_devset
*ds
= setting
->setting
;
175 if (!capable(CAP_SYS_ADMIN
))
179 if ((ds
->flags
& DS_SYNC
)
180 && (val
< setting
->min
|| val
> setting
->max
))
182 return ide_devset_execute(drive
, ds
, val
);
185 ide_devset_get(xfer_rate
, current_speed
);
187 static int set_xfer_rate (ide_drive_t
*drive
, int arg
)
191 if (arg
< XFER_PIO_0
|| arg
> XFER_UDMA_6
)
194 memset(&cmd
, 0, sizeof(cmd
));
195 cmd
.tf
.command
= ATA_CMD_SET_FEATURES
;
196 cmd
.tf
.feature
= SETFEATURES_XFER
;
197 cmd
.tf
.nsect
= (u8
)arg
;
198 cmd
.valid
.out
.tf
= IDE_VALID_FEATURE
| IDE_VALID_NSECT
;
199 cmd
.valid
.in
.tf
= IDE_VALID_NSECT
;
200 cmd
.tf_flags
= IDE_TFLAG_SET_XFER
;
202 return ide_no_data_taskfile(drive
, &cmd
);
205 ide_devset_rw(current_speed
, xfer_rate
);
206 ide_devset_rw_field(init_speed
, init_speed
);
207 ide_devset_rw_flag(nice1
, IDE_DFLAG_NICE1
);
208 ide_devset_rw_field(number
, dn
);
210 static const struct ide_proc_devset ide_generic_settings
[] = {
211 IDE_PROC_DEVSET(current_speed
, 0, 70),
212 IDE_PROC_DEVSET(init_speed
, 0, 70),
213 IDE_PROC_DEVSET(io_32bit
, 0, 1 + (SUPPORT_VLB_SYNC
<< 1)),
214 IDE_PROC_DEVSET(keepsettings
, 0, 1),
215 IDE_PROC_DEVSET(nice1
, 0, 1),
216 IDE_PROC_DEVSET(number
, 0, 3),
217 IDE_PROC_DEVSET(pio_mode
, 0, 255),
218 IDE_PROC_DEVSET(unmaskirq
, 0, 1),
219 IDE_PROC_DEVSET(using_dma
, 0, 1),
223 static void proc_ide_settings_warn(void)
225 printk_once(KERN_WARNING
"Warning: /proc/ide/hd?/settings interface is "
226 "obsolete, and will be removed soon!\n");
229 static int ide_settings_proc_show(struct seq_file
*m
, void *v
)
231 const struct ide_proc_devset
*setting
, *g
, *d
;
232 const struct ide_devset
*ds
;
233 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
234 int rc
, mul_factor
, div_factor
;
236 proc_ide_settings_warn();
238 mutex_lock(&ide_setting_mtx
);
239 g
= ide_generic_settings
;
241 seq_printf(m
, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
242 seq_printf(m
, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
243 while (g
->name
|| (d
&& d
->name
)) {
244 /* read settings in the alphabetical order */
245 if (g
->name
&& d
&& d
->name
) {
246 if (strcmp(d
->name
, g
->name
) < 0)
250 } else if (d
&& d
->name
) {
254 mul_factor
= setting
->mulf
? setting
->mulf(drive
) : 1;
255 div_factor
= setting
->divf
? setting
->divf(drive
) : 1;
256 seq_printf(m
, "%-24s", setting
->name
);
257 rc
= ide_read_setting(drive
, setting
);
259 seq_printf(m
, "%-16d", rc
* mul_factor
/ div_factor
);
261 seq_printf(m
, "%-16s", "write-only");
262 seq_printf(m
, "%-16d%-16d", (setting
->min
* mul_factor
+ div_factor
- 1) / div_factor
, setting
->max
* mul_factor
/ div_factor
);
263 ds
= setting
->setting
;
270 mutex_unlock(&ide_setting_mtx
);
274 static int ide_settings_proc_open(struct inode
*inode
, struct file
*file
)
276 return single_open(file
, ide_settings_proc_show
, PDE_DATA(inode
));
281 static ssize_t
ide_settings_proc_write(struct file
*file
, const char __user
*buffer
,
282 size_t count
, loff_t
*pos
)
284 ide_drive_t
*drive
= PDE_DATA(file_inode(file
));
285 char name
[MAX_LEN
+ 1];
286 int for_real
= 0, mul_factor
, div_factor
;
289 const struct ide_proc_devset
*setting
;
292 if (!capable(CAP_SYS_ADMIN
))
295 proc_ide_settings_warn();
297 if (count
>= PAGE_SIZE
)
300 s
= buf
= (char *)__get_free_page(GFP_USER
);
304 if (copy_from_user(buf
, buffer
, count
)) {
305 free_page((unsigned long)buf
);
312 * Skip over leading whitespace
314 while (count
&& isspace(*s
)) {
319 * Do one full pass to verify all parameters,
320 * then do another to actually write the new settings.
329 while (n
> 0 && *p
!= ':') {
337 memcpy(name
, q
, p
- q
);
346 val
= simple_strtoul(p
, &q
, 10);
349 if (n
> 0 && !isspace(*p
))
351 while (n
> 0 && isspace(*p
)) {
356 mutex_lock(&ide_setting_mtx
);
357 /* generic settings first, then driver specific ones */
358 setting
= ide_find_setting(ide_generic_settings
, name
);
361 setting
= ide_find_setting(drive
->settings
, name
);
363 mutex_unlock(&ide_setting_mtx
);
368 mul_factor
= setting
->mulf
? setting
->mulf(drive
) : 1;
369 div_factor
= setting
->divf
? setting
->divf(drive
) : 1;
370 ide_write_setting(drive
, setting
, val
* div_factor
/ mul_factor
);
372 mutex_unlock(&ide_setting_mtx
);
374 } while (!for_real
++);
375 free_page((unsigned long)buf
);
378 free_page((unsigned long)buf
);
379 printk("%s(): parse error\n", __func__
);
383 static const struct file_operations ide_settings_proc_fops
= {
384 .owner
= THIS_MODULE
,
385 .open
= ide_settings_proc_open
,
388 .release
= single_release
,
389 .write
= ide_settings_proc_write
,
392 int ide_capacity_proc_show(struct seq_file
*m
, void *v
)
394 seq_printf(m
, "%llu\n", (long long)0x7fffffff);
397 EXPORT_SYMBOL_GPL(ide_capacity_proc_show
);
399 int ide_geometry_proc_show(struct seq_file
*m
, void *v
)
401 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
403 seq_printf(m
, "physical %d/%d/%d\n",
404 drive
->cyl
, drive
->head
, drive
->sect
);
405 seq_printf(m
, "logical %d/%d/%d\n",
406 drive
->bios_cyl
, drive
->bios_head
, drive
->bios_sect
);
409 EXPORT_SYMBOL(ide_geometry_proc_show
);
411 static int ide_dmodel_proc_show(struct seq_file
*seq
, void *v
)
413 ide_drive_t
*drive
= (ide_drive_t
*) seq
->private;
414 char *m
= (char *)&drive
->id
[ATA_ID_PROD
];
416 seq_printf(seq
, "%.40s\n", m
[0] ? m
: "(none)");
420 static int ide_driver_proc_show(struct seq_file
*m
, void *v
)
422 ide_drive_t
*drive
= (ide_drive_t
*)m
->private;
423 struct device
*dev
= &drive
->gendev
;
424 struct ide_driver
*ide_drv
;
427 ide_drv
= to_ide_driver(dev
->driver
);
428 seq_printf(m
, "%s version %s\n",
429 dev
->driver
->name
, ide_drv
->version
);
431 seq_printf(m
, "ide-default version 0.9.newide\n");
435 static int ide_media_proc_show(struct seq_file
*m
, void *v
)
437 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
440 switch (drive
->media
) {
441 case ide_disk
: media
= "disk\n"; break;
442 case ide_cdrom
: media
= "cdrom\n"; break;
443 case ide_tape
: media
= "tape\n"; break;
444 case ide_floppy
: media
= "floppy\n"; break;
445 case ide_optical
: media
= "optical\n"; break;
446 default: media
= "UNKNOWN\n"; break;
452 static int ide_media_proc_open(struct inode
*inode
, struct file
*file
)
454 return single_open(file
, ide_media_proc_show
, PDE_DATA(inode
));
457 static const struct file_operations ide_media_proc_fops
= {
458 .owner
= THIS_MODULE
,
459 .open
= ide_media_proc_open
,
462 .release
= single_release
,
465 static ide_proc_entry_t generic_drive_entries
[] = {
466 { "driver", S_IFREG
|S_IRUGO
, ide_driver_proc_show
},
467 { "identify", S_IFREG
|S_IRUSR
, ide_identify_proc_show
},
468 { "media", S_IFREG
|S_IRUGO
, ide_media_proc_show
},
469 { "model", S_IFREG
|S_IRUGO
, ide_dmodel_proc_show
},
473 static void ide_add_proc_entries(struct proc_dir_entry
*dir
, ide_proc_entry_t
*p
, void *data
)
475 struct proc_dir_entry
*ent
;
479 while (p
->name
!= NULL
) {
480 ent
= proc_create_single_data(p
->name
, p
->mode
, dir
, p
->show
, data
);
486 static void ide_remove_proc_entries(struct proc_dir_entry
*dir
, ide_proc_entry_t
*p
)
490 while (p
->name
!= NULL
) {
491 remove_proc_entry(p
->name
, dir
);
496 void ide_proc_register_driver(ide_drive_t
*drive
, struct ide_driver
*driver
)
498 mutex_lock(&ide_setting_mtx
);
499 drive
->settings
= driver
->proc_devsets(drive
);
500 mutex_unlock(&ide_setting_mtx
);
502 ide_add_proc_entries(drive
->proc
, driver
->proc_entries(drive
), drive
);
505 EXPORT_SYMBOL(ide_proc_register_driver
);
508 * ide_proc_unregister_driver - remove driver specific data
512 * Clean up the driver specific /proc files and IDE settings
515 * Takes ide_setting_mtx.
518 void ide_proc_unregister_driver(ide_drive_t
*drive
, struct ide_driver
*driver
)
520 ide_remove_proc_entries(drive
->proc
, driver
->proc_entries(drive
));
522 mutex_lock(&ide_setting_mtx
);
524 * ide_setting_mtx protects both the settings list and the use
525 * of settings (we cannot take a setting out that is being used).
527 drive
->settings
= NULL
;
528 mutex_unlock(&ide_setting_mtx
);
530 EXPORT_SYMBOL(ide_proc_unregister_driver
);
532 void ide_proc_port_register_devices(ide_hwif_t
*hwif
)
534 struct proc_dir_entry
*ent
;
535 struct proc_dir_entry
*parent
= hwif
->proc
;
540 ide_port_for_each_dev(i
, drive
, hwif
) {
541 if ((drive
->dev_flags
& IDE_DFLAG_PRESENT
) == 0)
544 drive
->proc
= proc_mkdir(drive
->name
, parent
);
546 ide_add_proc_entries(drive
->proc
, generic_drive_entries
, drive
);
547 proc_create_data("settings", S_IFREG
|S_IRUSR
|S_IWUSR
,
548 drive
->proc
, &ide_settings_proc_fops
,
551 sprintf(name
, "ide%d/%s", (drive
->name
[2]-'a')/2, drive
->name
);
552 ent
= proc_symlink(drive
->name
, proc_ide_root
, name
);
557 void ide_proc_unregister_device(ide_drive_t
*drive
)
560 remove_proc_entry("settings", drive
->proc
);
561 ide_remove_proc_entries(drive
->proc
, generic_drive_entries
);
562 remove_proc_entry(drive
->name
, proc_ide_root
);
563 remove_proc_entry(drive
->name
, drive
->hwif
->proc
);
568 static ide_proc_entry_t hwif_entries
[] = {
569 { "channel", S_IFREG
|S_IRUGO
, ide_channel_proc_show
},
570 { "mate", S_IFREG
|S_IRUGO
, ide_mate_proc_show
},
571 { "model", S_IFREG
|S_IRUGO
, ide_imodel_proc_show
},
575 void ide_proc_register_port(ide_hwif_t
*hwif
)
578 hwif
->proc
= proc_mkdir(hwif
->name
, proc_ide_root
);
583 ide_add_proc_entries(hwif
->proc
, hwif_entries
, hwif
);
587 void ide_proc_unregister_port(ide_hwif_t
*hwif
)
590 ide_remove_proc_entries(hwif
->proc
, hwif_entries
);
591 remove_proc_entry(hwif
->name
, proc_ide_root
);
596 static int proc_print_driver(struct device_driver
*drv
, void *data
)
598 struct ide_driver
*ide_drv
= to_ide_driver(drv
);
599 struct seq_file
*s
= data
;
601 seq_printf(s
, "%s version %s\n", drv
->name
, ide_drv
->version
);
606 static int ide_drivers_show(struct seq_file
*s
, void *p
)
610 err
= bus_for_each_drv(&ide_bus_type
, NULL
, s
, proc_print_driver
);
612 printk(KERN_WARNING
"IDE: %s: bus_for_each_drv error: %d\n",
617 static int ide_drivers_open(struct inode
*inode
, struct file
*file
)
619 return single_open(file
, &ide_drivers_show
, NULL
);
622 static const struct file_operations ide_drivers_operations
= {
623 .owner
= THIS_MODULE
,
624 .open
= ide_drivers_open
,
627 .release
= single_release
,
630 void proc_ide_create(void)
632 proc_ide_root
= proc_mkdir("ide", NULL
);
637 proc_create("drivers", 0, proc_ide_root
, &ide_drivers_operations
);
640 void proc_ide_destroy(void)
642 remove_proc_entry("drivers", proc_ide_root
);
643 remove_proc_entry("ide", NULL
);