blob: 30a5780f4185a7e6c4fe6ac65c79df25d822357c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/ide/ide-proc.c Version 1.05 Mar 05, 2003
3 *
4 * Copyright (C) 1997-1998 Mark Lord
5 * Copyright (C) 2003 Red Hat <alan@redhat.com>
6 */
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
15 *
16 * Also useful, "cat /proc/ide0/hda/[identify, smart_values,
17 * smart_thresholds, capabilities]" will issue an IDENTIFY /
18 * PACKET_IDENTIFY / SMART_READ_VALUES / SMART_READ_THRESHOLDS /
19 * SENSE CAPABILITIES command to /dev/hda, and then dump out the
20 * returned data as 256 16-bit words. The "hdparm" utility will
21 * be updated someday soon to use this mechanism.
22 *
23 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26
27#include <asm/uaccess.h>
28#include <linux/errno.h>
29#include <linux/sched.h>
30#include <linux/proc_fs.h>
31#include <linux/stat.h>
32#include <linux/mm.h>
33#include <linux/pci.h>
34#include <linux/ctype.h>
35#include <linux/hdreg.h>
36#include <linux/ide.h>
37#include <linux/seq_file.h>
38
39#include <asm/io.h>
40
41static int proc_ide_read_imodel
42 (char *page, char **start, off_t off, int count, int *eof, void *data)
43{
44 ide_hwif_t *hwif = (ide_hwif_t *) data;
45 int len;
46 const char *name;
47
48 /*
49 * Neither ide_unknown nor ide_forced should be set at this point.
50 */
51 switch (hwif->chipset) {
52 case ide_generic: name = "generic"; break;
53 case ide_pci: name = "pci"; break;
54 case ide_cmd640: name = "cmd640"; break;
55 case ide_dtc2278: name = "dtc2278"; break;
56 case ide_ali14xx: name = "ali14xx"; break;
57 case ide_qd65xx: name = "qd65xx"; break;
58 case ide_umc8672: name = "umc8672"; break;
59 case ide_ht6560b: name = "ht6560b"; break;
60 case ide_rz1000: name = "rz1000"; break;
61 case ide_trm290: name = "trm290"; break;
62 case ide_cmd646: name = "cmd646"; break;
63 case ide_cy82c693: name = "cy82c693"; break;
64 case ide_4drives: name = "4drives"; break;
65 case ide_pmac: name = "mac-io"; break;
Pete Popov26a940e2005-09-15 08:03:12 +000066 case ide_au1xxx: name = "au1xxx"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 default: name = "(unknown)"; break;
68 }
69 len = sprintf(page, "%s\n", name);
70 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
71}
72
73static int proc_ide_read_mate
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 if (hwif && hwif->mate && hwif->mate->present)
80 len = sprintf(page, "%s\n", hwif->mate->name);
81 else
82 len = sprintf(page, "(none)\n");
83 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
84}
85
86static int proc_ide_read_channel
87 (char *page, char **start, off_t off, int count, int *eof, void *data)
88{
89 ide_hwif_t *hwif = (ide_hwif_t *) data;
90 int len;
91
92 page[0] = hwif->channel ? '1' : '0';
93 page[1] = '\n';
94 len = 2;
95 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
96}
97
98static int proc_ide_read_identify
99 (char *page, char **start, off_t off, int count, int *eof, void *data)
100{
101 ide_drive_t *drive = (ide_drive_t *)data;
102 int len = 0, i = 0;
103 int err = 0;
104
105 len = sprintf(page, "\n");
106
107 if (drive) {
108 unsigned short *val = (unsigned short *) page;
109
110 err = taskfile_lib_get_identify(drive, page);
111 if (!err) {
112 char *out = ((char *)page) + (SECTOR_WORDS * 4);
113 page = out;
114 do {
115 out += sprintf(out, "%04x%c",
116 le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
117 val += 1;
118 } while (i < (SECTOR_WORDS * 2));
119 len = out - page;
120 }
121 }
122 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
123}
124
125static void proc_ide_settings_warn(void)
126{
127 static int warned = 0;
128
129 if (warned)
130 return;
131
132 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
133 "obsolete, and will be removed soon!\n");
134 warned = 1;
135}
136
137static int proc_ide_read_settings
138 (char *page, char **start, off_t off, int count, int *eof, void *data)
139{
140 ide_drive_t *drive = (ide_drive_t *) data;
141 ide_settings_t *setting = (ide_settings_t *) drive->settings;
142 char *out = page;
143 int len, rc, mul_factor, div_factor;
144
145 proc_ide_settings_warn();
146
147 down(&ide_setting_sem);
148 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
149 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
150 while(setting) {
151 mul_factor = setting->mul_factor;
152 div_factor = setting->div_factor;
153 out += sprintf(out, "%-24s", setting->name);
154 if ((rc = ide_read_setting(drive, setting)) >= 0)
155 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
156 else
157 out += sprintf(out, "%-16s", "write-only");
158 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
159 if (setting->rw & SETTING_READ)
160 out += sprintf(out, "r");
161 if (setting->rw & SETTING_WRITE)
162 out += sprintf(out, "w");
163 out += sprintf(out, "\n");
164 setting = setting->next;
165 }
166 len = out - page;
167 up(&ide_setting_sem);
168 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
169}
170
171#define MAX_LEN 30
172
173static int proc_ide_write_settings(struct file *file, const char __user *buffer,
174 unsigned long count, void *data)
175{
176 ide_drive_t *drive = (ide_drive_t *) data;
177 char name[MAX_LEN + 1];
178 int for_real = 0;
179 unsigned long n;
180 ide_settings_t *setting;
181 char *buf, *s;
182
183 if (!capable(CAP_SYS_ADMIN))
184 return -EACCES;
185
186 proc_ide_settings_warn();
187
188 if (count >= PAGE_SIZE)
189 return -EINVAL;
190
191 s = buf = (char *)__get_free_page(GFP_USER);
192 if (!buf)
193 return -ENOMEM;
194
195 if (copy_from_user(buf, buffer, count)) {
196 free_page((unsigned long)buf);
197 return -EFAULT;
198 }
199
200 buf[count] = '\0';
201
202 /*
203 * Skip over leading whitespace
204 */
205 while (count && isspace(*s)) {
206 --count;
207 ++s;
208 }
209 /*
210 * Do one full pass to verify all parameters,
211 * then do another to actually write the new settings.
212 */
213 do {
214 char *p = s;
215 n = count;
216 while (n > 0) {
217 unsigned val;
218 char *q = p;
219
220 while (n > 0 && *p != ':') {
221 --n;
222 p++;
223 }
224 if (*p != ':')
225 goto parse_error;
226 if (p - q > MAX_LEN)
227 goto parse_error;
228 memcpy(name, q, p - q);
229 name[p - q] = 0;
230
231 if (n > 0) {
232 --n;
233 p++;
234 } else
235 goto parse_error;
236
237 val = simple_strtoul(p, &q, 10);
238 n -= q - p;
239 p = q;
240 if (n > 0 && !isspace(*p))
241 goto parse_error;
242 while (n > 0 && isspace(*p)) {
243 --n;
244 ++p;
245 }
246
247 down(&ide_setting_sem);
248 setting = ide_find_setting_by_name(drive, name);
249 if (!setting)
250 {
251 up(&ide_setting_sem);
252 goto parse_error;
253 }
254 if (for_real)
255 ide_write_setting(drive, setting, val * setting->div_factor / setting->mul_factor);
256 up(&ide_setting_sem);
257 }
258 } while (!for_real++);
259 free_page((unsigned long)buf);
260 return count;
261parse_error:
262 free_page((unsigned long)buf);
263 printk("proc_ide_write_settings(): parse error\n");
264 return -EINVAL;
265}
266
267int proc_ide_read_capacity
268 (char *page, char **start, off_t off, int count, int *eof, void *data)
269{
270 int len = sprintf(page,"%llu\n", (long long)0x7fffffff);
271 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
272}
273
274EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
275
276int proc_ide_read_geometry
277 (char *page, char **start, off_t off, int count, int *eof, void *data)
278{
279 ide_drive_t *drive = (ide_drive_t *) data;
280 char *out = page;
281 int len;
282
283 out += sprintf(out,"physical %d/%d/%d\n",
284 drive->cyl, drive->head, drive->sect);
285 out += sprintf(out,"logical %d/%d/%d\n",
286 drive->bios_cyl, drive->bios_head, drive->bios_sect);
287
288 len = out - page;
289 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
290}
291
292EXPORT_SYMBOL(proc_ide_read_geometry);
293
294static int proc_ide_read_dmodel
295 (char *page, char **start, off_t off, int count, int *eof, void *data)
296{
297 ide_drive_t *drive = (ide_drive_t *) data;
298 struct hd_driveid *id = drive->id;
299 int len;
300
301 len = sprintf(page, "%.40s\n",
302 (id && id->model[0]) ? (char *)id->model : "(none)");
303 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
304}
305
306static int proc_ide_read_driver
307 (char *page, char **start, off_t off, int count, int *eof, void *data)
308{
309 ide_drive_t *drive = (ide_drive_t *) data;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200310 struct device *dev = &drive->gendev;
311 ide_driver_t *ide_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int len;
313
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200314 down_read(&dev->bus->subsys.rwsem);
315 if (dev->driver) {
316 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 len = sprintf(page, "%s version %s\n",
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200318 dev->driver->name, ide_drv->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 } else
320 len = sprintf(page, "ide-default version 0.9.newide\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200321 up_read(&dev->bus->subsys.rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
323}
324
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200325static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
326{
327 struct device *dev = &drive->gendev;
328 int ret = 1;
Randy Dunlap349ae232006-10-03 01:14:23 -0700329 int err;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200330
331 down_write(&dev->bus->subsys.rwsem);
332 device_release_driver(dev);
333 /* FIXME: device can still be in use by previous driver */
334 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
Randy Dunlap349ae232006-10-03 01:14:23 -0700335 err = device_attach(dev);
336 if (err < 0)
337 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
338 __FUNCTION__, err);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200339 drive->driver_req[0] = 0;
Randy Dunlap349ae232006-10-03 01:14:23 -0700340 if (dev->driver == NULL) {
341 err = device_attach(dev);
342 if (err < 0)
343 printk(KERN_WARNING
344 "IDE: %s: device_attach(2) error: %d\n",
345 __FUNCTION__, err);
346 }
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200347 if (dev->driver && !strcmp(dev->driver->name, driver))
348 ret = 0;
349 up_write(&dev->bus->subsys.rwsem);
350
351 return ret;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static int proc_ide_write_driver
355 (struct file *file, const char __user *buffer, unsigned long count, void *data)
356{
357 ide_drive_t *drive = (ide_drive_t *) data;
358 char name[32];
359
360 if (!capable(CAP_SYS_ADMIN))
361 return -EACCES;
362 if (count > 31)
363 count = 31;
364 if (copy_from_user(name, buffer, count))
365 return -EFAULT;
366 name[count] = '\0';
367 if (ide_replace_subdriver(drive, name))
368 return -EINVAL;
369 return count;
370}
371
372static int proc_ide_read_media
373 (char *page, char **start, off_t off, int count, int *eof, void *data)
374{
375 ide_drive_t *drive = (ide_drive_t *) data;
376 const char *media;
377 int len;
378
379 switch (drive->media) {
380 case ide_disk: media = "disk\n";
381 break;
382 case ide_cdrom: media = "cdrom\n";
383 break;
384 case ide_tape: media = "tape\n";
385 break;
386 case ide_floppy:media = "floppy\n";
387 break;
Alexey Dobriyan4495c9e2006-09-08 09:47:24 -0700388 case ide_optical:media = "optical\n";
389 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 default: media = "UNKNOWN\n";
391 break;
392 }
393 strcpy(page,media);
394 len = strlen(media);
395 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
396}
397
398static ide_proc_entry_t generic_drive_entries[] = {
399 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver, proc_ide_write_driver },
400 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
401 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
402 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
403 { "settings", S_IFREG|S_IRUSR|S_IWUSR,proc_ide_read_settings, proc_ide_write_settings },
404 { NULL, 0, NULL, NULL }
405};
406
407void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
408{
409 struct proc_dir_entry *ent;
410
411 if (!dir || !p)
412 return;
413 while (p->name != NULL) {
414 ent = create_proc_entry(p->name, p->mode, dir);
415 if (!ent) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ent->data = data;
417 ent->read_proc = p->read_proc;
418 ent->write_proc = p->write_proc;
419 p++;
420 }
421}
422
423void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
424{
425 if (!dir || !p)
426 return;
427 while (p->name != NULL) {
428 remove_proc_entry(p->name, dir);
429 p++;
430 }
431}
432
433static void create_proc_ide_drives(ide_hwif_t *hwif)
434{
435 int d;
436 struct proc_dir_entry *ent;
437 struct proc_dir_entry *parent = hwif->proc;
438 char name[64];
439
440 for (d = 0; d < MAX_DRIVES; d++) {
441 ide_drive_t *drive = &hwif->drives[d];
442
443 if (!drive->present)
444 continue;
445 if (drive->proc)
446 continue;
447
448 drive->proc = proc_mkdir(drive->name, parent);
449 if (drive->proc)
450 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
451 sprintf(name,"ide%d/%s", (drive->name[2]-'a')/2, drive->name);
452 ent = proc_symlink(drive->name, proc_ide_root, name);
453 if (!ent) return;
454 }
455}
456
457static void destroy_proc_ide_device(ide_hwif_t *hwif, ide_drive_t *drive)
458{
459 if (drive->proc) {
460 ide_remove_proc_entries(drive->proc, generic_drive_entries);
461 remove_proc_entry(drive->name, proc_ide_root);
462 remove_proc_entry(drive->name, hwif->proc);
463 drive->proc = NULL;
464 }
465}
466
467static void destroy_proc_ide_drives(ide_hwif_t *hwif)
468{
469 int d;
470
471 for (d = 0; d < MAX_DRIVES; d++) {
472 ide_drive_t *drive = &hwif->drives[d];
473 if (drive->proc)
474 destroy_proc_ide_device(hwif, drive);
475 }
476}
477
478static ide_proc_entry_t hwif_entries[] = {
479 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
480 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
481 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
482 { NULL, 0, NULL, NULL }
483};
484
485void create_proc_ide_interfaces(void)
486{
487 int h;
488
489 for (h = 0; h < MAX_HWIFS; h++) {
490 ide_hwif_t *hwif = &ide_hwifs[h];
491
492 if (!hwif->present)
493 continue;
494 if (!hwif->proc) {
495 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
496 if (!hwif->proc)
497 return;
498 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
499 }
500 create_proc_ide_drives(hwif);
501 }
502}
503
504EXPORT_SYMBOL(create_proc_ide_interfaces);
505
506#ifdef CONFIG_BLK_DEV_IDEPCI
507void ide_pci_create_host_proc(const char *name, get_info_t *get_info)
508{
509 create_proc_info_entry(name, 0, proc_ide_root, get_info);
510}
511
512EXPORT_SYMBOL_GPL(ide_pci_create_host_proc);
513#endif
514
515void destroy_proc_ide_interface(ide_hwif_t *hwif)
516{
517 if (hwif->proc) {
518 destroy_proc_ide_drives(hwif);
519 ide_remove_proc_entries(hwif->proc, hwif_entries);
520 remove_proc_entry(hwif->name, proc_ide_root);
521 hwif->proc = NULL;
522 }
523}
524
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200525static int proc_print_driver(struct device_driver *drv, void *data)
526{
527 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
528 struct seq_file *s = data;
529
530 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
531
532 return 0;
533}
534
535static int ide_drivers_show(struct seq_file *s, void *p)
536{
Randy Dunlap349ae232006-10-03 01:14:23 -0700537 int err;
538
539 err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
540 if (err < 0)
541 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
542 __FUNCTION__, err);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200543 return 0;
544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546static int ide_drivers_open(struct inode *inode, struct file *file)
547{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200548 return single_open(file, &ide_drivers_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200550
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800551static const struct file_operations ide_drivers_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 .open = ide_drivers_open,
553 .read = seq_read,
554 .llseek = seq_lseek,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200555 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556};
557
558void proc_ide_create(void)
559{
560 struct proc_dir_entry *entry;
561
562 if (!proc_ide_root)
563 return;
564
565 create_proc_ide_interfaces();
566
567 entry = create_proc_entry("drivers", 0, proc_ide_root);
568 if (entry)
569 entry->proc_fops = &ide_drivers_operations;
570}
571
572void proc_ide_destroy(void)
573{
Zhang, Yanmin643bdc62005-05-16 21:53:11 -0700574 remove_proc_entry("drivers", proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 remove_proc_entry("ide", NULL);
576}