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