upgrade to linux 2.6.9-1.11_FC2
[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 do_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_from_key(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_write_config(struct file *file, const char __user *buffer,
262                                  unsigned long count, void *data)
263 {
264         int ret;
265         down(&ide_cfg_sem);
266         ret = do_proc_ide_write_config(file, buffer, count, data);
267         up(&ide_cfg_sem);
268         return ret;
269 }
270
271 static int proc_ide_read_config
272         (char *page, char **start, off_t off, int count, int *eof, void *data)
273 {
274         char            *out = page;
275         int             len;
276
277 #ifdef CONFIG_BLK_DEV_IDEPCI
278         ide_hwif_t      *hwif = ide_hwif_from_key(data);
279
280         down(&ide_cfg_sem);
281         hwif = ide_hwif_from_key(data);
282         
283         if(hwif)
284         {
285                 struct pci_dev  *dev = hwif->pci_dev;
286                 if ((hwif->pci_dev && hwif->pci_dev->vendor) && dev && dev->bus) 
287                 {
288                         int reg = 0;
289
290                         out += sprintf(out, "pci bus %02x device %02x vendor %04x "
291                                         "device %04x channel %d\n",
292                                 dev->bus->number, dev->devfn,
293                                 hwif->pci_dev->vendor, hwif->pci_dev->device,
294                                 hwif->channel);
295                         do {
296                                 u8 val;
297                                 int rc = pci_read_config_byte(dev, reg, &val);
298                                 if (rc) {
299                                         printk("proc_ide_read_config: error %d reading"
300                                                 " bus %02x dev %02x reg 0x%02x\n",
301                                                 rc, dev->bus->number, dev->devfn, reg);
302                                         out += sprintf(out, "??%c",
303                                                 (++reg & 0xf) ? ' ' : '\n');
304                                 } else
305                                         out += sprintf(out, "%02x%c",
306                                                 val, (++reg & 0xf) ? ' ' : '\n');
307                         } while (reg < 0x100);
308                 }
309                 else
310                         out += sprintf(out, "(none)\n");
311         }
312 #else   /* CONFIG_BLK_DEV_IDEPCI */
313         out += sprintf(out, "(none)\n");
314 #endif  
315         up(&ide_cfg_sem);
316         len = out - page;
317         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
318 }
319
320 static int proc_ide_read_imodel
321         (char *page, char **start, off_t off, int count, int *eof, void *data)
322 {
323         ide_hwif_t      *hwif;
324         int             len;
325         const char      *name = "";
326
327         down(&ide_cfg_sem);
328         hwif = ide_hwif_from_key(data); 
329         if(hwif)
330         {
331                 /*
332                  * Neither ide_unknown nor ide_forced should be set at this point.
333                  */
334                 switch (hwif->chipset) {
335                         case ide_generic:       name = "generic";       break;
336                         case ide_pci:           name = "pci";           break;
337                         case ide_cmd640:        name = "cmd640";        break;
338                         case ide_dtc2278:       name = "dtc2278";       break;
339                         case ide_ali14xx:       name = "ali14xx";       break;
340                         case ide_qd65xx:        name = "qd65xx";        break;
341                         case ide_umc8672:       name = "umc8672";       break;
342                         case ide_ht6560b:       name = "ht6560b";       break;
343                         case ide_pdc4030:       name = "pdc4030";       break;
344                         case ide_rz1000:        name = "rz1000";        break;
345                         case ide_trm290:        name = "trm290";        break;
346                         case ide_cmd646:        name = "cmd646";        break;
347                         case ide_cy82c693:      name = "cy82c693";      break;
348                         case ide_4drives:       name = "4drives";       break;
349                         case ide_pmac:          name = "mac-io";        break;
350                         default:                name = "(unknown)";     break;
351                 }
352         }
353         len = sprintf(page, "%s\n", name);
354         up(&ide_cfg_sem);
355         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
356 }
357
358 static int proc_ide_read_mate
359         (char *page, char **start, off_t off, int count, int *eof, void *data)
360 {
361         ide_hwif_t      *hwif;
362         int             len;
363
364         down(&ide_cfg_sem);
365         hwif = ide_hwif_from_key(data); 
366         if (hwif && hwif->mate && hwif->mate->present)
367                 len = sprintf(page, "%s\n", hwif->mate->name);
368         else
369                 len = sprintf(page, "(none)\n");
370         up(&ide_cfg_sem);
371         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
372 }
373
374 static int proc_ide_read_channel
375         (char *page, char **start, off_t off, int count, int *eof, void *data)
376 {
377         ide_hwif_t      *hwif;
378         int             len = 0;
379
380         down(&ide_cfg_sem);
381         hwif = ide_hwif_from_key(data); 
382         if(hwif) {
383                 page[0] = hwif->channel ? '1' : '0';
384                 page[1] = '\n';
385                 len = 2;
386         }
387         else
388                 page[0] = '\n';
389         up(&ide_cfg_sem);
390         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
391 }
392
393 static int proc_ide_read_identify
394         (char *page, char **start, off_t off, int count, int *eof, void *data)
395 {
396         ide_drive_t     *drive;
397         int             len = 0, i = 0;
398         int             err = 0;
399
400         len = sprintf(page, "\n");
401
402         down(&ide_cfg_sem);
403         drive = ide_drive_from_key(data);
404         
405         if (drive) {
406                 unsigned short *val = (unsigned short *) page;
407
408                 BUG_ON(!drive->driver);
409
410                 err = taskfile_lib_get_identify(drive, page);
411                 if (!err) {
412                         char *out = ((char *)page) + (SECTOR_WORDS * 4);
413                         page = out;
414                         do {
415                                 out += sprintf(out, "%04x%c",
416                                         le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
417                                 val += 1;
418                         } while (i < (SECTOR_WORDS * 2));
419                         len = out - page;
420                 }
421         }
422         up(&ide_cfg_sem);
423         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
424 }
425
426 static int proc_ide_read_settings
427         (char *page, char **start, off_t off, int count, int *eof, void *data)
428 {
429         ide_drive_t     *drive;
430         ide_settings_t  *setting;
431         char            *out = page;
432         int             len, rc, mul_factor, div_factor;
433         
434         down(&ide_cfg_sem);
435         drive = ide_drive_from_key(data);
436         
437         if(drive == NULL)
438         {
439                 up(&ide_cfg_sem);
440                 return -EIO;
441         }
442
443         setting = (ide_settings_t *) drive->settings;
444         down(&ide_setting_sem);
445         out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
446         out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
447         while(setting) {
448                 mul_factor = setting->mul_factor;
449                 div_factor = setting->div_factor;
450                 out += sprintf(out, "%-24s", setting->name);
451                 if ((rc = ide_read_setting(drive, setting)) >= 0)
452                         out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
453                 else
454                         out += sprintf(out, "%-16s", "write-only");
455                 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
456                 if (setting->rw & SETTING_READ)
457                         out += sprintf(out, "r");
458                 if (setting->rw & SETTING_WRITE)
459                         out += sprintf(out, "w");
460                 out += sprintf(out, "\n");
461                 setting = setting->next;
462         }
463         len = out - page;
464         up(&ide_setting_sem);
465         up(&ide_cfg_sem);
466         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
467 }
468
469 #define MAX_LEN 30
470
471 static int do_proc_ide_write_settings(struct file *file, const char __user *buffer,
472                                    unsigned long count, void *data)
473 {
474         ide_drive_t     *drive = ide_drive_from_key(data);
475         char            name[MAX_LEN + 1];
476         int             for_real = 0;
477         unsigned long   n;
478         ide_settings_t  *setting;
479         char *buf, *s;
480
481         if (!capable(CAP_SYS_ADMIN))
482                 return -EACCES;
483                 
484         if (drive == NULL)
485                 return -EIO;
486
487         if (count >= PAGE_SIZE)
488                 return -EINVAL;
489
490         s = buf = (char *)__get_free_page(GFP_USER);
491         if (!buf)
492                 return -ENOMEM;
493
494         if (copy_from_user(buf, buffer, count)) {
495                 free_page((unsigned long)buf);
496                 return -EFAULT;
497         }
498
499         buf[count] = '\0';
500
501         /*
502          * Skip over leading whitespace
503          */
504         while (count && isspace(*s)) {
505                 --count;
506                 ++s;
507         }
508         /*
509          * Do one full pass to verify all parameters,
510          * then do another to actually write the new settings.
511          */
512         do {
513                 char *p = s;
514                 n = count;
515                 while (n > 0) {
516                         unsigned val;
517                         char *q = p;
518
519                         while (n > 0 && *p != ':') {
520                                 --n;
521                                 p++;
522                         }
523                         if (*p != ':')
524                                 goto parse_error;
525                         if (p - q > MAX_LEN)
526                                 goto parse_error;
527                         memcpy(name, q, p - q);
528                         name[p - q] = 0;
529
530                         if (n > 0) {
531                                 --n;
532                                 p++;
533                         } else
534                                 goto parse_error;
535
536                         val = simple_strtoul(p, &q, 10);
537                         n -= q - p;
538                         p = q;
539                         if (n > 0 && !isspace(*p))
540                                 goto parse_error;
541                         while (n > 0 && isspace(*p)) {
542                                 --n;
543                                 ++p;
544                         }
545
546                         down(&ide_setting_sem);
547                         setting = ide_find_setting_by_name(drive, name);
548                         if (!setting)
549                         {
550                                 up(&ide_setting_sem);
551                                 goto parse_error;
552                         }
553                         if (for_real)
554                                 ide_write_setting(drive, setting, val * setting->div_factor / setting->mul_factor);
555                         up(&ide_setting_sem);
556                 }
557         } while (!for_real++);
558         free_page((unsigned long)buf);
559         return count;
560 parse_error:
561         free_page((unsigned long)buf);
562         printk("proc_ide_write_settings(): parse error\n");
563         return -EINVAL;
564 }
565
566 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
567                                    unsigned long count, void *data)
568 {
569         int ret;
570         
571         down(&ide_cfg_sem);
572         ret = do_proc_ide_write_settings(file, buffer, count, data);
573         up(&ide_cfg_sem);
574         return ret;
575 }
576
577 int proc_ide_read_capacity
578         (char *page, char **start, off_t off, int count, int *eof, void *data)
579 {
580         ide_drive_t     *drive;
581         int             len;
582
583         down(&ide_cfg_sem);
584         drive = ide_drive_from_key(data);
585         if(drive == NULL)
586         {
587                 up(&ide_cfg_sem);
588                 return -EIO;
589         }
590                 
591         len = sprintf(page,"%llu\n",
592                       (long long) (DRIVER(drive)->capacity(drive)));
593         up(&ide_cfg_sem);
594         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
595 }
596
597 int proc_ide_read_geometry
598         (char *page, char **start, off_t off, int count, int *eof, void *data)
599 {
600         ide_drive_t     *drive;
601         char            *out = page;
602         int             len;
603         
604         down(&ide_cfg_sem);
605         drive = ide_drive_from_key(data);
606         if(drive == NULL)
607         {
608                 up(&ide_cfg_sem);
609                 return -EIO;
610         }
611
612         out += sprintf(out,"physical     %d/%d/%d\n",
613                         drive->cyl, drive->head, drive->sect);
614         out += sprintf(out,"logical      %d/%d/%d\n",
615                         drive->bios_cyl, drive->bios_head, drive->bios_sect);
616
617         len = out - page;
618         up(&ide_cfg_sem);
619         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
620 }
621
622 EXPORT_SYMBOL(proc_ide_read_geometry);
623
624 static int proc_ide_read_dmodel
625         (char *page, char **start, off_t off, int count, int *eof, void *data)
626 {
627         ide_drive_t     *drive;
628         struct hd_driveid *id;
629         int             len;
630
631         down(&ide_cfg_sem);
632         drive = ide_drive_from_key(data);
633         if(drive == NULL)
634         {
635                 up(&ide_cfg_sem);
636                 return -EIO;
637         }
638
639         id = drive->id;
640         len = sprintf(page, "%.40s\n",
641                 (id && id->model[0]) ? (char *)id->model : "(none)");
642         up(&ide_cfg_sem);
643         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
644 }
645
646 static int proc_ide_read_driver
647         (char *page, char **start, off_t off, int count, int *eof, void *data)
648 {
649         ide_drive_t     *drive;
650         ide_driver_t    *driver;
651         int             len;
652
653         down(&ide_cfg_sem);
654         drive = ide_drive_from_key(data);
655         if(drive == NULL)
656         {
657                 up(&ide_cfg_sem);
658                 return -EIO;
659         }
660                         
661         driver = drive->driver;
662
663         len = sprintf(page, "%s version %s\n",
664                         driver->name, driver->version);
665         up(&ide_cfg_sem);
666         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
667 }
668
669 static int proc_ide_read_media
670         (char *page, char **start, off_t off, int count, int *eof, void *data)
671 {
672         ide_drive_t     *drive;
673         const char      *media;
674         int             len;
675
676         down(&ide_cfg_sem);
677         drive = ide_drive_from_key(data);
678         if(drive == NULL)
679         {
680                 up(&ide_cfg_sem);
681                 return -EIO;
682         }
683
684         switch (drive->media) {
685                 case ide_disk:  media = "disk\n";
686                                 break;
687                 case ide_cdrom: media = "cdrom\n";
688                                 break;
689                 case ide_tape:  media = "tape\n";
690                                 break;
691                 case ide_floppy:media = "floppy\n";
692                                 break;
693                 default:        media = "UNKNOWN\n";
694                                 break;
695         }
696         strcpy(page,media);
697         len = strlen(media);
698         up(&ide_cfg_sem);
699         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
700 }
701
702 static ide_proc_entry_t generic_drive_entries[] = {
703         { "driver",     S_IFREG|S_IRUGO,        proc_ide_read_driver,   NULL },
704         { "identify",   S_IFREG|S_IRUSR,        proc_ide_read_identify, NULL },
705         { "media",      S_IFREG|S_IRUGO,        proc_ide_read_media,    NULL },
706         { "model",      S_IFREG|S_IRUGO,        proc_ide_read_dmodel,   NULL },
707         { "settings",   S_IFREG|S_IRUSR|S_IWUSR,proc_ide_read_settings, proc_ide_write_settings },
708         { NULL, 0, NULL, NULL }
709 };
710
711 void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
712 {
713         struct proc_dir_entry *ent;
714
715         if (!dir || !p)
716                 return;
717         while (p->name != NULL) {
718                 ent = create_proc_entry(p->name, p->mode, dir);
719                 if (!ent) return;
720                 ent->nlink = 1;
721                 ent->data = data;
722                 ent->read_proc = p->read_proc;
723                 ent->write_proc = p->write_proc;
724                 p++;
725         }
726 }
727
728 void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
729 {
730         if (!dir || !p)
731                 return;
732         while (p->name != NULL) {
733                 remove_proc_entry(p->name, dir);
734                 p++;
735         }
736 }
737
738 static void create_proc_ide_drives(ide_hwif_t *hwif)
739 {
740         int     d;
741         struct proc_dir_entry *ent;
742         struct proc_dir_entry *parent = hwif->proc;
743         char name[64];
744
745         for (d = 0; d < MAX_DRIVES; d++) {
746                 ide_drive_t *drive = &hwif->drives[d];
747
748                 if (!drive->present)
749                         continue;
750                 if (drive->proc)
751                         continue;
752
753                 drive->proc = proc_mkdir(drive->name, parent);
754                 if (drive->proc)
755                         ide_add_proc_entries(drive->proc, generic_drive_entries, ide_drive_to_key(drive));
756                 sprintf(name,"ide%d/%s", (drive->name[2]-'a')/2, drive->name);
757                 ent = proc_symlink(drive->name, proc_ide_root, name);
758                 if (!ent) return;
759         }
760 }
761
762 static void destroy_proc_ide_device(ide_hwif_t *hwif, ide_drive_t *drive)
763 {
764         ide_driver_t *driver = drive->driver;
765
766         if (drive->proc) {
767                 ide_remove_proc_entries(drive->proc, driver->proc);
768                 ide_remove_proc_entries(drive->proc, generic_drive_entries);
769                 remove_proc_entry(drive->name, proc_ide_root);
770                 remove_proc_entry(drive->name, hwif->proc);
771                 drive->proc = NULL;
772         }
773 }
774
775 void destroy_proc_ide_drives(ide_hwif_t *hwif)
776 {
777         int     d;
778
779         for (d = 0; d < MAX_DRIVES; d++) {
780                 ide_drive_t *drive = &hwif->drives[d];
781                 if (drive->proc)
782                         destroy_proc_ide_device(hwif, drive);
783         }
784 }
785
786 static ide_proc_entry_t hwif_entries[] = {
787         { "channel",    S_IFREG|S_IRUGO,        proc_ide_read_channel,  NULL },
788         { "config",     S_IFREG|S_IRUGO|S_IWUSR,proc_ide_read_config,   proc_ide_write_config },
789         { "mate",       S_IFREG|S_IRUGO,        proc_ide_read_mate,     NULL },
790         { "model",      S_IFREG|S_IRUGO,        proc_ide_read_imodel,   NULL },
791         { NULL, 0, NULL, NULL }
792 };
793
794 void create_proc_ide_interfaces(void)
795 {
796         int     h;
797
798         for (h = 0; h < MAX_HWIFS; h++) {
799                 ide_hwif_t *hwif = &ide_hwifs[h];
800
801                 if (!hwif->present)
802                         continue;
803                 if (!hwif->proc) {
804                         hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
805                         if (!hwif->proc)
806                                 return;
807                         ide_add_proc_entries(hwif->proc, hwif_entries, ide_hwif_to_key(hwif));
808                 }
809                 create_proc_ide_drives(hwif);
810         }
811 }
812
813 EXPORT_SYMBOL(create_proc_ide_interfaces);
814
815 #ifdef CONFIG_BLK_DEV_IDEPCI
816 void ide_pci_create_host_proc(const char *name, get_info_t *get_info)
817 {
818         create_proc_info_entry(name, 0, proc_ide_root, get_info);
819 }
820
821 EXPORT_SYMBOL_GPL(ide_pci_create_host_proc);
822 #endif
823
824 void destroy_proc_ide_interface(ide_hwif_t *hwif)
825 {
826         int exist = (hwif->proc != NULL);
827 #if 0
828         if (!hwif->present)
829                 continue;
830 #endif
831         if (exist) {
832                 destroy_proc_ide_drives(hwif);
833                 ide_remove_proc_entries(hwif->proc, hwif_entries);
834                 remove_proc_entry(hwif->name, proc_ide_root);
835                 hwif->proc = NULL;
836         }
837 }
838
839 EXPORT_SYMBOL(destroy_proc_ide_interface);
840
841 static void destroy_proc_ide_interfaces(void)
842 {
843         int     h;
844
845         for (h = 0; h < MAX_HWIFS; h++) {
846                 ide_hwif_t *hwif = &ide_hwifs[h];
847                 destroy_proc_ide_interface(hwif);
848         }
849 }
850
851 extern struct seq_operations ide_drivers_op;
852 static int ide_drivers_open(struct inode *inode, struct file *file)
853 {
854         return seq_open(file, &ide_drivers_op);
855 }
856 static struct file_operations ide_drivers_operations = {
857         .open           = ide_drivers_open,
858         .read           = seq_read,
859         .llseek         = seq_lseek,
860         .release        = seq_release,
861 };
862
863 void proc_ide_create(void)
864 {
865         struct proc_dir_entry *entry;
866
867         if (!proc_ide_root)
868                 return;
869
870         create_proc_ide_interfaces();
871
872         entry = create_proc_entry("drivers", 0, proc_ide_root);
873         if (entry)
874                 entry->proc_fops = &ide_drivers_operations;
875 }
876
877 void proc_ide_destroy(void)
878 {
879         remove_proc_entry("ide/drivers", proc_ide_root);
880         destroy_proc_ide_interfaces();
881         remove_proc_entry("ide", NULL);
882 }