blob: 017c09540c2f70b416af3a39eaf122a5a49287f1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1997-1998 Mark Lord
Alan Coxccd32e22008-11-02 21:40:08 +01003 * Copyright (C) 2003 Red Hat
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004 *
5 * Some code was moved here from ide.c, see it for original copyrights.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8/*
9 * This is the /proc/ide/ filesystem implementation.
10 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18
19#include <asm/uaccess.h>
20#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/proc_fs.h>
22#include <linux/stat.h>
23#include <linux/mm.h>
24#include <linux/pci.h>
25#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/ide.h>
27#include <linux/seq_file.h>
28
29#include <asm/io.h>
30
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +020031static struct proc_dir_entry *proc_ide_root;
32
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070033static int ide_imodel_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070035 ide_hwif_t *hwif = (ide_hwif_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 const char *name;
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 switch (hwif->chipset) {
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020039 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;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020047 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;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020051 case ide_acorn: name = "acorn"; break;
52 default: name = "(unknown)"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070054 seq_printf(m, "%s\n", name);
55 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070058static int ide_imodel_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070060 return single_open(file, ide_imodel_proc_show, PDE(inode)->data);
61}
62
63static const struct file_operations ide_imodel_proc_fops = {
64 .owner = THIS_MODULE,
65 .open = ide_imodel_proc_open,
66 .read = seq_read,
67 .llseek = seq_lseek,
68 .release = single_release,
69};
70
71static int ide_mate_proc_show(struct seq_file *m, void *v)
72{
73 ide_hwif_t *hwif = (ide_hwif_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Bartlomiej Zolnierkiewicz4283e1b2008-06-30 20:14:45 +020075 if (hwif && hwif->mate)
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070076 seq_printf(m, "%s\n", hwif->mate->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 else
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070078 seq_printf(m, "(none)\n");
79 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070082static int ide_mate_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070084 return single_open(file, ide_mate_proc_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070087static const struct file_operations ide_mate_proc_fops = {
88 .owner = THIS_MODULE,
89 .open = ide_mate_proc_open,
90 .read = seq_read,
91 .llseek = seq_lseek,
92 .release = single_release,
93};
94
95static int ide_channel_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070097 ide_hwif_t *hwif = (ide_hwif_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070099 seq_printf(m, "%c\n", hwif->channel ? '1' : '0');
100 return 0;
101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700103static int ide_channel_proc_open(struct inode *inode, struct file *file)
104{
105 return single_open(file, ide_channel_proc_show, PDE(inode)->data);
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700108static const struct file_operations ide_channel_proc_fops = {
109 .owner = THIS_MODULE,
110 .open = ide_channel_proc_open,
111 .read = seq_read,
112 .llseek = seq_lseek,
113 .release = single_release,
114};
Bartlomiej Zolnierkiewicz151a6702008-10-10 22:39:28 +0200115
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700116static int ide_identify_proc_show(struct seq_file *m, void *v)
117{
118 ide_drive_t *drive = (ide_drive_t *)m->private;
119 u8 *buf;
120
121 if (!drive) {
122 seq_putc(m, '\n');
123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700125
126 buf = kmalloc(SECTOR_SIZE, GFP_KERNEL);
127 if (!buf)
128 return -ENOMEM;
129 if (taskfile_lib_get_identify(drive, buf) == 0) {
130 __le16 *val = (__le16 *)buf;
131 int i;
132
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' : ' ');
136 }
137 } else
138 seq_putc(m, buf[0]);
139 kfree(buf);
140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700143static int ide_identify_proc_open(struct inode *inode, struct file *file)
144{
145 return single_open(file, ide_identify_proc_show, PDE(inode)->data);
146}
147
148static const struct file_operations ide_identify_proc_fops = {
149 .owner = THIS_MODULE,
150 .open = ide_identify_proc_open,
151 .read = seq_read,
152 .llseek = seq_lseek,
153 .release = single_release,
154};
155
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200156/**
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200157 * ide_find_setting - find a specific setting
158 * @st: setting table pointer
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200159 * @name: setting name
160 *
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200161 * Scan's the setting table for a matching entry and returns
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200162 * this or NULL if no entry is found. The caller must hold the
163 * setting semaphore
164 */
165
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200166static
167const struct ide_proc_devset *ide_find_setting(const struct ide_proc_devset *st,
168 char *name)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200169{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200170 while (st->name) {
171 if (strcmp(st->name, name) == 0)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200172 break;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200173 st++;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200174 }
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200175 return st->name ? st : NULL;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200176}
177
178/**
179 * ide_read_setting - read an IDE setting
180 * @drive: drive to read from
181 * @setting: drive setting
182 *
183 * Read a drive setting and return the value. The caller
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200184 * must hold the ide_setting_mtx when making this call.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200185 *
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
188 * be told apart
189 */
190
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200191static int ide_read_setting(ide_drive_t *drive,
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200192 const struct ide_proc_devset *setting)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200193{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200194 const struct ide_devset *ds = setting->setting;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200195 int val = -EINVAL;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200196
Bartlomiej Zolnierkiewicz1f473e92008-12-29 20:27:29 +0100197 if (ds->get)
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200198 val = ds->get(drive);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200199
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200200 return val;
201}
202
203/**
204 * ide_write_setting - read an IDE setting
205 * @drive: drive to read from
206 * @setting: drive setting
207 * @val: value
208 *
209 * Write a drive setting if it is possible. The caller
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200210 * must hold the ide_setting_mtx when making this call.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200211 *
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
214 * be told apart
215 *
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.
219 */
220
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200221static int ide_write_setting(ide_drive_t *drive,
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200222 const struct ide_proc_devset *setting, int val)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200223{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200224 const struct ide_devset *ds = setting->setting;
225
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200226 if (!capable(CAP_SYS_ADMIN))
227 return -EACCES;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200228 if (!ds->set)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200229 return -EPERM;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200230 if ((ds->flags & DS_SYNC)
231 && (val < setting->min || val > setting->max))
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200232 return -EINVAL;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200233 return ide_devset_execute(drive, ds, val);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200234}
235
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200236ide_devset_get(xfer_rate, current_speed);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200237
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200238static int set_xfer_rate (ide_drive_t *drive, int arg)
239{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100240 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200241
Bartlomiej Zolnierkiewicz3b2a5c72008-07-23 19:55:56 +0200242 if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200243 return -EINVAL;
244
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100245 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;
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200249 cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
250 cmd.valid.in.tf = IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000251 cmd.tf_flags = IDE_TFLAG_SET_XFER;
Bartlomiej Zolnierkiewicz34f5d5a2008-01-26 20:13:12 +0100252
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000253 return ide_no_data_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200254}
255
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200256ide_devset_rw(current_speed, xfer_rate);
257ide_devset_rw_field(init_speed, init_speed);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200258ide_devset_rw_flag(nice1, IDE_DFLAG_NICE1);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200259ide_devset_rw_field(number, dn);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200260
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200261static 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),
Hannes Eder71bfc7a2009-03-05 16:10:56 +0100271 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200272};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static void proc_ide_settings_warn(void)
275{
Marcin Slusarz01f1afa2009-09-22 16:29:00 -0700276 printk_once(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 "obsolete, and will be removed soon!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700280static int ide_settings_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200282 const struct ide_proc_devset *setting, *g, *d;
283 const struct ide_devset *ds;
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700284 ide_drive_t *drive = (ide_drive_t *) m->private;
285 int rc, mul_factor, div_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 proc_ide_settings_warn();
288
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200289 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200290 g = ide_generic_settings;
291 d = drive->settings;
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700292 seq_printf(m, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
293 seq_printf(m, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200294 while (g->name || (d && d->name)) {
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200295 /* read settings in the alphabetical order */
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200296 if (g->name && d && d->name) {
297 if (strcmp(d->name, g->name) < 0)
298 setting = d++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200299 else
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200300 setting = g++;
301 } else if (d && d->name) {
302 setting = d++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200303 } else
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200304 setting = g++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200305 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
306 div_factor = setting->divf ? setting->divf(drive) : 1;
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700307 seq_printf(m, "%-24s", setting->name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200308 rc = ide_read_setting(drive, setting);
309 if (rc >= 0)
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700310 seq_printf(m, "%-16d", rc * mul_factor / div_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 else
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700312 seq_printf(m, "%-16s", "write-only");
313 seq_printf(m, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200314 ds = setting->setting;
315 if (ds->get)
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700316 seq_printf(m, "r");
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200317 if (ds->set)
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700318 seq_printf(m, "w");
319 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200321 mutex_unlock(&ide_setting_mtx);
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700322 return 0;
323}
324
325static int ide_settings_proc_open(struct inode *inode, struct file *file)
326{
327 return single_open(file, ide_settings_proc_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330#define MAX_LEN 30
331
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700332static ssize_t ide_settings_proc_write(struct file *file, const char __user *buffer,
333 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700335 ide_drive_t *drive = (ide_drive_t *) PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 char name[MAX_LEN + 1];
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200337 int for_real = 0, mul_factor, div_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 unsigned long n;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200339
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200340 const struct ide_proc_devset *setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 char *buf, *s;
342
343 if (!capable(CAP_SYS_ADMIN))
344 return -EACCES;
345
346 proc_ide_settings_warn();
347
348 if (count >= PAGE_SIZE)
349 return -EINVAL;
350
351 s = buf = (char *)__get_free_page(GFP_USER);
352 if (!buf)
353 return -ENOMEM;
354
355 if (copy_from_user(buf, buffer, count)) {
356 free_page((unsigned long)buf);
357 return -EFAULT;
358 }
359
360 buf[count] = '\0';
361
362 /*
363 * Skip over leading whitespace
364 */
365 while (count && isspace(*s)) {
366 --count;
367 ++s;
368 }
369 /*
370 * Do one full pass to verify all parameters,
371 * then do another to actually write the new settings.
372 */
373 do {
374 char *p = s;
375 n = count;
376 while (n > 0) {
377 unsigned val;
378 char *q = p;
379
380 while (n > 0 && *p != ':') {
381 --n;
382 p++;
383 }
384 if (*p != ':')
385 goto parse_error;
386 if (p - q > MAX_LEN)
387 goto parse_error;
388 memcpy(name, q, p - q);
389 name[p - q] = 0;
390
391 if (n > 0) {
392 --n;
393 p++;
394 } else
395 goto parse_error;
396
397 val = simple_strtoul(p, &q, 10);
398 n -= q - p;
399 p = q;
400 if (n > 0 && !isspace(*p))
401 goto parse_error;
402 while (n > 0 && isspace(*p)) {
403 --n;
404 ++p;
405 }
406
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200407 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200408 /* generic settings first, then driver specific ones */
409 setting = ide_find_setting(ide_generic_settings, name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200410 if (!setting) {
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200411 if (drive->settings)
412 setting = ide_find_setting(drive->settings, name);
413 if (!setting) {
414 mutex_unlock(&ide_setting_mtx);
415 goto parse_error;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200418 if (for_real) {
419 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
420 div_factor = setting->divf ? setting->divf(drive) : 1;
421 ide_write_setting(drive, setting, val * div_factor / mul_factor);
422 }
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200423 mutex_unlock(&ide_setting_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 } while (!for_real++);
426 free_page((unsigned long)buf);
427 return count;
428parse_error:
429 free_page((unsigned long)buf);
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700430 printk("%s(): parse error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -EINVAL;
432}
433
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700434static const struct file_operations ide_settings_proc_fops = {
435 .owner = THIS_MODULE,
436 .open = ide_settings_proc_open,
437 .read = seq_read,
438 .llseek = seq_lseek,
439 .release = single_release,
440 .write = ide_settings_proc_write,
441};
442
443static int ide_capacity_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700445 seq_printf(m, "%llu\n", (long long)0x7fffffff);
446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700449static int ide_capacity_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700451 return single_open(file, ide_capacity_proc_show, NULL);
452}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700454const struct file_operations ide_capacity_proc_fops = {
455 .owner = THIS_MODULE,
456 .open = ide_capacity_proc_open,
457 .read = seq_read,
458 .llseek = seq_lseek,
459 .release = single_release,
460};
461EXPORT_SYMBOL_GPL(ide_capacity_proc_fops);
462
463static int ide_geometry_proc_show(struct seq_file *m, void *v)
464{
465 ide_drive_t *drive = (ide_drive_t *) m->private;
466
467 seq_printf(m, "physical %d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 drive->cyl, drive->head, drive->sect);
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700469 seq_printf(m, "logical %d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 drive->bios_cyl, drive->bios_head, drive->bios_sect);
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700474static int ide_geometry_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700476 return single_open(file, ide_geometry_proc_show, PDE(inode)->data);
477}
478
479const struct file_operations ide_geometry_proc_fops = {
480 .owner = THIS_MODULE,
481 .open = ide_geometry_proc_open,
482 .read = seq_read,
483 .llseek = seq_lseek,
484 .release = single_release,
485};
486EXPORT_SYMBOL(ide_geometry_proc_fops);
487
488static int ide_dmodel_proc_show(struct seq_file *seq, void *v)
489{
490 ide_drive_t *drive = (ide_drive_t *) seq->private;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200491 char *m = (char *)&drive->id[ATA_ID_PROD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700493 seq_printf(seq, "%.40s\n", m[0] ? m : "(none)");
494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700497static int ide_dmodel_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700499 return single_open(file, ide_dmodel_proc_show, PDE(inode)->data);
500}
501
502static const struct file_operations ide_dmodel_proc_fops = {
503 .owner = THIS_MODULE,
504 .open = ide_dmodel_proc_open,
505 .read = seq_read,
506 .llseek = seq_lseek,
507 .release = single_release,
508};
509
510static int ide_driver_proc_show(struct seq_file *m, void *v)
511{
512 ide_drive_t *drive = (ide_drive_t *)m->private;
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100513 struct device *dev = &drive->gendev;
514 struct ide_driver *ide_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200516 if (dev->driver) {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100517 ide_drv = to_ide_driver(dev->driver);
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700518 seq_printf(m, "%s version %s\n",
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200519 dev->driver->name, ide_drv->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 } else
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700521 seq_printf(m, "ide-default version 0.9.newide\n");
522 return 0;
523}
524
525static int ide_driver_proc_open(struct inode *inode, struct file *file)
526{
527 return single_open(file, ide_driver_proc_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200530static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
531{
532 struct device *dev = &drive->gendev;
533 int ret = 1;
Randy Dunlap349ae232006-10-03 01:14:23 -0700534 int err;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200535
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200536 device_release_driver(dev);
537 /* FIXME: device can still be in use by previous driver */
538 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
Randy Dunlap349ae232006-10-03 01:14:23 -0700539 err = device_attach(dev);
540 if (err < 0)
541 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200542 __func__, err);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200543 drive->driver_req[0] = 0;
Randy Dunlap349ae232006-10-03 01:14:23 -0700544 if (dev->driver == NULL) {
545 err = device_attach(dev);
546 if (err < 0)
547 printk(KERN_WARNING
548 "IDE: %s: device_attach(2) error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200549 __func__, err);
Randy Dunlap349ae232006-10-03 01:14:23 -0700550 }
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200551 if (dev->driver && !strcmp(dev->driver->name, driver))
552 ret = 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200553
554 return ret;
555}
556
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700557static ssize_t ide_driver_proc_write(struct file *file, const char __user *buffer,
558 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700560 ide_drive_t *drive = (ide_drive_t *) PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 char name[32];
562
563 if (!capable(CAP_SYS_ADMIN))
564 return -EACCES;
565 if (count > 31)
566 count = 31;
567 if (copy_from_user(name, buffer, count))
568 return -EFAULT;
569 name[count] = '\0';
570 if (ide_replace_subdriver(drive, name))
571 return -EINVAL;
572 return count;
573}
574
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700575static const struct file_operations ide_driver_proc_fops = {
576 .owner = THIS_MODULE,
577 .open = ide_driver_proc_open,
578 .read = seq_read,
579 .llseek = seq_lseek,
580 .release = single_release,
581 .write = ide_driver_proc_write,
582};
583
584static int ide_media_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700586 ide_drive_t *drive = (ide_drive_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 const char *media;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 switch (drive->media) {
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200590 case ide_disk: media = "disk\n"; break;
591 case ide_cdrom: media = "cdrom\n"; break;
592 case ide_tape: media = "tape\n"; break;
593 case ide_floppy: media = "floppy\n"; break;
594 case ide_optical: media = "optical\n"; break;
595 default: media = "UNKNOWN\n"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700597 seq_puts(m, media);
598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700601static int ide_media_proc_open(struct inode *inode, struct file *file)
602{
603 return single_open(file, ide_media_proc_show, PDE(inode)->data);
604}
605
606static const struct file_operations ide_media_proc_fops = {
607 .owner = THIS_MODULE,
608 .open = ide_media_proc_open,
609 .read = seq_read,
610 .llseek = seq_lseek,
611 .release = single_release,
612};
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static ide_proc_entry_t generic_drive_entries[] = {
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700615 { "driver", S_IFREG|S_IRUGO, &ide_driver_proc_fops },
616 { "identify", S_IFREG|S_IRUSR, &ide_identify_proc_fops},
617 { "media", S_IFREG|S_IRUGO, &ide_media_proc_fops },
618 { "model", S_IFREG|S_IRUGO, &ide_dmodel_proc_fops },
619 { "settings", S_IFREG|S_IRUSR|S_IWUSR, &ide_settings_proc_fops},
620 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621};
622
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200623static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 struct proc_dir_entry *ent;
626
627 if (!dir || !p)
628 return;
629 while (p->name != NULL) {
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700630 ent = proc_create_data(p->name, p->mode, dir, p->proc_fops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!ent) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 p++;
633 }
634}
635
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200636static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 if (!dir || !p)
639 return;
640 while (p->name != NULL) {
641 remove_proc_entry(p->name, dir);
642 p++;
643 }
644}
645
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100646void ide_proc_register_driver(ide_drive_t *drive, struct ide_driver *driver)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200647{
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200648 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200649 drive->settings = driver->proc_devsets(drive);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200650 mutex_unlock(&ide_setting_mtx);
651
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200652 ide_add_proc_entries(drive->proc, driver->proc_entries(drive), drive);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200653}
654
655EXPORT_SYMBOL(ide_proc_register_driver);
656
657/**
658 * ide_proc_unregister_driver - remove driver specific data
659 * @drive: drive
660 * @driver: driver
661 *
662 * Clean up the driver specific /proc files and IDE settings
663 * for a given drive.
664 *
Bartlomiej Zolnierkiewicz1f473e92008-12-29 20:27:29 +0100665 * Takes ide_setting_mtx.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200666 */
667
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100668void ide_proc_unregister_driver(ide_drive_t *drive, struct ide_driver *driver)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200669{
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200670 ide_remove_proc_entries(drive->proc, driver->proc_entries(drive));
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200671
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200672 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200673 /*
Bartlomiej Zolnierkiewicz1f473e92008-12-29 20:27:29 +0100674 * ide_setting_mtx protects both the settings list and the use
675 * of settings (we cannot take a setting out that is being used).
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200676 */
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200677 drive->settings = NULL;
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200678 mutex_unlock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200679}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200680EXPORT_SYMBOL(ide_proc_unregister_driver);
681
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +0100682void ide_proc_port_register_devices(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct proc_dir_entry *ent;
685 struct proc_dir_entry *parent = hwif->proc;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100686 ide_drive_t *drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 char name[64];
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100688 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100690 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz1902a252009-03-24 23:22:40 +0100691 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 continue;
693
694 drive->proc = proc_mkdir(drive->name, parent);
695 if (drive->proc)
696 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200697 sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 ent = proc_symlink(drive->name, proc_ide_root, name);
699 if (!ent) return;
700 }
701}
702
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200703void ide_proc_unregister_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 if (drive->proc) {
706 ide_remove_proc_entries(drive->proc, generic_drive_entries);
707 remove_proc_entry(drive->name, proc_ide_root);
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200708 remove_proc_entry(drive->name, drive->hwif->proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 drive->proc = NULL;
710 }
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713static ide_proc_entry_t hwif_entries[] = {
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700714 { "channel", S_IFREG|S_IRUGO, &ide_channel_proc_fops },
715 { "mate", S_IFREG|S_IRUGO, &ide_mate_proc_fops },
716 { "model", S_IFREG|S_IRUGO, &ide_imodel_proc_fops },
717 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718};
719
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200720void ide_proc_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200722 if (!hwif->proc) {
723 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200725 if (!hwif->proc)
726 return;
727
728 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730}
731
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200732void ide_proc_unregister_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 if (hwif->proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 ide_remove_proc_entries(hwif->proc, hwif_entries);
736 remove_proc_entry(hwif->name, proc_ide_root);
737 hwif->proc = NULL;
738 }
739}
740
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200741static int proc_print_driver(struct device_driver *drv, void *data)
742{
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100743 struct ide_driver *ide_drv = to_ide_driver(drv);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200744 struct seq_file *s = data;
745
746 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
747
748 return 0;
749}
750
751static int ide_drivers_show(struct seq_file *s, void *p)
752{
Randy Dunlap349ae232006-10-03 01:14:23 -0700753 int err;
754
755 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
756 if (err < 0)
757 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200758 __func__, err);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200759 return 0;
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762static int ide_drivers_open(struct inode *inode, struct file *file)
763{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200764 return single_open(file, &ide_drivers_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200766
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800767static const struct file_operations ide_drivers_operations = {
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700768 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 .open = ide_drivers_open,
770 .read = seq_read,
771 .llseek = seq_lseek,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200772 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773};
774
775void proc_ide_create(void)
776{
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200777 proc_ide_root = proc_mkdir("ide", NULL);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (!proc_ide_root)
780 return;
781
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700782 proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785void proc_ide_destroy(void)
786{
Zhang, Yanmin643bdc62005-05-16 21:53:11 -0700787 remove_proc_entry("drivers", proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 remove_proc_entry("ide", NULL);
789}