blob: a14e2938e4f36c0b6bb77d9c269a0a4b6e7d8bc7 [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 Zolnierkiewicz34f5d5a2008-01-26 20:13:12 +0100197 ide_task_t task;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200198 int err;
199
Bartlomiej Zolnierkiewicz3b2a5c72008-07-23 19:55:56 +0200200 if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200201 return -EINVAL;
202
Bartlomiej Zolnierkiewicz34f5d5a2008-01-26 20:13:12 +0100203 memset(&task, 0, sizeof(task));
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200204 task.tf.command = ATA_CMD_SET_FEATURES;
Bartlomiej Zolnierkiewicz34f5d5a2008-01-26 20:13:12 +0100205 task.tf.feature = SETFEATURES_XFER;
206 task.tf.nsect = (u8)arg;
207 task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT |
208 IDE_TFLAG_IN_NSECT;
209
210 err = ide_no_data_taskfile(drive, &task);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200211
Bartlomiej Zolnierkiewicz3b2a5c72008-07-23 19:55:56 +0200212 if (!err) {
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200213 ide_set_xfer_rate(drive, (u8) arg);
214 ide_driveid_update(drive);
215 }
216 return err;
217}
218
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200219ide_devset_rw(current_speed, xfer_rate);
220ide_devset_rw_field(init_speed, init_speed);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200221ide_devset_rw_flag(nice1, IDE_DFLAG_NICE1);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200222ide_devset_rw_field(number, dn);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200223
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200224static const struct ide_proc_devset ide_generic_settings[] = {
225 IDE_PROC_DEVSET(current_speed, 0, 70),
226 IDE_PROC_DEVSET(init_speed, 0, 70),
227 IDE_PROC_DEVSET(io_32bit, 0, 1 + (SUPPORT_VLB_SYNC << 1)),
228 IDE_PROC_DEVSET(keepsettings, 0, 1),
229 IDE_PROC_DEVSET(nice1, 0, 1),
230 IDE_PROC_DEVSET(number, 0, 3),
231 IDE_PROC_DEVSET(pio_mode, 0, 255),
232 IDE_PROC_DEVSET(unmaskirq, 0, 1),
233 IDE_PROC_DEVSET(using_dma, 0, 1),
234 { 0 },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200235};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static void proc_ide_settings_warn(void)
238{
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200239 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (warned)
242 return;
243
244 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
245 "obsolete, and will be removed soon!\n");
246 warned = 1;
247}
248
249static int proc_ide_read_settings
250 (char *page, char **start, off_t off, int count, int *eof, void *data)
251{
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200252 const struct ide_proc_devset *setting, *g, *d;
253 const struct ide_devset *ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 ide_drive_t *drive = (ide_drive_t *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 char *out = page;
256 int len, rc, mul_factor, div_factor;
257
258 proc_ide_settings_warn();
259
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200260 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200261 g = ide_generic_settings;
262 d = drive->settings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
264 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200265 while (g->name || (d && d->name)) {
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200266 /* read settings in the alphabetical order */
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200267 if (g->name && d && d->name) {
268 if (strcmp(d->name, g->name) < 0)
269 setting = d++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200270 else
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200271 setting = g++;
272 } else if (d && d->name) {
273 setting = d++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200274 } else
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200275 setting = g++;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200276 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
277 div_factor = setting->divf ? setting->divf(drive) : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 out += sprintf(out, "%-24s", setting->name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200279 rc = ide_read_setting(drive, setting);
280 if (rc >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
282 else
283 out += sprintf(out, "%-16s", "write-only");
284 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 +0200285 ds = setting->setting;
286 if (ds->get)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 out += sprintf(out, "r");
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200288 if (ds->set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 out += sprintf(out, "w");
290 out += sprintf(out, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292 len = out - page;
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200293 mutex_unlock(&ide_setting_mtx);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200294 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297#define MAX_LEN 30
298
299static int proc_ide_write_settings(struct file *file, const char __user *buffer,
300 unsigned long count, void *data)
301{
302 ide_drive_t *drive = (ide_drive_t *) data;
303 char name[MAX_LEN + 1];
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200304 int for_real = 0, mul_factor, div_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 unsigned long n;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200306
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200307 const struct ide_proc_devset *setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 char *buf, *s;
309
310 if (!capable(CAP_SYS_ADMIN))
311 return -EACCES;
312
313 proc_ide_settings_warn();
314
315 if (count >= PAGE_SIZE)
316 return -EINVAL;
317
318 s = buf = (char *)__get_free_page(GFP_USER);
319 if (!buf)
320 return -ENOMEM;
321
322 if (copy_from_user(buf, buffer, count)) {
323 free_page((unsigned long)buf);
324 return -EFAULT;
325 }
326
327 buf[count] = '\0';
328
329 /*
330 * Skip over leading whitespace
331 */
332 while (count && isspace(*s)) {
333 --count;
334 ++s;
335 }
336 /*
337 * Do one full pass to verify all parameters,
338 * then do another to actually write the new settings.
339 */
340 do {
341 char *p = s;
342 n = count;
343 while (n > 0) {
344 unsigned val;
345 char *q = p;
346
347 while (n > 0 && *p != ':') {
348 --n;
349 p++;
350 }
351 if (*p != ':')
352 goto parse_error;
353 if (p - q > MAX_LEN)
354 goto parse_error;
355 memcpy(name, q, p - q);
356 name[p - q] = 0;
357
358 if (n > 0) {
359 --n;
360 p++;
361 } else
362 goto parse_error;
363
364 val = simple_strtoul(p, &q, 10);
365 n -= q - p;
366 p = q;
367 if (n > 0 && !isspace(*p))
368 goto parse_error;
369 while (n > 0 && isspace(*p)) {
370 --n;
371 ++p;
372 }
373
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200374 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200375 /* generic settings first, then driver specific ones */
376 setting = ide_find_setting(ide_generic_settings, name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200377 if (!setting) {
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200378 if (drive->settings)
379 setting = ide_find_setting(drive->settings, name);
380 if (!setting) {
381 mutex_unlock(&ide_setting_mtx);
382 goto parse_error;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200385 if (for_real) {
386 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
387 div_factor = setting->divf ? setting->divf(drive) : 1;
388 ide_write_setting(drive, setting, val * div_factor / mul_factor);
389 }
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200390 mutex_unlock(&ide_setting_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 } while (!for_real++);
393 free_page((unsigned long)buf);
394 return count;
395parse_error:
396 free_page((unsigned long)buf);
397 printk("proc_ide_write_settings(): parse error\n");
398 return -EINVAL;
399}
400
401int proc_ide_read_capacity
402 (char *page, char **start, off_t off, int count, int *eof, void *data)
403{
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200404 int len = sprintf(page, "%llu\n", (long long)0x7fffffff);
405 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
409
410int proc_ide_read_geometry
411 (char *page, char **start, off_t off, int count, int *eof, void *data)
412{
413 ide_drive_t *drive = (ide_drive_t *) data;
414 char *out = page;
415 int len;
416
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200417 out += sprintf(out, "physical %d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 drive->cyl, drive->head, drive->sect);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200419 out += sprintf(out, "logical %d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 drive->bios_cyl, drive->bios_head, drive->bios_sect);
421
422 len = out - page;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200423 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426EXPORT_SYMBOL(proc_ide_read_geometry);
427
428static int proc_ide_read_dmodel
429 (char *page, char **start, off_t off, int count, int *eof, void *data)
430{
431 ide_drive_t *drive = (ide_drive_t *) data;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200432 char *m = (char *)&drive->id[ATA_ID_PROD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int len;
434
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200435 len = sprintf(page, "%.40s\n", m[0] ? m : "(none)");
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200436 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439static int proc_ide_read_driver
440 (char *page, char **start, off_t off, int count, int *eof, void *data)
441{
442 ide_drive_t *drive = (ide_drive_t *) data;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200443 struct device *dev = &drive->gendev;
444 ide_driver_t *ide_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int len;
446
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200447 if (dev->driver) {
448 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 len = sprintf(page, "%s version %s\n",
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200450 dev->driver->name, ide_drv->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 } else
452 len = sprintf(page, "ide-default version 0.9.newide\n");
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200453 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200456static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
457{
458 struct device *dev = &drive->gendev;
459 int ret = 1;
Randy Dunlap349ae232006-10-03 01:14:23 -0700460 int err;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200461
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200462 device_release_driver(dev);
463 /* FIXME: device can still be in use by previous driver */
464 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
Randy Dunlap349ae232006-10-03 01:14:23 -0700465 err = device_attach(dev);
466 if (err < 0)
467 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200468 __func__, err);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200469 drive->driver_req[0] = 0;
Randy Dunlap349ae232006-10-03 01:14:23 -0700470 if (dev->driver == NULL) {
471 err = device_attach(dev);
472 if (err < 0)
473 printk(KERN_WARNING
474 "IDE: %s: device_attach(2) error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200475 __func__, err);
Randy Dunlap349ae232006-10-03 01:14:23 -0700476 }
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200477 if (dev->driver && !strcmp(dev->driver->name, driver))
478 ret = 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200479
480 return ret;
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static int proc_ide_write_driver
484 (struct file *file, const char __user *buffer, unsigned long count, void *data)
485{
486 ide_drive_t *drive = (ide_drive_t *) data;
487 char name[32];
488
489 if (!capable(CAP_SYS_ADMIN))
490 return -EACCES;
491 if (count > 31)
492 count = 31;
493 if (copy_from_user(name, buffer, count))
494 return -EFAULT;
495 name[count] = '\0';
496 if (ide_replace_subdriver(drive, name))
497 return -EINVAL;
498 return count;
499}
500
501static int proc_ide_read_media
502 (char *page, char **start, off_t off, int count, int *eof, void *data)
503{
504 ide_drive_t *drive = (ide_drive_t *) data;
505 const char *media;
506 int len;
507
508 switch (drive->media) {
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200509 case ide_disk: media = "disk\n"; break;
510 case ide_cdrom: media = "cdrom\n"; break;
511 case ide_tape: media = "tape\n"; break;
512 case ide_floppy: media = "floppy\n"; break;
513 case ide_optical: media = "optical\n"; break;
514 default: media = "UNKNOWN\n"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200516 strcpy(page, media);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 len = strlen(media);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200518 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521static ide_proc_entry_t generic_drive_entries[] = {
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200522 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver,
523 proc_ide_write_driver },
524 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
525 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
526 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
527 { "settings", S_IFREG|S_IRUSR|S_IWUSR, proc_ide_read_settings,
528 proc_ide_write_settings },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 { NULL, 0, NULL, NULL }
530};
531
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200532static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct proc_dir_entry *ent;
535
536 if (!dir || !p)
537 return;
538 while (p->name != NULL) {
539 ent = create_proc_entry(p->name, p->mode, dir);
540 if (!ent) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 ent->data = data;
542 ent->read_proc = p->read_proc;
543 ent->write_proc = p->write_proc;
544 p++;
545 }
546}
547
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200548static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 if (!dir || !p)
551 return;
552 while (p->name != NULL) {
553 remove_proc_entry(p->name, dir);
554 p++;
555 }
556}
557
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200558void ide_proc_register_driver(ide_drive_t *drive, ide_driver_t *driver)
559{
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200560 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200561 drive->settings = driver->proc_devsets(drive);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200562 mutex_unlock(&ide_setting_mtx);
563
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200564 ide_add_proc_entries(drive->proc, driver->proc_entries(drive), drive);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200565}
566
567EXPORT_SYMBOL(ide_proc_register_driver);
568
569/**
570 * ide_proc_unregister_driver - remove driver specific data
571 * @drive: drive
572 * @driver: driver
573 *
574 * Clean up the driver specific /proc files and IDE settings
575 * for a given drive.
576 *
Bartlomiej Zolnierkiewicz1f473e92008-12-29 20:27:29 +0100577 * Takes ide_setting_mtx.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200578 */
579
580void ide_proc_unregister_driver(ide_drive_t *drive, ide_driver_t *driver)
581{
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200582 ide_remove_proc_entries(drive->proc, driver->proc_entries(drive));
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200583
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200584 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200585 /*
Bartlomiej Zolnierkiewicz1f473e92008-12-29 20:27:29 +0100586 * ide_setting_mtx protects both the settings list and the use
587 * of settings (we cannot take a setting out that is being used).
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200588 */
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200589 drive->settings = NULL;
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200590 mutex_unlock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200591}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200592EXPORT_SYMBOL(ide_proc_unregister_driver);
593
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +0100594void ide_proc_port_register_devices(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 int d;
597 struct proc_dir_entry *ent;
598 struct proc_dir_entry *parent = hwif->proc;
599 char name[64];
600
601 for (d = 0; d < MAX_DRIVES; d++) {
602 ide_drive_t *drive = &hwif->drives[d];
603
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200604 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0 || drive->proc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 continue;
606
607 drive->proc = proc_mkdir(drive->name, parent);
608 if (drive->proc)
609 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200610 sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 ent = proc_symlink(drive->name, proc_ide_root, name);
612 if (!ent) return;
613 }
614}
615
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200616void ide_proc_unregister_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 if (drive->proc) {
619 ide_remove_proc_entries(drive->proc, generic_drive_entries);
620 remove_proc_entry(drive->name, proc_ide_root);
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200621 remove_proc_entry(drive->name, drive->hwif->proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 drive->proc = NULL;
623 }
624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626static ide_proc_entry_t hwif_entries[] = {
627 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
628 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
629 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
630 { NULL, 0, NULL, NULL }
631};
632
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200633void ide_proc_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200635 if (!hwif->proc) {
636 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200638 if (!hwif->proc)
639 return;
640
641 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643}
644
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200645void ide_proc_unregister_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 if (hwif->proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ide_remove_proc_entries(hwif->proc, hwif_entries);
649 remove_proc_entry(hwif->name, proc_ide_root);
650 hwif->proc = NULL;
651 }
652}
653
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200654static int proc_print_driver(struct device_driver *drv, void *data)
655{
656 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
657 struct seq_file *s = data;
658
659 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
660
661 return 0;
662}
663
664static int ide_drivers_show(struct seq_file *s, void *p)
665{
Randy Dunlap349ae232006-10-03 01:14:23 -0700666 int err;
667
668 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
669 if (err < 0)
670 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200671 __func__, err);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200672 return 0;
673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675static int ide_drivers_open(struct inode *inode, struct file *file)
676{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200677 return single_open(file, &ide_drivers_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200679
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800680static const struct file_operations ide_drivers_operations = {
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700681 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 .open = ide_drivers_open,
683 .read = seq_read,
684 .llseek = seq_lseek,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200685 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686};
687
688void proc_ide_create(void)
689{
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200690 proc_ide_root = proc_mkdir("ide", NULL);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (!proc_ide_root)
693 return;
694
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700695 proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
698void proc_ide_destroy(void)
699{
Zhang, Yanmin643bdc62005-05-16 21:53:11 -0700700 remove_proc_entry("drivers", proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 remove_proc_entry("ide", NULL);
702}