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