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