VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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  * The major reason this exists is to provide sufficient access
12  * to driver and config data, such that user-mode programs can
13  * be developed to handle chipset tuning for most PCI interfaces.
14  * This should provide better utilities, and less kernel bloat.
15  *
16  * The entire pci config space for a PCI interface chipset can be
17  * retrieved by just reading it.  e.g.    "cat /proc/ide3/config"
18  *
19  * To modify registers *safely*, do something like:
20  *   echo "P40:88" >/proc/ide/ide3/config
21  * That expression writes 0x88 to pci config register 0x40
22  * on the chip which controls ide3.  Multiple tuples can be issued,
23  * and the writes will be completed as an atomic set:
24  *   echo "P40:88 P41:35 P42:00 P43:00" >/proc/ide/ide3/config
25  *
26  * All numbers must be specified using pairs of ascii hex digits.
27  * It is important to note that these writes will be performed
28  * after waiting for the IDE controller (both interfaces)
29  * to be completely idle, to ensure no corruption of I/O in progress.
30  *
31  * Non-PCI registers can also be written, using "R" in place of "P"
32  * in the above examples.  The size of the port transfer is determined
33  * by the number of pairs of hex digits given for the data.  If a two
34  * digit value is given, the write will be a byte operation; if four
35  * digits are used, the write will be performed as a 16-bit operation;
36  * and if eight digits are specified, a 32-bit "dword" write will be
37  * performed.  Odd numbers of digits are not permitted.
38  *
39  * If there is an error *anywhere* in the string of registers/data
40  * then *none* of the writes will be performed.
41  *
42  * Drive/Driver settings can be retrieved by reading the drive's
43  * "settings" files.  e.g.    "cat /proc/ide0/hda/settings"
44  * To write a new value "val" into a specific setting "name", use:
45  *   echo "name:val" >/proc/ide/ide0/hda/settings
46  *
47  * Also useful, "cat /proc/ide0/hda/[identify, smart_values,
48  * smart_thresholds, capabilities]" will issue an IDENTIFY /
49  * PACKET_IDENTIFY / SMART_READ_VALUES / SMART_READ_THRESHOLDS /
50  * SENSE CAPABILITIES command to /dev/hda, and then dump out the
51  * returned data as 256 16-bit words.  The "hdparm" utility will
52  * be updated someday soon to use this mechanism.
53  *
54  * Feel free to develop and distribute fancy GUI configuration
55  * utilities for your favorite PCI chipsets.  I'll be working on
56  * one for the Promise 20246 someday soon.  -ml
57  *
58  */
59
60 #include <linux/config.h>
61 #include <linux/module.h>
62
63 #include <asm/uaccess.h>
64 #include <linux/errno.h>
65 #include <linux/sched.h>
66 #include <linux/proc_fs.h>
67 #include <linux/stat.h>
68 #include <linux/mm.h>
69 #include <linux/pci.h>
70 #include <linux/ctype.h>
71 #include <linux/hdreg.h>
72 #include <linux/ide.h>
73 #include <linux/seq_file.h>
74
75 #include <asm/io.h>
76
77 static int proc_ide_write_config(struct file *file, const char __user *buffer,
78                                  unsigned long count, void *data)
79 {
80         ide_hwif_t      *hwif = (ide_hwif_t *)data;
81         ide_hwgroup_t *mygroup = (ide_hwgroup_t *)(hwif->hwgroup);
82         ide_hwgroup_t *mategroup = NULL;
83         unsigned long timeout;
84         unsigned long flags;
85         const char *start = NULL, *msg = NULL;
86         struct entry { u32 val; u16 reg; u8 size; u8 pci; } *prog, *q, *r;
87         int want_pci = 0;
88         char *buf, *s;
89         int err;
90
91         if (hwif->mate && hwif->mate->hwgroup)
92                 mategroup = (ide_hwgroup_t *)(hwif->mate->hwgroup);
93
94         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
95                 return -EACCES;
96
97         if (count >= PAGE_SIZE)
98                 return -EINVAL;
99
100         s = buf = (char *)__get_free_page(GFP_USER);
101         if (!buf)
102                 return -ENOMEM;
103
104         err = -ENOMEM;
105         q = prog = (struct entry *)__get_free_page(GFP_USER);
106         if (!prog)
107                 goto out;
108
109         err = -EFAULT;
110         if (copy_from_user(buf, buffer, count))
111                 goto out1;
112
113         buf[count] = '\0';
114
115         while (isspace(*s))
116                 s++;
117
118         while (*s) {
119                 char *p;
120                 int digits;
121
122                 start = s;
123
124                 if ((char *)(q + 1) > (char *)prog + PAGE_SIZE) {
125                         msg = "too many entries";
126                         goto parse_error;
127                 }
128
129                 switch (*s++) {
130                         case 'R':       q->pci = 0;
131                                         break;
132                         case 'P':       q->pci = 1;
133                                         want_pci = 1;
134                                         break;
135                         default:        msg = "expected 'R' or 'P'";
136                                         goto parse_error;
137                 }
138
139                 q->reg = simple_strtoul(s, &p, 16);
140                 digits = p - s;
141                 if (!digits || digits > 4 || (q->pci && q->reg > 0xff)) {
142                         msg = "bad/missing register number";
143                         goto parse_error;
144                 }
145                 if (*p++ != ':') {
146                         msg = "missing ':'";
147                         goto parse_error;
148                 }
149                 q->val = simple_strtoul(p, &s, 16);
150                 digits = s - p;
151                 if (digits != 2 && digits != 4 && digits != 8) {
152                         msg = "bad data, 2/4/8 digits required";
153                         goto parse_error;
154                 }
155                 q->size = digits / 2;
156
157                 if (q->pci) {
158 #ifdef CONFIG_BLK_DEV_IDEPCI
159                         if (q->reg & (q->size - 1)) {
160                                 msg = "misaligned access";
161                                 goto parse_error;
162                         }
163 #else
164                         msg = "not a PCI device";
165                         goto parse_error;
166 #endif  /* CONFIG_BLK_DEV_IDEPCI */
167                 }
168
169                 q++;
170
171                 if (*s && !isspace(*s++)) {
172                         msg = "expected whitespace after data";
173                         goto parse_error;
174                 }
175                 while (isspace(*s))
176                         s++;
177         }
178
179         /*
180          * What follows below is fucking insane, even for IDE people.
181          * For now I've dealt with the obvious problems on the parsing
182          * side, but IMNSHO we should simply remove the write access
183          * to /proc/ide/.../config, killing that FPOS completely.
184          */
185
186         err = -EBUSY;
187         timeout = jiffies + (3 * HZ);
188         spin_lock_irqsave(&ide_lock, flags);
189         while (mygroup->busy ||
190                (mategroup && mategroup->busy)) {
191                 spin_unlock_irqrestore(&ide_lock, flags);
192                 if (time_after(jiffies, timeout)) {
193                         printk("/proc/ide/%s/config: channel(s) busy, cannot write\n", hwif->name);
194                         goto out1;
195                 }
196                 spin_lock_irqsave(&ide_lock, flags);
197         }
198
199 #ifdef CONFIG_BLK_DEV_IDEPCI
200         if (want_pci && (!hwif->pci_dev || hwif->pci_dev->vendor)) {
201                 spin_unlock_irqrestore(&ide_lock, flags);
202                 printk("proc_ide: PCI registers not accessible for %s\n",
203                         hwif->name);
204                 err = -EINVAL;
205                 goto out1;
206         }
207 #endif  /* CONFIG_BLK_DEV_IDEPCI */
208
209         for (r = prog; r < q; r++) {
210                 unsigned int reg = r->reg, val = r->val;
211                 if (r->pci) {
212 #ifdef CONFIG_BLK_DEV_IDEPCI
213                         int rc = 0;
214                         struct pci_dev *dev = hwif->pci_dev;
215                         switch (q->size) {
216                                 case 1: msg = "byte";
217                                         rc = pci_write_config_byte(dev, reg, val);
218                                         break;
219                                 case 2: msg = "word";
220                                         rc = pci_write_config_word(dev, reg, val);
221                                         break;
222                                 case 4: msg = "dword";
223                                         rc = pci_write_config_dword(dev, reg, val);
224                                         break;
225                         }
226                         if (rc) {
227                                 spin_unlock_irqrestore(&ide_lock, flags);
228                                 printk("proc_ide_write_config: error writing %s at bus %02x dev %02x reg 0x%x value 0x%x\n",
229                                         msg, dev->bus->number, dev->devfn, reg, val);
230                                 printk("proc_ide_write_config: error %d\n", rc);
231                                 err = -EIO;
232                                 goto out1;
233                         }
234 #endif  /* CONFIG_BLK_DEV_IDEPCI */
235                 } else {        /* not pci */
236                         switch (r->size) {
237                                 case 1: hwif->OUTB(val, reg);
238                                         break;
239                                 case 2: hwif->OUTW(val, reg);
240                                         break;
241                                 case 4: hwif->OUTL(val, reg);
242                                         break;
243                         }
244                 }
245         }
246         spin_unlock_irqrestore(&ide_lock, flags);
247         err = count;
248 out1:
249         free_page((unsigned long)prog);
250 out:
251         free_page((unsigned long)buf);
252         return err;
253
254 parse_error:
255         printk("parse error\n");
256         printk("proc_ide: error: %s: '%s'\n", msg, start);
257         err = -EINVAL;
258         goto out1;
259 }
260
261 static int proc_ide_read_config
262         (char *page, char **start, off_t off, int count, int *eof, void *data)
263 {
264         char            *out = page;
265         int             len;
266
267 #ifdef CONFIG_BLK_DEV_IDEPCI
268         ide_hwif_t      *hwif = (ide_hwif_t *)data;
269         struct pci_dev  *dev = hwif->pci_dev;
270         if ((hwif->pci_dev && hwif->pci_dev->vendor) && dev && dev->bus) {
271                 int reg = 0;
272
273                 out += sprintf(out, "pci bus %02x device %02x vendor %04x "
274                                 "device %04x channel %d\n",
275                         dev->bus->number, dev->devfn,
276                         hwif->pci_dev->vendor, hwif->pci_dev->device,
277                         hwif->channel);
278                 do {
279                         u8 val;
280                         int rc = pci_read_config_byte(dev, reg, &val);
281                         if (rc) {
282                                 printk("proc_ide_read_config: error %d reading"
283                                         " bus %02x dev %02x reg 0x%02x\n",
284                                         rc, dev->bus->number, dev->devfn, reg);
285                                 out += sprintf(out, "??%c",
286                                         (++reg & 0xf) ? ' ' : '\n');
287                         } else
288                                 out += sprintf(out, "%02x%c",
289                                         val, (++reg & 0xf) ? ' ' : '\n');
290                 } while (reg < 0x100);
291         } else
292 #endif  /* CONFIG_BLK_DEV_IDEPCI */
293                 out += sprintf(out, "(none)\n");
294         len = out - page;
295         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
296 }
297
298 static int proc_ide_read_imodel
299         (char *page, char **start, off_t off, int count, int *eof, void *data)
300 {
301         ide_hwif_t      *hwif = (ide_hwif_t *) data;
302         int             len;
303         const char      *name;
304
305         /*
306          * Neither ide_unknown nor ide_forced should be set at this point.
307          */
308         switch (hwif->chipset) {
309                 case ide_generic:       name = "generic";       break;
310                 case ide_pci:           name = "pci";           break;
311                 case ide_cmd640:        name = "cmd640";        break;
312                 case ide_dtc2278:       name = "dtc2278";       break;
313                 case ide_ali14xx:       name = "ali14xx";       break;
314                 case ide_qd65xx:        name = "qd65xx";        break;
315                 case ide_umc8672:       name = "umc8672";       break;
316                 case ide_ht6560b:       name = "ht6560b";       break;
317                 case ide_pdc4030:       name = "pdc4030";       break;
318                 case ide_rz1000:        name = "rz1000";        break;
319                 case ide_trm290:        name = "trm290";        break;
320                 case ide_cmd646:        name = "cmd646";        break;
321                 case ide_cy82c693:      name = "cy82c693";      break;
322                 case ide_4drives:       name = "4drives";       break;
323                 case ide_pmac:          name = "mac-io";        break;
324                 default:                name = "(unknown)";     break;
325         }
326         len = sprintf(page, "%s\n", name);
327         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
328 }
329
330 static int proc_ide_read_mate
331         (char *page, char **start, off_t off, int count, int *eof, void *data)
332 {
333         ide_hwif_t      *hwif = (ide_hwif_t *) data;
334         int             len;
335
336         if (hwif && hwif->mate && hwif->mate->present)
337                 len = sprintf(page, "%s\n", hwif->mate->name);
338         else
339                 len = sprintf(page, "(none)\n");
340         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
341 }
342
343 static int proc_ide_read_channel
344         (char *page, char **start, off_t off, int count, int *eof, void *data)
345 {
346         ide_hwif_t      *hwif = (ide_hwif_t *) data;
347         int             len;
348
349         page[0] = hwif->channel ? '1' : '0';
350         page[1] = '\n';
351         len = 2;
352         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
353 }
354
355 static int proc_ide_read_identify
356         (char *page, char **start, off_t off, int count, int *eof, void *data)
357 {
358         ide_drive_t     *drive = (ide_drive_t *)data;
359         int             len = 0, i = 0;
360         int             err = 0;
361
362         len = sprintf(page, "\n");
363         
364         if (drive)
365         {
366                 unsigned short *val = (unsigned short *) page;
367                 
368                 /*
369                  *      The current code can't handle a driverless
370                  *      identify query taskfile. Now the right fix is
371                  *      to add a 'default' driver but that is a bit
372                  *      more work. 
373                  *
374                  *      FIXME: this has to be fixed for hotswap devices
375                  */
376                  
377                 if(DRIVER(drive))
378                         err = taskfile_lib_get_identify(drive, page);
379                 else    /* This relies on the ID changes */
380                         val = (unsigned short *)drive->id;
381
382                 if(!err)
383                 {                                               
384                         char *out = ((char *)page) + (SECTOR_WORDS * 4);
385                         page = out;
386                         do {
387                                 out += sprintf(out, "%04x%c",
388                                         le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
389                                 val += 1;
390                         } while (i < (SECTOR_WORDS * 2));
391                         len = out - page;
392                 }
393         }
394         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
395 }
396
397 static int proc_ide_read_settings
398         (char *page, char **start, off_t off, int count, int *eof, void *data)
399 {
400         ide_drive_t     *drive = (ide_drive_t *) data;
401         ide_settings_t  *setting = (ide_settings_t *) drive->settings;
402         char            *out = page;
403         int             len, rc, mul_factor, div_factor;
404
405         down(&ide_setting_sem);
406         out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
407         out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
408         while(setting) {
409                 mul_factor = setting->mul_factor;
410                 div_factor = setting->div_factor;
411                 out += sprintf(out, "%-24s", setting->name);
412                 if ((rc = ide_read_setting(drive, setting)) >= 0)
413                         out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
414                 else
415                         out += sprintf(out, "%-16s", "write-only");
416                 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
417                 if (setting->rw & SETTING_READ)
418                         out += sprintf(out, "r");
419                 if (setting->rw & SETTING_WRITE)
420                         out += sprintf(out, "w");
421                 out += sprintf(out, "\n");
422                 setting = setting->next;
423         }
424         len = out - page;
425         up(&ide_setting_sem);
426         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
427 }
428
429 #define MAX_LEN 30
430
431 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
432                                    unsigned long count, void *data)
433 {
434         ide_drive_t     *drive = (ide_drive_t *) data;
435         char            name[MAX_LEN + 1];
436         int             for_real = 0;
437         unsigned long   n;
438         ide_settings_t  *setting;
439         char *buf, *s;
440
441         if (!capable(CAP_SYS_ADMIN))
442                 return -EACCES;
443
444         if (count >= PAGE_SIZE)
445                 return -EINVAL;
446
447         s = buf = (char *)__get_free_page(GFP_USER);
448         if (!buf)
449                 return -ENOMEM;
450
451         if (copy_from_user(buf, buffer, count)) {
452                 free_page((unsigned long)buf);
453                 return -EFAULT;
454         }
455
456         buf[count] = '\0';
457
458         /*
459          * Skip over leading whitespace
460          */
461         while (count && isspace(*s)) {
462                 --count;
463                 ++s;
464         }
465         /*
466          * Do one full pass to verify all parameters,
467          * then do another to actually write the new settings.
468          */
469         do {
470                 char *p = s;
471                 n = count;
472                 while (n > 0) {
473                         unsigned val;
474                         char *q = p;
475
476                         while (n > 0 && *p != ':') {
477                                 --n;
478                                 p++;
479                         }
480                         if (*p != ':')
481                                 goto parse_error;
482                         if (p - q > MAX_LEN)
483                                 goto parse_error;
484                         memcpy(name, q, p - q);
485                         name[p - q] = 0;
486
487                         if (n > 0) {
488                                 --n;
489                                 p++;
490                         } else
491                                 goto parse_error;
492
493                         val = simple_strtoul(p, &q, 10);
494                         n -= q - p;
495                         p = q;
496                         if (n > 0 && !isspace(*p))
497                                 goto parse_error;
498                         while (n > 0 && isspace(*p)) {
499                                 --n;
500                                 ++p;
501                         }
502
503                         down(&ide_setting_sem);
504                         setting = ide_find_setting_by_name(drive, name);
505                         if (!setting)
506                         {
507                                 up(&ide_setting_sem);
508                                 goto parse_error;
509                         }
510                         if (for_real)
511                                 ide_write_setting(drive, setting, val * setting->div_factor / setting->mul_factor);
512                         up(&ide_setting_sem);
513                 }
514         } while (!for_real++);
515         free_page((unsigned long)buf);
516         return count;
517 parse_error:
518         free_page((unsigned long)buf);
519         printk("proc_ide_write_settings(): parse error\n");
520         return -EINVAL;
521 }
522
523 int proc_ide_read_capacity
524         (char *page, char **start, off_t off, int count, int *eof, void *data)
525 {
526         ide_drive_t     *drive = (ide_drive_t *) data;
527         int             len;
528
529         len = sprintf(page,"%llu\n",
530                       (long long) (DRIVER(drive)->capacity(drive)));
531         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
532 }
533
534 int proc_ide_read_geometry
535         (char *page, char **start, off_t off, int count, int *eof, void *data)
536 {
537         ide_drive_t     *drive = (ide_drive_t *) data;
538         char            *out = page;
539         int             len;
540
541         out += sprintf(out,"physical     %d/%d/%d\n",
542                         drive->cyl, drive->head, drive->sect);
543         out += sprintf(out,"logical      %d/%d/%d\n",
544                         drive->bios_cyl, drive->bios_head, drive->bios_sect);
545
546         len = out - page;
547         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
548 }
549
550 EXPORT_SYMBOL(proc_ide_read_geometry);
551
552 static int proc_ide_read_dmodel
553         (char *page, char **start, off_t off, int count, int *eof, void *data)
554 {
555         ide_drive_t     *drive = (ide_drive_t *) data;
556         struct hd_driveid *id = drive->id;
557         int             len;
558
559         len = sprintf(page, "%.40s\n",
560                 (id && id->model[0]) ? (char *)id->model : "(none)");
561         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
562 }
563
564 static int proc_ide_read_driver
565         (char *page, char **start, off_t off, int count, int *eof, void *data)
566 {
567         ide_drive_t     *drive = (ide_drive_t *) data;
568         ide_driver_t    *driver = drive->driver;
569         int             len;
570
571         len = sprintf(page, "%s version %s\n",
572                         driver->name, driver->version);
573         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
574 }
575
576 static int proc_ide_write_driver
577         (struct file *file, const char __user *buffer, unsigned long count, void *data)
578 {
579         ide_drive_t     *drive = (ide_drive_t *) data;
580         char name[32];
581
582         if (!capable(CAP_SYS_ADMIN))
583                 return -EACCES;
584         if (count > 31)
585                 count = 31;
586         if (copy_from_user(name, buffer, count))
587                 return -EFAULT;
588         name[count] = '\0';
589         if (ide_replace_subdriver(drive, name))
590                 return -EINVAL;
591         return count;
592 }
593
594 static int proc_ide_read_media
595         (char *page, char **start, off_t off, int count, int *eof, void *data)
596 {
597         ide_drive_t     *drive = (ide_drive_t *) data;
598         const char      *media;
599         int             len;
600
601         switch (drive->media) {
602                 case ide_disk:  media = "disk\n";
603                                 break;
604                 case ide_cdrom: media = "cdrom\n";
605                                 break;
606                 case ide_tape:  media = "tape\n";
607                                 break;
608                 case ide_floppy:media = "floppy\n";
609                                 break;
610                 default:        media = "UNKNOWN\n";
611                                 break;
612         }
613         strcpy(page,media);
614         len = strlen(media);
615         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
616 }
617
618 static ide_proc_entry_t generic_drive_entries[] = {
619         { "driver",     S_IFREG|S_IRUGO,        proc_ide_read_driver,   proc_ide_write_driver },
620         { "identify",   S_IFREG|S_IRUSR,        proc_ide_read_identify, NULL },
621         { "media",      S_IFREG|S_IRUGO,        proc_ide_read_media,    NULL },
622         { "model",      S_IFREG|S_IRUGO,        proc_ide_read_dmodel,   NULL },
623         { "settings",   S_IFREG|S_IRUSR|S_IWUSR,proc_ide_read_settings, proc_ide_write_settings },
624         { NULL, 0, NULL, NULL }
625 };
626
627 void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
628 {
629         struct proc_dir_entry *ent;
630
631         if (!dir || !p)
632                 return;
633         while (p->name != NULL) {
634                 ent = create_proc_entry(p->name, p->mode, dir);
635                 if (!ent) return;
636                 ent->nlink = 1;
637                 ent->data = data;
638                 ent->read_proc = p->read_proc;
639                 ent->write_proc = p->write_proc;
640                 p++;
641         }
642 }
643
644 void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
645 {
646         if (!dir || !p)
647                 return;
648         while (p->name != NULL) {
649                 remove_proc_entry(p->name, dir);
650                 p++;
651         }
652 }
653
654 static void create_proc_ide_drives(ide_hwif_t *hwif)
655 {
656         int     d;
657         struct proc_dir_entry *ent;
658         struct proc_dir_entry *parent = hwif->proc;
659         char name[64];
660
661         for (d = 0; d < MAX_DRIVES; d++) {
662                 ide_drive_t *drive = &hwif->drives[d];
663
664                 if (!drive->present)
665                         continue;
666                 if (drive->proc)
667                         continue;
668
669                 drive->proc = proc_mkdir(drive->name, parent);
670                 if (drive->proc)
671                         ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
672                 sprintf(name,"ide%d/%s", (drive->name[2]-'a')/2, drive->name);
673                 ent = proc_symlink(drive->name, proc_ide_root, name);
674                 if (!ent) return;
675         }
676 }
677
678 static void destroy_proc_ide_device(ide_hwif_t *hwif, ide_drive_t *drive)
679 {
680         ide_driver_t *driver = drive->driver;
681
682         if (drive->proc) {
683                 ide_remove_proc_entries(drive->proc, driver->proc);
684                 ide_remove_proc_entries(drive->proc, generic_drive_entries);
685                 remove_proc_entry(drive->name, proc_ide_root);
686                 remove_proc_entry(drive->name, hwif->proc);
687                 drive->proc = NULL;
688         }
689 }
690
691 void destroy_proc_ide_drives(ide_hwif_t *hwif)
692 {
693         int     d;
694
695         for (d = 0; d < MAX_DRIVES; d++) {
696                 ide_drive_t *drive = &hwif->drives[d];
697                 if (drive->proc)
698                         destroy_proc_ide_device(hwif, drive);
699         }
700 }
701
702 static ide_proc_entry_t hwif_entries[] = {
703         { "channel",    S_IFREG|S_IRUGO,        proc_ide_read_channel,  NULL },
704         { "config",     S_IFREG|S_IRUGO|S_IWUSR,proc_ide_read_config,   proc_ide_write_config },
705         { "mate",       S_IFREG|S_IRUGO,        proc_ide_read_mate,     NULL },
706         { "model",      S_IFREG|S_IRUGO,        proc_ide_read_imodel,   NULL },
707         { NULL, 0, NULL, NULL }
708 };
709
710 void create_proc_ide_interfaces(void)
711 {
712         int     h;
713
714         for (h = 0; h < MAX_HWIFS; h++) {
715                 ide_hwif_t *hwif = &ide_hwifs[h];
716
717                 if (!hwif->present)
718                         continue;
719                 if (!hwif->proc) {
720                         hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
721                         if (!hwif->proc)
722                                 return;
723                         ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
724                 }
725                 create_proc_ide_drives(hwif);
726         }
727 }
728
729 EXPORT_SYMBOL(create_proc_ide_interfaces);
730
731 #ifdef CONFIG_BLK_DEV_IDEPCI
732 void ide_pci_create_host_proc(const char *name, get_info_t *get_info)
733 {
734         create_proc_info_entry(name, 0, proc_ide_root, get_info);
735 }
736
737 EXPORT_SYMBOL_GPL(ide_pci_create_host_proc);
738 #endif
739
740 void destroy_proc_ide_interfaces(void)
741 {
742         int     h;
743
744         for (h = 0; h < MAX_HWIFS; h++) {
745                 ide_hwif_t *hwif = &ide_hwifs[h];
746                 int exist = (hwif->proc != NULL);
747 #if 0
748                 if (!hwif->present)
749                         continue;
750 #endif
751                 if (exist) {
752                         destroy_proc_ide_drives(hwif);
753                         ide_remove_proc_entries(hwif->proc, hwif_entries);
754                         remove_proc_entry(hwif->name, proc_ide_root);
755                         hwif->proc = NULL;
756                 } else
757                         continue;
758         }
759 }
760
761 EXPORT_SYMBOL(destroy_proc_ide_interfaces);
762
763 extern struct seq_operations ide_drivers_op;
764 static int ide_drivers_open(struct inode *inode, struct file *file)
765 {
766         return seq_open(file, &ide_drivers_op);
767 }
768 static struct file_operations ide_drivers_operations = {
769         .open           = ide_drivers_open,
770         .read           = seq_read,
771         .llseek         = seq_lseek,
772         .release        = seq_release,
773 };
774
775 void proc_ide_create(void)
776 {
777         struct proc_dir_entry *entry;
778
779         if (!proc_ide_root)
780                 return;
781
782         create_proc_ide_interfaces();
783
784         entry = create_proc_entry("drivers", 0, proc_ide_root);
785         if (entry)
786                 entry->proc_fops = &ide_drivers_operations;
787 }
788
789 void proc_ide_destroy(void)
790 {
791         remove_proc_entry("ide/drivers", proc_ide_root);
792         destroy_proc_ide_interfaces();
793         remove_proc_entry("ide", NULL);
794 }