ide: move IDE settings handling to ide-proc.c
[linux-2.6] / drivers / ide / ide-proc.c
1 /*
2  *  linux/drivers/ide/ide-proc.c        Version 1.05    Mar 05, 2003
3  *
4  *  Copyright (C) 1997-1998     Mark Lord
5  *  Copyright (C) 2003          Red Hat <alan@redhat.com>
6  *
7  *  Some code was moved here from ide.c, see it for original copyrights.
8  */
9
10 /*
11  * This is the /proc/ide/ filesystem implementation.
12  *
13  * Drive/Driver settings can be retrieved by reading the drive's
14  * "settings" files.  e.g.    "cat /proc/ide0/hda/settings"
15  * To write a new value "val" into a specific setting "name", use:
16  *   echo "name:val" >/proc/ide/ide0/hda/settings
17  *
18  * Also useful, "cat /proc/ide0/hda/[identify, smart_values,
19  * smart_thresholds, capabilities]" will issue an IDENTIFY /
20  * PACKET_IDENTIFY / SMART_READ_VALUES / SMART_READ_THRESHOLDS /
21  * SENSE CAPABILITIES command to /dev/hda, and then dump out the
22  * returned data as 256 16-bit words.  The "hdparm" utility will
23  * be updated someday soon to use this mechanism.
24  *
25  */
26
27 #include <linux/module.h>
28
29 #include <asm/uaccess.h>
30 #include <linux/errno.h>
31 #include <linux/proc_fs.h>
32 #include <linux/stat.h>
33 #include <linux/mm.h>
34 #include <linux/pci.h>
35 #include <linux/ctype.h>
36 #include <linux/hdreg.h>
37 #include <linux/ide.h>
38 #include <linux/seq_file.h>
39
40 #include <asm/io.h>
41
42 static int proc_ide_read_imodel
43         (char *page, char **start, off_t off, int count, int *eof, void *data)
44 {
45         ide_hwif_t      *hwif = (ide_hwif_t *) data;
46         int             len;
47         const char      *name;
48
49         /*
50          * Neither ide_unknown nor ide_forced should be set at this point.
51          */
52         switch (hwif->chipset) {
53                 case ide_generic:       name = "generic";       break;
54                 case ide_pci:           name = "pci";           break;
55                 case ide_cmd640:        name = "cmd640";        break;
56                 case ide_dtc2278:       name = "dtc2278";       break;
57                 case ide_ali14xx:       name = "ali14xx";       break;
58                 case ide_qd65xx:        name = "qd65xx";        break;
59                 case ide_umc8672:       name = "umc8672";       break;
60                 case ide_ht6560b:       name = "ht6560b";       break;
61                 case ide_rz1000:        name = "rz1000";        break;
62                 case ide_trm290:        name = "trm290";        break;
63                 case ide_cmd646:        name = "cmd646";        break;
64                 case ide_cy82c693:      name = "cy82c693";      break;
65                 case ide_4drives:       name = "4drives";       break;
66                 case ide_pmac:          name = "mac-io";        break;
67                 case ide_au1xxx:        name = "au1xxx";        break;
68                 default:                name = "(unknown)";     break;
69         }
70         len = sprintf(page, "%s\n", name);
71         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
72 }
73
74 static int proc_ide_read_mate
75         (char *page, char **start, off_t off, int count, int *eof, void *data)
76 {
77         ide_hwif_t      *hwif = (ide_hwif_t *) data;
78         int             len;
79
80         if (hwif && hwif->mate && hwif->mate->present)
81                 len = sprintf(page, "%s\n", hwif->mate->name);
82         else
83                 len = sprintf(page, "(none)\n");
84         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
85 }
86
87 static int proc_ide_read_channel
88         (char *page, char **start, off_t off, int count, int *eof, void *data)
89 {
90         ide_hwif_t      *hwif = (ide_hwif_t *) data;
91         int             len;
92
93         page[0] = hwif->channel ? '1' : '0';
94         page[1] = '\n';
95         len = 2;
96         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
97 }
98
99 static int proc_ide_read_identify
100         (char *page, char **start, off_t off, int count, int *eof, void *data)
101 {
102         ide_drive_t     *drive = (ide_drive_t *)data;
103         int             len = 0, i = 0;
104         int             err = 0;
105
106         len = sprintf(page, "\n");
107
108         if (drive) {
109                 unsigned short *val = (unsigned short *) page;
110
111                 err = taskfile_lib_get_identify(drive, page);
112                 if (!err) {
113                         char *out = ((char *)page) + (SECTOR_WORDS * 4);
114                         page = out;
115                         do {
116                                 out += sprintf(out, "%04x%c",
117                                         le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
118                                 val += 1;
119                         } while (i < (SECTOR_WORDS * 2));
120                         len = out - page;
121                 }
122         }
123         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
124 }
125
126 /**
127  *      __ide_add_setting       -       add an ide setting option
128  *      @drive: drive to use
129  *      @name: setting name
130  *      @rw: true if the function is read write
131  *      @data_type: type of data
132  *      @min: range minimum
133  *      @max: range maximum
134  *      @mul_factor: multiplication scale
135  *      @div_factor: divison scale
136  *      @data: private data field
137  *      @set: setting
138  *      @auto_remove: setting auto removal flag
139  *
140  *      Removes the setting named from the device if it is present.
141  *      The function takes the settings_lock to protect against
142  *      parallel changes. This function must not be called from IRQ
143  *      context. Returns 0 on success or -1 on failure.
144  *
145  *      BUGS: This code is seriously over-engineered. There is also
146  *      magic about how the driver specific features are setup. If
147  *      a driver is attached we assume the driver settings are auto
148  *      remove.
149  */
150
151 static int __ide_add_setting(ide_drive_t *drive, const char *name, int rw, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set, int auto_remove)
152 {
153         ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
154
155         down(&ide_setting_sem);
156         while ((*p) && strcmp((*p)->name, name) < 0)
157                 p = &((*p)->next);
158         if ((setting = kzalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
159                 goto abort;
160         if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
161                 goto abort;
162         strcpy(setting->name, name);
163         setting->rw = rw;
164         setting->data_type = data_type;
165         setting->min = min;
166         setting->max = max;
167         setting->mul_factor = mul_factor;
168         setting->div_factor = div_factor;
169         setting->data = data;
170         setting->set = set;
171
172         setting->next = *p;
173         if (auto_remove)
174                 setting->auto_remove = 1;
175         *p = setting;
176         up(&ide_setting_sem);
177         return 0;
178 abort:
179         up(&ide_setting_sem);
180         kfree(setting);
181         return -1;
182 }
183
184 int ide_add_setting(ide_drive_t *drive, const char *name, int rw, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set)
185 {
186         return __ide_add_setting(drive, name, rw, data_type, min, max, mul_factor, div_factor, data, set, 1);
187 }
188
189 EXPORT_SYMBOL(ide_add_setting);
190
191 /**
192  *      __ide_remove_setting    -       remove an ide setting option
193  *      @drive: drive to use
194  *      @name: setting name
195  *
196  *      Removes the setting named from the device if it is present.
197  *      The caller must hold the setting semaphore.
198  */
199
200 static void __ide_remove_setting (ide_drive_t *drive, char *name)
201 {
202         ide_settings_t **p, *setting;
203
204         p = (ide_settings_t **) &drive->settings;
205
206         while ((*p) && strcmp((*p)->name, name))
207                 p = &((*p)->next);
208         if ((setting = (*p)) == NULL)
209                 return;
210
211         (*p) = setting->next;
212
213         kfree(setting->name);
214         kfree(setting);
215 }
216
217 /**
218  *      auto_remove_settings    -       remove driver specific settings
219  *      @drive: drive
220  *
221  *      Automatically remove all the driver specific settings for this
222  *      drive. This function may not be called from IRQ context. The
223  *      caller must hold ide_setting_sem.
224  */
225
226 static void auto_remove_settings (ide_drive_t *drive)
227 {
228         ide_settings_t *setting;
229 repeat:
230         setting = drive->settings;
231         while (setting) {
232                 if (setting->auto_remove) {
233                         __ide_remove_setting(drive, setting->name);
234                         goto repeat;
235                 }
236                 setting = setting->next;
237         }
238 }
239
240 /**
241  *      ide_find_setting_by_name        -       find a drive specific setting
242  *      @drive: drive to scan
243  *      @name: setting name
244  *
245  *      Scan's the device setting table for a matching entry and returns
246  *      this or NULL if no entry is found. The caller must hold the
247  *      setting semaphore
248  */
249
250 static ide_settings_t *ide_find_setting_by_name(ide_drive_t *drive, char *name)
251 {
252         ide_settings_t *setting = drive->settings;
253
254         while (setting) {
255                 if (strcmp(setting->name, name) == 0)
256                         break;
257                 setting = setting->next;
258         }
259         return setting;
260 }
261
262 /**
263  *      ide_read_setting        -       read an IDE setting
264  *      @drive: drive to read from
265  *      @setting: drive setting
266  *
267  *      Read a drive setting and return the value. The caller
268  *      must hold the ide_setting_sem when making this call.
269  *
270  *      BUGS: the data return and error are the same return value
271  *      so an error -EINVAL and true return of the same value cannot
272  *      be told apart
273  */
274
275 static int ide_read_setting(ide_drive_t *drive, ide_settings_t *setting)
276 {
277         int             val = -EINVAL;
278         unsigned long   flags;
279
280         if ((setting->rw & SETTING_READ)) {
281                 spin_lock_irqsave(&ide_lock, flags);
282                 switch(setting->data_type) {
283                         case TYPE_BYTE:
284                                 val = *((u8 *) setting->data);
285                                 break;
286                         case TYPE_SHORT:
287                                 val = *((u16 *) setting->data);
288                                 break;
289                         case TYPE_INT:
290                                 val = *((u32 *) setting->data);
291                                 break;
292                 }
293                 spin_unlock_irqrestore(&ide_lock, flags);
294         }
295         return val;
296 }
297
298 /**
299  *      ide_write_setting       -       read an IDE setting
300  *      @drive: drive to read from
301  *      @setting: drive setting
302  *      @val: value
303  *
304  *      Write a drive setting if it is possible. The caller
305  *      must hold the ide_setting_sem when making this call.
306  *
307  *      BUGS: the data return and error are the same return value
308  *      so an error -EINVAL and true return of the same value cannot
309  *      be told apart
310  *
311  *      FIXME:  This should be changed to enqueue a special request
312  *      to the driver to change settings, and then wait on a sema for completion.
313  *      The current scheme of polling is kludgy, though safe enough.
314  */
315
316 static int ide_write_setting(ide_drive_t *drive, ide_settings_t *setting, int val)
317 {
318         if (!capable(CAP_SYS_ADMIN))
319                 return -EACCES;
320         if (setting->set)
321                 return setting->set(drive, val);
322         if (!(setting->rw & SETTING_WRITE))
323                 return -EPERM;
324         if (val < setting->min || val > setting->max)
325                 return -EINVAL;
326         if (ide_spin_wait_hwgroup(drive))
327                 return -EBUSY;
328         switch (setting->data_type) {
329                 case TYPE_BYTE:
330                         *((u8 *) setting->data) = val;
331                         break;
332                 case TYPE_SHORT:
333                         *((u16 *) setting->data) = val;
334                         break;
335                 case TYPE_INT:
336                         *((u32 *) setting->data) = val;
337                         break;
338         }
339         spin_unlock_irq(&ide_lock);
340         return 0;
341 }
342
343 static int set_xfer_rate (ide_drive_t *drive, int arg)
344 {
345         int err;
346
347         if (arg < 0 || arg > 70)
348                 return -EINVAL;
349
350         err = ide_wait_cmd(drive,
351                         WIN_SETFEATURES, (u8) arg,
352                         SETFEATURES_XFER, 0, NULL);
353
354         if (!err && arg) {
355                 ide_set_xfer_rate(drive, (u8) arg);
356                 ide_driveid_update(drive);
357         }
358         return err;
359 }
360
361 /**
362  *      ide_add_generic_settings        -       generic ide settings
363  *      @drive: drive being configured
364  *
365  *      Add the generic parts of the system settings to the /proc files.
366  *      The caller must not be holding the ide_setting_sem.
367  */
368
369 void ide_add_generic_settings (ide_drive_t *drive)
370 {
371 /*
372  *                        drive         setting name            read/write access                               data type       min     max                             mul_factor      div_factor      data pointer                    set function
373  */
374         __ide_add_setting(drive,        "io_32bit",             drive->no_io_32bit ? SETTING_READ : SETTING_RW, TYPE_BYTE,      0,      1 + (SUPPORT_VLB_SYNC << 1),    1,              1,              &drive->io_32bit,               set_io_32bit,   0);
375         __ide_add_setting(drive,        "keepsettings",         SETTING_RW,                                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->keep_settings,          NULL,           0);
376         __ide_add_setting(drive,        "nice1",                SETTING_RW,                                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->nice1,                  NULL,           0);
377         __ide_add_setting(drive,        "pio_mode",             SETTING_WRITE,                                  TYPE_BYTE,      0,      255,                            1,              1,              NULL,                           set_pio_mode,   0);
378         __ide_add_setting(drive,        "unmaskirq",            drive->no_unmask ? SETTING_READ : SETTING_RW,   TYPE_BYTE,      0,      1,                              1,              1,              &drive->unmask,                 NULL,           0);
379         __ide_add_setting(drive,        "using_dma",            SETTING_RW,                                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->using_dma,              set_using_dma,  0);
380         __ide_add_setting(drive,        "init_speed",           SETTING_RW,                                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->init_speed,             NULL,           0);
381         __ide_add_setting(drive,        "current_speed",        SETTING_RW,                                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->current_speed,          set_xfer_rate,  0);
382         __ide_add_setting(drive,        "number",               SETTING_RW,                                     TYPE_BYTE,      0,      3,                              1,              1,              &drive->dn,                     NULL,           0);
383 }
384
385 static void proc_ide_settings_warn(void)
386 {
387         static int warned = 0;
388
389         if (warned)
390                 return;
391
392         printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
393                             "obsolete, and will be removed soon!\n");
394         warned = 1;
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         proc_ide_settings_warn();
406
407         down(&ide_setting_sem);
408         out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
409         out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
410         while(setting) {
411                 mul_factor = setting->mul_factor;
412                 div_factor = setting->div_factor;
413                 out += sprintf(out, "%-24s", setting->name);
414                 if ((rc = ide_read_setting(drive, setting)) >= 0)
415                         out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
416                 else
417                         out += sprintf(out, "%-16s", "write-only");
418                 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
419                 if (setting->rw & SETTING_READ)
420                         out += sprintf(out, "r");
421                 if (setting->rw & SETTING_WRITE)
422                         out += sprintf(out, "w");
423                 out += sprintf(out, "\n");
424                 setting = setting->next;
425         }
426         len = out - page;
427         up(&ide_setting_sem);
428         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
429 }
430
431 #define MAX_LEN 30
432
433 static int proc_ide_write_settings(struct file *file, const char __user *buffer,
434                                    unsigned long count, void *data)
435 {
436         ide_drive_t     *drive = (ide_drive_t *) data;
437         char            name[MAX_LEN + 1];
438         int             for_real = 0;
439         unsigned long   n;
440         ide_settings_t  *setting;
441         char *buf, *s;
442
443         if (!capable(CAP_SYS_ADMIN))
444                 return -EACCES;
445
446         proc_ide_settings_warn();
447
448         if (count >= PAGE_SIZE)
449                 return -EINVAL;
450
451         s = buf = (char *)__get_free_page(GFP_USER);
452         if (!buf)
453                 return -ENOMEM;
454
455         if (copy_from_user(buf, buffer, count)) {
456                 free_page((unsigned long)buf);
457                 return -EFAULT;
458         }
459
460         buf[count] = '\0';
461
462         /*
463          * Skip over leading whitespace
464          */
465         while (count && isspace(*s)) {
466                 --count;
467                 ++s;
468         }
469         /*
470          * Do one full pass to verify all parameters,
471          * then do another to actually write the new settings.
472          */
473         do {
474                 char *p = s;
475                 n = count;
476                 while (n > 0) {
477                         unsigned val;
478                         char *q = p;
479
480                         while (n > 0 && *p != ':') {
481                                 --n;
482                                 p++;
483                         }
484                         if (*p != ':')
485                                 goto parse_error;
486                         if (p - q > MAX_LEN)
487                                 goto parse_error;
488                         memcpy(name, q, p - q);
489                         name[p - q] = 0;
490
491                         if (n > 0) {
492                                 --n;
493                                 p++;
494                         } else
495                                 goto parse_error;
496
497                         val = simple_strtoul(p, &q, 10);
498                         n -= q - p;
499                         p = q;
500                         if (n > 0 && !isspace(*p))
501                                 goto parse_error;
502                         while (n > 0 && isspace(*p)) {
503                                 --n;
504                                 ++p;
505                         }
506
507                         down(&ide_setting_sem);
508                         setting = ide_find_setting_by_name(drive, name);
509                         if (!setting)
510                         {
511                                 up(&ide_setting_sem);
512                                 goto parse_error;
513                         }
514                         if (for_real)
515                                 ide_write_setting(drive, setting, val * setting->div_factor / setting->mul_factor);
516                         up(&ide_setting_sem);
517                 }
518         } while (!for_real++);
519         free_page((unsigned long)buf);
520         return count;
521 parse_error:
522         free_page((unsigned long)buf);
523         printk("proc_ide_write_settings(): parse error\n");
524         return -EINVAL;
525 }
526
527 int proc_ide_read_capacity
528         (char *page, char **start, off_t off, int count, int *eof, void *data)
529 {
530         int len = sprintf(page,"%llu\n", (long long)0x7fffffff);
531         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
532 }
533
534 EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
535
536 int proc_ide_read_geometry
537         (char *page, char **start, off_t off, int count, int *eof, void *data)
538 {
539         ide_drive_t     *drive = (ide_drive_t *) data;
540         char            *out = page;
541         int             len;
542
543         out += sprintf(out,"physical     %d/%d/%d\n",
544                         drive->cyl, drive->head, drive->sect);
545         out += sprintf(out,"logical      %d/%d/%d\n",
546                         drive->bios_cyl, drive->bios_head, drive->bios_sect);
547
548         len = out - page;
549         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
550 }
551
552 EXPORT_SYMBOL(proc_ide_read_geometry);
553
554 static int proc_ide_read_dmodel
555         (char *page, char **start, off_t off, int count, int *eof, void *data)
556 {
557         ide_drive_t     *drive = (ide_drive_t *) data;
558         struct hd_driveid *id = drive->id;
559         int             len;
560
561         len = sprintf(page, "%.40s\n",
562                 (id && id->model[0]) ? (char *)id->model : "(none)");
563         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
564 }
565
566 static int proc_ide_read_driver
567         (char *page, char **start, off_t off, int count, int *eof, void *data)
568 {
569         ide_drive_t     *drive = (ide_drive_t *) data;
570         struct device   *dev = &drive->gendev;
571         ide_driver_t    *ide_drv;
572         int             len;
573
574         if (dev->driver) {
575                 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
576                 len = sprintf(page, "%s version %s\n",
577                                 dev->driver->name, ide_drv->version);
578         } else
579                 len = sprintf(page, "ide-default version 0.9.newide\n");
580         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
581 }
582
583 static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
584 {
585         struct device *dev = &drive->gendev;
586         int ret = 1;
587         int err;
588
589         device_release_driver(dev);
590         /* FIXME: device can still be in use by previous driver */
591         strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
592         err = device_attach(dev);
593         if (err < 0)
594                 printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
595                         __FUNCTION__, err);
596         drive->driver_req[0] = 0;
597         if (dev->driver == NULL) {
598                 err = device_attach(dev);
599                 if (err < 0)
600                         printk(KERN_WARNING
601                                 "IDE: %s: device_attach(2) error: %d\n",
602                                 __FUNCTION__, err);
603         }
604         if (dev->driver && !strcmp(dev->driver->name, driver))
605                 ret = 0;
606
607         return ret;
608 }
609
610 static int proc_ide_write_driver
611         (struct file *file, const char __user *buffer, unsigned long count, void *data)
612 {
613         ide_drive_t     *drive = (ide_drive_t *) data;
614         char name[32];
615
616         if (!capable(CAP_SYS_ADMIN))
617                 return -EACCES;
618         if (count > 31)
619                 count = 31;
620         if (copy_from_user(name, buffer, count))
621                 return -EFAULT;
622         name[count] = '\0';
623         if (ide_replace_subdriver(drive, name))
624                 return -EINVAL;
625         return count;
626 }
627
628 static int proc_ide_read_media
629         (char *page, char **start, off_t off, int count, int *eof, void *data)
630 {
631         ide_drive_t     *drive = (ide_drive_t *) data;
632         const char      *media;
633         int             len;
634
635         switch (drive->media) {
636                 case ide_disk:  media = "disk\n";
637                                 break;
638                 case ide_cdrom: media = "cdrom\n";
639                                 break;
640                 case ide_tape:  media = "tape\n";
641                                 break;
642                 case ide_floppy:media = "floppy\n";
643                                 break;
644                 case ide_optical:media = "optical\n";
645                                 break;
646                 default:        media = "UNKNOWN\n";
647                                 break;
648         }
649         strcpy(page,media);
650         len = strlen(media);
651         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
652 }
653
654 static ide_proc_entry_t generic_drive_entries[] = {
655         { "driver",     S_IFREG|S_IRUGO,        proc_ide_read_driver,   proc_ide_write_driver },
656         { "identify",   S_IFREG|S_IRUSR,        proc_ide_read_identify, NULL },
657         { "media",      S_IFREG|S_IRUGO,        proc_ide_read_media,    NULL },
658         { "model",      S_IFREG|S_IRUGO,        proc_ide_read_dmodel,   NULL },
659         { "settings",   S_IFREG|S_IRUSR|S_IWUSR,proc_ide_read_settings, proc_ide_write_settings },
660         { NULL, 0, NULL, NULL }
661 };
662
663 static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
664 {
665         struct proc_dir_entry *ent;
666
667         if (!dir || !p)
668                 return;
669         while (p->name != NULL) {
670                 ent = create_proc_entry(p->name, p->mode, dir);
671                 if (!ent) return;
672                 ent->data = data;
673                 ent->read_proc = p->read_proc;
674                 ent->write_proc = p->write_proc;
675                 p++;
676         }
677 }
678
679 static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
680 {
681         if (!dir || !p)
682                 return;
683         while (p->name != NULL) {
684                 remove_proc_entry(p->name, dir);
685                 p++;
686         }
687 }
688
689 void ide_proc_register_driver(ide_drive_t *drive, ide_driver_t *driver)
690 {
691         ide_add_proc_entries(drive->proc, driver->proc, drive);
692 }
693
694 EXPORT_SYMBOL(ide_proc_register_driver);
695
696 /**
697  *      ide_proc_unregister_driver      -       remove driver specific data
698  *      @drive: drive
699  *      @driver: driver
700  *
701  *      Clean up the driver specific /proc files and IDE settings
702  *      for a given drive.
703  *
704  *      Takes ide_setting_sem and ide_lock.
705  *      Caller must hold none of the locks.
706  */
707
708 void ide_proc_unregister_driver(ide_drive_t *drive, ide_driver_t *driver)
709 {
710         unsigned long flags;
711
712         ide_remove_proc_entries(drive->proc, driver->proc);
713
714         down(&ide_setting_sem);
715         spin_lock_irqsave(&ide_lock, flags);
716         /*
717          * ide_setting_sem protects the settings list
718          * ide_lock protects the use of settings
719          *
720          * so we need to hold both, ide_settings_sem because we want to
721          * modify the settings list, and ide_lock because we cannot take
722          * a setting out that is being used.
723          *
724          * OTOH both ide_{read,write}_setting are only ever used under
725          * ide_setting_sem.
726          */
727         auto_remove_settings(drive);
728         spin_unlock_irqrestore(&ide_lock, flags);
729         up(&ide_setting_sem);
730 }
731
732 EXPORT_SYMBOL(ide_proc_unregister_driver);
733
734 static void create_proc_ide_drives(ide_hwif_t *hwif)
735 {
736         int     d;
737         struct proc_dir_entry *ent;
738         struct proc_dir_entry *parent = hwif->proc;
739         char name[64];
740
741         for (d = 0; d < MAX_DRIVES; d++) {
742                 ide_drive_t *drive = &hwif->drives[d];
743
744                 if (!drive->present)
745                         continue;
746                 if (drive->proc)
747                         continue;
748
749                 drive->proc = proc_mkdir(drive->name, parent);
750                 if (drive->proc)
751                         ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
752                 sprintf(name,"ide%d/%s", (drive->name[2]-'a')/2, drive->name);
753                 ent = proc_symlink(drive->name, proc_ide_root, name);
754                 if (!ent) return;
755         }
756 }
757
758 static void destroy_proc_ide_device(ide_hwif_t *hwif, ide_drive_t *drive)
759 {
760         if (drive->proc) {
761                 ide_remove_proc_entries(drive->proc, generic_drive_entries);
762                 remove_proc_entry(drive->name, proc_ide_root);
763                 remove_proc_entry(drive->name, hwif->proc);
764                 drive->proc = NULL;
765         }
766 }
767
768 static void destroy_proc_ide_drives(ide_hwif_t *hwif)
769 {
770         int     d;
771
772         for (d = 0; d < MAX_DRIVES; d++) {
773                 ide_drive_t *drive = &hwif->drives[d];
774                 if (drive->proc)
775                         destroy_proc_ide_device(hwif, drive);
776         }
777 }
778
779 static ide_proc_entry_t hwif_entries[] = {
780         { "channel",    S_IFREG|S_IRUGO,        proc_ide_read_channel,  NULL },
781         { "mate",       S_IFREG|S_IRUGO,        proc_ide_read_mate,     NULL },
782         { "model",      S_IFREG|S_IRUGO,        proc_ide_read_imodel,   NULL },
783         { NULL, 0, NULL, NULL }
784 };
785
786 void create_proc_ide_interfaces(void)
787 {
788         int     h;
789
790         for (h = 0; h < MAX_HWIFS; h++) {
791                 ide_hwif_t *hwif = &ide_hwifs[h];
792
793                 if (!hwif->present)
794                         continue;
795                 if (!hwif->proc) {
796                         hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
797                         if (!hwif->proc)
798                                 return;
799                         ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
800                 }
801                 create_proc_ide_drives(hwif);
802         }
803 }
804
805 EXPORT_SYMBOL(create_proc_ide_interfaces);
806
807 #ifdef CONFIG_BLK_DEV_IDEPCI
808 void ide_pci_create_host_proc(const char *name, get_info_t *get_info)
809 {
810         create_proc_info_entry(name, 0, proc_ide_root, get_info);
811 }
812
813 EXPORT_SYMBOL_GPL(ide_pci_create_host_proc);
814 #endif
815
816 void destroy_proc_ide_interface(ide_hwif_t *hwif)
817 {
818         if (hwif->proc) {
819                 destroy_proc_ide_drives(hwif);
820                 ide_remove_proc_entries(hwif->proc, hwif_entries);
821                 remove_proc_entry(hwif->name, proc_ide_root);
822                 hwif->proc = NULL;
823         }
824 }
825
826 static int proc_print_driver(struct device_driver *drv, void *data)
827 {
828         ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
829         struct seq_file *s = data;
830
831         seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
832
833         return 0;
834 }
835
836 static int ide_drivers_show(struct seq_file *s, void *p)
837 {
838         int err;
839
840         err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
841         if (err < 0)
842                 printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
843                         __FUNCTION__, err);
844         return 0;
845 }
846
847 static int ide_drivers_open(struct inode *inode, struct file *file)
848 {
849         return single_open(file, &ide_drivers_show, NULL);
850 }
851
852 static const struct file_operations ide_drivers_operations = {
853         .open           = ide_drivers_open,
854         .read           = seq_read,
855         .llseek         = seq_lseek,
856         .release        = single_release,
857 };
858
859 void proc_ide_create(void)
860 {
861         struct proc_dir_entry *entry;
862
863         if (!proc_ide_root)
864                 return;
865
866         create_proc_ide_interfaces();
867
868         entry = create_proc_entry("drivers", 0, proc_ide_root);
869         if (entry)
870                 entry->proc_fops = &ide_drivers_operations;
871 }
872
873 void proc_ide_destroy(void)
874 {
875         remove_proc_entry("drivers", proc_ide_root);
876         remove_proc_entry("ide", NULL);
877 }