blob: 6489c647be824263e4bd660c1f7ceb4818dde1a0 [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
3 * Copyright (C) 2003 Red Hat <alan@redhat.com>
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;
49 case ide_rz1000: name = "rz1000"; break;
50 case ide_trm290: name = "trm290"; break;
51 case ide_cmd646: name = "cmd646"; break;
52 case ide_cy82c693: name = "cy82c693"; break;
53 case ide_4drives: name = "4drives"; break;
54 case ide_pmac: name = "mac-io"; break;
55 case ide_au1xxx: name = "au1xxx"; break;
56 case ide_palm3710: name = "palm3710"; break;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020057 case ide_acorn: name = "acorn"; break;
58 default: name = "(unknown)"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60 len = sprintf(page, "%s\n", name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020061 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64static int proc_ide_read_mate
65 (char *page, char **start, off_t off, int count, int *eof, void *data)
66{
67 ide_hwif_t *hwif = (ide_hwif_t *) data;
68 int len;
69
Bartlomiej Zolnierkiewicz4283e1b2008-06-30 20:14:45 +020070 if (hwif && hwif->mate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 len = sprintf(page, "%s\n", hwif->mate->name);
72 else
73 len = sprintf(page, "(none)\n");
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020074 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static int proc_ide_read_channel
78 (char *page, char **start, off_t off, int count, int *eof, void *data)
79{
80 ide_hwif_t *hwif = (ide_hwif_t *) data;
81 int len;
82
83 page[0] = hwif->channel ? '1' : '0';
84 page[1] = '\n';
85 len = 2;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +020086 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89static int proc_ide_read_identify
90 (char *page, char **start, off_t off, int count, int *eof, void *data)
91{
92 ide_drive_t *drive = (ide_drive_t *)data;
93 int len = 0, i = 0;
94 int err = 0;
95
96 len = sprintf(page, "\n");
97
98 if (drive) {
Harvey Harrison7fa897b2008-07-24 22:53:34 +020099 __le16 *val = (__le16 *)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 err = taskfile_lib_get_identify(drive, page);
102 if (!err) {
Bartlomiej Zolnierkiewicz151a6702008-10-10 22:39:28 +0200103 char *out = (char *)page + SECTOR_SIZE;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 page = out;
106 do {
107 out += sprintf(out, "%04x%c",
Harvey Harrison7fa897b2008-07-24 22:53:34 +0200108 le16_to_cpup(val), (++i & 7) ? ' ' : '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 val += 1;
Bartlomiej Zolnierkiewicz151a6702008-10-10 22:39:28 +0200110 } while (i < SECTOR_SIZE / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 len = out - page;
112 }
113 }
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200114 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200117/**
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200118 * ide_find_setting - find a specific setting
119 * @st: setting table pointer
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200120 * @name: setting name
121 *
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200122 * Scan's the setting table for a matching entry and returns
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200123 * this or NULL if no entry is found. The caller must hold the
124 * setting semaphore
125 */
126
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200127static const struct ide_devset *ide_find_setting(const struct ide_devset **st,
128 char *name)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200129{
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200130 while (*st) {
131 if (strcmp((*st)->name, name) == 0)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200132 break;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200133 st++;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200134 }
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200135 return *st;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200136}
137
138/**
139 * ide_read_setting - read an IDE setting
140 * @drive: drive to read from
141 * @setting: drive setting
142 *
143 * Read a drive setting and return the value. The caller
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200144 * must hold the ide_setting_mtx when making this call.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200145 *
146 * BUGS: the data return and error are the same return value
147 * so an error -EINVAL and true return of the same value cannot
148 * be told apart
149 */
150
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200151static int ide_read_setting(ide_drive_t *drive,
152 const struct ide_devset *setting)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200153{
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200154 int val = -EINVAL;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200155
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200156 if ((setting->flags & S_READ)) {
157 unsigned long flags;
158
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200159 spin_lock_irqsave(&ide_lock, flags);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200160 val = setting->get(drive);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200161 spin_unlock_irqrestore(&ide_lock, flags);
162 }
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200163
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200164 return val;
165}
166
167/**
168 * ide_write_setting - read an IDE setting
169 * @drive: drive to read from
170 * @setting: drive setting
171 * @val: value
172 *
173 * Write a drive setting if it is possible. The caller
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200174 * must hold the ide_setting_mtx when making this call.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200175 *
176 * BUGS: the data return and error are the same return value
177 * so an error -EINVAL and true return of the same value cannot
178 * be told apart
179 *
180 * FIXME: This should be changed to enqueue a special request
181 * to the driver to change settings, and then wait on a sema for completion.
182 * The current scheme of polling is kludgy, though safe enough.
183 */
184
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200185static int ide_write_setting(ide_drive_t *drive,
186 const struct ide_devset *setting, int val)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200187{
188 if (!capable(CAP_SYS_ADMIN))
189 return -EACCES;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200190 if (setting->set && (setting->flags & S_NOLOCK))
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200191 return setting->set(drive, val);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200192 if (!(setting->flags & S_WRITE))
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200193 return -EPERM;
194 if (val < setting->min || val > setting->max)
195 return -EINVAL;
196 if (ide_spin_wait_hwgroup(drive))
197 return -EBUSY;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200198 setting->set(drive, val);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200199 spin_unlock_irq(&ide_lock);
200 return 0;
201}
202
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200203static ide_devset_get(xfer_rate, current_speed);
204
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200205static int set_xfer_rate (ide_drive_t *drive, int arg)
206{
Bartlomiej Zolnierkiewicz34f5d5a2008-01-26 20:13:12 +0100207 ide_task_t task;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200208 int err;
209
Bartlomiej Zolnierkiewicz3b2a5c72008-07-23 19:55:56 +0200210 if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200211 return -EINVAL;
212
Bartlomiej Zolnierkiewicz34f5d5a2008-01-26 20:13:12 +0100213 memset(&task, 0, sizeof(task));
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200214 task.tf.command = ATA_CMD_SET_FEATURES;
Bartlomiej Zolnierkiewicz34f5d5a2008-01-26 20:13:12 +0100215 task.tf.feature = SETFEATURES_XFER;
216 task.tf.nsect = (u8)arg;
217 task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT |
218 IDE_TFLAG_IN_NSECT;
219
220 err = ide_no_data_taskfile(drive, &task);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200221
Bartlomiej Zolnierkiewicz3b2a5c72008-07-23 19:55:56 +0200222 if (!err) {
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200223 ide_set_xfer_rate(drive, (u8) arg);
224 ide_driveid_update(drive);
225 }
226 return err;
227}
228
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200229ide_devset_rw_nolock(current_speed, 0, 70, xfer_rate);
230ide_devset_rw_nolock(io_32bit, 0, 1 + (SUPPORT_VLB_SYNC << 1), io_32bit);
231ide_devset_rw_nolock(keepsettings, 0, 1, ksettings);
232ide_devset_rw_nolock(unmaskirq, 0, 1, unmaskirq);
233ide_devset_rw_nolock(using_dma, 0, 1, using_dma);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200234
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200235ide_devset_w_nolock(pio_mode, 0, 255, pio_mode);
236
237ide_devset_rw(init_speed, 0, 70, init_speed);
238ide_devset_rw(nice1, 0, 1, nice1);
239ide_devset_rw(number, 0, 3, dn);
240
241static const struct ide_devset *ide_generic_settings[] = {
242 &ide_devset_current_speed,
243 &ide_devset_init_speed,
244 &ide_devset_io_32bit,
245 &ide_devset_keepsettings,
246 &ide_devset_nice1,
247 &ide_devset_number,
248 &ide_devset_pio_mode,
249 &ide_devset_unmaskirq,
250 &ide_devset_using_dma,
251 NULL
252};
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254static void proc_ide_settings_warn(void)
255{
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200256 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (warned)
259 return;
260
261 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
262 "obsolete, and will be removed soon!\n");
263 warned = 1;
264}
265
266static int proc_ide_read_settings
267 (char *page, char **start, off_t off, int count, int *eof, void *data)
268{
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200269 const struct ide_devset *setting, **g, **d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 ide_drive_t *drive = (ide_drive_t *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 char *out = page;
272 int len, rc, mul_factor, div_factor;
273
274 proc_ide_settings_warn();
275
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200276 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200277 g = ide_generic_settings;
278 d = drive->settings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
280 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200281 while (*g || (d && *d)) {
282 /* read settings in the alphabetical order */
283 if (*g && d && *d) {
284 if (strcmp((*d)->name, (*g)->name) < 0)
285 setting = *d++;
286 else
287 setting = *g++;
288 } else if (d && *d) {
289 setting = *d++;
290 } else
291 setting = *g++;
292 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
293 div_factor = setting->divf ? setting->divf(drive) : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 out += sprintf(out, "%-24s", setting->name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200295 rc = ide_read_setting(drive, setting);
296 if (rc >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
298 else
299 out += sprintf(out, "%-16s", "write-only");
300 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200301 if (setting->flags & S_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 out += sprintf(out, "r");
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200303 if (setting->flags & S_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 out += sprintf(out, "w");
305 out += sprintf(out, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307 len = out - page;
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200308 mutex_unlock(&ide_setting_mtx);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200309 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312#define MAX_LEN 30
313
314static int proc_ide_write_settings(struct file *file, const char __user *buffer,
315 unsigned long count, void *data)
316{
317 ide_drive_t *drive = (ide_drive_t *) data;
318 char name[MAX_LEN + 1];
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200319 int for_real = 0, mul_factor, div_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 unsigned long n;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200321
322 const struct ide_devset *setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 char *buf, *s;
324
325 if (!capable(CAP_SYS_ADMIN))
326 return -EACCES;
327
328 proc_ide_settings_warn();
329
330 if (count >= PAGE_SIZE)
331 return -EINVAL;
332
333 s = buf = (char *)__get_free_page(GFP_USER);
334 if (!buf)
335 return -ENOMEM;
336
337 if (copy_from_user(buf, buffer, count)) {
338 free_page((unsigned long)buf);
339 return -EFAULT;
340 }
341
342 buf[count] = '\0';
343
344 /*
345 * Skip over leading whitespace
346 */
347 while (count && isspace(*s)) {
348 --count;
349 ++s;
350 }
351 /*
352 * Do one full pass to verify all parameters,
353 * then do another to actually write the new settings.
354 */
355 do {
356 char *p = s;
357 n = count;
358 while (n > 0) {
359 unsigned val;
360 char *q = p;
361
362 while (n > 0 && *p != ':') {
363 --n;
364 p++;
365 }
366 if (*p != ':')
367 goto parse_error;
368 if (p - q > MAX_LEN)
369 goto parse_error;
370 memcpy(name, q, p - q);
371 name[p - q] = 0;
372
373 if (n > 0) {
374 --n;
375 p++;
376 } else
377 goto parse_error;
378
379 val = simple_strtoul(p, &q, 10);
380 n -= q - p;
381 p = q;
382 if (n > 0 && !isspace(*p))
383 goto parse_error;
384 while (n > 0 && isspace(*p)) {
385 --n;
386 ++p;
387 }
388
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200389 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200390 /* generic settings first, then driver specific ones */
391 setting = ide_find_setting(ide_generic_settings, name);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200392 if (!setting) {
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200393 if (drive->settings)
394 setting = ide_find_setting(drive->settings, name);
395 if (!setting) {
396 mutex_unlock(&ide_setting_mtx);
397 goto parse_error;
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200400 if (for_real) {
401 mul_factor = setting->mulf ? setting->mulf(drive) : 1;
402 div_factor = setting->divf ? setting->divf(drive) : 1;
403 ide_write_setting(drive, setting, val * div_factor / mul_factor);
404 }
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200405 mutex_unlock(&ide_setting_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407 } while (!for_real++);
408 free_page((unsigned long)buf);
409 return count;
410parse_error:
411 free_page((unsigned long)buf);
412 printk("proc_ide_write_settings(): parse error\n");
413 return -EINVAL;
414}
415
416int proc_ide_read_capacity
417 (char *page, char **start, off_t off, int count, int *eof, void *data)
418{
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200419 int len = sprintf(page, "%llu\n", (long long)0x7fffffff);
420 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
424
425int proc_ide_read_geometry
426 (char *page, char **start, off_t off, int count, int *eof, void *data)
427{
428 ide_drive_t *drive = (ide_drive_t *) data;
429 char *out = page;
430 int len;
431
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200432 out += sprintf(out, "physical %d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 drive->cyl, drive->head, drive->sect);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200434 out += sprintf(out, "logical %d/%d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 drive->bios_cyl, drive->bios_head, drive->bios_sect);
436
437 len = out - page;
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200438 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441EXPORT_SYMBOL(proc_ide_read_geometry);
442
443static int proc_ide_read_dmodel
444 (char *page, char **start, off_t off, int count, int *eof, void *data)
445{
446 ide_drive_t *drive = (ide_drive_t *) data;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200447 char *m = (char *)&drive->id[ATA_ID_PROD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int len;
449
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200450 len = sprintf(page, "%.40s\n", m[0] ? m : "(none)");
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200451 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454static int proc_ide_read_driver
455 (char *page, char **start, off_t off, int count, int *eof, void *data)
456{
457 ide_drive_t *drive = (ide_drive_t *) data;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200458 struct device *dev = &drive->gendev;
459 ide_driver_t *ide_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int len;
461
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200462 if (dev->driver) {
463 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 len = sprintf(page, "%s version %s\n",
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200465 dev->driver->name, ide_drv->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 } else
467 len = sprintf(page, "ide-default version 0.9.newide\n");
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200468 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200471static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
472{
473 struct device *dev = &drive->gendev;
474 int ret = 1;
Randy Dunlap349ae232006-10-03 01:14:23 -0700475 int err;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200476
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200477 device_release_driver(dev);
478 /* FIXME: device can still be in use by previous driver */
479 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
Randy Dunlap349ae232006-10-03 01:14:23 -0700480 err = device_attach(dev);
481 if (err < 0)
482 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200483 __func__, err);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200484 drive->driver_req[0] = 0;
Randy Dunlap349ae232006-10-03 01:14:23 -0700485 if (dev->driver == NULL) {
486 err = device_attach(dev);
487 if (err < 0)
488 printk(KERN_WARNING
489 "IDE: %s: device_attach(2) error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200490 __func__, err);
Randy Dunlap349ae232006-10-03 01:14:23 -0700491 }
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200492 if (dev->driver && !strcmp(dev->driver->name, driver))
493 ret = 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200494
495 return ret;
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static int proc_ide_write_driver
499 (struct file *file, const char __user *buffer, unsigned long count, void *data)
500{
501 ide_drive_t *drive = (ide_drive_t *) data;
502 char name[32];
503
504 if (!capable(CAP_SYS_ADMIN))
505 return -EACCES;
506 if (count > 31)
507 count = 31;
508 if (copy_from_user(name, buffer, count))
509 return -EFAULT;
510 name[count] = '\0';
511 if (ide_replace_subdriver(drive, name))
512 return -EINVAL;
513 return count;
514}
515
516static int proc_ide_read_media
517 (char *page, char **start, off_t off, int count, int *eof, void *data)
518{
519 ide_drive_t *drive = (ide_drive_t *) data;
520 const char *media;
521 int len;
522
523 switch (drive->media) {
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200524 case ide_disk: media = "disk\n"; break;
525 case ide_cdrom: media = "cdrom\n"; break;
526 case ide_tape: media = "tape\n"; break;
527 case ide_floppy: media = "floppy\n"; break;
528 case ide_optical: media = "optical\n"; break;
529 default: media = "UNKNOWN\n"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200531 strcpy(page, media);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 len = strlen(media);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200533 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536static ide_proc_entry_t generic_drive_entries[] = {
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200537 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver,
538 proc_ide_write_driver },
539 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
540 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
541 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
542 { "settings", S_IFREG|S_IRUSR|S_IWUSR, proc_ide_read_settings,
543 proc_ide_write_settings },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 { NULL, 0, NULL, NULL }
545};
546
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200547static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 struct proc_dir_entry *ent;
550
551 if (!dir || !p)
552 return;
553 while (p->name != NULL) {
554 ent = create_proc_entry(p->name, p->mode, dir);
555 if (!ent) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ent->data = data;
557 ent->read_proc = p->read_proc;
558 ent->write_proc = p->write_proc;
559 p++;
560 }
561}
562
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200563static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 if (!dir || !p)
566 return;
567 while (p->name != NULL) {
568 remove_proc_entry(p->name, dir);
569 p++;
570 }
571}
572
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200573void ide_proc_register_driver(ide_drive_t *drive, ide_driver_t *driver)
574{
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200575 mutex_lock(&ide_setting_mtx);
576 drive->settings = driver->settings;
577 mutex_unlock(&ide_setting_mtx);
578
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200579 ide_add_proc_entries(drive->proc, driver->proc, drive);
580}
581
582EXPORT_SYMBOL(ide_proc_register_driver);
583
584/**
585 * ide_proc_unregister_driver - remove driver specific data
586 * @drive: drive
587 * @driver: driver
588 *
589 * Clean up the driver specific /proc files and IDE settings
590 * for a given drive.
591 *
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200592 * Takes ide_setting_mtx and ide_lock.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200593 * Caller must hold none of the locks.
594 */
595
596void ide_proc_unregister_driver(ide_drive_t *drive, ide_driver_t *driver)
597{
598 unsigned long flags;
599
600 ide_remove_proc_entries(drive->proc, driver->proc);
601
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200602 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200603 spin_lock_irqsave(&ide_lock, flags);
604 /*
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200605 * ide_setting_mtx protects the settings list
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200606 * ide_lock protects the use of settings
607 *
608 * so we need to hold both, ide_settings_sem because we want to
609 * modify the settings list, and ide_lock because we cannot take
610 * a setting out that is being used.
611 *
612 * OTOH both ide_{read,write}_setting are only ever used under
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200613 * ide_setting_mtx.
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200614 */
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200615 drive->settings = NULL;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200616 spin_unlock_irqrestore(&ide_lock, flags);
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200617 mutex_unlock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200618}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200619EXPORT_SYMBOL(ide_proc_unregister_driver);
620
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +0100621void ide_proc_port_register_devices(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 int d;
624 struct proc_dir_entry *ent;
625 struct proc_dir_entry *parent = hwif->proc;
626 char name[64];
627
628 for (d = 0; d < MAX_DRIVES; d++) {
629 ide_drive_t *drive = &hwif->drives[d];
630
631 if (!drive->present)
632 continue;
633 if (drive->proc)
634 continue;
635
636 drive->proc = proc_mkdir(drive->name, parent);
637 if (drive->proc)
638 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
Paolo Ciarrocchi441e92d2008-04-26 17:36:40 +0200639 sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ent = proc_symlink(drive->name, proc_ide_root, name);
641 if (!ent) return;
642 }
643}
644
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200645void ide_proc_unregister_device(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 if (drive->proc) {
648 ide_remove_proc_entries(drive->proc, generic_drive_entries);
649 remove_proc_entry(drive->name, proc_ide_root);
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200650 remove_proc_entry(drive->name, drive->hwif->proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 drive->proc = NULL;
652 }
653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655static ide_proc_entry_t hwif_entries[] = {
656 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
657 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
658 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
659 { NULL, 0, NULL, NULL }
660};
661
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200662void ide_proc_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200664 if (!hwif->proc) {
665 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200667 if (!hwif->proc)
668 return;
669
670 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672}
673
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200674void ide_proc_unregister_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 if (hwif->proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 ide_remove_proc_entries(hwif->proc, hwif_entries);
678 remove_proc_entry(hwif->name, proc_ide_root);
679 hwif->proc = NULL;
680 }
681}
682
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200683static int proc_print_driver(struct device_driver *drv, void *data)
684{
685 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
686 struct seq_file *s = data;
687
688 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
689
690 return 0;
691}
692
693static int ide_drivers_show(struct seq_file *s, void *p)
694{
Randy Dunlap349ae232006-10-03 01:14:23 -0700695 int err;
696
697 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
698 if (err < 0)
699 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200700 __func__, err);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200701 return 0;
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704static int ide_drivers_open(struct inode *inode, struct file *file)
705{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200706 return single_open(file, &ide_drivers_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200708
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800709static const struct file_operations ide_drivers_operations = {
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700710 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 .open = ide_drivers_open,
712 .read = seq_read,
713 .llseek = seq_lseek,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200714 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715};
716
717void proc_ide_create(void)
718{
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200719 proc_ide_root = proc_mkdir("ide", NULL);
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!proc_ide_root)
722 return;
723
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700724 proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727void proc_ide_destroy(void)
728{
Zhang, Yanmin643bdc62005-05-16 21:53:11 -0700729 remove_proc_entry("drivers", proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 remove_proc_entry("ide", NULL);
731}