blob: a5dc13576394ffa8d7be2c2ae9298211ea216f47 [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
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/io.h>
31
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +020032static struct proc_dir_entry *proc_ide_root;
33
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070034static int ide_imodel_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070036 ide_hwif_t *hwif = (ide_hwif_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 const char *name;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 switch (hwif->chipset) {
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020040 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;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020048 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;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020052 case ide_acorn: name = "acorn"; break;
53 default: name = "(unknown)"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070055 seq_printf(m, "%s\n", name);
56 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070059static int ide_mate_proc_show(struct seq_file *m, void *v)
60{
61 ide_hwif_t *hwif = (ide_hwif_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Bartlomiej Zolnierkiewicz4283e1b2008-06-30 20:14:45 +020063 if (hwif && hwif->mate)
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070064 seq_printf(m, "%s\n", hwif->mate->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 else
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070066 seq_printf(m, "(none)\n");
67 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070070static int ide_channel_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070072 ide_hwif_t *hwif = (ide_hwif_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070074 seq_printf(m, "%c\n", hwif->channel ? '1' : '0');
75 return 0;
76}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070078static int ide_identify_proc_show(struct seq_file *m, void *v)
79{
80 ide_drive_t *drive = (ide_drive_t *)m->private;
81 u8 *buf;
82
83 if (!drive) {
84 seq_putc(m, '\n');
85 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070087
88 buf = kmalloc(SECTOR_SIZE, GFP_KERNEL);
89 if (!buf)
90 return -ENOMEM;
91 if (taskfile_lib_get_identify(drive, buf) == 0) {
92 __le16 *val = (__le16 *)buf;
93 int i;
94
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' : ' ');
98 }
99 } else
100 seq_putc(m, buf[0]);
101 kfree(buf);
102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200105/**
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200106 * ide_find_setting - find a specific setting
107 * @st: setting table pointer
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200108 * @name: setting name
109 *
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200110 * Scan's the setting table for a matching entry and returns
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200111 * this or NULL if no entry is found. The caller must hold the
112 * setting semaphore
113 */
114
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200115static
116const struct ide_proc_devset *ide_find_setting(const struct ide_proc_devset *st,
117 char *name)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200118{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200119 while (st->name) {
120 if (strcmp(st->name, name) == 0)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200121 break;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200122 st++;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200123 }
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200124 return st->name ? st : NULL;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200125}
126
127/**
128 * ide_read_setting - read an IDE setting
129 * @drive: drive to read from
130 * @setting: drive setting
131 *
132 * Read a drive setting and return the value. The caller
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200133 * must hold the ide_setting_mtx when making this call.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200134 *
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
137 * be told apart
138 */
139
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200140static int ide_read_setting(ide_drive_t *drive,
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200141 const struct ide_proc_devset *setting)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200142{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200143 const struct ide_devset *ds = setting->setting;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200144 int val = -EINVAL;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200145
Bartlomiej Zolnierkiewicz1f473e92008-12-29 20:27:29 +0100146 if (ds->get)
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200147 val = ds->get(drive);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200148
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200149 return val;
150}
151
152/**
153 * ide_write_setting - read an IDE setting
154 * @drive: drive to read from
155 * @setting: drive setting
156 * @val: value
157 *
158 * Write a drive setting if it is possible. The caller
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200159 * must hold the ide_setting_mtx when making this call.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200160 *
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
163 * be told apart
164 *
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.
168 */
169
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200170static int ide_write_setting(ide_drive_t *drive,
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200171 const struct ide_proc_devset *setting, int val)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200172{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200173 const struct ide_devset *ds = setting->setting;
174
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200175 if (!capable(CAP_SYS_ADMIN))
176 return -EACCES;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200177 if (!ds->set)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200178 return -EPERM;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200179 if ((ds->flags & DS_SYNC)
180 && (val < setting->min || val > setting->max))
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200181 return -EINVAL;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200182 return ide_devset_execute(drive, ds, val);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200183}
184
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200185ide_devset_get(xfer_rate, current_speed);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200186
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200187static int set_xfer_rate (ide_drive_t *drive, int arg)
188{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100189 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200190
Bartlomiej Zolnierkiewicz3b2a5c72008-07-23 19:55:56 +0200191 if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200192 return -EINVAL;
193
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100194 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;
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200198 cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
199 cmd.valid.in.tf = IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000200 cmd.tf_flags = IDE_TFLAG_SET_XFER;
Bartlomiej Zolnierkiewicz34f5d5a2008-01-26 20:13:12 +0100201
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000202 return ide_no_data_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200203}
204
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200205ide_devset_rw(current_speed, xfer_rate);
206ide_devset_rw_field(init_speed, init_speed);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200207ide_devset_rw_flag(nice1, IDE_DFLAG_NICE1);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200208ide_devset_rw_field(number, dn);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200209
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200210static 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),
Hannes Eder71bfc7a2009-03-05 16:10:56 +0100220 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200221};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static void proc_ide_settings_warn(void)
224{
Marcin Slusarz01f1afa2009-09-22 16:29:00 -0700225 printk_once(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 "obsolete, and will be removed soon!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700229static int ide_settings_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200231 const struct ide_proc_devset *setting, *g, *d;
232 const struct ide_devset *ds;
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700233 ide_drive_t *drive = (ide_drive_t *) m->private;
234 int rc, mul_factor, div_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 proc_ide_settings_warn();
237
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200238 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200239 g = ide_generic_settings;
240 d = drive->settings;
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700241 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");
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200243 while (g->name || (d && d->name)) {
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200244 /* read settings in the alphabetical order */
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200245 if (g->name && d && d->name) {
246 if (strcmp(d->name, g->name) < 0)
247 setting = d++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200248 else
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200249 setting = g++;
250 } else if (d && d->name) {
251 setting = d++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200252 } else
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200253 setting = g++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200254 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
255 div_factor = setting->divf ? setting->divf(drive) : 1;
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700256 seq_printf(m, "%-24s", setting->name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200257 rc = ide_read_setting(drive, setting);
258 if (rc >= 0)
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700259 seq_printf(m, "%-16d", rc * mul_factor / div_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 else
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700261 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);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200263 ds = setting->setting;
264 if (ds->get)
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700265 seq_printf(m, "r");
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200266 if (ds->set)
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700267 seq_printf(m, "w");
268 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200270 mutex_unlock(&ide_setting_mtx);
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700271 return 0;
272}
273
274static int ide_settings_proc_open(struct inode *inode, struct file *file)
275{
Al Virod9dda782013-03-31 18:16:14 -0400276 return single_open(file, ide_settings_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279#define MAX_LEN 30
280
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700281static ssize_t ide_settings_proc_write(struct file *file, const char __user *buffer,
282 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Al Virod9dda782013-03-31 18:16:14 -0400284 ide_drive_t *drive = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 char name[MAX_LEN + 1];
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200286 int for_real = 0, mul_factor, div_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 unsigned long n;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200288
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200289 const struct ide_proc_devset *setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 char *buf, *s;
291
292 if (!capable(CAP_SYS_ADMIN))
293 return -EACCES;
294
295 proc_ide_settings_warn();
296
297 if (count >= PAGE_SIZE)
298 return -EINVAL;
299
300 s = buf = (char *)__get_free_page(GFP_USER);
301 if (!buf)
302 return -ENOMEM;
303
304 if (copy_from_user(buf, buffer, count)) {
305 free_page((unsigned long)buf);
306 return -EFAULT;
307 }
308
309 buf[count] = '\0';
310
311 /*
312 * Skip over leading whitespace
313 */
314 while (count && isspace(*s)) {
315 --count;
316 ++s;
317 }
318 /*
319 * Do one full pass to verify all parameters,
320 * then do another to actually write the new settings.
321 */
322 do {
323 char *p = s;
324 n = count;
325 while (n > 0) {
326 unsigned val;
327 char *q = p;
328
329 while (n > 0 && *p != ':') {
330 --n;
331 p++;
332 }
333 if (*p != ':')
334 goto parse_error;
335 if (p - q > MAX_LEN)
336 goto parse_error;
337 memcpy(name, q, p - q);
338 name[p - q] = 0;
339
340 if (n > 0) {
341 --n;
342 p++;
343 } else
344 goto parse_error;
345
346 val = simple_strtoul(p, &q, 10);
347 n -= q - p;
348 p = q;
349 if (n > 0 && !isspace(*p))
350 goto parse_error;
351 while (n > 0 && isspace(*p)) {
352 --n;
353 ++p;
354 }
355
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200356 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200357 /* generic settings first, then driver specific ones */
358 setting = ide_find_setting(ide_generic_settings, name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200359 if (!setting) {
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200360 if (drive->settings)
361 setting = ide_find_setting(drive->settings, name);
362 if (!setting) {
363 mutex_unlock(&ide_setting_mtx);
364 goto parse_error;
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200367 if (for_real) {
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);
371 }
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200372 mutex_unlock(&ide_setting_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374 } while (!for_real++);
375 free_page((unsigned long)buf);
376 return count;
377parse_error:
378 free_page((unsigned long)buf);
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700379 printk("%s(): parse error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -EINVAL;
381}
382
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700383static const struct file_operations ide_settings_proc_fops = {
384 .owner = THIS_MODULE,
385 .open = ide_settings_proc_open,
386 .read = seq_read,
387 .llseek = seq_lseek,
388 .release = single_release,
389 .write = ide_settings_proc_write,
390};
391
Christoph Hellwigec7d9c92018-04-13 21:25:54 +0200392int ide_capacity_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700394 seq_printf(m, "%llu\n", (long long)0x7fffffff);
395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
Christoph Hellwigec7d9c92018-04-13 21:25:54 +0200397EXPORT_SYMBOL_GPL(ide_capacity_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Christoph Hellwigec7d9c92018-04-13 21:25:54 +0200399int ide_geometry_proc_show(struct seq_file *m, void *v)
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700400{
401 ide_drive_t *drive = (ide_drive_t *) m->private;
402
403 seq_printf(m, "physical %d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 drive->cyl, drive->head, drive->sect);
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700405 seq_printf(m, "logical %d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 drive->bios_cyl, drive->bios_head, drive->bios_sect);
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
Christoph Hellwigec7d9c92018-04-13 21:25:54 +0200409EXPORT_SYMBOL(ide_geometry_proc_show);
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700410
411static int ide_dmodel_proc_show(struct seq_file *seq, void *v)
412{
413 ide_drive_t *drive = (ide_drive_t *) seq->private;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200414 char *m = (char *)&drive->id[ATA_ID_PROD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700416 seq_printf(seq, "%.40s\n", m[0] ? m : "(none)");
417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700420static int ide_driver_proc_show(struct seq_file *m, void *v)
421{
422 ide_drive_t *drive = (ide_drive_t *)m->private;
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100423 struct device *dev = &drive->gendev;
424 struct ide_driver *ide_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200426 if (dev->driver) {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100427 ide_drv = to_ide_driver(dev->driver);
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700428 seq_printf(m, "%s version %s\n",
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200429 dev->driver->name, ide_drv->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 } else
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700431 seq_printf(m, "ide-default version 0.9.newide\n");
432 return 0;
433}
434
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700435static int ide_media_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700437 ide_drive_t *drive = (ide_drive_t *) m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 const char *media;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 switch (drive->media) {
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200441 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700448 seq_puts(m, media);
449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700452static int ide_media_proc_open(struct inode *inode, struct file *file)
453{
Al Virod9dda782013-03-31 18:16:14 -0400454 return single_open(file, ide_media_proc_show, PDE_DATA(inode));
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700455}
456
457static const struct file_operations ide_media_proc_fops = {
458 .owner = THIS_MODULE,
459 .open = ide_media_proc_open,
460 .read = seq_read,
461 .llseek = seq_lseek,
462 .release = single_release,
463};
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static ide_proc_entry_t generic_drive_entries[] = {
Christoph Hellwigec7d9c92018-04-13 21:25:54 +0200466 { "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 },
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700470 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471};
472
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200473static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct proc_dir_entry *ent;
476
477 if (!dir || !p)
478 return;
479 while (p->name != NULL) {
Christoph Hellwigec7d9c92018-04-13 21:25:54 +0200480 ent = proc_create_single_data(p->name, p->mode, dir, p->show, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!ent) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 p++;
483 }
484}
485
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200486static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 if (!dir || !p)
489 return;
490 while (p->name != NULL) {
491 remove_proc_entry(p->name, dir);
492 p++;
493 }
494}
495
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100496void ide_proc_register_driver(ide_drive_t *drive, struct ide_driver *driver)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200497{
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200498 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200499 drive->settings = driver->proc_devsets(drive);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200500 mutex_unlock(&ide_setting_mtx);
501
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200502 ide_add_proc_entries(drive->proc, driver->proc_entries(drive), drive);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200503}
504
505EXPORT_SYMBOL(ide_proc_register_driver);
506
507/**
508 * ide_proc_unregister_driver - remove driver specific data
509 * @drive: drive
510 * @driver: driver
511 *
512 * Clean up the driver specific /proc files and IDE settings
513 * for a given drive.
514 *
Bartlomiej Zolnierkiewicz1f473e92008-12-29 20:27:29 +0100515 * Takes ide_setting_mtx.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200516 */
517
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100518void ide_proc_unregister_driver(ide_drive_t *drive, struct ide_driver *driver)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200519{
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200520 ide_remove_proc_entries(drive->proc, driver->proc_entries(drive));
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200521
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200522 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200523 /*
Bartlomiej Zolnierkiewicz1f473e92008-12-29 20:27:29 +0100524 * ide_setting_mtx protects both the settings list and the use
525 * of settings (we cannot take a setting out that is being used).
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200526 */
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200527 drive->settings = NULL;
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200528 mutex_unlock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200529}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200530EXPORT_SYMBOL(ide_proc_unregister_driver);
531
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +0100532void ide_proc_port_register_devices(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct proc_dir_entry *ent;
535 struct proc_dir_entry *parent = hwif->proc;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100536 ide_drive_t *drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 char name[64];
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100538 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100540 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz1902a252009-03-24 23:22:40 +0100541 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 continue;
543
544 drive->proc = proc_mkdir(drive->name, parent);
Christoph Hellwigec7d9c92018-04-13 21:25:54 +0200545 if (drive->proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
Christoph Hellwigf8ff6c72018-12-20 17:16:53 +0100547 proc_create_data("settings", S_IFREG|S_IRUSR|S_IWUSR,
Christoph Hellwigec7d9c92018-04-13 21:25:54 +0200548 drive->proc, &ide_settings_proc_fops,
549 drive);
550 }
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200551 sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 ent = proc_symlink(drive->name, proc_ide_root, name);
553 if (!ent) return;
554 }
555}
556
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200557void ide_proc_unregister_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 if (drive->proc) {
Christoph Hellwigec7d9c92018-04-13 21:25:54 +0200560 remove_proc_entry("settings", drive->proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 ide_remove_proc_entries(drive->proc, generic_drive_entries);
562 remove_proc_entry(drive->name, proc_ide_root);
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200563 remove_proc_entry(drive->name, drive->hwif->proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 drive->proc = NULL;
565 }
566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568static ide_proc_entry_t hwif_entries[] = {
Christoph Hellwigec7d9c92018-04-13 21:25:54 +0200569 { "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 },
Alexey Dobriyan6d703a82009-09-01 17:52:57 -0700572 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573};
574
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200575void ide_proc_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200577 if (!hwif->proc) {
578 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200580 if (!hwif->proc)
581 return;
582
583 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585}
586
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200587void ide_proc_unregister_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 if (hwif->proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 ide_remove_proc_entries(hwif->proc, hwif_entries);
591 remove_proc_entry(hwif->name, proc_ide_root);
592 hwif->proc = NULL;
593 }
594}
595
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200596static int proc_print_driver(struct device_driver *drv, void *data)
597{
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100598 struct ide_driver *ide_drv = to_ide_driver(drv);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200599 struct seq_file *s = data;
600
601 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
602
603 return 0;
604}
605
606static int ide_drivers_show(struct seq_file *s, void *p)
607{
Randy Dunlap349ae232006-10-03 01:14:23 -0700608 int err;
609
610 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
611 if (err < 0)
612 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200613 __func__, err);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200614 return 0;
615}
616
Yangtao Li94d0fb12018-11-30 21:20:48 -0500617DEFINE_SHOW_ATTRIBUTE(ide_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619void proc_ide_create(void)
620{
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200621 proc_ide_root = proc_mkdir("ide", NULL);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!proc_ide_root)
624 return;
625
Yangtao Li94d0fb12018-11-30 21:20:48 -0500626 proc_create("drivers", 0, proc_ide_root, &ide_drivers_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629void proc_ide_destroy(void)
630{
Zhang, Yanmin643bdc62005-05-16 21:53:11 -0700631 remove_proc_entry("drivers", proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 remove_proc_entry("ide", NULL);
633}