1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 1997-1998 Mark Lord
4 * Copyright (C) 2003 Red Hat
6 * Some code was moved here from ide.c, see it for original copyrights.
10 * This is the /proc/ide/ filesystem implementation.
12 * Drive/Driver settings can be retrieved by reading the drive's
13 * "settings" files. e.g. "cat /proc/ide0/hda/settings"
14 * To write a new value "val" into a specific setting "name", use:
15 * echo "name:val" >/proc/ide/ide0/hda/settings
18 #include <linux/module.h>
20 #include <linux/uaccess.h>
21 #include <linux/errno.h>
22 #include <linux/proc_fs.h>
23 #include <linux/stat.h>
25 #include <linux/pci.h>
26 #include <linux/ctype.h>
27 #include <linux/ide.h>
28 #include <linux/seq_file.h>
29 #include <linux/slab.h>
33 static struct proc_dir_entry
*proc_ide_root
;
35 static int ide_imodel_proc_show(struct seq_file
*m
, void *v
)
37 ide_hwif_t
*hwif
= (ide_hwif_t
*) m
->private;
40 switch (hwif
->chipset
) {
41 case ide_generic
: name
= "generic"; break;
42 case ide_pci
: name
= "pci"; break;
43 case ide_cmd640
: name
= "cmd640"; break;
44 case ide_dtc2278
: name
= "dtc2278"; break;
45 case ide_ali14xx
: name
= "ali14xx"; break;
46 case ide_qd65xx
: name
= "qd65xx"; break;
47 case ide_umc8672
: name
= "umc8672"; break;
48 case ide_ht6560b
: name
= "ht6560b"; break;
49 case ide_4drives
: name
= "4drives"; break;
50 case ide_pmac
: name
= "mac-io"; break;
51 case ide_au1xxx
: name
= "au1xxx"; break;
52 case ide_palm3710
: name
= "palm3710"; break;
53 case ide_acorn
: name
= "acorn"; break;
54 default: name
= "(unknown)"; break;
56 seq_printf(m
, "%s\n", name
);
60 static int ide_mate_proc_show(struct seq_file
*m
, void *v
)
62 ide_hwif_t
*hwif
= (ide_hwif_t
*) m
->private;
64 if (hwif
&& hwif
->mate
)
65 seq_printf(m
, "%s\n", hwif
->mate
->name
);
67 seq_printf(m
, "(none)\n");
71 static int ide_channel_proc_show(struct seq_file
*m
, void *v
)
73 ide_hwif_t
*hwif
= (ide_hwif_t
*) m
->private;
75 seq_printf(m
, "%c\n", hwif
->channel
? '1' : '0');
79 static int ide_identify_proc_show(struct seq_file
*m
, void *v
)
81 ide_drive_t
*drive
= (ide_drive_t
*)m
->private;
89 buf
= kmalloc(SECTOR_SIZE
, GFP_KERNEL
);
92 if (taskfile_lib_get_identify(drive
, buf
) == 0) {
93 __le16
*val
= (__le16
*)buf
;
96 for (i
= 0; i
< SECTOR_SIZE
/ 2; i
++) {
97 seq_printf(m
, "%04x%c", le16_to_cpu(val
[i
]),
98 (i
% 8) == 7 ? '\n' : ' ');
107 * ide_find_setting - find a specific setting
108 * @st: setting table pointer
109 * @name: setting name
111 * Scan's the setting table for a matching entry and returns
112 * this or NULL if no entry is found. The caller must hold the
117 const struct ide_proc_devset
*ide_find_setting(const struct ide_proc_devset
*st
,
121 if (strcmp(st
->name
, name
) == 0)
125 return st
->name
? st
: NULL
;
129 * ide_read_setting - read an IDE setting
130 * @drive: drive to read from
131 * @setting: drive setting
133 * Read a drive setting and return the value. The caller
134 * must hold the ide_setting_mtx when making this call.
136 * BUGS: the data return and error are the same return value
137 * so an error -EINVAL and true return of the same value cannot
141 static int ide_read_setting(ide_drive_t
*drive
,
142 const struct ide_proc_devset
*setting
)
144 const struct ide_devset
*ds
= setting
->setting
;
148 val
= ds
->get(drive
);
154 * ide_write_setting - read an IDE setting
155 * @drive: drive to read from
156 * @setting: drive setting
159 * Write a drive setting if it is possible. The caller
160 * must hold the ide_setting_mtx when making this call.
162 * BUGS: the data return and error are the same return value
163 * so an error -EINVAL and true return of the same value cannot
166 * FIXME: This should be changed to enqueue a special request
167 * to the driver to change settings, and then wait on a sema for completion.
168 * The current scheme of polling is kludgy, though safe enough.
171 static int ide_write_setting(ide_drive_t
*drive
,
172 const struct ide_proc_devset
*setting
, int val
)
174 const struct ide_devset
*ds
= setting
->setting
;
176 if (!capable(CAP_SYS_ADMIN
))
180 if ((ds
->flags
& DS_SYNC
)
181 && (val
< setting
->min
|| val
> setting
->max
))
183 return ide_devset_execute(drive
, ds
, val
);
186 ide_devset_get(xfer_rate
, current_speed
);
188 static int set_xfer_rate (ide_drive_t
*drive
, int arg
)
192 if (arg
< XFER_PIO_0
|| arg
> XFER_UDMA_6
)
195 memset(&cmd
, 0, sizeof(cmd
));
196 cmd
.tf
.command
= ATA_CMD_SET_FEATURES
;
197 cmd
.tf
.feature
= SETFEATURES_XFER
;
198 cmd
.tf
.nsect
= (u8
)arg
;
199 cmd
.valid
.out
.tf
= IDE_VALID_FEATURE
| IDE_VALID_NSECT
;
200 cmd
.valid
.in
.tf
= IDE_VALID_NSECT
;
201 cmd
.tf_flags
= IDE_TFLAG_SET_XFER
;
203 return ide_no_data_taskfile(drive
, &cmd
);
206 ide_devset_rw(current_speed
, xfer_rate
);
207 ide_devset_rw_field(init_speed
, init_speed
);
208 ide_devset_rw_flag(nice1
, IDE_DFLAG_NICE1
);
209 ide_devset_rw_field(number
, dn
);
211 static const struct ide_proc_devset ide_generic_settings
[] = {
212 IDE_PROC_DEVSET(current_speed
, 0, 70),
213 IDE_PROC_DEVSET(init_speed
, 0, 70),
214 IDE_PROC_DEVSET(io_32bit
, 0, 1 + (SUPPORT_VLB_SYNC
<< 1)),
215 IDE_PROC_DEVSET(keepsettings
, 0, 1),
216 IDE_PROC_DEVSET(nice1
, 0, 1),
217 IDE_PROC_DEVSET(number
, 0, 3),
218 IDE_PROC_DEVSET(pio_mode
, 0, 255),
219 IDE_PROC_DEVSET(unmaskirq
, 0, 1),
220 IDE_PROC_DEVSET(using_dma
, 0, 1),
224 static void proc_ide_settings_warn(void)
226 printk_once(KERN_WARNING
"Warning: /proc/ide/hd?/settings interface is "
227 "obsolete, and will be removed soon!\n");
230 static int ide_settings_proc_show(struct seq_file
*m
, void *v
)
232 const struct ide_proc_devset
*setting
, *g
, *d
;
233 const struct ide_devset
*ds
;
234 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
235 int rc
, mul_factor
, div_factor
;
237 proc_ide_settings_warn();
239 mutex_lock(&ide_setting_mtx
);
240 g
= ide_generic_settings
;
242 seq_printf(m
, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
243 seq_printf(m
, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
244 while (g
->name
|| (d
&& d
->name
)) {
245 /* read settings in the alphabetical order */
246 if (g
->name
&& d
&& d
->name
) {
247 if (strcmp(d
->name
, g
->name
) < 0)
251 } else if (d
&& d
->name
) {
255 mul_factor
= setting
->mulf
? setting
->mulf(drive
) : 1;
256 div_factor
= setting
->divf
? setting
->divf(drive
) : 1;
257 seq_printf(m
, "%-24s", setting
->name
);
258 rc
= ide_read_setting(drive
, setting
);
260 seq_printf(m
, "%-16d", rc
* mul_factor
/ div_factor
);
262 seq_printf(m
, "%-16s", "write-only");
263 seq_printf(m
, "%-16d%-16d", (setting
->min
* mul_factor
+ div_factor
- 1) / div_factor
, setting
->max
* mul_factor
/ div_factor
);
264 ds
= setting
->setting
;
271 mutex_unlock(&ide_setting_mtx
);
275 static int ide_settings_proc_open(struct inode
*inode
, struct file
*file
)
277 return single_open(file
, ide_settings_proc_show
, PDE_DATA(inode
));
282 static ssize_t
ide_settings_proc_write(struct file
*file
, const char __user
*buffer
,
283 size_t count
, loff_t
*pos
)
285 ide_drive_t
*drive
= PDE_DATA(file_inode(file
));
286 char name
[MAX_LEN
+ 1];
287 int for_real
= 0, mul_factor
, div_factor
;
290 const struct ide_proc_devset
*setting
;
293 if (!capable(CAP_SYS_ADMIN
))
296 proc_ide_settings_warn();
298 if (count
>= PAGE_SIZE
)
301 s
= buf
= (char *)__get_free_page(GFP_USER
);
305 if (copy_from_user(buf
, buffer
, count
)) {
306 free_page((unsigned long)buf
);
313 * Skip over leading whitespace
315 while (count
&& isspace(*s
)) {
320 * Do one full pass to verify all parameters,
321 * then do another to actually write the new settings.
330 while (n
> 0 && *p
!= ':') {
338 memcpy(name
, q
, p
- q
);
347 val
= simple_strtoul(p
, &q
, 10);
350 if (n
> 0 && !isspace(*p
))
352 while (n
> 0 && isspace(*p
)) {
357 mutex_lock(&ide_setting_mtx
);
358 /* generic settings first, then driver specific ones */
359 setting
= ide_find_setting(ide_generic_settings
, name
);
362 setting
= ide_find_setting(drive
->settings
, name
);
364 mutex_unlock(&ide_setting_mtx
);
369 mul_factor
= setting
->mulf
? setting
->mulf(drive
) : 1;
370 div_factor
= setting
->divf
? setting
->divf(drive
) : 1;
371 ide_write_setting(drive
, setting
, val
* div_factor
/ mul_factor
);
373 mutex_unlock(&ide_setting_mtx
);
375 } while (!for_real
++);
376 free_page((unsigned long)buf
);
379 free_page((unsigned long)buf
);
380 printk("%s(): parse error\n", __func__
);
384 static const struct file_operations ide_settings_proc_fops
= {
385 .owner
= THIS_MODULE
,
386 .open
= ide_settings_proc_open
,
389 .release
= single_release
,
390 .write
= ide_settings_proc_write
,
393 int ide_capacity_proc_show(struct seq_file
*m
, void *v
)
395 seq_printf(m
, "%llu\n", (long long)0x7fffffff);
398 EXPORT_SYMBOL_GPL(ide_capacity_proc_show
);
400 int ide_geometry_proc_show(struct seq_file
*m
, void *v
)
402 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
404 seq_printf(m
, "physical %d/%d/%d\n",
405 drive
->cyl
, drive
->head
, drive
->sect
);
406 seq_printf(m
, "logical %d/%d/%d\n",
407 drive
->bios_cyl
, drive
->bios_head
, drive
->bios_sect
);
410 EXPORT_SYMBOL(ide_geometry_proc_show
);
412 static int ide_dmodel_proc_show(struct seq_file
*seq
, void *v
)
414 ide_drive_t
*drive
= (ide_drive_t
*) seq
->private;
415 char *m
= (char *)&drive
->id
[ATA_ID_PROD
];
417 seq_printf(seq
, "%.40s\n", m
[0] ? m
: "(none)");
421 static int ide_driver_proc_show(struct seq_file
*m
, void *v
)
423 ide_drive_t
*drive
= (ide_drive_t
*)m
->private;
424 struct device
*dev
= &drive
->gendev
;
425 struct ide_driver
*ide_drv
;
428 ide_drv
= to_ide_driver(dev
->driver
);
429 seq_printf(m
, "%s version %s\n",
430 dev
->driver
->name
, ide_drv
->version
);
432 seq_printf(m
, "ide-default version 0.9.newide\n");
436 static int ide_media_proc_show(struct seq_file
*m
, void *v
)
438 ide_drive_t
*drive
= (ide_drive_t
*) m
->private;
441 switch (drive
->media
) {
442 case ide_disk
: media
= "disk\n"; break;
443 case ide_cdrom
: media
= "cdrom\n"; break;
444 case ide_tape
: media
= "tape\n"; break;
445 case ide_floppy
: media
= "floppy\n"; break;
446 case ide_optical
: media
= "optical\n"; break;
447 default: media
= "UNKNOWN\n"; break;
453 static int ide_media_proc_open(struct inode
*inode
, struct file
*file
)
455 return single_open(file
, ide_media_proc_show
, PDE_DATA(inode
));
458 static const struct file_operations ide_media_proc_fops
= {
459 .owner
= THIS_MODULE
,
460 .open
= ide_media_proc_open
,
463 .release
= single_release
,
466 static ide_proc_entry_t generic_drive_entries
[] = {
467 { "driver", S_IFREG
|S_IRUGO
, ide_driver_proc_show
},
468 { "identify", S_IFREG
|S_IRUSR
, ide_identify_proc_show
},
469 { "media", S_IFREG
|S_IRUGO
, ide_media_proc_show
},
470 { "model", S_IFREG
|S_IRUGO
, ide_dmodel_proc_show
},
474 static void ide_add_proc_entries(struct proc_dir_entry
*dir
, ide_proc_entry_t
*p
, void *data
)
476 struct proc_dir_entry
*ent
;
480 while (p
->name
!= NULL
) {
481 ent
= proc_create_single_data(p
->name
, p
->mode
, dir
, p
->show
, data
);
487 static void ide_remove_proc_entries(struct proc_dir_entry
*dir
, ide_proc_entry_t
*p
)
491 while (p
->name
!= NULL
) {
492 remove_proc_entry(p
->name
, dir
);
497 void ide_proc_register_driver(ide_drive_t
*drive
, struct ide_driver
*driver
)
499 mutex_lock(&ide_setting_mtx
);
500 drive
->settings
= driver
->proc_devsets(drive
);
501 mutex_unlock(&ide_setting_mtx
);
503 ide_add_proc_entries(drive
->proc
, driver
->proc_entries(drive
), drive
);
506 EXPORT_SYMBOL(ide_proc_register_driver
);
509 * ide_proc_unregister_driver - remove driver specific data
513 * Clean up the driver specific /proc files and IDE settings
516 * Takes ide_setting_mtx.
519 void ide_proc_unregister_driver(ide_drive_t
*drive
, struct ide_driver
*driver
)
521 ide_remove_proc_entries(drive
->proc
, driver
->proc_entries(drive
));
523 mutex_lock(&ide_setting_mtx
);
525 * ide_setting_mtx protects both the settings list and the use
526 * of settings (we cannot take a setting out that is being used).
528 drive
->settings
= NULL
;
529 mutex_unlock(&ide_setting_mtx
);
531 EXPORT_SYMBOL(ide_proc_unregister_driver
);
533 void ide_proc_port_register_devices(ide_hwif_t
*hwif
)
535 struct proc_dir_entry
*ent
;
536 struct proc_dir_entry
*parent
= hwif
->proc
;
541 ide_port_for_each_dev(i
, drive
, hwif
) {
542 if ((drive
->dev_flags
& IDE_DFLAG_PRESENT
) == 0)
545 drive
->proc
= proc_mkdir(drive
->name
, parent
);
547 ide_add_proc_entries(drive
->proc
, generic_drive_entries
, drive
);
548 proc_create_data("settings", S_IFREG
|S_IRUSR
|S_IWUSR
,
549 drive
->proc
, &ide_settings_proc_fops
,
552 sprintf(name
, "ide%d/%s", (drive
->name
[2]-'a')/2, drive
->name
);
553 ent
= proc_symlink(drive
->name
, proc_ide_root
, name
);
558 void ide_proc_unregister_device(ide_drive_t
*drive
)
561 remove_proc_entry("settings", drive
->proc
);
562 ide_remove_proc_entries(drive
->proc
, generic_drive_entries
);
563 remove_proc_entry(drive
->name
, proc_ide_root
);
564 remove_proc_entry(drive
->name
, drive
->hwif
->proc
);
569 static ide_proc_entry_t hwif_entries
[] = {
570 { "channel", S_IFREG
|S_IRUGO
, ide_channel_proc_show
},
571 { "mate", S_IFREG
|S_IRUGO
, ide_mate_proc_show
},
572 { "model", S_IFREG
|S_IRUGO
, ide_imodel_proc_show
},
576 void ide_proc_register_port(ide_hwif_t
*hwif
)
579 hwif
->proc
= proc_mkdir(hwif
->name
, proc_ide_root
);
584 ide_add_proc_entries(hwif
->proc
, hwif_entries
, hwif
);
588 void ide_proc_unregister_port(ide_hwif_t
*hwif
)
591 ide_remove_proc_entries(hwif
->proc
, hwif_entries
);
592 remove_proc_entry(hwif
->name
, proc_ide_root
);
597 static int proc_print_driver(struct device_driver
*drv
, void *data
)
599 struct ide_driver
*ide_drv
= to_ide_driver(drv
);
600 struct seq_file
*s
= data
;
602 seq_printf(s
, "%s version %s\n", drv
->name
, ide_drv
->version
);
607 static int ide_drivers_show(struct seq_file
*s
, void *p
)
611 err
= bus_for_each_drv(&ide_bus_type
, NULL
, s
, proc_print_driver
);
613 printk(KERN_WARNING
"IDE: %s: bus_for_each_drv error: %d\n",
618 DEFINE_SHOW_ATTRIBUTE(ide_drivers
);
620 void proc_ide_create(void)
622 proc_ide_root
= proc_mkdir("ide", NULL
);
627 proc_create("drivers", 0, proc_ide_root
, &ide_drivers_fops
);
630 void proc_ide_destroy(void)
632 remove_proc_entry("drivers", proc_ide_root
);
633 remove_proc_entry("ide", NULL
);