vserver 1.9.5.x5
[linux-2.6.git] / drivers / ide / ide-proc.c
1 /*
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
25 #include <linux/config.h>
26 #include <linux/module.h>
27
28 #include <asm/uaccess.h>
29 #include <linux/errno.h>
30 #include <linux/sched.h>
31 #include <linux/proc_fs.h>
32 #include <linux/stat.h>
33 #include <linux/mm.h>
34 #include <linux/pci.h>
35 #include <linux/ctype.h>
36 #include <linux/hdreg.h>
37 #include <linux/ide.h>
38 #include <linux/seq_file.h>
39
40 #include <asm/io.h>
41
42 static int proc_ide_read_imodel
43         (char *page, char **start, off_t off, int count, int *eof, void *data)
44 {
45         ide_hwif_t      *hwif = (ide_hwif_t *) data;
46         int             len;
47         const char      *name;
48
49         /*
50          * Neither ide_unknown nor ide_forced should be set at this point.
51          */
52         switch (hwif->chipset) {
53                 case ide_generic:       name = "generic";       break;
54                 case ide_pci:           name = "pci";           break;
55                 case ide_cmd640:        name = "cmd640";        break;
56                 case ide_dtc2278:       name = "dtc2278";       break;
57                 case ide_ali14xx:       name = "ali14xx";       break;
58                 case ide_qd65xx:        name = "qd65xx";        break;
59                 case ide_umc8672:       name = "umc8672";       break;
60                 case ide_ht6560b:       name = "ht6560b";       break;
61                 case ide_rz1000:        name = "rz1000";        break;
62                 case ide_trm290:        name = "trm290";        break;
63                 case ide_cmd646:        name = "cmd646";        break;
64                 case ide_cy82c693:      name = "cy82c693";      break;
65                 case ide_4drives:       name = "4drives";       break;
66                 case ide_pmac:          name = "mac-io";        break;
67                 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
73 static 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
86 static 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
98 static 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                 BUG_ON(!drive->driver);
111
112                 err = taskfile_lib_get_identify(drive, page);
113                 if (!err) {
114                         char *out = ((char *)page) + (SECTOR_WORDS * 4);
115                         page = out;
116                         do {
117                                 out += sprintf(out, "%04x%c",
118                                         le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
119                                 val += 1;
120                         } while (i < (SECTOR_WORDS * 2));
121                         len = out - page;
122                 }
123         }
124         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
125 }
126
127 static void proc_ide_settings_warn(void)
128 {
129         static int warned = 0;
130
131         if (warned)
132                 return;
133
134         printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
135                             "obsolete, and will be removed soon!\n");
136         warned = 1;
137 }
138
139 static int proc_ide_read_settings
140         (char *page, char **start, off_t off, int count, int *eof, void *data)
141 {
142         ide_drive_t     *drive = (ide_drive_t *) data;
143         ide_settings_t  *setting = (ide_settings_t *) drive->settings;
144         char            *out = page;
145         int             len, rc, mul_factor, div_factor;
146
147         proc_ide_settings_warn();
148
149         down(&ide_setting_sem);
150         out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
151         out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
152         while(setting) {
153                 mul_factor = setting->mul_factor;
154                 div_factor = setting->div_factor;
155                 out += sprintf(out, "%-24s", setting->name);
156                 if ((rc = ide_read_setting(drive, setting)) >= 0)
157                         out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
158                 else
159                         out += sprintf(out, "%-16s", "write-only");
160                 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
161                 if (setting->rw & SETTING_READ)
162                         out += sprintf(out, "r");
163                 if (setting->rw & SETTING_WRITE)
164                         out += sprintf(out, "w");
165                 out += sprintf(out, "\n");
166                 setting = setting->next;
167         }
168         len = out - page;
169         up(&ide_setting_sem);
170         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
171 }
172
173 #define MAX_LEN 30
174
175 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
176                                    unsigned long count, void *data)
177 {
178         ide_drive_t     *drive = (ide_drive_t *) data;
179         char            name[MAX_LEN + 1];
180         int             for_real = 0;
181         unsigned long   n;
182         ide_settings_t  *setting;
183         char *buf, *s;
184
185         if (!capable(CAP_SYS_ADMIN))
186                 return -EACCES;
187
188         proc_ide_settings_warn();
189
190         if (count >= PAGE_SIZE)
191                 return -EINVAL;
192
193         s = buf = (char *)__get_free_page(GFP_USER);
194         if (!buf)
195                 return -ENOMEM;
196
197         if (copy_from_user(buf, buffer, count)) {
198                 free_page((unsigned long)buf);
199                 return -EFAULT;
200         }
201
202         buf[count] = '\0';
203
204         /*
205          * Skip over leading whitespace
206          */
207         while (count && isspace(*s)) {
208                 --count;
209                 ++s;
210         }
211         /*
212          * Do one full pass to verify all parameters,
213          * then do another to actually write the new settings.
214          */
215         do {
216                 char *p = s;
217                 n = count;
218                 while (n > 0) {
219                         unsigned val;
220                         char *q = p;
221
222                         while (n > 0 && *p != ':') {
223                                 --n;
224                                 p++;
225                         }
226                         if (*p != ':')
227                                 goto parse_error;
228                         if (p - q > MAX_LEN)
229                                 goto parse_error;
230                         memcpy(name, q, p - q);
231                         name[p - q] = 0;
232
233                         if (n > 0) {
234                                 --n;
235                                 p++;
236                         } else
237                                 goto parse_error;
238
239                         val = simple_strtoul(p, &q, 10);
240                         n -= q - p;
241                         p = q;
242                         if (n > 0 && !isspace(*p))
243                                 goto parse_error;
244                         while (n > 0 && isspace(*p)) {
245                                 --n;
246                                 ++p;
247                         }
248
249                         down(&ide_setting_sem);
250                         setting = ide_find_setting_by_name(drive, name);
251                         if (!setting)
252                         {
253                                 up(&ide_setting_sem);
254                                 goto parse_error;
255                         }
256                         if (for_real)
257                                 ide_write_setting(drive, setting, val * setting->div_factor / setting->mul_factor);
258                         up(&ide_setting_sem);
259                 }
260         } while (!for_real++);
261         free_page((unsigned long)buf);
262         return count;
263 parse_error:
264         free_page((unsigned long)buf);
265         printk("proc_ide_write_settings(): parse error\n");
266         return -EINVAL;
267 }
268
269 int proc_ide_read_capacity
270         (char *page, char **start, off_t off, int count, int *eof, void *data)
271 {
272         ide_drive_t     *drive = (ide_drive_t *) data;
273         int             len;
274
275         len = sprintf(page,"%llu\n",
276                       (long long) (DRIVER(drive)->capacity(drive)));
277         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
278 }
279
280 int proc_ide_read_geometry
281         (char *page, char **start, off_t off, int count, int *eof, void *data)
282 {
283         ide_drive_t     *drive = (ide_drive_t *) data;
284         char            *out = page;
285         int             len;
286
287         out += sprintf(out,"physical     %d/%d/%d\n",
288                         drive->cyl, drive->head, drive->sect);
289         out += sprintf(out,"logical      %d/%d/%d\n",
290                         drive->bios_cyl, drive->bios_head, drive->bios_sect);
291
292         len = out - page;
293         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
294 }
295
296 EXPORT_SYMBOL(proc_ide_read_geometry);
297
298 static int proc_ide_read_dmodel
299         (char *page, char **start, off_t off, int count, int *eof, void *data)
300 {
301         ide_drive_t     *drive = (ide_drive_t *) data;
302         struct hd_driveid *id = drive->id;
303         int             len;
304
305         len = sprintf(page, "%.40s\n",
306                 (id && id->model[0]) ? (char *)id->model : "(none)");
307         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
308 }
309
310 static int proc_ide_read_driver
311         (char *page, char **start, off_t off, int count, int *eof, void *data)
312 {
313         ide_drive_t     *drive = (ide_drive_t *) data;
314         ide_driver_t    *driver = drive->driver;
315         int             len;
316
317         len = sprintf(page, "%s version %s\n",
318                         driver->name, driver->version);
319         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
320 }
321
322 static int proc_ide_write_driver
323         (struct file *file, const char __user *buffer, unsigned long count, void *data)
324 {
325         ide_drive_t     *drive = (ide_drive_t *) data;
326         char name[32];
327
328         if (!capable(CAP_SYS_ADMIN))
329                 return -EACCES;
330         if (count > 31)
331                 count = 31;
332         if (copy_from_user(name, buffer, count))
333                 return -EFAULT;
334         name[count] = '\0';
335         if (ide_replace_subdriver(drive, name))
336                 return -EINVAL;
337         return count;
338 }
339
340 static int proc_ide_read_media
341         (char *page, char **start, off_t off, int count, int *eof, void *data)
342 {
343         ide_drive_t     *drive = (ide_drive_t *) data;
344         const char      *media;
345         int             len;
346
347         switch (drive->media) {
348                 case ide_disk:  media = "disk\n";
349                                 break;
350                 case ide_cdrom: media = "cdrom\n";
351                                 break;
352                 case ide_tape:  media = "tape\n";
353                                 break;
354                 case ide_floppy:media = "floppy\n";
355                                 break;
356                 default:        media = "UNKNOWN\n";
357                                 break;
358         }
359         strcpy(page,media);
360         len = strlen(media);
361         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
362 }
363
364 static ide_proc_entry_t generic_drive_entries[] = {
365         { "driver",     S_IFREG|S_IRUGO,        proc_ide_read_driver,   proc_ide_write_driver },
366         { "identify",   S_IFREG|S_IRUSR,        proc_ide_read_identify, NULL },
367         { "media",      S_IFREG|S_IRUGO,        proc_ide_read_media,    NULL },
368         { "model",      S_IFREG|S_IRUGO,        proc_ide_read_dmodel,   NULL },
369         { "settings",   S_IFREG|S_IRUSR|S_IWUSR,proc_ide_read_settings, proc_ide_write_settings },
370         { NULL, 0, NULL, NULL }
371 };
372
373 void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
374 {
375         struct proc_dir_entry *ent;
376
377         if (!dir || !p)
378                 return;
379         while (p->name != NULL) {
380                 ent = create_proc_entry(p->name, p->mode, dir);
381                 if (!ent) return;
382                 ent->nlink = 1;
383                 ent->data = data;
384                 ent->read_proc = p->read_proc;
385                 ent->write_proc = p->write_proc;
386                 p++;
387         }
388 }
389
390 void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
391 {
392         if (!dir || !p)
393                 return;
394         while (p->name != NULL) {
395                 remove_proc_entry(p->name, dir);
396                 p++;
397         }
398 }
399
400 static void create_proc_ide_drives(ide_hwif_t *hwif)
401 {
402         int     d;
403         struct proc_dir_entry *ent;
404         struct proc_dir_entry *parent = hwif->proc;
405         char name[64];
406
407         for (d = 0; d < MAX_DRIVES; d++) {
408                 ide_drive_t *drive = &hwif->drives[d];
409
410                 if (!drive->present)
411                         continue;
412                 if (drive->proc)
413                         continue;
414
415                 drive->proc = proc_mkdir(drive->name, parent);
416                 if (drive->proc)
417                         ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
418                 sprintf(name,"ide%d/%s", (drive->name[2]-'a')/2, drive->name);
419                 ent = proc_symlink(drive->name, proc_ide_root, name);
420                 if (!ent) return;
421         }
422 }
423
424 static void destroy_proc_ide_device(ide_hwif_t *hwif, ide_drive_t *drive)
425 {
426         ide_driver_t *driver = drive->driver;
427
428         if (drive->proc) {
429                 ide_remove_proc_entries(drive->proc, driver->proc);
430                 ide_remove_proc_entries(drive->proc, generic_drive_entries);
431                 remove_proc_entry(drive->name, proc_ide_root);
432                 remove_proc_entry(drive->name, hwif->proc);
433                 drive->proc = NULL;
434         }
435 }
436
437 static void destroy_proc_ide_drives(ide_hwif_t *hwif)
438 {
439         int     d;
440
441         for (d = 0; d < MAX_DRIVES; d++) {
442                 ide_drive_t *drive = &hwif->drives[d];
443                 if (drive->proc)
444                         destroy_proc_ide_device(hwif, drive);
445         }
446 }
447
448 static ide_proc_entry_t hwif_entries[] = {
449         { "channel",    S_IFREG|S_IRUGO,        proc_ide_read_channel,  NULL },
450         { "mate",       S_IFREG|S_IRUGO,        proc_ide_read_mate,     NULL },
451         { "model",      S_IFREG|S_IRUGO,        proc_ide_read_imodel,   NULL },
452         { NULL, 0, NULL, NULL }
453 };
454
455 void create_proc_ide_interfaces(void)
456 {
457         int     h;
458
459         for (h = 0; h < MAX_HWIFS; h++) {
460                 ide_hwif_t *hwif = &ide_hwifs[h];
461
462                 if (!hwif->present)
463                         continue;
464                 if (!hwif->proc) {
465                         hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
466                         if (!hwif->proc)
467                                 return;
468                         ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
469                 }
470                 create_proc_ide_drives(hwif);
471         }
472 }
473
474 EXPORT_SYMBOL(create_proc_ide_interfaces);
475
476 #ifdef CONFIG_BLK_DEV_IDEPCI
477 void ide_pci_create_host_proc(const char *name, get_info_t *get_info)
478 {
479         create_proc_info_entry(name, 0, proc_ide_root, get_info);
480 }
481
482 EXPORT_SYMBOL_GPL(ide_pci_create_host_proc);
483 #endif
484
485 void destroy_proc_ide_interface(ide_hwif_t *hwif)
486 {
487         if (hwif->proc) {
488                 destroy_proc_ide_drives(hwif);
489                 ide_remove_proc_entries(hwif->proc, hwif_entries);
490                 remove_proc_entry(hwif->name, proc_ide_root);
491                 hwif->proc = NULL;
492         }
493 }
494
495 extern struct seq_operations ide_drivers_op;
496 static int ide_drivers_open(struct inode *inode, struct file *file)
497 {
498         return seq_open(file, &ide_drivers_op);
499 }
500 static struct file_operations ide_drivers_operations = {
501         .open           = ide_drivers_open,
502         .read           = seq_read,
503         .llseek         = seq_lseek,
504         .release        = seq_release,
505 };
506
507 void proc_ide_create(void)
508 {
509         struct proc_dir_entry *entry;
510
511         if (!proc_ide_root)
512                 return;
513
514         create_proc_ide_interfaces();
515
516         entry = create_proc_entry("drivers", 0, proc_ide_root);
517         if (entry)
518                 entry->proc_fops = &ide_drivers_operations;
519 }
520
521 void proc_ide_destroy(void)
522 {
523         remove_proc_entry("ide/drivers", proc_ide_root);
524         remove_proc_entry("ide", NULL);
525 }