blob: 021de41655e68224a5b1274cbfd89d96bc2717d1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static int proc_ide_read_imodel
34 (char *page, char **start, off_t off, int count, int *eof, void *data)
35{
36 ide_hwif_t *hwif = (ide_hwif_t *) data;
37 int len;
38 const char *name;
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 switch (hwif->chipset) {
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020041 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;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020049 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;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020053 case ide_acorn: name = "acorn"; break;
54 default: name = "(unknown)"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
56 len = sprintf(page, "%s\n", name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020057 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60static int proc_ide_read_mate
61 (char *page, char **start, off_t off, int count, int *eof, void *data)
62{
63 ide_hwif_t *hwif = (ide_hwif_t *) data;
64 int len;
65
Bartlomiej Zolnierkiewicz4283e1b2008-06-30 20:14:45 +020066 if (hwif && hwif->mate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 len = sprintf(page, "%s\n", hwif->mate->name);
68 else
69 len = sprintf(page, "(none)\n");
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020070 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static int proc_ide_read_channel
74 (char *page, char **start, off_t off, int count, int *eof, void *data)
75{
76 ide_hwif_t *hwif = (ide_hwif_t *) data;
77 int len;
78
79 page[0] = hwif->channel ? '1' : '0';
80 page[1] = '\n';
81 len = 2;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020082 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85static int proc_ide_read_identify
86 (char *page, char **start, off_t off, int count, int *eof, void *data)
87{
88 ide_drive_t *drive = (ide_drive_t *)data;
89 int len = 0, i = 0;
90 int err = 0;
91
92 len = sprintf(page, "\n");
93
94 if (drive) {
Harvey Harrison7fa897b2008-07-24 22:53:34 +020095 __le16 *val = (__le16 *)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 err = taskfile_lib_get_identify(drive, page);
98 if (!err) {
Bartlomiej Zolnierkiewicz151a6702008-10-10 22:39:28 +020099 char *out = (char *)page + SECTOR_SIZE;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 page = out;
102 do {
103 out += sprintf(out, "%04x%c",
Harvey Harrison7fa897b2008-07-24 22:53:34 +0200104 le16_to_cpup(val), (++i & 7) ? ' ' : '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 val += 1;
Bartlomiej Zolnierkiewicz151a6702008-10-10 22:39:28 +0200106 } while (i < SECTOR_SIZE / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 len = out - page;
108 }
109 }
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200110 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200113/**
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200114 * ide_find_setting - find a specific setting
115 * @st: setting table pointer
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200116 * @name: setting name
117 *
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200118 * Scan's the setting table for a matching entry and returns
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200119 * this or NULL if no entry is found. The caller must hold the
120 * setting semaphore
121 */
122
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200123static
124const struct ide_proc_devset *ide_find_setting(const struct ide_proc_devset *st,
125 char *name)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200126{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200127 while (st->name) {
128 if (strcmp(st->name, name) == 0)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200129 break;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200130 st++;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200131 }
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200132 return st->name ? st : NULL;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200133}
134
135/**
136 * ide_read_setting - read an IDE setting
137 * @drive: drive to read from
138 * @setting: drive setting
139 *
140 * Read a drive setting and return the value. The caller
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200141 * must hold the ide_setting_mtx when making this call.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200142 *
143 * BUGS: the data return and error are the same return value
144 * so an error -EINVAL and true return of the same value cannot
145 * be told apart
146 */
147
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200148static int ide_read_setting(ide_drive_t *drive,
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200149 const struct ide_proc_devset *setting)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200150{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200151 const struct ide_devset *ds = setting->setting;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200152 int val = -EINVAL;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200153
Bartlomiej Zolnierkiewicz1f473e92008-12-29 20:27:29 +0100154 if (ds->get)
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200155 val = ds->get(drive);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200156
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200157 return val;
158}
159
160/**
161 * ide_write_setting - read an IDE setting
162 * @drive: drive to read from
163 * @setting: drive setting
164 * @val: value
165 *
166 * Write a drive setting if it is possible. The caller
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200167 * must hold the ide_setting_mtx when making this call.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200168 *
169 * BUGS: the data return and error are the same return value
170 * so an error -EINVAL and true return of the same value cannot
171 * be told apart
172 *
173 * FIXME: This should be changed to enqueue a special request
174 * to the driver to change settings, and then wait on a sema for completion.
175 * The current scheme of polling is kludgy, though safe enough.
176 */
177
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200178static int ide_write_setting(ide_drive_t *drive,
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200179 const struct ide_proc_devset *setting, int val)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200180{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200181 const struct ide_devset *ds = setting->setting;
182
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200183 if (!capable(CAP_SYS_ADMIN))
184 return -EACCES;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200185 if (!ds->set)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200186 return -EPERM;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200187 if ((ds->flags & DS_SYNC)
188 && (val < setting->min || val > setting->max))
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200189 return -EINVAL;
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200190 return ide_devset_execute(drive, ds, val);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200191}
192
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200193ide_devset_get(xfer_rate, current_speed);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200194
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200195static int set_xfer_rate (ide_drive_t *drive, int arg)
196{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100197 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200198
Bartlomiej Zolnierkiewicz3b2a5c72008-07-23 19:55:56 +0200199 if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200200 return -EINVAL;
201
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100202 memset(&cmd, 0, sizeof(cmd));
203 cmd.tf.command = ATA_CMD_SET_FEATURES;
204 cmd.tf.feature = SETFEATURES_XFER;
205 cmd.tf.nsect = (u8)arg;
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200206 cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
207 cmd.valid.in.tf = IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000208 cmd.tf_flags = IDE_TFLAG_SET_XFER;
Bartlomiej Zolnierkiewicz34f5d5a2008-01-26 20:13:12 +0100209
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000210 return ide_no_data_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200211}
212
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200213ide_devset_rw(current_speed, xfer_rate);
214ide_devset_rw_field(init_speed, init_speed);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200215ide_devset_rw_flag(nice1, IDE_DFLAG_NICE1);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200216ide_devset_rw_field(number, dn);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200217
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200218static const struct ide_proc_devset ide_generic_settings[] = {
219 IDE_PROC_DEVSET(current_speed, 0, 70),
220 IDE_PROC_DEVSET(init_speed, 0, 70),
221 IDE_PROC_DEVSET(io_32bit, 0, 1 + (SUPPORT_VLB_SYNC << 1)),
222 IDE_PROC_DEVSET(keepsettings, 0, 1),
223 IDE_PROC_DEVSET(nice1, 0, 1),
224 IDE_PROC_DEVSET(number, 0, 3),
225 IDE_PROC_DEVSET(pio_mode, 0, 255),
226 IDE_PROC_DEVSET(unmaskirq, 0, 1),
227 IDE_PROC_DEVSET(using_dma, 0, 1),
Hannes Eder71bfc7a2009-03-05 16:10:56 +0100228 { NULL },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200229};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static void proc_ide_settings_warn(void)
232{
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200233 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 if (warned)
236 return;
237
238 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
239 "obsolete, and will be removed soon!\n");
240 warned = 1;
241}
242
243static int proc_ide_read_settings
244 (char *page, char **start, off_t off, int count, int *eof, void *data)
245{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200246 const struct ide_proc_devset *setting, *g, *d;
247 const struct ide_devset *ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 ide_drive_t *drive = (ide_drive_t *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 char *out = page;
250 int len, rc, mul_factor, div_factor;
251
252 proc_ide_settings_warn();
253
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200254 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200255 g = ide_generic_settings;
256 d = drive->settings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
258 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200259 while (g->name || (d && d->name)) {
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200260 /* read settings in the alphabetical order */
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200261 if (g->name && d && d->name) {
262 if (strcmp(d->name, g->name) < 0)
263 setting = d++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200264 else
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200265 setting = g++;
266 } else if (d && d->name) {
267 setting = d++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200268 } else
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200269 setting = g++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200270 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
271 div_factor = setting->divf ? setting->divf(drive) : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 out += sprintf(out, "%-24s", setting->name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200273 rc = ide_read_setting(drive, setting);
274 if (rc >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
276 else
277 out += sprintf(out, "%-16s", "write-only");
278 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200279 ds = setting->setting;
280 if (ds->get)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 out += sprintf(out, "r");
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200282 if (ds->set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 out += sprintf(out, "w");
284 out += sprintf(out, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286 len = out - page;
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200287 mutex_unlock(&ide_setting_mtx);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200288 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291#define MAX_LEN 30
292
293static int proc_ide_write_settings(struct file *file, const char __user *buffer,
294 unsigned long count, void *data)
295{
296 ide_drive_t *drive = (ide_drive_t *) data;
297 char name[MAX_LEN + 1];
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200298 int for_real = 0, mul_factor, div_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unsigned long n;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200300
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200301 const struct ide_proc_devset *setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 char *buf, *s;
303
304 if (!capable(CAP_SYS_ADMIN))
305 return -EACCES;
306
307 proc_ide_settings_warn();
308
309 if (count >= PAGE_SIZE)
310 return -EINVAL;
311
312 s = buf = (char *)__get_free_page(GFP_USER);
313 if (!buf)
314 return -ENOMEM;
315
316 if (copy_from_user(buf, buffer, count)) {
317 free_page((unsigned long)buf);
318 return -EFAULT;
319 }
320
321 buf[count] = '\0';
322
323 /*
324 * Skip over leading whitespace
325 */
326 while (count && isspace(*s)) {
327 --count;
328 ++s;
329 }
330 /*
331 * Do one full pass to verify all parameters,
332 * then do another to actually write the new settings.
333 */
334 do {
335 char *p = s;
336 n = count;
337 while (n > 0) {
338 unsigned val;
339 char *q = p;
340
341 while (n > 0 && *p != ':') {
342 --n;
343 p++;
344 }
345 if (*p != ':')
346 goto parse_error;
347 if (p - q > MAX_LEN)
348 goto parse_error;
349 memcpy(name, q, p - q);
350 name[p - q] = 0;
351
352 if (n > 0) {
353 --n;
354 p++;
355 } else
356 goto parse_error;
357
358 val = simple_strtoul(p, &q, 10);
359 n -= q - p;
360 p = q;
361 if (n > 0 && !isspace(*p))
362 goto parse_error;
363 while (n > 0 && isspace(*p)) {
364 --n;
365 ++p;
366 }
367
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200368 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200369 /* generic settings first, then driver specific ones */
370 setting = ide_find_setting(ide_generic_settings, name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200371 if (!setting) {
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200372 if (drive->settings)
373 setting = ide_find_setting(drive->settings, name);
374 if (!setting) {
375 mutex_unlock(&ide_setting_mtx);
376 goto parse_error;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200379 if (for_real) {
380 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
381 div_factor = setting->divf ? setting->divf(drive) : 1;
382 ide_write_setting(drive, setting, val * div_factor / mul_factor);
383 }
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200384 mutex_unlock(&ide_setting_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 } while (!for_real++);
387 free_page((unsigned long)buf);
388 return count;
389parse_error:
390 free_page((unsigned long)buf);
391 printk("proc_ide_write_settings(): parse error\n");
392 return -EINVAL;
393}
394
395int proc_ide_read_capacity
396 (char *page, char **start, off_t off, int count, int *eof, void *data)
397{
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200398 int len = sprintf(page, "%llu\n", (long long)0x7fffffff);
399 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
403
404int proc_ide_read_geometry
405 (char *page, char **start, off_t off, int count, int *eof, void *data)
406{
407 ide_drive_t *drive = (ide_drive_t *) data;
408 char *out = page;
409 int len;
410
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200411 out += sprintf(out, "physical %d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 drive->cyl, drive->head, drive->sect);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200413 out += sprintf(out, "logical %d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 drive->bios_cyl, drive->bios_head, drive->bios_sect);
415
416 len = out - page;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200417 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420EXPORT_SYMBOL(proc_ide_read_geometry);
421
422static int proc_ide_read_dmodel
423 (char *page, char **start, off_t off, int count, int *eof, void *data)
424{
425 ide_drive_t *drive = (ide_drive_t *) data;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200426 char *m = (char *)&drive->id[ATA_ID_PROD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 int len;
428
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200429 len = sprintf(page, "%.40s\n", m[0] ? m : "(none)");
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200430 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433static int proc_ide_read_driver
434 (char *page, char **start, off_t off, int count, int *eof, void *data)
435{
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100436 ide_drive_t *drive = (ide_drive_t *)data;
437 struct device *dev = &drive->gendev;
438 struct ide_driver *ide_drv;
439 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200441 if (dev->driver) {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100442 ide_drv = to_ide_driver(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 len = sprintf(page, "%s version %s\n",
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200444 dev->driver->name, ide_drv->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else
446 len = sprintf(page, "ide-default version 0.9.newide\n");
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200447 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200450static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
451{
452 struct device *dev = &drive->gendev;
453 int ret = 1;
Randy Dunlap349ae232006-10-03 01:14:23 -0700454 int err;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200455
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200456 device_release_driver(dev);
457 /* FIXME: device can still be in use by previous driver */
458 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
Randy Dunlap349ae232006-10-03 01:14:23 -0700459 err = device_attach(dev);
460 if (err < 0)
461 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200462 __func__, err);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200463 drive->driver_req[0] = 0;
Randy Dunlap349ae232006-10-03 01:14:23 -0700464 if (dev->driver == NULL) {
465 err = device_attach(dev);
466 if (err < 0)
467 printk(KERN_WARNING
468 "IDE: %s: device_attach(2) error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200469 __func__, err);
Randy Dunlap349ae232006-10-03 01:14:23 -0700470 }
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200471 if (dev->driver && !strcmp(dev->driver->name, driver))
472 ret = 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200473
474 return ret;
475}
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477static int proc_ide_write_driver
478 (struct file *file, const char __user *buffer, unsigned long count, void *data)
479{
480 ide_drive_t *drive = (ide_drive_t *) data;
481 char name[32];
482
483 if (!capable(CAP_SYS_ADMIN))
484 return -EACCES;
485 if (count > 31)
486 count = 31;
487 if (copy_from_user(name, buffer, count))
488 return -EFAULT;
489 name[count] = '\0';
490 if (ide_replace_subdriver(drive, name))
491 return -EINVAL;
492 return count;
493}
494
495static int proc_ide_read_media
496 (char *page, char **start, off_t off, int count, int *eof, void *data)
497{
498 ide_drive_t *drive = (ide_drive_t *) data;
499 const char *media;
500 int len;
501
502 switch (drive->media) {
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200503 case ide_disk: media = "disk\n"; break;
504 case ide_cdrom: media = "cdrom\n"; break;
505 case ide_tape: media = "tape\n"; break;
506 case ide_floppy: media = "floppy\n"; break;
507 case ide_optical: media = "optical\n"; break;
508 default: media = "UNKNOWN\n"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200510 strcpy(page, media);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 len = strlen(media);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200512 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515static ide_proc_entry_t generic_drive_entries[] = {
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200516 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver,
517 proc_ide_write_driver },
518 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
519 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
520 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
521 { "settings", S_IFREG|S_IRUSR|S_IWUSR, proc_ide_read_settings,
522 proc_ide_write_settings },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 { NULL, 0, NULL, NULL }
524};
525
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200526static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct proc_dir_entry *ent;
529
530 if (!dir || !p)
531 return;
532 while (p->name != NULL) {
533 ent = create_proc_entry(p->name, p->mode, dir);
534 if (!ent) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ent->data = data;
536 ent->read_proc = p->read_proc;
537 ent->write_proc = p->write_proc;
538 p++;
539 }
540}
541
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200542static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 if (!dir || !p)
545 return;
546 while (p->name != NULL) {
547 remove_proc_entry(p->name, dir);
548 p++;
549 }
550}
551
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100552void ide_proc_register_driver(ide_drive_t *drive, struct ide_driver *driver)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200553{
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200554 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200555 drive->settings = driver->proc_devsets(drive);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200556 mutex_unlock(&ide_setting_mtx);
557
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200558 ide_add_proc_entries(drive->proc, driver->proc_entries(drive), drive);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200559}
560
561EXPORT_SYMBOL(ide_proc_register_driver);
562
563/**
564 * ide_proc_unregister_driver - remove driver specific data
565 * @drive: drive
566 * @driver: driver
567 *
568 * Clean up the driver specific /proc files and IDE settings
569 * for a given drive.
570 *
Bartlomiej Zolnierkiewicz1f473e92008-12-29 20:27:29 +0100571 * Takes ide_setting_mtx.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200572 */
573
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100574void ide_proc_unregister_driver(ide_drive_t *drive, struct ide_driver *driver)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200575{
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200576 ide_remove_proc_entries(drive->proc, driver->proc_entries(drive));
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200577
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200578 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200579 /*
Bartlomiej Zolnierkiewicz1f473e92008-12-29 20:27:29 +0100580 * ide_setting_mtx protects both the settings list and the use
581 * of settings (we cannot take a setting out that is being used).
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200582 */
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200583 drive->settings = NULL;
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200584 mutex_unlock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200585}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200586EXPORT_SYMBOL(ide_proc_unregister_driver);
587
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +0100588void ide_proc_port_register_devices(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct proc_dir_entry *ent;
591 struct proc_dir_entry *parent = hwif->proc;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100592 ide_drive_t *drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 char name[64];
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100594 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100596 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz1902a252009-03-24 23:22:40 +0100597 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 continue;
599
600 drive->proc = proc_mkdir(drive->name, parent);
601 if (drive->proc)
602 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200603 sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ent = proc_symlink(drive->name, proc_ide_root, name);
605 if (!ent) return;
606 }
607}
608
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200609void ide_proc_unregister_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 if (drive->proc) {
612 ide_remove_proc_entries(drive->proc, generic_drive_entries);
613 remove_proc_entry(drive->name, proc_ide_root);
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200614 remove_proc_entry(drive->name, drive->hwif->proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 drive->proc = NULL;
616 }
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619static ide_proc_entry_t hwif_entries[] = {
620 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
621 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
622 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
623 { NULL, 0, NULL, NULL }
624};
625
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200626void ide_proc_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200628 if (!hwif->proc) {
629 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200631 if (!hwif->proc)
632 return;
633
634 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636}
637
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200638void ide_proc_unregister_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 if (hwif->proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ide_remove_proc_entries(hwif->proc, hwif_entries);
642 remove_proc_entry(hwif->name, proc_ide_root);
643 hwif->proc = NULL;
644 }
645}
646
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200647static int proc_print_driver(struct device_driver *drv, void *data)
648{
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100649 struct ide_driver *ide_drv = to_ide_driver(drv);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200650 struct seq_file *s = data;
651
652 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
653
654 return 0;
655}
656
657static int ide_drivers_show(struct seq_file *s, void *p)
658{
Randy Dunlap349ae232006-10-03 01:14:23 -0700659 int err;
660
661 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
662 if (err < 0)
663 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200664 __func__, err);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200665 return 0;
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static int ide_drivers_open(struct inode *inode, struct file *file)
669{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200670 return single_open(file, &ide_drivers_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200672
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800673static const struct file_operations ide_drivers_operations = {
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700674 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 .open = ide_drivers_open,
676 .read = seq_read,
677 .llseek = seq_lseek,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200678 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679};
680
681void proc_ide_create(void)
682{
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200683 proc_ide_root = proc_mkdir("ide", NULL);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!proc_ide_root)
686 return;
687
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700688 proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691void proc_ide_destroy(void)
692{
Zhang, Yanmin643bdc62005-05-16 21:53:11 -0700693 remove_proc_entry("drivers", proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 remove_proc_entry("ide", NULL);
695}